Skip to content

Commit fca522a

Browse files
committed
enable user to specify network id
1 parent 24d0d6a commit fca522a

File tree

9 files changed

+731
-668
lines changed

9 files changed

+731
-668
lines changed

client/client.go

+8
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ func (c *client) Start(ctx context.Context, execPath string, opts ...OpOption) (
117117
ret.applyOpts(opts)
118118

119119
req := &rpcpb.StartRequest{
120+
NetworkId: ret.networkID,
120121
ExecPath: execPath,
121122
NumNodes: &ret.numNodes,
122123
ChainConfigs: ret.chainConfigs,
@@ -454,6 +455,7 @@ type Op struct {
454455
subnetConfigs map[string]string
455456
reassignPortsIfUsed bool
456457
dynamicPorts bool
458+
networkID *uint32
457459
}
458460

459461
type OpOption func(*Op)
@@ -470,6 +472,12 @@ func WithGlobalNodeConfig(nodeConfig string) OpOption {
470472
}
471473
}
472474

475+
func WithNetworkID(networkId uint32) OpOption {
476+
return func(op *Op) {
477+
op.networkID = &networkId
478+
}
479+
}
480+
473481
func WithNumNodes(numNodes uint32) OpOption {
474482
return func(op *Op) {
475483
op.numNodes = numNodes

cmd/control/control.go

+11
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ var (
104104
subnetConfigs string
105105
reassignPortsIfUsed bool
106106
dynamicPorts bool
107+
networkId uint32
107108
)
108109

109110
func setLogs() error {
@@ -176,6 +177,12 @@ func newStartCommand() *cobra.Command {
176177
"",
177178
"avalanchego binary path",
178179
)
180+
cmd.PersistentFlags().Uint32Var(
181+
&networkId,
182+
"network-id",
183+
0,
184+
"network id to assign to the network",
185+
)
179186
cmd.PersistentFlags().Uint32Var(
180187
&numNodes,
181188
"number-of-nodes",
@@ -267,6 +274,10 @@ func startFunc(*cobra.Command, []string) error {
267274
client.WithDynamicPorts(dynamicPorts),
268275
}
269276

277+
if networkId != 0 {
278+
opts = append(opts, client.WithNetworkID(networkId))
279+
}
280+
270281
if globalNodeConfig != "" {
271282
ux.Print(log, logging.Yellow.Wrap("global node config provided, will be applied to all nodes: %s"), globalNodeConfig)
272283
// validate it's valid JSON

local/network.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,19 @@ func (ln *localNetwork) loadConfig(ctx context.Context, networkConfig network.Co
444444

445445
ln.genesis = []byte(networkConfig.Genesis)
446446

447-
var err error
448-
ln.networkID, err = utils.NetworkIDFromGenesis([]byte(networkConfig.Genesis))
449-
if err != nil {
450-
return fmt.Errorf("couldn't get network ID from genesis: %w", err)
447+
if networkConfig.NetworkID != nil {
448+
ln.networkID = *networkConfig.NetworkID
449+
genesis, err := utils.SetGenesisNetworkID(ln.genesis, ln.networkID)
450+
if err != nil {
451+
return fmt.Errorf("couldn't set network ID to genesis: %w", err)
452+
}
453+
ln.genesis = genesis
454+
} else {
455+
var err error
456+
ln.networkID, err = utils.NetworkIDFromGenesis(ln.genesis)
457+
if err != nil {
458+
return fmt.Errorf("couldn't get network ID from genesis: %w", err)
459+
}
451460
}
452461

453462
// save node defaults

network/config.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ type AddrAndBalance struct {
5050
// Config that defines a network when it is created.
5151
type Config struct {
5252
// Must not be empty
53-
Genesis string `json:"genesis"`
53+
Genesis string `json:"genesis"`
54+
NetworkID *uint32 `json:"networkID"`
5455
// May have length 0
5556
// (i.e. network may have no nodes on creation.)
5657
NodeConfigs []node.Config `json:"nodeConfigs"`

rpcpb/rpc.pb.go

+674-663
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpcpb/rpc.proto

+2
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ message StartRequest {
350350
// If specified, will create a file "subnetid.json" under subnets config dir with
351351
// the contents provided here.
352352
map<string, string> subnet_configs = 13;
353+
354+
optional uint32 network_id = 14;
353355
}
354356

355357
message RPCVersionRequest {}

server/network.go

+6
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ type localNetworkOptions struct {
109109
reassignPortsIfUsed bool
110110

111111
dynamicPorts bool
112+
113+
networkID *uint32
112114
}
113115

114116
func newLocalNetwork(opts localNetworkOptions) (*localNetwork, error) {
@@ -161,6 +163,10 @@ func (lc *localNetwork) createConfig() error {
161163
cfg.Flags[config.PluginDirKey] = lc.pluginDir
162164
}
163165

166+
if lc.options.networkID != nil {
167+
cfg.NetworkID = lc.options.networkID
168+
}
169+
164170
for i := range cfg.NodeConfigs {
165171
// NOTE: Naming convention for node names is currently `node` + number, i.e. `node1,node2,node3,...node101`
166172
nodeName := fmt.Sprintf("node%d", i+1)

server/server.go

+1
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ func (s *server) Start(_ context.Context, req *rpcpb.StartRequest) (*rpcpb.Start
328328
}
329329

330330
s.network, err = newLocalNetwork(localNetworkOptions{
331+
networkID: req.NetworkId,
331332
execPath: execPath,
332333
rootDataDir: rootDataDir,
333334
numNodes: numNodes,

utils/utils.go

+14
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ func NetworkIDFromGenesis(genesis []byte) (uint32, error) {
4747
return uint32(networkID), nil
4848
}
4949

50+
func SetGenesisNetworkID(genesis []byte, networkID uint32) ([]byte, error) {
51+
genesisMap := map[string]interface{}{}
52+
if err := json.Unmarshal(genesis, &genesisMap); err != nil {
53+
return nil, fmt.Errorf("couldn't unmarshal genesis: %w", err)
54+
}
55+
genesisMap[genesisNetworkIDKey] = networkID
56+
var err error
57+
genesis, err = json.Marshal(genesisMap)
58+
if err != nil {
59+
return nil, fmt.Errorf("couldn't marshal genesis: %w", err)
60+
}
61+
return genesis, nil
62+
}
63+
5064
var (
5165
ErrEmptyExecPath = errors.New("avalanche exec is not defined")
5266
ErrNotExists = errors.New("avalanche exec not exists")

0 commit comments

Comments
 (0)