Skip to content

Commit

Permalink
Use network name instead of cluster name on sidecar (#2303)
Browse files Browse the repository at this point in the history
* use network instead of cluster

* replace clustername with network

* update test

* fix test

* fix test

* fix lint

* fix lint

* fix test

* fix lint

* fix test

* fix lint

* address comments

* fix lint

* address comments

* fix lint
  • Loading branch information
sukantoraymond authored Nov 13, 2024
1 parent ff4e381 commit ea37fed
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 31 deletions.
8 changes: 6 additions & 2 deletions cmd/blockchaincmd/add_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var (
errMutuallyExclusiveWeightOptions = errors.New("--use-default-validator-params and --weight are mutually exclusive")
ErrNotPermissionedSubnet = errors.New("subnet is not permissioned")
aggregatorExtraEndpoints []string
clusterNameFlagValue string
)

// avalanche blockchain addValidator
Expand Down Expand Up @@ -134,6 +135,10 @@ func addValidator(_ *cobra.Command, args []string) error {
if err != nil {
return err
}
if network.ClusterName != "" {
clusterNameFlagValue = network.ClusterName
network = models.ConvertClusterToNetwork(network)
}

fee := network.GenesisParams().TxFeeConfig.StaticFeeConfig.AddSubnetValidatorFee
kc, err := keychain.GetKeychainFromCmdLineFlags(
Expand Down Expand Up @@ -319,11 +324,10 @@ func CallAddValidator(
Addresses: disableOwnerAddrID,
}

extraAggregatorPeers, err := GetAggregatorExtraPeers(network, aggregatorExtraEndpoints)
extraAggregatorPeers, err := GetAggregatorExtraPeers(clusterNameFlagValue, aggregatorExtraEndpoints)
if err != nil {
return err
}

signedMessage, validationID, err := validatormanager.InitValidatorRegistration(
app,
network,
Expand Down
39 changes: 20 additions & 19 deletions cmd/blockchaincmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ func deployBlockchain(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}

clusterNameFlagValue = globalNetworkFlags.ClusterName
isEVMGenesis, validationErr, err := app.HasSubnetEVMGenesis(chain)
if err != nil {
return err
Expand Down Expand Up @@ -479,6 +479,7 @@ func deployBlockchain(cmd *cobra.Command, args []string) error {
deployInfo.ICMMessengerAddress,
deployInfo.ICMRegistryAddress,
nil,
clusterNameFlagValue,
); err != nil {
return err
}
Expand All @@ -488,8 +489,8 @@ func deployBlockchain(cmd *cobra.Command, args []string) error {
if sidecar.Sovereign {
if !generateNodeID {
clusterName := fmt.Sprintf("%s-local-node", blockchainName)
if globalNetworkFlags.ClusterName != "" {
clusterName = globalNetworkFlags.ClusterName
if clusterNameFlagValue != "" {
clusterName = clusterNameFlagValue
clusterConfig, err := app.GetClusterConfig(clusterName)
if err != nil {
return err
Expand All @@ -504,11 +505,11 @@ func deployBlockchain(cmd *cobra.Command, args []string) error {
"please create your local node again and call subnet deploy command again", err)
}
}
network = models.NewNetworkFromCluster(network, clusterName)
network = models.ConvertClusterToNetwork(network)
}
}
// ask user if we want to use local machine if cluster is not provided
if !useLocalMachine && globalNetworkFlags.ClusterName == "" {
if !useLocalMachine && clusterNameFlagValue == "" {
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.")

Expand All @@ -518,7 +519,7 @@ func deployBlockchain(cmd *cobra.Command, args []string) error {
}
}
// if no cluster provided - we create one with fmt.Sprintf("%s-local-node", blockchainName) name
if useLocalMachine && globalNetworkFlags.ClusterName == "" {
if useLocalMachine && clusterNameFlagValue == "" {
// stop local avalanchego process so that we can generate new local cluster
_ = node.StopLocalNode(app)
anrSettings := node.ANRSettings{}
Expand All @@ -533,7 +534,6 @@ func deployBlockchain(cmd *cobra.Command, args []string) error {
return err
}
}
network = models.NewNetworkFromCluster(network, clusterName)
nodeConfig := ""
if app.AvagoNodeConfigExists(blockchainName) {
nodeConfigBytes, err := os.ReadFile(app.GetAvagoNodeConfigPath(blockchainName))
Expand All @@ -557,6 +557,7 @@ func deployBlockchain(cmd *cobra.Command, args []string) error {
); err != nil {
return err
}
clusterNameFlagValue = clusterName
if len(bootstrapEndpoints) == 0 {
bootstrapEndpoints, err = getLocalBootstrapEndpoints()
if err != nil {
Expand Down Expand Up @@ -594,11 +595,11 @@ func deployBlockchain(cmd *cobra.Command, args []string) error {
ChangeOwnerAddr: changeOwnerAddress,
})
}
case globalNetworkFlags.ClusterName != "":
case clusterNameFlagValue != "":
// for remote clusters we don't need to ask for bootstrap validators and can read it from filesystem
bootstrapValidators, err = getClusterBootstrapValidators(globalNetworkFlags.ClusterName, network)
bootstrapValidators, err = getClusterBootstrapValidators(clusterNameFlagValue, network)
if err != nil {
return fmt.Errorf("error getting bootstrap validators from cluster %s: %w", globalNetworkFlags.ClusterName, err)
return fmt.Errorf("error getting bootstrap validators from cluster %s: %w", clusterNameFlagValue, err)
}

default:
Expand Down Expand Up @@ -814,12 +815,12 @@ func deployBlockchain(cmd *cobra.Command, args []string) error {
}
fmt.Println()

if err := app.UpdateSidecarNetworks(&sidecar, network, subnetID, blockchainID, "", "", bootstrapValidators); err != nil {
if err := app.UpdateSidecarNetworks(&sidecar, network, subnetID, blockchainID, "", "", bootstrapValidators, clusterNameFlagValue); err != nil {
return err
}

if !convertOnly && !generateNodeID {
clusterName := network.ClusterName
clusterName := clusterNameFlagValue
if clusterName == "" {
clusterName, err = node.GetClusterNameFromList(app)
if err != nil {
Expand Down Expand Up @@ -870,7 +871,7 @@ func deployBlockchain(cmd *cobra.Command, args []string) error {
return err
}
evm.WaitForChainID(client)
extraAggregatorPeers, err := GetAggregatorExtraPeers(network, aggregatorExtraEndpoints)
extraAggregatorPeers, err := GetAggregatorExtraPeers(clusterName, aggregatorExtraEndpoints)
if err != nil {
return err
}
Expand Down Expand Up @@ -914,7 +915,7 @@ func deployBlockchain(cmd *cobra.Command, args []string) error {
ux.Logger.PrintToUser("Once the Avalanche Node(s) are created and are tracking the blockchain, call `avalanche contract initPoaManager %s` to finish conversion to sovereign L1", blockchainName)
}
} else {
if err := app.UpdateSidecarNetworks(&sidecar, network, subnetID, blockchainID, "", "", nil); err != nil {
if err := app.UpdateSidecarNetworks(&sidecar, network, subnetID, blockchainID, "", "", nil, clusterNameFlagValue); err != nil {
return err
}
}
Expand Down Expand Up @@ -1262,10 +1263,10 @@ func ConvertURIToPeers(uris []string) ([]info.Peer, error) {
}

func GetAggregatorExtraPeers(
network models.Network,
clusterName string,
extraURIs []string,
) ([]info.Peer, error) {
uris, err := GetAggregatorNetworkUris(network)
uris, err := GetAggregatorNetworkUris(clusterName)
if err != nil {
return nil, err
}
Expand All @@ -1275,14 +1276,14 @@ func GetAggregatorExtraPeers(
return ConvertURIToPeers(uris)
}

func GetAggregatorNetworkUris(network models.Network) ([]string, error) {
func GetAggregatorNetworkUris(clusterName string) ([]string, error) {
aggregatorExtraPeerEndpointsUris := []string{}
if network.ClusterName != "" {
if clusterName != "" {
clustersConfig, err := app.LoadClustersConfig()
if err != nil {
return nil, err
}
clusterConfig := clustersConfig.Clusters[network.ClusterName]
clusterConfig := clustersConfig.Clusters[clusterName]
if clusterConfig.Local {
cli, err := binutils.NewGRPCClientWithEndpoint(
binutils.LocalClusterGRPCServerEndpoint,
Expand Down
8 changes: 5 additions & 3 deletions cmd/blockchaincmd/remove_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ func removeValidator(_ *cobra.Command, args []string) error {
if err != nil {
return err
}

if network.ClusterName != "" {
network = models.ConvertClusterToNetwork(network)
}
fee := network.GenesisParams().TxFeeConfig.StaticFeeConfig.TxFee
kc, err := keychain.GetKeychainFromCmdLineFlags(
app,
Expand Down Expand Up @@ -215,8 +217,8 @@ func removeValidatorSOV(
}
}
ux.Logger.PrintToUser(logging.Yellow.Wrap("RPC Endpoint: %s"), rpcURL)

extraAggregatorPeers, err := GetAggregatorExtraPeers(network, aggregatorExtraEndpoints)
clusterName := sc.Networks[network.Name()].ClusterName
extraAggregatorPeers, err := GetAggregatorExtraPeers(clusterName, aggregatorExtraEndpoints)
if err != nil {
return err
}
Expand Down
8 changes: 7 additions & 1 deletion cmd/contractcmd/init_poa_validator_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ava-labs/avalanche-cli/cmd/blockchaincmd"
"github.com/ava-labs/avalanche-cli/pkg/cobrautils"
"github.com/ava-labs/avalanche-cli/pkg/contract"
"github.com/ava-labs/avalanche-cli/pkg/models"
"github.com/ava-labs/avalanche-cli/pkg/networkoptions"
"github.com/ava-labs/avalanche-cli/pkg/prompts"
"github.com/ava-labs/avalanche-cli/pkg/ux"
Expand All @@ -31,6 +32,7 @@ var (
initPOAManagerSupportedNetworkOptions = []networkoptions.NetworkOption{
networkoptions.Local,
networkoptions.Devnet,
networkoptions.EtnaDevnet,
networkoptions.Fuji,
}
initPOAManagerFlags InitPOAManagerFlags
Expand Down Expand Up @@ -70,6 +72,9 @@ func initPOAManager(_ *cobra.Command, args []string) error {
if err != nil {
return err
}
if network.ClusterName != "" {
network = models.ConvertClusterToNetwork(network)
}
if initPOAManagerFlags.rpcEndpoint == "" {
initPOAManagerFlags.rpcEndpoint, _, err = contract.GetBlockchainEndpoints(
app,
Expand Down Expand Up @@ -120,7 +125,8 @@ func initPOAManager(_ *cobra.Command, args []string) error {
if err != nil {
return err
}
extraAggregatorPeers, err := blockchaincmd.GetAggregatorExtraPeers(network, initPOAManagerFlags.aggregatorExtraEndpoints)
clusterName := sc.Networks[network.Name()].ClusterName
extraAggregatorPeers, err := blockchaincmd.GetAggregatorExtraPeers(clusterName, initPOAManagerFlags.aggregatorExtraEndpoints)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/transactioncmd/transaction_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func commitTx(_ *cobra.Command, args []string) error {
if err := blockchaincmd.PrintDeployResults(subnetName, subnetID, txID); err != nil {
return err
}
return app.UpdateSidecarNetworks(&sc, network, subnetID, txID, "", "", sc.Networks[network.Name()].BootstrapValidators)
return app.UpdateSidecarNetworks(&sc, network, subnetID, txID, "", "", sc.Networks[network.Name()].BootstrapValidators, "")
}

return nil
Expand Down
2 changes: 2 additions & 0 deletions pkg/application/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ func (app *Avalanche) UpdateSidecarNetworks(
teleporterMessengerAddress string,
teleporterRegistryAddress string,
bootstrapValidators []models.SubnetValidator,
clusterName string,
) error {
if sc.Networks == nil {
sc.Networks = make(map[string]models.NetworkData)
Expand All @@ -549,6 +550,7 @@ func (app *Avalanche) UpdateSidecarNetworks(
TeleporterMessengerAddress: teleporterMessengerAddress,
TeleporterRegistryAddress: teleporterRegistryAddress,
BootstrapValidators: bootstrapValidators,
ClusterName: clusterName,
}
if err := app.UpdateSidecar(sc); err != nil {
return fmt.Errorf("creation of chains and subnet was successful, but failed to update sidecar: %w", err)
Expand Down
51 changes: 51 additions & 0 deletions pkg/models/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"os"
"strings"

"github.com/ava-labs/avalanche-cli/pkg/utils"
"github.com/ava-labs/avalanchego/api/info"

"github.com/ava-labs/avalanche-cli/pkg/constants"
"github.com/ava-labs/avalanchego/genesis"
avagoconstants "github.com/ava-labs/avalanchego/utils/constants"
Expand Down Expand Up @@ -75,6 +78,37 @@ func NewDevnetNetwork(endpoint string, id uint32) Network {
return NewNetwork(Devnet, id, endpoint, "")
}

// ConvertClusterToNetwork converts a cluster network into a non cluster network
func ConvertClusterToNetwork(clusterNetwork Network) Network {
if clusterNetwork.ClusterName == "" {
return clusterNetwork
}
switch {
case clusterNetwork.ID == constants.LocalNetworkID:
return NewLocalNetwork()
case clusterNetwork.ID == avagoconstants.FujiID:
return NewFujiNetwork()
case clusterNetwork.ID == avagoconstants.MainnetID:
return NewMainnetNetwork()
case clusterNetwork.ID == constants.EtnaDevnetNetworkID:
return NewEtnaDevnetNetwork()
default:
networkID := uint32(0)
if clusterNetwork.Endpoint != "" {
infoClient := info.NewClient(clusterNetwork.Endpoint)
ctx, cancel := utils.GetAPIContext()
defer cancel()
var err error
networkID, err = infoClient.GetNetworkID(ctx)
if err != nil {
return clusterNetwork
}
return NewDevnetNetwork(clusterNetwork.Endpoint, networkID)
}
return clusterNetwork
}
}

func NewEtnaDevnetNetwork() Network {
return NewNetwork(EtnaDevnet, constants.EtnaDevnetNetworkID, constants.EtnaDevnetEndpoint, "")
}
Expand Down Expand Up @@ -189,3 +223,20 @@ func (n *Network) HandlePublicNetworkSimulation() {
func (n *Network) Equals(n2 Network) bool {
return n.Kind == n2.Kind && n.Endpoint == n2.Endpoint
}

// GetNetworkFromCluster gets the network that a cluster is on
func GetNetworkFromCluster(clusterConfig ClusterConfig) Network {
network := clusterConfig.Network
switch {
case network.ID == constants.LocalNetworkID:
return NewLocalNetwork()
case network.ID == avagoconstants.FujiID:
return NewFujiNetwork()
case network.ID == avagoconstants.MainnetID:
return NewMainnetNetwork()
case network.ID == constants.EtnaDevnetNetworkID:
return NewEtnaDevnetNetwork()
default:
return network
}
}
1 change: 1 addition & 0 deletions pkg/models/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type NetworkData struct {
RPCEndpoints []string
WSEndpoints []string
BootstrapValidators []SubnetValidator
ClusterName string
}

type Sidecar struct {
Expand Down
7 changes: 5 additions & 2 deletions pkg/node/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ func TrackSubnetWithLocalMachine(
return err
}
clusterConfig := clustersConfig.Clusters[clusterName]
network := clusterConfig.Network
network := models.GetNetworkFromCluster(clusterConfig)
if network.ClusterName != "" {
network = models.ConvertClusterToNetwork(network)
}
if sc.Networks[network.Name()].BlockchainID == ids.Empty {
return fmt.Errorf("blockchain %s has not been deployed to %s", blockchainName, network.Name())
}
Expand Down Expand Up @@ -130,7 +133,7 @@ func TrackSubnetWithLocalMachine(
return err
}
}
sc.Networks[clusterConfig.Network.Name()] = networkInfo
sc.Networks[network.Name()] = networkInfo
if err := app.UpdateSidecar(&sc); err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion tests/e2e/testcases/subnet/sov/addRemoveValidator/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
var blockchainID = ""

const (
CLIBinary = "./bin/avalanche"
subnetName = "e2eSubnetTest"
keyName = "ewoq"
ewoqEVMAddress = "0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC"
Expand Down
Loading

0 comments on commit ea37fed

Please sign in to comment.