Skip to content

Commit

Permalink
fix: use last finalized block on startup (#3737)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimjbrettj authored and timwu20 committed Apr 19, 2024
1 parent b4fd9b6 commit a6db41d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 38 deletions.
43 changes: 20 additions & 23 deletions dot/sync/chain_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,21 +238,17 @@ func (cs *chainSync) stop() error {
}
}

func (cs *chainSync) currentSyncInformations() (bestBlockHeader *types.Header,
isBootstrap bool, err error) {
bestBlockHeader, err = cs.blockState.BestBlockHeader()
if err != nil {
return nil, false, fmt.Errorf("getting best block header: %w", err)
}

func (cs *chainSync) isBootstrapSync(currentBlockNumber uint) bool {
syncTarget := cs.peerViewSet.getTarget()
bestBlockNumber := bestBlockHeader.Number
isBootstrap = bestBlockNumber+network.MaxBlocksInResponse < syncTarget
return bestBlockHeader, isBootstrap, nil
return currentBlockNumber+network.MaxBlocksInResponse < syncTarget
}

func (cs *chainSync) bootstrapSync() {
defer cs.wg.Done()
currentBlock, err := cs.blockState.GetHighestFinalisedHeader()
if err != nil {
panic("cannot find highest finalised header")
}

for {
select {
Expand All @@ -262,18 +258,18 @@ func (cs *chainSync) bootstrapSync() {
default:
}

bestBlockHeader, isBootstrap, err := cs.currentSyncInformations()
if err != nil {
logger.Criticalf("ending bootstrap sync, getting current sync info: %s", err)
return
}

isBootstrap := cs.isBootstrapSync(currentBlock.Number)
if isBootstrap {
cs.workerPool.useConnectedPeers()
err = cs.requestMaxBlocksFrom(bestBlockHeader, networkInitialSync)
err = cs.requestMaxBlocksFrom(currentBlock, networkInitialSync)
if err != nil {
logger.Errorf("requesting max blocks from best block header: %s", err)
}

currentBlock, err = cs.blockState.BestBlockHeader()
if err != nil {
logger.Errorf("getting best block header: %v", err)
}
} else {
// we are less than 128 blocks behind the target we can use tip sync
cs.syncMode.Store(tip)
Expand All @@ -297,11 +293,12 @@ func (cs *chainSync) onBlockAnnounceHandshake(who peer.ID, bestHash common.Hash,
return nil
}

_, isBootstrap, err := cs.currentSyncInformations()
bestBlockHeader, err := cs.blockState.BestBlockHeader()
if err != nil {
return fmt.Errorf("getting current sync info: %w", err)
return err
}

isBootstrap := cs.isBootstrapSync(bestBlockHeader.Number)
if !isBootstrap {
return nil
}
Expand All @@ -318,7 +315,6 @@ func (cs *chainSync) onBlockAnnounceHandshake(who peer.ID, bestHash common.Hash,

func (cs *chainSync) onBlockAnnounce(announced announcedBlock) error {
// TODO: https://github.com/ChainSafe/gossamer/issues/3432
cs.workerPool.fromBlockAnnounce(announced.who)
if cs.pendingBlocks.hasBlock(announced.header.Hash()) {
return fmt.Errorf("%w: block #%d (%s)",
errAlreadyInDisjointSet, announced.header.Number, announced.header.Hash())
Expand All @@ -333,12 +329,13 @@ func (cs *chainSync) onBlockAnnounce(announced announcedBlock) error {
return nil
}

bestBlockHeader, isFarFromTarget, err := cs.currentSyncInformations()
bestBlockHeader, err := cs.blockState.BestBlockHeader()
if err != nil {
return fmt.Errorf("getting current sync info: %w", err)
return fmt.Errorf("getting best block header: %w", err)
}

if !isFarFromTarget {
isBootstrap := cs.isBootstrapSync(bestBlockHeader.Number)
if !isBootstrap {
return cs.requestAnnouncedBlock(bestBlockHeader, announced)
}

Expand Down
30 changes: 15 additions & 15 deletions dot/sync/chain_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,16 +269,16 @@ func Test_chainSync_onBlockAnnounceHandshake_tipModeNeedToCatchup(t *testing.T)
blockStateMock.EXPECT().
GetHighestFinalisedHeader().
Return(block1AnnounceHeader, nil).
Times(1)
Times(3)

expectedRequest := network.NewAscendingBlockRequests(
block1AnnounceHeader.Number+1,
block2AnnounceHeader.Number, network.BootstrapRequestData)

networkMock := NewMockNetwork(ctrl)
networkMock.EXPECT().Peers().Return([]common.PeerInfo{}).
Times(1)
networkMock.EXPECT().AllConnectedPeersIDs().Return([]peer.ID{})
Times(2)
networkMock.EXPECT().AllConnectedPeersIDs().Return([]peer.ID{}).Times(2)

firstMockedResponse := createSuccesfullBlockResponse(t, block1AnnounceHeader.Hash(), 2, 128)
latestItemFromMockedResponse := firstMockedResponse.BlockData[len(firstMockedResponse.BlockData)-1]
Expand All @@ -293,15 +293,15 @@ func Test_chainSync_onBlockAnnounceHandshake_tipModeNeedToCatchup(t *testing.T)
responsePtr := response.(*network.BlockResponseMessage)
*responsePtr = *firstMockedResponse
return nil
})
}).Times(2)

requestMaker.EXPECT().
Do(somePeer, expectedRequest[1], &network.BlockResponseMessage{}).
DoAndReturn(func(_, _, response any) any {
responsePtr := response.(*network.BlockResponseMessage)
*responsePtr = *secondMockedResponse
return nil
})
}).Times(2)

babeVerifierMock := NewMockBabeVerifier(ctrl)
storageStateMock := NewMockStorageState(ctrl)
Expand Down Expand Up @@ -1334,39 +1334,39 @@ func ensureSuccessfulBlockImportFlow(t *testing.T, parentHeader *types.Header,
previousHeader = blocksReceived[idx-1].Header
}

mockBlockState.EXPECT().GetHeader(blockData.Header.ParentHash).Return(previousHeader, nil)
mockStorageState.EXPECT().Lock()
mockStorageState.EXPECT().Unlock()
mockBlockState.EXPECT().GetHeader(blockData.Header.ParentHash).Return(previousHeader, nil).AnyTimes()
mockStorageState.EXPECT().Lock().AnyTimes()
mockStorageState.EXPECT().Unlock().AnyTimes()

emptyTrieState := storage.NewTrieState(trie.NewEmptyTrie())
parentStateRoot := previousHeader.StateRoot
mockStorageState.EXPECT().TrieState(&parentStateRoot).
Return(emptyTrieState, nil)
Return(emptyTrieState, nil).AnyTimes()

ctrl := gomock.NewController(t)
mockRuntimeInstance := NewMockInstance(ctrl)
mockBlockState.EXPECT().GetRuntime(previousHeader.Hash()).
Return(mockRuntimeInstance, nil)
Return(mockRuntimeInstance, nil).AnyTimes()

expectedBlock := &types.Block{
Header: *blockData.Header,
Body: *blockData.Body,
}

mockRuntimeInstance.EXPECT().SetContextStorage(emptyTrieState)
mockRuntimeInstance.EXPECT().SetContextStorage(emptyTrieState).AnyTimes()
mockRuntimeInstance.EXPECT().ExecuteBlock(expectedBlock).
Return(nil, nil)
Return(nil, nil).AnyTimes()

mockImportHandler.EXPECT().HandleBlockImport(expectedBlock, emptyTrieState, announceBlock).
Return(nil)
Return(nil).AnyTimes()

blockHash := blockData.Header.Hash()
expectedTelemetryMessage := telemetry.NewBlockImport(
&blockHash,
blockData.Header.Number,
"NetworkInitialSync")
mockTelemetry.EXPECT().SendMessage(expectedTelemetryMessage)
mockBlockState.EXPECT().CompareAndSetBlockData(blockData).Return(nil)
mockTelemetry.EXPECT().SendMessage(expectedTelemetryMessage).AnyTimes()
mockBlockState.EXPECT().CompareAndSetBlockData(blockData).Return(nil).AnyTimes()
}
}

Expand Down

0 comments on commit a6db41d

Please sign in to comment.