Skip to content

Commit

Permalink
Remove duplicate genesis creations in P-chain unit tests (#3318)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Aug 23, 2024
1 parent bb9f77e commit 85ab969
Show file tree
Hide file tree
Showing 30 changed files with 823 additions and 1,194 deletions.
25 changes: 13 additions & 12 deletions vms/platformvm/block/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand All @@ -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)
Expand Down Expand Up @@ -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{
Expand All @@ -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)
Expand All @@ -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{
Expand All @@ -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)
Expand Down
107 changes: 14 additions & 93 deletions vms/platformvm/block/builder/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -275,7 +253,7 @@ func defaultConfig(f upgradetest.Fork) *config.Config {
upgradetest.SetTimesTo(
&upgrades,
min(f, upgradetest.ApricotPhase5),
defaultValidateEndTime,
genesistest.DefaultValidatorEndTime,
)

return &config.Config{
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
9 changes: 5 additions & 4 deletions vms/platformvm/block/builder/standard_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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))

Expand All @@ -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()},
},
},
}
Expand All @@ -59,7 +60,7 @@ func TestAtomicTxImports(t *testing.T) {
Key: inputID[:],
Value: utxoBytes,
Traits: [][]byte{
recipientKey.PublicKey().Address().Bytes(),
recipientKey.Address().Bytes(),
},
}}},
}))
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 85ab969

Please sign in to comment.