forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_state.go
62 lines (51 loc) · 1.62 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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package validators
import (
"context"
"errors"
"testing"
"github.com/MetalBlockchain/metalgo/ids"
)
var (
errMinimumHeight = errors.New("unexpectedly called GetMinimumHeight")
errCurrentHeight = errors.New("unexpectedly called GetCurrentHeight")
errGetValidatorSet = errors.New("unexpectedly called GetValidatorSet")
)
var _ State = (*TestState)(nil)
type TestState struct {
T *testing.T
CantGetMinimumHeight,
CantGetCurrentHeight,
CantGetValidatorSet bool
GetMinimumHeightF func(context.Context) (uint64, error)
GetCurrentHeightF func(context.Context) (uint64, error)
GetValidatorSetF func(ctx context.Context, height uint64, subnetID ids.ID) (map[ids.NodeID]uint64, error)
}
func (vm *TestState) GetMinimumHeight(ctx context.Context) (uint64, error) {
if vm.GetMinimumHeightF != nil {
return vm.GetMinimumHeightF(ctx)
}
if vm.CantGetMinimumHeight && vm.T != nil {
vm.T.Fatal(errMinimumHeight)
}
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 {
vm.T.Fatal(errCurrentHeight)
}
return 0, errCurrentHeight
}
func (vm *TestState) GetValidatorSet(ctx context.Context, height uint64, subnetID ids.ID) (map[ids.NodeID]uint64, error) {
if vm.GetValidatorSetF != nil {
return vm.GetValidatorSetF(ctx, height, subnetID)
}
if vm.CantGetValidatorSet && vm.T != nil {
vm.T.Fatal(errGetValidatorSet)
}
return nil, errGetValidatorSet
}