Skip to content

Commit 3f46a5a

Browse files
Move test function for coreth to avalanchego
1 parent 57c6ec7 commit 3f46a5a

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

snow/context.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package snow
55

66
import (
7+
"context"
8+
"errors"
79
"sync"
810

911
"github.com/prometheus/client_golang/prometheus"
@@ -13,8 +15,10 @@ import (
1315
"github.com/ava-labs/avalanchego/chains/atomic"
1416
"github.com/ava-labs/avalanchego/ids"
1517
"github.com/ava-labs/avalanchego/snow/validators"
18+
"github.com/ava-labs/avalanchego/snow/validators/validatorstest"
1619
"github.com/ava-labs/avalanchego/upgrade"
1720
"github.com/ava-labs/avalanchego/utils"
21+
"github.com/ava-labs/avalanchego/utils/constants"
1822
"github.com/ava-labs/avalanchego/utils/crypto/bls"
1923
"github.com/ava-labs/avalanchego/utils/logging"
2024
"github.com/ava-labs/avalanchego/vms/platformvm/warp"
@@ -95,3 +99,59 @@ type ConsensusContext struct {
9599
// True iff this chain is currently state-syncing
96100
StateSyncing utils.Atomic[bool]
97101
}
102+
103+
func NewTestContext() *Context {
104+
var (
105+
testCChainID = ids.ID{'c', 'c', 'h', 'a', 'i', 'n', 't', 'e', 's', 't'}
106+
testXChainID = ids.ID{'t', 'e', 's', 't', 'x'}
107+
testChainID = ids.ID{'t', 'e', 's', 't', 'c', 'h', 'a', 'i', 'n'}
108+
)
109+
110+
signer, err := bls.NewSigner()
111+
if err != nil {
112+
panic(err)
113+
}
114+
pk := signer.PublicKey()
115+
networkID := constants.UnitTestID
116+
chainID := testChainID
117+
118+
validatorState := &validatorstest.State{
119+
GetCurrentHeightF: func(context.Context) (uint64, error) {
120+
return 0, nil
121+
},
122+
GetSubnetIDF: func(_ context.Context, chainID ids.ID) (ids.ID, error) {
123+
subnetID, ok := map[ids.ID]ids.ID{
124+
constants.PlatformChainID: constants.PrimaryNetworkID,
125+
testXChainID: constants.PrimaryNetworkID,
126+
testCChainID: constants.PrimaryNetworkID,
127+
}[chainID]
128+
if !ok {
129+
return ids.Empty, errors.New("unknown chain")
130+
}
131+
return subnetID, nil
132+
},
133+
GetValidatorSetF: func(context.Context, uint64, ids.ID) (map[ids.NodeID]*validators.GetValidatorOutput, error) {
134+
return map[ids.NodeID]*validators.GetValidatorOutput{}, nil
135+
},
136+
GetCurrentValidatorSetF: func(context.Context, ids.ID) (map[ids.ID]*validators.GetCurrentValidatorOutput, uint64, error) {
137+
return map[ids.ID]*validators.GetCurrentValidatorOutput{}, 0, nil
138+
},
139+
}
140+
141+
ctx := &Context{
142+
NetworkID: networkID,
143+
SubnetID: ids.Empty,
144+
ChainID: chainID,
145+
NodeID: ids.GenerateTestNodeID(),
146+
XChainID: testXChainID,
147+
CChainID: testCChainID,
148+
PublicKey: pk,
149+
WarpSigner: warp.NewSigner(signer, networkID, chainID),
150+
Log: logging.NoLog{},
151+
BCLookup: ids.NewAliaser(),
152+
Metrics: metrics.NewPrefixGatherer(),
153+
ChainDataDir: "",
154+
ValidatorState: validatorState,
155+
}
156+
return ctx
157+
}

0 commit comments

Comments
 (0)