From d71336a200a766655a9cbc1241865a39da94b40c Mon Sep 17 00:00:00 2001 From: Raymond Sukanto Date: Fri, 22 Nov 2024 13:31:51 -0500 Subject: [PATCH 1/5] filter network --- cmd/blockchaincmd/add_validator.go | 25 ++++++++++++++++++++----- cmd/blockchaincmd/change_weight.go | 24 ++++++++++++++++++------ cmd/blockchaincmd/remove_validator.go | 25 +++++++++++++++++++------ pkg/networkoptions/network_options.go | 6 ++++++ 4 files changed, 63 insertions(+), 17 deletions(-) diff --git a/cmd/blockchaincmd/add_validator.go b/cmd/blockchaincmd/add_validator.go index 8cec283dd..f65e2caf6 100644 --- a/cmd/blockchaincmd/add_validator.go +++ b/cmd/blockchaincmd/add_validator.go @@ -41,6 +41,7 @@ var ( networkoptions.Devnet, networkoptions.Fuji, networkoptions.Mainnet, + networkoptions.EtnaDevnet, } nodeIDStr string @@ -128,18 +129,36 @@ func addValidator(_ *cobra.Command, args []string) error { return err } + sc, err := app.LoadSidecar(blockchainName) + if err != nil { + return fmt.Errorf("failed to load sidecar: %w", err) + } + + networkOptionsList := []networkoptions.NetworkOption{} + for scNetwork := range sc.Networks { + if networkoptions.NetworkOptionFromString(scNetwork) != networkoptions.Undefined { + networkOptionsList = append(networkOptionsList, networkoptions.NetworkOptionFromString(scNetwork)) + } + } + + // default network options to add validator options + if len(networkOptionsList) == 0 { + networkOptionsList = addValidatorSupportedNetworkOptions + } + network, err := networkoptions.GetNetworkFromCmdLineFlags( app, "", globalNetworkFlags, true, false, - addValidatorSupportedNetworkOptions, + networkOptionsList, "", ) if err != nil { return err } + if network.ClusterName != "" { clusterNameFlagValue = network.ClusterName network = models.ConvertClusterToNetwork(network) @@ -183,10 +202,6 @@ func addValidator(_ *cobra.Command, args []string) error { return err } - sc, err := app.LoadSidecar(blockchainName) - if err != nil { - return fmt.Errorf("failed to load sidecar: %w", err) - } sovereign := sc.Sovereign if sovereign && network.Kind == models.Mainnet { diff --git a/cmd/blockchaincmd/change_weight.go b/cmd/blockchaincmd/change_weight.go index 996d08153..750b54690 100644 --- a/cmd/blockchaincmd/change_weight.go +++ b/cmd/blockchaincmd/change_weight.go @@ -48,24 +48,36 @@ func setWeight(_ *cobra.Command, args []string) error { return err } + sc, err := app.LoadSidecar(blockchainName) + if err != nil { + return fmt.Errorf("failed to load sidecar: %w", err) + } + + networkOptionsList := []networkoptions.NetworkOption{} + for scNetwork := range sc.Networks { + if networkoptions.NetworkOptionFromString(scNetwork) != networkoptions.Undefined { + networkOptionsList = append(networkOptionsList, networkoptions.NetworkOptionFromString(scNetwork)) + } + } + + // default network options to remove validator options + if len(networkOptionsList) == 0 { + networkOptionsList = removeValidatorSupportedNetworkOptions + } + network, err := networkoptions.GetNetworkFromCmdLineFlags( app, "", globalNetworkFlags, true, false, - removeValidatorSupportedNetworkOptions, + networkOptionsList, "", ) if err != nil { return err } - sc, err := app.LoadSidecar(blockchainName) - if err != nil { - return err - } - if network.Kind == models.Mainnet && sc.Sovereign { return errNotSupportedOnMainnet } diff --git a/cmd/blockchaincmd/remove_validator.go b/cmd/blockchaincmd/remove_validator.go index 61fc353a9..a9cc91ca5 100644 --- a/cmd/blockchaincmd/remove_validator.go +++ b/cmd/blockchaincmd/remove_validator.go @@ -33,6 +33,7 @@ var removeValidatorSupportedNetworkOptions = []networkoptions.NetworkOption{ networkoptions.Devnet, networkoptions.Fuji, networkoptions.Mainnet, + networkoptions.EtnaDevnet, } // avalanche blockchain removeValidator @@ -70,13 +71,30 @@ func removeValidator(_ *cobra.Command, args []string) error { return err } + sc, err := app.LoadSidecar(blockchainName) + if err != nil { + return err + } + + networkOptionsList := []networkoptions.NetworkOption{} + for scNetwork := range sc.Networks { + if networkoptions.NetworkOptionFromString(scNetwork) != networkoptions.Undefined { + networkOptionsList = append(networkOptionsList, networkoptions.NetworkOptionFromString(scNetwork)) + } + } + + // default network options to remove validator options + if len(networkOptionsList) == 0 { + networkOptionsList = removeValidatorSupportedNetworkOptions + } + network, err := networkoptions.GetNetworkFromCmdLineFlags( app, "", globalNetworkFlags, true, false, - removeValidatorSupportedNetworkOptions, + networkOptionsList, "", ) if err != nil { @@ -101,11 +119,6 @@ func removeValidator(_ *cobra.Command, args []string) error { } network.HandlePublicNetworkSimulation() - sc, err := app.LoadSidecar(blockchainName) - if err != nil { - return err - } - if sc.Sovereign && network.Kind == models.Mainnet { return errNotSupportedOnMainnet } diff --git a/pkg/networkoptions/network_options.go b/pkg/networkoptions/network_options.go index b07b28211..c3e58ff68 100644 --- a/pkg/networkoptions/network_options.go +++ b/pkg/networkoptions/network_options.go @@ -64,6 +64,12 @@ func NetworkOptionFromString(s string) NetworkOption { return Devnet case "Cluster": return Cluster + default: + if strings.Contains(s, "Devnet") { + return Devnet + } else if strings.Contains(s, "Cluster") { + return Cluster + } } return Undefined } From 5986b402765d2fff4f6e3a5a5b2a82f0c53a9ea0 Mon Sep 17 00:00:00 2001 From: Raymond Sukanto Date: Fri, 22 Nov 2024 14:18:12 -0500 Subject: [PATCH 2/5] address comments --- cmd/blockchaincmd/add_validator.go | 13 +------------ cmd/blockchaincmd/change_weight.go | 13 +------------ cmd/blockchaincmd/remove_validator.go | 13 +------------ pkg/networkoptions/network_options.go | 15 +++++++++++++++ 4 files changed, 18 insertions(+), 36 deletions(-) diff --git a/cmd/blockchaincmd/add_validator.go b/cmd/blockchaincmd/add_validator.go index f65e2caf6..2c0631e8d 100644 --- a/cmd/blockchaincmd/add_validator.go +++ b/cmd/blockchaincmd/add_validator.go @@ -134,18 +134,7 @@ func addValidator(_ *cobra.Command, args []string) error { return fmt.Errorf("failed to load sidecar: %w", err) } - networkOptionsList := []networkoptions.NetworkOption{} - for scNetwork := range sc.Networks { - if networkoptions.NetworkOptionFromString(scNetwork) != networkoptions.Undefined { - networkOptionsList = append(networkOptionsList, networkoptions.NetworkOptionFromString(scNetwork)) - } - } - - // default network options to add validator options - if len(networkOptionsList) == 0 { - networkOptionsList = addValidatorSupportedNetworkOptions - } - + networkOptionsList := networkoptions.GetNetworkFromSidecar(sc, addValidatorSupportedNetworkOptions) network, err := networkoptions.GetNetworkFromCmdLineFlags( app, "", diff --git a/cmd/blockchaincmd/change_weight.go b/cmd/blockchaincmd/change_weight.go index 750b54690..a3fb5a949 100644 --- a/cmd/blockchaincmd/change_weight.go +++ b/cmd/blockchaincmd/change_weight.go @@ -53,18 +53,7 @@ func setWeight(_ *cobra.Command, args []string) error { return fmt.Errorf("failed to load sidecar: %w", err) } - networkOptionsList := []networkoptions.NetworkOption{} - for scNetwork := range sc.Networks { - if networkoptions.NetworkOptionFromString(scNetwork) != networkoptions.Undefined { - networkOptionsList = append(networkOptionsList, networkoptions.NetworkOptionFromString(scNetwork)) - } - } - - // default network options to remove validator options - if len(networkOptionsList) == 0 { - networkOptionsList = removeValidatorSupportedNetworkOptions - } - + networkOptionsList := networkoptions.GetNetworkFromSidecar(sc, removeValidatorSupportedNetworkOptions) network, err := networkoptions.GetNetworkFromCmdLineFlags( app, "", diff --git a/cmd/blockchaincmd/remove_validator.go b/cmd/blockchaincmd/remove_validator.go index a9cc91ca5..8a093c2ef 100644 --- a/cmd/blockchaincmd/remove_validator.go +++ b/cmd/blockchaincmd/remove_validator.go @@ -76,18 +76,7 @@ func removeValidator(_ *cobra.Command, args []string) error { return err } - networkOptionsList := []networkoptions.NetworkOption{} - for scNetwork := range sc.Networks { - if networkoptions.NetworkOptionFromString(scNetwork) != networkoptions.Undefined { - networkOptionsList = append(networkOptionsList, networkoptions.NetworkOptionFromString(scNetwork)) - } - } - - // default network options to remove validator options - if len(networkOptionsList) == 0 { - networkOptionsList = removeValidatorSupportedNetworkOptions - } - + networkOptionsList := networkoptions.GetNetworkFromSidecar(sc, removeValidatorSupportedNetworkOptions) network, err := networkoptions.GetNetworkFromCmdLineFlags( app, "", diff --git a/pkg/networkoptions/network_options.go b/pkg/networkoptions/network_options.go index c3e58ff68..a9bf0f8bb 100644 --- a/pkg/networkoptions/network_options.go +++ b/pkg/networkoptions/network_options.go @@ -178,6 +178,21 @@ func GetSupportedNetworkOptionsForSubnet( return filteredSupportedNetworkOptions, clusterNames, devnetEndpoints, nil } +func GetNetworkFromSidecar(sc models.Sidecar, defaultOption []NetworkOption) []NetworkOption { + networkOptionsList := []NetworkOption{} + for scNetwork := range sc.Networks { + if NetworkOptionFromString(scNetwork) != Undefined { + networkOptionsList = append(networkOptionsList, NetworkOptionFromString(scNetwork)) + } + } + + // default network options to add validator options + if len(networkOptionsList) == 0 { + networkOptionsList = defaultOption + } + return networkOptionsList +} + func GetNetworkFromCmdLineFlags( app *application.Avalanche, promptStr string, From 6ea0a790fc9f9c7be7aee956c1b94c970f5605e5 Mon Sep 17 00:00:00 2001 From: arturrez Date: Fri, 22 Nov 2024 12:30:21 -0800 Subject: [PATCH 3/5] fix case --- pkg/networkoptions/network_options.go | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/pkg/networkoptions/network_options.go b/pkg/networkoptions/network_options.go index a9bf0f8bb..0abca4a3c 100644 --- a/pkg/networkoptions/network_options.go +++ b/pkg/networkoptions/network_options.go @@ -51,27 +51,22 @@ func (n NetworkOption) String() string { } func NetworkOptionFromString(s string) NetworkOption { - switch s { - case "Mainnet": + switch { + case s == "Mainnet": return Mainnet - case "Fuji Testnet": + case s == "Fuji Testnet": return Fuji - case "Local Network": + case s == "Local Network": return Local - case "Etna Devnet": + case s == "Etna Devnet": return EtnaDevnet - case "Devnet": + case s == "Devnet" || strings.Contains(s, "Devnet"): return Devnet - case "Cluster": + case s == "Cluster" || strings.Contains(s, "Cluster"): return Cluster default: - if strings.Contains(s, "Devnet") { - return Devnet - } else if strings.Contains(s, "Cluster") { - return Cluster - } + return Undefined } - return Undefined } type NetworkFlags struct { From f6e8a38baa0e67e3193903ea056e48bc624f97af Mon Sep 17 00:00:00 2001 From: Raymond Sukanto Date: Fri, 22 Nov 2024 17:45:06 -0500 Subject: [PATCH 4/5] allow cluster in flags --- pkg/networkoptions/network_options.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/networkoptions/network_options.go b/pkg/networkoptions/network_options.go index 0abca4a3c..a0e25c115 100644 --- a/pkg/networkoptions/network_options.go +++ b/pkg/networkoptions/network_options.go @@ -257,7 +257,7 @@ func GetNetworkFromCmdLineFlags( } } // unsupported option - if networkOption != Undefined && !slices.Contains(supportedNetworkOptions, networkOption) { + if networkOption != Undefined && !slices.Contains(supportedNetworkOptions, networkOption) && networkOption != Cluster { errMsg := fmt.Errorf("network flag %s is not supported. use one of %s", networkFlagsMap[networkOption], supportedNetworksFlags) if subnetName != "" { clustersMsg := "" From b0ea9e21ff77c7cb470b47f0e510e54d54afdb8b Mon Sep 17 00:00:00 2001 From: Raymond Sukanto Date: Fri, 22 Nov 2024 17:45:32 -0500 Subject: [PATCH 5/5] allow cluster in flags --- pkg/networkoptions/network_options.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/networkoptions/network_options.go b/pkg/networkoptions/network_options.go index a0e25c115..3d75b94b4 100644 --- a/pkg/networkoptions/network_options.go +++ b/pkg/networkoptions/network_options.go @@ -257,6 +257,7 @@ func GetNetworkFromCmdLineFlags( } } // unsupported option + // allow cluster because we can extract underlying network from cluster if networkOption != Undefined && !slices.Contains(supportedNetworkOptions, networkOption) && networkOption != Cluster { errMsg := fmt.Errorf("network flag %s is not supported. use one of %s", networkFlagsMap[networkOption], supportedNetworksFlags) if subnetName != "" {