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

Fix tracker errors after a block reorganization #247

Merged
merged 3 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
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 {
ferranbt marked this conversation as resolved.
Show resolved Hide resolved
// 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 := getBlockByNumber(t.provider, 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 := getBlockByNumber(t.provider, 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 := getBlockByNumber(t.provider, 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 := getBlockByNumber(t.provider, 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 := getBlockByNumber(t.provider, 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 = getBlockByNumber(t.provider, ancestor)
if err != nil {
return err
}
Expand Down Expand Up @@ -882,3 +888,15 @@ func parseUint64orHex(str string) (uint64, error) {
}
return strconv.ParseUint(str, base, 64)
}

func getBlockByNumber(provider Provider, blockNumber uint64) (*ethgo.Block, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add this as a function of Tracker.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed: 4b3217e

block, err := 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
}
Loading