-
Notifications
You must be signed in to change notification settings - Fork 670
/
test_height_indexed_vm.go
52 lines (41 loc) · 1.35 KB
/
test_height_indexed_vm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package block
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/require"
"github.com/ava-labs/avalanchego/ids"
)
var (
errVerifyHeightIndex = errors.New("unexpectedly called VerifyHeightIndex")
errGetBlockIDAtHeight = errors.New("unexpectedly called GetBlockIDAtHeight")
_ HeightIndexedChainVM = (*TestHeightIndexedVM)(nil)
)
// TestBatchedVM is a BatchedVM that is useful for testing.
type TestHeightIndexedVM struct {
T *testing.T
CantVerifyHeightIndex bool
CantGetBlockIDAtHeight bool
VerifyHeightIndexF func(context.Context) error
GetBlockIDAtHeightF func(ctx context.Context, height uint64) (ids.ID, error)
}
func (vm *TestHeightIndexedVM) VerifyHeightIndex(ctx context.Context) error {
if vm.VerifyHeightIndexF != nil {
return vm.VerifyHeightIndexF(ctx)
}
if vm.CantVerifyHeightIndex && vm.T != nil {
require.FailNow(vm.T, errVerifyHeightIndex.Error())
}
return errVerifyHeightIndex
}
func (vm *TestHeightIndexedVM) GetBlockIDAtHeight(ctx context.Context, height uint64) (ids.ID, error) {
if vm.GetBlockIDAtHeightF != nil {
return vm.GetBlockIDAtHeightF(ctx, height)
}
if vm.CantGetBlockIDAtHeight && vm.T != nil {
require.FailNow(vm.T, errGetAncestor.Error())
}
return ids.Empty, errGetBlockIDAtHeight
}