forked from ava-labs/avalanchego
/
test_state.go
61 lines (50 loc) · 1.48 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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package validators
import (
"errors"
"testing"
"github.com/kukrer/savannahnode/ids"
)
var (
errMinimumHeight = errors.New("unexpectedly called GetMinimumHeight")
errCurrentHeight = errors.New("unexpectedly called GetCurrentHeight")
errGetValidatorSet = errors.New("unexpectedly called GetValidatorSet")
)
var _ State = &TestState{}
type TestState struct {
T *testing.T
CantGetMinimumHeight,
CantGetCurrentHeight,
CantGetValidatorSet bool
GetMinimumHeightF func() (uint64, error)
GetCurrentHeightF func() (uint64, error)
GetValidatorSetF func(height uint64, subnetID ids.ID) (map[ids.NodeID]uint64, error)
}
func (vm *TestState) GetMinimumHeight() (uint64, error) {
if vm.GetMinimumHeightF != nil {
return vm.GetMinimumHeightF()
}
if vm.CantGetMinimumHeight && vm.T != nil {
vm.T.Fatal(errMinimumHeight)
}
return 0, errMinimumHeight
}
func (vm *TestState) GetCurrentHeight() (uint64, error) {
if vm.GetCurrentHeightF != nil {
return vm.GetCurrentHeightF()
}
if vm.CantGetCurrentHeight && vm.T != nil {
vm.T.Fatal(errCurrentHeight)
}
return 0, errCurrentHeight
}
func (vm *TestState) GetValidatorSet(height uint64, subnetID ids.ID) (map[ids.NodeID]uint64, error) {
if vm.GetValidatorSetF != nil {
return vm.GetValidatorSetF(height, subnetID)
}
if vm.CantGetValidatorSet && vm.T != nil {
vm.T.Fatal(errGetValidatorSet)
}
return nil, errGetValidatorSet
}