Skip to content

Commit

Permalink
Merge pull request #284 from ava-labs/static-ports-by-default
Browse files Browse the repository at this point in the history
Static ports by default
  • Loading branch information
felipemadero authored Sep 27, 2022
2 parents 30b1a20 + 712b546 commit c3202a5
Show file tree
Hide file tree
Showing 12 changed files with 426 additions and 372 deletions.
8 changes: 8 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func (c *client) Start(ctx context.Context, execPath string, opts ...OpOption) (
req.CustomNodeConfigs = ret.customNodeConfigs
}
req.ReassignPortsIfUsed = &ret.reassignPortsIfUsed
req.DynamicPorts = &ret.dynamicPorts

c.log.Info("start")
return c.controlc.Start(ctx, req)
Expand Down Expand Up @@ -340,6 +341,7 @@ type Op struct {
chainConfigs map[string]string
upgradeConfigs map[string]string
reassignPortsIfUsed bool
dynamicPorts bool
}

type OpOption func(*Op)
Expand Down Expand Up @@ -426,6 +428,12 @@ func WithReassignPortsIfUsed(reassignPortsIfUsed bool) OpOption {
}
}

func WithDynamicPorts(dynamicPorts bool) OpOption {
return func(op *Op) {
op.dynamicPorts = dynamicPorts
}
}

func isClientCanceled(ctxErr error, err error) bool {
if ctxErr != nil {
return true
Expand Down
10 changes: 9 additions & 1 deletion cmd/control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ var (
chainConfigs string
upgradeConfigs string
reassignPortsIfUsed bool
dynamicPorts bool
)

func newStartCommand() *cobra.Command {
Expand Down Expand Up @@ -175,7 +176,13 @@ func newStartCommand() *cobra.Command {
&reassignPortsIfUsed,
"reassign-ports-if-used",
false,
"true to reassign given ports if already taken",
"true to reassign default/given ports if already taken",
)
cmd.PersistentFlags().BoolVar(
&dynamicPorts,
"dynamic-ports",
false,
"true to assign dynamic ports",
)
if err := cmd.MarkPersistentFlagRequired("avalanchego-path"); err != nil {
panic(err)
Expand All @@ -196,6 +203,7 @@ func startFunc(cmd *cobra.Command, args []string) error {
client.WithWhitelistedSubnets(whitelistedSubnets),
client.WithRootDataDir(rootDataDir),
client.WithReassignPortsIfUsed(reassignPortsIfUsed),
client.WithDynamicPorts(dynamicPorts),
}

if globalNodeConfig != "" {
Expand Down
3 changes: 2 additions & 1 deletion local/default/node1/flags.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"http-port": 9650
"http-port": 9650,
"staking-port": 9651
}
3 changes: 2 additions & 1 deletion local/default/node2/flags.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"http-port": 9652
"http-port": 9652,
"staking-port": 9653
}
3 changes: 2 additions & 1 deletion local/default/node3/flags.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"http-port": 9654
"http-port": 9654,
"staking-port": 9655
}
3 changes: 2 additions & 1 deletion local/default/node4/flags.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"http-port": 9656
"http-port": 9656,
"staking-port": 9657
}
3 changes: 2 additions & 1 deletion local/default/node5/flags.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"http-port": 9658
"http-port": 9658,
"staking-port": 9659
}
27 changes: 20 additions & 7 deletions local/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,23 @@ func NewDefaultConfigNNodes(binaryPath string, numNodes uint32) (network.Config,
netConfig := NewDefaultConfig(binaryPath)
if int(numNodes) > len(netConfig.NodeConfigs) {
toAdd := int(numNodes) - len(netConfig.NodeConfigs)
refNodeConfig := netConfig.NodeConfigs[0]
refNodeConfig := netConfig.NodeConfigs[len(netConfig.NodeConfigs)-1]
refApiPortIntf, ok := refNodeConfig.Flags[config.HTTPPortKey]
if !ok {
return netConfig, fmt.Errorf("could not get last standard api port from config")
}
refApiPort, ok := refApiPortIntf.(float64)
if !ok {
return netConfig, fmt.Errorf("expected float64 for last standard api port, got %T", refApiPortIntf)
}
refStakingPortIntf, ok := refNodeConfig.Flags[config.StakingPortKey]
if !ok {
return netConfig, fmt.Errorf("could not get last standard staking port from config")
}
refStakingPort, ok := refStakingPortIntf.(float64)
if !ok {
return netConfig, fmt.Errorf("expected float64 for last standard api port, got %T", refStakingPortIntf)
}
for i := 0; i < toAdd; i++ {
nodeConfig := refNodeConfig
stakingCert, stakingKey, err := staking.NewCertAndKeyBytes()
Expand All @@ -368,13 +384,10 @@ func NewDefaultConfigNNodes(binaryPath string, numNodes uint32) (network.Config,
}
nodeConfig.StakingKey = string(stakingKey)
nodeConfig.StakingCert = string(stakingCert)
// replace api port in refNodeConfig.ConfigFile
apiPort, err := getFreePort()
if err != nil {
return netConfig, fmt.Errorf("couldn't get free API port: %w", err)
}
// replace ports
nodeConfig.Flags = map[string]interface{}{
config.HTTPPortKey: int(apiPort),
config.HTTPPortKey: int(refApiPort) + (i+1)*2,
config.StakingPortKey: int(refStakingPort) + (i+1)*2,
}
netConfig.NodeConfigs = append(netConfig.NodeConfigs, nodeConfig)
}
Expand Down
722 changes: 367 additions & 355 deletions rpcpb/rpc.pb.go

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion rpcpb/rpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,11 @@ message StartRequest {
// the contents provided here.
map<string, string> upgrade_configs = 10;

// reassign given ports (currently if using custom_node_configs) if they are already taken
// reassign default/custom ports if they are already taken
optional bool reassign_ports_if_used = 11;

// use dynamic ports instead of default ones
optional bool dynamic_ports = 12;
}

message StartResponse {
Expand Down
10 changes: 7 additions & 3 deletions server/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ type localNetworkOptions struct {
logLevel logging.Level

reassignPortsIfUsed bool

dynamicPorts bool
}

func newLocalNetwork(opts localNetworkOptions) (*localNetwork, error) {
Expand Down Expand Up @@ -162,9 +164,11 @@ func (lc *localNetwork) createConfig() error {
return err
}

// remove http port defined in local network config, to get dynamic port
// generation as default when creating a new network
delete(cfg.NodeConfigs[i].Flags, config.HTTPPortKey)
if lc.options.dynamicPorts {
// remove http port defined in local network config, to get dynamic port generation
delete(cfg.NodeConfigs[i].Flags, config.HTTPPortKey)
delete(cfg.NodeConfigs[i].Flags, config.StakingPortKey)
}

cfg.NodeConfigs[i].Flags[config.LogsDirKey] = logDir
cfg.NodeConfigs[i].Flags[config.DBPathKey] = dbDir
Expand Down
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ func (s *server) Start(ctx context.Context, req *rpcpb.StartRequest) (*rpcpb.Sta
upgradeConfigs: req.UpgradeConfigs,
logLevel: s.cfg.LogLevel,
reassignPortsIfUsed: req.GetReassignPortsIfUsed(),
dynamicPorts: req.GetDynamicPorts(),

// to block racey restart
// "s.network.start" runs asynchronously
Expand Down

0 comments on commit c3202a5

Please sign in to comment.