Skip to content

Commit

Permalink
persist api/staking ports on node restart
Browse files Browse the repository at this point in the history
  • Loading branch information
darioush committed Jul 29, 2022
1 parent 9207e77 commit 69a67ed
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
24 changes: 15 additions & 9 deletions local/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -1109,17 +1109,23 @@ func (ln *localNetwork) buildFlags(
return buildFlagsReturn{}, err
}

// Use random free API port unless given in config file
apiPort, err := getPort(nodeConfig.Flags, configFile, config.HTTPPortKey)
if err != nil {
return buildFlagsReturn{}, err
// Prefer API port and P2P (staking) port from nodeConfig
apiPort, p2pPort := nodeConfig.ApiPort, nodeConfig.StakingPort

if apiPort == 0 {
// Use random free API port unless given in config file
apiPort, err = getPort(nodeConfig.Flags, configFile, config.HTTPPortKey)
if err != nil {
return buildFlagsReturn{}, err
}
}

// Use a random free P2P (staking) port unless given in config file
// Use random free API port unless given in config file
p2pPort, err := getPort(nodeConfig.Flags, configFile, config.StakingPortKey)
if err != nil {
return buildFlagsReturn{}, err
if p2pPort == 0 {
// Use a random free P2P (staking) port unless given in config file
p2pPort, err = getPort(nodeConfig.Flags, configFile, config.StakingPortKey)
if err != nil {
return buildFlagsReturn{}, err
}
}

// Flags for AvalancheGo
Expand Down
5 changes: 5 additions & 0 deletions network/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ type Config struct {
RedirectStdout bool `json:"redirectStdout"`
// If non-nil, direct this node's Stderr to os.Stderr
RedirectStderr bool `json:"redirectStderr"`

// If non-zero, prefer these ports to the one specified in the config file
// Used to persist node ports accross restart
ApiPort uint16
StakingPort uint16
}

// Validate returns an error if this config is invalid
Expand Down
4 changes: 4 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,10 @@ func (s *server) RestartNode(ctx context.Context, req *rpcpb.RestartNodeRequest)
return nil, fmt.Errorf("failed to generate json node config string: %w", err)
}

// keep the same api & staking ports across the restart
nodeConfig.ApiPort = node.GetAPIPort()
nodeConfig.StakingPort = node.GetP2PPort()

nodeConfig.BinaryPath = nodeInfo.ExecPath
nodeConfig.RedirectStdout = s.cfg.RedirectNodesOutput
nodeConfig.RedirectStderr = s.cfg.RedirectNodesOutput
Expand Down

0 comments on commit 69a67ed

Please sign in to comment.