Skip to content

Commit

Permalink
Genesis poa (#2164)
Browse files Browse the repository at this point in the history
* prompt addresses

* fix lint

* fix lint

* fix lint

* fix lint

* add flags

* adding teleporter contract to genesis

* validator prompt

* bootstrap validators

* add mock

* fix lint

* update prompt validator

* update prompt validator

* fixes for icm on genesis

* add icm from a given embedded file

* add function to deploy ICM registry

* creating mapping values

* add consts

* make local deploy to work

* do not print info for icm support addrs

* nit

* implement prompts

* fix prompt mock

* update sidecar

* update prompts

* update prompts

* refactor

* nit

* update prompts

* fix wiz stuff

* update prompts

* use default balance and weight

* generate new node ids and bls

* fix lint

* add flags

* add flags

* add a function to initialize PoA manager

* add lib folder

* address comments

* address comments

* address comments

* move validator prompt to deploy

* address comments

* address comments

* fix lint

* address comments

* address comments

* fix test

* address PR comments

* basic PoA manager setting

* lint

* address comments

* fix lint

* fix e2e

* fix merge

* fix merge

* filter based on balance

* add unit tests

* nit

* address PR comments

* Update cmd/blockchaincmd/describe.go

Co-authored-by: Michael Kaplan <[email protected]>
Signed-off-by: felipemadero <[email protected]>

* nit

* address PR comments

* lint

---------

Signed-off-by: sukantoraymond <[email protected]>
Signed-off-by: felipemadero <[email protected]>
Co-authored-by: Raymond Sukanto <[email protected]>
Co-authored-by: Michael Kaplan <[email protected]>
  • Loading branch information
3 people authored Sep 26, 2024
1 parent ad16a88 commit 93b24c2
Show file tree
Hide file tree
Showing 15 changed files with 313 additions and 127 deletions.
42 changes: 31 additions & 11 deletions cmd/blockchaincmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type CreateFlags struct {
addICMRegistryToGenesis bool
proofOfStake bool
proofOfAuthority bool
poaValidatorManagerOwner string
}

var (
Expand Down Expand Up @@ -107,6 +108,7 @@ configuration, pass the -f flag.`,
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")
cmd.Flags().StringVar(&createFlags.poaValidatorManagerOwner, "poa-manager-owner", "", "EVM address that controls Validator Manager Owner (for Proof of Authority only)")
return cmd
}

Expand Down Expand Up @@ -204,7 +206,6 @@ func createBlockchainConfig(cmd *cobra.Command, args []string) error {

var (
genesisBytes []byte
sc *models.Sidecar
useTeleporterFlag *bool
deployTeleporter bool
useExternalGasToken bool
Expand All @@ -222,7 +223,28 @@ func createBlockchainConfig(cmd *cobra.Command, args []string) error {
return err
}

sc := &models.Sidecar{}

if err = promptValidatorManagementType(app, sc); err != nil {
return err
}

if !sc.PoA() && createFlags.poaValidatorManagerOwner != "" {
return errors.New("--poa-manager-owner flag cannot be used when blockchain validator management type is not Proof of Authority")
}

if vmType == models.SubnetEvm {
if sc.PoA() {
if createFlags.poaValidatorManagerOwner == "" {
createFlags.poaValidatorManagerOwner, err = getValidatorContractManagerAddr()
if err != nil {
return err
}
}
sc.PoAValidatorManagerOwner = createFlags.poaValidatorManagerOwner
ux.Logger.GreenCheckmarkToUser("Validator Manager Contract owner address %s", createFlags.poaValidatorManagerOwner)
}

if genesisFile == "" {
// Default
defaultsKind, err = vm.PromptDefaults(app, defaultsKind)
Expand Down Expand Up @@ -272,6 +294,7 @@ func createBlockchainConfig(cmd *cobra.Command, args []string) error {
var params vm.SubnetEVMGenesisParams
params, tokenSymbol, err = vm.PromptSubnetEVMGenesisParams(
app,
sc,
vmVersion,
createFlags.chainID,
createFlags.tokenSymbol,
Expand All @@ -287,7 +310,6 @@ func createBlockchainConfig(cmd *cobra.Command, args []string) error {
deployTeleporter = params.UseTeleporter
useExternalGasToken = params.UseExternalGasToken
genesisBytes, err = vm.CreateEVMGenesis(
blockchainName,
params,
teleporterInfo,
createFlags.addICMRegistryToGenesis,
Expand All @@ -296,14 +318,14 @@ func createBlockchainConfig(cmd *cobra.Command, args []string) error {
return err
}
}
sc, err = vm.CreateEvmSidecar(
if sc, err = vm.CreateEvmSidecar(
sc,
app,
blockchainName,
vmVersion,
tokenSymbol,
true,
)
if err != nil {
); err != nil {
return err
}
} else {
Expand All @@ -322,7 +344,8 @@ func createBlockchainConfig(cmd *cobra.Command, args []string) error {
return err
}
}
sc, err = vm.CreateCustomSidecar(
if sc, err = vm.CreateCustomSidecar(
sc,
app,
blockchainName,
useRepo,
Expand All @@ -331,8 +354,7 @@ func createBlockchainConfig(cmd *cobra.Command, args []string) error {
customVMBuildScript,
vmFile,
tokenSymbol,
)
if err != nil {
); err != nil {
return err
}
}
Expand All @@ -355,9 +377,6 @@ func createBlockchainConfig(cmd *cobra.Command, args []string) error {
}
}
}
if err = promptValidatorManagementType(app, sc); err != nil {
return err
}

if err = app.WriteGenesisFile(blockchainName, genesisBytes); err != nil {
return err
Expand All @@ -373,6 +392,7 @@ func createBlockchainConfig(cmd *cobra.Command, args []string) error {
}
}
ux.Logger.GreenCheckmarkToUser("Successfully created blockchain configuration")
ux.Logger.PrintToUser("Run 'avalanche blockchain describe' to view all created addresses and what their roles are")
return nil
}

Expand Down
48 changes: 35 additions & 13 deletions cmd/blockchaincmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/ava-labs/avalanche-cli/pkg/txutils"
"github.com/ava-labs/avalanche-cli/pkg/utils"
"github.com/ava-labs/avalanche-cli/pkg/ux"
"github.com/ava-labs/avalanche-cli/pkg/validatormanager"
"github.com/ava-labs/avalanche-cli/pkg/vm"
anr_utils "github.com/ava-labs/avalanche-network-runner/utils"
"github.com/ava-labs/avalanchego/ids"
Expand Down Expand Up @@ -107,6 +108,7 @@ func PrintSubnetInfo(blockchainName string, onlyLocalnetInfo bool) error {
}
t.AppendRow(table.Row{"VM ID", vmIDstr, vmIDstr}, rowConfig)
t.AppendRow(table.Row{"VM Version", sc.VMVersion, sc.VMVersion}, rowConfig)
t.AppendRow(table.Row{"Validation", sc.ValidatorManagement, sc.ValidatorManagement}, rowConfig)

locallyDeployed, err := localnet.Deployed(sc.Name)
if err != nil {
Expand Down Expand Up @@ -217,7 +219,7 @@ func PrintSubnetInfo(blockchainName string, onlyLocalnetInfo bool) error {
if err := printAllocations(sc, genesis); err != nil {
return err
}
printSmartContracts(genesis)
printSmartContracts(sc, genesis)
printPrecompiles(genesis)
}

Expand Down Expand Up @@ -256,16 +258,14 @@ func PrintSubnetInfo(blockchainName string, onlyLocalnetInfo bool) error {

func printAllocations(sc models.Sidecar, genesis core.Genesis) error {
teleporterKeyAddress := ""
teleporterPrivKey := ""
if sc.TeleporterReady {
k, err := key.LoadSoft(models.NewLocalNetwork().ID, app.GetKeyPath(sc.TeleporterKey))
if err != nil {
return err
}
teleporterKeyAddress = k.C()
teleporterPrivKey = k.PrivKeyHex()
}
subnetAirdropKeyName, subnetAirdropAddress, subnetAirdropPrivKey, err := subnet.GetDefaultSubnetAirdropKeyInfo(app, sc.Name)
_, subnetAirdropAddress, _, err := subnet.GetDefaultSubnetAirdropKeyInfo(app, sc.Name)
if err != nil {
return err
}
Expand All @@ -276,7 +276,12 @@ func printAllocations(sc models.Sidecar, genesis core.Genesis) error {
t.Style().Title.Format = text.FormatUpper
t.Style().Options.SeparateRows = true
t.SetTitle("Initial Token Allocation")
t.AppendHeader(table.Row{"Description", "Address and Private Key", "Amount (10^18)", "Amount (wei)"})
t.AppendHeader(table.Row{
"Description",
"Address and Private Key",
fmt.Sprintf("Amount (%s)", sc.TokenSymbol),
"Amount (wei)",
})
for address, allocation := range genesis.Alloc {
amount := allocation.Balance
// we are only interested in supply distribution here
Expand All @@ -288,14 +293,24 @@ func printAllocations(sc models.Sidecar, genesis core.Genesis) error {
privKey := ""
switch address.Hex() {
case teleporterKeyAddress:
description = fmt.Sprintf("%s\n%s", sc.TeleporterKey, logging.Orange.Wrap("Teleporter Deploys"))
privKey = teleporterPrivKey
description = logging.Orange.Wrap("Used by ICM")
case subnetAirdropAddress:
description = fmt.Sprintf("%s\n%s", subnetAirdropKeyName, logging.Orange.Wrap("Main funded account"))
privKey = subnetAirdropPrivKey
description = logging.Orange.Wrap("Main funded account")
case vm.PrefundedEwoqAddress.Hex():
description = "Main funded account EWOQ"
privKey = vm.PrefundedEwoqPrivate
description = logging.Orange.Wrap("Main funded account")
case sc.PoAValidatorManagerOwner:
description = logging.Orange.Wrap("PoA Validator Manager Owner")
}
var (
found bool
name string
)
found, name, _, privKey, err = contract.SearchForManagedKey(app, models.NewLocalNetwork(), address, true)
if err != nil {
return err
}
if found {
description = fmt.Sprintf("%s\n%s", description, name)
}
t.AppendRow(table.Row{description, address.Hex() + "\n" + privKey, formattedAmount.String(), amount.String()})
}
Expand All @@ -304,7 +319,7 @@ func printAllocations(sc models.Sidecar, genesis core.Genesis) error {
return nil
}

func printSmartContracts(genesis core.Genesis) {
func printSmartContracts(sc models.Sidecar, genesis core.Genesis) {
if len(genesis.Alloc) == 0 {
return
}
Expand All @@ -320,9 +335,16 @@ func printSmartContracts(genesis core.Genesis) {
continue
}
var description, deployer string
if address == common.HexToAddress(icmgenesis.MessengerContractAddress) {
switch {
case address == common.HexToAddress(icmgenesis.MessengerContractAddress):
description = "ICM Messenger"
deployer = icmgenesis.MessengerDeployerAddress
case address == common.HexToAddress(validatormanager.ValidatorContractAddress):
if sc.PoA() {
description = "PoA Validator Manager"
} else {
description = "PoS Validator Manager"
}
}
t.AppendRow(table.Row{description, address.Hex(), deployer})
}
Expand Down
1 change: 1 addition & 0 deletions cmd/blockchaincmd/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TestExportImportSubnet(t *testing.T) {
)
require.NoError(err)
sc, err := vm.CreateEvmSidecar(
nil,
app,
testSubnet,
vmVersion,
Expand Down
14 changes: 14 additions & 0 deletions cmd/blockchaincmd/prompt_genesis_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ import (
"github.com/ava-labs/avalanchego/vms/platformvm/signer"
)

func getValidatorContractManagerAddr() (string, error) {
return prompts.PromptAddress(
app.Prompt,
"enable as controller of ValidatorManager contract",
app.GetKeyDir(),
app.GetKey,
"",
models.UndefinedNetwork,
prompts.EVMFormat,
"Enter address",
)
}

func promptProofOfPossession() (string, string, error) {
ux.Logger.PrintToUser("Next, we need the public key and proof of possession of the node's BLS")
ux.Logger.PrintToUser("Check https://docs.avax.network/api-reference/info-api#infogetnodeid for instructions on calling info.getNodeID API")
Expand Down Expand Up @@ -49,6 +62,7 @@ func promptValidatorManagementType(
sidecar.ValidatorManagement = models.ProofOfAuthority
return nil
}

options := []string{models.ProofOfAuthority, models.ProofOfStake, explainOption}
for {
option, err := app.Prompt.CaptureList(
Expand Down
2 changes: 1 addition & 1 deletion cmd/blockchaincmd/upgradecmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ func promptAdminManagerAndEnabledAddresses(
sc *models.Sidecar,
action string,
) ([]common.Address, []common.Address, []common.Address, bool, error) {
allowList, cancelled, err := vm.GenerateAllowList(app, action, sc.VMVersion)
allowList, cancelled, err := vm.GenerateAllowList(app, vm.AllowList{}, action, sc.VMVersion)
if cancelled || err != nil {
return nil, nil, nil, cancelled, err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/contract/allocations.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func GetBlockchainAirdropKeyInfo(
}
}
for address := range genesis.Alloc {
found, keyName, addressStr, privKey, err := searchForManagedKey(app, network, address, false)
found, keyName, addressStr, privKey, err := SearchForManagedKey(app, network, address, false)
if err != nil {
return "", "", "", err
}
Expand All @@ -82,7 +82,7 @@ func GetBlockchainAirdropKeyInfo(
return "", "", "", nil
}

func searchForManagedKey(
func SearchForManagedKey(
app *application.Avalanche,
network models.Network,
address common.Address,
Expand Down Expand Up @@ -204,7 +204,7 @@ func getGenesisNativeMinterAdmin(
return false, false, "", "", "", nil
}
for _, admin := range allowListCfg.AllowListConfig.AdminAddresses {
found, keyName, addressStr, privKey, err := searchForManagedKey(app, network, admin, true)
found, keyName, addressStr, privKey, err := SearchForManagedKey(app, network, admin, true)
if err != nil {
return false, false, "", "", "", err
}
Expand Down Expand Up @@ -238,7 +238,7 @@ func getGenesisNativeMinterManager(
return false, false, "", "", "", nil
}
for _, admin := range allowListCfg.AllowListConfig.ManagerAddresses {
found, keyName, addressStr, privKey, err := searchForManagedKey(app, network, admin, true)
found, keyName, addressStr, privKey, err := SearchForManagedKey(app, network, admin, true)
if err != nil {
return false, false, "", "", "", err
}
Expand Down
11 changes: 10 additions & 1 deletion pkg/models/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ type Sidecar struct {
// SubnetEVM based VM's only
SubnetEVMMainnetChainID uint
// TODO: remove if not needed for subnet acp 77 create flow once avalnache go releases etna
ValidatorManagement ValidatorManagementType
ValidatorManagement ValidatorManagementType
PoAValidatorManagerOwner string
}

func (sc Sidecar) GetVMID() (string, error) {
Expand All @@ -65,3 +66,11 @@ func (sc Sidecar) NetworkDataIsEmpty(network string) bool {
_, networkExists := sc.Networks[network]
return !networkExists
}

func (sc Sidecar) PoA() bool {
return sc.ValidatorManagement == ProofOfAuthority
}

func (sc Sidecar) PoS() bool {
return sc.ValidatorManagement == ProofOfStake
}

Large diffs are not rendered by default.

Loading

0 comments on commit 93b24c2

Please sign in to comment.