2023-01-05 12:45:09 +01:00
|
|
|
package pipeline
|
|
|
|
|
|
|
|
import (
|
2023-01-11 09:13:59 +01:00
|
|
|
"context"
|
|
|
|
|
2023-01-19 09:42:59 +01:00
|
|
|
"github.com/grassrootseconomics/cic-chain-events/internal/filter"
|
2023-01-05 12:45:09 +01:00
|
|
|
"github.com/grassrootseconomics/cic-chain-events/internal/store"
|
2023-01-14 10:20:16 +01:00
|
|
|
"github.com/grassrootseconomics/cic-chain-events/pkg/fetch"
|
2023-01-05 12:45:09 +01:00
|
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
"github.com/zerodha/logf"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PipelineOpts struct {
|
|
|
|
BlockFetcher fetch.Fetch
|
|
|
|
Filters []filter.Filter
|
|
|
|
Logg logf.Logger
|
|
|
|
Store store.Store[pgx.Rows]
|
|
|
|
}
|
|
|
|
|
|
|
|
type Pipeline struct {
|
|
|
|
fetch fetch.Fetch
|
|
|
|
filters []filter.Filter
|
|
|
|
logg logf.Logger
|
|
|
|
store store.Store[pgx.Rows]
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPipeline(o PipelineOpts) *Pipeline {
|
|
|
|
return &Pipeline{
|
|
|
|
fetch: o.BlockFetcher,
|
|
|
|
filters: o.Filters,
|
|
|
|
logg: o.Logg,
|
|
|
|
store: o.Store,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 09:13:59 +01:00
|
|
|
// Run is the task executor which runs in its own goroutine and does the following:
|
|
|
|
// 1. Fetches the block and all transactional data
|
|
|
|
// 2. Passes the block through all filters
|
|
|
|
// 3. Commits the block to store as successfully processed
|
|
|
|
//
|
|
|
|
// Note:
|
2023-01-11 10:29:16 +01:00
|
|
|
// - Blocks are processed atomically, a failure in-between will process the block from the start
|
2023-01-11 09:13:59 +01:00
|
|
|
// - Therefore, any side effect/event sink in the filter should support dedup
|
|
|
|
func (md *Pipeline) Run(ctx context.Context, blockNumber uint64) error {
|
|
|
|
fetchResp, err := md.fetch.Block(ctx, blockNumber)
|
2023-01-05 12:45:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tx := range fetchResp.Data.Block.Transactions {
|
|
|
|
for _, filter := range md.filters {
|
2023-02-24 11:28:30 +01:00
|
|
|
next, err := filter.Execute(ctx, tx)
|
2023-01-05 12:45:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !next {
|
2023-01-06 12:32:20 +01:00
|
|
|
break
|
2023-01-05 12:45:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 09:13:59 +01:00
|
|
|
if err := md.store.CommitBlock(ctx, blockNumber); err != nil {
|
2023-01-05 12:45:09 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|