Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
felipemadero authored Jan 14, 2025
2 parents 97bcdca + 4aa2ac1 commit 50ace3f
Show file tree
Hide file tree
Showing 43 changed files with 510 additions and 447 deletions.
69 changes: 47 additions & 22 deletions cmd/blockchaincmd/add_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"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/sdk/validator"
"github.com/ava-labs/avalanchego/ids"
avagoconstants "github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/formatting/address"
Expand All @@ -37,16 +38,9 @@ import (
)

var (
addValidatorSupportedNetworkOptions = []networkoptions.NetworkOption{
networkoptions.Local,
networkoptions.Devnet,
networkoptions.Fuji,
networkoptions.Mainnet,
}

nodeIDStr string
nodeEndpoint string
balance uint64
balanceAVAX float64
weight uint64
startTimeStr string
duration time.Duration
Expand Down Expand Up @@ -92,11 +86,16 @@ Testnet or Mainnet.`,
RunE: addValidator,
Args: cobrautils.ExactArgs(1),
}
networkoptions.AddNetworkFlagsToCmd(cmd, &globalNetworkFlags, true, addValidatorSupportedNetworkOptions)
networkoptions.AddNetworkFlagsToCmd(cmd, &globalNetworkFlags, true, networkoptions.DefaultSupportedNetworkOptions)

cmd.Flags().StringVarP(&keyName, "key", "k", "", "select the key to use [fuji/devnet only]")
cmd.Flags().Uint64Var(&weight, "weight", constants.NonBootstrapValidatorWeight, "set the staking weight of the validator to add")
cmd.Flags().Uint64Var(&balance, "balance", 0, "set the AVAX balance of the validator that will be used for continuous fee on P-Chain")
cmd.Flags().Float64Var(
&balanceAVAX,
"balance",
0,
"set the AVAX balance of the validator that will be used for continuous fee on P-Chain",
)
cmd.Flags().BoolVarP(&useEwoq, "ewoq", "e", false, "use ewoq key [fuji/devnet only]")
cmd.Flags().BoolVarP(&useLedger, "ledger", "g", false, "use ledger instead of key (always true on mainnet, defaults to false on fuji/devnet)")
cmd.Flags().StringSliceVar(&ledgerAddresses, "ledger-addrs", []string{}, "use the given ledger addresses")
Expand Down Expand Up @@ -151,14 +150,13 @@ func addValidator(_ *cobra.Command, args []string) error {
return fmt.Errorf("failed to load sidecar: %w", err)
}

networkOptionsList := networkoptions.GetNetworkFromSidecar(sc, addValidatorSupportedNetworkOptions)
network, err := networkoptions.GetNetworkFromCmdLineFlags(
app,
"",
globalNetworkFlags,
true,
false,
networkOptionsList,
networkoptions.GetNetworkFromSidecar(sc, networkoptions.DefaultSupportedNetworkOptions),
"",
)
if err != nil {
Expand Down Expand Up @@ -219,6 +217,8 @@ func addValidator(_ *cobra.Command, args []string) error {
}
}

subnetID := sc.Networks[network.Name()].SubnetID

// if user chose to upsize a local node to add another local validator
if createLocalValidator {
anrSettings := node.ANRSettings{}
Expand All @@ -240,7 +240,6 @@ func addValidator(_ *cobra.Command, args []string) error {

nodeName := ""
blockchainID := sc.Networks[network.Name()].BlockchainID
subnetID := sc.Networks[network.Name()].SubnetID

if nodeName, err = node.UpsizeLocalNode(
app,
Expand Down Expand Up @@ -303,10 +302,23 @@ func addValidator(_ *cobra.Command, args []string) error {
if !sovereign {
return CallAddValidatorNonSOV(deployer, network, kc, useLedger, blockchainName, nodeIDStr, defaultValidatorParams, waitForTxAcceptance)
}
return CallAddValidator(deployer, network, kc, blockchainName, nodeIDStr, publicKey, pop)
return CallAddValidator(
deployer,
network,
kc,
blockchainName,
subnetID,
nodeIDStr,
publicKey,
pop,
weight,
balanceAVAX,
remainingBalanceOwnerAddr,
disableOwnerAddr,
)
}

func promptValidatorBalance(availableBalance uint64) (uint64, error) {
func promptValidatorBalanceAVAX(availableBalance float64) (float64, error) {
ux.Logger.PrintToUser("Validator's balance is used to pay for continuous fee to the P-Chain")
ux.Logger.PrintToUser("When this Balance reaches 0, the validator will be considered inactive and will no longer participate in validating the L1")
txt := "What balance would you like to assign to the validator (in AVAX)?"
Expand All @@ -318,9 +330,14 @@ func CallAddValidator(
network models.Network,
kc *keychain.Keychain,
blockchainName string,
subnetID ids.ID,
nodeIDStr string,
publicKey string,
pop string,
weight uint64,
balanceAVAX float64,
remainingBalanceOwnerAddr string,
disableOwnerAddr string,
) error {
nodeID, err := ids.NodeIDFromString(nodeIDStr)
if err != nil {
Expand Down Expand Up @@ -401,19 +418,27 @@ func CallAddValidator(

ux.Logger.PrintToUser(logging.Yellow.Wrap("RPC Endpoint: %s"), rpcURL)

if balance == 0 {
totalWeight, err := validator.GetTotalWeight(network.SDKNetwork(), subnetID)
if err != nil {
return err
}
allowedChange := float64(totalWeight) * constants.MaxL1TotalWeightChange
if float64(weight) > allowedChange {
return fmt.Errorf("can't make change: desired validator weight %d exceeds max allowed weight change of %d", newWeight, uint64(allowedChange))
}

if balanceAVAX == 0 {
availableBalance, err := utils.GetNetworkBalance(kc.Addresses().List(), network.Endpoint)
if err != nil {
return err
}
balance, err = promptValidatorBalance(availableBalance / units.Avax)
balanceAVAX, err = promptValidatorBalanceAVAX(float64(availableBalance) / float64(units.Avax))
if err != nil {
return err
}
} else {
// convert to nanoAVAX
balance *= units.Avax
}
// convert to nanoAVAX
balance := uint64(balanceAVAX * float64(units.Avax))

if remainingBalanceOwnerAddr == "" {
remainingBalanceOwnerAddr, err = getKeyForChangeOwner(network)
Expand Down Expand Up @@ -529,7 +554,7 @@ func CallAddValidator(
if !pos {
ux.Logger.PrintToUser(" Weight: %d", weight)
}
ux.Logger.PrintToUser(" Balance: %d", balance/units.Avax)
ux.Logger.PrintToUser(" Balance: %.2f", balanceAVAX)
ux.Logger.GreenCheckmarkToUser("Validator successfully added to the L1")

return nil
Expand Down Expand Up @@ -818,7 +843,7 @@ func getWeight() (uint64, error) {
case defaultWeight:
useDefaultWeight = true
default:
weight, err = app.Prompt.CaptureWeight(txt)
weight, err = app.Prompt.CaptureWeight(txt, func(uint64) error { return nil })
if err != nil {
return 0, err
}
Expand Down
11 changes: 2 additions & 9 deletions cmd/blockchaincmd/change_owner.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ import (
"github.com/spf13/cobra"
)

var changeOwnerSupportedNetworkOptions = []networkoptions.NetworkOption{
networkoptions.Local,
networkoptions.Devnet,
networkoptions.Fuji,
networkoptions.Mainnet,
}

// avalanche blockchain changeOwner
func newChangeOwnerCmd() *cobra.Command {
cmd := &cobra.Command{
Expand All @@ -34,7 +27,7 @@ func newChangeOwnerCmd() *cobra.Command {
RunE: changeOwner,
Args: cobrautils.ExactArgs(1),
}
networkoptions.AddNetworkFlagsToCmd(cmd, &globalNetworkFlags, true, changeOwnerSupportedNetworkOptions)
networkoptions.AddNetworkFlagsToCmd(cmd, &globalNetworkFlags, true, networkoptions.DefaultSupportedNetworkOptions)
cmd.Flags().BoolVarP(&useLedger, "ledger", "g", false, "use ledger instead of key (always true on mainnet, defaults to false on fuji/devnet)")
cmd.Flags().StringSliceVar(&ledgerAddresses, "ledger-addrs", []string{}, "use the given ledger addresses")
cmd.Flags().StringVarP(&keyName, "key", "k", "", "select the key to use [fuji/devnet]")
Expand All @@ -56,7 +49,7 @@ func changeOwner(_ *cobra.Command, args []string) error {
globalNetworkFlags,
true,
false,
changeOwnerSupportedNetworkOptions,
networkoptions.DefaultSupportedNetworkOptions,
"",
)
if err != nil {
Expand Down
Loading

0 comments on commit 50ace3f

Please sign in to comment.