-
Notifications
You must be signed in to change notification settings - Fork 661
/
test_batched_vm.go
84 lines (71 loc) · 1.76 KB
/
test_batched_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package block
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
)
var (
errGetAncestor = errors.New("unexpectedly called GetAncestor")
errBatchedParseBlock = errors.New("unexpectedly called BatchedParseBlock")
_ BatchedChainVM = (*TestBatchedVM)(nil)
)
// TestBatchedVM is a BatchedVM that is useful for testing.
type TestBatchedVM struct {
T *testing.T
CantGetAncestors bool
CantBatchParseBlock bool
GetAncestorsF func(
ctx context.Context,
blkID ids.ID,
maxBlocksNum int,
maxBlocksSize int,
maxBlocksRetrivalTime time.Duration,
) ([][]byte, error)
BatchedParseBlockF func(
ctx context.Context,
blks [][]byte,
) ([]snowman.Block, error)
}
func (vm *TestBatchedVM) Default(cant bool) {
vm.CantGetAncestors = cant
vm.CantBatchParseBlock = cant
}
func (vm *TestBatchedVM) GetAncestors(
ctx context.Context,
blkID ids.ID,
maxBlocksNum int,
maxBlocksSize int,
maxBlocksRetrivalTime time.Duration,
) ([][]byte, error) {
if vm.GetAncestorsF != nil {
return vm.GetAncestorsF(
ctx,
blkID,
maxBlocksNum,
maxBlocksSize,
maxBlocksRetrivalTime,
)
}
if vm.CantGetAncestors && vm.T != nil {
require.FailNow(vm.T, errGetAncestor.Error())
}
return nil, errGetAncestor
}
func (vm *TestBatchedVM) BatchedParseBlock(
ctx context.Context,
blks [][]byte,
) ([]snowman.Block, error) {
if vm.BatchedParseBlockF != nil {
return vm.BatchedParseBlockF(ctx, blks)
}
if vm.CantBatchParseBlock && vm.T != nil {
require.FailNow(vm.T, errBatchedParseBlock.Error())
}
return nil, errBatchedParseBlock
}