Skip to content

Commit

Permalink
remove validator update
Browse files Browse the repository at this point in the history
  • Loading branch information
sukantoraymond committed Sep 26, 2024
1 parent 0793c89 commit 3dff1e0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 34 deletions.
2 changes: 1 addition & 1 deletion cmd/blockchaincmd/add_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Testnet or Mainnet.`,
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")
cmd.Flags().BoolVar(&nonSOV, "not-sov", false, "set to true if adding validator to a non SOV blockchain")
cmd.Flags().BoolVar(&nonSOV, "not-sov", false, "set to true if adding validator to a non-SOV blockchain")
cmd.Flags().StringVar(&publicKey, "public-key", "", "set the BLS public key of the validator to add")
cmd.Flags().StringVar(&pop, "proof-of-possession", "", "set the BLS proof of possession of the validator to add")
cmd.Flags().StringVar(&changeAddr, "change-address", "", "P-Chain address that will receive any leftover AVAX from the validator when it is removed from Subnet")
Expand Down
88 changes: 55 additions & 33 deletions cmd/blockchaincmd/remove_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ these prompts by providing the values with flags.`,
networkoptions.AddNetworkFlagsToCmd(cmd, &globalNetworkFlags, false, removeValidatorSupportedNetworkOptions)
cmd.Flags().StringVarP(&keyName, "key", "k", "", "select the key to use [fuji deploy only]")
cmd.Flags().StringVar(&nodeIDStr, "nodeID", "", "set the NodeID of the validator to remove")
cmd.Flags().StringSliceVar(&subnetAuthKeys, "subnet-auth-keys", nil, "control keys that will be used to authenticate the removeValidator tx")
cmd.Flags().StringVar(&outputTxPath, "output-tx-path", "", "file path of the removeValidator tx")
cmd.Flags().StringSliceVar(&subnetAuthKeys, "subnet-auth-keys", nil, "(for non-SOV blockchain only) control keys that will be used to authenticate the removeValidator tx")
cmd.Flags().StringVar(&outputTxPath, "output-tx-path", "", "(for non-SOV blockchain only) file path of the removeValidator tx")
cmd.Flags().BoolVarP(&useLedger, "ledger", "g", false, "use ledger instead of key (always true on mainnet, defaults to false on fuji)")
cmd.Flags().StringSliceVar(&ledgerAddresses, "ledger-addrs", []string{}, "use the given ledger addresses")
cmd.Flags().BoolVar(&nonSOV, "not-sov", false, "set to true if removing validator in a non SOV blockchain")
cmd.Flags().BoolVar(&nonSOV, "not-sov", false, "set to true if removing validator in a non-SOV blockchain")
return cmd
}

Expand All @@ -71,12 +71,36 @@ func removeValidator(_ *cobra.Command, args []string) error {
return err
}

if nonSOV {
if outputTxPath != "" {
return errors.New("--output-tx-path flag cannot be used for non-SOV (Subnet-Only Validators) blockchains")
}

if len(subnetAuthKeys) > 0 {
return errors.New("--subnetAuthKeys flag cannot be used for non-SOV (Subnet-Only Validators) blockchains")
}
}

if outputTxPath != "" {
if _, err := os.Stat(outputTxPath); err == nil {
return fmt.Errorf("outputTxPath %q already exists", outputTxPath)
}
}

var nodeID ids.NodeID
if nodeIDStr == "" {
nodeID, err = PromptNodeID("remove as validator")
if err != nil {
return err
}
nodeIDStr = nodeID.String()
} else {
nodeID, err = ids.NodeIDFromString(nodeIDStr)
if err != nil {
return err
}
}

if len(ledgerAddresses) > 0 {
useLedger = true
}
Expand Down Expand Up @@ -138,11 +162,21 @@ func removeValidator(_ *cobra.Command, args []string) error {
return errNoSubnetID
}

// check that this guy actually is a validator on the subnet
isValidator, err := subnet.IsSubnetValidator(subnetID, nodeID, network)
if err != nil {
// just warn the user, don't fail
ux.Logger.PrintToUser("failed to check if node is a validator on the subnet: %s", err)
} else if !isValidator {
// this is actually an error
return fmt.Errorf("node %s is not a validator on subnet %s", nodeID, subnetID)
}

deployer := subnet.NewPublicDeployer(app, kc, network)
if nonSOV {
return removeValidatorNonSOV(deployer, network, subnetID, kc, blockchainName)
}
return removeValidatorSOV(deployer)
return removeValidatorSOV(deployer, network, subnetID)
}

// TODO: implement getMinNonce
Expand All @@ -153,7 +187,7 @@ func getMinNonce(validationID [32]byte) (uint64, error) {

// TODO: implement getValidationID
// get validation ID for a node from P Chain
func getValidationID() [32]byte {
func getValidationID(nodeID string) [32]byte {
return [32]byte{}
}

Expand All @@ -162,14 +196,21 @@ func generateWarpMessageRemoveValidator(validationID [32]byte, nonce, weight uin
return warpPlatformVM.Message{}, nil
}

func removeValidatorSOV(deployer *subnet.PublicDeployer) error {
// TODO: check for number of validators
// return error if there is only 1 validator
validationID := getValidationID()
func removeValidatorSOV(deployer *subnet.PublicDeployer, network models.Network, subnetID ids.ID) error {
validators, err := subnet.GetPublicSubnetValidators(subnetID, network)
if err != nil {
return err
}
if len(validators) == 1 {
return fmt.Errorf("cannot remove the only validator left in blockchain")
}

validationID := getValidationID(nodeIDStr)
minNonce, err := getMinNonce(validationID)
if err != nil {
return err
}
// automatically increase nonce by 1
message, err := generateWarpMessageRemoveValidator(validationID, minNonce+1, 0)
if err != nil {
return err
Expand All @@ -183,8 +224,6 @@ func removeValidatorSOV(deployer *subnet.PublicDeployer) error {
}

func removeValidatorNonSOV(deployer *subnet.PublicDeployer, network models.Network, subnetID ids.ID, kc *keychain.Keychain, blockchainName string) error {
var nodeID ids.NodeID

isPermissioned, controlKeys, threshold, err := txutils.GetOwners(network, subnetID)
if err != nil {
return err
Expand Down Expand Up @@ -216,32 +255,15 @@ func removeValidatorNonSOV(deployer *subnet.PublicDeployer, network models.Netwo
}
ux.Logger.PrintToUser("Your subnet auth keys for remove validator tx creation: %s", subnetAuthKeys)

if nodeIDStr == "" {
nodeID, err = PromptNodeID("remove as validator")
if err != nil {
return err
}
} else {
nodeID, err = ids.NodeIDFromString(nodeIDStr)
if err != nil {
return err
}
}
ux.Logger.PrintToUser("NodeID: %s", nodeIDStr)
ux.Logger.PrintToUser("Network: %s", network.Name())
ux.Logger.PrintToUser("Inputs complete, issuing transaction to remove the specified validator...")

// check that this guy actually is a validator on the subnet
isValidator, err := subnet.IsSubnetValidator(subnetID, nodeID, network)
nodeID, err := ids.NodeIDFromString(nodeIDStr)
if err != nil {
// just warn the user, don't fail
ux.Logger.PrintToUser("failed to check if node is a validator on the subnet: %s", err)
} else if !isValidator {
// this is actually an error
return fmt.Errorf("node %s is not a validator on subnet %s", nodeID, subnetID)
return err
}

ux.Logger.PrintToUser("NodeID: %s", nodeID.String())
ux.Logger.PrintToUser("Network: %s", network.Name())
ux.Logger.PrintToUser("Inputs complete, issuing transaction to remove the specified validator...")

isFullySigned, tx, remainingSubnetAuthKeys, err := deployer.RemoveValidator(
controlKeys,
subnetAuthKeys,
Expand Down

0 comments on commit 3dff1e0

Please sign in to comment.