diff --git a/cmd/blockchaincmd/deploy.go b/cmd/blockchaincmd/deploy.go index 13815f5d7..f95bf15ec 100644 --- a/cmd/blockchaincmd/deploy.go +++ b/cmd/blockchaincmd/deploy.go @@ -70,7 +70,6 @@ var ( outputTxPath string useLedger bool useLocalMachine bool - localMachineCluster string useEwoq bool ledgerAddresses []string sovereign bool @@ -144,7 +143,6 @@ so you can take your locally tested Subnet and deploy it on Fuji or Mainnet.`, cmd.Flags().StringVar(&aggregatorLogLevel, "aggregator-log-level", "Off", "log level to use with signature aggregator") cmd.Flags().StringSliceVar(&aggregatorExtraEndpoints, "aggregator-extra-endpoints", nil, "endpoints for extra nodes that are needed in signature aggregation") cmd.Flags().BoolVar(&useLocalMachine, "use-local-machine", false, "use local machine as a blockchain validator") - cmd.Flags().StringVar(&localMachineCluster, "local-machine-cluster", "", "existing local machine to be used as a blockchain validator") return cmd } @@ -482,7 +480,28 @@ func deployBlockchain(cmd *cobra.Command, args []string) error { if sidecar.Sovereign { if !convertOnly && !generateNodeID { - if !useLocalMachine { + clusterName := fmt.Sprintf("%s-local-node", blockchainName) + if globalNetworkFlags.ClusterName != "" { + clusterName = globalNetworkFlags.ClusterName + clusterConfig, err := app.GetClusterConfig(clusterName) + if err != nil { + return err + } + // check if cluster is local + if clusterConfig.Local { + useLocalMachine = true + if len(bootstrapEndpoints) == 0 { + bootstrapEndpoints, err = getLocalBootstrapEndpoints() + if err != nil { + return fmt.Errorf("error getting local host bootstrap endpoints: %w, "+ + "please create your local node again and call subnet deploy command again", err) + } + } + network = models.NewNetworkFromCluster(network, clusterName) + } + } + // ask user if we wants to use local machine if cluster is not provided + if !useLocalMachine && globalNetworkFlags.ClusterName == "" { ux.Logger.PrintToUser("You can use your local machine as a bootstrap validator on the blockchain") ux.Logger.PrintToUser("This means that you don't have to to set up a remote server on a cloud service (e.g. AWS / GCP) to be a validator on the blockchain.") @@ -491,20 +510,10 @@ func deployBlockchain(cmd *cobra.Command, args []string) error { return err } } - if useLocalMachine { - // stop any local avalanche go process running before we start local node + // if no cluster provided - we create one with fmt.Sprintf("%s-local-node", blockchainName) name + if useLocalMachine && globalNetworkFlags.ClusterName == "" { + // stop local avalanchego process so that we can generate new local cluster _ = node.StopLocalNode(app) - clusterName := fmt.Sprintf("%s-local-node", blockchainName) - if localMachineCluster != "" { - // don't destroy cluster if local cluster name is provided - clusterName = localMachineCluster - } else if utils.DirectoryExists(app.GetLocalDir(clusterName)) { - _ = node.DestroyLocalNode(app, clusterName) - } - // destroy any cluster with same name before we start local node - // we don't want to reuse snapshots from previous sessions - // TODO: replace bootstrapEndpoints with dynamic port number - bootstrapEndpoints = []string{"http://127.0.0.1:9650"} anrSettings := node.ANRSettings{} avagoVersionSettings := node.AvalancheGoVersionSettings{} useEtnaDevnet := network.Kind == models.EtnaDevnet @@ -524,7 +533,7 @@ func deployBlockchain(cmd *cobra.Command, args []string) error { clusterName, useEtnaDevnet, avagoBinaryPath, - 1, + 5, anrSettings, avagoVersionSettings, globalNetworkFlags, @@ -532,6 +541,13 @@ func deployBlockchain(cmd *cobra.Command, args []string) error { ); err != nil { return err } + if len(bootstrapEndpoints) == 0 { + bootstrapEndpoints, err = getLocalBootstrapEndpoints() + if err != nil { + return fmt.Errorf("error getting local host bootstrap endpoints: %w, "+ + "please create your local node again and call subnet deploy command again", err) + } + } } } if len(bootstrapEndpoints) > 0 { @@ -802,7 +818,6 @@ func deployBlockchain(cmd *cobra.Command, args []string) error { return err } } - if !useLocalMachine { if err = node.SyncSubnet(app, clusterName, blockchainName, true, nil); err != nil { return err diff --git a/cmd/blockchaincmd/helpers.go b/cmd/blockchaincmd/helpers.go index 6bf702fab..63d1cc606 100644 --- a/cmd/blockchaincmd/helpers.go +++ b/cmd/blockchaincmd/helpers.go @@ -6,6 +6,9 @@ import ( "fmt" "time" + "github.com/ava-labs/avalanche-cli/pkg/binutils" + "github.com/ava-labs/avalanche-cli/pkg/utils" + "github.com/ava-labs/avalanche-cli/pkg/keychain" "github.com/ava-labs/avalanche-cli/pkg/models" "github.com/ava-labs/avalanche-cli/pkg/networkoptions" @@ -130,3 +133,21 @@ func UpdatePChainHeight( fmt.Println() return nil } + +func getLocalBootstrapEndpoints() ([]string, error) { + ctx, cancel := utils.GetANRContext() + defer cancel() + cli, err := binutils.NewGRPCClientWithEndpoint(binutils.LocalClusterGRPCServerEndpoint) + if err != nil { + return nil, err + } + status, err := cli.Status(ctx) + if err != nil { + return nil, err + } + localBootstrapEndpoints := []string{} + for _, nodeInfo := range status.ClusterInfo.NodeInfos { + localBootstrapEndpoints = append(localBootstrapEndpoints, nodeInfo.Uri) + } + return localBootstrapEndpoints, nil +} diff --git a/pkg/node/local.go b/pkg/node/local.go index 64896edc8..a646d28b2 100644 --- a/pkg/node/local.go +++ b/pkg/node/local.go @@ -135,7 +135,8 @@ func checkClusterIsLocal(app *application.Avalanche, clusterName string) (bool, func StartLocalNode( app *application.Avalanche, - clusterName string, useEtnaDevnet bool, + clusterName string, + useEtnaDevnet bool, avalanchegoBinaryPath string, numNodes uint32, anrSettings ANRSettings,