Skip to content

Commit

Permalink
address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
felipemadero committed Nov 4, 2024
1 parent e160b05 commit 2885ec5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 51 deletions.
7 changes: 4 additions & 3 deletions cmd/blockchaincmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/ava-labs/avalanche-cli/pkg/contract"
"github.com/ava-labs/avalanche-cli/pkg/utils"
"github.com/ava-labs/avalanchego/config"
"github.com/ava-labs/avalanchego/utils/formatting/address"
"github.com/ava-labs/avalanchego/vms/platformvm/fx"
"github.com/ava-labs/avalanchego/vms/platformvm/signer"
Expand Down Expand Up @@ -534,14 +535,14 @@ func deployBlockchain(cmd *cobra.Command, args []string) error {
}
}
network = models.NewNetworkFromCluster(network, clusterName)
nodeConfig := ""
nodeConfig := map[string]interface{}{}
if app.AvagoNodeConfigExists(blockchainName) {
nodeConfigBytes, err := os.ReadFile(app.GetAvagoNodeConfigPath(blockchainName))
nodeConfig, err = utils.ReadJSON(app.GetAvagoNodeConfigPath(blockchainName))
if err != nil {
return err
}
nodeConfig = string(nodeConfigBytes)
}
nodeConfig[config.PartialSyncPrimaryNetworkKey] = true
// anrSettings, avagoVersionSettings, globalNetworkFlags are empty
if err = node.StartLocalNode(
app,
Expand Down
13 changes: 9 additions & 4 deletions cmd/nodecmd/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ package nodecmd

import (
"fmt"
"os"

"github.com/ava-labs/avalanche-cli/pkg/cobrautils"
"github.com/ava-labs/avalanche-cli/pkg/networkoptions"
"github.com/ava-labs/avalanche-cli/pkg/node"
"github.com/ava-labs/avalanche-cli/pkg/utils"
"github.com/ava-labs/avalanche-cli/pkg/ux"
"github.com/ava-labs/avalanchego/config"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -151,14 +152,18 @@ func localStartNode(_ *cobra.Command, args []string) error {
UseLatestAvalanchegoReleaseVersion: useLatestAvalanchegoReleaseVersion,
UseAvalanchegoVersionFromSubnet: useAvalanchegoVersionFromSubnet,
}
nodeConfig := ""
var (
err error
nodeConfig map[string]interface{}
)
if nodeConfigPath != "" {
nodeConfigBytes, err := os.ReadFile(nodeConfigPath)
nodeConfig, err = utils.ReadJSON(nodeConfigPath)
if err != nil {
return err
}
nodeConfig = string(nodeConfigBytes)
}
nodeConfig[config.PartialSyncPrimaryNetworkKey] = true

return node.StartLocalNode(
app,
clusterName,
Expand Down
34 changes: 9 additions & 25 deletions pkg/node/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package node

import (
"encoding/hex"
"encoding/json"
"fmt"
"os"
"path/filepath"
Expand All @@ -23,7 +24,6 @@ import (
"github.com/ava-labs/avalanche-network-runner/client"
anrutils "github.com/ava-labs/avalanche-network-runner/utils"
"github.com/ava-labs/avalanchego/api/info"
"github.com/ava-labs/avalanchego/config"
"github.com/ava-labs/avalanchego/ids"
avagoconstants "github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/logging"
Expand Down Expand Up @@ -155,7 +155,7 @@ func StartLocalNode(
useEtnaDevnet bool,
avalanchegoBinaryPath string,
numNodes uint32,
nodeConfig string,
nodeConfig map[string]interface{},
anrSettings ANRSettings,
avaGoVersionSetting AvalancheGoVersionSettings,
globalNetworkFlags networkoptions.NetworkFlags,
Expand All @@ -182,27 +182,6 @@ func StartLocalNode(
localDataExists = false
}

// set node configs
if nodeConfig == "" {
nodeConfig = "{}"
}
nodeConfig, err = utils.SetJSONKey(
nodeConfig,
config.PartialSyncPrimaryNetworkKey,
true,
)
if err != nil {
return err
}
nodeConfig, err = utils.SetJSONKey(
nodeConfig,
config.ProposerVMUseCurrentHeightKey,
true,
)
if err != nil {
return err
}

// check if this is existing cluster
rootDir := app.GetLocalDir(clusterName)
pluginDir := filepath.Join(rootDir, "node1", "plugins")
Expand Down Expand Up @@ -258,14 +237,19 @@ func StartLocalNode(
return nil
}

nodeConfigBytes, err := json.Marshal(nodeConfig)
if err != nil {
return err
}
nodeConfigStr := string(nodeConfigBytes)
if localClusterExists && localDataExists {
ux.Logger.GreenCheckmarkToUser("Local cluster %s found. Booting up...", clusterName)
loadSnapshotOpts := []client.OpOption{
client.WithExecPath(avalancheGoBinPath),
client.WithReassignPortsIfUsed(true),
client.WithPluginDir(pluginDir),
client.WithSnapshotPath(rootDir),
client.WithGlobalNodeConfig(nodeConfig),
client.WithGlobalNodeConfig(nodeConfigStr),
}
// load snapshot for existing network
if _, err := cli.LoadSnapshot(
Expand Down Expand Up @@ -366,7 +350,7 @@ func StartLocalNode(
client.WithPluginDir(pluginDir),
client.WithFreshStakingIds(true),
client.WithZeroIP(false),
client.WithGlobalNodeConfig(nodeConfig),
client.WithGlobalNodeConfig(nodeConfigStr),
}
if anrSettings.GenesisPath != "" && utils.FileExists(anrSettings.GenesisPath) {
anrOpts = append(anrOpts, client.WithGenesisPath(anrSettings.GenesisPath))
Expand Down
19 changes: 0 additions & 19 deletions pkg/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,22 +589,3 @@ func LogLevelToEmoji(logLevel string) (string, error) {
}
return levelEmoji, nil
}

// Set k=v in JSON string
// e.g., "track-subnets" is the key and value is "a,b,c".
func SetJSONKey(jsonBody string, k string, v interface{}) (string, error) {
var config map[string]interface{}
if err := json.Unmarshal([]byte(jsonBody), &config); err != nil {
return "", err
}
if v == nil {
delete(config, k)
} else {
config[k] = v
}
updatedJSON, err := json.Marshal(config)
if err != nil {
return "", err
}
return string(updatedJSON), nil
}
33 changes: 33 additions & 0 deletions pkg/utils/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,36 @@ func ValidateJSON(path string) ([]byte, error) {

return contentBytes, nil
}

// ReadJSON takes a json string and returns its associated map
// if it contains valid JSON
func ReadJSON(path string) (map[string]interface{}, error) {
var content map[string]interface{}
contentBytes, err := os.ReadFile(path)
if err != nil {
return nil, err
}
if err := json.Unmarshal(contentBytes, &content); err != nil {
return nil, fmt.Errorf("this looks like invalid JSON: %w", err)
}
return content, nil
}

// Set k=v in JSON string
// e.g., "track-subnets" is the key and value is "a,b,c".
func SetJSONKey(jsonBody string, k string, v interface{}) (string, error) {
var config map[string]interface{}
if err := json.Unmarshal([]byte(jsonBody), &config); err != nil {
return "", err
}
if v == nil {
delete(config, k)
} else {
config[k] = v
}
updatedJSON, err := json.Marshal(config)
if err != nil {
return "", err
}
return string(updatedJSON), nil
}

0 comments on commit 2885ec5

Please sign in to comment.