Skip to content

Commit

Permalink
chore: fix sync test
Browse files Browse the repository at this point in the history
  • Loading branch information
EclesioMeloJunior committed Sep 13, 2024
1 parent 8f86d9b commit ed6c755
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 32 deletions.
2 changes: 1 addition & 1 deletion dot/network/messages/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func NewAscendingBlockRequests(startNumber, targetNumber uint32, requestedData b
diff := targetNumber - (startNumber - 1)

// start and end block are the same, just request 1 block
if diff == 0 {
if diff == 1 {
return []*BlockRequestMessage{
NewBlockRequest(*variadic.Uint32OrHashFrom(startNumber), 1, requestedData, Ascending),
}
Expand Down
17 changes: 10 additions & 7 deletions dot/sync/fullsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,14 @@ func (f *FullSyncStrategy) NextActions() ([]*syncTask, error) {
f.startedAt = time.Now()
f.syncedBlocks = 0

messagesToSend := []*messages.BlockRequestMessage{}
for f.requestQueue.Len() > 0 {
reqsFromQueue := []*messages.BlockRequestMessage{}
for i := 0; i < int(f.numOfTasks); i++ {

Check failure on line 103 in dot/sync/fullsync.go

View workflow job for this annotation

GitHub Actions / linting

unnecessary conversion (unconvert)
msg, ok := f.requestQueue.PopFront()
if ok {
messagesToSend = append(messagesToSend, msg)
if !ok {
break
}

reqsFromQueue = append(reqsFromQueue, msg)
}

currentTarget := f.peers.getTarget()
Expand All @@ -117,11 +119,11 @@ func (f *FullSyncStrategy) NextActions() ([]*syncTask, error) {
// in the node's pov we are not legging behind so there's nothing to do
// or we didn't receive block announces, so lets ask for more blocks
if uint32(bestBlockHeader.Number) >= currentTarget {
return f.createTasks(messagesToSend), nil
return f.createTasks(reqsFromQueue), nil
}

startRequestAt := bestBlockHeader.Number + 1
targetBlockNumber := startRequestAt + maxRequestsAllowed*127
targetBlockNumber := startRequestAt + uint(f.numOfTasks)*127

if targetBlockNumber > uint(currentTarget) {
targetBlockNumber = uint(currentTarget)
Expand All @@ -130,8 +132,9 @@ func (f *FullSyncStrategy) NextActions() ([]*syncTask, error) {
ascendingBlockRequests := messages.NewAscendingBlockRequests(
uint32(startRequestAt), uint32(targetBlockNumber),
messages.BootstrapRequestData)
reqsFromQueue = append(reqsFromQueue, ascendingBlockRequests...)

return f.createTasks(ascendingBlockRequests), nil
return f.createTasks(reqsFromQueue), nil
}

func (f *FullSyncStrategy) createTasks(requests []*messages.BlockRequestMessage) []*syncTask {
Expand Down
66 changes: 42 additions & 24 deletions dot/sync/fullsync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestFullSyncNextActions(t *testing.T) {
task, err := fs.NextActions()
require.NoError(t, err)

require.Len(t, task, 1)
require.Len(t, task, int(maxRequestsAllowed))
request := task[0].request.(*messages.BlockRequestMessage)
require.Equal(t, uint32(1), request.StartingBlock.Uint32())
require.Equal(t, uint32(128), *request.Max)
Expand All @@ -85,51 +85,61 @@ func TestFullSyncNextActions(t *testing.T) {
expectedQueueLen int
expectedTasks []*messages.BlockRequestMessage
}{
"should_have_one_from_request_queue": {
"should_get_all_from_request_queue": {
setupRequestQueue: func(t *testing.T) *requestsQueue[*messages.BlockRequestMessage] {
request := messages.NewAscendingBlockRequests(
129, 129+127,
messages.BootstrapRequestData)
// insert a task to retrieve the block body of a single block
request := messages.NewAscendingBlockRequests(129, 129, messages.RequestedDataBody)
require.Len(t, request, 1)

rq := &requestsQueue[*messages.BlockRequestMessage]{queue: list.New()}
for _, req := range request {
rq.PushBack(req)
}
rq.PushBack(request[0])
return rq
},
expectedQueueLen: 0,
expectedTasks: []*messages.BlockRequestMessage{
{
RequestedData: messages.BootstrapRequestData,
RequestedData: messages.RequestedDataBody,
StartingBlock: *variadic.Uint32OrHashFrom(uint32(129)),
Direction: messages.Ascending,
Max: refTo(128),
Max: refTo(1),
},
{
RequestedData: messages.BootstrapRequestData,
StartingBlock: *variadic.Uint32OrHashFrom(uint32(1)),
Direction: messages.Ascending,
Max: refTo(127),
},
},
},
// creating a amount of 4 requests, but since we have a max num of
// request set to 2 (see FullSyncConfig) we should only have 2 tasks
"four_items_on_queue_should_pop_only_one": {
"should_remain_1_in_request_queue": {
setupRequestQueue: func(t *testing.T) *requestsQueue[*messages.BlockRequestMessage] {
request := messages.NewAscendingBlockRequests(
129, 129+(4*127),
messages.BootstrapRequestData)
require.Len(t, request, 4)

rq := &requestsQueue[*messages.BlockRequestMessage]{queue: list.New()}
for _, req := range request {
rq.PushBack(req)
}

fstReqByHash := messages.NewBlockRequest(
*variadic.Uint32OrHashFrom(common.BytesToHash([]byte{0, 1, 1, 2})),
1, messages.RequestedDataBody, messages.Ascending)
rq.PushBack(fstReqByHash)

sndReqByHash := messages.NewBlockRequest(
*variadic.Uint32OrHashFrom(common.BytesToHash([]byte{1, 2, 2, 4})),
1, messages.RequestedDataBody, messages.Ascending)
rq.PushBack(sndReqByHash)

return rq
},
expectedQueueLen: 3,
expectedQueueLen: 1,
expectedTasks: []*messages.BlockRequestMessage{
{
RequestedData: messages.RequestedDataBody,
StartingBlock: *variadic.Uint32OrHashFrom(common.BytesToHash([]byte{0, 1, 1, 2})),
Direction: messages.Ascending,
Max: refTo(1),
},
{
RequestedData: messages.BootstrapRequestData,
StartingBlock: *variadic.Uint32OrHashFrom(uint32(129)),
StartingBlock: *variadic.Uint32OrHashFrom(uint32(1)),
Direction: messages.Ascending,
Max: refTo(128),
Max: refTo(127),
},
},
},
Expand All @@ -140,6 +150,14 @@ func TestFullSyncNextActions(t *testing.T) {
t.Run(tname, func(t *testing.T) {
fs := NewFullSyncStrategy(&FullSyncConfig{})
fs.requestQueue = tt.setupRequestQueue(t)
fs.numOfTasks = 1

ctrl := gomock.NewController(t)
mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().
BestBlockHeader().
Return(&types.Header{Number: 0}, nil)
fs.blockState = mockBlockState

// introduce a peer and a target
err := fs.OnBlockAnnounceHandshake(peer.ID("peer-A"), &network.BlockAnnounceHandshake{
Expand Down

0 comments on commit ed6c755

Please sign in to comment.