Sort list before processing batched blocks (#2531)

This commit is contained in:
Preston Van Loon 2019-05-08 00:27:00 -04:00 committed by Nishant Das
parent 76881fd1ae
commit e5cb1db5bc
2 changed files with 8 additions and 0 deletions

View File

@ -128,6 +128,9 @@ func TestProcessingBatchedBlocks_OK(t *testing.T) {
Slot: params.BeaconConfig().GenesisSlot + uint64(i),
}
}
// edge case: handle out of order block list. Specifically with the highest
// block first. This is swapping the first and last blocks in the list.
batchedBlocks[0], batchedBlocks[batchSize-1] = batchedBlocks[batchSize-1], batchedBlocks[0]
msg := p2p.Message{
Ctx: context.Background(),

View File

@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"sort"
"strings"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
@ -67,6 +68,10 @@ func (s *InitialSync) processBatchedBlocks(msg p2p.Message) {
}
log.Debug("Processing batched block response")
// Sort batchBlocks in ascending order.
sort.Slice(batchedBlocks, func(i, j int) bool {
return batchedBlocks[i].Slot < batchedBlocks[j].Slot
})
for _, block := range batchedBlocks {
s.processBlock(ctx, block)
}