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

Improve change weight #2545

Merged
merged 13 commits into from
Jan 14, 2025
57 changes: 45 additions & 12 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"
validatorManagerSDK "github.com/ava-labs/avalanche-cli/sdk/validatormanager"
"github.com/ava-labs/avalanchego/ids"
avagoconstants "github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/formatting/address"
Expand All @@ -46,7 +47,7 @@ var (

nodeIDStr string
nodeEndpoint string
balance uint64
balanceAVAX float64
weight uint64
startTimeStr string
duration time.Duration
Expand Down Expand Up @@ -96,7 +97,12 @@ Testnet or Mainnet.`,

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 @@ -219,6 +225,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 +248,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 +310,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 +338,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 +426,27 @@ func CallAddValidator(

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

if balance == 0 {
totalWeight, err := validatorManagerSDK.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 +562,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 +851,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
Loading
Loading