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

Genesis icm #2154

Open
wants to merge 15 commits into
base: acp-77
Choose a base branch
from
3 changes: 3 additions & 0 deletions cmd/blockchaincmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type CreateFlags struct {
useLatestReleasedVMVersion bool
useLatestPreReleasedVMVersion bool
useExternalGasToken bool
addICMRegistryToGenesis bool
proofOfStake bool
proofOfAuthority bool
}
Expand Down Expand Up @@ -106,6 +107,7 @@ configuration, pass the -f flag.`,
cmd.Flags().BoolVar(&createFlags.useWarp, "warp", true, "generate a vm with warp support (needed for teleporter)")
cmd.Flags().BoolVar(&createFlags.useTeleporter, "teleporter", false, "interoperate with other blockchains using teleporter")
cmd.Flags().BoolVar(&createFlags.useExternalGasToken, "external-gas-token", false, "use a gas token from another blockchain")
cmd.Flags().BoolVar(&createFlags.addICMRegistryToGenesis, "icm-registry-at-genesis", false, "setup ICM registry smart contract on genesis [experimental]")
cmd.Flags().BoolVar(&createFlags.proofOfAuthority, "proof-of-authority", false, "use proof of authority for validator management")
cmd.Flags().BoolVar(&createFlags.proofOfStake, "proof-of-stake", false, "(coming soon) use proof of stake for validator management")
return cmd
Expand Down Expand Up @@ -291,6 +293,7 @@ func createBlockchainConfig(cmd *cobra.Command, args []string) error {
blockchainName,
params,
teleporterInfo,
createFlags.addICMRegistryToGenesis,
)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion cmd/blockchaincmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ so you can take your locally tested Subnet and deploy it on Fuji or Mainnet.`,
cmd.Flags().BoolVar(&icmSpec.SkipICMDeploy, "skip-local-teleporter", false, "skip automatic teleporter deploy on local networks [to be deprecated]")
cmd.Flags().BoolVar(&icmSpec.SkipICMDeploy, "skip-teleporter-deploy", false, "skip automatic teleporter deploy")
cmd.Flags().BoolVar(&icmSpec.SkipRelayerDeploy, "skip-relayer", false, "skip relayer deploy")
cmd.Flags().StringVar(&icmSpec.Version, "teleporter-version", "latest", "teleporter version to deploy")
cmd.Flags().StringVar(&icmSpec.ICMVersion, "teleporter-version", "latest", "teleporter version to deploy")
cmd.Flags().StringVar(&icmSpec.RelayerVersion, "relayer-version", "latest", "relayer version to deploy")
cmd.Flags().StringVar(&icmSpec.MessengerContractAddressPath, "teleporter-messenger-contract-address-path", "", "path to an interchain messenger contract address file")
cmd.Flags().StringVar(&icmSpec.MessengerDeployerAddressPath, "teleporter-messenger-deployer-address-path", "", "path to an interchain messenger deployer address file")
cmd.Flags().StringVar(&icmSpec.MessengerDeployerTxPath, "teleporter-messenger-deployer-tx-path", "", "path to an interchain messenger deployer tx file")
Expand Down
11 changes: 9 additions & 2 deletions cmd/blockchaincmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/ava-labs/avalanche-cli/pkg/models"
"github.com/ava-labs/avalanche-cli/pkg/networkoptions"
"github.com/ava-labs/avalanche-cli/pkg/subnet"
icmgenesis "github.com/ava-labs/avalanche-cli/pkg/teleporter/genesis"
"github.com/ava-labs/avalanche-cli/pkg/txutils"
"github.com/ava-labs/avalanche-cli/pkg/utils"
"github.com/ava-labs/avalanche-cli/pkg/ux"
Expand Down Expand Up @@ -275,8 +276,14 @@ func printAllocations(sc models.Sidecar, genesis core.Genesis) error {
t.Style().Options.SeparateRows = true
t.SetTitle("Initial Token Allocation")
t.AppendHeader(table.Row{"Description", "Address and Private Key", "Amount (10^18)", "Amount (wei)"})
for address := range genesis.Alloc {
amount := genesis.Alloc[address].Balance
for address, allocation := range genesis.Alloc {
if len(allocation.Code) > 0 {
continue
}
if address == common.HexToAddress(icmgenesis.MessengerDeployerAddress) {
continue
}
Comment on lines +280 to +285
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we skip the deployer address and any allocation with code here? Could be helpful to add a comment explaining

amount := allocation.Balance
formattedAmount := new(big.Int).Div(amount, big.NewInt(params.Ether))
description := ""
privKey := ""
Expand Down
1 change: 1 addition & 0 deletions cmd/nodecmd/wiz.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ func wiz(cmd *cobra.Command, args []string) error {
},
DeployMessenger: deployTeleporterMessenger,
DeployRegistry: deployTeleporterRegistry,
ForceRegistryDeploy: true,
Version: teleporterVersion,
MessengerContractAddressPath: teleporterMessengerContractAddressPath,
MessengerDeployerAddressPath: teleporterMessengerDeployerAddressPath,
Expand Down
4 changes: 4 additions & 0 deletions cmd/teleportercmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type DeployFlags struct {
GenesisKey bool
DeployMessenger bool
DeployRegistry bool
ForceRegistryDeploy bool
RPCURL string
Version string
MessengerContractAddressPath string
Expand Down Expand Up @@ -62,6 +63,7 @@ func newDeployCmd() *cobra.Command {
deployFlags.ChainFlags.AddToCmd(cmd, "deploy ICM", true)
cmd.Flags().BoolVar(&deployFlags.DeployMessenger, "deploy-messenger", true, "deploy Teleporter Messenger")
cmd.Flags().BoolVar(&deployFlags.DeployRegistry, "deploy-registry", true, "deploy Teleporter Registry")
cmd.Flags().BoolVar(&deployFlags.ForceRegistryDeploy, "force-registry-deploy", false, "deploy Teleporter Registry even if Messenger has already been deployed")
cmd.Flags().StringVar(&deployFlags.RPCURL, "rpc-url", "", "use the given RPC URL to connect to the subnet")
cmd.Flags().StringVar(&deployFlags.Version, "version", "latest", "version to deploy")
cmd.Flags().StringVar(&deployFlags.MessengerContractAddressPath, "messenger-contract-address-path", "", "path to a messenger contract address file")
Expand Down Expand Up @@ -188,6 +190,7 @@ func CallDeploy(_ []string, flags DeployFlags) error {
privateKey,
flags.DeployMessenger,
flags.DeployRegistry,
flags.ForceRegistryDeploy,
)
if err != nil {
return err
Expand Down Expand Up @@ -224,6 +227,7 @@ func CallDeploy(_ []string, flags DeployFlags) error {
ewoq.PrivKeyHex(),
flags.DeployMessenger,
flags.DeployRegistry,
false,
)
if err != nil {
return err
Expand Down
36 changes: 36 additions & 0 deletions pkg/contract/allocations.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,39 @@ func GetEVMSubnetGenesisNativeMinterAdmin(
}
return getGenesisNativeMinterAdmin(app, network, genesisData)
}

func ContractAddressIsInBlockchainGenesis(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we add unit tests for these two new functions?

app *application.Avalanche,
network models.Network,
chainSpec ChainSpec,
contractAddress common.Address,
) (bool, error) {
genesisData, err := GetBlockchainGenesis(
app,
network,
chainSpec,
)
if err != nil {
return false, err
}
if !utils.ByteSliceIsSubnetEvmGenesis(genesisData) {
felipemadero marked this conversation as resolved.
Show resolved Hide resolved
return false, fmt.Errorf("only EVM based vms support genesis contract checks")
}
return ContractAddressIsInGenesisData(genesisData, contractAddress)
}

func ContractAddressIsInGenesisData(
genesisData []byte,
contractAddress common.Address,
) (bool, error) {
genesis, err := utils.ByteSliceToSubnetEvmGenesis(genesisData)
if err != nil {
return false, err
}
for address, allocation := range genesis.Alloc {
if address == contractAddress {
return len(allocation.Code) > 0, nil
}
}
return false, nil
}
91 changes: 62 additions & 29 deletions pkg/subnet/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/ava-labs/avalanche-cli/pkg/localnet"
"github.com/ava-labs/avalanche-cli/pkg/models"
"github.com/ava-labs/avalanche-cli/pkg/teleporter"
icmgenesis "github.com/ava-labs/avalanche-cli/pkg/teleporter/genesis"
"github.com/ava-labs/avalanche-cli/pkg/utils"
"github.com/ava-labs/avalanche-cli/pkg/ux"
"github.com/ava-labs/avalanche-network-runner/client"
Expand Down Expand Up @@ -81,11 +82,12 @@ type setDefaultSnapshotFunc func(string, bool, string, bool) (bool, error)
type ICMSpec struct {
SkipICMDeploy bool
SkipRelayerDeploy bool
Version string
ICMVersion string
MessengerContractAddressPath string
MessengerDeployerAddressPath string
MessengerDeployerTxPath string
RegistryBydecodePath string
RelayerVersion string
}

type DeployInfo struct {
Expand All @@ -99,15 +101,15 @@ type DeployInfo struct {
// * it checks the gRPC is running, if not, it starts it
// * kicks off the actual deployment
func (d *LocalDeployer) DeployToLocalNetwork(
chain string,
blockchainName string,
genesisPath string,
icmSpec ICMSpec,
subnetIDStr string,
) (*DeployInfo, error) {
if err := d.StartServer(); err != nil {
return nil, err
}
return d.doDeploy(chain, genesisPath, icmSpec, subnetIDStr)
return d.doDeploy(blockchainName, genesisPath, icmSpec, subnetIDStr)
}

func (d *LocalDeployer) StartServer() error {
Expand Down Expand Up @@ -151,7 +153,7 @@ func (d *LocalDeployer) BackendStartedHere() bool {
// - deploy a new blockchain for the given VM ID, genesis, and available subnet ID
// - waits completion of operation
// - show status
func (d *LocalDeployer) doDeploy(chain string, genesisPath string, icmSpec ICMSpec, subnetIDStr string) (*DeployInfo, error) {
func (d *LocalDeployer) doDeploy(blockchainName string, genesisPath string, icmSpec ICMSpec, subnetIDStr string) (*DeployInfo, error) {
needsRestart, avalancheGoBinPath, err := d.SetupLocalEnv()
if err != nil {
return nil, err
Expand All @@ -174,7 +176,7 @@ func (d *LocalDeployer) doDeploy(chain string, genesisPath string, icmSpec ICMSp
defer cancel()

// loading sidecar before it's needed so we catch any error early
sc, err := d.app.LoadSidecar(chain)
sc, err := d.app.LoadSidecar(blockchainName)
if err != nil {
return nil, fmt.Errorf("failed to load sidecar: %w", err)
}
Expand All @@ -192,9 +194,9 @@ func (d *LocalDeployer) doDeploy(chain string, genesisPath string, icmSpec ICMSp
}
}

chainVMID, err := anrutils.VMID(chain)
chainVMID, err := anrutils.VMID(blockchainName)
if err != nil {
return nil, fmt.Errorf("failed to create VM ID from %s: %w", chain, err)
return nil, fmt.Errorf("failed to create VM ID from %s: %w", blockchainName, err)
}
d.app.Log.Debug("this VM will get ID", zap.String("vm-id", chainVMID.String()))

Expand Down Expand Up @@ -253,7 +255,7 @@ func (d *LocalDeployer) doDeploy(chain string, genesisPath string, icmSpec ICMSp
logRootDir = clusterInfo.GetLogRootDir()

if alreadyDeployed(chainVMID, clusterInfo) {
return nil, fmt.Errorf("subnet %s has already been deployed", chain)
return nil, fmt.Errorf("subnet %s has already been deployed", blockchainName)
}

numBlockchains := len(clusterInfo.CustomChains)
Expand All @@ -278,11 +280,11 @@ func (d *LocalDeployer) doDeploy(chain string, genesisPath string, icmSpec ICMSp
// if a chainConfig has been configured
var (
chainConfig string
chainConfigFile = filepath.Join(d.app.GetSubnetDir(), chain, constants.ChainConfigFileName)
chainConfigFile = filepath.Join(d.app.GetSubnetDir(), blockchainName, constants.ChainConfigFileName)
perNodeChainConfig string
perNodeChainConfigFile = filepath.Join(d.app.GetSubnetDir(), chain, constants.PerNodeChainConfigFileName)
perNodeChainConfigFile = filepath.Join(d.app.GetSubnetDir(), blockchainName, constants.PerNodeChainConfigFileName)
subnetConfig string
subnetConfigFile = filepath.Join(d.app.GetSubnetDir(), chain, constants.SubnetConfigFileName)
subnetConfigFile = filepath.Join(d.app.GetSubnetDir(), blockchainName, constants.SubnetConfigFileName)
)
if _, err := os.Stat(chainConfigFile); err == nil {
// currently the ANR only accepts the file as a path, not its content
Expand All @@ -307,14 +309,14 @@ func (d *LocalDeployer) doDeploy(chain string, genesisPath string, icmSpec ICMSp
// the given VM ID, genesis, and available subnet ID
blockchainSpecs := []*rpcpb.BlockchainSpec{
{
VmName: chain,
VmName: blockchainName,
Genesis: genesisPath,
SubnetId: &subnetIDStr,
SubnetSpec: &rpcpb.SubnetSpec{
SubnetConfig: subnetConfig,
},
ChainConfig: chainConfig,
BlockchainAlias: chain,
BlockchainAlias: blockchainName,
PerNodeChainConfig: perNodeChainConfig,
},
}
Expand Down Expand Up @@ -384,8 +386,8 @@ func (d *LocalDeployer) doDeploy(chain string, genesisPath string, icmSpec ICMSp
} else {
icmVersion := ""
switch {
case icmSpec.Version != "" && icmSpec.Version != "latest":
icmVersion = icmSpec.Version
case icmSpec.ICMVersion != "" && icmSpec.ICMVersion != "latest":
icmVersion = icmSpec.ICMVersion
case sc.TeleporterVersion != "":
icmVersion = sc.TeleporterVersion
default:
Expand All @@ -412,6 +414,7 @@ func (d *LocalDeployer) doDeploy(chain string, genesisPath string, icmSpec ICMSp
cChainKey.PrivKeyHex(),
true,
true,
false,
)
if err != nil {
return nil, err
Expand All @@ -423,35 +426,65 @@ func (d *LocalDeployer) doDeploy(chain string, genesisPath string, icmSpec ICMSp
}
// deploy current blockchain
ux.Logger.PrintToUser("")
subnetID, blockchainID, err := utils.GetChainIDs(network.Endpoint, chain)
subnetID, blockchainID, err := utils.GetChainIDs(network.Endpoint, blockchainName)
if err != nil {
return nil, err
}
teleporterKeyName := sc.TeleporterKey
if teleporterKeyName == "" {
genesisData, err := d.app.LoadRawGenesis(chain)
blockchainKeyName := sc.TeleporterKey
if blockchainKeyName == "" {
genesisData, err := d.app.LoadRawGenesis(blockchainName)
if err != nil {
return nil, err
}
teleporterKeyName, _, _, err = GetSubnetAirdropKeyInfo(d.app, network, chain, genesisData)
blockchainKeyName, _, _, err = GetSubnetAirdropKeyInfo(d.app, network, blockchainName, genesisData)
if err != nil {
return nil, err
}
}
blockchainKey, err := key.LoadSoft(network.ID, d.app.GetKeyPath(teleporterKeyName))
blockchainKey, err := key.LoadSoft(network.ID, d.app.GetKeyPath(blockchainKeyName))
if err != nil {
return nil, err
}
_, icmMessengerAddress, icmRegistryAddress, err = icmd.Deploy(
chain,
network.BlockchainEndpoint(blockchainID),
blockchainKey.PrivKeyHex(),
true,
true,
)
genesisData, err := os.ReadFile(genesisPath)
if err != nil {
return nil, err
}
messengerAtGenesis, registryAtGenesis, err := icmgenesis.ICMAtGenesis(genesisData)
if err != nil {
return nil, err
}
switch {
case registryAtGenesis:
ux.Logger.PrintToUser("Teleporter Messenger and Registry already included in %s's Genesis", blockchainName)
icmMessengerAddress = icmgenesis.MessengerContractAddress
icmRegistryAddress = icmgenesis.RegistryContractAddress
case messengerAtGenesis:
ux.Logger.PrintToUser("Teleporter Messenger already included in %s's Genesis", blockchainName)
icmMessengerAddress = icmgenesis.MessengerContractAddress
_, _, icmRegistryAddress, err = icmd.Deploy(
blockchainName,
network.BlockchainEndpoint(blockchainID),
blockchainKey.PrivKeyHex(),
false,
true,
false,
)
if err != nil {
return nil, err
}
default:
_, icmMessengerAddress, icmRegistryAddress, err = icmd.Deploy(
blockchainName,
network.BlockchainEndpoint(blockchainID),
blockchainKey.PrivKeyHex(),
true,
true,
false,
)
if err != nil {
return nil, err
}
}
if sc.RunRelayer && !icmSpec.SkipRelayerDeploy {
if !cchainAlreadyDeployed {
if err := teleporter.FundRelayer(
Expand Down Expand Up @@ -502,7 +535,7 @@ func (d *LocalDeployer) doDeploy(chain string, genesisPath string, icmSpec ICMSp
ux.Logger.PrintToUser("")
// start relayer
if err := teleporter.DeployRelayer(
"latest",
icmSpec.RelayerVersion,
d.app.GetAWMRelayerBinDir(),
relayerConfigPath,
d.app.GetLocalRelayerLogPath(models.Local),
Expand Down
1 change: 1 addition & 0 deletions pkg/teleporter/genesis/deployed_messenger_bytecode.txt

Large diffs are not rendered by default.

Loading
Loading