From 6ded29a7d8081a89d605eb77238b99f1d423efb9 Mon Sep 17 00:00:00 2001 From: Alberto Benegiamo Date: Wed, 1 Nov 2023 23:59:36 +0100 Subject: [PATCH] wip: adding UTs --- .../state_sync_block_backfilling_test.go | 11 +++++++---- vms/proposervm/state_syncable_vm.go | 16 +++++++++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/vms/proposervm/state_sync_block_backfilling_test.go b/vms/proposervm/state_sync_block_backfilling_test.go index a7e5ebb3f4d4..d73895c4e7bd 100644 --- a/vms/proposervm/state_sync_block_backfilling_test.go +++ b/vms/proposervm/state_sync_block_backfilling_test.go @@ -35,6 +35,7 @@ func TestBlockBackfillEnabled(t *testing.T) { // 1. Accept a State summary var ( + forkHeight = uint64(100) stateSummaryHeight = uint64(2023) proVMParentStateSummaryBlk = ids.GenerateTestID() ) @@ -51,7 +52,7 @@ func TestBlockBackfillEnabled(t *testing.T) { HeightV: innerSummary.Height(), BytesV: []byte("inner state synced block"), } - stateSummary := createTestStateSummary(t, vm, proVMParentStateSummaryBlk, innerVM, innerSummary, innerStateSyncedBlk) + stateSummary := createTestStateSummary(t, vm, proVMParentStateSummaryBlk, forkHeight, innerVM, innerSummary, innerStateSyncedBlk) // Block backfilling not enabled before state sync is accepted ctx := context.Background() @@ -115,7 +116,8 @@ func TestBlockBackfill(t *testing.T) { }() var ( - blkCount = 1 + forkHeight = uint64(100) + blkCount = 10 startBlkHeight = uint64(1492) proBlks, innerBlks = createTestBlocks(t, vm, blkCount, startBlkHeight) @@ -137,7 +139,7 @@ func TestBlockBackfill(t *testing.T) { HeightV: innerSummary.Height(), BytesV: []byte("inner state synced block"), } - stateSummary := createTestStateSummary(t, vm, proTopBlk.ID(), innerVM, innerSummary, innerStateSyncedBlk) + stateSummary := createTestStateSummary(t, vm, proTopBlk.ID(), forkHeight, innerVM, innerSummary, innerStateSyncedBlk) innerSummary.AcceptF = func(ctx context.Context) (block.StateSyncMode, error) { return block.StateSyncStatic, nil @@ -275,6 +277,7 @@ func createTestStateSummary( t *testing.T, vm *VM, proVMParentStateSummaryBlk ids.ID, + forkHeight uint64, innerVM *fullVM, innerSummary *block.TestStateSummary, innerBlk *snowman.TestBlock, @@ -291,7 +294,7 @@ func createTestStateSummary( ) require.NoError(err) - statelessSummary, err := summary.Build(innerSummary.Height()-1, slb.Bytes(), innerSummary.Bytes()) + statelessSummary, err := summary.Build(forkHeight, slb.Bytes(), innerSummary.Bytes()) require.NoError(err) innerVM.ParseStateSummaryF = func(ctx context.Context, summaryBytes []byte) (block.StateSummary, error) { diff --git a/vms/proposervm/state_syncable_vm.go b/vms/proposervm/state_syncable_vm.go index 88b7825bb341..c90371031ab1 100644 --- a/vms/proposervm/state_syncable_vm.go +++ b/vms/proposervm/state_syncable_vm.go @@ -6,8 +6,10 @@ package proposervm import ( "context" "fmt" + "sort" "go.uber.org/zap" + "golang.org/x/exp/maps" "github.com/ava-labs/avalanchego/database" "github.com/ava-labs/avalanchego/ids" @@ -176,7 +178,7 @@ func (vm *VM) BackfillBlocksEnabled(ctx context.Context) (ids.ID, uint64, error) func (vm *VM) BackfillBlocks(ctx context.Context, blksBytes [][]byte) (ids.ID, uint64, error) { var ( - blks = make([]Block, 0, len(blksBytes)) + blks = make(map[uint64]Block) innerBlksBytes = make([][]byte, 0, len(blksBytes)) ) @@ -188,7 +190,7 @@ func (vm *VM) BackfillBlocks(ctx context.Context, blksBytes [][]byte) (ids.ID, u // TODO: introduce validation - blks = append(blks, blk) + blks[blk.Height()] = blk innerBlksBytes = append(innerBlksBytes, blk.getInnerBlk().Bytes()) } @@ -198,7 +200,15 @@ func (vm *VM) BackfillBlocks(ctx context.Context, blksBytes [][]byte) (ids.ID, u // Find out which blocks have been accepted and store them. // Should the process err while looping there will be innerVM blocks not indexed by proposerVM. Repair will be // carried out upon restart. - for _, blk := range blks { + + // Make sure to sort blocks by height to accept them in the right order + blkHeights := maps.Keys(blks) + sort.Slice(blkHeights, func(i, j int) bool { + return blkHeights[i] < blkHeights[j] // sort in ascending order + }) + + for _, h := range blkHeights { + blk := blks[h] _, err := vm.ChainVM.GetBlock(ctx, blk.getInnerBlk().ID()) if err != nil { continue