Skip to content

Commit

Permalink
Merge branch 'convertSubnetAvaGo' into add-poa-setup-call
Browse files Browse the repository at this point in the history
Signed-off-by: sukantoraymond <[email protected]>
  • Loading branch information
sukantoraymond authored Oct 8, 2024
2 parents 65f6df6 + ae81087 commit 6fd0482
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 559 deletions.
50 changes: 18 additions & 32 deletions cmd/blockchaincmd/add_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var (
// avalanche blockchain addValidator
func newAddValidatorCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "addValidator [blockchainName] [nodeID]",
Use: "addValidator [blockchainName]",
Short: "Allow a validator to validate your blockchain's subnet",
Long: `The blockchain addValidator command whitelists a primary network validator to
validate the subnet of the provided deployed Blockchain.
Expand All @@ -72,7 +72,7 @@ these prompts by providing the values with flags.
This command currently only works on Blockchains deployed to either the Fuji
Testnet or Mainnet.`,
RunE: addValidator,
Args: cobrautils.ExactArgs(2),
Args: cobrautils.ExactArgs(1),
}
networkoptions.AddNetworkFlagsToCmd(cmd, &globalNetworkFlags, true, addValidatorSupportedNetworkOptions)

Expand All @@ -82,20 +82,20 @@ 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().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().BoolVar(&sovereign, "sovereign", true, "set to false if adding validator to a non-sovereign blockchain")
cmd.Flags().StringVar(&nodeIDStr, "node-id", "", "node-id of the validator to add")
cmd.Flags().StringVar(&publicKey, "bls-public-key", "", "set the BLS public key of the validator to add")
cmd.Flags().StringVar(&pop, "bls-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")
return cmd
}

func addValidator(_ *cobra.Command, args []string) error {
blockchainName := args[0]
_, err := ids.NodeIDFromString(args[1])
err := prompts.ValidateNodeID(nodeIDStr)
if err != nil {
return err
}
nodeIDStr = args[1]

network, err := networkoptions.GetNetworkFromCmdLineFlags(
app,
Expand Down Expand Up @@ -124,13 +124,13 @@ func addValidator(_ *cobra.Command, args []string) error {
return err
}
network.HandlePublicNetworkSimulation()
if nonSOV {
if !sovereign {
if err := UpdateKeychainWithSubnetControlKeys(kc, network, blockchainName); err != nil {
return err
}
}
deployer := subnet.NewPublicDeployer(app, kc, network)
if nonSOV {
if !sovereign {
return CallAddValidatorNonSOV(deployer, network, kc, useLedger, blockchainName, nodeIDStr, defaultValidatorParams, waitForTxAcceptance)
}
return CallAddValidator(deployer, network, kc, useLedger, blockchainName, nodeIDStr)
Expand Down Expand Up @@ -197,6 +197,14 @@ func CallAddValidator(
// return err
//}

if nodeIDStr == "" {
nodeID, err := PromptNodeID("add as a blockchain validator")
if err != nil {
return err
}
nodeIDStr = nodeID.String()
}

publicKey, pop, err = promptProofOfPossession(publicKey == "", pop == "")
if err != nil {
return err
Expand All @@ -223,28 +231,6 @@ func CallAddValidator(
ux.Logger.PrintToUser("Change Address: %s", changeAddr)
ux.Logger.PrintToUser("Inputs complete, issuing transaction to add the provided validator information...")

//type RegisterSubnetValidatorTx struct {
// // Metadata, inputs and outputs
// BaseTx
// // Balance <= sum($AVAX inputs) - sum($AVAX outputs) - TxFee.
// Balance uint64 `json:"balance"`
// // [Signer] is the BLS key for this validator.
// // Note: We do not enforce that the BLS key is unique across all validators.
// // This means that validators can share a key if they so choose.
// // However, a NodeID does uniquely map to a BLS key
// Signer signer.Signer `json:"signer"`
// // Leftover $AVAX from the Subnet Validator's Balance will be issued to
// // this owner after it is removed from the validator set.
// ChangeOwner fx.Owner `json:"changeOwner"`
// // AddressedCall with Payload:
// // - SubnetID
// // - NodeID (must be Ed25519 NodeID)
// // - Weight
// // - BLS public key
// // - Expiry
// Message warp.Message `json:"message"`
//}

blsInfo, err := getBLSInfo(publicKey, pop)
if err != nil {
return fmt.Errorf("failure parsing BLS info: %w", err)
Expand Down Expand Up @@ -275,7 +261,7 @@ func CallAddValidator(
return nil
}

func generateWarpMessageAddValidator(SubnetID ids.ID, NodeID ids.NodeID, weight uint64, blsPublicKey string, expiry uint64) (warpPlatformVM.Message, error) {
func generateWarpMessageAddValidator(subnetID ids.ID, nodeID ids.NodeID, weight uint64, blsPublicKey string, expiry uint64) (warpPlatformVM.Message, error) {
return warpPlatformVM.Message{}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/blockchaincmd/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ manage your Blockchain configurations and live deployments.`,
// subnet changeOwner
cmd.AddCommand(newChangeOwnerCmd())
// subnet changeWeight
cmd.AddCommand(newSetWeightCmd())
cmd.AddCommand(newChangeWeightCmd())
return cmd
}
52 changes: 23 additions & 29 deletions cmd/blockchaincmd/change_weight.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package blockchaincmd
import (
"errors"
"fmt"
"os"

"github.com/ava-labs/avalanche-cli/pkg/cobrautils"
"github.com/ava-labs/avalanche-cli/pkg/constants"
"github.com/ava-labs/avalanche-cli/pkg/keychain"
Expand All @@ -15,42 +17,36 @@ import (
"github.com/ava-labs/avalanche-cli/pkg/ux"
"github.com/ava-labs/avalanchego/ids"
"github.com/spf13/cobra"
"os"
)

var ()

// avalanche blockchain setWeight
func newSetWeightCmd() *cobra.Command {
// avalanche blockchain addValidator
func newChangeWeightCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "setWeight [blockchainName] [nodeID]",
Short: "Updates the weight of a Subnet validator",
Long: `The blockchain changeWeight command updates the weight of a Subnet Validator.
Use: "changeWeight [blockchainName]",
Short: "Changes the weight of a Subnet validator",
Long: `The blockchain changeWeight command changes the weight of a Subnet Validator.
The Subnet has to be a Proof of Authority Subnet-Only Validator Subnet.`,
RunE: setWeight,
Args: cobrautils.ExactArgs(2),
Args: cobrautils.ExactArgs(1),
}
networkoptions.AddNetworkFlagsToCmd(cmd, &globalNetworkFlags, true, addValidatorSupportedNetworkOptions)

cmd.Flags().StringVarP(&keyName, "key", "k", "", "select the key to use [fuji/devnet only]")
cmd.Flags().Uint64Var(&weight, "weight", constants.BootstrapValidatorWeight, "set the new staking weight of the validator")
cmd.Flags().BoolVarP(&useEwoq, "ewoq", "e", false, "use ewoq key [fuji/devnet only]")
cmd.Flags().StringVar(&nodeIDStr, "node-id", "", "node-id of the validator")
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")
return cmd
}

func setWeight(_ *cobra.Command, args []string) error {
blockchainName := args[0]
_, err := ids.NodeIDFromString(args[1])
err := prompts.ValidateNodeID(nodeIDStr)
if err != nil {
return err
}
nodeIDStr = args[1]

//TODO: add check for non SOV subnet
// return err if non SOV

network, err := networkoptions.GetNetworkFromCmdLineFlags(
app,
Expand Down Expand Up @@ -122,9 +118,17 @@ func setWeight(_ *cobra.Command, args []string) error {
return errNoSubnetID
}

nodeID, err := ids.NodeIDFromString(nodeIDStr)
if err != nil {
return err
var nodeID ids.NodeID
if nodeIDStr == "" {
nodeID, err = PromptNodeID("add as a blockchain validator")
if err != nil {
return err
}
} else {
nodeID, err = ids.NodeIDFromString(nodeIDStr)
if err != nil {
return err
}
}

isValidator, err := subnet.IsSubnetValidator(subnetID, nodeID, network)
Expand All @@ -147,7 +151,7 @@ func setWeight(_ *cobra.Command, args []string) error {
// TODO: we need to wait for the balance from the removed validator to arrive in changeAddr
// set arbitrary time.sleep here?

weight, err = promptWeightSubnetValidator()
weight, err = app.Prompt.CaptureWeight("What weight would you like to assign to the validator?")
if err != nil {
return err
}
Expand All @@ -168,17 +172,7 @@ func setWeight(_ *cobra.Command, args []string) error {
}

// add back validator to subnet with updated weight
return CallAddValidator(deployer, network, kc, useLedger, blockchainName, nodeIDStr)
}

// TODO: implement checkIfSubnetIsSOV
// checkIfSubnetIsSOV returns true if Subnet is SOV from P Chain
func checkIfSubnetIsSOV() (bool, error) {
return false, nil
}
func promptWeightSubnetValidator() (uint64, error) {
txt := "What weight would you like to assign to the validator?"
return app.Prompt.CaptureWeight(txt)
return CallAddValidator(deployer, network, kc, useLedger, blockchainName, nodeID.String())
}

// getValidatorBalanceFromPChain gets remaining balance of validator from p chain
Expand Down
Loading

0 comments on commit 6fd0482

Please sign in to comment.