Skip to content

Commit

Permalink
chore: fix removeIrrelevantFragments out of bounds panic
Browse files Browse the repository at this point in the history
  • Loading branch information
EclesioMeloJunior committed Sep 13, 2024
1 parent f233d05 commit 330444c
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 77 deletions.
12 changes: 6 additions & 6 deletions dot/sync/mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 6 additions & 10 deletions dot/sync/unready_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,29 +138,25 @@ func (u *unreadyBlocks) removeIrrelevantFragments(finalisedNumber uint) {
return value.Header.Number <= finalisedNumber
})

idxsToRemove := make([]int, 0, len(u.disjointFragments))
for fragmentIdx, fragment := range u.disjointFragments {
fragmentIdx := 0
for _, fragment := range u.disjointFragments {
// the fragments are sorted in ascending order
// starting from the latest item and going backwards
// we have a higher chance to find the idx that has
// a block with number lower or equal the finalised one
idx := len(fragment) - 1
for idx >= 0 {
for ; idx >= 0; idx-- {
if fragment[idx].Header.Number <= finalisedNumber {
break
}
idx--
}

updatedFragment := fragment[idx+1:]
if len(updatedFragment) == 0 {
idxsToRemove = append(idxsToRemove, fragmentIdx)
} else {
if len(updatedFragment) != 0 {
u.disjointFragments[fragmentIdx] = updatedFragment
fragmentIdx++
}
}

for _, idx := range idxsToRemove {
u.disjointFragments = append(u.disjointFragments[:idx], u.disjointFragments[idx+1:]...)
}
u.disjointFragments = u.disjointFragments[:fragmentIdx]
}
180 changes: 120 additions & 60 deletions dot/sync/unready_blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,109 @@ import (
)

func TestUnreadyBlocks_removeIrrelevantFragments(t *testing.T) {
ub := newUnreadyBlocks()
ub.disjointFragments = [][]*types.BlockData{
// first fragment
{
t.Run("removing all disjoint fragment", func(t *testing.T) {
ub := newUnreadyBlocks()
ub.disjointFragments = [][]*types.BlockData{
{
Header: &types.Header{
Number: 192,
{
Header: &types.Header{
Number: 100,
},
},
},

{
Header: &types.Header{
Number: 191,
{
Header: &types.Header{
Number: 99,
},
},
},
{
{
Header: &types.Header{
Number: 92,
},
},
},
}
ub.removeIrrelevantFragments(100)
require.Empty(t, ub.disjointFragments)
})

t.Run("removing irrelevant fragments", func(t *testing.T) {
ub := newUnreadyBlocks()
ub.disjointFragments = [][]*types.BlockData{
// first fragment
{
{
Header: &types.Header{
Number: 192,
},
},

{
Header: &types.Header{
Number: 191,
},
},

{
Header: &types.Header{
Number: 190,
},
},
},

// second fragment
{
Header: &types.Header{
Number: 190,
{
Header: &types.Header{
Number: 253,
},
},

{
Header: &types.Header{
Number: 254,
},
},

{
Header: &types.Header{
Number: 255,
},
},
},
},

// second fragment
{
// third fragment
{
Header: &types.Header{
Number: 253,
{
Header: &types.Header{
Number: 1022,
},
},

{
Header: &types.Header{
Number: 1023,
},
},

{
Header: &types.Header{
Number: 1024,
},
},
},
}

// the first fragment should be removed
// the second fragment should have only 2 items
// the third frament shold not be affected
ub.removeIrrelevantFragments(253)
require.Len(t, ub.disjointFragments, 2)

expectedSecondFrag := []*types.BlockData{
{
Header: &types.Header{
Number: 254,
Expand All @@ -50,10 +122,9 @@ func TestUnreadyBlocks_removeIrrelevantFragments(t *testing.T) {
Number: 255,
},
},
},
}

// third fragment
{
expectedThirdFragment := []*types.BlockData{
{
Header: &types.Header{
Number: 1022,
Expand All @@ -71,48 +142,37 @@ func TestUnreadyBlocks_removeIrrelevantFragments(t *testing.T) {
Number: 1024,
},
},
},
}

// the first fragment should be removed
// the second fragment should have only 2 items
// the third frament shold not be affected
ub.removeIrrelevantFragments(253)
require.Len(t, ub.disjointFragments, 2)

expectedSecondFrag := []*types.BlockData{
{
Header: &types.Header{
Number: 254,
},
},

{
Header: &types.Header{
Number: 255,
},
},
}

expectedThirdFragment := []*types.BlockData{
{
Header: &types.Header{
Number: 1022,
}
require.Equal(t, ub.disjointFragments[0], expectedSecondFrag)
require.Equal(t, ub.disjointFragments[1], expectedThirdFragment)
})

t.Run("keep all fragments", func(t *testing.T) {
ub := newUnreadyBlocks()
ub.disjointFragments = [][]*types.BlockData{
{
{
Header: &types.Header{
Number: 101,
},
},
},
},

{
Header: &types.Header{
Number: 1023,
{
{
Header: &types.Header{
Number: 103,
},
},
},
},

{
Header: &types.Header{
Number: 1024,
{
{
Header: &types.Header{
Number: 104,
},
},
},
},
}
require.Equal(t, ub.disjointFragments[0], expectedSecondFrag)
require.Equal(t, ub.disjointFragments[1], expectedThirdFragment)
}
ub.removeIrrelevantFragments(100)
require.Len(t, ub.disjointFragments, 3)
})
}
1 change: 0 additions & 1 deletion scripts/retrieve_block/retrieve_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ func main() {

protocolID := protocol.ID(fmt.Sprintf("/%s/sync/2", chain.ProtocolID))
for _, bootnodesAddr := range bootnodes {
log.Println("connecting...")
err := p2pHost.Connect(ctx, bootnodesAddr)
if err != nil {
log.Printf("fail with: %s\n", err.Error())
Expand Down

0 comments on commit 330444c

Please sign in to comment.