Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add TestSnowContext util #430

Merged
merged 17 commits into from
Dec 22, 2023
26 changes: 13 additions & 13 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ var (
}

TestChainConfig = &ChainConfig{
AvalancheContext: AvalancheContext{snow.DefaultContextTest()},
AvalancheContext: AvalancheContext{utils.TestSnowContext()},
ChainID: big.NewInt(1),
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
Expand All @@ -161,7 +161,7 @@ var (
}

TestLaunchConfig = &ChainConfig{
AvalancheContext: AvalancheContext{snow.DefaultContextTest()},
AvalancheContext: AvalancheContext{utils.TestSnowContext()},
ChainID: big.NewInt(1),
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
Expand All @@ -188,7 +188,7 @@ var (
}

TestApricotPhase1Config = &ChainConfig{
AvalancheContext: AvalancheContext{snow.DefaultContextTest()},
AvalancheContext: AvalancheContext{utils.TestSnowContext()},
ChainID: big.NewInt(1),
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
Expand All @@ -215,7 +215,7 @@ var (
}

TestApricotPhase2Config = &ChainConfig{
AvalancheContext: AvalancheContext{snow.DefaultContextTest()},
AvalancheContext: AvalancheContext{utils.TestSnowContext()},
ChainID: big.NewInt(1),
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
Expand All @@ -242,7 +242,7 @@ var (
}

TestApricotPhase3Config = &ChainConfig{
AvalancheContext: AvalancheContext{snow.DefaultContextTest()},
AvalancheContext: AvalancheContext{utils.TestSnowContext()},
ChainID: big.NewInt(1),
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
Expand All @@ -269,7 +269,7 @@ var (
}

TestApricotPhase4Config = &ChainConfig{
AvalancheContext: AvalancheContext{snow.DefaultContextTest()},
AvalancheContext: AvalancheContext{utils.TestSnowContext()},
ChainID: big.NewInt(1),
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
Expand All @@ -296,7 +296,7 @@ var (
}

TestApricotPhase5Config = &ChainConfig{
AvalancheContext: AvalancheContext{snow.DefaultContextTest()},
AvalancheContext: AvalancheContext{utils.TestSnowContext()},
ChainID: big.NewInt(1),
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
Expand All @@ -323,7 +323,7 @@ var (
}

TestApricotPhasePre6Config = &ChainConfig{
AvalancheContext: AvalancheContext{snow.DefaultContextTest()},
AvalancheContext: AvalancheContext{utils.TestSnowContext()},
ChainID: big.NewInt(1),
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
Expand All @@ -350,7 +350,7 @@ var (
}

TestApricotPhase6Config = &ChainConfig{
AvalancheContext: AvalancheContext{snow.DefaultContextTest()},
AvalancheContext: AvalancheContext{utils.TestSnowContext()},
ChainID: big.NewInt(1),
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
Expand All @@ -377,7 +377,7 @@ var (
}

TestApricotPhasePost6Config = &ChainConfig{
AvalancheContext: AvalancheContext{snow.DefaultContextTest()},
AvalancheContext: AvalancheContext{utils.TestSnowContext()},
ChainID: big.NewInt(1),
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
Expand All @@ -404,7 +404,7 @@ var (
}

TestBanffChainConfig = &ChainConfig{
AvalancheContext: AvalancheContext{snow.DefaultContextTest()},
AvalancheContext: AvalancheContext{utils.TestSnowContext()},
ChainID: big.NewInt(1),
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
Expand All @@ -431,7 +431,7 @@ var (
}

TestCortinaChainConfig = &ChainConfig{
AvalancheContext: AvalancheContext{snow.DefaultContextTest()},
AvalancheContext: AvalancheContext{utils.TestSnowContext()},
ChainID: big.NewInt(1),
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
Expand All @@ -458,7 +458,7 @@ var (
}

TestDUpgradeChainConfig = &ChainConfig{
AvalancheContext: AvalancheContext{snow.DefaultContextTest()},
AvalancheContext: AvalancheContext{utils.TestSnowContext()},
ChainID: big.NewInt(1),
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
Expand Down
7 changes: 3 additions & 4 deletions plugin/evm/block_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import (
"time"

"github.com/ava-labs/coreth/params"

"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/coreth/utils"
)

func TestBlockBuilderShutsDown(t *testing.T) {
shutdownChan := make(chan struct{})
wg := &sync.WaitGroup{}
builder := &blockBuilder{
ctx: snow.DefaultContextTest(),
ctx: utils.TestSnowContext(),
chainConfig: params.TestChainConfig,
shutdownChan: shutdownChan,
shutdownWg: wg,
Expand All @@ -34,7 +33,7 @@ func TestBlockBuilderSkipsTimerInitialization(t *testing.T) {
shutdownChan := make(chan struct{})
wg := &sync.WaitGroup{}
builder := &blockBuilder{
ctx: snow.DefaultContextTest(),
ctx: utils.TestSnowContext(),
chainConfig: params.TestChainConfig,
shutdownChan: shutdownChan,
shutdownWg: wg,
Expand Down
13 changes: 7 additions & 6 deletions plugin/evm/tx_gossip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ import (

"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/params"
corethUtils "github.com/ava-labs/coreth/utils"
)

func TestEthTxGossip(t *testing.T) {
require := require.New(t)
ctx := context.Background()
snowCtx := snow.DefaultContextTest()
snowCtx := corethUtils.TestSnowContext()
validatorState := &validators.TestState{}
snowCtx.ValidatorState = validatorState

Expand Down Expand Up @@ -160,7 +161,7 @@ func TestEthTxGossip(t *testing.T) {
func TestAtomicTxGossip(t *testing.T) {
require := require.New(t)
ctx := context.Background()
snowCtx := snow.DefaultContextTest()
snowCtx := corethUtils.TestSnowContext()
snowCtx.AVAXAssetID = ids.GenerateTestID()
snowCtx.XChainID = ids.GenerateTestID()
validatorState := &validators.TestState{
Expand Down Expand Up @@ -296,7 +297,7 @@ func TestAtomicTxGossip(t *testing.T) {
func TestEthTxPushGossipOutbound(t *testing.T) {
require := require.New(t)
ctx := context.Background()
snowCtx := snow.DefaultContextTest()
snowCtx := corethUtils.TestSnowContext()
sender := &common.FakeSender{
SentAppGossip: make(chan []byte, 1),
}
Expand Down Expand Up @@ -353,7 +354,7 @@ func TestEthTxPushGossipOutbound(t *testing.T) {
func TestEthTxPushGossipInbound(t *testing.T) {
require := require.New(t)
ctx := context.Background()
snowCtx := snow.DefaultContextTest()
snowCtx := corethUtils.TestSnowContext()

sender := &common.FakeSender{
SentAppGossip: make(chan []byte, 1),
Expand Down Expand Up @@ -422,7 +423,7 @@ func TestEthTxPushGossipInbound(t *testing.T) {
func TestAtomicTxPushGossipOutbound(t *testing.T) {
require := require.New(t)
ctx := context.Background()
snowCtx := snow.DefaultContextTest()
snowCtx := corethUtils.TestSnowContext()
snowCtx.AVAXAssetID = ids.GenerateTestID()
snowCtx.XChainID = ids.GenerateTestID()
validatorState := &validators.TestState{
Expand Down Expand Up @@ -495,7 +496,7 @@ func TestAtomicTxPushGossipOutbound(t *testing.T) {
func TestAtomicTxPushGossipInbound(t *testing.T) {
require := require.New(t)
ctx := context.Background()
snowCtx := snow.DefaultContextTest()
snowCtx := corethUtils.TestSnowContext()
snowCtx.AVAXAssetID = ids.GenerateTestID()
snowCtx.XChainID = ids.GenerateTestID()
validatorState := &validators.TestState{
Expand Down
2 changes: 1 addition & 1 deletion plugin/evm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func BuildGenesisTest(t *testing.T, genesisJSON string) []byte {
}

func NewContext() *snow.Context {
ctx := snow.DefaultContextTest()
ctx := utils.TestSnowContext()
ctx.NodeID = ids.GenerateTestNodeID()
ctx.NetworkID = testNetworkID
ctx.ChainID = testCChainID
Expand Down
6 changes: 3 additions & 3 deletions precompile/contracts/warp/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"testing"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/vms/platformvm/warp"
Expand All @@ -19,6 +18,7 @@ import (
"github.com/ava-labs/coreth/precompile/contract"
"github.com/ava-labs/coreth/precompile/testutils"
"github.com/ava-labs/coreth/predicate"
corethUtils "github.com/ava-labs/coreth/utils"
"github.com/ava-labs/coreth/vmerrs"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
Expand All @@ -27,7 +27,7 @@ import (
func TestGetBlockchainID(t *testing.T) {
callerAddr := common.HexToAddress("0x0123")

defaultSnowCtx := snow.DefaultContextTest()
defaultSnowCtx := corethUtils.TestSnowContext()
blockchainID := defaultSnowCtx.ChainID

tests := map[string]testutils.PrecompileTest{
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestGetBlockchainID(t *testing.T) {
func TestSendWarpMessage(t *testing.T) {
callerAddr := common.HexToAddress("0x0123")

defaultSnowCtx := snow.DefaultContextTest()
defaultSnowCtx := corethUtils.TestSnowContext()
blockchainID := defaultSnowCtx.ChainID
sendWarpMessagePayload := utils.RandomBytes(100)

Expand Down
6 changes: 3 additions & 3 deletions precompile/contracts/warp/predicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func createSnowCtx(validatorRanges []validatorRange) *snow.Context {
}
}

snowCtx := snow.DefaultContextTest()
snowCtx := corethUtils.TestSnowContext()
state := &validators.TestState{
GetSubnetIDF: func(ctx context.Context, chainID ids.ID) (ids.ID, error) {
return sourceSubnetID, nil
Expand Down Expand Up @@ -263,7 +263,7 @@ func TestWarpMessageFromPrimaryNetwork(t *testing.T) {

predicateBytes := predicate.PackPredicate(warpMsg.Bytes())

snowCtx := snow.DefaultContextTest()
snowCtx := corethUtils.TestSnowContext()
snowCtx.SubnetID = ids.GenerateTestID()
snowCtx.ChainID = ids.GenerateTestID()
snowCtx.CChainID = cChainID
Expand Down Expand Up @@ -664,7 +664,7 @@ func initWarpPredicateTests() {
}
}

snowCtx := snow.DefaultContextTest()
snowCtx := corethUtils.TestSnowContext()
snowCtx.NetworkID = networkID
state := &validators.TestState{
GetSubnetIDF: func(ctx context.Context, chainID ids.ID) (ids.ID, error) {
Expand Down
4 changes: 2 additions & 2 deletions precompile/testutils/test_precompile.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"testing"
"time"

"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/coreth/precompile/contract"
"github.com/ava-labs/coreth/precompile/modules"
"github.com/ava-labs/coreth/precompile/precompileconfig"
"github.com/ava-labs/coreth/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
Expand Down Expand Up @@ -103,7 +103,7 @@ func (test PrecompileTest) setup(t testing.TB, module modules.Module, state cont
blockContext.EXPECT().Number().Return(big.NewInt(0)).AnyTimes()
blockContext.EXPECT().Timestamp().Return(uint64(time.Now().Unix())).AnyTimes()
}
snowContext := snow.DefaultContextTest()
snowContext := utils.TestSnowContext()

accessibleState := contract.NewMockAccessibleState(ctrl)
accessibleState.EXPECT().GetStateDB().Return(state).AnyTimes()
Expand Down
31 changes: 31 additions & 0 deletions utils/snow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package utils

import (
"github.com/ava-labs/avalanchego/api/metrics"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/utils/logging"
)

func TestSnowContext() *snow.Context {
sk, err := bls.NewSecretKey()
if err != nil {
panic(err)
}
pk := bls.PublicFromSecretKey(sk)
return &snow.Context{
NetworkID: 0,
SubnetID: ids.Empty,
ChainID: ids.Empty,
NodeID: ids.EmptyNodeID,
PublicKey: pk,
Log: logging.NoLog{},
BCLookup: ids.NewAliaser(),
Metrics: metrics.NewOptionalGatherer(),
ChainDataDir: "",
}
}
6 changes: 3 additions & 3 deletions warp/handlers/signature_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/ava-labs/avalanchego/database/memdb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"github.com/ava-labs/avalanchego/snow/engine/common"
Expand All @@ -19,13 +18,14 @@ import (
avalancheWarp "github.com/ava-labs/avalanchego/vms/platformvm/warp"
"github.com/ava-labs/avalanchego/vms/platformvm/warp/payload"
"github.com/ava-labs/coreth/plugin/evm/message"
"github.com/ava-labs/coreth/utils"
"github.com/ava-labs/coreth/warp"
"github.com/stretchr/testify/require"
)

func TestMessageSignatureHandler(t *testing.T) {
database := memdb.New()
snowCtx := snow.DefaultContextTest()
snowCtx := utils.TestSnowContext()
blsSecretKey, err := bls.NewSecretKey()
require.NoError(t, err)
warpSigner := avalancheWarp.NewSigner(blsSecretKey, snowCtx.NetworkID, snowCtx.ChainID)
Expand Down Expand Up @@ -129,7 +129,7 @@ func TestMessageSignatureHandler(t *testing.T) {

func TestBlockSignatureHandler(t *testing.T) {
database := memdb.New()
snowCtx := snow.DefaultContextTest()
snowCtx := utils.TestSnowContext()
blsSecretKey, err := bls.NewSecretKey()
require.NoError(t, err)

Expand Down
4 changes: 2 additions & 2 deletions warp/validators/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"testing"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/coreth/utils"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)
Expand All @@ -23,7 +23,7 @@ func TestGetValidatorSetPrimaryNetwork(t *testing.T) {
otherSubnetID := ids.GenerateTestID()

mockState := validators.NewMockState(ctrl)
snowCtx := snow.DefaultContextTest()
snowCtx := utils.TestSnowContext()
snowCtx.SubnetID = mySubnetID
snowCtx.ValidatorState = mockState
state := NewState(snowCtx)
Expand Down