-
Notifications
You must be signed in to change notification settings - Fork 661
/
test_state.go
81 lines (68 loc) · 2.18 KB
/
test_state.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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package validators
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/require"
"github.com/ava-labs/avalanchego/ids"
)
var (
errMinimumHeight = errors.New("unexpectedly called GetMinimumHeight")
errCurrentHeight = errors.New("unexpectedly called GetCurrentHeight")
errSubnetID = errors.New("unexpectedly called GetSubnetID")
errGetValidatorSet = errors.New("unexpectedly called GetValidatorSet")
)
var _ State = (*TestState)(nil)
type TestState struct {
T testing.TB
CantGetMinimumHeight,
CantGetCurrentHeight,
CantGetSubnetID,
CantGetValidatorSet bool
GetMinimumHeightF func(ctx context.Context) (uint64, error)
GetCurrentHeightF func(ctx context.Context) (uint64, error)
GetSubnetIDF func(ctx context.Context, chainID ids.ID) (ids.ID, error)
GetValidatorSetF func(ctx context.Context, height uint64, subnetID ids.ID) (map[ids.NodeID]*GetValidatorOutput, error)
}
func (vm *TestState) GetMinimumHeight(ctx context.Context) (uint64, error) {
if vm.GetMinimumHeightF != nil {
return vm.GetMinimumHeightF(ctx)
}
if vm.CantGetMinimumHeight && vm.T != nil {
require.FailNow(vm.T, errMinimumHeight.Error())
}
return 0, errMinimumHeight
}
func (vm *TestState) GetCurrentHeight(ctx context.Context) (uint64, error) {
if vm.GetCurrentHeightF != nil {
return vm.GetCurrentHeightF(ctx)
}
if vm.CantGetCurrentHeight && vm.T != nil {
require.FailNow(vm.T, errCurrentHeight.Error())
}
return 0, errCurrentHeight
}
func (vm *TestState) GetSubnetID(ctx context.Context, chainID ids.ID) (ids.ID, error) {
if vm.GetSubnetIDF != nil {
return vm.GetSubnetIDF(ctx, chainID)
}
if vm.CantGetSubnetID && vm.T != nil {
require.FailNow(vm.T, errSubnetID.Error())
}
return ids.Empty, errSubnetID
}
func (vm *TestState) GetValidatorSet(
ctx context.Context,
height uint64,
subnetID ids.ID,
) (map[ids.NodeID]*GetValidatorOutput, error) {
if vm.GetValidatorSetF != nil {
return vm.GetValidatorSetF(ctx, height, subnetID)
}
if vm.CantGetValidatorSet && vm.T != nil {
require.FailNow(vm.T, errGetValidatorSet.Error())
}
return nil, errGetValidatorSet
}