Skip to content

Commit

Permalink
introduce network.Name()
Browse files Browse the repository at this point in the history
  • Loading branch information
felipemadero committed Nov 7, 2023
1 parent 648550b commit 3944738
Show file tree
Hide file tree
Showing 17 changed files with 53 additions and 43 deletions.
6 changes: 3 additions & 3 deletions cmd/keycmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func getClients(networks []models.Network, cchain bool) (
for _, network := range networks {
pClients[network] = platformvm.NewClient(network.Endpoint)
if cchain {
cClients[network], err = ethclient.Dial(fmt.Sprintf("%s/ext/bc/%s/rpc", network.Endpoint, "C"))
cClients[network], err = ethclient.Dial(network.CChainEndpoint())
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -330,7 +330,7 @@ func getPChainAddrInfo(
chain: "P-Chain (Bech32 format)",
address: pChainAddr,
balance: balance,
network: network.Kind.String(),
network: network.Name(),
}, nil
}

Expand All @@ -354,7 +354,7 @@ func getCChainAddrInfo(
chain: "C-Chain (Ethereum hex format)",
address: cChainAddr,
balance: cChainBalance,
network: network.Kind.String(),
network: network.Name(),
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/nodecmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func statusNode(_ *cobra.Command, args []string) error {
if err != nil {
return err
}
blockchainID := sc.Networks[network.Kind.String()].BlockchainID
blockchainID := sc.Networks[network.Name()].BlockchainID
if blockchainID == ids.Empty {
return ErrNoBlockchainID
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/nodecmd/validatePrimary.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ func convertNanoAvaxToAvaxString(weight uint64) string {

func PrintNodeJoinPrimaryNetworkOutput(nodeID ids.NodeID, weight uint64, network models.Network, start time.Time) {
ux.Logger.PrintToUser("NodeID: %s", nodeID.String())
ux.Logger.PrintToUser("Network: %s", network.Kind.String())
ux.Logger.PrintToUser("Network: %s", network.Name())
ux.Logger.PrintToUser("Start time: %s", start.Format(constants.TimeParseLayout))
ux.Logger.PrintToUser("End time: %s", start.Add(duration).Format(constants.TimeParseLayout))
// we need to divide by 10 ^ 9 since we were using nanoAvax
Expand Down
8 changes: 4 additions & 4 deletions cmd/subnetcmd/addDelegator.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func addPermissionlessDelegator(_ *cobra.Command, args []string) error {
if useLedger && keyName != "" {
return ErrMutuallyExlusiveKeyLedger
}
subnetID := sc.Networks[network.Kind.String()].SubnetID
subnetID := sc.Networks[network.Name()].SubnetID
if os.Getenv(constants.SimulatePublicNetwork) != "" {
subnetID = sc.Networks[models.Local.String()].SubnetID
}
Expand Down Expand Up @@ -161,7 +161,7 @@ func printAddPermissionlessDelOutput(txID ids.ID, nodeID ids.NodeID, network mod
ux.Logger.PrintToUser("Node successfully added as delegator!")
ux.Logger.PrintToUser("TX ID: %s", txID.String())
ux.Logger.PrintToUser("NodeID: %s", nodeID.String())
ux.Logger.PrintToUser("Network: %s", network.Kind.String())
ux.Logger.PrintToUser("Network: %s", network.Name())
ux.Logger.PrintToUser("Start time: %s", start.UTC().Format(constants.TimeParseLayout))
ux.Logger.PrintToUser("End time: %s", endTime.Format(constants.TimeParseLayout))
ux.Logger.PrintToUser("Stake Amount: %d", stakedTokenAmount)
Expand All @@ -180,10 +180,10 @@ func handleAddPermissionlessDelegatorLocal(subnetName string, network models.Net
}
ux.Logger.PrintToUser("Inputs complete, issuing transaction addPermissionlessDelegatorTx...")
ux.Logger.PrintToUser("")
assetID := sc.ElasticSubnet[network.Kind.String()].AssetID
assetID := sc.ElasticSubnet[network.Name()].AssetID
testKey := genesis.EWOQKey
keyChain := secp256k1fx.NewKeychain(testKey)
subnetID := sc.Networks[network.Kind.String()].SubnetID
subnetID := sc.Networks[network.Name()].SubnetID
txID, err := subnet.IssueAddPermissionlessDelegatorTx(keyChain, subnetID, nodeID, stakedTokenAmount, assetID, uint64(start.Unix()), uint64(endTime.Unix()))
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions cmd/subnetcmd/addValidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func addValidator(_ *cobra.Command, args []string) error {
return err
}

subnetID := sc.Networks[network.Kind.String()].SubnetID
subnetID := sc.Networks[network.Name()].SubnetID
if subnetID == ids.Empty {
return errNoSubnetID
}
Expand Down Expand Up @@ -202,7 +202,7 @@ func addValidator(_ *cobra.Command, args []string) error {
}

ux.Logger.PrintToUser("NodeID: %s", nodeID.String())
ux.Logger.PrintToUser("Network: %s", network.Kind.String())
ux.Logger.PrintToUser("Network: %s", network.Name())
ux.Logger.PrintToUser("Start time: %s", start.Format(constants.TimeParseLayout))
ux.Logger.PrintToUser("End time: %s", start.Add(duration).Format(constants.TimeParseLayout))
ux.Logger.PrintToUser("Weight: %d", weight)
Expand Down
8 changes: 4 additions & 4 deletions cmd/subnetcmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func deploySubnet(cmd *cobra.Command, args []string) error {
}

// deploy based on chosen network
ux.Logger.PrintToUser("Deploying %s to %s", chains, network.Kind.String())
ux.Logger.PrintToUser("Deploying %s to %s", chains, network.Name())
chainGenesis, err := app.LoadRawGenesis(chain, network)
if err != nil {
return err
Expand Down Expand Up @@ -350,7 +350,7 @@ func deploySubnet(cmd *cobra.Command, args []string) error {
return err
}
flags := make(map[string]string)
flags[constants.Network] = network.Kind.String()
flags[constants.Network] = network.Name()
utils.HandleTracking(cmd, app, flags)
return app.UpdateSidecarNetworks(&sidecar, network, subnetID, blockchainID)

Expand Down Expand Up @@ -395,7 +395,7 @@ func deploySubnet(cmd *cobra.Command, args []string) error {
}
createSubnet = false
} else if sidecar.Networks != nil {
model, ok := sidecar.Networks[network.Kind.String()]
model, ok := sidecar.Networks[network.Name()]
if ok {
if model.SubnetID != ids.Empty && model.BlockchainID == ids.Empty {
subnetID = model.SubnetID
Expand Down Expand Up @@ -514,7 +514,7 @@ func deploySubnet(cmd *cobra.Command, args []string) error {
}

flags := make(map[string]string)
flags[constants.Network] = network.Kind.String()
flags[constants.Network] = network.Name()
utils.HandleTracking(cmd, app, flags)

// update sidecar
Expand Down
8 changes: 4 additions & 4 deletions cmd/subnetcmd/elastic.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func transformElasticSubnet(cmd *cobra.Command, args []string) error {
return ErrMutuallyExlusiveKeyLedger
}

subnetID := sc.Networks[network.Kind.String()].SubnetID
subnetID := sc.Networks[network.Name()].SubnetID
if os.Getenv(constants.SimulatePublicNetwork) != "" {
subnetID = sc.Networks[models.Local.String()].SubnetID
}
Expand Down Expand Up @@ -394,7 +394,7 @@ func transformElasticSubnet(cmd *cobra.Command, args []string) error {
return err
}
flags := make(map[string]string)
flags[constants.Network] = network.Kind.String()
flags[constants.Network] = network.Name()
if !isFullySigned {
flags[constants.MultiSig] = "multi-sig"
} else {
Expand Down Expand Up @@ -727,8 +727,8 @@ func checkIfTxHasOccurred(
if sc.ElasticSubnet == nil {
return false, ids.Empty
}
if sc.ElasticSubnet[network.Kind.String()].Txs != nil {
txID, ok := sc.ElasticSubnet[network.Kind.String()].Txs[txName]
if sc.ElasticSubnet[network.Name()].Txs != nil {
txID, ok := sc.ElasticSubnet[network.Name()].Txs[txName]
if ok {
return true, txID
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/subnetcmd/import_from_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func importRunningSubnet(*cobra.Command, []string) error {
defer cancel()
options := []rpc.Option{}

ux.Logger.PrintToUser("Getting information from the %s network...", network.Kind.String())
ux.Logger.PrintToUser("Getting information from the %s network...", network.Name())

txBytes, err := client.GetTx(ctx, blockchainID, options...)
if err != nil {
Expand Down Expand Up @@ -205,7 +205,7 @@ func importRunningSubnet(*cobra.Command, []string) error {
Name: subnetName,
VM: vmType,
Networks: map[string]models.NetworkData{
network.Kind.String(): {
network.Name(): {
SubnetID: subnetID,
BlockchainID: blockchainID,
},
Expand Down
10 changes: 5 additions & 5 deletions cmd/subnetcmd/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func joinCmd(_ *cobra.Command, args []string) error {
network = models.LocalNetwork
}

subnetID := sc.Networks[network.Kind.String()].SubnetID
subnetID := sc.Networks[network.Name()].SubnetID
if subnetID == ids.Empty {
return errNoSubnetID
}
Expand Down Expand Up @@ -316,12 +316,12 @@ func writeAvagoChainConfigFiles(
dataDir = filepath.Join(home, ".avalanchego")
}

subnetID := sc.Networks[network.Kind.String()].SubnetID
subnetID := sc.Networks[network.Name()].SubnetID
if subnetID == ids.Empty {
return errNoSubnetID
}
subnetIDStr := subnetID.String()
blockchainID := sc.Networks[network.Kind.String()].BlockchainID
blockchainID := sc.Networks[network.Name()].BlockchainID
if blockchainID == ids.Empty {
return errNoBlockchainID
}
Expand Down Expand Up @@ -390,7 +390,7 @@ func handleValidatorJoinElasticSubnet(sc models.Sidecar, network models.Network,
return ErrMutuallyExlusiveKeyLedger
}

subnetID := sc.Networks[network.Kind.String()].SubnetID
subnetID := sc.Networks[network.Name()].SubnetID
if os.Getenv(constants.SimulatePublicNetwork) != "" {
subnetID = sc.Networks[models.Local.String()].SubnetID
}
Expand Down Expand Up @@ -473,7 +473,7 @@ func printAddPermissionlessValOutput(txID ids.ID, nodeID ids.NodeID, network mod
ux.Logger.PrintToUser("Validator successfully joined elastic subnet!")
ux.Logger.PrintToUser("TX ID: %s", txID.String())
ux.Logger.PrintToUser("NodeID: %s", nodeID.String())
ux.Logger.PrintToUser("Network: %s", network.Kind.String())
ux.Logger.PrintToUser("Network: %s", network.Name())
ux.Logger.PrintToUser("Start time: %s", start.UTC().Format(constants.TimeParseLayout))
ux.Logger.PrintToUser("End time: %s", endTime.Format(constants.TimeParseLayout))
ux.Logger.PrintToUser("Stake Amount: %d", stakedTokenAmount)
Expand Down
4 changes: 2 additions & 2 deletions cmd/subnetcmd/removeValidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func removeValidator(_ *cobra.Command, args []string) error {
return err
}

subnetID := sc.Networks[network.Kind.String()].SubnetID
subnetID := sc.Networks[network.Name()].SubnetID
if subnetID == ids.Empty {
return errNoSubnetID
}
Expand Down Expand Up @@ -180,7 +180,7 @@ func removeValidator(_ *cobra.Command, args []string) error {
}

ux.Logger.PrintToUser("NodeID: %s", nodeID.String())
ux.Logger.PrintToUser("Network: %s", network.Kind.String())
ux.Logger.PrintToUser("Network: %s", network.Name())
ux.Logger.PrintToUser("Inputs complete, issuing transaction to remove the specified validator...")

deployer := subnet.NewPublicDeployer(app, useLedger, kc, network)
Expand Down
2 changes: 1 addition & 1 deletion cmd/subnetcmd/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func stats(_ *cobra.Command, args []string) error {
return err
}

subnetID := sc.Networks[network.Kind.String()].SubnetID
subnetID := sc.Networks[network.Name()].SubnetID
if subnetID == ids.Empty {
return errors.New("no subnetID found for the provided subnet name; has this subnet actually been deployed to this network?")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/subnetcmd/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func printValidators(_ *cobra.Command, args []string) error {
return err
}

deployInfo, ok := sc.Networks[network.Kind.String()]
deployInfo, ok := sc.Networks[network.Name()]
if !ok {
return errors.New("no deployment found for subnet")
}
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 @@ -52,7 +52,7 @@ func commitTx(_ *cobra.Command, args []string) error {
if err != nil {
return err
}
subnetID := sc.Networks[network.Kind.String()].SubnetID
subnetID := sc.Networks[network.Name()].SubnetID
if subnetID == ids.Empty {
return errNoSubnetID
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/transactioncmd/transaction_sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func signTx(_ *cobra.Command, args []string) error {
if err != nil {
return err
}
subnetID := sc.Networks[network.Kind.String()].SubnetID
subnetID := sc.Networks[network.Name()].SubnetID
if subnetID == ids.Empty {
return errNoSubnetID
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/application/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ func (app *Avalanche) UpdateSidecarNetworks(
if sc.Networks == nil {
sc.Networks = make(map[string]models.NetworkData)
}
sc.Networks[network.Kind.String()] = models.NetworkData{
sc.Networks[network.Name()] = models.NetworkData{
SubnetID: subnetID,
BlockchainID: blockchainID,
RPCVersion: sc.RPCVersion,
Expand All @@ -506,8 +506,8 @@ func (app *Avalanche) UpdateSidecarElasticSubnet(
if sc.ElasticSubnet == nil {
sc.ElasticSubnet = make(map[string]models.ElasticSubnet)
}
partialTxs := sc.ElasticSubnet[network.Kind.String()].Txs
sc.ElasticSubnet[network.Kind.String()] = models.ElasticSubnet{
partialTxs := sc.ElasticSubnet[network.Name()].Txs
sc.ElasticSubnet[network.Name()] = models.ElasticSubnet{
SubnetID: subnetID,
AssetID: assetID,
PChainTXID: pchainTXID,
Expand All @@ -527,12 +527,12 @@ func (app *Avalanche) UpdateSidecarPermissionlessValidator(
nodeID string,
txID ids.ID,
) error {
elasticSubnet := sc.ElasticSubnet[network.Kind.String()]
elasticSubnet := sc.ElasticSubnet[network.Name()]
if elasticSubnet.Validators == nil {
elasticSubnet.Validators = make(map[string]models.PermissionlessValidators)
}
elasticSubnet.Validators[nodeID] = models.PermissionlessValidators{TxID: txID}
sc.ElasticSubnet[network.Kind.String()] = elasticSubnet
sc.ElasticSubnet[network.Name()] = elasticSubnet
if err := app.UpdateSidecar(sc); err != nil {
return err
}
Expand All @@ -549,11 +549,11 @@ func (app *Avalanche) UpdateSidecarElasticSubnetPartialTx(
sc.ElasticSubnet = make(map[string]models.ElasticSubnet)
}
partialTxs := make(map[string]ids.ID)
if sc.ElasticSubnet[network.Kind.String()].Txs != nil {
partialTxs = sc.ElasticSubnet[network.Kind.String()].Txs
if sc.ElasticSubnet[network.Name()].Txs != nil {
partialTxs = sc.ElasticSubnet[network.Name()].Txs
}
partialTxs[txName] = txID
sc.ElasticSubnet[network.Kind.String()] = models.ElasticSubnet{
sc.ElasticSubnet[network.Name()] = models.ElasticSubnet{
Txs: partialTxs,
}
return app.UpdateSidecar(sc)
Expand Down
14 changes: 12 additions & 2 deletions pkg/models/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package models

import (
"fmt"

"github.com/ava-labs/avalanche-cli/pkg/constants"
avago_constants "github.com/ava-labs/avalanchego/utils/constants"
)
Expand All @@ -17,8 +19,8 @@ const (
Devnet
)

func (s NetworkKind) String() string {
switch s {
func (nk NetworkKind) String() string {
switch nk {
case Mainnet:
return "Mainnet"
case Fuji:
Expand Down Expand Up @@ -80,3 +82,11 @@ func NetworkFromNetworkID(networkID uint32) Network {
}
return UndefinedNetwork
}

func (n Network) Name() string {
return n.Kind.String()
}

func (n Network) CChainEndpoint() string {
return fmt.Sprintf("%s/ext/bc/%s/rpc", n.Endpoint, "C")
}
2 changes: 1 addition & 1 deletion tests/e2e/utils/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func AddSubnetIDToSidecar(subnetName string, network models.Network, subnetID st
if err != nil {
return err
}
sc.Networks[network.Kind.String()] = models.NetworkData{
sc.Networks[network.Name()] = models.NetworkData{
SubnetID: subnetIDstr,
}

Expand Down

0 comments on commit 3944738

Please sign in to comment.