Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Pull Genesis parsing out of state #2284

Closed
wants to merge 38 commits into from
Closed
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
2d2eac2
cleaned up state ctor
abi87 Nov 9, 2023
f599f94
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Nov 9, 2023
47f3496
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Nov 9, 2023
3da37dc
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Nov 13, 2023
317cb83
nit
abi87 Nov 13, 2023
d929504
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Nov 14, 2023
37f2130
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Nov 15, 2023
3eba6b7
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Nov 16, 2023
b61fa88
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Nov 17, 2023
b5dd88a
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Nov 18, 2023
18b101f
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Nov 21, 2023
1d8437d
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Nov 22, 2023
e4c3328
replaced GenesisID with GenesisBytes attribute
abi87 Nov 22, 2023
dfe422c
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Nov 23, 2023
0bc87ac
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Nov 27, 2023
64bfb9f
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Nov 28, 2023
1a72930
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Nov 29, 2023
f4e28c2
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Nov 30, 2023
19a9fd5
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Dec 3, 2023
80ad266
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Dec 4, 2023
e0ab6c8
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Dec 5, 2023
2a4ba56
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Dec 6, 2023
c3fef3a
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Dec 7, 2023
bc0b84f
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Dec 8, 2023
9efc31d
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Dec 9, 2023
1362114
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Dec 12, 2023
b9c227a
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Dec 12, 2023
5db63ac
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Dec 13, 2023
2bbe45a
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Dec 16, 2023
3e1890c
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Dec 18, 2023
e2aaa5e
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Dec 19, 2023
ab1cd0b
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Jan 2, 2024
f87a7e4
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Jan 2, 2024
19cf59d
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Jan 4, 2024
afc5b39
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Jan 16, 2024
b30633d
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Jan 17, 2024
f56646b
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Jan 18, 2024
267dfa4
Merge branch 'dev' into pchain_state_ctor_cleanup
abi87 Jan 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 67 additions & 52 deletions vms/platformvm/block/builder/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,21 @@ 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/components/avax"
"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"
"github.com/ava-labs/avalanchego/vms/platformvm/metrics"
"github.com/ava-labs/avalanchego/vms/platformvm/network"
"github.com/ava-labs/avalanchego/vms/platformvm/reward"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/vms/platformvm/status"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/txheap"
"github.com/ava-labs/avalanchego/vms/platformvm/utxo"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"

Expand All @@ -55,10 +53,7 @@ import (
pvalidators "github.com/ava-labs/avalanchego/vms/platformvm/validators"
)

const (
defaultWeight = 10000
trackChecksum = false
)
const defaultWeight = 10000

var (
defaultMinStakingDuration = 24 * time.Hour
Expand Down Expand Up @@ -245,10 +240,10 @@ func defaultState(
require := require.New(t)

execCfg, _ := config.GetExecutionConfig([]byte(`{}`))
genesisBytes := buildGenesisTest(t, ctx)
genesis := buildGenesisTest(t, ctx)
state, err := state.New(
db,
genesisBytes,
genesis,
prometheus.NewRegistry(),
cfg,
execCfg,
Expand Down Expand Up @@ -362,61 +357,81 @@ func defaultFx(t *testing.T, clk *mockable.Clock, log logging.Logger, isBootstra
return res
}

func buildGenesisTest(t *testing.T, ctx *snow.Context) []byte {
func buildGenesisTest(t *testing.T, ctx *snow.Context) *genesis.Genesis {
marun marked this conversation as resolved.
Show resolved Hide resolved
require := require.New(t)

genesisUTXOs := make([]api.UTXO, len(preFundedKeys))
genesisUTXOs := make([]*genesis.UTXO, len(preFundedKeys))
for i, key := range preFundedKeys {
marun marked this conversation as resolved.
Show resolved Hide resolved
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,
addr := key.PublicKey().Address()
genesisUTXOs[i] = &genesis.UTXO{
UTXO: avax.UTXO{
UTXOID: avax.UTXOID{
TxID: ids.Empty,
OutputIndex: uint32(i),
},
Asset: avax.Asset{ID: ctx.AVAXAssetID},
Out: &secp256k1fx.TransferOutput{
Amt: defaultBalance,
OutputOwners: secp256k1fx.OutputOwners{
Locktime: 0,
Threshold: 1,
Addrs: []ids.ShortID{addr},
},
},
},
Message: nil,
}
}

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,
vdrs := txheap.NewByEndTime()
for idx, nodeID := range genesisNodeIDs {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We initialize genesisNodeIDs in init(), so I think it would make sense for us to also initialize them here as well. Maybe we can move that blob of code down here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then what if we write a test that does not really use the environment? The genesisNodeIDs content would change depending on the order tests are executed right?
This is the reason I'd keep them in the init,

addr := preFundedKeys[idx].PublicKey().Address()

utxo := &avax.TransferableOutput{
Asset: avax.Asset{ID: ctx.AVAXAssetID},
Out: &secp256k1fx.TransferOutput{
Amt: defaultWeight,
OutputOwners: secp256k1fx.OutputOwners{
Locktime: 0,
Threshold: 1,
Addrs: []ids.ShortID{addr},
},
},
RewardOwner: &api.Owner{
Threshold: 1,
Addresses: []string{addr},
},
Staked: []api.UTXO{{
Amount: json.Uint64(defaultWeight),
Address: addr,
}},
DelegationFee: reward.PercentDenominator,
}

owner := &secp256k1fx.OutputOwners{
Locktime: 0,
Threshold: 1,
Addrs: []ids.ShortID{addr},
}

tx := &txs.Tx{Unsigned: &txs.AddValidatorTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: ctx.NetworkID,
BlockchainID: constants.PlatformChainID,
}},
Validator: txs.Validator{
NodeID: nodeID,
Start: uint64(defaultValidateStartTime.Unix()),
End: uint64(defaultValidateEndTime.Unix()),
Wght: utxo.Output().Amount(),
},
StakeOuts: []*avax.TransferableOutput{utxo},
RewardsOwner: owner,
DelegationShares: reward.PercentDenominator,
}}
require.NoError(tx.Initialize(txs.GenesisCodec))
vdrs.Add(tx)
}

buildGenesisArgs := api.BuildGenesisArgs{
NetworkID: json.Uint32(constants.UnitTestID),
AvaxAssetID: ctx.AVAXAssetID,
return &genesis.Genesis{
GenesisBytes: []byte{'g', 'e', 'n', 'e', 's', 'i', 's', 'B', 'y', 't', 'e', 's'},
UTXOs: genesisUTXOs,
Validators: genesisValidators,
Validators: vdrs.List(),
Chains: nil,
Time: json.Uint64(defaultGenesisTime.Unix()),
InitialSupply: json.Uint64(360 * units.MegaAvax),
Encoding: formatting.Hex,
Timestamp: uint64(defaultGenesisTime.Unix()),
InitialSupply: 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 genesisBytes
}

func shutdownEnvironment(env *environment) error {
Expand Down
122 changes: 67 additions & 55 deletions vms/platformvm/block/executor/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,21 @@ 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/components/avax"
"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"
"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/status"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/executor"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/txheap"
"github.com/ava-labs/avalanchego/vms/platformvm/utxo"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"

Expand Down Expand Up @@ -268,11 +266,11 @@ func defaultState(
db database.Database,
rewards reward.Calculator,
) state.State {
genesisBytes := buildGenesisTest(ctx)
genesis := buildGenesisTest(ctx)
execCfg, _ := config.GetExecutionConfig([]byte(`{}`))
state, err := state.New(
db,
genesisBytes,
genesis,
prometheus.NewRegistry(),
cfg,
execCfg,
Expand Down Expand Up @@ -389,67 +387,81 @@ 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))
func buildGenesisTest(ctx *snow.Context) *genesis.Genesis {
Copy link
Contributor

@joshua-kim joshua-kim Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually didn't reailze we have a lot of genesis helpers to create pre-funded genesis. Wondering if it makes sense for us to consolidate this into a single place somewhere and export it out of a package. It could be something like:

package somewhere

func NewPrefundedGenesis(keys ...*secp2561.PrivateKey) (*TestGenesis, error) {
...
}

type *TestGenesis struct {
    *Genesis
    Keys []*secp256k1.PrivateKey
}

I'm not sure what package i'd put his in. Maybe we can make a package like genesis/test or put this in platformvm somewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I do in #2107 for genesis helpers and subsequent PRs for other test helpers.
Sorry we got the code duplication here, just trying to segment changes limit PRs scope

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also historically, duplication arised when I repackaged platformvm but the way test parameters were set were slightly different across packaged. #2107 and subsequent PR remove duplicated code and fixes test to make them work with the default values and not ony with the specific values used in different packages.

genesisUTXOs := make([]*genesis.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,
addr := key.PublicKey().Address()
genesisUTXOs[i] = &genesis.UTXO{
UTXO: avax.UTXO{
UTXOID: avax.UTXOID{
TxID: ids.Empty,
OutputIndex: uint32(i),
},
Asset: avax.Asset{ID: ctx.AVAXAssetID},
Out: &secp256k1fx.TransferOutput{
Amt: defaultBalance,
OutputOwners: secp256k1fx.OutputOwners{
Locktime: 0,
Threshold: 1,
Addrs: []ids.ShortID{addr},
},
},
},
Message: nil,
}
}

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,
vdrs := txheap.NewByEndTime()
for idx, nodeID := range genesisNodeIDs {
addr := preFundedKeys[idx].PublicKey().Address()
utxo := &avax.TransferableOutput{
Asset: avax.Asset{ID: ctx.AVAXAssetID},
Out: &secp256k1fx.TransferOutput{
Amt: defaultWeight,
OutputOwners: secp256k1fx.OutputOwners{
Locktime: 0,
Threshold: 1,
Addrs: []ids.ShortID{addr},
},
},
RewardOwner: &api.Owner{
Threshold: 1,
Addresses: []string{addr},
},
Staked: []api.UTXO{{
Amount: json.Uint64(defaultWeight),
Address: addr,
}

owner := &secp256k1fx.OutputOwners{
Locktime: 0,
Threshold: 1,
Addrs: []ids.ShortID{addr},
}

tx := &txs.Tx{Unsigned: &txs.AddValidatorTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: ctx.NetworkID,
BlockchainID: constants.PlatformChainID,
}},
DelegationFee: reward.PercentDenominator,
Validator: txs.Validator{
NodeID: nodeID,
Start: uint64(defaultValidateStartTime.Unix()),
End: uint64(defaultValidateEndTime.Unix()),
Wght: utxo.Output().Amount(),
},
StakeOuts: []*avax.TransferableOutput{utxo},
RewardsOwner: owner,
DelegationShares: reward.PercentDenominator,
}}

if err := tx.Initialize(txs.GenesisCodec); err != nil {
panic(err)
marun marked this conversation as resolved.
Show resolved Hide resolved
}
vdrs.Add(tx)
}

buildGenesisArgs := api.BuildGenesisArgs{
NetworkID: json.Uint32(constants.UnitTestID),
AvaxAssetID: ctx.AVAXAssetID,
return &genesis.Genesis{
GenesisBytes: []byte{'g', 'e', 'n', 'e', 's', 'i', 's', 'B', 'y', 't', 'e', 's'},
UTXOs: genesisUTXOs,
Validators: genesisValidators,
Validators: vdrs.List(),
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))
Timestamp: uint64(defaultGenesisTime.Unix()),
InitialSupply: 360 * units.MegaAvax,
}

genesisBytes, err := formatting.Decode(buildGenesisResponse.Encoding, buildGenesisResponse.Bytes)
if err != nil {
panic(err)
}

return genesisBytes
}

func shutdownEnvironment(t *environment) error {
Expand Down
4 changes: 4 additions & 0 deletions vms/platformvm/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type Genesis struct {
Timestamp uint64 `serialize:"true"`
InitialSupply uint64 `serialize:"true"`
Message string `serialize:"true"`

GenesisBytes []byte `serialize:"false"`
}

func Parse(genesisBytes []byte) (*Genesis, error) {
Expand All @@ -39,5 +41,7 @@ func Parse(genesisBytes []byte) (*Genesis, error) {
return nil, err
}
}

gen.GenesisBytes = genesisBytes
return gen, nil
}
Loading
Loading