Skip to content

Commit

Permalink
wip: adding UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 committed Nov 1, 2023
1 parent d1d5ad0 commit 6ded29a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
11 changes: 7 additions & 4 deletions vms/proposervm/state_sync_block_backfilling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestBlockBackfillEnabled(t *testing.T) {

// 1. Accept a State summary
var (
forkHeight = uint64(100)
stateSummaryHeight = uint64(2023)
proVMParentStateSummaryBlk = ids.GenerateTestID()
)
Expand All @@ -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()
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -275,6 +277,7 @@ func createTestStateSummary(
t *testing.T,
vm *VM,
proVMParentStateSummaryBlk ids.ID,
forkHeight uint64,
innerVM *fullVM,
innerSummary *block.TestStateSummary,
innerBlk *snowman.TestBlock,
Expand All @@ -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) {
Expand Down
16 changes: 13 additions & 3 deletions vms/proposervm/state_syncable_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
)

Expand All @@ -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())
}

Expand All @@ -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
Expand Down

0 comments on commit 6ded29a

Please sign in to comment.