Skip to content

Commit

Permalink
Remove redundancies (#2229)
Browse files Browse the repository at this point in the history
* remove redundant code

* fix lint

* fix lint

* Convert Subnet in  1 command (#2230)

* one command deploy

* use default private key to deploy validator manager contract

* fix lint

* remove redundant code

* address comments

---------

Signed-off-by: sukantoraymond <[email protected]>
  • Loading branch information
sukantoraymond authored Oct 9, 2024
1 parent bb7d561 commit cc135b1
Show file tree
Hide file tree
Showing 28 changed files with 274 additions and 652 deletions.
105 changes: 52 additions & 53 deletions cmd/blockchaincmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"strings"
"time"

"github.com/ava-labs/avalanche-cli/pkg/node"

"github.com/ava-labs/avalanchego/vms/platformvm/warp/message"
"github.com/ethereum/go-ethereum/common"

Expand Down Expand Up @@ -684,62 +686,59 @@ func deployBlockchain(cmd *cobra.Command, args []string) error {
return err
}

if false {
chainSpec := contract.ChainSpec{
clusterName, err := node.GetClusterNameFromList(app)
if err != nil {
return err
}

if err = node.SyncSubnet(app, clusterName, blockchainName, true, nil); err != nil {
return err
}

if err := node.WaitForHealthyCluster(app, clusterName, node.HealthCheckTimeout, node.HealthCheckPoolTime); err != nil {
return err
}

chainSpec := contract.ChainSpec{
BlockchainName: blockchainName,
}
_, genesisPrivateKey, err := contract.GetEVMSubnetPrefundedKey(
app,
network,
chainSpec,
)
if err != nil {
return err
}
rpcURL, _, err := contract.GetBlockchainEndpoints(
app,
network,
chainSpec,
true,
false,
)
if err != nil {
return err
}
if err := validatormanager.SetupPoA(
app,
network,
rpcURL,
contract.ChainSpec{
BlockchainName: blockchainName,
}
genesisAddress, genesisPrivateKey, err := contract.GetEVMSubnetPrefundedKey(
app,
network,
chainSpec,
)
if err != nil {
return err
}
privateKey, err := privateKeyFlags.GetPrivateKey(app, genesisPrivateKey)
if err != nil {
return err
}
if privateKey == "" {
privateKey, err = prompts.PromptPrivateKey(
app.Prompt,
"Which key to you want to use to pay for initializing Validator Manager contract? (Uses Blockchain gas token)",
app.GetKeyDir(),
app.GetKey,
genesisAddress,
genesisPrivateKey,
)
if err != nil {
return err
}
}
rpcURL, _, err := contract.GetBlockchainEndpoints(
app,
network,
chainSpec,
true,
false,
)
if err != nil {
return err
}
if err := validatormanager.SetupPoA(
app,
network,
rpcURL,
contract.ChainSpec{
BlockchainName: blockchainName,
},
privateKey,
common.HexToAddress(sidecar.PoAValidatorManagerOwner),
avaGoBootstrapValidators,
); err != nil {
return err
}
ux.Logger.GreenCheckmarkToUser("Subnet is successfully converted into Subnet Only Validator")
},
genesisPrivateKey,
common.HexToAddress(sidecar.PoAValidatorManagerOwner),
avaGoBootstrapValidators,
); err != nil {
return err
}
ux.Logger.GreenCheckmarkToUser("L1 is successfully converted to sovereign blockchain")
} else {
if err := app.UpdateSidecarNetworks(&sidecar, network, subnetID, blockchainID, "", "", nil); err != nil {
return err
}
}

flags := make(map[string]string)
flags[constants.MetricsNetwork] = network.Name()
metrics.HandleTracking(cmd, constants.MetricsSubnetDeployCommand, app, flags)
Expand Down
8 changes: 5 additions & 3 deletions cmd/nodecmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"sync"
"time"

"github.com/ava-labs/avalanche-cli/pkg/node"

awsAPI "github.com/ava-labs/avalanche-cli/pkg/cloud/aws"
"github.com/ava-labs/avalanche-cli/pkg/docker"

Expand Down Expand Up @@ -204,7 +206,7 @@ func preCreateChecks(clusterName string) error {
}

func checkClusterExternal(clusterName string) (bool, error) {
clusterExists, err := checkClusterExists(clusterName)
clusterExists, err := node.CheckClusterExists(app, clusterName)
if err != nil {
return false, fmt.Errorf("error checking cluster: %w", err)
}
Expand Down Expand Up @@ -392,7 +394,7 @@ func createNodes(cmd *cobra.Command, args []string) error {
} else {
if cloudService == constants.AWSCloudService {
// Get AWS Credential, region and AMI
if !(authorizeAccess || authorizedAccessFromSettings()) && (requestCloudAuth(constants.AWSCloudService) != nil) {
if !(authorizeAccess || node.AuthorizedAccessFromSettings(app)) && (requestCloudAuth(constants.AWSCloudService) != nil) {
return fmt.Errorf("cloud access is required")
}
ec2SvcMap, ami, numNodesMap, err := getAWSCloudConfig(awsProfile, false, nil, nodeType)
Expand Down Expand Up @@ -464,7 +466,7 @@ func createNodes(cmd *cobra.Command, args []string) error {
}
}
} else {
if !(authorizeAccess || authorizedAccessFromSettings()) && (requestCloudAuth(constants.GCPCloudService) != nil) {
if !(authorizeAccess || node.AuthorizedAccessFromSettings(app)) && (requestCloudAuth(constants.GCPCloudService) != nil) {
return fmt.Errorf("cloud access is required")
}
// Get GCP Credential, zone, Image ID, service account key file path, and GCP project name
Expand Down
6 changes: 4 additions & 2 deletions cmd/nodecmd/create_devnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"sync"
"time"

"github.com/ava-labs/avalanche-cli/pkg/node"

"golang.org/x/exp/slices"

"github.com/ava-labs/avalanche-cli/pkg/ansible"
Expand Down Expand Up @@ -155,7 +157,7 @@ func generateCustomGenesis(
}

func setupDevnet(clusterName string, hosts []*models.Host, apiNodeIPMap map[string]string) error {
if err := checkCluster(clusterName); err != nil {
if err := node.CheckCluster(app, clusterName); err != nil {
return err
}
inventoryPath := app.GetAnsibleInventoryDirPath(clusterName)
Expand All @@ -178,7 +180,7 @@ func setupDevnet(clusterName string, hosts []*models.Host, apiNodeIPMap map[stri
} else {
endpointIP = ansibleHosts[ansibleHostIDs[0]].IP
}
endpoint := getAvalancheGoEndpoint(endpointIP)
endpoint := node.GetAvalancheGoEndpoint(endpointIP)
network := models.NewDevnetNetwork(endpoint, 0)
network = models.NewNetworkFromCluster(network, clusterName)

Expand Down
10 changes: 6 additions & 4 deletions cmd/nodecmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package nodecmd
import (
"fmt"

"github.com/ava-labs/avalanche-cli/pkg/node"

"github.com/ava-labs/avalanche-cli/cmd/blockchaincmd"
"github.com/ava-labs/avalanche-cli/pkg/ansible"
"github.com/ava-labs/avalanche-cli/pkg/cobrautils"
Expand Down Expand Up @@ -41,7 +43,7 @@ It saves the deploy info both locally and remotely.
func deploySubnet(cmd *cobra.Command, args []string) error {
clusterName := args[0]
subnetName := args[1]
if err := checkCluster(clusterName); err != nil {
if err := node.CheckCluster(app, clusterName); err != nil {
return err
}
if _, err := blockchaincmd.ValidateSubnetNameAndGetChains([]string{subnetName}); err != nil {
Expand All @@ -58,12 +60,12 @@ func deploySubnet(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
defer disconnectHosts(hosts)
defer node.DisconnectHosts(hosts)
if !avoidChecks {
if err := checkHostsAreHealthy(hosts); err != nil {
if err := node.CheckHostsAreHealthy(hosts); err != nil {
return err
}
if err := checkHostsAreRPCCompatible(hosts, subnetName); err != nil {
if err := node.CheckHostsAreRPCCompatible(app, hosts, subnetName); err != nil {
return err
}
}
Expand Down
43 changes: 6 additions & 37 deletions cmd/nodecmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"strings"

nodePkg "github.com/ava-labs/avalanche-cli/pkg/node"

awsAPI "github.com/ava-labs/avalanche-cli/pkg/cloud/aws"
gcpAPI "github.com/ava-labs/avalanche-cli/pkg/cloud/gcp"
"github.com/ava-labs/avalanche-cli/pkg/cobrautils"
Expand Down Expand Up @@ -155,7 +157,7 @@ func destroyNodes(_ *cobra.Command, args []string) error {
return Cleanup()
}
clusterName := args[0]
if err := checkCluster(clusterName); err != nil {
if err := nodePkg.CheckCluster(app, clusterName); err != nil {
return err
}
isExternalCluster, err := checkClusterExternal(clusterName)
Expand All @@ -169,7 +171,7 @@ func destroyNodes(_ *cobra.Command, args []string) error {
if err := getDeleteConfigConfirmation(); err != nil {
return err
}
nodesToStop, err := getClusterNodes(clusterName)
nodesToStop, err := nodePkg.GetClusterNodes(app, clusterName)
if err != nil {
return err
}
Expand Down Expand Up @@ -236,7 +238,7 @@ func destroyNodes(_ *cobra.Command, args []string) error {
continue
}
if nodeConfig.CloudService == "" || nodeConfig.CloudService == constants.AWSCloudService {
if !(authorizeAccess || authorizedAccessFromSettings()) && (requestCloudAuth(constants.AWSCloudService) != nil) {
if !(authorizeAccess || nodePkg.AuthorizedAccessFromSettings(app)) && (requestCloudAuth(constants.AWSCloudService) != nil) {
return fmt.Errorf("cloud access is required")
}
if err = ec2SvcMap[nodeConfig.Region].DestroyAWSNode(nodeConfig, clusterName); err != nil {
Expand All @@ -258,7 +260,7 @@ func destroyNodes(_ *cobra.Command, args []string) error {
}
}
} else {
if !(authorizeAccess || authorizedAccessFromSettings()) && (requestCloudAuth(constants.GCPCloudService) != nil) {
if !(authorizeAccess || nodePkg.AuthorizedAccessFromSettings(app)) && (requestCloudAuth(constants.GCPCloudService) != nil) {
return fmt.Errorf("cloud access is required")
}
if gcpCloud == nil {
Expand Down Expand Up @@ -328,36 +330,3 @@ func getClusterMonitoringNode(clusterName string) (string, error) {
}
return clustersConfig.Clusters[clusterName].MonitoringInstance, nil
}

func checkCluster(clusterName string) error {
_, err := getClusterNodes(clusterName)
return err
}

func checkClusterExists(clusterName string) (bool, error) {
clustersConfig := models.ClustersConfig{}
if app.ClustersConfigExists() {
var err error
clustersConfig, err = app.LoadClustersConfig()
if err != nil {
return false, err
}
}
_, ok := clustersConfig.Clusters[clusterName]
return ok, nil
}

func getClusterNodes(clusterName string) ([]string, error) {
if exists, err := checkClusterExists(clusterName); err != nil || !exists {
return nil, fmt.Errorf("cluster %q not found", clusterName)
}
clustersConfig, err := app.LoadClustersConfig()
if err != nil {
return nil, err
}
clusterNodes := clustersConfig.Clusters[clusterName].Nodes
if len(clusterNodes) == 0 {
return nil, fmt.Errorf("no nodes found in cluster %s", clusterName)
}
return clusterNodes, nil
}
6 changes: 4 additions & 2 deletions cmd/nodecmd/dynamic_ips.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"context"
"fmt"

nodePkg "github.com/ava-labs/avalanche-cli/pkg/node"

awsAPI "github.com/ava-labs/avalanche-cli/pkg/cloud/aws"
gcpAPI "github.com/ava-labs/avalanche-cli/pkg/cloud/gcp"

Expand Down Expand Up @@ -51,7 +53,7 @@ func getPublicIPsForNodesWithDynamicIP(nodesWithDynamicIP []models.NodeConfig) (
}
var publicIP map[string]string
if node.CloudService == constants.GCPCloudService {
if !(authorizeAccess || authorizedAccessFromSettings()) && (requestCloudAuth(constants.GCPCloudService) != nil) {
if !(authorizeAccess || nodePkg.AuthorizedAccessFromSettings(app)) && (requestCloudAuth(constants.GCPCloudService) != nil) {
return nil, fmt.Errorf("cloud access is required")
}
if gcpCloud == nil {
Expand Down Expand Up @@ -87,7 +89,7 @@ func getPublicIPsForNodesWithDynamicIP(nodesWithDynamicIP []models.NodeConfig) (
// - in ansible inventory file
// - in host config file
func updatePublicIPs(clusterName string) error {
clusterNodes, err := getClusterNodes(clusterName)
clusterNodes, err := nodePkg.GetClusterNodes(app, clusterName)
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/nodecmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"path/filepath"

"github.com/ava-labs/avalanche-cli/pkg/node"

"github.com/ava-labs/avalanche-cli/pkg/cobrautils"
"github.com/ava-labs/avalanche-cli/pkg/constants"
"github.com/ava-labs/avalanche-cli/pkg/models"
Expand Down Expand Up @@ -51,7 +53,7 @@ func exportFile(_ *cobra.Command, args []string) error {
ux.Logger.RedXToUser("file already exists, use --force to overwrite")
return nil
}
if err := checkCluster(clusterName); err != nil {
if err := node.CheckCluster(app, clusterName); err != nil {
ux.Logger.RedXToUser("cluster not found: %v", err)
return err
}
Expand Down
Loading

0 comments on commit cc135b1

Please sign in to comment.