Skip to content

Commit

Permalink
Move P-chain txHeap, static APIs, and genesis into their own packages (
Browse files Browse the repository at this point in the history
…ava-labs#1593)

Co-authored-by: Stephen Buttolph <[email protected]>
  • Loading branch information
abi87 and StephenButtolph authored Jun 16, 2022
1 parent 6d52877 commit cfc1ff0
Show file tree
Hide file tree
Showing 28 changed files with 491 additions and 471 deletions.
11 changes: 4 additions & 7 deletions genesis/aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/vms/nftfx"
"github.com/ava-labs/avalanchego/vms/platformvm"
"github.com/ava-labs/avalanchego/vms/platformvm/genesis"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/propertyfx"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
Expand All @@ -28,14 +28,11 @@ func Aliases(genesisBytes []byte) (map[string][]string, map[ids.ID][]string, err
chainAliases := map[ids.ID][]string{
constants.PlatformChainID: {"P", "platform"},
}
genesis := &platformvm.Genesis{} // TODO let's not re-create genesis to do aliasing
if _, err := platformvm.GenesisCodec.Unmarshal(genesisBytes, genesis); err != nil {
return nil, nil, err
}
if err := genesis.Initialize(); err != nil {

genesis, err := genesis.Parse(genesisBytes) // TODO let's not re-create genesis to do aliasing
if err != nil {
return nil, nil, err
}

for _, chain := range genesis.Chains {
uChain := chain.Unsigned.(*txs.CreateChainTx)
chainID := chain.ID()
Expand Down
32 changes: 15 additions & 17 deletions genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (
"github.com/ava-labs/avalanchego/vms/avm"
"github.com/ava-labs/avalanchego/vms/avm/fxs"
"github.com/ava-labs/avalanchego/vms/nftfx"
"github.com/ava-labs/avalanchego/vms/platformvm"
"github.com/ava-labs/avalanchego/vms/platformvm/api"
"github.com/ava-labs/avalanchego/vms/platformvm/genesis"
"github.com/ava-labs/avalanchego/vms/propertyfx"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"

Expand Down Expand Up @@ -326,7 +327,7 @@ func FromConfig(config *Config) ([]byte, ids.ID, error) {
skippedAllocations := []Allocation(nil)

// Specify the initial state of the Platform Chain
platformvmArgs := platformvm.BuildGenesisArgs{
platformvmArgs := api.BuildGenesisArgs{
AvaxAssetID: avaxAssetID,
NetworkID: json.Uint32(config.NetworkID),
Time: json.Uint64(config.StartTime),
Expand All @@ -350,7 +351,7 @@ func FromConfig(config *Config) ([]byte, ids.ID, error) {
return nil, ids.Empty, fmt.Errorf("couldn't encode message: %w", err)
}
platformvmArgs.UTXOs = append(platformvmArgs.UTXOs,
platformvm.APIUTXO{
api.UTXO{
Locktime: json.Uint64(unlock.Locktime),
Amount: json.Uint64(unlock.Amount),
Address: addr,
Expand All @@ -375,7 +376,7 @@ func FromConfig(config *Config) ([]byte, ids.ID, error) {
return nil, ids.ID{}, err
}

utxos := []platformvm.APIUTXO(nil)
utxos := []api.UTXO(nil)
for _, allocation := range nodeAllocations {
addr, err := address.FormatBech32(hrp, allocation.AVAXAddr.Bytes())
if err != nil {
Expand All @@ -386,7 +387,7 @@ func FromConfig(config *Config) ([]byte, ids.ID, error) {
if err != nil {
return nil, ids.Empty, fmt.Errorf("couldn't encode message: %w", err)
}
utxos = append(utxos, platformvm.APIUTXO{
utxos = append(utxos, api.UTXO{
Locktime: json.Uint64(unlock.Locktime),
Amount: json.Uint64(unlock.Amount),
Address: addr,
Expand All @@ -399,13 +400,13 @@ func FromConfig(config *Config) ([]byte, ids.ID, error) {
delegationFee := json.Uint32(staker.DelegationFee)

platformvmArgs.Validators = append(platformvmArgs.Validators,
platformvm.APIPrimaryValidator{
APIStaker: platformvm.APIStaker{
api.PrimaryValidator{
Staker: api.Staker{
StartTime: json.Uint64(genesisTime.Unix()),
EndTime: json.Uint64(endStakingTime.Unix()),
NodeID: staker.NodeID,
},
RewardOwner: &platformvm.APIOwner{
RewardOwner: &api.Owner{
Threshold: 1,
Addresses: []string{destAddrStr},
},
Expand All @@ -420,7 +421,7 @@ func FromConfig(config *Config) ([]byte, ids.ID, error) {
if err != nil {
return nil, ids.Empty, fmt.Errorf("couldn't encode message: %w", err)
}
platformvmArgs.Chains = []platformvm.APIChain{
platformvmArgs.Chains = []api.Chain{
{
GenesisData: avmReply.Bytes,
SubnetID: constants.PrimaryNetworkID,
Expand All @@ -440,8 +441,8 @@ func FromConfig(config *Config) ([]byte, ids.ID, error) {
},
}

platformvmReply := platformvm.BuildGenesisReply{}
platformvmSS := platformvm.StaticService{}
platformvmReply := api.BuildGenesisReply{}
platformvmSS := api.StaticService{}
if err := platformvmSS.BuildGenesis(nil, &platformvmArgs, &platformvmReply); err != nil {
return nil, ids.ID{}, fmt.Errorf("problem while building platform chain's genesis state: %w", err)
}
Expand Down Expand Up @@ -518,12 +519,9 @@ func splitAllocations(allocations []Allocation, numSplits int) [][]Allocation {
}

func VMGenesis(genesisBytes []byte, vmID ids.ID) (*pchaintxs.Tx, error) {
genesis := platformvm.Genesis{}
if _, err := platformvm.GenesisCodec.Unmarshal(genesisBytes, &genesis); err != nil {
return nil, fmt.Errorf("couldn't unmarshal genesis bytes due to: %w", err)
}
if err := genesis.Initialize(); err != nil {
return nil, err
genesis, err := genesis.Parse(genesisBytes)
if err != nil {
return nil, fmt.Errorf("failed to parse genesis: %w", err)
}
for _, chain := range genesis.Chains {
uChain := chain.Unsigned.(*pchaintxs.CreateChainTx)
Expand Down
8 changes: 3 additions & 5 deletions genesis/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/hashing"
"github.com/ava-labs/avalanchego/utils/perms"
"github.com/ava-labs/avalanchego/vms/platformvm"
"github.com/ava-labs/avalanchego/vms/platformvm/genesis"
)

func TestValidateConfig(t *testing.T) {
Expand Down Expand Up @@ -311,8 +311,7 @@ func TestGenesisFromFile(t *testing.T) {
genesisHash := fmt.Sprintf("%x", hashing.ComputeHash256(genesisBytes))
assert.Equal(test.expected, genesisHash, "genesis hash mismatch")

genesis := platformvm.Genesis{}
_, err = platformvm.GenesisCodec.Unmarshal(genesisBytes, &genesis)
_, err = genesis.Parse(genesisBytes)
assert.NoError(err)
})
}
Expand Down Expand Up @@ -401,8 +400,7 @@ func TestGenesisFromFlag(t *testing.T) {
genesisHash := fmt.Sprintf("%x", hashing.ComputeHash256(genesisBytes))
assert.Equal(test.expected, genesisHash, "genesis hash mismatch")

genesis := platformvm.Genesis{}
_, err = platformvm.GenesisCodec.Unmarshal(genesisBytes, &genesis)
_, err = genesis.Parse(genesisBytes)
assert.NoError(err)
})
}
Expand Down
20 changes: 10 additions & 10 deletions tests/e2e/static-handlers/suites.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/ava-labs/avalanchego/utils/json"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/vms/avm"
"github.com/ava-labs/avalanchego/vms/platformvm"
"github.com/ava-labs/avalanchego/vms/platformvm/api"
"github.com/ava-labs/avalanchego/vms/platformvm/reward"
)

Expand Down Expand Up @@ -131,42 +131,42 @@ var _ = ginkgo.Describe("[StaticHandlers]", func() {
keys = append(keys, pk.(*crypto.PrivateKeySECP256K1R))
}

genesisUTXOs := make([]platformvm.APIUTXO, len(keys))
genesisUTXOs := make([]api.UTXO, len(keys))
hrp := constants.NetworkIDToHRP[constants.UnitTestID]
for i, key := range keys {
id := key.PublicKey().Address()
addr, err := address.FormatBech32(hrp, id.Bytes())
gomega.Expect(err).Should(gomega.BeNil())
genesisUTXOs[i] = platformvm.APIUTXO{
genesisUTXOs[i] = api.UTXO{
Amount: json.Uint64(50000 * units.MilliAvax),
Address: addr,
}
}

genesisValidators := make([]platformvm.APIPrimaryValidator, len(keys))
genesisValidators := make([]api.PrimaryValidator, len(keys))
for i, key := range keys {
id := key.PublicKey().Address()
addr, err := address.FormatBech32(hrp, id.Bytes())
gomega.Expect(err).Should(gomega.BeNil())
genesisValidators[i] = platformvm.APIPrimaryValidator{
APIStaker: platformvm.APIStaker{
genesisValidators[i] = api.PrimaryValidator{
Staker: api.Staker{
StartTime: json.Uint64(time.Date(1997, 1, 1, 0, 0, 0, 0, time.UTC).Unix()),
EndTime: json.Uint64(time.Date(1997, 1, 30, 0, 0, 0, 0, time.UTC).Unix()),
NodeID: ids.NodeID(id),
},
RewardOwner: &platformvm.APIOwner{
RewardOwner: &api.Owner{
Threshold: 1,
Addresses: []string{addr},
},
Staked: []platformvm.APIUTXO{{
Staked: []api.UTXO{{
Amount: json.Uint64(10000),
Address: addr,
}},
DelegationFee: reward.PercentDenominator,
}
}

buildGenesisArgs := platformvm.BuildGenesisArgs{
buildGenesisArgs := api.BuildGenesisArgs{
NetworkID: json.Uint32(constants.UnitTestID),
AvaxAssetID: ids.ID{'a', 'v', 'a', 'x'},
UTXOs: genesisUTXOs,
Expand All @@ -180,7 +180,7 @@ var _ = ginkgo.Describe("[StaticHandlers]", func() {
uris := e2e.GetURIs()
gomega.Expect(uris).ShouldNot(gomega.BeEmpty())

staticClient := platformvm.NewStaticClient(uris[0])
staticClient := api.NewStaticClient(uris[0])
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
resp, err := staticClient.BuildGenesis(ctx, &buildGenesisArgs)
cancel()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package platformvm
package api

import (
"context"
Expand All @@ -13,7 +13,11 @@ var _ StaticClient = &staticClient{}

// StaticClient for interacting with the platformvm static api
type StaticClient interface {
BuildGenesis(ctx context.Context, args *BuildGenesisArgs, options ...rpc.Option) (*BuildGenesisReply, error)
BuildGenesis(
ctx context.Context,
args *BuildGenesisArgs,
options ...rpc.Option,
) (*BuildGenesisReply, error)
}

// staticClient is an implementation of a platformvm client for interacting with
Expand All @@ -30,7 +34,11 @@ func NewStaticClient(uri string) StaticClient {
)}
}

func (c *staticClient) BuildGenesis(ctx context.Context, args *BuildGenesisArgs, options ...rpc.Option) (resp *BuildGenesisReply, err error) {
func (c *staticClient) BuildGenesis(
ctx context.Context,
args *BuildGenesisArgs,
options ...rpc.Option,
) (resp *BuildGenesisReply, err error) {
resp = &BuildGenesisReply{}
err = c.requester.SendRequest(ctx, "buildGenesis", args, resp, options...)
return resp, err
Expand Down
Loading

0 comments on commit cfc1ff0

Please sign in to comment.