Skip to content

Commit

Permalink
address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
felipemadero committed Oct 12, 2024
1 parent e3f1d97 commit 016fa62
Show file tree
Hide file tree
Showing 11 changed files with 885 additions and 844 deletions.
36 changes: 18 additions & 18 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,14 @@ func (c *client) Start(ctx context.Context, execPath string, opts ...OpOption) (
ret.applyOpts(opts)

req := &rpcpb.StartRequest{
NetworkId: ret.networkID,
ExecPath: execPath,
NumNodes: &ret.numNodes,
ChainConfigs: ret.chainConfigs,
UpgradeConfigs: ret.upgradeConfigs,
SubnetConfigs: ret.subnetConfigs,
ZeroIpIfPublicHttpHost: ret.zeroIPIfPublicHTTPHost,
FreshStakingIds: ret.freshStakingIds,
NetworkId: ret.networkID,
ExecPath: execPath,
NumNodes: &ret.numNodes,
ChainConfigs: ret.chainConfigs,
UpgradeConfigs: ret.upgradeConfigs,
SubnetConfigs: ret.subnetConfigs,
ZeroIp: ret.zeroIP,
FreshStakingIds: ret.freshStakingIds,
}
if ret.trackSubnets != "" {
req.WhitelistedSubnets = &ret.trackSubnets
Expand Down Expand Up @@ -409,13 +409,13 @@ func (c *client) LoadSnapshot(
ret := &Op{}
ret.applyOpts(opts)
req := rpcpb.LoadSnapshotRequest{
SnapshotName: snapshotName,
SnapshotPath: ret.snapshotPath,
ChainConfigs: ret.chainConfigs,
UpgradeConfigs: ret.upgradeConfigs,
SubnetConfigs: ret.subnetConfigs,
InPlace: inPlace,
ZeroIpIfPublicHttpHost: ret.zeroIPIfPublicHTTPHost,
SnapshotName: snapshotName,
SnapshotPath: ret.snapshotPath,
ChainConfigs: ret.chainConfigs,
UpgradeConfigs: ret.upgradeConfigs,
SubnetConfigs: ret.subnetConfigs,
InPlace: inPlace,
ZeroIp: ret.zeroIP,
}
if ret.execPath != "" {
req.ExecPath = &ret.execPath
Expand Down Expand Up @@ -530,7 +530,7 @@ type Op struct {
bootstrapNodeIPPortPairs []string
upgradePath string
snapshotPath string
zeroIPIfPublicHTTPHost bool
zeroIP bool
freshStakingIds bool
}

Expand Down Expand Up @@ -685,9 +685,9 @@ func WithSnapshotPath(snapshotPath string) OpOption {
}
}

func WithZeroIPIfPublicHTTPHost(zeroIPIfPublicHTTPHost bool) OpOption {
func WithZeroIP(zeroIP bool) OpOption {
return func(op *Op) {
op.zeroIPIfPublicHTTPHost = zeroIPIfPublicHTTPHost
op.zeroIP = zeroIP
}
}

Expand Down
11 changes: 6 additions & 5 deletions local/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,12 @@ func (ln *localNetwork) getMinValidatorWeight() uint64 {
// get node client URI for an arbitrary node in the network
func (ln *localNetwork) getClientURI() (string, error) { //nolint
node := ln.getNode()
clientURI := fmt.Sprintf("http://%s:%d", node.GetIP(), node.GetAPIPort())
ln.log.Info("getClientURI",
clientURI := node.GetURI()
ln.log.Info(
"getClientURI",
zap.String("nodeName", node.GetName()),
zap.String("uri", clientURI))
zap.String("uri", clientURI),
)
return clientURI, nil
}

Expand Down Expand Up @@ -1635,8 +1637,7 @@ func (ln *localNetwork) reloadVMPlugins(ctx context.Context) error {
if node.paused {
continue
}
uri := fmt.Sprintf("http://%s:%d", node.GetIP(), node.GetAPIPort())
adminCli := admin.NewClient(uri)
adminCli := admin.NewClient(node.GetURI())
cctx, cancel := createDefaultCtx(ctx)
_, failedVMs, err := adminCli.LoadVMs(cctx)
cancel()
Expand Down
86 changes: 48 additions & 38 deletions local/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,23 @@ import (
)

const (
defaultNodeNamePrefix = "node"
configFileName = "config.json"
upgradeConfigFileName = "upgrade.json"
stakingTLSKeyFileName = "staker.key"
stakingCertFileName = "staker.crt"
stakingSignerKeyFileName = "signer.key"
genesisFileName = "genesis.json"
upgradeFileName = "upgrade.json"
stopTimeout = 30 * time.Second
healthCheckFreq = 3 * time.Second
snapshotPrefix = "anr-snapshot-"
networkRootDirPrefix = "network"
defaultDBSubdir = "db"
defaultLogsSubdir = "logs"
nodeStartupTime = 2 * time.Second
defaultNodeNamePrefix = "node"
configFileName = "config.json"
upgradeConfigFileName = "upgrade.json"
stakingTLSKeyFileName = "staker.key"
stakingCertFileName = "staker.crt"
stakingSignerKeyFileName = "signer.key"
genesisFileName = "genesis.json"
upgradeFileName = "upgrade.json"
stopTimeout = 30 * time.Second
healthCheckFreq = 3 * time.Second
snapshotPrefix = "anr-snapshot-"
networkRootDirPrefix = "network"
defaultDBSubdir = "db"
defaultLogsSubdir = "logs"
nodeStartupTime = 1 * time.Second
processContextWaitTimeout = 3 * time.Second
processContextCheckInterval = 100 * time.Millisecond
)

// interface compliance
Expand Down Expand Up @@ -130,7 +132,7 @@ type localNetwork struct {
walletPrivateKey string
// nodes always returns 127.0.0.1 as IP
// if not set, may return 0.0.0.0 depending on httpHost settings
zeroIPIfPublicHTTPHost bool
zeroIP bool
}

type deprecatedFlagEsp struct {
Expand Down Expand Up @@ -179,7 +181,7 @@ func NewNetwork(
redirectStdout bool,
redirectStderr bool,
walletPrivateKey string,
zeroIPIfPublicHTTPHost bool,
zeroIP bool,
) (network.Network, error) {
beaconSet, err := utils.BeaconMapToSet(networkConfig.BeaconConfig)
if err != nil {
Expand All @@ -202,7 +204,7 @@ func NewNetwork(
redirectStderr,
walletPrivateKey,
beaconSet,
zeroIPIfPublicHTTPHost,
zeroIP,
)
if err != nil {
return net, err
Expand All @@ -225,7 +227,7 @@ func newNetwork(
redirectStderr bool,
walletPrivateKey string,
beaconSet beacon.Set,
zeroIPIfPublicHTTPHost bool,
zeroIP bool,
) (*localNetwork, error) {
var err error
if rootDir == "" {
Expand Down Expand Up @@ -269,7 +271,7 @@ func newNetwork(
subnetID2ElasticSubnetID: map[ids.ID]ids.ID{},
blockchainAliases: map[string][]string{},
walletPrivateKey: walletPrivateKey,
zeroIPIfPublicHTTPHost: zeroIPIfPublicHTTPHost,
zeroIP: zeroIP,
}
return net, nil
}
Expand All @@ -295,7 +297,7 @@ func NewDefaultNetwork(
reassignPortsIfUsed bool,
redirectStdout bool,
redirectStderr bool,
zeroIPIfPublicHTTPHost bool,
zeroIP bool,
) (network.Network, error) {
config, err := NewDefaultConfig(binaryPath, constants.DefaultNetworkID, "", "", nil)
if err != nil {
Expand All @@ -311,7 +313,7 @@ func NewDefaultNetwork(
redirectStdout,
redirectStderr,
"",
zeroIPIfPublicHTTPHost,
zeroIP,
)
}

Expand Down Expand Up @@ -708,21 +710,21 @@ func (ln *localNetwork) addNode(nodeConfig node.Config) (node.Node, error) {

// Create a wrapper for this node so we can reference it later
node := &localNode{
name: nodeConfig.Name,
nodeID: nodeID,
networkID: ln.networkID,
apiPort: nodeData.apiPort,
p2pPort: nodeData.p2pPort,
publicIP: nodeData.publicIP,
getConnFunc: defaultGetConnFunc,
dataDir: nodeData.dataDir,
dbDir: nodeData.dbDir,
logsDir: nodeData.logsDir,
config: nodeConfig,
pluginDir: nodeData.pluginDir,
httpHost: nodeData.httpHost,
zeroIPIfPublicHTTPHost: ln.zeroIPIfPublicHTTPHost,
attachedPeers: map[string]peer.Peer{},
name: nodeConfig.Name,
nodeID: nodeID,
networkID: ln.networkID,
apiPort: nodeData.apiPort,
p2pPort: nodeData.p2pPort,
publicIP: nodeData.publicIP,
getConnFunc: defaultGetConnFunc,
dataDir: nodeData.dataDir,
dbDir: nodeData.dbDir,
logsDir: nodeData.logsDir,
config: nodeConfig,
pluginDir: nodeData.pluginDir,
httpHost: nodeData.httpHost,
zeroIP: ln.zeroIP,
attachedPeers: map[string]peer.Peer{},
}

// Start the AvalancheGo node and pass it the flags defined above
Expand All @@ -737,6 +739,14 @@ func (ln *localNetwork) addNode(nodeConfig node.Config) (node.Node, error) {

if node.apiPort == 0 {
processFilePath := filepath.Join(nodeData.dataDir, config.DefaultProcessContextFilename)
if err := utils.WaitForFile(
processFilePath,
processContextWaitTimeout,
processContextCheckInterval,
"node process info file was not generated",
); err != nil {
return node, err
}
processFileBytes, err := os.ReadFile(processFilePath)
if err != nil {
return node, fmt.Errorf("could not read node process info file %s", processFilePath)
Expand All @@ -751,7 +761,7 @@ func (ln *localNetwork) addNode(nodeConfig node.Config) (node.Node, error) {
}
p2pPort, err := strconv.ParseUint(stakingAddressWords[len(stakingAddressWords)-1], 10, 16)
if err != nil {
return node, fmt.Errorf("unexpected format on staking address %s: %w", processContext.StakingAddress, err)
return node, fmt.Errorf("unexpected format on P2P port %s: %w", processContext.StakingAddress, err)
}
uriWords := strings.Split(processContext.URI, ":")
if len(uriWords) == 0 {
Expand Down
9 changes: 7 additions & 2 deletions local/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type localNode struct {
// and can be resumed
paused bool
// if set, returns 0.0.0.0 if httpHost setting is public
zeroIPIfPublicHTTPHost bool
zeroIP bool
}

func defaultGetConnFunc(ctx context.Context, node node.Node) (net.Conn, error) {
Expand Down Expand Up @@ -215,12 +215,17 @@ func (node *localNode) GetAPIClient() api.Client {

// See node.Node
func (node *localNode) GetIP() string {
if node.zeroIPIfPublicHTTPHost && (node.httpHost == "0.0.0.0" || node.httpHost == ".") {
if node.zeroIP && (node.httpHost == "0.0.0.0" || node.httpHost == ".") {
return "0.0.0.0"
}
return node.publicIP
}

// See node.Node
func (node *localNode) GetURI() string {
return fmt.Sprintf("http://%s:%d", node.GetIP(), node.GetAPIPort())
}

// See node.Node
func (node *localNode) GetP2PPort() uint16 {
return node.p2pPort
Expand Down
13 changes: 6 additions & 7 deletions local/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func NewNetworkFromSnapshot(
inPlace bool,
walletPrivateKey string,
beaconConfig map[ids.NodeID]netip.AddrPort,
zeroIPIfPublicHTTPHost bool,
zeroIP bool,
) (network.Network, error) {
if inPlace {
if rootDir != "" {
Expand Down Expand Up @@ -108,7 +108,7 @@ func NewNetworkFromSnapshot(
redirectStderr,
walletPrivateKey,
beaconSet,
zeroIPIfPublicHTTPHost,
zeroIP,
)
if err != nil {
return net, err
Expand Down Expand Up @@ -449,14 +449,13 @@ func getSnapshotDir(
snapshotName string,
snapshotPath string,
) string {
if snapshotPath != "" {
return snapshotPath
}
if snapshotsDir == "" {
snapshotsDir = DefaultSnapshotsDir
}
snapshotDir := filepath.Join(snapshotsDir, snapshotPrefix+snapshotName)
if snapshotPath != "" {
snapshotDir = snapshotPath
}
return snapshotDir
return filepath.Join(snapshotsDir, snapshotPrefix+snapshotName)
}

func RemoveSnapshot(
Expand Down
2 changes: 2 additions & 0 deletions network/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type Node interface {
GetP2PPort() uint16
// Return this node's HTTP API port.
GetAPIPort() uint16
// Return this node's URI (e.g. http://127.0.0.1:9650).
GetURI() string
// Starts a new test peer, connects it to the given node, and returns the peer.
// [handler] defines how the test peer handles messages it receives.
// The test peer can be used to send messages to the node it's attached to.
Expand Down
Loading

0 comments on commit 016fa62

Please sign in to comment.