Skip to content

Commit

Permalink
Add TestSnowContext util (ava-labs#1030)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrubabasu authored Dec 22, 2023
1 parent 61f5b21 commit 468534b
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 38 deletions.
6 changes: 3 additions & 3 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ var (
}

TestChainConfig = &ChainConfig{
AvalancheContext: AvalancheContext{snow.DefaultContextTest()},
AvalancheContext: AvalancheContext{utils.TestSnowContext()},
ChainID: big.NewInt(1),
FeeConfig: DefaultFeeConfig,
AllowFeeRecipients: false,
Expand All @@ -113,7 +113,7 @@ var (
}

TestSubnetEVMConfig = &ChainConfig{
AvalancheContext: AvalancheContext{snow.DefaultContextTest()},
AvalancheContext: AvalancheContext{utils.TestSnowContext()},
ChainID: big.NewInt(1),
FeeConfig: DefaultFeeConfig,
AllowFeeRecipients: false,
Expand All @@ -134,7 +134,7 @@ var (
}

TestPreSubnetEVMConfig = &ChainConfig{
AvalancheContext: AvalancheContext{snow.DefaultContextTest()},
AvalancheContext: AvalancheContext{utils.TestSnowContext()},
ChainID: big.NewInt(1),
FeeConfig: DefaultFeeConfig,
AllowFeeRecipients: false,
Expand Down
6 changes: 2 additions & 4 deletions plugin/evm/block_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (

"github.com/ava-labs/subnet-evm/params"
"github.com/ava-labs/subnet-evm/utils"

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

func attemptAwait(t *testing.T, wg *sync.WaitGroup, delay time.Duration) {
Expand Down Expand Up @@ -40,7 +38,7 @@ func TestBlockBuilderShutsDown(t *testing.T) {
config.SubnetEVMTimestamp = utils.TimeToNewUint64(time.Now().Add(time.Hour))

builder := &blockBuilder{
ctx: snow.DefaultContextTest(),
ctx: utils.TestSnowContext(),
chainConfig: &config,
shutdownChan: shutdownChan,
shutdownWg: wg,
Expand All @@ -57,7 +55,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
2 changes: 1 addition & 1 deletion plugin/evm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func buildGenesisTest(t *testing.T, genesisJSON string) []byte {
}

func NewContext() *snow.Context {
ctx := snow.DefaultContextTest()
ctx := utils.TestSnowContext()
ctx.NetworkID = testNetworkID
ctx.NodeID = ids.GenerateTestNodeID()
ctx.ChainID = testCChainID
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,11 +8,11 @@ import (
"testing"
"time"

"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/subnet-evm/commontype"
"github.com/ava-labs/subnet-evm/precompile/contract"
"github.com/ava-labs/subnet-evm/precompile/modules"
"github.com/ava-labs/subnet-evm/precompile/precompileconfig"
"github.com/ava-labs/subnet-evm/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
Expand Down Expand Up @@ -108,7 +108,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,21 +10,21 @@ 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"
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
avalancheWarp "github.com/ava-labs/avalanchego/vms/platformvm/warp"
"github.com/ava-labs/subnet-evm/plugin/evm/message"
"github.com/ava-labs/subnet-evm/utils"
"github.com/ava-labs/subnet-evm/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)

Expand Down Expand Up @@ -105,7 +105,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/subnet-evm/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
10 changes: 5 additions & 5 deletions x/warp/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
"testing"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils"
agoUtils "github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/vms/platformvm/warp"
avalancheWarp "github.com/ava-labs/avalanchego/vms/platformvm/warp"
Expand All @@ -19,6 +18,7 @@ import (
"github.com/ava-labs/subnet-evm/precompile/contract"
"github.com/ava-labs/subnet-evm/precompile/testutils"
"github.com/ava-labs/subnet-evm/predicate"
"github.com/ava-labs/subnet-evm/utils"
"github.com/ava-labs/subnet-evm/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 := utils.TestSnowContext()
blockchainID := defaultSnowCtx.ChainID

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

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

sendWarpMessageInput, err := PackSendWarpMessage(sendWarpMessagePayload)
require.NoError(t, err)
Expand Down
36 changes: 18 additions & 18 deletions x/warp/predicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils"
agoUtils "github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/utils/set"
Expand All @@ -23,15 +23,15 @@ import (
"github.com/ava-labs/subnet-evm/precompile/precompileconfig"
"github.com/ava-labs/subnet-evm/precompile/testutils"
"github.com/ava-labs/subnet-evm/predicate"
subnetEVMUtils "github.com/ava-labs/subnet-evm/utils"
"github.com/ava-labs/subnet-evm/utils"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

const pChainHeight uint64 = 1337

var (
_ utils.Sortable[*testValidator] = (*testValidator)(nil)
_ agoUtils.Sortable[*testValidator] = (*testValidator)(nil)

errTest = errors.New("non-nil error")
networkID = uint32(54321)
Expand Down Expand Up @@ -59,7 +59,7 @@ func init() {
for i := 0; i < numTestVdrs; i++ {
testVdrs = append(testVdrs, newTestValidator())
}
utils.Sort(testVdrs)
agoUtils.Sort(testVdrs)

vdrs = map[ids.NodeID]*validators.GetValidatorOutput{
testVdrs[0].nodeID: {
Expand Down Expand Up @@ -197,7 +197,7 @@ func createSnowCtx(validatorRanges []validatorRange) *snow.Context {
}
}

snowCtx := snow.DefaultContextTest()
snowCtx := utils.TestSnowContext()
state := &validators.TestState{
GetSubnetIDF: func(ctx context.Context, chainID ids.ID) (ids.ID, error) {
return sourceSubnetID, nil
Expand All @@ -213,7 +213,7 @@ func createSnowCtx(validatorRanges []validatorRange) *snow.Context {

func createValidPredicateTest(snowCtx *snow.Context, numKeys uint64, predicateBytes []byte) testutils.PredicateTest {
return testutils.PredicateTest{
Config: NewDefaultConfig(subnetEVMUtils.NewUint64(0)),
Config: NewDefaultConfig(utils.NewUint64(0)),
PredicateContext: &precompileconfig.PredicateContext{
SnowCtx: snowCtx,
ProposerVMBlockCtx: &block.Context{
Expand All @@ -231,7 +231,7 @@ func TestWarpMessageFromPrimaryNetwork(t *testing.T) {
require := require.New(t)
numKeys := 10
cChainID := ids.GenerateTestID()
addressedCall, err := payload.NewAddressedCall(utils.RandomBytes(20), utils.RandomBytes(100))
addressedCall, err := payload.NewAddressedCall(agoUtils.RandomBytes(20), agoUtils.RandomBytes(100))
require.NoError(err)
unsignedMsg, err := avalancheWarp.NewUnsignedMessage(networkID, cChainID, addressedCall.Bytes())
require.NoError(err)
Expand Down Expand Up @@ -262,7 +262,7 @@ func TestWarpMessageFromPrimaryNetwork(t *testing.T) {

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

snowCtx := snow.DefaultContextTest()
snowCtx := utils.TestSnowContext()
snowCtx.SubnetID = ids.GenerateTestID()
snowCtx.ChainID = ids.GenerateTestID()
snowCtx.CChainID = cChainID
Expand All @@ -279,7 +279,7 @@ func TestWarpMessageFromPrimaryNetwork(t *testing.T) {
}

test := testutils.PredicateTest{
Config: NewDefaultConfig(subnetEVMUtils.NewUint64(0)),
Config: NewDefaultConfig(utils.NewUint64(0)),
PredicateContext: &precompileconfig.PredicateContext{
SnowCtx: snowCtx,
ProposerVMBlockCtx: &block.Context{
Expand Down Expand Up @@ -309,7 +309,7 @@ func TestInvalidPredicatePacking(t *testing.T) {
predicateBytes = append(predicateBytes, byte(0x01)) // Invalidate the predicate byte packing

test := testutils.PredicateTest{
Config: NewDefaultConfig(subnetEVMUtils.NewUint64(0)),
Config: NewDefaultConfig(utils.NewUint64(0)),
PredicateContext: &precompileconfig.PredicateContext{
SnowCtx: snowCtx,
ProposerVMBlockCtx: &block.Context{
Expand Down Expand Up @@ -340,7 +340,7 @@ func TestInvalidWarpMessage(t *testing.T) {
predicateBytes := predicate.PackPredicate(warpMsgBytes)

test := testutils.PredicateTest{
Config: NewDefaultConfig(subnetEVMUtils.NewUint64(0)),
Config: NewDefaultConfig(utils.NewUint64(0)),
PredicateContext: &precompileconfig.PredicateContext{
SnowCtx: snowCtx,
ProposerVMBlockCtx: &block.Context{
Expand Down Expand Up @@ -384,7 +384,7 @@ func TestInvalidAddressedPayload(t *testing.T) {
predicateBytes := predicate.PackPredicate(warpMsgBytes)

test := testutils.PredicateTest{
Config: NewDefaultConfig(subnetEVMUtils.NewUint64(0)),
Config: NewDefaultConfig(utils.NewUint64(0)),
PredicateContext: &precompileconfig.PredicateContext{
SnowCtx: snowCtx,
ProposerVMBlockCtx: &block.Context{
Expand All @@ -400,7 +400,7 @@ func TestInvalidAddressedPayload(t *testing.T) {
}

func TestInvalidBitSet(t *testing.T) {
addressedCall, err := payload.NewAddressedCall(utils.RandomBytes(20), utils.RandomBytes(100))
addressedCall, err := payload.NewAddressedCall(agoUtils.RandomBytes(20), agoUtils.RandomBytes(100))
require.NoError(t, err)
unsignedMsg, err := avalancheWarp.NewUnsignedMessage(
networkID,
Expand Down Expand Up @@ -429,7 +429,7 @@ func TestInvalidBitSet(t *testing.T) {
})
predicateBytes := predicate.PackPredicate(msg.Bytes())
test := testutils.PredicateTest{
Config: NewDefaultConfig(subnetEVMUtils.NewUint64(0)),
Config: NewDefaultConfig(utils.NewUint64(0)),
PredicateContext: &precompileconfig.PredicateContext{
SnowCtx: snowCtx,
ProposerVMBlockCtx: &block.Context{
Expand Down Expand Up @@ -474,7 +474,7 @@ func TestWarpSignatureWeightsDefaultQuorumNumerator(t *testing.T) {
}

tests[fmt.Sprintf("default quorum %d signature(s)", numSigners)] = testutils.PredicateTest{
Config: NewDefaultConfig(subnetEVMUtils.NewUint64(0)),
Config: NewDefaultConfig(utils.NewUint64(0)),
PredicateContext: &precompileconfig.PredicateContext{
SnowCtx: snowCtx,
ProposerVMBlockCtx: &block.Context{
Expand Down Expand Up @@ -532,7 +532,7 @@ func TestWarpMultiplePredicates(t *testing.T) {
}

tests[fmt.Sprintf("multiple predicates %v", validMessageIndices)] = testutils.PredicateTest{
Config: NewDefaultConfig(subnetEVMUtils.NewUint64(0)),
Config: NewDefaultConfig(utils.NewUint64(0)),
PredicateContext: &precompileconfig.PredicateContext{
SnowCtx: snowCtx,
ProposerVMBlockCtx: &block.Context{
Expand Down Expand Up @@ -576,7 +576,7 @@ func TestWarpSignatureWeightsNonDefaultQuorumNumerator(t *testing.T) {

name := fmt.Sprintf("non-default quorum %d signature(s)", numSigners)
tests[name] = testutils.PredicateTest{
Config: NewConfig(subnetEVMUtils.NewUint64(0), uint64(nonDefaultQuorumNumerator)),
Config: NewConfig(utils.NewUint64(0), uint64(nonDefaultQuorumNumerator)),
PredicateContext: &precompileconfig.PredicateContext{
SnowCtx: snowCtx,
ProposerVMBlockCtx: &block.Context{
Expand Down Expand Up @@ -665,7 +665,7 @@ func initWarpPredicateTests() {
}
}

snowCtx := snow.DefaultContextTest()
snowCtx := utils.TestSnowContext()
snowCtx.NetworkID = networkID
state := &validators.TestState{
GetSubnetIDF: func(ctx context.Context, chainID ids.ID) (ids.ID, error) {
Expand Down

0 comments on commit 468534b

Please sign in to comment.