-
Notifications
You must be signed in to change notification settings - Fork 661
/
test_vm.go
66 lines (53 loc) · 1.28 KB
/
test_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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package vertex
import (
"errors"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/consensus/snowstorm"
"github.com/ava-labs/avalanchego/snow/engine/common"
)
var (
errPending = errors.New("unexpectedly called Pending")
_ DAGVM = &TestVM{}
)
type TestVM struct {
common.TestVM
CantPendingTxs, CantParse, CantGet bool
PendingTxsF func() []snowstorm.Tx
ParseTxF func([]byte) (snowstorm.Tx, error)
GetTxF func(ids.ID) (snowstorm.Tx, error)
}
func (vm *TestVM) Default(cant bool) {
vm.TestVM.Default(cant)
vm.CantPendingTxs = cant
vm.CantParse = cant
vm.CantGet = cant
}
func (vm *TestVM) PendingTxs() []snowstorm.Tx {
if vm.PendingTxsF != nil {
return vm.PendingTxsF()
}
if vm.CantPendingTxs && vm.T != nil {
vm.T.Fatal(errPending)
}
return nil
}
func (vm *TestVM) ParseTx(b []byte) (snowstorm.Tx, error) {
if vm.ParseTxF != nil {
return vm.ParseTxF(b)
}
if vm.CantParse && vm.T != nil {
vm.T.Fatal(errParse)
}
return nil, errParse
}
func (vm *TestVM) GetTx(txID ids.ID) (snowstorm.Tx, error) {
if vm.GetTxF != nil {
return vm.GetTxF(txID)
}
if vm.CantGet && vm.T != nil {
vm.T.Fatal(errGet)
}
return nil, errGet
}