forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_vm.go
56 lines (43 loc) · 1.22 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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package vertex
import (
"context"
"errors"
"github.com/stretchr/testify/require"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/snow/consensus/snowstorm"
"github.com/MetalBlockchain/metalgo/snow/engine/snowman/block"
)
var (
errLinearize = errors.New("unexpectedly called Linearize")
_ LinearizableVM = (*TestVM)(nil)
)
type TestVM struct {
block.TestVM
CantLinearize, CantParse bool
LinearizeF func(context.Context, ids.ID) error
ParseTxF func(context.Context, []byte) (snowstorm.Tx, error)
}
func (vm *TestVM) Default(cant bool) {
vm.TestVM.Default(cant)
vm.CantParse = 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 {
require.FailNow(vm.T, errLinearize.Error())
}
return errLinearize
}
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 {
require.FailNow(vm.T, errParse.Error())
}
return nil, errParse
}