Skip to content

Commit

Permalink
chore: use GossipSuccessValue to good block announced received
Browse files Browse the repository at this point in the history
  • Loading branch information
EclesioMeloJunior committed Sep 17, 2024
1 parent 9dff75a commit cc71723
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
4 changes: 2 additions & 2 deletions dot/sync/fullsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ func (f *FullSyncStrategy) OnBlockAnnounce(from peer.ID, msg *network.BlockAnnou
return true, &Change{
who: from,
rep: peerset.ReputationChange{
Value: peerset.NotRelevantBlockAnnounceValue,
Reason: peerset.NotRelevantBlockAnnounceReason,
Value: peerset.GossipSuccessValue,
Reason: peerset.GossipSuccessReason,
},
}, nil
}
Expand Down
74 changes: 71 additions & 3 deletions dot/sync/fullsync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func TestFullSyncIsFinished(t *testing.T) {
}

func TestFullSyncBlockAnnounce(t *testing.T) {
t.Run("announce_a_block_without_any_commom_ancestor", func(t *testing.T) {
t.Run("announce_a_far_block_without_any_commom_ancestor", func(t *testing.T) {
highestFinalizedHeader := &types.Header{
ParentHash: common.BytesToHash([]byte{0}),
StateRoot: common.BytesToHash([]byte{3, 3, 3, 3}),
Expand All @@ -307,8 +307,12 @@ func TestFullSyncBlockAnnounce(t *testing.T) {
Return(highestFinalizedHeader, nil)

mockBlockState.EXPECT().
HasHeader(gomock.AnyOf(common.Hash{})).
Return(false, nil)
BestBlockHeader().
Return(highestFinalizedHeader, nil)

// mockBlockState.EXPECT().
// HasHeader(gomock.AnyOf(common.Hash{})).
// Return(false, nil)

fsCfg := &FullSyncConfig{
BlockState: mockBlockState,
Expand All @@ -327,6 +331,8 @@ func TestFullSyncBlockAnnounce(t *testing.T) {
err := fs.OnBlockAnnounceHandshake(firstPeer, firstHandshake)
require.NoError(t, err)

// still far from aproaching the calculated target
// then we can ignore the block announce
firstBlockAnnounce := &network.BlockAnnounceMessage{
ParentHash: common.BytesToHash([]byte{0, 1, 2}),
Number: 1024,
Expand All @@ -339,5 +345,67 @@ func TestFullSyncBlockAnnounce(t *testing.T) {
_, rep, err := fs.OnBlockAnnounce(firstPeer, firstBlockAnnounce)
require.NoError(t, err)
require.Nil(t, rep)
require.Zero(t, fs.requestQueue.Len())
})

t.Run("announce_closer_valid_block_without_any_commom_ancestor", func(t *testing.T) {
highestFinalizedHeader := &types.Header{
ParentHash: common.BytesToHash([]byte{0}),
StateRoot: common.BytesToHash([]byte{3, 3, 3, 3}),
ExtrinsicsRoot: common.BytesToHash([]byte{4, 4, 4, 4}),
Number: 0,
Digest: types.NewDigest(),
}

ctrl := gomock.NewController(t)
mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().IsPaused().Return(false)
mockBlockState.EXPECT().
GetHighestFinalisedHeader().
Return(highestFinalizedHeader, nil)

mockBlockState.EXPECT().
BestBlockHeader().
Return(highestFinalizedHeader, nil)

mockBlockState.EXPECT().
HasHeader(gomock.AssignableToTypeOf(common.Hash{})).
Return(false, nil)

fsCfg := &FullSyncConfig{
BlockState: mockBlockState,
}

fs := NewFullSyncStrategy(fsCfg)

firstPeer := peer.ID("fst-peer")
firstHandshake := &network.BlockAnnounceHandshake{
Roles: 1,
BestBlockNumber: 17,
BestBlockHash: common.BytesToHash([]byte{0, 1, 2}),
GenesisHash: common.BytesToHash([]byte{1, 1, 1, 1}),
}

err := fs.OnBlockAnnounceHandshake(firstPeer, firstHandshake)
require.NoError(t, err)

// still far from aproaching the calculated target
// then we can ignore the block announce
firstBlockAnnounce := &network.BlockAnnounceMessage{
ParentHash: common.BytesToHash([]byte{0, 1, 2}),
Number: 17,
StateRoot: common.BytesToHash([]byte{3, 3, 3, 3}),
ExtrinsicsRoot: common.BytesToHash([]byte{4, 4, 4, 4}),
Digest: types.NewDigest(),
BestBlock: true,
}

// the announced block 17 is not far from our best block (0) then
// we will consider it and start a ancestor search
_, rep, err := fs.OnBlockAnnounce(firstPeer, firstBlockAnnounce)
require.NoError(t, err)
require.Nil(t, rep)
require.Equal(t, 1, fs.requestQueue.Len())
})

}

0 comments on commit cc71723

Please sign in to comment.