Skip to content

Commit

Permalink
Fix tracker errors after a block reorganization (#247)
Browse files Browse the repository at this point in the history
* Fix blocktraceker.go and tracker.go after block reorganization

* func (t *BlockTracker) Init() fix

* include getBlockByNumber() into Tracker

---------

Co-authored-by: Igor Crevar <crewce@gmail.com>
  • Loading branch information
Nemanja0x and igorcrevar committed Jun 20, 2023
1 parent 8aa9d5b commit bcb1d27
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
8 changes: 8 additions & 0 deletions blocktracker/blocktracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ func (t *BlockTracker) Init() (err error) {
}
block, err = t.provider.GetBlockByHash(block.ParentHash, false)
if err != nil {
return
} else if block == nil {
// if block does not exist (for example reorg happened) GetBlockByHash will return nil, nil
err = fmt.Errorf("block with hash %s not found", block.ParentHash)

return
}
}
Expand Down Expand Up @@ -231,6 +236,9 @@ func (t *BlockTracker) handleReconcileImpl(block *ethgo.Block) ([]*ethgo.Block,

parent, err := t.provider.GetBlockByHash(block.ParentHash, false)
if err != nil {
return nil, -1, fmt.Errorf("parent with hash retrieving error: %w", err)
} else if parent == nil {
// if block does not exist (for example reorg happened) GetBlockByHash will return nil, nil
return nil, -1, fmt.Errorf("parent with hash %s not found", block.ParentHash)
}

Expand Down
30 changes: 24 additions & 6 deletions tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,16 @@ func (t *Tracker) findAncestor(block, pivot *ethgo.Block) (uint64, error) {
block, err = t.provider.GetBlockByHash(block.ParentHash, false)
if err != nil {
return 0, err
} else if block == nil {
// if block does not exist (for example reorg happened) GetBlockByNumber will return nil, nil
return 0, fmt.Errorf("block with hash %s not found", block.ParentHash)
}
pivot, err = t.provider.GetBlockByHash(pivot.ParentHash, false)
if err != nil {
return 0, err
} else if pivot == nil {
// if block does not exist (for example reorg happened) GetBlockByNumber will return nil, nil
return 0, fmt.Errorf("block/pivot with hash %s not found", pivot.ParentHash)
}
}
return 0, fmt.Errorf("the reorg is bigger than maxBlockBacklog %d", t.blockTracker.MaxBlockBacklog())
Expand Down Expand Up @@ -383,7 +389,7 @@ START:
t.emitLogs(EventAdd, logs)

// update the last block entry
block, err := t.provider.GetBlockByNumber(ethgo.BlockNumber(dst), false)
block, err := t.getBlockByNumber(dst)
if err != nil {
return err
}
Expand Down Expand Up @@ -418,7 +424,7 @@ func (t *Tracker) preSyncCheck() error {
}

func (t *Tracker) preSyncCheckImpl() error {
rGenesis, err := t.provider.GetBlockByNumber(0, false)
rGenesis, err := t.getBlockByNumber(0)
if err != nil {
return err
}
Expand Down Expand Up @@ -456,7 +462,7 @@ func (t *Tracker) preSyncCheckImpl() error {
func (t *Tracker) fastTrack(filterConfig *FilterConfig) (*ethgo.Block, error) {
// Try to use first the user provided block if any
if filterConfig.Start != 0 {
bb, err := t.provider.GetBlockByNumber(ethgo.BlockNumber(filterConfig.Start), false)
bb, err := t.getBlockByNumber(filterConfig.Start)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -518,7 +524,7 @@ func (t *Tracker) fastTrack(filterConfig *FilterConfig) (*ethgo.Block, error) {
}
}

bb, err := t.provider.GetBlockByNumber(ethgo.BlockNumber(minBlock-1), false)
bb, err := t.getBlockByNumber(minBlock - 1)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -650,7 +656,7 @@ func (t *Tracker) syncImpl(ctx context.Context) error {
return fmt.Errorf("store is more advanced than the chain")
}

pivot, err := t.provider.GetBlockByNumber(ethgo.BlockNumber(last.Number), false)
pivot, err := t.getBlockByNumber(last.Number)
if err != nil {
return err
}
Expand All @@ -674,7 +680,7 @@ func (t *Tracker) syncImpl(ctx context.Context) error {
}
t.emitLogs(EventDel, logs)

last, err = t.provider.GetBlockByNumber(ethgo.BlockNumber(ancestor), false)
last, err = t.getBlockByNumber(ancestor)
if err != nil {
return err
}
Expand Down Expand Up @@ -843,6 +849,18 @@ func (t *Tracker) doFilter(added []*ethgo.Block, removed []*ethgo.Block) (*Event
return evnt, nil
}

func (t *Tracker) getBlockByNumber(blockNumber uint64) (*ethgo.Block, error) {
block, err := t.provider.GetBlockByNumber(ethgo.BlockNumber(blockNumber), false)
if err != nil {
return nil, err
} else if block == nil {
// if block does not exist (for example reorg happened) GetBlockByNumber will return nil, nil
return nil, fmt.Errorf("block with number %d not found", blockNumber)
}

return block, nil
}

// EventType is the type of the event
type EventType int

Expand Down

0 comments on commit bcb1d27

Please sign in to comment.