Fixes overflow issue in sync/pending_blocks_queue (#6039)

* fixes overflow issue
* Merge refs/heads/master into fix-overflow
* Merge refs/heads/master into fix-overflow
This commit is contained in:
Victor Farazdagi 2020-05-29 16:50:41 +03:00 committed by GitHub
parent a0bf8cba52
commit 0243bdcf37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 7 deletions

View File

@ -55,7 +55,7 @@ func (r *Service) processPendingBlocks(ctx context.Context) error {
span.AddAttributes(trace.Int64Attribute("slot", int64(s)))
r.pendingQueueLock.RLock()
b := r.slotToPendingBlocks[uint64(s)]
b := r.slotToPendingBlocks[s]
// Skip if block does not exist.
if b == nil || b.Block == nil {
r.pendingQueueLock.RUnlock()
@ -85,7 +85,7 @@ func (r *Service) processPendingBlocks(ctx context.Context) error {
if err != nil {
return errors.Wrap(err, "failed to read chain state for peer")
}
if cs != nil && cs.HeadSlot >= uint64(s) {
if cs != nil && cs.HeadSlot >= s {
pid = p
break
}
@ -122,7 +122,7 @@ func (r *Service) processPendingBlocks(ctx context.Context) error {
}
r.pendingQueueLock.Lock()
delete(r.slotToPendingBlocks, uint64(s))
delete(r.slotToPendingBlocks, s)
delete(r.seenPendingBlocks, blkRoot)
r.pendingQueueLock.Unlock()
@ -137,15 +137,17 @@ func (r *Service) processPendingBlocks(ctx context.Context) error {
return nil
}
func (r *Service) sortedPendingSlots() []int {
func (r *Service) sortedPendingSlots() []uint64 {
r.pendingQueueLock.RLock()
defer r.pendingQueueLock.RUnlock()
slots := make([]int, 0, len(r.slotToPendingBlocks))
slots := make([]uint64, 0, len(r.slotToPendingBlocks))
for s := range r.slotToPendingBlocks {
slots = append(slots, int(s))
slots = append(slots, s)
}
sort.Ints(slots)
sort.Slice(slots, func(i, j int) bool {
return slots[i] < slots[j]
})
return slots
}

View File

@ -2,6 +2,8 @@ package sync
import (
"context"
"math"
"reflect"
"sync"
"testing"
@ -309,3 +311,21 @@ func TestRegularSyncBeaconBlockSubscriber_PruneOldPendingBlocks(t *testing.T) {
t.Errorf("Incorrect size for seen pending block: got %d", len(r.seenPendingBlocks))
}
}
func TestService_sortedPendingSlots(t *testing.T) {
r := &Service{
slotToPendingBlocks: make(map[uint64]*ethpb.SignedBeaconBlock),
}
var lastSlot uint64 = math.MaxUint64
r.slotToPendingBlocks[lastSlot] = &ethpb.SignedBeaconBlock{}
r.slotToPendingBlocks[lastSlot-3] = &ethpb.SignedBeaconBlock{}
r.slotToPendingBlocks[lastSlot-5] = &ethpb.SignedBeaconBlock{}
r.slotToPendingBlocks[lastSlot-2] = &ethpb.SignedBeaconBlock{}
want := []uint64{lastSlot - 5, lastSlot - 3, lastSlot - 2, lastSlot}
got := r.sortedPendingSlots()
if !reflect.DeepEqual(want, got) {
t.Errorf("unexpected pending slots list, want: %v, got: %v", want, got)
}
}