diff --git a/vms/platformvm/block/builder/builder_test.go b/vms/platformvm/block/builder/builder_test.go index 7c8213124d50..2d83b37615d0 100644 --- a/vms/platformvm/block/builder/builder_test.go +++ b/vms/platformvm/block/builder/builder_test.go @@ -21,6 +21,7 @@ import ( "github.com/ava-labs/avalanchego/utils/timer/mockable" "github.com/ava-labs/avalanchego/utils/units" "github.com/ava-labs/avalanchego/vms/platformvm/block" + "github.com/ava-labs/avalanchego/vms/platformvm/genesis/genesistest" "github.com/ava-labs/avalanchego/vms/platformvm/reward" "github.com/ava-labs/avalanchego/vms/platformvm/signer" "github.com/ava-labs/avalanchego/vms/platformvm/state" @@ -113,7 +114,7 @@ func TestBuildBlockShouldReward(t *testing.T) { require.NoError(err) // Create a valid [AddPermissionlessValidatorTx] - builder, txSigner := env.factory.NewWallet(preFundedKeys[0]) + builder, txSigner := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddPermissionlessValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -128,16 +129,16 @@ func TestBuildBlockShouldReward(t *testing.T) { env.ctx.AVAXAssetID, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }, reward.PercentDenominator, common.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }), ) require.NoError(err) @@ -322,7 +323,7 @@ func TestBuildBlockInvalidStakingDurations(t *testing.T) { sk, err := bls.NewSecretKey() require.NoError(err) - builder1, signer1 := env.factory.NewWallet(preFundedKeys[0]) + builder1, signer1 := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx1, err := builder1.NewAddPermissionlessValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -337,16 +338,16 @@ func TestBuildBlockInvalidStakingDurations(t *testing.T) { env.ctx.AVAXAssetID, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }, reward.PercentDenominator, common.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }), ) require.NoError(err) @@ -363,7 +364,7 @@ func TestBuildBlockInvalidStakingDurations(t *testing.T) { sk, err = bls.NewSecretKey() require.NoError(err) - builder2, signer2 := env.factory.NewWallet(preFundedKeys[2]) + builder2, signer2 := env.factory.NewWallet(genesistest.DefaultFundedKeys[2]) utx2, err := builder2.NewAddPermissionlessValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -378,16 +379,16 @@ func TestBuildBlockInvalidStakingDurations(t *testing.T) { env.ctx.AVAXAssetID, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[2].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[2].Address()}, }, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[2].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[2].Address()}, }, reward.PercentDenominator, common.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[2].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[2].Address()}, }), ) require.NoError(err) diff --git a/vms/platformvm/block/builder/helpers_test.go b/vms/platformvm/block/builder/helpers_test.go index 1d7436485e17..79b4453f6aa0 100644 --- a/vms/platformvm/block/builder/helpers_test.go +++ b/vms/platformvm/block/builder/helpers_test.go @@ -28,16 +28,12 @@ import ( "github.com/ava-labs/avalanchego/upgrade/upgradetest" "github.com/ava-labs/avalanchego/utils" "github.com/ava-labs/avalanchego/utils/constants" - "github.com/ava-labs/avalanchego/utils/crypto/secp256k1" - "github.com/ava-labs/avalanchego/utils/formatting" - "github.com/ava-labs/avalanchego/utils/formatting/address" - "github.com/ava-labs/avalanchego/utils/json" "github.com/ava-labs/avalanchego/utils/logging" "github.com/ava-labs/avalanchego/utils/timer/mockable" "github.com/ava-labs/avalanchego/utils/units" - "github.com/ava-labs/avalanchego/vms/platformvm/api" "github.com/ava-labs/avalanchego/vms/platformvm/config" "github.com/ava-labs/avalanchego/vms/platformvm/fx" + "github.com/ava-labs/avalanchego/vms/platformvm/genesis/genesistest" "github.com/ava-labs/avalanchego/vms/platformvm/metrics" "github.com/ava-labs/avalanchego/vms/platformvm/network" "github.com/ava-labs/avalanchego/vms/platformvm/reward" @@ -59,34 +55,16 @@ import ( ) const ( - defaultWeight = 10000 - trackChecksum = false -) - -var ( defaultMinStakingDuration = 24 * time.Hour defaultMaxStakingDuration = 365 * 24 * time.Hour - defaultGenesisTime = time.Date(1997, 1, 1, 0, 0, 0, 0, time.UTC) - defaultValidateStartTime = defaultGenesisTime - defaultValidateEndTime = defaultValidateStartTime.Add(10 * defaultMinStakingDuration) - defaultMinValidatorStake = 5 * units.MilliAvax - defaultBalance = 100 * defaultMinValidatorStake - preFundedKeys = secp256k1.TestKeys() - defaultTxFee = uint64(100) - testSubnet1 *txs.Tx - testSubnet1ControlKeys = preFundedKeys[0:3] - - // Node IDs of genesis validators. Initialized in init function - genesisNodeIDs []ids.NodeID + defaultTxFee = 100 * units.NanoAvax ) -func init() { - genesisNodeIDs = make([]ids.NodeID, len(preFundedKeys)) - for i := range preFundedKeys { - genesisNodeIDs[i] = ids.GenerateTestNodeID() - } -} +var ( + testSubnet1 *txs.Tx + testSubnet1ControlKeys = genesistest.DefaultFundedKeys[0:3] +) type mutableSharedMemory struct { atomic.SharedMemory @@ -141,7 +119,7 @@ func newEnvironment(t *testing.T, f upgradetest.Fork) *environment { //nolint:un rewardsCalc := reward.NewCalculator(res.config.RewardConfig) res.state = statetest.New(t, statetest.Config{ DB: res.baseDB, - Genesis: buildGenesisTest(t, res.ctx), + Genesis: genesistest.NewBytes(t, genesistest.Config{}), Validators: res.config.Validators, Context: res.ctx, Rewards: rewardsCalc, @@ -232,19 +210,19 @@ func newEnvironment(t *testing.T, f upgradetest.Fork) *environment { //nolint:un func addSubnet(t *testing.T, env *environment) { require := require.New(t) - builder, signer := env.factory.NewWallet(preFundedKeys[0]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewCreateSubnetTx( &secp256k1fx.OutputOwners{ Threshold: 2, Addrs: []ids.ShortID{ - preFundedKeys[0].PublicKey().Address(), - preFundedKeys[1].PublicKey().Address(), - preFundedKeys[2].PublicKey().Address(), + genesistest.DefaultFundedKeys[0].Address(), + genesistest.DefaultFundedKeys[1].Address(), + genesistest.DefaultFundedKeys[2].Address(), }, }, walletcommon.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }), ) require.NoError(err) @@ -275,7 +253,7 @@ func defaultConfig(f upgradetest.Fork) *config.Config { upgradetest.SetTimesTo( &upgrades, min(f, upgradetest.ApricotPhase5), - defaultValidateEndTime, + genesistest.DefaultValidatorEndTime, ) return &config.Config{ @@ -305,7 +283,7 @@ func defaultConfig(f upgradetest.Fork) *config.Config { func defaultClock() *mockable.Clock { // set time after Banff fork (and before default nextStakerTime) clk := &mockable.Clock{} - clk.Set(defaultGenesisTime) + clk.Set(genesistest.DefaultValidatorStartTime) return clk } @@ -342,60 +320,3 @@ func defaultFx(t *testing.T, clk *mockable.Clock, log logging.Logger, isBootstra } return res } - -func buildGenesisTest(t *testing.T, ctx *snow.Context) []byte { - require := require.New(t) - - genesisUTXOs := make([]api.UTXO, len(preFundedKeys)) - for i, key := range preFundedKeys { - id := key.PublicKey().Address() - addr, err := address.FormatBech32(constants.UnitTestHRP, id.Bytes()) - require.NoError(err) - genesisUTXOs[i] = api.UTXO{ - Amount: json.Uint64(defaultBalance), - Address: addr, - } - } - - genesisValidators := make([]api.GenesisPermissionlessValidator, len(genesisNodeIDs)) - for i, nodeID := range genesisNodeIDs { - addr, err := address.FormatBech32(constants.UnitTestHRP, nodeID.Bytes()) - require.NoError(err) - genesisValidators[i] = api.GenesisPermissionlessValidator{ - GenesisValidator: api.GenesisValidator{ - StartTime: json.Uint64(defaultValidateStartTime.Unix()), - EndTime: json.Uint64(defaultValidateEndTime.Unix()), - NodeID: nodeID, - }, - RewardOwner: &api.Owner{ - Threshold: 1, - Addresses: []string{addr}, - }, - Staked: []api.UTXO{{ - Amount: json.Uint64(defaultWeight), - Address: addr, - }}, - DelegationFee: reward.PercentDenominator, - } - } - - buildGenesisArgs := api.BuildGenesisArgs{ - NetworkID: json.Uint32(constants.UnitTestID), - AvaxAssetID: ctx.AVAXAssetID, - UTXOs: genesisUTXOs, - Validators: genesisValidators, - Chains: nil, - Time: json.Uint64(defaultGenesisTime.Unix()), - InitialSupply: json.Uint64(360 * units.MegaAvax), - Encoding: formatting.Hex, - } - - buildGenesisResponse := api.BuildGenesisReply{} - platformvmSS := api.StaticService{} - require.NoError(platformvmSS.BuildGenesis(nil, &buildGenesisArgs, &buildGenesisResponse)) - - genesisBytes, err := formatting.Decode(buildGenesisResponse.Encoding, buildGenesisResponse.Bytes) - require.NoError(err) - - return genesisBytes -} diff --git a/vms/platformvm/block/builder/standard_block_test.go b/vms/platformvm/block/builder/standard_block_test.go index 6909c0efd794..e97cbee313fd 100644 --- a/vms/platformvm/block/builder/standard_block_test.go +++ b/vms/platformvm/block/builder/standard_block_test.go @@ -14,6 +14,7 @@ import ( "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/upgrade/upgradetest" "github.com/ava-labs/avalanchego/vms/components/avax" + "github.com/ava-labs/avalanchego/vms/platformvm/genesis/genesistest" "github.com/ava-labs/avalanchego/vms/platformvm/status" "github.com/ava-labs/avalanchego/vms/platformvm/txs" "github.com/ava-labs/avalanchego/vms/secp256k1fx" @@ -33,7 +34,7 @@ func TestAtomicTxImports(t *testing.T) { OutputIndex: 1, } amount := uint64(70000) - recipientKey := preFundedKeys[1] + recipientKey := genesistest.DefaultFundedKeys[1] m := atomic.NewMemory(prefixdb.New([]byte{5}, env.baseDB)) @@ -46,7 +47,7 @@ func TestAtomicTxImports(t *testing.T) { Amt: amount, OutputOwners: secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{recipientKey.PublicKey().Address()}, + Addrs: []ids.ShortID{recipientKey.Address()}, }, }, } @@ -59,7 +60,7 @@ func TestAtomicTxImports(t *testing.T) { Key: inputID[:], Value: utxoBytes, Traits: [][]byte{ - recipientKey.PublicKey().Address().Bytes(), + recipientKey.Address().Bytes(), }, }}}, })) @@ -69,7 +70,7 @@ func TestAtomicTxImports(t *testing.T) { env.ctx.XChainID, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{recipientKey.PublicKey().Address()}, + Addrs: []ids.ShortID{recipientKey.Address()}, }, ) require.NoError(err) diff --git a/vms/platformvm/block/executor/helpers_test.go b/vms/platformvm/block/executor/helpers_test.go index 08b1d334d4df..947fd15a8691 100644 --- a/vms/platformvm/block/executor/helpers_test.go +++ b/vms/platformvm/block/executor/helpers_test.go @@ -30,15 +30,12 @@ import ( "github.com/ava-labs/avalanchego/utils" "github.com/ava-labs/avalanchego/utils/constants" "github.com/ava-labs/avalanchego/utils/crypto/secp256k1" - "github.com/ava-labs/avalanchego/utils/formatting" - "github.com/ava-labs/avalanchego/utils/formatting/address" - "github.com/ava-labs/avalanchego/utils/json" "github.com/ava-labs/avalanchego/utils/logging" "github.com/ava-labs/avalanchego/utils/timer/mockable" "github.com/ava-labs/avalanchego/utils/units" - "github.com/ava-labs/avalanchego/vms/platformvm/api" "github.com/ava-labs/avalanchego/vms/platformvm/config" "github.com/ava-labs/avalanchego/vms/platformvm/fx" + "github.com/ava-labs/avalanchego/vms/platformvm/genesis/genesistest" "github.com/ava-labs/avalanchego/vms/platformvm/metrics" "github.com/ava-labs/avalanchego/vms/platformvm/reward" "github.com/ava-labs/avalanchego/vms/platformvm/state" @@ -61,34 +58,13 @@ const ( pending stakerStatus = iota current - defaultWeight = 10000 - trackChecksum = false -) - -var ( defaultMinStakingDuration = 24 * time.Hour defaultMaxStakingDuration = 365 * 24 * time.Hour - defaultGenesisTime = time.Date(1997, 1, 1, 0, 0, 0, 0, time.UTC) - defaultValidateStartTime = defaultGenesisTime - defaultValidateEndTime = defaultValidateStartTime.Add(10 * defaultMinStakingDuration) - defaultMinValidatorStake = 5 * units.MilliAvax - defaultBalance = 100 * defaultMinValidatorStake - preFundedKeys = secp256k1.TestKeys() - avaxAssetID = ids.ID{'y', 'e', 'e', 't'} - defaultTxFee = uint64(100) - - testSubnet1 *txs.Tx - - // Node IDs of genesis validators. Initialized in init function - genesisNodeIDs []ids.NodeID + + defaultTxFee = 100 * units.NanoAvax ) -func init() { - genesisNodeIDs = make([]ids.NodeID, len(preFundedKeys)) - for i := range preFundedKeys { - genesisNodeIDs[i] = ids.GenerateTestNodeID() - } -} +var testSubnet1 *txs.Tx type stakerStatus uint @@ -139,7 +115,6 @@ func newEnvironment(t *testing.T, ctrl *gomock.Controller, f upgradetest.Fork) * m := atomic.NewMemory(atomicDB) res.ctx = snowtest.Context(t, snowtest.PChainID) - res.ctx.AVAXAssetID = avaxAssetID res.ctx.SharedMemory = m.NewSharedMemory(res.ctx.ChainID) res.fx = defaultFx(res.clk, res.ctx.Log, res.isBootstrapped.Get()) @@ -149,7 +124,7 @@ func newEnvironment(t *testing.T, ctrl *gomock.Controller, f upgradetest.Fork) * if ctrl == nil { res.state = statetest.New(t, statetest.Config{ DB: res.baseDB, - Genesis: buildGenesisTest(res.ctx), + Genesis: genesistest.NewBytes(t, genesistest.Config{}), Validators: res.config.Validators, Context: res.ctx, Rewards: rewardsCalc, @@ -248,19 +223,19 @@ func newEnvironment(t *testing.T, ctrl *gomock.Controller, f upgradetest.Fork) * } func addSubnet(env *environment) { - builder, signer := env.factory.NewWallet(preFundedKeys[0]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewCreateSubnetTx( &secp256k1fx.OutputOwners{ Threshold: 2, Addrs: []ids.ShortID{ - preFundedKeys[0].PublicKey().Address(), - preFundedKeys[1].PublicKey().Address(), - preFundedKeys[2].PublicKey().Address(), + genesistest.DefaultFundedKeys[0].Address(), + genesistest.DefaultFundedKeys[1].Address(), + genesistest.DefaultFundedKeys[2].Address(), }, }, walletcommon.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }), ) if err != nil { @@ -303,7 +278,7 @@ func defaultConfig(f upgradetest.Fork) *config.Config { upgradetest.SetTimesTo( &upgrades, min(f, upgradetest.ApricotPhase5), - defaultValidateEndTime, + genesistest.DefaultValidatorEndTime, ) return &config.Config{ @@ -332,7 +307,7 @@ func defaultConfig(f upgradetest.Fork) *config.Config { func defaultClock() *mockable.Clock { clk := &mockable.Clock{} - clk.Set(defaultGenesisTime) + clk.Set(genesistest.DefaultValidatorStartTime) return clk } @@ -372,69 +347,6 @@ func defaultFx(clk *mockable.Clock, log logging.Logger, isBootstrapped bool) fx. return res } -func buildGenesisTest(ctx *snow.Context) []byte { - genesisUTXOs := make([]api.UTXO, len(preFundedKeys)) - for i, key := range preFundedKeys { - id := key.PublicKey().Address() - addr, err := address.FormatBech32(constants.UnitTestHRP, id.Bytes()) - if err != nil { - panic(err) - } - genesisUTXOs[i] = api.UTXO{ - Amount: json.Uint64(defaultBalance), - Address: addr, - } - } - - genesisValidators := make([]api.GenesisPermissionlessValidator, len(genesisNodeIDs)) - for i, nodeID := range genesisNodeIDs { - addr, err := address.FormatBech32(constants.UnitTestHRP, nodeID.Bytes()) - if err != nil { - panic(err) - } - genesisValidators[i] = api.GenesisPermissionlessValidator{ - GenesisValidator: api.GenesisValidator{ - StartTime: json.Uint64(defaultValidateStartTime.Unix()), - EndTime: json.Uint64(defaultValidateEndTime.Unix()), - NodeID: nodeID, - }, - RewardOwner: &api.Owner{ - Threshold: 1, - Addresses: []string{addr}, - }, - Staked: []api.UTXO{{ - Amount: json.Uint64(defaultWeight), - Address: addr, - }}, - DelegationFee: reward.PercentDenominator, - } - } - - buildGenesisArgs := api.BuildGenesisArgs{ - NetworkID: json.Uint32(constants.UnitTestID), - AvaxAssetID: ctx.AVAXAssetID, - UTXOs: genesisUTXOs, - Validators: genesisValidators, - Chains: nil, - Time: json.Uint64(defaultGenesisTime.Unix()), - InitialSupply: json.Uint64(360 * units.MegaAvax), - Encoding: formatting.Hex, - } - - buildGenesisResponse := api.BuildGenesisReply{} - platformvmSS := api.StaticService{} - if err := platformvmSS.BuildGenesis(nil, &buildGenesisArgs, &buildGenesisResponse); err != nil { - panic(fmt.Errorf("problem while building platform chain's genesis state: %w", err)) - } - - genesisBytes, err := formatting.Decode(buildGenesisResponse.Encoding, buildGenesisResponse.Bytes) - if err != nil { - panic(err) - } - - return genesisBytes -} - func addPendingValidator( env *environment, startTime time.Time, diff --git a/vms/platformvm/block/executor/proposal_block_test.go b/vms/platformvm/block/executor/proposal_block_test.go index db7d5b44c532..1060c1c59be0 100644 --- a/vms/platformvm/block/executor/proposal_block_test.go +++ b/vms/platformvm/block/executor/proposal_block_test.go @@ -24,6 +24,7 @@ import ( "github.com/ava-labs/avalanchego/vms/components/avax" "github.com/ava-labs/avalanchego/vms/components/gas" "github.com/ava-labs/avalanchego/vms/platformvm/block" + "github.com/ava-labs/avalanchego/vms/platformvm/genesis/genesistest" "github.com/ava-labs/avalanchego/vms/platformvm/reward" "github.com/ava-labs/avalanchego/vms/platformvm/signer" "github.com/ava-labs/avalanchego/vms/platformvm/state" @@ -145,7 +146,7 @@ func TestBanffProposalBlockTimeVerification(t *testing.T) { env := newEnvironment(t, ctrl, upgradetest.Banff) // create parentBlock. It's a standard one for simplicity - parentTime := defaultGenesisTime + parentTime := genesistest.DefaultValidatorStartTime parentHeight := uint64(2022) banffParentBlk, err := block.NewApricotStandardBlock( @@ -375,15 +376,15 @@ func TestBanffProposalBlockUpdateStakers(t *testing.T) { staker0 := staker{ nodeID: ids.BuildTestNodeID([]byte{0xf0}), rewardAddress: ids.ShortID{0xf0}, - startTime: defaultGenesisTime, + startTime: genesistest.DefaultValidatorStartTime, endTime: time.Time{}, // actual endTime depends on specific test } staker1 := staker{ nodeID: ids.BuildTestNodeID([]byte{0xf1}), rewardAddress: ids.ShortID{0xf1}, - startTime: defaultGenesisTime.Add(1 * time.Minute), - endTime: defaultGenesisTime.Add(10 * defaultMinStakingDuration).Add(1 * time.Minute), + startTime: genesistest.DefaultValidatorStartTime.Add(1 * time.Minute), + endTime: genesistest.DefaultValidatorStartTime.Add(10 * defaultMinStakingDuration).Add(1 * time.Minute), } staker2 := staker{ nodeID: ids.BuildTestNodeID([]byte{0xf2}), @@ -509,25 +510,6 @@ func TestBanffProposalBlockUpdateStakers(t *testing.T) { staker5.nodeID: pending, }, }, - { - description: "advance time to staker5 start", - stakers: []staker{staker1, staker2, staker3, staker4, staker5}, - advanceTimeTo: []time.Time{staker1.startTime, staker2.startTime, staker3.startTime, staker5.startTime}, - expectedStakers: map[ids.NodeID]stakerStatus{ - staker1.nodeID: current, - - // Staker2's end time matches staker5's start time, so typically - // the block builder would produce a ProposalBlock to remove - // staker2 when advancing the time. However, this test injects - // staker0 into the staker set artificially to advance the time. - // This means that staker2 is not removed by the ProposalBlock - // when advancing the time. - staker2.nodeID: current, - staker3.nodeID: current, - staker4.nodeID: current, - staker5.nodeID: current, - }, - }, } for _, test := range tests { @@ -539,7 +521,7 @@ func TestBanffProposalBlockUpdateStakers(t *testing.T) { env.config.TrackedSubnets.Add(subnetID) for _, staker := range test.stakers { - builder, signer := env.factory.NewWallet(preFundedKeys[0]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: staker.nodeID, @@ -573,7 +555,7 @@ func TestBanffProposalBlockUpdateStakers(t *testing.T) { } for _, subStaker := range test.subnetStakers { - builder, signer := env.factory.NewWallet(preFundedKeys[0], preFundedKeys[1]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) utx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -610,7 +592,7 @@ func TestBanffProposalBlockUpdateStakers(t *testing.T) { // add Staker0 (with the right end time) to state // so to allow proposalBlk issuance staker0.endTime = newTime - builder, signer := env.factory.NewWallet(preFundedKeys[0], preFundedKeys[1]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: staker0.nodeID, @@ -717,15 +699,14 @@ func TestBanffProposalBlockRemoveSubnetValidator(t *testing.T) { env.config.TrackedSubnets.Add(subnetID) // Add a subnet validator to the staker set - subnetValidatorNodeID := genesisNodeIDs[0] - subnetVdr1StartTime := defaultValidateStartTime - subnetVdr1EndTime := defaultValidateStartTime.Add(defaultMinStakingDuration) - builder, signer := env.factory.NewWallet(preFundedKeys[0], preFundedKeys[1]) + subnetValidatorNodeID := genesistest.DefaultNodeIDs[0] + subnetVdr1EndTime := genesistest.DefaultValidatorStartTime.Add(defaultMinStakingDuration) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) utx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ NodeID: subnetValidatorNodeID, - Start: uint64(subnetVdr1StartTime.Unix()), + Start: genesistest.DefaultValidatorStartTimeUnix, End: uint64(subnetVdr1EndTime.Unix()), Wght: 1, }, @@ -752,7 +733,7 @@ func TestBanffProposalBlockRemoveSubnetValidator(t *testing.T) { // The above validator is now part of the staking set // Queue a staker that joins the staker set after the above validator leaves - subnetVdr2NodeID := genesisNodeIDs[1] + subnetVdr2NodeID := genesistest.DefaultNodeIDs[1] utx, err = builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -785,12 +766,11 @@ func TestBanffProposalBlockRemoveSubnetValidator(t *testing.T) { // add Staker0 (with the right end time) to state // so to allow proposalBlk issuance - staker0StartTime := defaultValidateStartTime staker0EndTime := subnetVdr1EndTime uVdrTx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: ids.GenerateTestNodeID(), - Start: uint64(staker0StartTime.Unix()), + Start: genesistest.DefaultValidatorStartTimeUnix, End: uint64(staker0EndTime.Unix()), Wght: 10, }, @@ -876,11 +856,11 @@ func TestBanffProposalBlockTrackedSubnet(t *testing.T) { } // Add a subnet validator to the staker set - subnetValidatorNodeID := genesisNodeIDs[0] - subnetVdr1StartTime := defaultGenesisTime.Add(1 * time.Minute) - subnetVdr1EndTime := defaultGenesisTime.Add(10 * defaultMinStakingDuration).Add(1 * time.Minute) + subnetValidatorNodeID := genesistest.DefaultNodeIDs[0] + subnetVdr1StartTime := genesistest.DefaultValidatorStartTime.Add(1 * time.Minute) + subnetVdr1EndTime := genesistest.DefaultValidatorStartTime.Add(10 * defaultMinStakingDuration).Add(1 * time.Minute) - builder, signer := env.factory.NewWallet(preFundedKeys[0], preFundedKeys[1]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) utx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -911,7 +891,7 @@ func TestBanffProposalBlockTrackedSubnet(t *testing.T) { // add Staker0 (with the right end time) to state // so to allow proposalBlk issuance - staker0StartTime := defaultGenesisTime + staker0StartTime := genesistest.DefaultValidatorStartTime staker0EndTime := subnetVdr1StartTime uVdrTx, err := builder.NewAddValidatorTx( @@ -986,7 +966,7 @@ func TestBanffProposalBlockDelegatorStakerWeight(t *testing.T) { // Case: Timestamp is after next validator start time // Add a pending validator - pendingValidatorStartTime := defaultGenesisTime.Add(1 * time.Second) + pendingValidatorStartTime := genesistest.DefaultValidatorStartTime.Add(1 * time.Second) pendingValidatorEndTime := pendingValidatorStartTime.Add(defaultMaxStakingDuration) nodeID := ids.GenerateTestNodeID() rewardAddress := ids.GenerateTestShortID() @@ -996,15 +976,15 @@ func TestBanffProposalBlockDelegatorStakerWeight(t *testing.T) { pendingValidatorEndTime, nodeID, rewardAddress, - []*secp256k1.PrivateKey{preFundedKeys[0]}, + []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}, ) require.NoError(err) // add Staker0 (with the right end time) to state // just to allow proposalBlk issuance (with a reward Tx) - staker0StartTime := defaultGenesisTime + staker0StartTime := genesistest.DefaultValidatorStartTime staker0EndTime := pendingValidatorStartTime - builder, signer := env.factory.NewWallet(preFundedKeys[0], preFundedKeys[1]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: ids.GenerateTestNodeID(), @@ -1075,7 +1055,7 @@ func TestBanffProposalBlockDelegatorStakerWeight(t *testing.T) { pendingDelegatorStartTime := pendingValidatorStartTime.Add(1 * time.Second) pendingDelegatorEndTime := pendingDelegatorStartTime.Add(1 * time.Second) - builder, signer = env.factory.NewWallet(preFundedKeys[0], preFundedKeys[1], preFundedKeys[4]) + builder, signer = env.factory.NewWallet(genesistest.DefaultFundedKeys[0], genesistest.DefaultFundedKeys[1], genesistest.DefaultFundedKeys[4]) uDelTx, err := builder.NewAddDelegatorTx( &txs.Validator{ NodeID: nodeID, @@ -1085,7 +1065,7 @@ func TestBanffProposalBlockDelegatorStakerWeight(t *testing.T) { }, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }, ) require.NoError(err) @@ -1106,7 +1086,7 @@ func TestBanffProposalBlockDelegatorStakerWeight(t *testing.T) { // add Staker0 (with the right end time) to state // so to allow proposalBlk issuance staker0EndTime = pendingDelegatorStartTime - builder, signer = env.factory.NewWallet(preFundedKeys[0], preFundedKeys[1]) + builder, signer = env.factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) utx, err = builder.NewAddValidatorTx( &txs.Validator{ NodeID: ids.GenerateTestNodeID(), @@ -1181,10 +1161,10 @@ func TestBanffProposalBlockDelegatorStakers(t *testing.T) { // Case: Timestamp is after next validator start time // Add a pending validator - pendingValidatorStartTime := defaultGenesisTime.Add(1 * time.Second) + pendingValidatorStartTime := genesistest.DefaultValidatorStartTime.Add(1 * time.Second) pendingValidatorEndTime := pendingValidatorStartTime.Add(defaultMinStakingDuration) nodeIDKey, _ := secp256k1.NewPrivateKey() - rewardAddress := nodeIDKey.PublicKey().Address() + rewardAddress := nodeIDKey.Address() nodeID := ids.BuildTestNodeID(rewardAddress[:]) _, err := addPendingValidator( @@ -1193,15 +1173,15 @@ func TestBanffProposalBlockDelegatorStakers(t *testing.T) { pendingValidatorEndTime, nodeID, rewardAddress, - []*secp256k1.PrivateKey{preFundedKeys[0]}, + []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}, ) require.NoError(err) // add Staker0 (with the right end time) to state // so to allow proposalBlk issuance - staker0StartTime := defaultGenesisTime + staker0StartTime := genesistest.DefaultValidatorStartTime staker0EndTime := pendingValidatorStartTime - builder, txSigner := env.factory.NewWallet(preFundedKeys[0], preFundedKeys[1]) + builder, txSigner := env.factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: ids.GenerateTestNodeID(), @@ -1271,7 +1251,7 @@ func TestBanffProposalBlockDelegatorStakers(t *testing.T) { // Add delegator pendingDelegatorStartTime := pendingValidatorStartTime.Add(1 * time.Second) pendingDelegatorEndTime := pendingDelegatorStartTime.Add(defaultMinStakingDuration) - builder, txSigner = env.factory.NewWallet(preFundedKeys[0], preFundedKeys[1], preFundedKeys[4]) + builder, txSigner = env.factory.NewWallet(genesistest.DefaultFundedKeys[0], genesistest.DefaultFundedKeys[1], genesistest.DefaultFundedKeys[4]) uDelTx, err := builder.NewAddDelegatorTx( &txs.Validator{ NodeID: nodeID, @@ -1281,7 +1261,7 @@ func TestBanffProposalBlockDelegatorStakers(t *testing.T) { }, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }, ) require.NoError(err) @@ -1302,7 +1282,7 @@ func TestBanffProposalBlockDelegatorStakers(t *testing.T) { // add Staker0 (with the right end time) to state // so to allow proposalBlk issuance staker0EndTime = pendingDelegatorStartTime - builder, txSigner = env.factory.NewWallet(preFundedKeys[0], preFundedKeys[1]) + builder, txSigner = env.factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) utx, err = builder.NewAddValidatorTx( &txs.Validator{ NodeID: ids.GenerateTestNodeID(), @@ -1386,7 +1366,7 @@ func TestAddValidatorProposalBlock(t *testing.T) { sk, err := bls.NewSecretKey() require.NoError(err) - builder, txSigner := env.factory.NewWallet(preFundedKeys[0], preFundedKeys[1], preFundedKeys[4]) + builder, txSigner := env.factory.NewWallet(genesistest.DefaultFundedKeys[0], genesistest.DefaultFundedKeys[1], genesistest.DefaultFundedKeys[4]) utx, err := builder.NewAddPermissionlessValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -1401,11 +1381,11 @@ func TestAddValidatorProposalBlock(t *testing.T) { env.ctx.AVAXAssetID, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }, 10000, ) @@ -1485,11 +1465,11 @@ func TestAddValidatorProposalBlock(t *testing.T) { env.ctx.AVAXAssetID, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }, 10000, ) diff --git a/vms/platformvm/block/executor/standard_block_test.go b/vms/platformvm/block/executor/standard_block_test.go index e0154b8c9ef0..3b9b55408ef1 100644 --- a/vms/platformvm/block/executor/standard_block_test.go +++ b/vms/platformvm/block/executor/standard_block_test.go @@ -14,6 +14,7 @@ import ( "github.com/ava-labs/avalanchego/database" "github.com/ava-labs/avalanchego/ids" + "github.com/ava-labs/avalanchego/snow/snowtest" "github.com/ava-labs/avalanchego/upgrade/upgradetest" "github.com/ava-labs/avalanchego/utils/constants" "github.com/ava-labs/avalanchego/utils/crypto/secp256k1" @@ -21,6 +22,7 @@ import ( "github.com/ava-labs/avalanchego/vms/components/avax" "github.com/ava-labs/avalanchego/vms/components/gas" "github.com/ava-labs/avalanchego/vms/platformvm/block" + "github.com/ava-labs/avalanchego/vms/platformvm/genesis/genesistest" "github.com/ava-labs/avalanchego/vms/platformvm/state" "github.com/ava-labs/avalanchego/vms/platformvm/status" "github.com/ava-labs/avalanchego/vms/platformvm/txs" @@ -143,7 +145,7 @@ func TestBanffStandardBlockTimeVerification(t *testing.T) { TxID: txID, }, Asset: avax.Asset{ - ID: avaxAssetID, + ID: snowtest.AVAXAssetID, }, Out: &secp256k1fx.TransferOutput{ Amt: env.config.StaticFeeConfig.CreateSubnetTxFee, @@ -296,7 +298,7 @@ func TestBanffStandardBlockUpdatePrimaryNetworkStakers(t *testing.T) { // Case: Timestamp is after next validator start time // Add a pending validator - pendingValidatorStartTime := defaultGenesisTime.Add(1 * time.Second) + pendingValidatorStartTime := genesistest.DefaultValidatorStartTime.Add(1 * time.Second) pendingValidatorEndTime := pendingValidatorStartTime.Add(defaultMinStakingDuration) nodeID := ids.GenerateTestNodeID() rewardAddress := ids.GenerateTestShortID() @@ -306,7 +308,7 @@ func TestBanffStandardBlockUpdatePrimaryNetworkStakers(t *testing.T) { pendingValidatorEndTime, nodeID, rewardAddress, - []*secp256k1.PrivateKey{preFundedKeys[0]}, + []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}, ) require.NoError(err) @@ -363,8 +365,8 @@ func TestBanffStandardBlockUpdateStakers(t *testing.T) { staker1 := staker{ nodeID: ids.BuildTestNodeID([]byte{0xf1}), rewardAddress: ids.ShortID{0xf1}, - startTime: defaultGenesisTime.Add(1 * time.Minute), - endTime: defaultGenesisTime.Add(10 * defaultMinStakingDuration).Add(1 * time.Minute), + startTime: genesistest.DefaultValidatorStartTime.Add(1 * time.Minute), + endTime: genesistest.DefaultValidatorStartTime.Add(10 * defaultMinStakingDuration).Add(1 * time.Minute), } staker2 := staker{ nodeID: ids.BuildTestNodeID([]byte{0xf2}), @@ -505,13 +507,13 @@ func TestBanffStandardBlockUpdateStakers(t *testing.T) { staker.endTime, staker.nodeID, staker.rewardAddress, - []*secp256k1.PrivateKey{preFundedKeys[0]}, + []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}, ) require.NoError(err) } for _, staker := range test.subnetStakers { - builder, signer := env.factory.NewWallet(preFundedKeys[0], preFundedKeys[1]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) utx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -602,15 +604,14 @@ func TestBanffStandardBlockRemoveSubnetValidator(t *testing.T) { env.config.TrackedSubnets.Add(subnetID) // Add a subnet validator to the staker set - subnetValidatorNodeID := genesisNodeIDs[0] - subnetVdr1StartTime := defaultValidateStartTime - subnetVdr1EndTime := defaultValidateStartTime.Add(defaultMinStakingDuration) - builder, signer := env.factory.NewWallet(preFundedKeys[0], preFundedKeys[1]) + subnetValidatorNodeID := genesistest.DefaultNodeIDs[0] + subnetVdr1EndTime := genesistest.DefaultValidatorStartTime.Add(defaultMinStakingDuration) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) utx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ NodeID: subnetValidatorNodeID, - Start: uint64(subnetVdr1StartTime.Unix()), + Start: genesistest.DefaultValidatorStartTimeUnix, End: uint64(subnetVdr1EndTime.Unix()), Wght: 1, }, @@ -637,7 +638,7 @@ func TestBanffStandardBlockRemoveSubnetValidator(t *testing.T) { // The above validator is now part of the staking set // Queue a staker that joins the staker set after the above validator leaves - subnetVdr2NodeID := genesisNodeIDs[1] + subnetVdr2NodeID := genesistest.DefaultNodeIDs[1] utx, err = builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -708,10 +709,10 @@ func TestBanffStandardBlockTrackedSubnet(t *testing.T) { } // Add a subnet validator to the staker set - subnetValidatorNodeID := genesisNodeIDs[0] - subnetVdr1StartTime := defaultGenesisTime.Add(1 * time.Minute) - subnetVdr1EndTime := defaultGenesisTime.Add(10 * defaultMinStakingDuration).Add(1 * time.Minute) - builder, signer := env.factory.NewWallet(preFundedKeys[0], preFundedKeys[1]) + subnetValidatorNodeID := genesistest.DefaultNodeIDs[0] + subnetVdr1StartTime := genesistest.DefaultValidatorStartTime.Add(1 * time.Minute) + subnetVdr1EndTime := genesistest.DefaultValidatorStartTime.Add(10 * defaultMinStakingDuration).Add(1 * time.Minute) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) utx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -768,7 +769,7 @@ func TestBanffStandardBlockDelegatorStakerWeight(t *testing.T) { // Case: Timestamp is after next validator start time // Add a pending validator - pendingValidatorStartTime := defaultGenesisTime.Add(1 * time.Second) + pendingValidatorStartTime := genesistest.DefaultValidatorStartTime.Add(1 * time.Second) pendingValidatorEndTime := pendingValidatorStartTime.Add(defaultMaxStakingDuration) nodeID := ids.GenerateTestNodeID() rewardAddress := ids.GenerateTestShortID() @@ -778,7 +779,7 @@ func TestBanffStandardBlockDelegatorStakerWeight(t *testing.T) { pendingValidatorEndTime, nodeID, rewardAddress, - []*secp256k1.PrivateKey{preFundedKeys[0]}, + []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}, ) require.NoError(err) @@ -806,7 +807,7 @@ func TestBanffStandardBlockDelegatorStakerWeight(t *testing.T) { pendingDelegatorStartTime := pendingValidatorStartTime.Add(1 * time.Second) pendingDelegatorEndTime := pendingDelegatorStartTime.Add(1 * time.Second) - builder, signer := env.factory.NewWallet(preFundedKeys[0], preFundedKeys[1], preFundedKeys[4]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0], genesistest.DefaultFundedKeys[1], genesistest.DefaultFundedKeys[4]) utx, err := builder.NewAddDelegatorTx( &txs.Validator{ NodeID: nodeID, @@ -816,7 +817,7 @@ func TestBanffStandardBlockDelegatorStakerWeight(t *testing.T) { }, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }, ) require.NoError(err) diff --git a/vms/platformvm/block/executor/verifier_test.go b/vms/platformvm/block/executor/verifier_test.go index 73bef61aa9f7..d2b131a3f2a3 100644 --- a/vms/platformvm/block/executor/verifier_test.go +++ b/vms/platformvm/block/executor/verifier_test.go @@ -23,6 +23,7 @@ import ( "github.com/ava-labs/avalanchego/vms/components/verify" "github.com/ava-labs/avalanchego/vms/platformvm/block" "github.com/ava-labs/avalanchego/vms/platformvm/config" + "github.com/ava-labs/avalanchego/vms/platformvm/genesis/genesistest" "github.com/ava-labs/avalanchego/vms/platformvm/state" "github.com/ava-labs/avalanchego/vms/platformvm/status" "github.com/ava-labs/avalanchego/vms/platformvm/txs" @@ -469,7 +470,7 @@ func TestVerifyUnverifiedParent(t *testing.T) { func TestBanffAbortBlockTimestampChecks(t *testing.T) { ctrl := gomock.NewController(t) - now := defaultGenesisTime.Add(time.Hour) + now := genesistest.DefaultValidatorStartTime.Add(time.Hour) tests := []struct { description string @@ -532,7 +533,7 @@ func TestBanffAbortBlockTimestampChecks(t *testing.T) { require.NoError(err) // setup parent state - parentTime := defaultGenesisTime + parentTime := genesistest.DefaultValidatorStartTime s.EXPECT().GetLastAccepted().Return(parentID).Times(3) s.EXPECT().GetTimestamp().Return(parentTime).Times(3) s.EXPECT().GetFeeState().Return(gas.State{}).Times(3) @@ -566,7 +567,7 @@ func TestBanffAbortBlockTimestampChecks(t *testing.T) { func TestBanffCommitBlockTimestampChecks(t *testing.T) { ctrl := gomock.NewController(t) - now := defaultGenesisTime.Add(time.Hour) + now := genesistest.DefaultValidatorStartTime.Add(time.Hour) tests := []struct { description string @@ -629,7 +630,7 @@ func TestBanffCommitBlockTimestampChecks(t *testing.T) { require.NoError(err) // setup parent state - parentTime := defaultGenesisTime + parentTime := genesistest.DefaultValidatorStartTime s.EXPECT().GetLastAccepted().Return(parentID).Times(3) s.EXPECT().GetTimestamp().Return(parentTime).Times(3) s.EXPECT().GetFeeState().Return(gas.State{}).Times(3) diff --git a/vms/platformvm/block/parse_test.go b/vms/platformvm/block/parse_test.go index 8bebb50753c4..b63d4bbc513f 100644 --- a/vms/platformvm/block/parse_test.go +++ b/vms/platformvm/block/parse_test.go @@ -296,7 +296,7 @@ func testAtomicTx() (*txs.Tx, error) { Amt: uint64(1234), OutputOwners: secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{preFundedKeys[0].Address()}, }, }, }}, @@ -345,7 +345,7 @@ func testDecisionTxs() ([]*txs.Tx, error) { Amt: uint64(1234), OutputOwners: secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{preFundedKeys[0].Address()}, }, }, }}, diff --git a/vms/platformvm/genesis/genesistest/genesis.go b/vms/platformvm/genesis/genesistest/genesis.go index 24e16ecf0934..9a9f9a2a48c4 100644 --- a/vms/platformvm/genesis/genesistest/genesis.go +++ b/vms/platformvm/genesis/genesistest/genesis.go @@ -10,98 +10,166 @@ import ( "github.com/stretchr/testify/require" "github.com/ava-labs/avalanchego/ids" + "github.com/ava-labs/avalanchego/snow/snowtest" + "github.com/ava-labs/avalanchego/upgrade" "github.com/ava-labs/avalanchego/utils/constants" + "github.com/ava-labs/avalanchego/utils/crypto/secp256k1" "github.com/ava-labs/avalanchego/utils/units" "github.com/ava-labs/avalanchego/vms/components/avax" - "github.com/ava-labs/avalanchego/vms/platformvm/genesis" "github.com/ava-labs/avalanchego/vms/platformvm/reward" "github.com/ava-labs/avalanchego/vms/platformvm/txs" "github.com/ava-labs/avalanchego/vms/secp256k1fx" + + platformvmgenesis "github.com/ava-labs/avalanchego/vms/platformvm/genesis" +) + +const ( + DefaultValidatorDuration = 28 * 24 * time.Hour + DefaultValidatorWeight = 5 * units.MilliAvax + DefaultInitialBalance = 1 * units.Avax + + ValidatorDelegationShares = reward.PercentDenominator + XChainName = "x" + InitialSupply = 360 * units.MegaAvax ) var ( - AVAXAssetID = ids.GenerateTestID() - AVAXAsset = avax.Asset{ID: AVAXAssetID} - - ValidatorNodeID = ids.GenerateTestNodeID() - Time = time.Now().Round(time.Second) - TimeUnix = uint64(Time.Unix()) - ValidatorDuration = 28 * 24 * time.Hour - ValidatorEndTime = Time.Add(ValidatorDuration) - ValidatorEndTimeUnix = uint64(ValidatorEndTime.Unix()) - ValidatorWeight = units.Avax - ValidatorRewardsOwner = &secp256k1fx.OutputOwners{} - ValidatorDelegationShares uint32 = reward.PercentDenominator - - XChainName = "x" - - InitialBalance = units.Schmeckle - InitialSupply = ValidatorWeight + InitialBalance + AVAXAsset = avax.Asset{ID: snowtest.AVAXAssetID} + + DefaultValidatorStartTime = upgrade.InitiallyActiveTime + DefaultValidatorStartTimeUnix = uint64(DefaultValidatorStartTime.Unix()) + DefaultValidatorEndTime = DefaultValidatorStartTime.Add(DefaultValidatorDuration) + DefaultValidatorEndTimeUnix = uint64(DefaultValidatorEndTime.Unix()) ) -func New(t testing.TB) *genesis.Genesis { - require := require.New(t) +var ( + // Keys that are funded in the genesis + DefaultFundedKeys = secp256k1.TestKeys() - genesisValidator := &txs.AddValidatorTx{ - Validator: txs.Validator{ - NodeID: ValidatorNodeID, - Start: TimeUnix, - End: ValidatorEndTimeUnix, - Wght: ValidatorWeight, - }, - StakeOuts: []*avax.TransferableOutput{ - { - Asset: AVAXAsset, - Out: &secp256k1fx.TransferOutput{ - Amt: ValidatorWeight, - }, - }, - }, - RewardsOwner: ValidatorRewardsOwner, - DelegationShares: ValidatorDelegationShares, + // Node IDs of genesis validators + DefaultNodeIDs []ids.NodeID +) + +func init() { + DefaultNodeIDs = make([]ids.NodeID, len(DefaultFundedKeys)) + for i := range DefaultFundedKeys { + DefaultNodeIDs[i] = ids.GenerateTestNodeID() } - genesisValidatorTx := &txs.Tx{Unsigned: genesisValidator} - require.NoError(genesisValidatorTx.Initialize(txs.Codec)) +} - genesisChain := &txs.CreateChainTx{ - SubnetID: constants.PrimaryNetworkID, - ChainName: XChainName, - VMID: constants.AVMID, - SubnetAuth: &secp256k1fx.Input{}, +type Config struct { + NodeIDs []ids.NodeID + ValidatorWeight uint64 + ValidatorStartTime time.Time + ValidatorEndTime time.Time + + FundedKeys []*secp256k1.PrivateKey + InitialBalance uint64 +} + +func New(t testing.TB, c Config) *platformvmgenesis.Genesis { + if len(c.NodeIDs) == 0 { + c.NodeIDs = DefaultNodeIDs + } + if c.ValidatorWeight == 0 { + c.ValidatorWeight = DefaultValidatorWeight + } + if c.ValidatorStartTime.IsZero() { + c.ValidatorStartTime = DefaultValidatorStartTime + } + if c.ValidatorEndTime.IsZero() { + c.ValidatorEndTime = DefaultValidatorEndTime } - genesisChainTx := &txs.Tx{Unsigned: genesisChain} - require.NoError(genesisChainTx.Initialize(txs.Codec)) - - return &genesis.Genesis{ - UTXOs: []*genesis.UTXO{ - { - UTXO: avax.UTXO{ - UTXOID: avax.UTXOID{ - TxID: AVAXAssetID, - OutputIndex: 0, + if len(c.FundedKeys) == 0 { + c.FundedKeys = DefaultFundedKeys + } + if c.InitialBalance == 0 { + c.InitialBalance = DefaultInitialBalance + } + + require := require.New(t) + + genesis := &platformvmgenesis.Genesis{ + UTXOs: make([]*platformvmgenesis.UTXO, len(c.FundedKeys)), + Validators: make([]*txs.Tx, len(c.NodeIDs)), + Timestamp: uint64(c.ValidatorStartTime.Unix()), + InitialSupply: InitialSupply, + } + for i, key := range c.FundedKeys { + genesis.UTXOs[i] = &platformvmgenesis.UTXO{UTXO: avax.UTXO{ + UTXOID: avax.UTXOID{ + TxID: snowtest.AVAXAssetID, + OutputIndex: uint32(i), + }, + Asset: AVAXAsset, + Out: &secp256k1fx.TransferOutput{ + Amt: c.InitialBalance, + OutputOwners: secp256k1fx.OutputOwners{ + Threshold: 1, + Addrs: []ids.ShortID{ + key.Address(), }, + }, + }, + }} + } + for i, nodeID := range c.NodeIDs { + key := c.FundedKeys[i%len(c.FundedKeys)] + owner := secp256k1fx.OutputOwners{ + Threshold: 1, + Addrs: []ids.ShortID{ + key.Address(), + }, + } + validator := &txs.AddValidatorTx{ + BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{ + NetworkID: constants.UnitTestID, + BlockchainID: constants.PlatformChainID, + }}, + Validator: txs.Validator{ + NodeID: nodeID, + Start: uint64(c.ValidatorStartTime.Unix()), + End: uint64(c.ValidatorEndTime.Unix()), + Wght: c.ValidatorWeight, + }, + StakeOuts: []*avax.TransferableOutput{ + { Asset: AVAXAsset, Out: &secp256k1fx.TransferOutput{ - Amt: InitialBalance, + Amt: c.ValidatorWeight, + OutputOwners: owner, }, }, - Message: nil, }, - }, - Validators: []*txs.Tx{ - genesisValidatorTx, - }, - Chains: []*txs.Tx{ - genesisChainTx, - }, - Timestamp: TimeUnix, - InitialSupply: InitialSupply, + RewardsOwner: &owner, + DelegationShares: ValidatorDelegationShares, + } + validatorTx := &txs.Tx{Unsigned: validator} + require.NoError(validatorTx.Initialize(txs.GenesisCodec)) + + genesis.Validators[i] = validatorTx } + + chain := &txs.CreateChainTx{ + BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{ + NetworkID: constants.UnitTestID, + BlockchainID: constants.PlatformChainID, + }}, + SubnetID: constants.PrimaryNetworkID, + ChainName: XChainName, + VMID: constants.AVMID, + SubnetAuth: &secp256k1fx.Input{}, + } + chainTx := &txs.Tx{Unsigned: chain} + require.NoError(chainTx.Initialize(txs.GenesisCodec)) + + genesis.Chains = []*txs.Tx{chainTx} + return genesis } -func NewBytes(t testing.TB) []byte { - g := New(t) - genesisBytes, err := genesis.Codec.Marshal(genesis.CodecVersion, g) +func NewBytes(t testing.TB, c Config) []byte { + g := New(t, c) + genesisBytes, err := platformvmgenesis.Codec.Marshal(platformvmgenesis.CodecVersion, g) require.NoError(t, err) return genesisBytes } diff --git a/vms/platformvm/service_test.go b/vms/platformvm/service_test.go index f1c1cd2f44dc..47e8a48ef578 100644 --- a/vms/platformvm/service_test.go +++ b/vms/platformvm/service_test.go @@ -32,11 +32,13 @@ import ( "github.com/ava-labs/avalanchego/utils/crypto/bls" "github.com/ava-labs/avalanchego/utils/crypto/secp256k1" "github.com/ava-labs/avalanchego/utils/formatting" + "github.com/ava-labs/avalanchego/utils/formatting/address" "github.com/ava-labs/avalanchego/utils/logging" "github.com/ava-labs/avalanchego/vms/components/avax" "github.com/ava-labs/avalanchego/vms/components/gas" "github.com/ava-labs/avalanchego/vms/platformvm/block" "github.com/ava-labs/avalanchego/vms/platformvm/block/executor/executormock" + "github.com/ava-labs/avalanchego/vms/platformvm/genesis/genesistest" "github.com/ava-labs/avalanchego/vms/platformvm/signer" "github.com/ava-labs/avalanchego/vms/platformvm/state" "github.com/ava-labs/avalanchego/vms/platformvm/status" @@ -106,7 +108,7 @@ func TestExportKey(t *testing.T) { pk, err := secp256k1.ToPrivateKey(testPrivateKey) require.NoError(err) - require.NoError(user.PutKeys(pk, keys[0])) + require.NoError(user.PutKeys(pk, genesistest.DefaultFundedKeys[0])) service.vm.ctx.Lock.Unlock() @@ -146,7 +148,7 @@ func TestGetTxStatus(t *testing.T) { Amt: 1234567, OutputOwners: secp256k1fx.OutputOwners{ Locktime: 0, - Addrs: []ids.ShortID{recipientKey.PublicKey().Address()}, + Addrs: []ids.ShortID{recipientKey.Address()}, Threshold: 1, }, }, @@ -162,7 +164,7 @@ func TestGetTxStatus(t *testing.T) { Key: inputID[:], Value: utxoBytes, Traits: [][]byte{ - recipientKey.PublicKey().Address().Bytes(), + recipientKey.Address().Bytes(), }, }, }, @@ -233,7 +235,7 @@ func TestGetTx(t *testing.T) { "chain name", common.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }), ) require.NoError(t, err) @@ -251,7 +253,7 @@ func TestGetTx(t *testing.T) { Addrs: []ids.ShortID{ids.GenerateTestShortID()}, } - builder, txSigner := factory.NewWallet(keys[0]) + builder, txSigner := factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddPermissionlessValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -269,7 +271,7 @@ func TestGetTx(t *testing.T) { 0, common.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }), ) require.NoError(t, err) @@ -279,7 +281,7 @@ func TestGetTx(t *testing.T) { { "atomic block", func(service *Service, factory *txstest.WalletFactory) (*txs.Tx, error) { - builder, signer := factory.NewWallet(keys[0]) + builder, signer := factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewExportTx( service.vm.ctx.XChainID, []*avax.TransferableOutput{{ @@ -295,7 +297,7 @@ func TestGetTx(t *testing.T) { }}, common.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }), ) require.NoError(t, err) @@ -383,21 +385,28 @@ func TestGetBalance(t *testing.T) { require.NoError(err) // Ensure GetStake is correct for each of the genesis validators - genesis, _ := defaultGenesis(t, service.vm.ctx.AVAXAssetID) + genesis := genesistest.New(t, genesistest.Config{}) for idx, utxo := range genesis.UTXOs { + out := utxo.Out.(*secp256k1fx.TransferOutput) + require.Len(out.Addrs, 1) + + addr := out.Addrs[0] + addrStr, err := address.Format("P", constants.UnitTestHRP, addr.Bytes()) + require.NoError(err) + request := GetBalanceRequest{ Addresses: []string{ - "P-" + utxo.Address, + addrStr, }, } reply := GetBalanceResponse{} require.NoError(service.GetBalance(nil, &request, &reply)) - balance := defaultBalance + balance := genesistest.DefaultInitialBalance if idx == 0 { // we use the first key to fund a subnet creation in [defaultGenesis]. // As such we need to account for the subnet creation fee - balance = defaultBalance - createSubnetFee + balance = genesistest.DefaultInitialBalance - createSubnetFee } require.Equal(avajson.Uint64(balance), reply.Balance) require.Equal(avajson.Uint64(balance), reply.Unlocked) @@ -411,21 +420,29 @@ func TestGetStake(t *testing.T) { service, _, factory := defaultService(t) // Ensure GetStake is correct for each of the genesis validators - genesis, _ := defaultGenesis(t, service.vm.ctx.AVAXAssetID) + genesis := genesistest.New(t, genesistest.Config{}) addrsStrs := []string{} - for i, validator := range genesis.Validators { - addr := "P-" + validator.RewardOwner.Addresses[0] - addrsStrs = append(addrsStrs, addr) + for _, validatorTx := range genesis.Validators { + validator := validatorTx.Unsigned.(*txs.AddValidatorTx) + require.Len(validator.StakeOuts, 1) + stakeOut := validator.StakeOuts[0].Out.(*secp256k1fx.TransferOutput) + require.Len(stakeOut.Addrs, 1) + addr := stakeOut.Addrs[0] + + addrStr, err := address.Format("P", constants.UnitTestHRP, addr.Bytes()) + require.NoError(err) + + addrsStrs = append(addrsStrs, addrStr) args := GetStakeArgs{ JSONAddresses: api.JSONAddresses{ - Addresses: []string{addr}, + Addresses: []string{addrStr}, }, Encoding: formatting.Hex, } response := GetStakeReply{} require.NoError(service.GetStake(nil, &args, &response)) - require.Equal(defaultWeight, uint64(response.Staked)) + require.Equal(genesistest.DefaultValidatorWeight, uint64(response.Staked)) require.Len(response.Outputs, 1) // Unmarshal into an output @@ -436,12 +453,23 @@ func TestGetStake(t *testing.T) { _, err = txs.Codec.Unmarshal(outputBytes, &output) require.NoError(err) - out := output.Out.(*secp256k1fx.TransferOutput) - require.Equal(defaultWeight, out.Amount()) - require.Equal(uint32(1), out.Threshold) - require.Len(out.Addrs, 1) - require.Equal(keys[i].PublicKey().Address(), out.Addrs[0]) - require.Zero(out.Locktime) + require.Equal( + avax.TransferableOutput{ + Asset: avax.Asset{ + ID: service.vm.ctx.AVAXAssetID, + }, + Out: &secp256k1fx.TransferOutput{ + Amt: genesistest.DefaultValidatorWeight, + OutputOwners: secp256k1fx.OutputOwners{ + Threshold: 1, + Addrs: []ids.ShortID{ + addr, + }, + }, + }, + }, + output, + ) } // Make sure this works for multiple addresses @@ -453,7 +481,7 @@ func TestGetStake(t *testing.T) { } response := GetStakeReply{} require.NoError(service.GetStake(nil, &args, &response)) - require.Equal(len(genesis.Validators)*int(defaultWeight), int(response.Staked)) + require.Equal(len(genesis.Validators)*int(genesistest.DefaultValidatorWeight), int(response.Staked)) require.Len(response.Outputs, len(genesis.Validators)) for _, outputStr := range response.Outputs { @@ -465,26 +493,25 @@ func TestGetStake(t *testing.T) { require.NoError(err) out := output.Out.(*secp256k1fx.TransferOutput) - require.Equal(defaultWeight, out.Amount()) + require.Equal(genesistest.DefaultValidatorWeight, out.Amt) require.Equal(uint32(1), out.Threshold) require.Zero(out.Locktime) require.Len(out.Addrs, 1) } - oldStake := defaultWeight + oldStake := genesistest.DefaultValidatorWeight service.vm.ctx.Lock.Lock() // Add a delegator stakeAmount := service.vm.MinDelegatorStake + 12345 - delegatorNodeID := genesisNodeIDs[0] - delegatorStartTime := defaultValidateStartTime - delegatorEndTime := defaultGenesisTime.Add(defaultMinStakingDuration) - builder, signer := factory.NewWallet(keys[0]) + delegatorNodeID := genesistest.DefaultNodeIDs[0] + delegatorEndTime := genesistest.DefaultValidatorStartTime.Add(defaultMinStakingDuration) + builder, signer := factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddDelegatorTx( &txs.Validator{ NodeID: delegatorNodeID, - Start: uint64(delegatorStartTime.Unix()), + Start: genesistest.DefaultValidatorStartTimeUnix, End: uint64(delegatorEndTime.Unix()), Wght: stakeAmount, }, @@ -494,7 +521,7 @@ func TestGetStake(t *testing.T) { }, common.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }), ) require.NoError(err) @@ -505,7 +532,7 @@ func TestGetStake(t *testing.T) { staker, err := state.NewCurrentStaker( tx.ID(), addDelTx, - delegatorStartTime, + genesistest.DefaultValidatorStartTime, 0, ) require.NoError(err) @@ -517,7 +544,7 @@ func TestGetStake(t *testing.T) { service.vm.ctx.Lock.Unlock() // Make sure the delegator addr has the right stake (old stake + stakeAmount) - addr, _ := service.addrManager.FormatLocalAddress(keys[0].PublicKey().Address()) + addr, _ := service.addrManager.FormatLocalAddress(genesistest.DefaultFundedKeys[0].Address()) args.Addresses = []string{addr} require.NoError(service.GetStake(nil, &args, &response)) require.Equal(oldStake+stakeAmount, uint64(response.Staked)) @@ -543,11 +570,11 @@ func TestGetStake(t *testing.T) { // Add a pending staker stakeAmount = service.vm.MinValidatorStake + 54321 pendingStakerNodeID := ids.GenerateTestNodeID() - pendingStakerEndTime := uint64(defaultGenesisTime.Add(defaultMinStakingDuration).Unix()) + pendingStakerEndTime := uint64(genesistest.DefaultValidatorStartTime.Add(defaultMinStakingDuration).Unix()) utx2, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: pendingStakerNodeID, - Start: uint64(defaultGenesisTime.Unix()), + Start: uint64(genesistest.DefaultValidatorStartTime.Unix()), End: pendingStakerEndTime, Wght: stakeAmount, }, @@ -558,7 +585,7 @@ func TestGetStake(t *testing.T) { 0, common.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }), ) require.NoError(err) @@ -599,7 +626,7 @@ func TestGetCurrentValidators(t *testing.T) { require := require.New(t) service, _, factory := defaultService(t) - genesis, _ := defaultGenesis(t, service.vm.ctx.AVAXAssetID) + genesis := genesistest.New(t, genesistest.Config{}) // Call getValidators args := GetCurrentValidatorsArgs{SubnetID: constants.PrimaryNetworkID} @@ -608,34 +635,37 @@ func TestGetCurrentValidators(t *testing.T) { require.NoError(service.GetCurrentValidators(nil, &args, &response)) require.Len(response.Validators, len(genesis.Validators)) - for _, vdr := range genesis.Validators { + for _, validatorTx := range genesis.Validators { + validator := validatorTx.Unsigned.(*txs.AddValidatorTx) + nodeID := validator.NodeID() + found := false - for i := 0; i < len(response.Validators) && !found; i++ { + for i := 0; i < len(response.Validators); i++ { gotVdr := response.Validators[i].(pchainapi.PermissionlessValidator) - if gotVdr.NodeID != vdr.NodeID { + if gotVdr.NodeID != nodeID { continue } - require.Equal(vdr.EndTime, gotVdr.EndTime) - require.Equal(vdr.StartTime, gotVdr.StartTime) + require.Equal(validator.EndTime().Unix(), int64(gotVdr.EndTime)) + require.Equal(validator.StartTime().Unix(), int64(gotVdr.StartTime)) found = true + break } - require.True(found, "expected validators to contain %s but didn't", vdr.NodeID) + require.True(found, "expected validators to contain %s but didn't", nodeID) } // Add a delegator stakeAmount := service.vm.MinDelegatorStake + 12345 - validatorNodeID := genesisNodeIDs[1] - delegatorStartTime := defaultValidateStartTime - delegatorEndTime := delegatorStartTime.Add(defaultMinStakingDuration) + validatorNodeID := genesistest.DefaultNodeIDs[1] + delegatorEndTime := genesistest.DefaultValidatorStartTime.Add(defaultMinStakingDuration) service.vm.ctx.Lock.Lock() - builder, signer := factory.NewWallet(keys[0]) + builder, signer := factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddDelegatorTx( &txs.Validator{ NodeID: validatorNodeID, - Start: uint64(delegatorStartTime.Unix()), + Start: genesistest.DefaultValidatorStartTimeUnix, End: uint64(delegatorEndTime.Unix()), Wght: stakeAmount, }, @@ -645,7 +675,7 @@ func TestGetCurrentValidators(t *testing.T) { }, common.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }), ) require.NoError(err) @@ -656,7 +686,7 @@ func TestGetCurrentValidators(t *testing.T) { staker, err := state.NewCurrentStaker( delTx.ID(), addDelTx, - delegatorStartTime, + genesistest.DefaultValidatorStartTime, 0, ) require.NoError(err) @@ -698,7 +728,7 @@ func TestGetCurrentValidators(t *testing.T) { require.Len(*innerVdr.Delegators, 1) delegator := (*innerVdr.Delegators)[0] require.Equal(delegator.NodeID, innerVdr.NodeID) - require.Equal(int64(delegator.StartTime), delegatorStartTime.Unix()) + require.Equal(uint64(delegator.StartTime), genesistest.DefaultValidatorStartTimeUnix) require.Equal(int64(delegator.EndTime), delegatorEndTime.Unix()) require.Equal(uint64(delegator.Weight), stakeAmount) } @@ -782,7 +812,7 @@ func TestGetBlock(t *testing.T) { "chain name", common.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }), ) require.NoError(err) diff --git a/vms/platformvm/state/stakers_test.go b/vms/platformvm/state/stakers_test.go index 6b1343891540..e4ae2aa840c5 100644 --- a/vms/platformvm/state/stakers_test.go +++ b/vms/platformvm/state/stakers_test.go @@ -221,7 +221,7 @@ func TestDiffStakersDelegator(t *testing.T) { func newTestStaker() *Staker { startTime := time.Now().Round(time.Second) - endTime := startTime.Add(genesistest.ValidatorDuration) + endTime := startTime.Add(genesistest.DefaultValidatorDuration) return &Staker{ TxID: ids.GenerateTestID(), NodeID: ids.GenerateTestNodeID(), diff --git a/vms/platformvm/state/state_test.go b/vms/platformvm/state/state_test.go index 7e52453d5742..de2bab544c00 100644 --- a/vms/platformvm/state/state_test.go +++ b/vms/platformvm/state/state_test.go @@ -47,16 +47,22 @@ import ( safemath "github.com/ava-labs/avalanchego/utils/math" ) +var defaultValidatorNodeID = ids.GenerateTestNodeID() + func newTestState(t testing.TB, db database.Database) *state { s, err := New( db, - genesistest.NewBytes(t), + genesistest.NewBytes(t, genesistest.Config{ + NodeIDs: []ids.NodeID{defaultValidatorNodeID}, + }), prometheus.NewRegistry(), validators.NewManager(), upgradetest.GetConfig(upgradetest.Latest), &config.DefaultExecutionConfig, &snow.Context{ - Log: logging.NoLog{}, + NetworkID: constants.UnitTestID, + NodeID: ids.GenerateTestNodeID(), + Log: logging.NoLog{}, }, metrics.Noop, reward.NewCalculator(reward.Config{ @@ -75,12 +81,12 @@ func TestStateSyncGenesis(t *testing.T) { require := require.New(t) state := newTestState(t, memdb.New()) - staker, err := state.GetCurrentValidator(constants.PrimaryNetworkID, genesistest.ValidatorNodeID) + staker, err := state.GetCurrentValidator(constants.PrimaryNetworkID, defaultValidatorNodeID) require.NoError(err) require.NotNil(staker) - require.Equal(genesistest.ValidatorNodeID, staker.NodeID) + require.Equal(defaultValidatorNodeID, staker.NodeID) - delegatorIterator, err := state.GetCurrentDelegatorIterator(constants.PrimaryNetworkID, genesistest.ValidatorNodeID) + delegatorIterator, err := state.GetCurrentDelegatorIterator(constants.PrimaryNetworkID, defaultValidatorNodeID) require.NoError(err) assertIteratorsEqual(t, iterator.Empty[*Staker]{}, delegatorIterator) @@ -88,10 +94,10 @@ func TestStateSyncGenesis(t *testing.T) { require.NoError(err) assertIteratorsEqual(t, iterator.FromSlice(staker), stakerIterator) - _, err = state.GetPendingValidator(constants.PrimaryNetworkID, genesistest.ValidatorNodeID) + _, err = state.GetPendingValidator(constants.PrimaryNetworkID, defaultValidatorNodeID) require.ErrorIs(err, database.ErrNotFound) - delegatorIterator, err = state.GetPendingDelegatorIterator(constants.PrimaryNetworkID, genesistest.ValidatorNodeID) + delegatorIterator, err = state.GetPendingDelegatorIterator(constants.PrimaryNetworkID, defaultValidatorNodeID) require.NoError(err) assertIteratorsEqual(t, iterator.Empty[*Staker]{}, delegatorIterator) } diff --git a/vms/platformvm/state/statetest/state.go b/vms/platformvm/state/statetest/state.go index fbc71381ec51..66998ea3cf10 100644 --- a/vms/platformvm/state/statetest/state.go +++ b/vms/platformvm/state/statetest/state.go @@ -27,6 +27,8 @@ import ( "github.com/ava-labs/avalanchego/vms/platformvm/state" ) +var DefaultNodeID = ids.GenerateTestNodeID() + type Config struct { DB database.Database Genesis []byte @@ -44,11 +46,14 @@ func New(t testing.TB, c Config) state.State { c.DB = memdb.New() } if len(c.Genesis) == 0 { - c.Genesis = genesistest.NewBytes(t) + c.Genesis = genesistest.NewBytes(t, genesistest.Config{}) } if c.Registerer == nil { c.Registerer = prometheus.NewRegistry() } + if c.Validators == nil { + c.Validators = validators.NewManager() + } if c.Upgrades == (upgrade.Config{}) { c.Upgrades = upgradetest.GetConfig(upgradetest.Latest) } @@ -58,7 +63,7 @@ func New(t testing.TB, c Config) state.State { if c.Context == nil { c.Context = &snow.Context{ NetworkID: constants.UnitTestID, - NodeID: ids.GenerateTestNodeID(), + NodeID: DefaultNodeID, Log: logging.NoLog{}, } } diff --git a/vms/platformvm/txs/add_delegator_test.go b/vms/platformvm/txs/add_delegator_test.go index ac3290fb2431..3d2ba834f9c6 100644 --- a/vms/platformvm/txs/add_delegator_test.go +++ b/vms/platformvm/txs/add_delegator_test.go @@ -58,7 +58,7 @@ func TestAddDelegatorTxSyntacticVerify(t *testing.T) { Amt: uint64(1234), OutputOwners: secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{preFundedKeys[0].Address()}, }, }, }} @@ -70,7 +70,7 @@ func TestAddDelegatorTxSyntacticVerify(t *testing.T) { Amt: validatorWeight, OutputOwners: secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{preFundedKeys[0].Address()}, }, }, }, @@ -93,7 +93,7 @@ func TestAddDelegatorTxSyntacticVerify(t *testing.T) { DelegationRewardsOwner: &secp256k1fx.OutputOwners{ Locktime: 0, Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{preFundedKeys[0].Address()}, }, } @@ -157,7 +157,7 @@ func TestAddDelegatorTxSyntacticVerifyNotAVAX(t *testing.T) { Amt: uint64(1234), OutputOwners: secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{preFundedKeys[0].Address()}, }, }, }} @@ -169,7 +169,7 @@ func TestAddDelegatorTxSyntacticVerifyNotAVAX(t *testing.T) { Amt: validatorWeight, OutputOwners: secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{preFundedKeys[0].Address()}, }, }, }, @@ -192,7 +192,7 @@ func TestAddDelegatorTxSyntacticVerifyNotAVAX(t *testing.T) { DelegationRewardsOwner: &secp256k1fx.OutputOwners{ Locktime: 0, Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{preFundedKeys[0].Address()}, }, } diff --git a/vms/platformvm/txs/add_subnet_validator_test.go b/vms/platformvm/txs/add_subnet_validator_test.go index 8dc8d76782ac..b3c3630ef2f0 100644 --- a/vms/platformvm/txs/add_subnet_validator_test.go +++ b/vms/platformvm/txs/add_subnet_validator_test.go @@ -58,7 +58,7 @@ func TestAddSubnetValidatorTxSyntacticVerify(t *testing.T) { Amt: uint64(1234), OutputOwners: secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{preFundedKeys[0].Address()}, }, }, }} @@ -169,7 +169,7 @@ func TestAddSubnetValidatorMarshal(t *testing.T) { Amt: uint64(1234), OutputOwners: secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{preFundedKeys[0].Address()}, }, }, }} diff --git a/vms/platformvm/txs/add_validator_test.go b/vms/platformvm/txs/add_validator_test.go index daf32f66746d..27ea18dc5a7e 100644 --- a/vms/platformvm/txs/add_validator_test.go +++ b/vms/platformvm/txs/add_validator_test.go @@ -40,7 +40,7 @@ func TestAddValidatorTxSyntacticVerify(t *testing.T) { require.ErrorIs(err, ErrNilTx) validatorWeight := uint64(2022) - rewardAddress := preFundedKeys[0].PublicKey().Address() + rewardAddress := preFundedKeys[0].Address() inputs := []*avax.TransferableInput{{ UTXOID: avax.UTXOID{ TxID: ids.ID{'t', 'x', 'I', 'D'}, @@ -58,7 +58,7 @@ func TestAddValidatorTxSyntacticVerify(t *testing.T) { Amt: uint64(1234), OutputOwners: secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{preFundedKeys[0].Address()}, }, }, }} @@ -70,7 +70,7 @@ func TestAddValidatorTxSyntacticVerify(t *testing.T) { Amt: validatorWeight, OutputOwners: secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{preFundedKeys[0].Address()}, }, }, }, @@ -156,7 +156,7 @@ func TestAddValidatorTxSyntacticVerifyNotAVAX(t *testing.T) { assetID := ids.GenerateTestID() validatorWeight := uint64(2022) - rewardAddress := preFundedKeys[0].PublicKey().Address() + rewardAddress := preFundedKeys[0].Address() inputs := []*avax.TransferableInput{{ UTXOID: avax.UTXOID{ TxID: ids.ID{'t', 'x', 'I', 'D'}, @@ -174,7 +174,7 @@ func TestAddValidatorTxSyntacticVerifyNotAVAX(t *testing.T) { Amt: uint64(1234), OutputOwners: secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{preFundedKeys[0].Address()}, }, }, }} @@ -186,7 +186,7 @@ func TestAddValidatorTxSyntacticVerifyNotAVAX(t *testing.T) { Amt: validatorWeight, OutputOwners: secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{preFundedKeys[0].Address()}, }, }, }, diff --git a/vms/platformvm/txs/create_chain_test.go b/vms/platformvm/txs/create_chain_test.go index ecf52567d357..a3565d4ae29d 100644 --- a/vms/platformvm/txs/create_chain_test.go +++ b/vms/platformvm/txs/create_chain_test.go @@ -143,7 +143,7 @@ func TestUnsignedCreateChainTxVerify(t *testing.T) { Amt: uint64(1234), OutputOwners: secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{preFundedKeys[0].Address()}, }, }, }} diff --git a/vms/platformvm/txs/executor/advance_time_test.go b/vms/platformvm/txs/executor/advance_time_test.go index e9e54ad8a3e6..c10c76ec7bac 100644 --- a/vms/platformvm/txs/executor/advance_time_test.go +++ b/vms/platformvm/txs/executor/advance_time_test.go @@ -17,6 +17,7 @@ import ( "github.com/ava-labs/avalanchego/upgrade/upgradetest" "github.com/ava-labs/avalanchego/utils/constants" "github.com/ava-labs/avalanchego/utils/crypto/secp256k1" + "github.com/ava-labs/avalanchego/vms/platformvm/genesis/genesistest" "github.com/ava-labs/avalanchego/vms/platformvm/reward" "github.com/ava-labs/avalanchego/vms/platformvm/state" "github.com/ava-labs/avalanchego/vms/platformvm/status" @@ -46,7 +47,7 @@ func TestAdvanceTimeTxUpdatePrimaryNetworkStakers(t *testing.T) { // Case: Timestamp is after next validator start time // Add a pending validator - pendingValidatorStartTime := defaultValidateStartTime.Add(1 * time.Second) + pendingValidatorStartTime := genesistest.DefaultValidatorStartTime.Add(1 * time.Second) pendingValidatorEndTime := pendingValidatorStartTime.Add(defaultMinStakingDuration) nodeID := ids.GenerateTestNodeID() addPendingValidatorTx, err := addPendingValidator( @@ -54,7 +55,7 @@ func TestAdvanceTimeTxUpdatePrimaryNetworkStakers(t *testing.T) { pendingValidatorStartTime, pendingValidatorEndTime, nodeID, - []*secp256k1.PrivateKey{preFundedKeys[0]}, + []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}, ) require.NoError(err) @@ -136,10 +137,10 @@ func TestAdvanceTimeTxTimestampTooLate(t *testing.T) { // Case: Timestamp is after next validator start time // Add a pending validator - pendingValidatorStartTime := defaultValidateStartTime.Add(1 * time.Second) + pendingValidatorStartTime := genesistest.DefaultValidatorStartTime.Add(1 * time.Second) pendingValidatorEndTime := pendingValidatorStartTime.Add(defaultMinStakingDuration) nodeID := ids.GenerateTestNodeID() - _, err := addPendingValidator(env, pendingValidatorStartTime, pendingValidatorEndTime, nodeID, []*secp256k1.PrivateKey{preFundedKeys[0]}) + _, err := addPendingValidator(env, pendingValidatorStartTime, pendingValidatorEndTime, nodeID, []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}) require.NoError(err) { @@ -170,11 +171,11 @@ func TestAdvanceTimeTxTimestampTooLate(t *testing.T) { defer env.ctx.Lock.Unlock() // fast forward clock to 10 seconds before genesis validators stop validating - env.clk.Set(defaultValidateEndTime.Add(-10 * time.Second)) + env.clk.Set(genesistest.DefaultValidatorEndTime.Add(-10 * time.Second)) { // Proposes advancing timestamp to 1 second after genesis validators stop validating - tx, err := newAdvanceTimeTx(t, defaultValidateEndTime.Add(1*time.Second)) + tx, err := newAdvanceTimeTx(t, genesistest.DefaultValidatorEndTime.Add(1*time.Second)) require.NoError(err) onCommitState, err := state.NewDiff(lastAcceptedID, env) @@ -228,8 +229,8 @@ func TestAdvanceTimeTxUpdateStakers(t *testing.T) { // Staker5: |--------------------| staker1 := staker{ nodeID: ids.GenerateTestNodeID(), - startTime: defaultValidateStartTime.Add(1 * time.Minute), - endTime: defaultValidateStartTime.Add(10 * defaultMinStakingDuration).Add(1 * time.Minute), + startTime: genesistest.DefaultValidatorStartTime.Add(1 * time.Minute), + endTime: genesistest.DefaultValidatorStartTime.Add(10 * defaultMinStakingDuration).Add(1 * time.Minute), } staker2 := staker{ nodeID: ids.GenerateTestNodeID(), @@ -382,13 +383,13 @@ func TestAdvanceTimeTxUpdateStakers(t *testing.T) { staker.startTime, staker.endTime, staker.nodeID, - []*secp256k1.PrivateKey{preFundedKeys[0]}, + []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}, ) require.NoError(err) } for _, staker := range test.subnetStakers { - builder, signer := env.factory.NewWallet(preFundedKeys[0], preFundedKeys[1]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) utx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -486,16 +487,15 @@ func TestAdvanceTimeTxRemoveSubnetValidator(t *testing.T) { dummyHeight := uint64(1) // Add a subnet validator to the staker set - subnetValidatorNodeID := genesisNodeIDs[0] - subnetVdr1StartTime := defaultValidateStartTime - subnetVdr1EndTime := defaultValidateStartTime.Add(defaultMinStakingDuration) + subnetValidatorNodeID := genesistest.DefaultNodeIDs[0] + subnetVdr1EndTime := genesistest.DefaultValidatorStartTime.Add(defaultMinStakingDuration) - builder, signer := env.factory.NewWallet(preFundedKeys[0], preFundedKeys[1]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) utx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ NodeID: subnetValidatorNodeID, - Start: uint64(subnetVdr1StartTime.Unix()), + Start: genesistest.DefaultValidatorStartTimeUnix, End: uint64(subnetVdr1EndTime.Unix()), Wght: 1, }, @@ -523,7 +523,7 @@ func TestAdvanceTimeTxRemoveSubnetValidator(t *testing.T) { // The above validator is now part of the staking set // Queue a staker that joins the staker set after the above validator leaves - subnetVdr2NodeID := genesisNodeIDs[1] + subnetVdr2NodeID := genesistest.DefaultNodeIDs[1] utx, err = builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -602,11 +602,11 @@ func TestTrackedSubnet(t *testing.T) { } // Add a subnet validator to the staker set - subnetValidatorNodeID := genesisNodeIDs[0] + subnetValidatorNodeID := genesistest.DefaultNodeIDs[0] - subnetVdr1StartTime := defaultValidateStartTime.Add(1 * time.Minute) - subnetVdr1EndTime := defaultValidateStartTime.Add(10 * defaultMinStakingDuration).Add(1 * time.Minute) - builder, signer := env.factory.NewWallet(preFundedKeys[0], preFundedKeys[1]) + subnetVdr1StartTime := genesistest.DefaultValidatorStartTime.Add(1 * time.Minute) + subnetVdr1EndTime := genesistest.DefaultValidatorStartTime.Add(10 * defaultMinStakingDuration).Add(1 * time.Minute) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) utx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -673,7 +673,7 @@ func TestAdvanceTimeTxDelegatorStakerWeight(t *testing.T) { // Case: Timestamp is after next validator start time // Add a pending validator - pendingValidatorStartTime := defaultValidateStartTime.Add(1 * time.Second) + pendingValidatorStartTime := genesistest.DefaultValidatorStartTime.Add(1 * time.Second) pendingValidatorEndTime := pendingValidatorStartTime.Add(defaultMaxStakingDuration) nodeID := ids.GenerateTestNodeID() _, err := addPendingValidator( @@ -681,7 +681,7 @@ func TestAdvanceTimeTxDelegatorStakerWeight(t *testing.T) { pendingValidatorStartTime, pendingValidatorEndTime, nodeID, - []*secp256k1.PrivateKey{preFundedKeys[0]}, + []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}, ) require.NoError(err) @@ -717,7 +717,7 @@ func TestAdvanceTimeTxDelegatorStakerWeight(t *testing.T) { pendingDelegatorStartTime := pendingValidatorStartTime.Add(1 * time.Second) pendingDelegatorEndTime := pendingDelegatorStartTime.Add(1 * time.Second) - builder, signer := env.factory.NewWallet(preFundedKeys[0], preFundedKeys[1], preFundedKeys[4]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0], genesistest.DefaultFundedKeys[1], genesistest.DefaultFundedKeys[4]) utx, err := builder.NewAddDelegatorTx( &txs.Validator{ NodeID: nodeID, @@ -727,7 +727,7 @@ func TestAdvanceTimeTxDelegatorStakerWeight(t *testing.T) { }, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }, ) require.NoError(err) @@ -783,10 +783,10 @@ func TestAdvanceTimeTxDelegatorStakers(t *testing.T) { // Case: Timestamp is after next validator start time // Add a pending validator - pendingValidatorStartTime := defaultValidateStartTime.Add(1 * time.Second) + pendingValidatorStartTime := genesistest.DefaultValidatorStartTime.Add(1 * time.Second) pendingValidatorEndTime := pendingValidatorStartTime.Add(defaultMinStakingDuration) nodeID := ids.GenerateTestNodeID() - _, err := addPendingValidator(env, pendingValidatorStartTime, pendingValidatorEndTime, nodeID, []*secp256k1.PrivateKey{preFundedKeys[0]}) + _, err := addPendingValidator(env, pendingValidatorStartTime, pendingValidatorEndTime, nodeID, []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}) require.NoError(err) tx, err := newAdvanceTimeTx(t, pendingValidatorStartTime) @@ -820,7 +820,7 @@ func TestAdvanceTimeTxDelegatorStakers(t *testing.T) { // Add delegator pendingDelegatorStartTime := pendingValidatorStartTime.Add(1 * time.Second) pendingDelegatorEndTime := pendingDelegatorStartTime.Add(defaultMinStakingDuration) - builder, signer := env.factory.NewWallet(preFundedKeys[0], preFundedKeys[1], preFundedKeys[4]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0], genesistest.DefaultFundedKeys[1], genesistest.DefaultFundedKeys[4]) utx, err := builder.NewAddDelegatorTx( &txs.Validator{ NodeID: nodeID, @@ -830,7 +830,7 @@ func TestAdvanceTimeTxDelegatorStakers(t *testing.T) { }, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }, ) require.NoError(err) @@ -882,7 +882,7 @@ func TestAdvanceTimeTxAfterBanff(t *testing.T) { env := newEnvironment(t, upgradetest.Durango) env.ctx.Lock.Lock() defer env.ctx.Lock.Unlock() - env.clk.Set(defaultGenesisTime) // VM's clock reads the genesis time + env.clk.Set(genesistest.DefaultValidatorStartTime) // VM's clock reads the genesis time upgradeTime := env.clk.Time().Add(SyncBound) env.config.UpgradeConfig.BanffTime = upgradeTime env.config.UpgradeConfig.CortinaTime = upgradeTime diff --git a/vms/platformvm/txs/executor/create_chain_test.go b/vms/platformvm/txs/executor/create_chain_test.go index a1ad4c81e186..d454cda30a78 100644 --- a/vms/platformvm/txs/executor/create_chain_test.go +++ b/vms/platformvm/txs/executor/create_chain_test.go @@ -18,6 +18,7 @@ import ( "github.com/ava-labs/avalanchego/utils/hashing" "github.com/ava-labs/avalanchego/utils/set" "github.com/ava-labs/avalanchego/utils/units" + "github.com/ava-labs/avalanchego/vms/platformvm/genesis/genesistest" "github.com/ava-labs/avalanchego/vms/platformvm/state" "github.com/ava-labs/avalanchego/vms/platformvm/txs" "github.com/ava-labs/avalanchego/vms/platformvm/txs/txstest" @@ -34,7 +35,7 @@ func TestCreateChainTxInsufficientControlSigs(t *testing.T) { env.ctx.Lock.Lock() defer env.ctx.Lock.Unlock() - builder, signer := env.factory.NewWallet(preFundedKeys[0], preFundedKeys[1]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) utx, err := builder.NewCreateChainTx( testSubnet1.ID(), nil, @@ -180,7 +181,7 @@ func TestCreateChainTxValid(t *testing.T) { } func TestCreateChainTxAP3FeeChange(t *testing.T) { - ap3Time := defaultGenesisTime.Add(time.Hour) + ap3Time := genesistest.DefaultValidatorStartTime.Add(time.Hour) tests := []struct { name string time time.Time @@ -189,7 +190,7 @@ func TestCreateChainTxAP3FeeChange(t *testing.T) { }{ { name: "pre-fork - correctly priced", - time: defaultGenesisTime, + time: genesistest.DefaultValidatorStartTime, fee: 0, expectedError: nil, }, @@ -213,8 +214,8 @@ func TestCreateChainTxAP3FeeChange(t *testing.T) { env := newEnvironment(t, upgradetest.Banff) env.config.UpgradeConfig.ApricotPhase3Time = ap3Time - addrs := set.NewSet[ids.ShortID](len(preFundedKeys)) - for _, key := range preFundedKeys { + addrs := set.NewSet[ids.ShortID](len(genesistest.DefaultFundedKeys)) + for _, key := range genesistest.DefaultFundedKeys { addrs.Add(key.Address()) } @@ -224,7 +225,7 @@ func TestCreateChainTxAP3FeeChange(t *testing.T) { cfg.StaticFeeConfig.CreateBlockchainTxFee = test.fee factory := txstest.NewWalletFactory(env.ctx, &cfg, env.state) - builder, signer := factory.NewWallet(preFundedKeys...) + builder, signer := factory.NewWallet(genesistest.DefaultFundedKeys...) utx, err := builder.NewCreateChainTx( testSubnet1.ID(), nil, diff --git a/vms/platformvm/txs/executor/create_subnet_test.go b/vms/platformvm/txs/executor/create_subnet_test.go index a991bda7de0e..ad51fdc0b2b0 100644 --- a/vms/platformvm/txs/executor/create_subnet_test.go +++ b/vms/platformvm/txs/executor/create_subnet_test.go @@ -14,6 +14,7 @@ import ( "github.com/ava-labs/avalanchego/upgrade/upgradetest" "github.com/ava-labs/avalanchego/utils/set" "github.com/ava-labs/avalanchego/utils/units" + "github.com/ava-labs/avalanchego/vms/platformvm/genesis/genesistest" "github.com/ava-labs/avalanchego/vms/platformvm/state" "github.com/ava-labs/avalanchego/vms/platformvm/txs/txstest" "github.com/ava-labs/avalanchego/vms/platformvm/utxo" @@ -23,7 +24,7 @@ import ( ) func TestCreateSubnetTxAP3FeeChange(t *testing.T) { - ap3Time := defaultGenesisTime.Add(time.Hour) + ap3Time := genesistest.DefaultValidatorStartTime.Add(time.Hour) tests := []struct { name string time time.Time @@ -32,7 +33,7 @@ func TestCreateSubnetTxAP3FeeChange(t *testing.T) { }{ { name: "pre-fork - correctly priced", - time: defaultGenesisTime, + time: genesistest.DefaultValidatorStartTime, fee: 0, expectedErr: nil, }, @@ -60,15 +61,15 @@ func TestCreateSubnetTxAP3FeeChange(t *testing.T) { env.state.SetTimestamp(test.time) // to duly set fee - addrs := set.NewSet[ids.ShortID](len(preFundedKeys)) - for _, key := range preFundedKeys { + addrs := set.NewSet[ids.ShortID](len(genesistest.DefaultFundedKeys)) + for _, key := range genesistest.DefaultFundedKeys { addrs.Add(key.Address()) } cfg := *env.config cfg.StaticFeeConfig.CreateSubnetTxFee = test.fee factory := txstest.NewWalletFactory(env.ctx, &cfg, env.state) - builder, signer := factory.NewWallet(preFundedKeys...) + builder, signer := factory.NewWallet(genesistest.DefaultFundedKeys...) utx, err := builder.NewCreateSubnetTx( &secp256k1fx.OutputOwners{}, ) diff --git a/vms/platformvm/txs/executor/export_test.go b/vms/platformvm/txs/executor/export_test.go index d392f0bc9618..a6f292c92c33 100644 --- a/vms/platformvm/txs/executor/export_test.go +++ b/vms/platformvm/txs/executor/export_test.go @@ -14,6 +14,7 @@ import ( "github.com/ava-labs/avalanchego/upgrade/upgradetest" "github.com/ava-labs/avalanchego/utils/crypto/secp256k1" "github.com/ava-labs/avalanchego/vms/components/avax" + "github.com/ava-labs/avalanchego/vms/platformvm/genesis/genesistest" "github.com/ava-labs/avalanchego/vms/platformvm/state" "github.com/ava-labs/avalanchego/vms/secp256k1fx" @@ -32,14 +33,14 @@ func TestNewExportTx(t *testing.T) { timestamp time.Time } - sourceKey := preFundedKeys[0] + sourceKey := genesistest.DefaultFundedKeys[0] tests := []test{ { description: "P->X export", destinationChainID: env.ctx.XChainID, sourceKeys: []*secp256k1.PrivateKey{sourceKey}, - timestamp: defaultValidateStartTime, + timestamp: genesistest.DefaultValidatorStartTime, }, { description: "P->C export", @@ -60,7 +61,7 @@ func TestNewExportTx(t *testing.T) { []*avax.TransferableOutput{{ Asset: avax.Asset{ID: env.ctx.AVAXAssetID}, Out: &secp256k1fx.TransferOutput{ - Amt: defaultBalance - defaultTxFee, + Amt: genesistest.DefaultInitialBalance - defaultTxFee, OutputOwners: secp256k1fx.OutputOwners{ Locktime: 0, Threshold: 1, diff --git a/vms/platformvm/txs/executor/helpers_test.go b/vms/platformvm/txs/executor/helpers_test.go index 4005c32fddaa..e740178fbe48 100644 --- a/vms/platformvm/txs/executor/helpers_test.go +++ b/vms/platformvm/txs/executor/helpers_test.go @@ -5,7 +5,6 @@ package executor import ( "context" - "fmt" "math" "testing" "time" @@ -26,16 +25,12 @@ import ( "github.com/ava-labs/avalanchego/upgrade/upgradetest" "github.com/ava-labs/avalanchego/utils" "github.com/ava-labs/avalanchego/utils/constants" - "github.com/ava-labs/avalanchego/utils/crypto/secp256k1" - "github.com/ava-labs/avalanchego/utils/formatting" - "github.com/ava-labs/avalanchego/utils/formatting/address" - "github.com/ava-labs/avalanchego/utils/json" "github.com/ava-labs/avalanchego/utils/logging" "github.com/ava-labs/avalanchego/utils/timer/mockable" "github.com/ava-labs/avalanchego/utils/units" - "github.com/ava-labs/avalanchego/vms/platformvm/api" "github.com/ava-labs/avalanchego/vms/platformvm/config" "github.com/ava-labs/avalanchego/vms/platformvm/fx" + "github.com/ava-labs/avalanchego/vms/platformvm/genesis/genesistest" "github.com/ava-labs/avalanchego/vms/platformvm/reward" "github.com/ava-labs/avalanchego/vms/platformvm/state" "github.com/ava-labs/avalanchego/vms/platformvm/state/statetest" @@ -51,35 +46,20 @@ import ( ) const ( - defaultWeight = 5 * units.MilliAvax - trackChecksum = false -) + defaultMinValidatorStake = 5 * units.MilliAvax -var ( defaultMinStakingDuration = 24 * time.Hour defaultMaxStakingDuration = 365 * 24 * time.Hour - defaultGenesisTime = time.Date(1997, 1, 1, 0, 0, 0, 0, time.UTC) - defaultValidateStartTime = defaultGenesisTime - defaultValidateEndTime = defaultValidateStartTime.Add(20 * defaultMinStakingDuration) - defaultMinValidatorStake = 5 * units.MilliAvax - defaultBalance = 100 * defaultMinValidatorStake - preFundedKeys = secp256k1.TestKeys() - defaultTxFee = uint64(100) - lastAcceptedID = ids.GenerateTestID() - - testSubnet1 *txs.Tx - testSubnet1ControlKeys = preFundedKeys[0:3] - // Node IDs of genesis validators. Initialized in init function - genesisNodeIDs []ids.NodeID + defaultTxFee = 100 * units.NanoAvax ) -func init() { - genesisNodeIDs = make([]ids.NodeID, len(preFundedKeys)) - for i := range preFundedKeys { - genesisNodeIDs[i] = ids.GenerateTestNodeID() - } -} +var ( + lastAcceptedID = ids.GenerateTestID() + + testSubnet1 *txs.Tx + testSubnet1ControlKeys = genesistest.DefaultFundedKeys[0:3] +) type mutableSharedMemory struct { atomic.SharedMemory @@ -133,7 +113,7 @@ func newEnvironment(t *testing.T, f upgradetest.Fork) *environment { rewards := reward.NewCalculator(config.RewardConfig) baseState := statetest.New(t, statetest.Config{ DB: baseDB, - Genesis: buildGenesisTest(ctx), + Genesis: genesistest.NewBytes(t, genesistest.Config{}), Validators: config.Validators, Upgrades: config.UpgradeConfig, Context: ctx, @@ -205,19 +185,19 @@ func newEnvironment(t *testing.T, f upgradetest.Fork) *environment { func addSubnet(t *testing.T, env *environment) { require := require.New(t) - builder, signer := env.factory.NewWallet(preFundedKeys[0]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewCreateSubnetTx( &secp256k1fx.OutputOwners{ Threshold: 2, Addrs: []ids.ShortID{ - preFundedKeys[0].PublicKey().Address(), - preFundedKeys[1].PublicKey().Address(), - preFundedKeys[2].PublicKey().Address(), + genesistest.DefaultFundedKeys[0].Address(), + genesistest.DefaultFundedKeys[1].Address(), + genesistest.DefaultFundedKeys[2].Address(), }, }, common.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{preFundedKeys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }), ) require.NoError(err) @@ -244,12 +224,12 @@ func addSubnet(t *testing.T, env *environment) { func defaultConfig(f upgradetest.Fork) *config.Config { upgrades := upgradetest.GetConfigWithUpgradeTime( f, - defaultValidateStartTime.Add(-2*time.Second), + genesistest.DefaultValidatorStartTime.Add(-2*time.Second), ) upgradetest.SetTimesTo( &upgrades, min(f, upgradetest.ApricotPhase5), - defaultValidateEndTime, + genesistest.DefaultValidatorEndTime, ) return &config.Config{ @@ -277,10 +257,10 @@ func defaultConfig(f upgradetest.Fork) *config.Config { } func defaultClock(f upgradetest.Fork) *mockable.Clock { - now := defaultGenesisTime + now := genesistest.DefaultValidatorStartTime if f >= upgradetest.Banff { // 1 second after active fork - now = defaultValidateEndTime.Add(-2 * time.Second) + now = genesistest.DefaultValidatorEndTime.Add(-2 * time.Second) } clk := &mockable.Clock{} clk.Set(now) @@ -322,66 +302,3 @@ func defaultFx(clk *mockable.Clock, log logging.Logger, isBootstrapped bool) fx. } return res } - -func buildGenesisTest(ctx *snow.Context) []byte { - genesisUTXOs := make([]api.UTXO, len(preFundedKeys)) - for i, key := range preFundedKeys { - id := key.PublicKey().Address() - addr, err := address.FormatBech32(constants.UnitTestHRP, id.Bytes()) - if err != nil { - panic(err) - } - genesisUTXOs[i] = api.UTXO{ - Amount: json.Uint64(defaultBalance), - Address: addr, - } - } - - genesisValidators := make([]api.GenesisPermissionlessValidator, len(genesisNodeIDs)) - for i, nodeID := range genesisNodeIDs { - addr, err := address.FormatBech32(constants.UnitTestHRP, nodeID.Bytes()) - if err != nil { - panic(err) - } - genesisValidators[i] = api.GenesisPermissionlessValidator{ - GenesisValidator: api.GenesisValidator{ - StartTime: json.Uint64(defaultValidateStartTime.Unix()), - EndTime: json.Uint64(defaultValidateEndTime.Unix()), - NodeID: nodeID, - }, - RewardOwner: &api.Owner{ - Threshold: 1, - Addresses: []string{addr}, - }, - Staked: []api.UTXO{{ - Amount: json.Uint64(defaultWeight), - Address: addr, - }}, - DelegationFee: reward.PercentDenominator, - } - } - - buildGenesisArgs := api.BuildGenesisArgs{ - NetworkID: json.Uint32(constants.UnitTestID), - AvaxAssetID: ctx.AVAXAssetID, - UTXOs: genesisUTXOs, - Validators: genesisValidators, - Chains: nil, - Time: json.Uint64(defaultGenesisTime.Unix()), - InitialSupply: json.Uint64(360 * units.MegaAvax), - Encoding: formatting.Hex, - } - - buildGenesisResponse := api.BuildGenesisReply{} - platformvmSS := api.StaticService{} - if err := platformvmSS.BuildGenesis(nil, &buildGenesisArgs, &buildGenesisResponse); err != nil { - panic(fmt.Errorf("problem while building platform chain's genesis state: %w", err)) - } - - genesisBytes, err := formatting.Decode(buildGenesisResponse.Encoding, buildGenesisResponse.Bytes) - if err != nil { - panic(err) - } - - return genesisBytes -} diff --git a/vms/platformvm/txs/executor/import_test.go b/vms/platformvm/txs/executor/import_test.go index ed9ff97375af..6cc5c8a0cb38 100644 --- a/vms/platformvm/txs/executor/import_test.go +++ b/vms/platformvm/txs/executor/import_test.go @@ -200,7 +200,7 @@ func fundedSharedMemory( Amt: amt, OutputOwners: secp256k1fx.OutputOwners{ Locktime: 0, - Addrs: []ids.ShortID{sourceKey.PublicKey().Address()}, + Addrs: []ids.ShortID{sourceKey.Address()}, Threshold: 1, }, }, @@ -216,7 +216,7 @@ func fundedSharedMemory( Key: inputID[:], Value: utxoBytes, Traits: [][]byte{ - sourceKey.PublicKey().Address().Bytes(), + sourceKey.Address().Bytes(), }, }, }, diff --git a/vms/platformvm/txs/executor/proposal_tx_executor_test.go b/vms/platformvm/txs/executor/proposal_tx_executor_test.go index 0efbae5e65ba..e7d79ca58eba 100644 --- a/vms/platformvm/txs/executor/proposal_tx_executor_test.go +++ b/vms/platformvm/txs/executor/proposal_tx_executor_test.go @@ -16,6 +16,7 @@ import ( "github.com/ava-labs/avalanchego/upgrade/upgradetest" "github.com/ava-labs/avalanchego/utils/crypto/secp256k1" "github.com/ava-labs/avalanchego/utils/hashing" + "github.com/ava-labs/avalanchego/vms/platformvm/genesis/genesistest" "github.com/ava-labs/avalanchego/vms/platformvm/reward" "github.com/ava-labs/avalanchego/vms/platformvm/state" "github.com/ava-labs/avalanchego/vms/platformvm/status" @@ -27,19 +28,19 @@ import ( func TestProposalTxExecuteAddDelegator(t *testing.T) { dummyHeight := uint64(1) - rewardAddress := preFundedKeys[0].PublicKey().Address() - nodeID := genesisNodeIDs[0] + rewardAddress := genesistest.DefaultFundedKeys[0].Address() + nodeID := genesistest.DefaultNodeIDs[0] newValidatorID := ids.GenerateTestNodeID() - newValidatorStartTime := uint64(defaultValidateStartTime.Add(5 * time.Second).Unix()) - newValidatorEndTime := uint64(defaultValidateEndTime.Add(-5 * time.Second).Unix()) + newValidatorStartTime := uint64(genesistest.DefaultValidatorStartTime.Add(5 * time.Second).Unix()) + newValidatorEndTime := uint64(genesistest.DefaultValidatorEndTime.Add(-5 * time.Second).Unix()) // [addMinStakeValidator] adds a new validator to the primary network's // pending validator set with the minimum staking amount addMinStakeValidator := func(env *environment) { require := require.New(t) - builder, signer := env.factory.NewWallet(preFundedKeys[0]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: newValidatorID, @@ -77,7 +78,7 @@ func TestProposalTxExecuteAddDelegator(t *testing.T) { addMaxStakeValidator := func(env *environment) { require := require.New(t) - builder, signer := env.factory.NewWallet(preFundedKeys[0]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: newValidatorID, @@ -129,23 +130,23 @@ func TestProposalTxExecuteAddDelegator(t *testing.T) { { description: "validator stops validating earlier than delegator", stakeAmount: env.config.MinDelegatorStake, - startTime: uint64(defaultValidateStartTime.Unix()) + 1, - endTime: uint64(defaultValidateEndTime.Unix()) + 1, + startTime: genesistest.DefaultValidatorStartTimeUnix + 1, + endTime: genesistest.DefaultValidatorEndTimeUnix + 1, nodeID: nodeID, - feeKeys: []*secp256k1.PrivateKey{preFundedKeys[0]}, + feeKeys: []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}, setup: nil, - AP3Time: defaultGenesisTime, + AP3Time: genesistest.DefaultValidatorStartTime, expectedErr: ErrPeriodMismatch, }, { description: "validator not in the current or pending validator sets", stakeAmount: env.config.MinDelegatorStake, - startTime: uint64(defaultValidateStartTime.Add(5 * time.Second).Unix()), - endTime: uint64(defaultValidateEndTime.Add(-5 * time.Second).Unix()), + startTime: uint64(genesistest.DefaultValidatorStartTime.Add(5 * time.Second).Unix()), + endTime: uint64(genesistest.DefaultValidatorEndTime.Add(-5 * time.Second).Unix()), nodeID: newValidatorID, - feeKeys: []*secp256k1.PrivateKey{preFundedKeys[0]}, + feeKeys: []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}, setup: nil, - AP3Time: defaultGenesisTime, + AP3Time: genesistest.DefaultValidatorStartTime, expectedErr: database.ErrNotFound, }, { @@ -154,9 +155,9 @@ func TestProposalTxExecuteAddDelegator(t *testing.T) { startTime: newValidatorStartTime - 1, // start validating subnet before primary network endTime: newValidatorEndTime, nodeID: newValidatorID, - feeKeys: []*secp256k1.PrivateKey{preFundedKeys[0]}, + feeKeys: []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}, setup: addMinStakeValidator, - AP3Time: defaultGenesisTime, + AP3Time: genesistest.DefaultValidatorStartTime, expectedErr: ErrPeriodMismatch, }, { @@ -165,9 +166,9 @@ func TestProposalTxExecuteAddDelegator(t *testing.T) { startTime: newValidatorStartTime, endTime: newValidatorEndTime + 1, // stop validating subnet after stopping validating primary network nodeID: newValidatorID, - feeKeys: []*secp256k1.PrivateKey{preFundedKeys[0]}, + feeKeys: []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}, setup: addMinStakeValidator, - AP3Time: defaultGenesisTime, + AP3Time: genesistest.DefaultValidatorStartTime, expectedErr: ErrPeriodMismatch, }, { @@ -176,32 +177,32 @@ func TestProposalTxExecuteAddDelegator(t *testing.T) { startTime: newValidatorStartTime, // same start time as for primary network endTime: newValidatorEndTime, // same end time as for primary network nodeID: newValidatorID, - feeKeys: []*secp256k1.PrivateKey{preFundedKeys[0]}, + feeKeys: []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}, setup: addMinStakeValidator, - AP3Time: defaultGenesisTime, + AP3Time: genesistest.DefaultValidatorStartTime, expectedErr: nil, }, { description: "starts delegating at current timestamp", stakeAmount: env.config.MinDelegatorStake, startTime: uint64(currentTimestamp.Unix()), - endTime: uint64(defaultValidateEndTime.Unix()), + endTime: genesistest.DefaultValidatorEndTimeUnix, nodeID: nodeID, - feeKeys: []*secp256k1.PrivateKey{preFundedKeys[0]}, + feeKeys: []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}, setup: nil, - AP3Time: defaultGenesisTime, + AP3Time: genesistest.DefaultValidatorStartTime, expectedErr: ErrTimestampNotBeforeStartTime, }, { description: "tx fee paying key has no funds", stakeAmount: env.config.MinDelegatorStake, - startTime: uint64(defaultValidateStartTime.Unix()) + 1, - endTime: uint64(defaultValidateEndTime.Unix()), + startTime: genesistest.DefaultValidatorStartTimeUnix + 1, + endTime: genesistest.DefaultValidatorEndTimeUnix, nodeID: nodeID, - feeKeys: []*secp256k1.PrivateKey{preFundedKeys[1]}, + feeKeys: []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[1]}, setup: func(env *environment) { // Remove all UTXOs owned by keys[1] utxoIDs, err := env.state.UTXOIDs( - preFundedKeys[1].PublicKey().Address().Bytes(), + genesistest.DefaultFundedKeys[1].Address().Bytes(), ids.Empty, math.MaxInt32) require.NoError(t, err) @@ -212,7 +213,7 @@ func TestProposalTxExecuteAddDelegator(t *testing.T) { env.state.SetHeight(dummyHeight) require.NoError(t, env.state.Commit()) }, - AP3Time: defaultGenesisTime, + AP3Time: genesistest.DefaultValidatorStartTime, expectedErr: ErrFlowCheckFailed, }, { @@ -221,9 +222,9 @@ func TestProposalTxExecuteAddDelegator(t *testing.T) { startTime: newValidatorStartTime, // same start time as for primary network endTime: newValidatorEndTime, // same end time as for primary network nodeID: newValidatorID, - feeKeys: []*secp256k1.PrivateKey{preFundedKeys[0]}, + feeKeys: []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}, setup: addMaxStakeValidator, - AP3Time: defaultValidateEndTime, + AP3Time: genesistest.DefaultValidatorEndTime, expectedErr: nil, }, { @@ -232,9 +233,9 @@ func TestProposalTxExecuteAddDelegator(t *testing.T) { startTime: newValidatorStartTime, // same start time as for primary network endTime: newValidatorEndTime, // same end time as for primary network nodeID: newValidatorID, - feeKeys: []*secp256k1.PrivateKey{preFundedKeys[0]}, + feeKeys: []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}, setup: addMaxStakeValidator, - AP3Time: defaultGenesisTime, + AP3Time: genesistest.DefaultValidatorStartTime, expectedErr: ErrOverDelegated, }, } @@ -292,7 +293,7 @@ func TestProposalTxExecuteAddSubnetValidator(t *testing.T) { env.ctx.Lock.Lock() defer env.ctx.Lock.Unlock() - nodeID := genesisNodeIDs[0] + nodeID := genesistest.DefaultNodeIDs[0] { // Case: Proposed validator currently validating primary network // but stops validating subnet after stops validating primary network @@ -302,9 +303,9 @@ func TestProposalTxExecuteAddSubnetValidator(t *testing.T) { &txs.SubnetValidator{ Validator: txs.Validator{ NodeID: nodeID, - Start: uint64(defaultValidateStartTime.Unix()) + 1, - End: uint64(defaultValidateEndTime.Unix()) + 1, - Wght: defaultWeight, + Start: genesistest.DefaultValidatorStartTimeUnix + 1, + End: genesistest.DefaultValidatorEndTimeUnix + 1, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -341,9 +342,9 @@ func TestProposalTxExecuteAddSubnetValidator(t *testing.T) { &txs.SubnetValidator{ Validator: txs.Validator{ NodeID: nodeID, - Start: uint64(defaultValidateStartTime.Unix()) + 1, - End: uint64(defaultValidateEndTime.Unix()), - Wght: defaultWeight, + Start: genesistest.DefaultValidatorStartTimeUnix + 1, + End: genesistest.DefaultValidatorEndTimeUnix, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -372,10 +373,10 @@ func TestProposalTxExecuteAddSubnetValidator(t *testing.T) { // Add a validator to pending validator set of primary network // Starts validating primary network 10 seconds after genesis pendingDSValidatorID := ids.GenerateTestNodeID() - dsStartTime := defaultValidateStartTime.Add(10 * time.Second) + dsStartTime := genesistest.DefaultValidatorStartTime.Add(10 * time.Second) dsEndTime := dsStartTime.Add(5 * defaultMinStakingDuration) - builder, signer := env.factory.NewWallet(preFundedKeys[0]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: pendingDSValidatorID, @@ -402,7 +403,7 @@ func TestProposalTxExecuteAddSubnetValidator(t *testing.T) { NodeID: pendingDSValidatorID, Start: uint64(dsStartTime.Unix()), // start validating subnet before primary network End: uint64(dsEndTime.Unix()), - Wght: defaultWeight, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -444,7 +445,7 @@ func TestProposalTxExecuteAddSubnetValidator(t *testing.T) { env.state.SetHeight(dummyHeight) require.NoError(env.state.Commit()) - // Node with ID key.PublicKey().Address() now a pending validator for primary network + // Node with ID key.Address() now a pending validator for primary network { // Case: Proposed validator is pending validator of primary network @@ -456,7 +457,7 @@ func TestProposalTxExecuteAddSubnetValidator(t *testing.T) { NodeID: pendingDSValidatorID, Start: uint64(dsStartTime.Unix()) - 1, // start validating subnet before primary network End: uint64(dsEndTime.Unix()), - Wght: defaultWeight, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -493,7 +494,7 @@ func TestProposalTxExecuteAddSubnetValidator(t *testing.T) { NodeID: pendingDSValidatorID, Start: uint64(dsStartTime.Unix()), End: uint64(dsEndTime.Unix()) + 1, // stop validating subnet after stopping validating primary network - Wght: defaultWeight, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -530,7 +531,7 @@ func TestProposalTxExecuteAddSubnetValidator(t *testing.T) { NodeID: pendingDSValidatorID, Start: uint64(dsStartTime.Unix()), // same start time as for primary network End: uint64(dsEndTime.Unix()), // same end time as for primary network - Wght: defaultWeight, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -558,7 +559,7 @@ func TestProposalTxExecuteAddSubnetValidator(t *testing.T) { // Case: Proposed validator start validating at/before current timestamp // First, advance the timestamp - newTimestamp := defaultValidateStartTime.Add(2 * time.Second) + newTimestamp := genesistest.DefaultValidatorStartTime.Add(2 * time.Second) env.state.SetTimestamp(newTimestamp) { @@ -569,7 +570,7 @@ func TestProposalTxExecuteAddSubnetValidator(t *testing.T) { NodeID: nodeID, Start: uint64(newTimestamp.Unix()), End: uint64(newTimestamp.Add(defaultMinStakingDuration).Unix()), - Wght: defaultWeight, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -597,7 +598,7 @@ func TestProposalTxExecuteAddSubnetValidator(t *testing.T) { } // reset the timestamp - env.state.SetTimestamp(defaultValidateStartTime) + env.state.SetTimestamp(genesistest.DefaultValidatorStartTime) // Case: Proposed validator already validating the subnet // First, add validator as validator of subnet @@ -606,9 +607,9 @@ func TestProposalTxExecuteAddSubnetValidator(t *testing.T) { &txs.SubnetValidator{ Validator: txs.Validator{ NodeID: nodeID, - Start: uint64(defaultValidateStartTime.Unix()), - End: uint64(defaultValidateEndTime.Unix()), - Wght: defaultWeight, + Start: genesistest.DefaultValidatorStartTimeUnix, + End: genesistest.DefaultValidatorEndTimeUnix, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -632,15 +633,15 @@ func TestProposalTxExecuteAddSubnetValidator(t *testing.T) { require.NoError(env.state.Commit()) { - // Node with ID nodeIDKey.PublicKey().Address() now validating subnet with ID testSubnet1.ID + // Node with ID nodeIDKey.Address() now validating subnet with ID testSubnet1.ID builder, signer := env.factory.NewWallet(testSubnet1ControlKeys[0], testSubnet1ControlKeys[1]) utx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ NodeID: nodeID, - Start: uint64(defaultValidateStartTime.Unix()) + 1, - End: uint64(defaultValidateEndTime.Unix()), - Wght: defaultWeight, + Start: genesistest.DefaultValidatorStartTimeUnix + 1, + End: genesistest.DefaultValidatorEndTimeUnix, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -678,9 +679,9 @@ func TestProposalTxExecuteAddSubnetValidator(t *testing.T) { &txs.SubnetValidator{ Validator: txs.Validator{ NodeID: nodeID, - Start: uint64(defaultValidateStartTime.Unix()) + 1, - End: uint64(defaultValidateStartTime.Add(defaultMinStakingDuration).Unix()) + 1, - Wght: defaultWeight, + Start: genesistest.DefaultValidatorStartTimeUnix + 1, + End: uint64(genesistest.DefaultValidatorStartTime.Add(defaultMinStakingDuration).Unix()) + 1, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -716,14 +717,14 @@ func TestProposalTxExecuteAddSubnetValidator(t *testing.T) { { // Case: Control Signature from invalid key (keys[3] is not a control key) - builder, signer := env.factory.NewWallet(testSubnet1ControlKeys[0], preFundedKeys[1]) + builder, signer := env.factory.NewWallet(testSubnet1ControlKeys[0], genesistest.DefaultFundedKeys[1]) utx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ NodeID: nodeID, - Start: uint64(defaultValidateStartTime.Unix()) + 1, - End: uint64(defaultValidateStartTime.Add(defaultMinStakingDuration).Unix()) + 1, - Wght: defaultWeight, + Start: genesistest.DefaultValidatorStartTimeUnix + 1, + End: uint64(genesistest.DefaultValidatorStartTime.Add(defaultMinStakingDuration).Unix()) + 1, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -733,7 +734,7 @@ func TestProposalTxExecuteAddSubnetValidator(t *testing.T) { require.NoError(err) // Replace a valid signature with one from keys[3] - sig, err := preFundedKeys[3].SignHash(hashing.ComputeHash256(tx.Unsigned.Bytes())) + sig, err := genesistest.DefaultFundedKeys[3].SignHash(hashing.ComputeHash256(tx.Unsigned.Bytes())) require.NoError(err) copy(tx.Creds[0].(*secp256k1fx.Credential).Sigs[0][:], sig) @@ -763,9 +764,9 @@ func TestProposalTxExecuteAddSubnetValidator(t *testing.T) { &txs.SubnetValidator{ Validator: txs.Validator{ NodeID: nodeID, - Start: uint64(defaultValidateStartTime.Unix()) + 1, - End: uint64(defaultValidateStartTime.Add(defaultMinStakingDuration).Unix()) + 1, - Wght: defaultWeight, + Start: genesistest.DefaultValidatorStartTimeUnix + 1, + End: uint64(genesistest.DefaultValidatorStartTime.Add(defaultMinStakingDuration).Unix()) + 1, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -818,12 +819,12 @@ func TestProposalTxExecuteAddValidator(t *testing.T) { { // Case: Validator's start time too early - builder, signer := env.factory.NewWallet(preFundedKeys[0]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: nodeID, Start: uint64(chainTime.Unix()), - End: uint64(defaultValidateEndTime.Unix()), + End: genesistest.DefaultValidatorEndTimeUnix, Wght: env.config.MinValidatorStake, }, &secp256k1fx.OutputOwners{ @@ -855,15 +856,15 @@ func TestProposalTxExecuteAddValidator(t *testing.T) { } { - nodeID := genesisNodeIDs[0] + nodeID := genesistest.DefaultNodeIDs[0] // Case: Validator already validating primary network - builder, signer := env.factory.NewWallet(preFundedKeys[0]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: nodeID, - Start: uint64(defaultValidateStartTime.Unix()) + 1, - End: uint64(defaultValidateEndTime.Unix()), + Start: genesistest.DefaultValidatorStartTimeUnix + 1, + End: genesistest.DefaultValidatorEndTimeUnix, Wght: env.config.MinValidatorStake, }, &secp256k1fx.OutputOwners{ @@ -896,8 +897,8 @@ func TestProposalTxExecuteAddValidator(t *testing.T) { { // Case: Validator in pending validator set of primary network - startTime := defaultValidateStartTime.Add(1 * time.Second) - builder, signer := env.factory.NewWallet(preFundedKeys[0]) + startTime := genesistest.DefaultValidatorStartTime.Add(1 * time.Second) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: nodeID, @@ -950,12 +951,12 @@ func TestProposalTxExecuteAddValidator(t *testing.T) { { // Case: Validator doesn't have enough tokens to cover stake amount - builder, signer := env.factory.NewWallet(preFundedKeys[0]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: ids.GenerateTestNodeID(), - Start: uint64(defaultValidateStartTime.Unix()) + 1, - End: uint64(defaultValidateEndTime.Unix()), + Start: genesistest.DefaultValidatorStartTimeUnix + 1, + End: genesistest.DefaultValidatorEndTimeUnix, Wght: env.config.MinValidatorStake, }, &secp256k1fx.OutputOwners{ @@ -969,7 +970,7 @@ func TestProposalTxExecuteAddValidator(t *testing.T) { require.NoError(err) // Remove all UTXOs owned by preFundedKeys[0] - utxoIDs, err := env.state.UTXOIDs(preFundedKeys[0].PublicKey().Address().Bytes(), ids.Empty, math.MaxInt32) + utxoIDs, err := env.state.UTXOIDs(genesistest.DefaultFundedKeys[0].Address().Bytes(), ids.Empty, math.MaxInt32) require.NoError(err) for _, utxoID := range utxoIDs { diff --git a/vms/platformvm/txs/executor/reward_validator_test.go b/vms/platformvm/txs/executor/reward_validator_test.go index 04dc3c27af9a..27dd9d304937 100644 --- a/vms/platformvm/txs/executor/reward_validator_test.go +++ b/vms/platformvm/txs/executor/reward_validator_test.go @@ -18,6 +18,7 @@ import ( "github.com/ava-labs/avalanchego/utils/math" "github.com/ava-labs/avalanchego/utils/set" "github.com/ava-labs/avalanchego/vms/components/avax" + "github.com/ava-labs/avalanchego/vms/platformvm/genesis/genesistest" "github.com/ava-labs/avalanchego/vms/platformvm/reward" "github.com/ava-labs/avalanchego/vms/platformvm/state" "github.com/ava-labs/avalanchego/vms/platformvm/status" @@ -137,7 +138,7 @@ func TestRewardValidatorTxExecuteOnCommit(t *testing.T) { onCommitBalance, err := avax.GetBalance(env.state, stakeOwners) require.NoError(err) - require.Equal(oldBalance+stakerToRemove.Weight+27697, onCommitBalance) + require.Equal(oldBalance+stakerToRemove.Weight+38944, onCommitBalance) } func TestRewardValidatorTxExecuteOnAbort(t *testing.T) { @@ -246,11 +247,11 @@ func TestRewardDelegatorTxExecuteOnCommitPreDelegateeDeferral(t *testing.T) { vdrRewardAddress := ids.GenerateTestShortID() delRewardAddress := ids.GenerateTestShortID() - vdrStartTime := uint64(defaultValidateStartTime.Unix()) + 1 - vdrEndTime := uint64(defaultValidateStartTime.Add(2 * defaultMinStakingDuration).Unix()) + vdrStartTime := genesistest.DefaultValidatorStartTimeUnix + 1 + vdrEndTime := uint64(genesistest.DefaultValidatorStartTime.Add(2 * defaultMinStakingDuration).Unix()) vdrNodeID := ids.GenerateTestNodeID() - builder, signer := env.factory.NewWallet(preFundedKeys[0]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) uVdrTx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: vdrNodeID, @@ -380,11 +381,11 @@ func TestRewardDelegatorTxExecuteOnCommitPostDelegateeDeferral(t *testing.T) { vdrRewardAddress := ids.GenerateTestShortID() delRewardAddress := ids.GenerateTestShortID() - vdrStartTime := uint64(defaultValidateStartTime.Unix()) + 1 - vdrEndTime := uint64(defaultValidateStartTime.Add(2 * defaultMinStakingDuration).Unix()) + vdrStartTime := genesistest.DefaultValidatorStartTimeUnix + 1 + vdrEndTime := uint64(genesistest.DefaultValidatorStartTime.Add(2 * defaultMinStakingDuration).Unix()) vdrNodeID := ids.GenerateTestNodeID() - builder, signer := env.factory.NewWallet(preFundedKeys[0]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) uVdrTx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: vdrNodeID, @@ -610,11 +611,11 @@ func TestRewardDelegatorTxAndValidatorTxExecuteOnCommitPostDelegateeDeferral(t * vdrRewardAddress := ids.GenerateTestShortID() delRewardAddress := ids.GenerateTestShortID() - vdrStartTime := uint64(defaultValidateStartTime.Unix()) + 1 - vdrEndTime := uint64(defaultValidateStartTime.Add(2 * defaultMinStakingDuration).Unix()) + vdrStartTime := genesistest.DefaultValidatorStartTimeUnix + 1 + vdrEndTime := uint64(genesistest.DefaultValidatorStartTime.Add(2 * defaultMinStakingDuration).Unix()) vdrNodeID := ids.GenerateTestNodeID() - builder, signer := env.factory.NewWallet(preFundedKeys[0]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) uVdrTx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: vdrNodeID, @@ -786,11 +787,11 @@ func TestRewardDelegatorTxExecuteOnAbort(t *testing.T) { vdrRewardAddress := ids.GenerateTestShortID() delRewardAddress := ids.GenerateTestShortID() - vdrStartTime := uint64(defaultValidateStartTime.Unix()) + 1 - vdrEndTime := uint64(defaultValidateStartTime.Add(2 * defaultMinStakingDuration).Unix()) + vdrStartTime := genesistest.DefaultValidatorStartTimeUnix + 1 + vdrEndTime := uint64(genesistest.DefaultValidatorStartTime.Add(2 * defaultMinStakingDuration).Unix()) vdrNodeID := ids.GenerateTestNodeID() - builder, signer := env.factory.NewWallet(preFundedKeys[0]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) uVdrTx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: vdrNodeID, diff --git a/vms/platformvm/txs/executor/standard_tx_executor_test.go b/vms/platformvm/txs/executor/standard_tx_executor_test.go index d6f16a981c7d..d0652e6bd419 100644 --- a/vms/platformvm/txs/executor/standard_tx_executor_test.go +++ b/vms/platformvm/txs/executor/standard_tx_executor_test.go @@ -28,6 +28,7 @@ import ( "github.com/ava-labs/avalanchego/vms/components/verify" "github.com/ava-labs/avalanchego/vms/platformvm/config" "github.com/ava-labs/avalanchego/vms/platformvm/fx/fxmock" + "github.com/ava-labs/avalanchego/vms/platformvm/genesis/genesistest" "github.com/ava-labs/avalanchego/vms/platformvm/reward" "github.com/ava-labs/avalanchego/vms/platformvm/signer" "github.com/ava-labs/avalanchego/vms/platformvm/state" @@ -53,7 +54,7 @@ func TestStandardTxExecutorAddValidatorTxEmptyID(t *testing.T) { defer env.ctx.Lock.Unlock() chainTime := env.state.GetTimestamp() - startTime := defaultValidateStartTime.Add(1 * time.Second) + startTime := genesistest.DefaultValidatorStartTime.Add(1 * time.Second) tests := []struct { banffTime time.Time @@ -76,12 +77,12 @@ func TestStandardTxExecutorAddValidatorTxEmptyID(t *testing.T) { // Case: Empty validator node ID after banff env.config.UpgradeConfig.BanffTime = test.banffTime - builder, signer := env.factory.NewWallet(preFundedKeys[0]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: ids.EmptyNodeID, Start: uint64(startTime.Unix()), - End: uint64(defaultValidateEndTime.Unix()), + End: genesistest.DefaultValidatorEndTimeUnix, Wght: env.config.MinValidatorStake, }, &secp256k1fx.OutputOwners{ @@ -111,19 +112,19 @@ func TestStandardTxExecutorAddValidatorTxEmptyID(t *testing.T) { func TestStandardTxExecutorAddDelegator(t *testing.T) { dummyHeight := uint64(1) - rewardAddress := preFundedKeys[0].PublicKey().Address() - nodeID := genesisNodeIDs[0] + rewardAddress := genesistest.DefaultFundedKeys[0].Address() + nodeID := genesistest.DefaultNodeIDs[0] newValidatorID := ids.GenerateTestNodeID() - newValidatorStartTime := defaultValidateStartTime.Add(5 * time.Second) - newValidatorEndTime := defaultValidateEndTime.Add(-5 * time.Second) + newValidatorStartTime := genesistest.DefaultValidatorStartTime.Add(5 * time.Second) + newValidatorEndTime := genesistest.DefaultValidatorEndTime.Add(-5 * time.Second) // [addMinStakeValidator] adds a new validator to the primary network's // pending validator set with the minimum staking amount addMinStakeValidator := func(env *environment) { require := require.New(t) - builder, signer := env.factory.NewWallet(preFundedKeys[0]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: newValidatorID, @@ -161,7 +162,7 @@ func TestStandardTxExecutorAddDelegator(t *testing.T) { addMaxStakeValidator := func(env *environment) { require := require.New(t) - builder, signer := env.factory.NewWallet(preFundedKeys[0]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: newValidatorID, @@ -213,23 +214,23 @@ func TestStandardTxExecutorAddDelegator(t *testing.T) { { description: "validator stops validating earlier than delegator", stakeAmount: env.config.MinDelegatorStake, - startTime: defaultValidateStartTime.Add(time.Second), - endTime: defaultValidateEndTime.Add(time.Second), + startTime: genesistest.DefaultValidatorStartTime.Add(time.Second), + endTime: genesistest.DefaultValidatorEndTime.Add(time.Second), nodeID: nodeID, - feeKeys: []*secp256k1.PrivateKey{preFundedKeys[0]}, + feeKeys: []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}, setup: nil, - AP3Time: defaultGenesisTime, + AP3Time: genesistest.DefaultValidatorStartTime, expectedExecutionErr: ErrPeriodMismatch, }, { description: "validator not in the current or pending validator sets", stakeAmount: env.config.MinDelegatorStake, - startTime: defaultValidateStartTime.Add(5 * time.Second), - endTime: defaultValidateEndTime.Add(-5 * time.Second), + startTime: genesistest.DefaultValidatorStartTime.Add(5 * time.Second), + endTime: genesistest.DefaultValidatorEndTime.Add(-5 * time.Second), nodeID: newValidatorID, - feeKeys: []*secp256k1.PrivateKey{preFundedKeys[0]}, + feeKeys: []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}, setup: nil, - AP3Time: defaultGenesisTime, + AP3Time: genesistest.DefaultValidatorStartTime, expectedExecutionErr: database.ErrNotFound, }, { @@ -238,9 +239,9 @@ func TestStandardTxExecutorAddDelegator(t *testing.T) { startTime: newValidatorStartTime.Add(-1 * time.Second), // start validating subnet before primary network endTime: newValidatorEndTime, nodeID: newValidatorID, - feeKeys: []*secp256k1.PrivateKey{preFundedKeys[0]}, + feeKeys: []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}, setup: addMinStakeValidator, - AP3Time: defaultGenesisTime, + AP3Time: genesistest.DefaultValidatorStartTime, expectedExecutionErr: ErrPeriodMismatch, }, { @@ -249,9 +250,9 @@ func TestStandardTxExecutorAddDelegator(t *testing.T) { startTime: newValidatorStartTime, endTime: newValidatorEndTime.Add(time.Second), // stop validating subnet after stopping validating primary network nodeID: newValidatorID, - feeKeys: []*secp256k1.PrivateKey{preFundedKeys[0]}, + feeKeys: []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}, setup: addMinStakeValidator, - AP3Time: defaultGenesisTime, + AP3Time: genesistest.DefaultValidatorStartTime, expectedExecutionErr: ErrPeriodMismatch, }, { @@ -260,32 +261,32 @@ func TestStandardTxExecutorAddDelegator(t *testing.T) { startTime: newValidatorStartTime, // same start time as for primary network endTime: newValidatorEndTime, // same end time as for primary network nodeID: newValidatorID, - feeKeys: []*secp256k1.PrivateKey{preFundedKeys[0]}, + feeKeys: []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}, setup: addMinStakeValidator, - AP3Time: defaultGenesisTime, + AP3Time: genesistest.DefaultValidatorStartTime, expectedExecutionErr: nil, }, { description: "starts delegating at current timestamp", - stakeAmount: env.config.MinDelegatorStake, // weight - startTime: currentTimestamp, // start time - endTime: defaultValidateEndTime, // end time - nodeID: nodeID, // node ID - feeKeys: []*secp256k1.PrivateKey{preFundedKeys[0]}, // tx fee payer + stakeAmount: env.config.MinDelegatorStake, // weight + startTime: currentTimestamp, // start time + endTime: genesistest.DefaultValidatorEndTime, // end time + nodeID: nodeID, // node ID + feeKeys: []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}, // tx fee payer setup: nil, - AP3Time: defaultGenesisTime, + AP3Time: genesistest.DefaultValidatorStartTime, expectedExecutionErr: ErrTimestampNotBeforeStartTime, }, { description: "tx fee paying key has no funds", - stakeAmount: env.config.MinDelegatorStake, // weight - startTime: defaultValidateStartTime.Add(time.Second), // start time - endTime: defaultValidateEndTime, // end time - nodeID: nodeID, // node ID - feeKeys: []*secp256k1.PrivateKey{preFundedKeys[1]}, // tx fee payer + stakeAmount: env.config.MinDelegatorStake, // weight + startTime: genesistest.DefaultValidatorStartTime.Add(time.Second), // start time + endTime: genesistest.DefaultValidatorEndTime, // end time + nodeID: nodeID, // node ID + feeKeys: []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[1]}, // tx fee payer setup: func(env *environment) { // Remove all UTXOs owned by keys[1] utxoIDs, err := env.state.UTXOIDs( - preFundedKeys[1].PublicKey().Address().Bytes(), + genesistest.DefaultFundedKeys[1].Address().Bytes(), ids.Empty, math.MaxInt32) require.NoError(t, err) @@ -296,7 +297,7 @@ func TestStandardTxExecutorAddDelegator(t *testing.T) { env.state.SetHeight(dummyHeight) require.NoError(t, env.state.Commit()) }, - AP3Time: defaultGenesisTime, + AP3Time: genesistest.DefaultValidatorStartTime, expectedExecutionErr: ErrFlowCheckFailed, }, { @@ -305,9 +306,9 @@ func TestStandardTxExecutorAddDelegator(t *testing.T) { startTime: newValidatorStartTime, // same start time as for primary network endTime: newValidatorEndTime, // same end time as for primary network nodeID: newValidatorID, - feeKeys: []*secp256k1.PrivateKey{preFundedKeys[0]}, + feeKeys: []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}, setup: addMaxStakeValidator, - AP3Time: defaultValidateEndTime, + AP3Time: genesistest.DefaultValidatorEndTime, expectedExecutionErr: nil, }, { @@ -316,9 +317,9 @@ func TestStandardTxExecutorAddDelegator(t *testing.T) { startTime: newValidatorStartTime, // same start time as for primary network endTime: newValidatorEndTime, // same end time as for primary network nodeID: newValidatorID, - feeKeys: []*secp256k1.PrivateKey{preFundedKeys[0]}, + feeKeys: []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]}, setup: addMaxStakeValidator, - AP3Time: defaultGenesisTime, + AP3Time: genesistest.DefaultValidatorStartTime, expectedExecutionErr: ErrOverDelegated, }, } @@ -374,21 +375,21 @@ func TestApricotStandardTxExecutorAddSubnetValidator(t *testing.T) { env.ctx.Lock.Lock() defer env.ctx.Lock.Unlock() - nodeID := genesisNodeIDs[0] + nodeID := genesistest.DefaultNodeIDs[0] { // Case: Proposed validator currently validating primary network // but stops validating subnet after stops validating primary network // (note that keys[0] is a genesis validator) - startTime := defaultValidateStartTime.Add(time.Second) + startTime := genesistest.DefaultValidatorStartTime.Add(time.Second) builder, signer := env.factory.NewWallet(testSubnet1ControlKeys[0], testSubnet1ControlKeys[1]) utx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ NodeID: nodeID, Start: uint64(startTime.Unix()), - End: uint64(defaultValidateEndTime.Unix()) + 1, - Wght: defaultWeight, + End: genesistest.DefaultValidatorEndTimeUnix + 1, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -421,9 +422,9 @@ func TestApricotStandardTxExecutorAddSubnetValidator(t *testing.T) { &txs.SubnetValidator{ Validator: txs.Validator{ NodeID: nodeID, - Start: uint64(defaultValidateStartTime.Unix() + 1), - End: uint64(defaultValidateEndTime.Unix()), - Wght: defaultWeight, + Start: genesistest.DefaultValidatorStartTimeUnix + 1, + End: genesistest.DefaultValidatorEndTimeUnix, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -448,10 +449,10 @@ func TestApricotStandardTxExecutorAddSubnetValidator(t *testing.T) { // Add a validator to pending validator set of primary network // Starts validating primary network 10 seconds after genesis pendingDSValidatorID := ids.GenerateTestNodeID() - dsStartTime := defaultGenesisTime.Add(10 * time.Second) + dsStartTime := genesistest.DefaultValidatorStartTime.Add(10 * time.Second) dsEndTime := dsStartTime.Add(5 * defaultMinStakingDuration) - builder, signer := env.factory.NewWallet(preFundedKeys[0]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: pendingDSValidatorID, @@ -478,7 +479,7 @@ func TestApricotStandardTxExecutorAddSubnetValidator(t *testing.T) { NodeID: pendingDSValidatorID, Start: uint64(dsStartTime.Unix()), // start validating subnet before primary network End: uint64(dsEndTime.Unix()), - Wght: defaultWeight, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -516,7 +517,7 @@ func TestApricotStandardTxExecutorAddSubnetValidator(t *testing.T) { env.state.SetHeight(dummyHeight) require.NoError(env.state.Commit()) - // Node with ID key.PublicKey().Address() now a pending validator for primary network + // Node with ID key.Address() now a pending validator for primary network { // Case: Proposed validator is pending validator of primary network @@ -528,7 +529,7 @@ func TestApricotStandardTxExecutorAddSubnetValidator(t *testing.T) { NodeID: pendingDSValidatorID, Start: uint64(dsStartTime.Unix()) - 1, // start validating subnet before primary network End: uint64(dsEndTime.Unix()), - Wght: defaultWeight, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -561,7 +562,7 @@ func TestApricotStandardTxExecutorAddSubnetValidator(t *testing.T) { NodeID: pendingDSValidatorID, Start: uint64(dsStartTime.Unix()), End: uint64(dsEndTime.Unix()) + 1, // stop validating subnet after stopping validating primary network - Wght: defaultWeight, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -594,7 +595,7 @@ func TestApricotStandardTxExecutorAddSubnetValidator(t *testing.T) { NodeID: pendingDSValidatorID, Start: uint64(dsStartTime.Unix()), // same start time as for primary network End: uint64(dsEndTime.Unix()), // same end time as for primary network - Wght: defaultWeight, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -618,7 +619,7 @@ func TestApricotStandardTxExecutorAddSubnetValidator(t *testing.T) { // Case: Proposed validator start validating at/before current timestamp // First, advance the timestamp - newTimestamp := defaultGenesisTime.Add(2 * time.Second) + newTimestamp := genesistest.DefaultValidatorStartTime.Add(2 * time.Second) env.state.SetTimestamp(newTimestamp) { @@ -629,7 +630,7 @@ func TestApricotStandardTxExecutorAddSubnetValidator(t *testing.T) { NodeID: nodeID, Start: uint64(newTimestamp.Unix()), End: uint64(newTimestamp.Add(defaultMinStakingDuration).Unix()), - Wght: defaultWeight, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -653,7 +654,7 @@ func TestApricotStandardTxExecutorAddSubnetValidator(t *testing.T) { } // reset the timestamp - env.state.SetTimestamp(defaultGenesisTime) + env.state.SetTimestamp(genesistest.DefaultValidatorStartTime) // Case: Proposed validator already validating the subnet // First, add validator as validator of subnet @@ -662,9 +663,9 @@ func TestApricotStandardTxExecutorAddSubnetValidator(t *testing.T) { &txs.SubnetValidator{ Validator: txs.Validator{ NodeID: nodeID, - Start: uint64(defaultValidateStartTime.Unix()), - End: uint64(defaultValidateEndTime.Unix()), - Wght: defaultWeight, + Start: genesistest.DefaultValidatorStartTimeUnix, + End: genesistest.DefaultValidatorEndTimeUnix, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -677,7 +678,7 @@ func TestApricotStandardTxExecutorAddSubnetValidator(t *testing.T) { staker, err = state.NewCurrentStaker( subnetTx.ID(), addSubnetValTx, - defaultValidateStartTime, + genesistest.DefaultValidatorStartTime, 0, ) require.NoError(err) @@ -688,16 +689,16 @@ func TestApricotStandardTxExecutorAddSubnetValidator(t *testing.T) { require.NoError(env.state.Commit()) { - // Node with ID nodeIDKey.PublicKey().Address() now validating subnet with ID testSubnet1.ID - startTime := defaultValidateStartTime.Add(time.Second) + // Node with ID nodeIDKey.Address() now validating subnet with ID testSubnet1.ID + startTime := genesistest.DefaultValidatorStartTime.Add(time.Second) builder, signer := env.factory.NewWallet(testSubnet1ControlKeys[0], testSubnet1ControlKeys[1]) utx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ NodeID: nodeID, Start: uint64(startTime.Unix()), - End: uint64(defaultValidateEndTime.Unix()), - Wght: defaultWeight, + End: genesistest.DefaultValidatorEndTimeUnix, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -726,7 +727,7 @@ func TestApricotStandardTxExecutorAddSubnetValidator(t *testing.T) { { // Case: Duplicate signatures - startTime := defaultValidateStartTime.Add(time.Second) + startTime := genesistest.DefaultValidatorStartTime.Add(time.Second) builder, signer := env.factory.NewWallet(testSubnet1ControlKeys[0], testSubnet1ControlKeys[1], testSubnet1ControlKeys[2]) utx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ @@ -734,7 +735,7 @@ func TestApricotStandardTxExecutorAddSubnetValidator(t *testing.T) { NodeID: nodeID, Start: uint64(startTime.Unix()), End: uint64(startTime.Add(defaultMinStakingDuration).Unix()) + 1, - Wght: defaultWeight, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -766,7 +767,7 @@ func TestApricotStandardTxExecutorAddSubnetValidator(t *testing.T) { { // Case: Too few signatures - startTime := defaultValidateStartTime.Add(time.Second) + startTime := genesistest.DefaultValidatorStartTime.Add(time.Second) builder, signer := env.factory.NewWallet(testSubnet1ControlKeys[0], testSubnet1ControlKeys[2]) utx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ @@ -774,7 +775,7 @@ func TestApricotStandardTxExecutorAddSubnetValidator(t *testing.T) { NodeID: nodeID, Start: uint64(startTime.Unix()), End: uint64(startTime.Add(defaultMinStakingDuration).Unix()), - Wght: defaultWeight, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -806,15 +807,15 @@ func TestApricotStandardTxExecutorAddSubnetValidator(t *testing.T) { { // Case: Control Signature from invalid key (keys[3] is not a control key) - startTime := defaultValidateStartTime.Add(time.Second) - builder, signer := env.factory.NewWallet(testSubnet1ControlKeys[0], preFundedKeys[1]) + startTime := genesistest.DefaultValidatorStartTime.Add(time.Second) + builder, signer := env.factory.NewWallet(testSubnet1ControlKeys[0], genesistest.DefaultFundedKeys[1]) utx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ NodeID: nodeID, Start: uint64(startTime.Unix()), End: uint64(startTime.Add(defaultMinStakingDuration).Unix()), - Wght: defaultWeight, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -824,7 +825,7 @@ func TestApricotStandardTxExecutorAddSubnetValidator(t *testing.T) { require.NoError(err) // Replace a valid signature with one from keys[3] - sig, err := preFundedKeys[3].SignHash(hashing.ComputeHash256(tx.Unsigned.Bytes())) + sig, err := genesistest.DefaultFundedKeys[3].SignHash(hashing.ComputeHash256(tx.Unsigned.Bytes())) require.NoError(err) copy(tx.Creds[0].(*secp256k1fx.Credential).Sigs[0][:], sig) @@ -845,7 +846,7 @@ func TestApricotStandardTxExecutorAddSubnetValidator(t *testing.T) { { // Case: Proposed validator in pending validator set for subnet // First, add validator to pending validator set of subnet - startTime := defaultValidateStartTime.Add(time.Second) + startTime := genesistest.DefaultValidatorStartTime.Add(time.Second) builder, signer := env.factory.NewWallet(testSubnet1ControlKeys[0], testSubnet1ControlKeys[1]) utx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ @@ -853,7 +854,7 @@ func TestApricotStandardTxExecutorAddSubnetValidator(t *testing.T) { NodeID: nodeID, Start: uint64(startTime.Unix()) + 1, End: uint64(startTime.Add(defaultMinStakingDuration).Unix()) + 1, - Wght: defaultWeight, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -866,7 +867,7 @@ func TestApricotStandardTxExecutorAddSubnetValidator(t *testing.T) { staker, err = state.NewCurrentStaker( subnetTx.ID(), addSubnetValTx, - defaultValidateStartTime, + genesistest.DefaultValidatorStartTime, 0, ) require.NoError(err) @@ -897,16 +898,16 @@ func TestEtnaStandardTxExecutorAddSubnetValidator(t *testing.T) { env.ctx.Lock.Lock() defer env.ctx.Lock.Unlock() - nodeID := genesisNodeIDs[0] + nodeID := genesistest.DefaultNodeIDs[0] builder, signer := env.factory.NewWallet(testSubnet1ControlKeys[0], testSubnet1ControlKeys[1]) utx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ NodeID: nodeID, - Start: uint64(defaultValidateStartTime.Unix() + 1), - End: uint64(defaultValidateEndTime.Unix()), - Wght: defaultWeight, + Start: genesistest.DefaultValidatorStartTimeUnix + 1, + End: genesistest.DefaultValidatorEndTimeUnix, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -939,12 +940,12 @@ func TestBanffStandardTxExecutorAddValidator(t *testing.T) { { // Case: Validator's start time too early - builder, signer := env.factory.NewWallet(preFundedKeys[0]) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: nodeID, - Start: uint64(defaultValidateStartTime.Unix()) - 1, - End: uint64(defaultValidateEndTime.Unix()), + Start: genesistest.DefaultValidatorStartTimeUnix - 1, + End: genesistest.DefaultValidatorEndTimeUnix, Wght: env.config.MinValidatorStake, }, &secp256k1fx.OutputOwners{ @@ -973,8 +974,8 @@ func TestBanffStandardTxExecutorAddValidator(t *testing.T) { { // Case: Validator in current validator set of primary network - startTime := defaultValidateStartTime.Add(1 * time.Second) - builder, signer := env.factory.NewWallet(preFundedKeys[0]) + startTime := genesistest.DefaultValidatorStartTime.Add(1 * time.Second) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: nodeID, @@ -1020,8 +1021,8 @@ func TestBanffStandardTxExecutorAddValidator(t *testing.T) { { // Case: Validator in pending validator set of primary network - startTime := defaultValidateStartTime.Add(1 * time.Second) - builder, signer := env.factory.NewWallet(preFundedKeys[0]) + startTime := genesistest.DefaultValidatorStartTime.Add(1 * time.Second) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: nodeID, @@ -1064,8 +1065,8 @@ func TestBanffStandardTxExecutorAddValidator(t *testing.T) { { // Case: Validator doesn't have enough tokens to cover stake amount - startTime := defaultValidateStartTime.Add(1 * time.Second) - builder, signer := env.factory.NewWallet(preFundedKeys[0]) + startTime := genesistest.DefaultValidatorStartTime.Add(1 * time.Second) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: nodeID, @@ -1084,7 +1085,7 @@ func TestBanffStandardTxExecutorAddValidator(t *testing.T) { require.NoError(err) // Remove all UTXOs owned by preFundedKeys[0] - utxoIDs, err := env.state.UTXOIDs(preFundedKeys[0].PublicKey().Address().Bytes(), ids.Empty, math.MaxInt32) + utxoIDs, err := env.state.UTXOIDs(genesistest.DefaultFundedKeys[0].Address().Bytes(), ids.Empty, math.MaxInt32) require.NoError(err) onAcceptState, err := state.NewDiff(lastAcceptedID, env) @@ -1124,7 +1125,7 @@ func TestDurangoDisabledTransactions(t *testing.T) { endTime = chainTime.Add(defaultMaxStakingDuration) ) - builder, signer := env.factory.NewWallet(preFundedKeys...) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys...) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: nodeID, @@ -1162,7 +1163,7 @@ func TestDurangoDisabledTransactions(t *testing.T) { } it.Release() - builder, signer := env.factory.NewWallet(preFundedKeys...) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys...) utx, err := builder.NewAddDelegatorTx( &txs.Validator{ NodeID: primaryValidator.NodeID, @@ -1234,7 +1235,7 @@ func TestDurangoMemoField(t *testing.T) { } it.Release() - builder, signer := env.factory.NewWallet(preFundedKeys...) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys...) utx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -1259,7 +1260,7 @@ func TestDurangoMemoField(t *testing.T) { { name: "CreateChainTx", setupTest: func(env *environment, memoField []byte) (*txs.Tx, state.Diff) { - builder, signer := env.factory.NewWallet(preFundedKeys...) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys...) utx, err := builder.NewCreateChainTx( testSubnet1.TxID, []byte{}, @@ -1281,7 +1282,7 @@ func TestDurangoMemoField(t *testing.T) { { name: "CreateSubnetTx", setupTest: func(env *environment, memoField []byte) (*txs.Tx, state.Diff) { - builder, signer := env.factory.NewWallet(preFundedKeys...) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys...) utx, err := builder.NewCreateSubnetTx( &secp256k1fx.OutputOwners{ Threshold: 1, @@ -1307,7 +1308,7 @@ func TestDurangoMemoField(t *testing.T) { var ( sourceChain = env.ctx.XChainID - sourceKey = preFundedKeys[1] + sourceKey = genesistest.DefaultFundedKeys[1] sourceAmount = 10 * units.Avax ) @@ -1323,13 +1324,13 @@ func TestDurangoMemoField(t *testing.T) { ) env.msm.SharedMemory = sharedMemory - builder, signer := env.factory.NewWallet(preFundedKeys...) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys...) utx, err := builder.NewImportTx( sourceChain, &secp256k1fx.OutputOwners{ Locktime: 0, Threshold: 1, - Addrs: []ids.ShortID{sourceKey.PublicKey().Address()}, + Addrs: []ids.ShortID{sourceKey.Address()}, }, common.WithMemo(memoField), ) @@ -1346,7 +1347,7 @@ func TestDurangoMemoField(t *testing.T) { { name: "ExportTx", setupTest: func(env *environment, memoField []byte) (*txs.Tx, state.Diff) { - builder, signer := env.factory.NewWallet(preFundedKeys...) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys...) utx, err := builder.NewExportTx( env.ctx.XChainID, []*avax.TransferableOutput{{ @@ -1396,7 +1397,7 @@ func TestDurangoMemoField(t *testing.T) { NodeID: primaryValidator.NodeID, Start: 0, End: uint64(endTime.Unix()), - Wght: defaultWeight, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -1416,7 +1417,7 @@ func TestDurangoMemoField(t *testing.T) { Tx: subnetValTx, })) - builder, signer = env.factory.NewWallet(preFundedKeys...) + builder, signer = env.factory.NewWallet(genesistest.DefaultFundedKeys...) utx2, err := builder.NewRemoveSubnetValidatorTx( primaryValidator.NodeID, testSubnet1.ID(), @@ -1432,7 +1433,7 @@ func TestDurangoMemoField(t *testing.T) { { name: "TransformSubnetTx", setupTest: func(env *environment, memoField []byte) (*txs.Tx, state.Diff) { - builder, signer := env.factory.NewWallet(preFundedKeys...) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys...) utx, err := builder.NewTransformSubnetTx( testSubnet1.TxID, // subnetID ids.GenerateTestID(), // assetID @@ -1471,7 +1472,7 @@ func TestDurangoMemoField(t *testing.T) { sk, err := bls.NewSecretKey() require.NoError(t, err) - builder, txSigner := env.factory.NewWallet(preFundedKeys...) + builder, txSigner := env.factory.NewWallet(genesistest.DefaultFundedKeys...) utx, err := builder.NewAddPermissionlessValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -1521,7 +1522,7 @@ func TestDurangoMemoField(t *testing.T) { } it.Release() - builder, signer := env.factory.NewWallet(preFundedKeys...) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys...) utx, err := builder.NewAddPermissionlessDelegatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -1552,7 +1553,7 @@ func TestDurangoMemoField(t *testing.T) { { name: "TransferSubnetOwnershipTx", setupTest: func(env *environment, memoField []byte) (*txs.Tx, state.Diff) { - builder, signer := env.factory.NewWallet(preFundedKeys...) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys...) utx, err := builder.NewTransferSubnetOwnershipTx( testSubnet1.TxID, &secp256k1fx.OutputOwners{ @@ -1574,7 +1575,7 @@ func TestDurangoMemoField(t *testing.T) { { name: "BaseTx", setupTest: func(env *environment, memoField []byte) (*txs.Tx, state.Diff) { - builder, signer := env.factory.NewWallet(preFundedKeys...) + builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys...) utx, err := builder.NewBaseTx( []*avax.TransferableOutput{ { diff --git a/vms/platformvm/validator_set_property_test.go b/vms/platformvm/validator_set_property_test.go index 3935b75416a9..44ba02e0e1a7 100644 --- a/vms/platformvm/validator_set_property_test.go +++ b/vms/platformvm/validator_set_property_test.go @@ -32,14 +32,10 @@ import ( "github.com/ava-labs/avalanchego/upgrade/upgradetest" "github.com/ava-labs/avalanchego/utils/constants" "github.com/ava-labs/avalanchego/utils/crypto/bls" - "github.com/ava-labs/avalanchego/utils/formatting" - "github.com/ava-labs/avalanchego/utils/formatting/address" - "github.com/ava-labs/avalanchego/utils/json" "github.com/ava-labs/avalanchego/utils/timer/mockable" - "github.com/ava-labs/avalanchego/utils/units" - "github.com/ava-labs/avalanchego/vms/platformvm/api" "github.com/ava-labs/avalanchego/vms/platformvm/block" "github.com/ava-labs/avalanchego/vms/platformvm/config" + "github.com/ava-labs/avalanchego/vms/platformvm/genesis/genesistest" "github.com/ava-labs/avalanchego/vms/platformvm/reward" "github.com/ava-labs/avalanchego/vms/platformvm/signer" "github.com/ava-labs/avalanchego/vms/platformvm/state" @@ -89,7 +85,7 @@ func TestGetValidatorsSetProperty(t *testing.T) { }() nodeID := ids.GenerateTestNodeID() - currentTime := defaultGenesisTime + currentTime := genesistest.DefaultValidatorStartTime vm.clock.Set(currentTime) vm.state.SetTimestamp(currentTime) @@ -260,7 +256,7 @@ func takeValidatorsSnapshotAtCurrentHeight(vm *VM, validatorsSetByHeightAndSubne func addSubnetValidator(vm *VM, data *validatorInputData, subnetID ids.ID) (*state.Staker, error) { factory := txstest.NewWalletFactory(vm.ctx, &vm.Config, vm.state) - builder, signer := factory.NewWallet(keys[0], keys[1]) + builder, signer := factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) utx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -273,7 +269,7 @@ func addSubnetValidator(vm *VM, data *validatorInputData, subnetID ids.ID) (*sta }, walletcommon.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }), ) if err != nil { @@ -287,7 +283,7 @@ func addSubnetValidator(vm *VM, data *validatorInputData, subnetID ids.ID) (*sta } func addPrimaryValidatorWithBLSKey(vm *VM, data *validatorInputData) (*state.Staker, error) { - addr := keys[0].PublicKey().Address() + addr := genesistest.DefaultFundedKeys[0].Address() sk, err := bls.NewSecretKey() if err != nil { @@ -295,7 +291,7 @@ func addPrimaryValidatorWithBLSKey(vm *VM, data *validatorInputData) (*state.Sta } factory := txstest.NewWalletFactory(vm.ctx, &vm.Config, vm.state) - builder, txSigner := factory.NewWallet(keys[0], keys[1]) + builder, txSigner := factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) utx, err := builder.NewAddPermissionlessValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -662,9 +658,9 @@ func buildVM(t *testing.T) (*VM, ids.ID, error) { MinStakeDuration: defaultMinStakingDuration, MaxStakeDuration: defaultMaxStakingDuration, RewardConfig: defaultRewardConfig, - UpgradeConfig: upgradetest.GetConfigWithUpgradeTime(upgradetest.Durango, defaultGenesisTime), + UpgradeConfig: upgradetest.GetConfigWithUpgradeTime(upgradetest.Durango, genesistest.DefaultValidatorStartTime), }} - vm.clock.Set(defaultGenesisTime.Add(time.Second)) + vm.clock.Set(genesistest.DefaultValidatorStartTime.Add(time.Second)) baseDB := memdb.New() chainDB := prefixdb.New([]byte{0}, baseDB) @@ -684,16 +680,16 @@ func buildVM(t *testing.T) (*VM, ids.ID, error) { return nil } - genesisBytes, err := buildCustomGenesis(ctx.AVAXAssetID) - if err != nil { - return nil, ids.Empty, err - } - - err = vm.Initialize( + err := vm.Initialize( context.Background(), ctx, chainDB, - genesisBytes, + genesistest.NewBytes(t, genesistest.Config{ + NodeIDs: []ids.NodeID{ + genesistest.DefaultNodeIDs[len(genesistest.DefaultNodeIDs)-1], + }, + ValidatorEndTime: mockable.MaxTime, + }), nil, nil, msgChan, @@ -713,15 +709,15 @@ func buildVM(t *testing.T) (*VM, ids.ID, error) { // Note: following Banff activation, block acceptance will move // chain time ahead factory := txstest.NewWalletFactory(vm.ctx, &vm.Config, vm.state) - builder, signer := factory.NewWallet(keys[len(keys)-1]) + builder, signer := factory.NewWallet(genesistest.DefaultFundedKeys[len(genesistest.DefaultFundedKeys)-1]) utx, err := builder.NewCreateSubnetTx( &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }, walletcommon.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }), ) if err != nil { @@ -754,71 +750,3 @@ func buildVM(t *testing.T) (*VM, ids.ID, error) { return vm, testSubnet1.ID(), nil } - -func buildCustomGenesis(avaxAssetID ids.ID) ([]byte, error) { - genesisUTXOs := make([]api.UTXO, len(keys)) - for i, key := range keys { - id := key.PublicKey().Address() - addr, err := address.FormatBech32(constants.UnitTestHRP, id.Bytes()) - if err != nil { - return nil, err - } - genesisUTXOs[i] = api.UTXO{ - Amount: json.Uint64(defaultBalance), - Address: addr, - } - } - - // we need at least a validator, otherwise BuildBlock would fail, since it - // won't find next staker to promote/evict from stakers set. Contrary to - // what happens with production code we push such validator at the end of - // times, so to avoid interference with our tests - nodeID := genesisNodeIDs[len(genesisNodeIDs)-1] - addr, err := address.FormatBech32(constants.UnitTestHRP, nodeID.Bytes()) - if err != nil { - return nil, err - } - - starTime := mockable.MaxTime.Add(-1 * defaultMinStakingDuration) - endTime := mockable.MaxTime - genesisValidator := api.GenesisPermissionlessValidator{ - GenesisValidator: api.GenesisValidator{ - StartTime: json.Uint64(starTime.Unix()), - EndTime: json.Uint64(endTime.Unix()), - NodeID: nodeID, - }, - RewardOwner: &api.Owner{ - Threshold: 1, - Addresses: []string{addr}, - }, - Staked: []api.UTXO{{ - Amount: json.Uint64(defaultWeight), - Address: addr, - }}, - DelegationFee: reward.PercentDenominator, - } - - buildGenesisArgs := api.BuildGenesisArgs{ - Encoding: formatting.Hex, - NetworkID: json.Uint32(constants.UnitTestID), - AvaxAssetID: avaxAssetID, - UTXOs: genesisUTXOs, - Validators: []api.GenesisPermissionlessValidator{genesisValidator}, - Chains: nil, - Time: json.Uint64(defaultGenesisTime.Unix()), - InitialSupply: json.Uint64(360 * units.MegaAvax), - } - - buildGenesisResponse := api.BuildGenesisReply{} - platformvmSS := api.StaticService{} - if err := platformvmSS.BuildGenesis(nil, &buildGenesisArgs, &buildGenesisResponse); err != nil { - return nil, err - } - - genesisBytes, err := formatting.Decode(buildGenesisResponse.Encoding, buildGenesisResponse.Bytes) - if err != nil { - return nil, err - } - - return genesisBytes, nil -} diff --git a/vms/platformvm/validators/manager_benchmark_test.go b/vms/platformvm/validators/manager_benchmark_test.go index 905bb8fc2a1d..98227e09913d 100644 --- a/vms/platformvm/validators/manager_benchmark_test.go +++ b/vms/platformvm/validators/manager_benchmark_test.go @@ -17,17 +17,13 @@ import ( "github.com/ava-labs/avalanchego/snow/validators" "github.com/ava-labs/avalanchego/utils/constants" "github.com/ava-labs/avalanchego/utils/crypto/bls" - "github.com/ava-labs/avalanchego/utils/formatting" - "github.com/ava-labs/avalanchego/utils/formatting/address" - "github.com/ava-labs/avalanchego/utils/json" "github.com/ava-labs/avalanchego/utils/logging" "github.com/ava-labs/avalanchego/utils/timer/mockable" "github.com/ava-labs/avalanchego/utils/units" - "github.com/ava-labs/avalanchego/vms/platformvm/api" "github.com/ava-labs/avalanchego/vms/platformvm/block" "github.com/ava-labs/avalanchego/vms/platformvm/config" + "github.com/ava-labs/avalanchego/vms/platformvm/genesis/genesistest" "github.com/ava-labs/avalanchego/vms/platformvm/metrics" - "github.com/ava-labs/avalanchego/vms/platformvm/reward" "github.com/ava-labs/avalanchego/vms/platformvm/state" "github.com/ava-labs/avalanchego/vms/platformvm/state/statetest" "github.com/ava-labs/avalanchego/vms/platformvm/txs" @@ -56,52 +52,9 @@ func BenchmarkGetValidatorSet(b *testing.B) { require.NoError(db.Close()) }() - avaxAssetID := ids.GenerateTestID() - genesisTime := time.Now().Truncate(time.Second) - genesisEndTime := genesisTime.Add(28 * 24 * time.Hour) - - addr, err := address.FormatBech32(constants.UnitTestHRP, ids.GenerateTestShortID().Bytes()) - require.NoError(err) - - genesisValidators := []api.GenesisPermissionlessValidator{{ - GenesisValidator: api.GenesisValidator{ - StartTime: json.Uint64(genesisTime.Unix()), - EndTime: json.Uint64(genesisEndTime.Unix()), - NodeID: ids.GenerateTestNodeID(), - }, - RewardOwner: &api.Owner{ - Threshold: 1, - Addresses: []string{addr}, - }, - Staked: []api.UTXO{{ - Amount: json.Uint64(2 * units.KiloAvax), - Address: addr, - }}, - DelegationFee: reward.PercentDenominator, - }} - - buildGenesisArgs := api.BuildGenesisArgs{ - NetworkID: json.Uint32(constants.UnitTestID), - AvaxAssetID: avaxAssetID, - UTXOs: nil, - Validators: genesisValidators, - Chains: nil, - Time: json.Uint64(genesisTime.Unix()), - InitialSupply: json.Uint64(360 * units.MegaAvax), - Encoding: formatting.Hex, - } - - buildGenesisResponse := api.BuildGenesisReply{} - platformvmSS := api.StaticService{} - require.NoError(platformvmSS.BuildGenesis(nil, &buildGenesisArgs, &buildGenesisResponse)) - - genesisBytes, err := formatting.Decode(buildGenesisResponse.Encoding, buildGenesisResponse.Bytes) - require.NoError(err) - vdrs := validators.NewManager() s := statetest.New(b, statetest.Config{ DB: db, - Genesis: genesisBytes, Validators: vdrs, }) @@ -121,18 +74,18 @@ func BenchmarkGetValidatorSet(b *testing.B) { ) for i := 0; i < 50; i++ { currentHeight++ - nodeID, err := addPrimaryValidator(s, genesisTime, genesisEndTime, currentHeight) + nodeID, err := addPrimaryValidator(s, genesistest.DefaultValidatorStartTime, genesistest.DefaultValidatorEndTime, currentHeight) require.NoError(err) nodeIDs = append(nodeIDs, nodeID) } subnetID := ids.GenerateTestID() for _, nodeID := range nodeIDs { currentHeight++ - require.NoError(addSubnetValidator(s, subnetID, genesisTime, genesisEndTime, nodeID, currentHeight)) + require.NoError(addSubnetValidator(s, subnetID, genesistest.DefaultValidatorStartTime, genesistest.DefaultValidatorEndTime, nodeID, currentHeight)) } for i := 0; i < 9900; i++ { currentHeight++ - require.NoError(addSubnetDelegator(s, subnetID, genesisTime, genesisEndTime, nodeIDs, currentHeight)) + require.NoError(addSubnetDelegator(s, subnetID, genesistest.DefaultValidatorStartTime, genesistest.DefaultValidatorEndTime, nodeIDs, currentHeight)) } ctx := context.Background() diff --git a/vms/platformvm/vm_regression_test.go b/vms/platformvm/vm_regression_test.go index 851117920bcf..9b4d8b19a5a6 100644 --- a/vms/platformvm/vm_regression_test.go +++ b/vms/platformvm/vm_regression_test.go @@ -35,6 +35,7 @@ import ( "github.com/ava-labs/avalanchego/vms/components/avax" "github.com/ava-labs/avalanchego/vms/platformvm/block" "github.com/ava-labs/avalanchego/vms/platformvm/config" + "github.com/ava-labs/avalanchego/vms/platformvm/genesis/genesistest" "github.com/ava-labs/avalanchego/vms/platformvm/reward" "github.com/ava-labs/avalanchego/vms/platformvm/signer" "github.com/ava-labs/avalanchego/vms/platformvm/state/statetest" @@ -58,10 +59,10 @@ func TestAddDelegatorTxOverDelegatedRegression(t *testing.T) { validatorEndTime := validatorStartTime.Add(360 * 24 * time.Hour) nodeID := ids.GenerateTestNodeID() - changeAddr := keys[0].PublicKey().Address() + changeAddr := genesistest.DefaultFundedKeys[0].Address() // create valid tx - builder, txSigner := factory.NewWallet(keys[0]) + builder, txSigner := factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: nodeID, @@ -106,7 +107,7 @@ func TestAddDelegatorTxOverDelegatedRegression(t *testing.T) { firstDelegatorEndTime := firstDelegatorStartTime.Add(vm.MinStakeDuration) // create valid tx - builder, txSigner = factory.NewWallet(keys[0], keys[1]) + builder, txSigner = factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) uDelTx1, err := builder.NewAddDelegatorTx( &txs.Validator{ NodeID: nodeID, @@ -152,7 +153,7 @@ func TestAddDelegatorTxOverDelegatedRegression(t *testing.T) { vm.clock.Set(secondDelegatorStartTime.Add(-10 * executor.SyncBound)) // create valid tx - builder, txSigner = factory.NewWallet(keys[0], keys[1], keys[3]) + builder, txSigner = factory.NewWallet(genesistest.DefaultFundedKeys[0], genesistest.DefaultFundedKeys[1], genesistest.DefaultFundedKeys[3]) uDelTx2, err := builder.NewAddDelegatorTx( &txs.Validator{ NodeID: nodeID, @@ -188,7 +189,7 @@ func TestAddDelegatorTxOverDelegatedRegression(t *testing.T) { thirdDelegatorEndTime := thirdDelegatorStartTime.Add(vm.MinStakeDuration) // create valid tx - builder, txSigner = factory.NewWallet(keys[0], keys[1], keys[4]) + builder, txSigner = factory.NewWallet(genesistest.DefaultFundedKeys[0], genesistest.DefaultFundedKeys[1], genesistest.DefaultFundedKeys[4]) uDelTx3, err := builder.NewAddDelegatorTx( &txs.Validator{ NodeID: nodeID, @@ -247,7 +248,7 @@ func TestAddDelegatorTxHeapCorruption(t *testing.T) { }, { name: "post-upgrade calculate max stake correctly", - ap3Time: defaultGenesisTime, + ap3Time: genesistest.DefaultValidatorStartTime, }, } @@ -264,12 +265,12 @@ func TestAddDelegatorTxHeapCorruption(t *testing.T) { key, err := secp256k1.NewPrivateKey() require.NoError(err) - id := key.PublicKey().Address() + id := key.Address() nodeID := ids.GenerateTestNodeID() - changeAddr := keys[0].PublicKey().Address() + changeAddr := genesistest.DefaultFundedKeys[0].Address() // create valid tx - builder, txSigner := factory.NewWallet(keys[0], keys[1]) + builder, txSigner := factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: nodeID, @@ -313,7 +314,7 @@ func TestAddDelegatorTxHeapCorruption(t *testing.T) { }, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }, walletcommon.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, @@ -346,7 +347,7 @@ func TestAddDelegatorTxHeapCorruption(t *testing.T) { }, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }, walletcommon.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, @@ -379,7 +380,7 @@ func TestAddDelegatorTxHeapCorruption(t *testing.T) { }, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }, walletcommon.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, @@ -412,7 +413,7 @@ func TestAddDelegatorTxHeapCorruption(t *testing.T) { }, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }, walletcommon.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, @@ -463,14 +464,12 @@ func TestUnverifiedParentPanicRegression(t *testing.T) { ctx.Lock.Unlock() }() - _, genesisBytes := defaultGenesis(t, ctx.AVAXAssetID) - msgChan := make(chan common.Message, 1) require.NoError(vm.Initialize( context.Background(), ctx, baseDB, - genesisBytes, + genesistest.NewBytes(t, genesistest.Config{}), nil, nil, msgChan, @@ -485,10 +484,10 @@ func TestUnverifiedParentPanicRegression(t *testing.T) { vm.clock.Set(latestForkTime.Add(time.Second)) vm.state.SetTimestamp(latestForkTime.Add(time.Second)) - key0 := keys[0] - key1 := keys[1] - addr0 := key0.PublicKey().Address() - addr1 := key1.PublicKey().Address() + key0 := genesistest.DefaultFundedKeys[0] + key1 := genesistest.DefaultFundedKeys[1] + addr0 := key0.Address() + addr1 := key1.Address() factory := txstest.NewWalletFactory( vm.ctx, @@ -601,7 +600,7 @@ func TestRejectedStateRegressionInvalidValidatorTimestamp(t *testing.T) { newValidatorEndTime := newValidatorStartTime.Add(defaultMinStakingDuration) // Create the tx to add a new validator - builder, txSigner := factory.NewWallet(keys[0]) + builder, txSigner := factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: nodeID, @@ -798,7 +797,7 @@ func TestRejectedStateRegressionInvalidValidatorReward(t *testing.T) { nodeID0 := ids.GenerateTestNodeID() // Create the tx to add the first new validator - builder, txSigner := factory.NewWallet(keys[0]) + builder, txSigner := factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: nodeID0, @@ -967,7 +966,7 @@ func TestRejectedStateRegressionInvalidValidatorReward(t *testing.T) { nodeID1 := ids.GenerateTestNodeID() // Create the tx to add the second new validator - builder, txSigner = factory.NewWallet(keys[1]) + builder, txSigner = factory.NewWallet(genesistest.DefaultFundedKeys[1]) utx1, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: nodeID1, @@ -1102,11 +1101,11 @@ func TestValidatorSetAtCacheOverwriteRegression(t *testing.T) { require.Equal(uint64(1), currentHeight) expectedValidators1 := map[ids.NodeID]uint64{ - genesisNodeIDs[0]: defaultWeight, - genesisNodeIDs[1]: defaultWeight, - genesisNodeIDs[2]: defaultWeight, - genesisNodeIDs[3]: defaultWeight, - genesisNodeIDs[4]: defaultWeight, + genesistest.DefaultNodeIDs[0]: genesistest.DefaultValidatorWeight, + genesistest.DefaultNodeIDs[1]: genesistest.DefaultValidatorWeight, + genesistest.DefaultNodeIDs[2]: genesistest.DefaultValidatorWeight, + genesistest.DefaultNodeIDs[3]: genesistest.DefaultValidatorWeight, + genesistest.DefaultNodeIDs[4]: genesistest.DefaultValidatorWeight, } validators, err := vm.GetValidatorSet(context.Background(), 1, constants.PrimaryNetworkID) require.NoError(err) @@ -1120,7 +1119,7 @@ func TestValidatorSetAtCacheOverwriteRegression(t *testing.T) { extraNodeID := ids.GenerateTestNodeID() // Create the tx to add the first new validator - builder, txSigner := factory.NewWallet(keys[0]) + builder, txSigner := factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: extraNodeID, @@ -1206,12 +1205,12 @@ func TestValidatorSetAtCacheOverwriteRegression(t *testing.T) { } expectedValidators2 := map[ids.NodeID]uint64{ - genesisNodeIDs[0]: defaultWeight, - genesisNodeIDs[1]: defaultWeight, - genesisNodeIDs[2]: defaultWeight, - genesisNodeIDs[3]: defaultWeight, - genesisNodeIDs[4]: defaultWeight, - extraNodeID: vm.MaxValidatorStake, + genesistest.DefaultNodeIDs[0]: genesistest.DefaultValidatorWeight, + genesistest.DefaultNodeIDs[1]: genesistest.DefaultValidatorWeight, + genesistest.DefaultNodeIDs[2]: genesistest.DefaultValidatorWeight, + genesistest.DefaultNodeIDs[3]: genesistest.DefaultValidatorWeight, + genesistest.DefaultNodeIDs[4]: genesistest.DefaultValidatorWeight, + extraNodeID: vm.MaxValidatorStake, } validators, err = vm.GetValidatorSet(context.Background(), 3, constants.PrimaryNetworkID) require.NoError(err) @@ -1244,10 +1243,10 @@ func TestAddDelegatorTxAddBeforeRemove(t *testing.T) { id := key.Address() nodeID := ids.GenerateTestNodeID() - changeAddr := keys[0].PublicKey().Address() + changeAddr := genesistest.DefaultFundedKeys[0].Address() // create valid tx - builder, txSigner := factory.NewWallet(keys[0], keys[1]) + builder, txSigner := factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: nodeID, @@ -1291,7 +1290,7 @@ func TestAddDelegatorTxAddBeforeRemove(t *testing.T) { }, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }, walletcommon.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, @@ -1324,7 +1323,7 @@ func TestAddDelegatorTxAddBeforeRemove(t *testing.T) { }, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }, walletcommon.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, @@ -1358,9 +1357,9 @@ func TestRemovePermissionedValidatorDuringPendingToCurrentTransitionNotTracked(t id := key.Address() nodeID := ids.GenerateTestNodeID() - changeAddr := keys[0].PublicKey().Address() + changeAddr := genesistest.DefaultFundedKeys[0].Address() - builder, txSigner := factory.NewWallet(keys[0], keys[1]) + builder, txSigner := factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: nodeID, @@ -1505,11 +1504,11 @@ func TestRemovePermissionedValidatorDuringPendingToCurrentTransitionTracked(t *t key, err := secp256k1.NewPrivateKey() require.NoError(err) - id := key.PublicKey().Address() + id := key.Address() nodeID := ids.GenerateTestNodeID() - changeAddr := keys[0].PublicKey().Address() + changeAddr := genesistest.DefaultFundedKeys[0].Address() - builder, txSigner := factory.NewWallet(keys[0], keys[1]) + builder, txSigner := factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: nodeID, @@ -1637,7 +1636,7 @@ func TestSubnetValidatorBLSKeyDiffAfterExpiry(t *testing.T) { subnetID := testSubnet1.TxID // setup time - currentTime := defaultGenesisTime + currentTime := genesistest.DefaultValidatorStartTime vm.clock.Set(currentTime) vm.state.SetTimestamp(currentTime) @@ -1654,13 +1653,13 @@ func TestSubnetValidatorBLSKeyDiffAfterExpiry(t *testing.T) { // insert primary network validator var ( nodeID = ids.GenerateTestNodeID() - addr = keys[0].PublicKey().Address() + addr = genesistest.DefaultFundedKeys[0].Address() ) sk1, err := bls.NewSecretKey() require.NoError(err) // build primary network validator with BLS key - builder, txSigner := factory.NewWallet(keys...) + builder, txSigner := factory.NewWallet(genesistest.DefaultFundedKeys...) uPrimaryTx, err := builder.NewAddPermissionlessValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -1709,7 +1708,7 @@ func TestSubnetValidatorBLSKeyDiffAfterExpiry(t *testing.T) { require.NoError(err) // insert the subnet validator - builder, txSigner = factory.NewWallet(keys[0], keys[1]) + builder, txSigner = factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) uAddSubnetValTx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -1790,7 +1789,7 @@ func TestSubnetValidatorBLSKeyDiffAfterExpiry(t *testing.T) { require.NoError(err) require.NotEqual(sk1, sk2) - builder, txSigner = factory.NewWallet(keys...) + builder, txSigner = factory.NewWallet(genesistest.DefaultFundedKeys...) uPrimaryRestartTx, err := builder.NewAddPermissionlessValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -1900,7 +1899,7 @@ func TestPrimaryNetworkValidatorPopulatedToEmptyBLSKeyDiff(t *testing.T) { defer vm.ctx.Lock.Unlock() // setup time - currentTime := defaultGenesisTime + currentTime := genesistest.DefaultValidatorStartTime vm.clock.Set(currentTime) vm.state.SetTimestamp(currentTime) @@ -1914,9 +1913,9 @@ func TestPrimaryNetworkValidatorPopulatedToEmptyBLSKeyDiff(t *testing.T) { // Add a primary network validator with no BLS key nodeID := ids.GenerateTestNodeID() - addr := keys[0].PublicKey().Address() + addr := genesistest.DefaultFundedKeys[0].Address() - builder, txSigner := factory.NewWallet(keys[0]) + builder, txSigner := factory.NewWallet(genesistest.DefaultFundedKeys[0]) uAddValTx1, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: nodeID, @@ -1986,7 +1985,7 @@ func TestPrimaryNetworkValidatorPopulatedToEmptyBLSKeyDiff(t *testing.T) { sk2, err := bls.NewSecretKey() require.NoError(err) - builder, txSigner = factory.NewWallet(keys...) + builder, txSigner = factory.NewWallet(genesistest.DefaultFundedKeys...) uPrimaryRestartTx, err := builder.NewAddPermissionlessValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -2058,7 +2057,7 @@ func TestSubnetValidatorPopulatedToEmptyBLSKeyDiff(t *testing.T) { subnetID := testSubnet1.TxID // setup time - currentTime := defaultGenesisTime + currentTime := genesistest.DefaultValidatorStartTime vm.clock.Set(currentTime) vm.state.SetTimestamp(currentTime) @@ -2074,9 +2073,9 @@ func TestSubnetValidatorPopulatedToEmptyBLSKeyDiff(t *testing.T) { // Add a primary network validator with no BLS key nodeID := ids.GenerateTestNodeID() - addr := keys[0].PublicKey().Address() + addr := genesistest.DefaultFundedKeys[0].Address() - builder, txSigner := factory.NewWallet(keys[0]) + builder, txSigner := factory.NewWallet(genesistest.DefaultFundedKeys[0]) uPrimaryTx1, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: nodeID, @@ -2116,7 +2115,7 @@ func TestSubnetValidatorPopulatedToEmptyBLSKeyDiff(t *testing.T) { require.NoError(err) // insert the subnet validator - builder, txSigner = factory.NewWallet(keys[0], keys[1]) + builder, txSigner = factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) uAddSubnetValTx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -2196,7 +2195,7 @@ func TestSubnetValidatorPopulatedToEmptyBLSKeyDiff(t *testing.T) { sk2, err := bls.NewSecretKey() require.NoError(err) - builder, txSigner = factory.NewWallet(keys...) + builder, txSigner = factory.NewWallet(genesistest.DefaultFundedKeys...) uPrimaryRestartTx, err := builder.NewAddPermissionlessValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -2277,7 +2276,7 @@ func TestSubnetValidatorSetAfterPrimaryNetworkValidatorRemoval(t *testing.T) { subnetID := testSubnet1.TxID // setup time - currentTime := defaultGenesisTime + currentTime := genesistest.DefaultValidatorStartTime vm.clock.Set(currentTime) vm.state.SetTimestamp(currentTime) @@ -2291,9 +2290,9 @@ func TestSubnetValidatorSetAfterPrimaryNetworkValidatorRemoval(t *testing.T) { // Add a primary network validator with no BLS key nodeID := ids.GenerateTestNodeID() - addr := keys[0].PublicKey().Address() + addr := genesistest.DefaultFundedKeys[0].Address() - builder, txSigner := factory.NewWallet(keys[0]) + builder, txSigner := factory.NewWallet(genesistest.DefaultFundedKeys[0]) uPrimaryTx1, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: nodeID, @@ -2330,7 +2329,7 @@ func TestSubnetValidatorSetAfterPrimaryNetworkValidatorRemoval(t *testing.T) { require.NoError(err) // insert the subnet validator - builder, txSigner = factory.NewWallet(keys[0], keys[1]) + builder, txSigner = factory.NewWallet(genesistest.DefaultFundedKeys[:2]...) uAddSubnetValTx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ diff --git a/vms/platformvm/vm_test.go b/vms/platformvm/vm_test.go index 89aac04ce947..e6cfada5e7f7 100644 --- a/vms/platformvm/vm_test.go +++ b/vms/platformvm/vm_test.go @@ -40,9 +40,6 @@ import ( "github.com/ava-labs/avalanchego/utils/constants" "github.com/ava-labs/avalanchego/utils/crypto/bls" "github.com/ava-labs/avalanchego/utils/crypto/secp256k1" - "github.com/ava-labs/avalanchego/utils/formatting" - "github.com/ava-labs/avalanchego/utils/formatting/address" - "github.com/ava-labs/avalanchego/utils/json" "github.com/ava-labs/avalanchego/utils/logging" "github.com/ava-labs/avalanchego/utils/math/meter" "github.com/ava-labs/avalanchego/utils/resource" @@ -52,9 +49,9 @@ import ( "github.com/ava-labs/avalanchego/version" "github.com/ava-labs/avalanchego/vms/components/avax" "github.com/ava-labs/avalanchego/vms/components/gas" - "github.com/ava-labs/avalanchego/vms/platformvm/api" "github.com/ava-labs/avalanchego/vms/platformvm/block" "github.com/ava-labs/avalanchego/vms/platformvm/config" + "github.com/ava-labs/avalanchego/vms/platformvm/genesis/genesistest" "github.com/ava-labs/avalanchego/vms/platformvm/reward" "github.com/ava-labs/avalanchego/vms/platformvm/signer" "github.com/ava-labs/avalanchego/vms/platformvm/status" @@ -76,12 +73,18 @@ import ( walletcommon "github.com/ava-labs/avalanchego/wallet/subnet/primary/common" ) -const defaultWeight uint64 = 10000 +const ( + defaultMinDelegatorStake = 1 * units.MilliAvax + defaultMinValidatorStake = 5 * defaultMinDelegatorStake + defaultMaxValidatorStake = 100 * defaultMinValidatorStake -var ( defaultMinStakingDuration = 24 * time.Hour defaultMaxStakingDuration = 365 * 24 * time.Hour + defaultTxFee = 100 * units.NanoAvax +) + +var ( defaultRewardConfig = reward.Config{ MaxConsumptionRate: .12 * reward.PercentDenominator, MinConsumptionRate: .10 * reward.PercentDenominator, @@ -89,28 +92,7 @@ var ( SupplyCap: 720 * units.MegaAvax, } - defaultTxFee = uint64(100) - - // chain timestamp at genesis - defaultGenesisTime = time.Date(1997, 1, 1, 0, 0, 0, 0, time.UTC) - - // time that genesis validators start validating - defaultValidateStartTime = defaultGenesisTime - - // time that genesis validators stop validating - defaultValidateEndTime = defaultValidateStartTime.Add(10 * defaultMinStakingDuration) - - latestForkTime = defaultGenesisTime.Add(time.Second) - - // each key controls an address that has [defaultBalance] AVAX at genesis - keys = secp256k1.TestKeys() - - // Node IDs of genesis validators. Initialized in init function - genesisNodeIDs []ids.NodeID - defaultMinDelegatorStake = 1 * units.MilliAvax - defaultMinValidatorStake = 5 * defaultMinDelegatorStake - defaultMaxValidatorStake = 100 * defaultMinValidatorStake - defaultBalance = 2 * defaultMaxValidatorStake // amount all genesis validators have in defaultVM + latestForkTime = genesistest.DefaultValidatorStartTime.Add(time.Second) defaultStaticFeeConfig = fee.StaticConfig{ TxFee: defaultTxFee, @@ -136,90 +118,19 @@ var ( // Its controlKeys are keys[0], keys[1], keys[2] // Its threshold is 2 testSubnet1 *txs.Tx - testSubnet1ControlKeys = keys[0:3] + testSubnet1ControlKeys = genesistest.DefaultFundedKeys[0:3] ) -func init() { - for _, key := range keys { - // TODO: use ids.GenerateTestNodeID() instead of ids.BuildTestNodeID - // Can be done when TestGetState is refactored - nodeBytes := key.PublicKey().Address() - nodeID := ids.BuildTestNodeID(nodeBytes[:]) - - genesisNodeIDs = append(genesisNodeIDs, nodeID) - } -} - type mutableSharedMemory struct { atomic.SharedMemory } -// Returns: -// 1) The genesis state -// 2) The byte representation of the default genesis for tests -func defaultGenesis(t *testing.T, avaxAssetID ids.ID) (*api.BuildGenesisArgs, []byte) { - require := require.New(t) - - genesisUTXOs := make([]api.UTXO, len(keys)) - for i, key := range keys { - id := key.PublicKey().Address() - addr, err := address.FormatBech32(constants.UnitTestHRP, id.Bytes()) - require.NoError(err) - genesisUTXOs[i] = api.UTXO{ - Amount: json.Uint64(defaultBalance), - Address: addr, - } - } - - genesisValidators := make([]api.GenesisPermissionlessValidator, len(genesisNodeIDs)) - for i, nodeID := range genesisNodeIDs { - addr, err := address.FormatBech32(constants.UnitTestHRP, nodeID.Bytes()) - require.NoError(err) - genesisValidators[i] = api.GenesisPermissionlessValidator{ - GenesisValidator: api.GenesisValidator{ - StartTime: json.Uint64(defaultValidateStartTime.Unix()), - EndTime: json.Uint64(defaultValidateEndTime.Unix()), - NodeID: nodeID, - }, - RewardOwner: &api.Owner{ - Threshold: 1, - Addresses: []string{addr}, - }, - Staked: []api.UTXO{{ - Amount: json.Uint64(defaultWeight), - Address: addr, - }}, - DelegationFee: reward.PercentDenominator, - } - } - - buildGenesisArgs := api.BuildGenesisArgs{ - Encoding: formatting.Hex, - NetworkID: json.Uint32(constants.UnitTestID), - AvaxAssetID: avaxAssetID, - UTXOs: genesisUTXOs, - Validators: genesisValidators, - Chains: nil, - Time: json.Uint64(defaultGenesisTime.Unix()), - InitialSupply: json.Uint64(360 * units.MegaAvax), - } - - buildGenesisResponse := api.BuildGenesisReply{} - platformvmSS := api.StaticService{} - require.NoError(platformvmSS.BuildGenesis(nil, &buildGenesisArgs, &buildGenesisResponse)) - - genesisBytes, err := formatting.Decode(buildGenesisResponse.Encoding, buildGenesisResponse.Bytes) - require.NoError(err) - - return &buildGenesisArgs, genesisBytes -} - func defaultVM(t *testing.T, f upgradetest.Fork) (*VM, *txstest.WalletFactory, database.Database, *mutableSharedMemory) { require := require.New(t) // always reset latestForkTime (a package level variable) // to ensure test independence - latestForkTime = defaultGenesisTime.Add(time.Second) + latestForkTime = genesistest.DefaultValidatorStartTime.Add(time.Second) vm := &VM{Config: config.Config{ Chains: chains.TestManager, UptimeLockedCalculator: uptime.NewLockedCalculator(), @@ -252,7 +163,6 @@ func defaultVM(t *testing.T, f upgradetest.Fork) (*VM, *txstest.WalletFactory, d ctx.Lock.Lock() defer ctx.Lock.Unlock() - _, genesisBytes := defaultGenesis(t, ctx.AVAXAssetID) appSender := &enginetest.Sender{} appSender.CantSendAppGossip = true appSender.SendAppGossipF = func(context.Context, common.SendConfig, []byte) error { @@ -267,7 +177,7 @@ func defaultVM(t *testing.T, f upgradetest.Fork) (*VM, *txstest.WalletFactory, d context.Background(), ctx, chainDB, - genesisBytes, + genesistest.NewBytes(t, genesistest.Config{}), nil, dynamicConfigBytes, msgChan, @@ -289,19 +199,19 @@ func defaultVM(t *testing.T, f upgradetest.Fork) (*VM, *txstest.WalletFactory, d // Create a subnet and store it in testSubnet1 // Note: following Banff activation, block acceptance will move // chain time ahead - builder, signer := factory.NewWallet(keys[0]) + builder, signer := factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewCreateSubnetTx( &secp256k1fx.OutputOwners{ Threshold: 2, Addrs: []ids.ShortID{ - keys[0].PublicKey().Address(), - keys[1].PublicKey().Address(), - keys[2].PublicKey().Address(), + genesistest.DefaultFundedKeys[0].Address(), + genesistest.DefaultFundedKeys[1].Address(), + genesistest.DefaultFundedKeys[2].Address(), }, }, walletcommon.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }), ) require.NoError(err) @@ -343,35 +253,31 @@ func TestGenesis(t *testing.T) { require.NoError(err) require.NotNil(genesisBlock) - genesisState, _ := defaultGenesis(t, vm.ctx.AVAXAssetID) + genesisState := genesistest.New(t, genesistest.Config{}) // Ensure all the genesis UTXOs are there for _, utxo := range genesisState.UTXOs { - _, addrBytes, err := address.ParseBech32(utxo.Address) - require.NoError(err) - - addr, err := ids.ToShortID(addrBytes) - require.NoError(err) - - addrs := set.Of(addr) - utxos, err := avax.GetAllUTXOs(vm.state, addrs) + genesisOut := utxo.Out.(*secp256k1fx.TransferOutput) + utxos, err := avax.GetAllUTXOs( + vm.state, + genesisOut.OutputOwners.AddressesSet(), + ) require.NoError(err) require.Len(utxos, 1) out := utxos[0].Out.(*secp256k1fx.TransferOutput) - if out.Amount() != uint64(utxo.Amount) { - id := keys[0].PublicKey().Address() - addr, err := address.FormatBech32(constants.UnitTestHRP, id.Bytes()) - require.NoError(err) - - require.Equal(utxo.Address, addr) - require.Equal(uint64(utxo.Amount)-vm.StaticFeeConfig.CreateSubnetTxFee, out.Amount()) + if out.Amt != genesisOut.Amt { + require.Equal( + []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, + out.OutputOwners.Addrs, + ) + require.Equal(genesisOut.Amt-vm.StaticFeeConfig.CreateSubnetTxFee, out.Amt) } } // Ensure current validator set of primary network is correct require.Len(genesisState.Validators, vm.Validators.Count(constants.PrimaryNetworkID)) - for _, nodeID := range genesisNodeIDs { + for _, nodeID := range genesistest.DefaultNodeIDs { _, ok := vm.Validators.GetValidator(constants.PrimaryNetworkID, nodeID) require.True(ok) } @@ -399,7 +305,7 @@ func TestAddValidatorCommit(t *testing.T) { require.NoError(err) // create valid tx - builder, txSigner := factory.NewWallet(keys[0]) + builder, txSigner := factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddPermissionlessValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -454,11 +360,11 @@ func TestInvalidAddValidatorCommit(t *testing.T) { defer vm.ctx.Lock.Unlock() nodeID := ids.GenerateTestNodeID() - startTime := defaultGenesisTime.Add(-txexecutor.SyncBound).Add(-1 * time.Second) + startTime := genesistest.DefaultValidatorStartTime.Add(-txexecutor.SyncBound).Add(-1 * time.Second) endTime := startTime.Add(defaultMinStakingDuration) // create invalid tx - builder, txSigner := factory.NewWallet(keys[0]) + builder, txSigner := factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: nodeID, @@ -517,7 +423,7 @@ func TestAddValidatorReject(t *testing.T) { ) // create valid tx - builder, txSigner := factory.NewWallet(keys[0]) + builder, txSigner := factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddValidatorTx( &txs.Validator{ NodeID: nodeID, @@ -561,7 +467,7 @@ func TestAddValidatorInvalidNotReissued(t *testing.T) { defer vm.ctx.Lock.Unlock() // Use nodeID that is already in the genesis - repeatNodeID := genesisNodeIDs[0] + repeatNodeID := genesistest.DefaultNodeIDs[0] startTime := latestForkTime.Add(txexecutor.SyncBound).Add(1 * time.Second) endTime := startTime.Add(defaultMinStakingDuration) @@ -570,7 +476,7 @@ func TestAddValidatorInvalidNotReissued(t *testing.T) { require.NoError(err) // create valid tx - builder, txSigner := factory.NewWallet(keys[0]) + builder, txSigner := factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewAddPermissionlessValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -614,12 +520,12 @@ func TestAddSubnetValidatorAccept(t *testing.T) { var ( startTime = vm.clock.Time().Add(txexecutor.SyncBound).Add(1 * time.Second) endTime = startTime.Add(defaultMinStakingDuration) - nodeID = genesisNodeIDs[0] + nodeID = genesistest.DefaultNodeIDs[0] ) // create valid tx // note that [startTime, endTime] is a subset of time that keys[0] - // validates primary network ([defaultValidateStartTime, defaultValidateEndTime]) + // validates primary network ([genesistest.DefaultValidatorStartTime, genesistest.DefaultValidatorEndTime]) builder, txSigner := factory.NewWallet(testSubnet1ControlKeys[0], testSubnet1ControlKeys[1]) utx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ @@ -627,7 +533,7 @@ func TestAddSubnetValidatorAccept(t *testing.T) { NodeID: nodeID, Start: uint64(startTime.Unix()), End: uint64(endTime.Unix()), - Wght: defaultWeight, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -666,12 +572,12 @@ func TestAddSubnetValidatorReject(t *testing.T) { var ( startTime = vm.clock.Time().Add(txexecutor.SyncBound).Add(1 * time.Second) endTime = startTime.Add(defaultMinStakingDuration) - nodeID = genesisNodeIDs[0] + nodeID = genesistest.DefaultNodeIDs[0] ) // create valid tx // note that [startTime, endTime] is a subset of time that keys[0] - // validates primary network ([defaultValidateStartTime, defaultValidateEndTime]) + // validates primary network ([genesistest.DefaultValidatorStartTime, genesistest.DefaultValidatorEndTime]) builder, txSigner := factory.NewWallet(testSubnet1ControlKeys[1], testSubnet1ControlKeys[2]) utx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ @@ -679,7 +585,7 @@ func TestAddSubnetValidatorReject(t *testing.T) { NodeID: nodeID, Start: uint64(startTime.Unix()), End: uint64(endTime.Unix()), - Wght: defaultWeight, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: testSubnet1.ID(), }, @@ -715,7 +621,7 @@ func TestRewardValidatorAccept(t *testing.T) { defer vm.ctx.Lock.Unlock() // Fast forward clock to time for genesis validators to leave - vm.clock.Set(defaultValidateEndTime) + vm.clock.Set(genesistest.DefaultValidatorEndTime) // Advance time and create proposal to reward a genesis validator blk, err := vm.Builder.BuildBlock(context.Background()) @@ -753,7 +659,7 @@ func TestRewardValidatorAccept(t *testing.T) { // Verify that chain's timestamp has advanced timestamp := vm.state.GetTimestamp() - require.Equal(defaultValidateEndTime.Unix(), timestamp.Unix()) + require.Equal(genesistest.DefaultValidatorEndTimeUnix, uint64(timestamp.Unix())) // Verify that rewarded validator has been removed. // Note that test genesis has multiple validators @@ -783,7 +689,7 @@ func TestRewardValidatorReject(t *testing.T) { defer vm.ctx.Lock.Unlock() // Fast forward clock to time for genesis validators to leave - vm.clock.Set(defaultValidateEndTime) + vm.clock.Set(genesistest.DefaultValidatorEndTime) // Advance time and create proposal to reward a genesis validator blk, err := vm.Builder.BuildBlock(context.Background()) @@ -823,7 +729,7 @@ func TestRewardValidatorReject(t *testing.T) { // Verify that chain's timestamp has advanced timestamp := vm.state.GetTimestamp() - require.Equal(defaultValidateEndTime.Unix(), timestamp.Unix()) + require.Equal(genesistest.DefaultValidatorEndTimeUnix, uint64(timestamp.Unix())) // Verify that rewarded validator has been removed. // Note that test genesis has multiple validators @@ -913,18 +819,18 @@ func TestCreateSubnet(t *testing.T) { vm.ctx.Lock.Lock() defer vm.ctx.Lock.Unlock() - builder, txSigner := factory.NewWallet(keys[0]) + builder, txSigner := factory.NewWallet(genesistest.DefaultFundedKeys[0]) uCreateSubnetTx, err := builder.NewCreateSubnetTx( &secp256k1fx.OutputOwners{ Threshold: 1, Addrs: []ids.ShortID{ - keys[0].PublicKey().Address(), - keys[1].PublicKey().Address(), + genesistest.DefaultFundedKeys[0].Address(), + genesistest.DefaultFundedKeys[1].Address(), }, }, walletcommon.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }), ) require.NoError(err) @@ -953,7 +859,7 @@ func TestCreateSubnet(t *testing.T) { require.Contains(subnetIDs, subnetID) // Now that we've created a new subnet, add a validator to that subnet - nodeID := genesisNodeIDs[0] + nodeID := genesistest.DefaultNodeIDs[0] startTime := vm.clock.Time().Add(txexecutor.SyncBound).Add(1 * time.Second) endTime := startTime.Add(defaultMinStakingDuration) // [startTime, endTime] is subset of time keys[0] validates default subnet so tx is valid @@ -963,7 +869,7 @@ func TestCreateSubnet(t *testing.T) { NodeID: nodeID, Start: uint64(startTime.Unix()), End: uint64(endTime.Unix()), - Wght: defaultWeight, + Wght: genesistest.DefaultValidatorWeight, }, Subnet: subnetID, }, @@ -1020,19 +926,19 @@ func TestAtomicImport(t *testing.T) { OutputIndex: 1, } amount := uint64(50000) - recipientKey := keys[1] + recipientKey := genesistest.DefaultFundedKeys[1] m := atomic.NewMemory(prefixdb.New([]byte{5}, baseDB)) mutableSharedMemory.SharedMemory = m.NewSharedMemory(vm.ctx.ChainID) peerSharedMemory := m.NewSharedMemory(vm.ctx.XChainID) - builder, _ := factory.NewWallet(keys[0]) + builder, _ := factory.NewWallet(genesistest.DefaultFundedKeys[0]) _, err := builder.NewImportTx( vm.ctx.XChainID, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{recipientKey.PublicKey().Address()}, + Addrs: []ids.ShortID{recipientKey.Address()}, }, ) require.ErrorIs(err, walletbuilder.ErrInsufficientFunds) @@ -1046,7 +952,7 @@ func TestAtomicImport(t *testing.T) { Amt: amount, OutputOwners: secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{recipientKey.PublicKey().Address()}, + Addrs: []ids.ShortID{recipientKey.Address()}, }, }, } @@ -1061,7 +967,7 @@ func TestAtomicImport(t *testing.T) { Key: inputID[:], Value: utxoBytes, Traits: [][]byte{ - recipientKey.PublicKey().Address().Bytes(), + recipientKey.Address().Bytes(), }, }, }, @@ -1073,7 +979,7 @@ func TestAtomicImport(t *testing.T) { vm.ctx.XChainID, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{recipientKey.PublicKey().Address()}, + Addrs: []ids.ShortID{recipientKey.Address()}, }, ) require.NoError(err) @@ -1175,7 +1081,7 @@ func TestRestartFullyAccepted(t *testing.T) { firstCtx := snowtest.Context(t, snowtest.PChainID) - _, genesisBytes := defaultGenesis(t, firstCtx.AVAXAssetID) + genesisBytes := genesistest.NewBytes(t, genesistest.Config{}) baseDB := memdb.New() atomicDB := prefixdb.New([]byte{1}, baseDB) @@ -1308,8 +1214,6 @@ func TestBootstrapPartiallyAccepted(t *testing.T) { vm.clock.Set(initialClkTime) ctx := snowtest.Context(t, snowtest.PChainID) - _, genesisBytes := defaultGenesis(t, ctx.AVAXAssetID) - atomicDB := prefixdb.New([]byte{1}, baseDB) m := atomic.NewMemory(atomicDB) ctx.SharedMemory = m.NewSharedMemory(ctx.ChainID) @@ -1322,7 +1226,7 @@ func TestBootstrapPartiallyAccepted(t *testing.T) { context.Background(), ctx, vmDB, - genesisBytes, + genesistest.NewBytes(t, genesistest.Config{}), nil, nil, msgChan, @@ -1659,14 +1563,12 @@ func TestUnverifiedParent(t *testing.T) { ctx.Lock.Unlock() }() - _, genesisBytes := defaultGenesis(t, ctx.AVAXAssetID) - msgChan := make(chan common.Message, 1) require.NoError(vm.Initialize( context.Background(), ctx, memdb.New(), - genesisBytes, + genesistest.NewBytes(t, genesistest.Config{}), nil, nil, msgChan, @@ -1750,7 +1652,7 @@ func TestMaxStakeAmount(t *testing.T) { vm.ctx.Lock.Lock() defer vm.ctx.Lock.Unlock() - nodeID := genesisNodeIDs[0] + nodeID := genesistest.DefaultNodeIDs[0] tests := []struct { description string @@ -1759,23 +1661,23 @@ func TestMaxStakeAmount(t *testing.T) { }{ { description: "[validator.StartTime] == [startTime] < [endTime] == [validator.EndTime]", - startTime: defaultValidateStartTime, - endTime: defaultValidateEndTime, + startTime: genesistest.DefaultValidatorStartTime, + endTime: genesistest.DefaultValidatorEndTime, }, { description: "[validator.StartTime] < [startTime] < [endTime] == [validator.EndTime]", - startTime: defaultValidateStartTime.Add(time.Minute), - endTime: defaultValidateEndTime, + startTime: genesistest.DefaultValidatorStartTime.Add(time.Minute), + endTime: genesistest.DefaultValidatorEndTime, }, { description: "[validator.StartTime] == [startTime] < [endTime] < [validator.EndTime]", - startTime: defaultValidateStartTime, - endTime: defaultValidateEndTime.Add(-time.Minute), + startTime: genesistest.DefaultValidatorStartTime, + endTime: genesistest.DefaultValidatorEndTime.Add(-time.Minute), }, { description: "[validator.StartTime] < [startTime] < [endTime] < [validator.EndTime]", - startTime: defaultValidateStartTime.Add(time.Minute), - endTime: defaultValidateEndTime.Add(-time.Minute), + startTime: genesistest.DefaultValidatorStartTime.Add(time.Minute), + endTime: genesistest.DefaultValidatorEndTime.Add(-time.Minute), }, } @@ -1787,14 +1689,14 @@ func TestMaxStakeAmount(t *testing.T) { amount, err := txexecutor.GetMaxWeight(vm.state, staker, test.startTime, test.endTime) require.NoError(err) - require.Equal(defaultWeight, amount) + require.Equal(genesistest.DefaultValidatorWeight, amount) }) } } func TestUptimeDisallowedWithRestart(t *testing.T) { require := require.New(t) - latestForkTime = defaultValidateStartTime.Add(defaultMinStakingDuration) + latestForkTime = genesistest.DefaultValidatorStartTime.Add(defaultMinStakingDuration) db := memdb.New() firstDB := prefixdb.New([]byte{}, db) @@ -1811,7 +1713,7 @@ func TestUptimeDisallowedWithRestart(t *testing.T) { firstCtx := snowtest.Context(t, snowtest.PChainID) firstCtx.Lock.Lock() - _, genesisBytes := defaultGenesis(t, firstCtx.AVAXAssetID) + genesisBytes := genesistest.NewBytes(t, genesistest.Config{}) firstMsgChan := make(chan common.Message, 1) require.NoError(firstVM.Initialize( @@ -1834,8 +1736,8 @@ func TestUptimeDisallowedWithRestart(t *testing.T) { require.NoError(firstVM.SetState(context.Background(), snow.NormalOp)) // Fast forward clock so that validators meet 20% uptime required for reward - durationForReward := defaultValidateEndTime.Sub(defaultValidateStartTime) * firstUptimePercentage / 100 - vmStopTime := defaultValidateStartTime.Add(durationForReward) + durationForReward := genesistest.DefaultValidatorEndTime.Sub(genesistest.DefaultValidatorStartTime) * firstUptimePercentage / 100 + vmStopTime := genesistest.DefaultValidatorStartTime.Add(durationForReward) firstVM.clock.Set(vmStopTime) // Shutdown VM to stop all genesis validator uptime. @@ -1885,7 +1787,7 @@ func TestUptimeDisallowedWithRestart(t *testing.T) { require.NoError(secondVM.SetState(context.Background(), snow.NormalOp)) // after restart and change of uptime required for reward, push validators to their end of life - secondVM.clock.Set(defaultValidateEndTime) + secondVM.clock.Set(genesistest.DefaultValidatorEndTime) // evaluate a genesis validator for reward blk, err := secondVM.Builder.BuildBlock(context.Background()) @@ -1938,7 +1840,7 @@ func TestUptimeDisallowedWithRestart(t *testing.T) { func TestUptimeDisallowedAfterNeverConnecting(t *testing.T) { require := require.New(t) - latestForkTime = defaultValidateStartTime.Add(defaultMinStakingDuration) + latestForkTime = genesistest.DefaultValidatorStartTime.Add(defaultMinStakingDuration) db := memdb.New() @@ -1954,8 +1856,6 @@ func TestUptimeDisallowedAfterNeverConnecting(t *testing.T) { ctx := snowtest.Context(t, snowtest.PChainID) ctx.Lock.Lock() - _, genesisBytes := defaultGenesis(t, ctx.AVAXAssetID) - atomicDB := prefixdb.New([]byte{1}, db) m := atomic.NewMemory(atomicDB) ctx.SharedMemory = m.NewSharedMemory(ctx.ChainID) @@ -1966,7 +1866,7 @@ func TestUptimeDisallowedAfterNeverConnecting(t *testing.T) { context.Background(), ctx, db, - genesisBytes, + genesistest.NewBytes(t, genesistest.Config{}), nil, nil, msgChan, @@ -1987,7 +1887,7 @@ func TestUptimeDisallowedAfterNeverConnecting(t *testing.T) { require.NoError(vm.SetState(context.Background(), snow.NormalOp)) // Fast forward clock to time for genesis validators to leave - vm.clock.Set(defaultValidateEndTime) + vm.clock.Set(genesistest.DefaultValidatorEndTime) // evaluate a genesis validator for reward blk, err := vm.Builder.BuildBlock(context.Background()) @@ -2051,12 +1951,12 @@ func TestRemovePermissionedValidatorDuringAddPending(t *testing.T) { key, err := secp256k1.NewPrivateKey() require.NoError(err) - id := key.PublicKey().Address() + id := key.Address() nodeID := ids.GenerateTestNodeID() sk, err := bls.NewSecretKey() require.NoError(err) - builder, txSigner := factory.NewWallet(keys[0]) + builder, txSigner := factory.NewWallet(genesistest.DefaultFundedKeys[0]) uAddValTx, err := builder.NewAddPermissionlessValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -2080,7 +1980,7 @@ func TestRemovePermissionedValidatorDuringAddPending(t *testing.T) { reward.PercentDenominator, walletcommon.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }), ) require.NoError(err) @@ -2105,7 +2005,7 @@ func TestRemovePermissionedValidatorDuringAddPending(t *testing.T) { }, walletcommon.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }), ) require.NoError(err) @@ -2123,7 +2023,7 @@ func TestRemovePermissionedValidatorDuringAddPending(t *testing.T) { require.NoError(createSubnetBlock.Accept(context.Background())) require.NoError(vm.SetPreference(context.Background(), vm.manager.LastAccepted())) - builder, txSigner = factory.NewWallet(key, keys[1]) + builder, txSigner = factory.NewWallet(key, genesistest.DefaultFundedKeys[1]) uAddSubnetValTx, err := builder.NewAddSubnetValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -2136,20 +2036,20 @@ func TestRemovePermissionedValidatorDuringAddPending(t *testing.T) { }, walletcommon.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[1].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[1].Address()}, }), ) require.NoError(err) addSubnetValidatorTx, err := walletsigner.SignUnsigned(context.Background(), txSigner, uAddSubnetValTx) require.NoError(err) - builder, txSigner = factory.NewWallet(key, keys[2]) + builder, txSigner = factory.NewWallet(key, genesistest.DefaultFundedKeys[2]) uRemoveSubnetValTx, err := builder.NewRemoveSubnetValidatorTx( nodeID, createSubnetTx.ID(), walletcommon.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[2].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[2].Address()}, }), ) require.NoError(err) @@ -2184,15 +2084,15 @@ func TestTransferSubnetOwnershipTx(t *testing.T) { vm.ctx.Lock.Lock() defer vm.ctx.Lock.Unlock() - builder, txSigner := factory.NewWallet(keys[0]) + builder, txSigner := factory.NewWallet(genesistest.DefaultFundedKeys[0]) uCreateSubnetTx, err := builder.NewCreateSubnetTx( &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }, walletcommon.WithChangeOwner(&secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[0].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()}, }), ) require.NoError(err) @@ -2221,7 +2121,7 @@ func TestTransferSubnetOwnershipTx(t *testing.T) { Locktime: 0, Threshold: 1, Addrs: []ids.ShortID{ - keys[0].PublicKey().Address(), + genesistest.DefaultFundedKeys[0].Address(), }, } ctx, err := walletbuilder.NewSnowContext(vm.ctx.NetworkID, vm.ctx.AVAXAssetID) @@ -2233,7 +2133,7 @@ func TestTransferSubnetOwnershipTx(t *testing.T) { subnetID, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[1].PublicKey().Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[1].Address()}, }, ) require.NoError(err) @@ -2260,7 +2160,7 @@ func TestTransferSubnetOwnershipTx(t *testing.T) { Locktime: 0, Threshold: 1, Addrs: []ids.ShortID{ - keys[1].PublicKey().Address(), + genesistest.DefaultFundedKeys[1].Address(), }, } expectedOwner.InitCtx(ctx) @@ -2276,7 +2176,7 @@ func TestBaseTx(t *testing.T) { sendAmt := uint64(100000) changeAddr := ids.ShortEmpty - builder, txSigner := factory.NewWallet(keys[0]) + builder, txSigner := factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewBaseTx( []*avax.TransferableOutput{ { @@ -2286,7 +2186,7 @@ func TestBaseTx(t *testing.T) { OutputOwners: secp256k1fx.OutputOwners{ Threshold: 1, Addrs: []ids.ShortID{ - keys[1].Address(), + genesistest.DefaultFundedKeys[1].Address(), }, }, }, @@ -2308,7 +2208,7 @@ func TestBaseTx(t *testing.T) { require.NoError(err) require.IsType(&secp256k1fx.TransferOutput{}, utxo.Out) castOut := utxo.Out.(*secp256k1fx.TransferOutput) - if castOut.AddressesSet().Equals(set.Of(keys[0].Address())) { + if castOut.AddressesSet().Equals(set.Of(genesistest.DefaultFundedKeys[0].Address())) { key0InputAmt += castOut.Amt } totalInputAmt += castOut.Amt @@ -2322,10 +2222,10 @@ func TestBaseTx(t *testing.T) { for _, output := range baseTx.Unsigned.Outputs() { require.IsType(&secp256k1fx.TransferOutput{}, output.Out) castOut := output.Out.(*secp256k1fx.TransferOutput) - if castOut.AddressesSet().Equals(set.Of(keys[0].Address())) { + if castOut.AddressesSet().Equals(set.Of(genesistest.DefaultFundedKeys[0].Address())) { key0OutputAmt += castOut.Amt } - if castOut.AddressesSet().Equals(set.Of(keys[1].Address())) { + if castOut.AddressesSet().Equals(set.Of(genesistest.DefaultFundedKeys[1].Address())) { key1OutputAmt += castOut.Amt } if castOut.AddressesSet().Equals(set.Of(changeAddr)) { @@ -2363,7 +2263,7 @@ func TestPruneMempool(t *testing.T) { sendAmt := uint64(100000) changeAddr := ids.ShortEmpty - builder, txSigner := factory.NewWallet(keys[0]) + builder, txSigner := factory.NewWallet(genesistest.DefaultFundedKeys[0]) utx, err := builder.NewBaseTx( []*avax.TransferableOutput{ { @@ -2373,7 +2273,7 @@ func TestPruneMempool(t *testing.T) { OutputOwners: secp256k1fx.OutputOwners{ Threshold: 1, Addrs: []ids.ShortID{ - keys[1].Address(), + genesistest.DefaultFundedKeys[1].Address(), }, }, }, @@ -2406,7 +2306,7 @@ func TestPruneMempool(t *testing.T) { sk, err := bls.NewSecretKey() require.NoError(err) - builder, txSigner = factory.NewWallet(keys[1]) + builder, txSigner = factory.NewWallet(genesistest.DefaultFundedKeys[1]) uAddValTx, err := builder.NewAddPermissionlessValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ @@ -2421,11 +2321,11 @@ func TestPruneMempool(t *testing.T) { vm.ctx.AVAXAssetID, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[2].Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[2].Address()}, }, &secp256k1fx.OutputOwners{ Threshold: 1, - Addrs: []ids.ShortID{keys[2].Address()}, + Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[2].Address()}, }, 20000, )