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

Check for non empty genesis #40

Merged
merged 11 commits into from
Jun 5, 2024
Merged
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/ethereum/go-ethereum v1.13.2
go.uber.org/zap v1.27.0
golang.org/x/exp v0.0.0-20231127185646-65229373498e
golang.org/x/net v0.23.0
)

require (
Expand Down Expand Up @@ -115,7 +116,6 @@ require (
go.uber.org/mock v0.4.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
Expand Down
5 changes: 1 addition & 4 deletions subnet/add_validator_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@ import (
func TestAddValidatorDeploy(_ *testing.T) {
// Initialize a new Avalanche Object which will be used to set shared properties
// like logging, metrics preferences, etc
baseApp := avalanche.New(avalanche.DefaultLeveledLogger)
subnetParams := SubnetParams{
SubnetEVM: &SubnetEVMParams{
EvmChainID: 1234567,
EvmDefaults: true,
EnableWarp: true,
EnableTeleporter: true,
EnableRelayer: true,
},
}
newSubnet, _ := New(baseApp, &subnetParams)
newSubnet, _ := New(&subnetParams)
ctx := context.Background()
wallet, _ := wallet.New(
ctx,
Expand Down
65 changes: 26 additions & 39 deletions subnet/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"os"
"time"

"github.com/ava-labs/avalanche-tooling-sdk-go/avalanche"
"github.com/ava-labs/avalanche-tooling-sdk-go/key"
"github.com/ava-labs/subnet-evm/precompile/contracts/warp"

"github.com/ava-labs/avalanche-tooling-sdk-go/teleporter"
"github.com/ava-labs/avalanche-tooling-sdk-go/vm"

Expand All @@ -23,13 +23,12 @@ import (
"github.com/ava-labs/subnet-evm/core"
"github.com/ava-labs/subnet-evm/params"
"github.com/ava-labs/subnet-evm/precompile/contracts/txallowlist"
"github.com/ava-labs/subnet-evm/precompile/contracts/warp"
"github.com/ethereum/go-ethereum/common"
)

type SubnetParams struct {
// File path of Genesis to use
// Do not set EvmChainID, EvmToken and EvmDefaults values in SubnetEVM
// Do not set SubnetEVMParams or CustomVMParams
// if GenesisFilePath value is set

// See https://docs.avax.network/build/subnet/upgrade/customize-a-subnet#genesis for
Expand All @@ -48,12 +47,6 @@ type SubnetParams struct {
}

type SubnetEVMParams struct {
// Chain ID to use in Subnet-EVM
EvmChainID uint64

// Use default settings for fees, airdrop, precompiles and teleporter in Subnet-EVM
EvmDefaults bool

// Enable Avalanche Warp Messaging (AWM) when deploying a VM

// See https://docs.avax.network/build/cross-chain/awm/overview for
Expand Down Expand Up @@ -107,8 +100,6 @@ type Subnet struct {
DeployInfo DeployParams

RPCVersion int

Logger avalanche.LeveledLoggerInterface
}

type DeployParams struct {
Expand All @@ -122,29 +113,35 @@ type DeployParams struct {
}

type EVMGenesisParams struct {
// Chain ID to use in Subnet-EVM
ChainID *big.Int
FeeConfig commontype.FeeConfig
Allocation core.GenesisAlloc
Precompiles params.Precompiles
TeleporterInfo *teleporter.Info
AllocationKey *key.SoftKey
}

func New(client *avalanche.BaseApp, subnetParams *SubnetParams) (*Subnet, error) {
func New(subnetParams *SubnetParams) (*Subnet, error) {
if subnetParams.GenesisFilePath != "" && (subnetParams.CustomVM != nil || subnetParams.SubnetEVM != nil) {
return nil, fmt.Errorf("genesis file path cannot be non-empty if either CustomVM params or SubnetEVM params is not empty")
}
if subnetParams.SubnetEVM == nil && subnetParams.CustomVM != nil {
return nil, fmt.Errorf("SubnetEVM params and CustomVM params cannot both be non-empty")
}
if subnetParams.SubnetEVM != nil {
if subnetParams.SubnetEVM.GenesisParams == nil {
return nil, fmt.Errorf("SubnetEVM Genesis params cannot be empty")
}
}
var genesisBytes []byte
var err error
switch {
case subnetParams.GenesisFilePath != "":
genesisBytes, err = os.ReadFile(subnetParams.GenesisFilePath)
case subnetParams.SubnetEVM != nil:
genesisBytes, err = createEvmGenesis(
subnetParams.SubnetEVM.EvmChainID,
subnetParams.SubnetEVM.GenesisParams,
subnetParams.SubnetEVM.EnableWarp,
)
case subnetParams.CustomVM != nil:
genesisBytes, err = createCustomVMGenesis()
Expand All @@ -155,15 +152,13 @@ func New(client *avalanche.BaseApp, subnetParams *SubnetParams) (*Subnet, error)
}
subnet := Subnet{
Genesis: genesisBytes,
Logger: client.Logger,
}
return &subnet, nil
}

// removed usewarp from argument, to use warp add it manualluy to precompile
func createEvmGenesis(
chainID uint64,
genesisParams *EVMGenesisParams,
useWarp bool,
) ([]byte, error) {
genesis := core.Genesis{}
genesis.Timestamp = *utils.TimeToNewUint64(time.Now())
Expand All @@ -173,31 +168,33 @@ func createEvmGenesis(

var err error

if genesisParams.ChainID == nil {
return nil, fmt.Errorf("genesis params chain ID cannot be empty")
}

if genesisParams.FeeConfig == commontype.EmptyFeeConfig {
conf.FeeConfig = vm.StarterFeeConfig
} else {
conf.FeeConfig = genesisParams.FeeConfig
return nil, fmt.Errorf("genesis params fee config cannot be empty")
}
allocation := core.GenesisAlloc{}

if genesisParams.Allocation == nil {
allocation, err = getNewAllocation(vm.DefaultEvmAirdropAmount, genesisParams.AllocationKey)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("genesis params allocation cannot be empty")
}

allocation := genesisParams.Allocation
if genesisParams.TeleporterInfo != nil {
allocation = addTeleporterAddressToAllocations(
allocation,
genesisParams.TeleporterInfo.FundedAddress,
genesisParams.TeleporterInfo.FundedBalance,
)
}

if genesisParams.Precompiles == nil {
return nil, fmt.Errorf("genesis params precompiles cannot be empty")
}
if useWarp {
warpConfig := vm.ConfigureWarp(&genesis.Timestamp)
conf.GenesisPrecompiles[warp.ConfigKey] = &warpConfig
}

if genesisParams.TeleporterInfo != nil {
*conf = vm.AddTeleporterAddressesToAllowLists(
*conf,
Expand All @@ -223,7 +220,7 @@ func createEvmGenesis(
}
}

conf.ChainID = new(big.Int).SetUint64(chainID)
conf.ChainID = genesisParams.ChainID

genesis.Alloc = allocation
genesis.Config = conf
Expand Down Expand Up @@ -262,16 +259,6 @@ func ensureAdminsHaveBalance(admins []common.Address, alloc core.GenesisAlloc) e
)
}

func getNewAllocation(defaultAirdropAmount string, key *key.SoftKey) (core.GenesisAlloc, error) {
allocation := core.GenesisAlloc{}
defaultAmount, ok := new(big.Int).SetString(defaultAirdropAmount, 10)
if !ok {
return allocation, errors.New("unable to decode default allocation")
}
addAllocation(allocation, key.C(), defaultAmount)
return allocation, nil
}

func addAllocation(alloc core.GenesisAlloc, address string, amount *big.Int) {
alloc[common.HexToAddress(address)] = core.GenesisAccount{
Balance: amount,
Expand Down
6 changes: 1 addition & 5 deletions subnet/subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"testing"

"github.com/ava-labs/avalanche-tooling-sdk-go/avalanche"
"github.com/ava-labs/avalanche-tooling-sdk-go/wallet"

"github.com/ava-labs/avalanchego/vms/secp256k1fx"
Expand All @@ -18,17 +17,14 @@ import (
func TestSubnetDeploy(_ *testing.T) {
// Initialize a new Avalanche Object which will be used to set shared properties
// like logging, metrics preferences, etc
baseApp := avalanche.New(avalanche.DefaultLeveledLogger)
subnetParams := SubnetParams{
SubnetEVM: &SubnetEVMParams{
EvmChainID: 1234567,
EvmDefaults: true,
EnableWarp: true,
EnableTeleporter: true,
EnableRelayer: true,
},
}
newSubnet, _ := New(baseApp, &subnetParams)
newSubnet, _ := New(&subnetParams)
ctx := context.Background()
wallet, _ := wallet.New(
ctx,
Expand Down
Loading