Skip to content

Commit

Permalink
abstract block tracker for tracker (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
igorcrevar authored Sep 19, 2023
1 parent b3ad0a0 commit 1000cf3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
6 changes: 5 additions & 1 deletion blocktracker/blocktracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (b *BlockTracker) Subscribe() chan *BlockEvent {
}

func (b *BlockTracker) AcquireLock() Lock {
return Lock{lock: &b.blocksLock}
return NewLock(&b.blocksLock)
}

func (t *BlockTracker) Init() (err error) {
Expand Down Expand Up @@ -425,6 +425,10 @@ type Lock struct {
lock *sync.Mutex
}

func NewLock(lock *sync.Mutex) Lock {
return Lock{lock: lock}
}

func (l *Lock) Lock() {
l.Locked = true
l.lock.Lock()
Expand Down
24 changes: 19 additions & 5 deletions tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,24 @@ var (
)

const (
defaultMaxBlockBacklog = 10
defaultBatchSize = 100
defaultBatchSize = 100
)

// BlockTracking defines interface for block tracker implementations
type BlockTracking interface {
BlocksBlocked() []*ethgo.Block
AddBlockLocked(block *ethgo.Block) error
MaxBlockBacklog() uint64
Init() error
Start() error
Close() error
Subscribe() chan *blocktracker.BlockEvent
AcquireLock() blocktracker.Lock
LastBlocked() *ethgo.Block
HandleBlockEvent(block *ethgo.Block) (*blocktracker.BlockEvent, error)
Len() int
}

// FilterConfig is a tracker filter configuration
type FilterConfig struct {
Address []ethgo.Address `json:"address"`
Expand Down Expand Up @@ -83,7 +97,7 @@ func (f *FilterConfig) getFilterSearch() *ethgo.LogFilter {
// Config is the configuration of the tracker
type Config struct {
BatchSize uint64
BlockTracker *blocktracker.BlockTracker // move to interface
BlockTracker BlockTracking
EtherscanAPIKey string
Filter *FilterConfig
Store store.Store
Expand All @@ -97,7 +111,7 @@ func WithBatchSize(b uint64) ConfigOption {
}
}

func WithBlockTracker(b *blocktracker.BlockTracker) ConfigOption {
func WithBlockTracker(b BlockTracking) ConfigOption {
return func(c *Config) {
c.BlockTracker = b
}
Expand Down Expand Up @@ -148,7 +162,7 @@ type Tracker struct {
store store.Store
entry store.Entry
preSyncOnce sync.Once
blockTracker *blocktracker.BlockTracker
blockTracker BlockTracking
synced int32
BlockCh chan *blocktracker.BlockEvent
ReadyCh chan struct{}
Expand Down

0 comments on commit 1000cf3

Please sign in to comment.