Skip to content

Commit

Permalink
chore: Fix indefinite loop bug while fast sync (ethereum-optimism#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry authored Sep 20, 2023
1 parent 357b259 commit bbe499d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
3 changes: 2 additions & 1 deletion service/fastsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func (v *ValidatorInstance) FastSync(startHeight, endHeight uint64) (*FastSyncRe
break
}

startHeight = blocks[len(blocks)-1].Height + 1

// Note: not all the blocks in the range will have votes cast
// due to lack of voting power or public randomness, so we may
// have gaps during sync
Expand Down Expand Up @@ -75,7 +77,6 @@ func (v *ValidatorInstance) FastSync(startHeight, endHeight uint64) (*FastSyncRe
return nil, err
}

startHeight = blocks[len(blocks)-1].Height + 1
responses = append(responses, res)

v.logger.WithFields(logrus.Fields{
Expand Down
1 change: 0 additions & 1 deletion service/fastsync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func FuzzFastSync(f *testing.F) {
currentHeight := finalizedHeight + uint64(r.Int63n(10)+1)
startingBlock := &types.BlockInfo{Height: randomStartingHeight, LastCommitHash: testutil.GenRandomByteArray(r, 32)}
mockClientController := testutil.PrepareMockedClientController(t, r, randomStartingHeight, currentHeight)
mockClientController.EXPECT().QueryLatestFinalizedBlocks(gomock.Any()).Return(nil, nil)
app, storeValidator, cleanUp := newValidatorAppWithRegisteredValidator(t, r, mockClientController, randomStartingHeight)
defer cleanUp()
err := app.Start()
Expand Down
12 changes: 12 additions & 0 deletions service/validator_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,18 @@ func (v *ValidatorInstance) tryFastSync(targetBlock *types.BlockInfo) (*FastSync
if v.inSync.Load() {
return nil, fmt.Errorf("the validator %s is already in sync", v.GetBtcPkHex())
}

if v.GetLastCommittedHeight() <= v.GetLastVotedHeight() {
if err := v.SetLastProcessedHeight(targetBlock.Height); err != nil {
return nil, err
}
v.logger.WithFields(logrus.Fields{
"btc_pk_hex": v.GetBtcPkHex(),
"block_height": targetBlock.Height,
}).Debug("insufficient public randomness, jumping to the latest block")
return nil, nil
}

// get the last finalized height
lastFinalizedBlocks, err := v.cc.QueryLatestFinalizedBlocks(1)
if err != nil {
Expand Down

0 comments on commit bbe499d

Please sign in to comment.