diff --git a/plugin/evm/tx_gossip_test.go b/plugin/evm/tx_gossip_test.go index 1c3a423373..d983586878 100644 --- a/plugin/evm/tx_gossip_test.go +++ b/plugin/evm/tx_gossip_test.go @@ -18,6 +18,7 @@ import ( "github.com/ava-labs/avalanchego/proto/pb/sdk" "github.com/ava-labs/avalanchego/snow" "github.com/ava-labs/avalanchego/snow/engine/enginetest" + "github.com/ava-labs/avalanchego/snow/snowtest" "github.com/ava-labs/avalanchego/snow/validators" "github.com/ava-labs/avalanchego/upgrade/upgradetest" "github.com/ava-labs/avalanchego/utils/logging" @@ -34,10 +35,13 @@ import ( agoUtils "github.com/ava-labs/avalanchego/utils" ) +// SubnetEVMTestChainID is a subnet-evm specific chain ID for testing +var SubnetEVMTestChainID = ids.GenerateTestID() + func TestEthTxGossip(t *testing.T) { require := require.New(t) ctx := t.Context() - snowCtx := utilstest.NewTestSnowContext(t) + snowCtx := snowtest.Context(t, SubnetEVMTestChainID) validatorState := utilstest.NewTestValidatorState() snowCtx.ValidatorState = validatorState @@ -70,7 +74,7 @@ func TestEthTxGossip(t *testing.T) { validatorSet := p2p.NewValidators( logging.NoLog{}, snowCtx.SubnetID, - validatorState, + snowCtx.ValidatorState, 0, ) network, err := p2p.NewNetwork( diff --git a/utils/utilstest/context.go b/utils/utilstest/context.go index 16fa6ddf0b..bfcdcfa32c 100644 --- a/utils/utilstest/context.go +++ b/utils/utilstest/context.go @@ -4,55 +4,13 @@ package utilstest import ( - "context" - "errors" "testing" "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/snow" "github.com/ava-labs/avalanchego/snow/snowtest" - "github.com/ava-labs/avalanchego/snow/validators" - "github.com/ava-labs/avalanchego/snow/validators/validatorstest" - "github.com/ava-labs/avalanchego/utils/constants" ) -// SubnetEVMTestChainID is a subnet-evm specific chain ID for testing -var SubnetEVMTestChainID = ids.GenerateTestID() - -// @TODO: This should eventually be replaced by a more robust solution, or alternatively, the presence of nil -// validator states shouldn't be depended upon by tests -func NewTestValidatorState() *validatorstest.State { - return &validatorstest.State{ - GetCurrentHeightF: func(context.Context) (uint64, error) { - return 0, nil - }, - GetSubnetIDF: func(_ context.Context, chainID ids.ID) (ids.ID, error) { - subnetID, ok := map[ids.ID]ids.ID{ - constants.PlatformChainID: constants.PrimaryNetworkID, - snowtest.XChainID: constants.PrimaryNetworkID, - snowtest.CChainID: constants.PrimaryNetworkID, - SubnetEVMTestChainID: constants.PrimaryNetworkID, - }[chainID] - if !ok { - return ids.Empty, errors.New("unknown chain") - } - return subnetID, nil - }, - GetValidatorSetF: func(context.Context, uint64, ids.ID) (map[ids.NodeID]*validators.GetValidatorOutput, error) { - return map[ids.NodeID]*validators.GetValidatorOutput{}, nil - }, - GetWarpValidatorSetsF: func(context.Context, uint64) (map[ids.ID]validators.WarpSet, error) { - return nil, nil - }, - GetWarpValidatorSetF: func(context.Context, uint64, ids.ID) (validators.WarpSet, error) { - return validators.WarpSet{}, nil - }, - GetCurrentValidatorSetF: func(context.Context, ids.ID) (map[ids.ID]*validators.GetCurrentValidatorOutput, uint64, error) { - return map[ids.ID]*validators.GetCurrentValidatorOutput{}, 0, nil - }, - } -} - // NewTestSnowContext returns a snow.Context with validator state properly configured for testing. // This wraps snowtest.Context and sets the validator state to avoid the missing GetValidatorSetF issue. // diff --git a/utils/utilstest/snow.go b/utils/utilstest/snow.go new file mode 100644 index 0000000000..f2015779d2 --- /dev/null +++ b/utils/utilstest/snow.go @@ -0,0 +1,52 @@ +// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package utilstest + +import ( + "context" + "errors" + + "github.com/ava-labs/avalanchego/ids" + "github.com/ava-labs/avalanchego/snow/snowtest" + "github.com/ava-labs/avalanchego/snow/validators" + "github.com/ava-labs/avalanchego/snow/validators/validatorstest" + "github.com/ava-labs/avalanchego/utils/constants" +) + +// SubnetEVMTestChainID is a subnet-evm specific chain ID for testing +var SubnetEVMTestChainID = ids.GenerateTestID() + +// @TODO: This should eventually be replaced by a more robust solution, or alternatively, the presence of nil +// validator states shouldn't be depended upon by tests +func NewTestValidatorState() *validatorstest.State { + return &validatorstest.State{ + GetCurrentHeightF: func(context.Context) (uint64, error) { + return 0, nil + }, + GetSubnetIDF: func(_ context.Context, chainID ids.ID) (ids.ID, error) { + subnetID, ok := map[ids.ID]ids.ID{ + constants.PlatformChainID: constants.PrimaryNetworkID, + snowtest.XChainID: constants.PrimaryNetworkID, + snowtest.CChainID: constants.PrimaryNetworkID, + SubnetEVMTestChainID: constants.PrimaryNetworkID, + }[chainID] + if !ok { + return ids.Empty, errors.New("unknown chain") + } + return subnetID, nil + }, + GetValidatorSetF: func(context.Context, uint64, ids.ID) (map[ids.NodeID]*validators.GetValidatorOutput, error) { + return map[ids.NodeID]*validators.GetValidatorOutput{}, nil + }, + GetWarpValidatorSetsF: func(context.Context, uint64) (map[ids.ID]validators.WarpSet, error) { + return nil, nil + }, + GetWarpValidatorSetF: func(context.Context, uint64, ids.ID) (validators.WarpSet, error) { + return validators.WarpSet{}, nil + }, + GetCurrentValidatorSetF: func(context.Context, ids.ID) (map[ids.ID]*validators.GetCurrentValidatorOutput, uint64, error) { + return map[ids.ID]*validators.GetCurrentValidatorOutput{}, 0, nil + }, + } +}