-
Notifications
You must be signed in to change notification settings - Fork 661
/
test_batched_vm.go
67 lines (55 loc) · 1.54 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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package block
import (
"errors"
"testing"
"time"
"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{}
)
// TestBatchedVM is a BatchedVM that is useful for testing.
type TestBatchedVM struct {
T *testing.T
CantGetAncestors bool
CantBatchParseBlock bool
GetAncestorsF func(
blkID ids.ID,
maxBlocksNum int,
maxBlocksSize int,
maxBlocksRetrivalTime time.Duration,
) ([][]byte, error)
BatchedParseBlockF func(blks [][]byte) ([]snowman.Block, error)
}
func (vm *TestBatchedVM) Default(cant bool) {
vm.CantGetAncestors = cant
vm.CantBatchParseBlock = cant
}
func (vm *TestBatchedVM) GetAncestors(
blkID ids.ID,
maxBlocksNum int,
maxBlocksSize int,
maxBlocksRetrivalTime time.Duration,
) ([][]byte, error) {
if vm.GetAncestorsF != nil {
return vm.GetAncestorsF(blkID, maxBlocksNum, maxBlocksSize, maxBlocksRetrivalTime)
}
if vm.CantGetAncestors && vm.T != nil {
vm.T.Fatal(errGetAncestor)
}
return nil, errGetAncestor
}
func (vm *TestBatchedVM) BatchedParseBlock(blks [][]byte) ([]snowman.Block, error) {
if vm.BatchedParseBlockF != nil {
return vm.BatchedParseBlockF(blks)
}
if vm.CantBatchParseBlock && vm.T != nil {
vm.T.Fatal(errBatchedParseBlock)
}
return nil, errBatchedParseBlock
}