forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_vm.go
79 lines (65 loc) · 1.79 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
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package vertex
import (
"context"
"errors"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/snow/consensus/snowstorm"
"github.com/MetalBlockchain/metalgo/snow/engine/snowman/block"
)
var (
errPending = errors.New("unexpectedly called Pending")
errLinearize = errors.New("unexpectedly called Linearize")
_ DAGVM = (*TestVM)(nil)
)
type TestVM struct {
block.TestVM
CantLinearize, CantPendingTxs, CantParse, CantGet bool
LinearizeF func(context.Context, ids.ID) error
PendingTxsF func(context.Context) []snowstorm.Tx
ParseTxF func(context.Context, []byte) (snowstorm.Tx, error)
GetTxF func(context.Context, 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) Linearize(ctx context.Context, stopVertexID ids.ID) error {
if vm.LinearizeF != nil {
return vm.LinearizeF(ctx, stopVertexID)
}
if vm.CantLinearize && vm.T != nil {
vm.T.Fatal(errLinearize)
}
return errLinearize
}
func (vm *TestVM) PendingTxs(ctx context.Context) []snowstorm.Tx {
if vm.PendingTxsF != nil {
return vm.PendingTxsF(ctx)
}
if vm.CantPendingTxs && vm.T != nil {
vm.T.Fatal(errPending)
}
return nil
}
func (vm *TestVM) ParseTx(ctx context.Context, b []byte) (snowstorm.Tx, error) {
if vm.ParseTxF != nil {
return vm.ParseTxF(ctx, b)
}
if vm.CantParse && vm.T != nil {
vm.T.Fatal(errParse)
}
return nil, errParse
}
func (vm *TestVM) GetTx(ctx context.Context, txID ids.ID) (snowstorm.Tx, error) {
if vm.GetTxF != nil {
return vm.GetTxF(ctx, txID)
}
if vm.CantGet && vm.T != nil {
vm.T.Fatal(errGet)
}
return nil, errGet
}