Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

abstract block tracker for tracker #256

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading