Skip to content

Commit 2a42e97

Browse files
committed
add network id option to Start()
1 parent fca522a commit 2a42e97

File tree

9 files changed

+703
-701
lines changed

9 files changed

+703
-701
lines changed

client/client.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ type Op struct {
455455
subnetConfigs map[string]string
456456
reassignPortsIfUsed bool
457457
dynamicPorts bool
458-
networkID *uint32
458+
networkID uint32
459459
}
460460

461461
type OpOption func(*Op)
@@ -474,7 +474,7 @@ func WithGlobalNodeConfig(nodeConfig string) OpOption {
474474

475475
func WithNetworkID(networkId uint32) OpOption {
476476
return func(op *Op) {
477-
op.networkID = &networkId
477+
op.networkID = networkId
478478
}
479479
}
480480

local/network.go

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

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

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)
447+
// Set network ID
448+
ln.networkID = constants.DefaultNetworkID
449+
if networkConfig.NetworkID != 0 {
450+
if networkConfig.NetworkID < 11 {
451+
return fmt.Errorf("network IDs < 11 are reserved ones")
459452
}
453+
ln.networkID = networkConfig.NetworkID
454+
}
455+
genesis, err := utils.SetGenesisNetworkID(ln.genesis, ln.networkID)
456+
if err != nil {
457+
return fmt.Errorf("couldn't set network ID to genesis: %w", err)
460458
}
459+
ln.genesis = genesis
461460

462461
// save node defaults
463462
ln.flags = networkConfig.Flags

network/config.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ 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"`
54-
NetworkID *uint32 `json:"networkID"`
53+
Genesis string `json:"genesis"`
54+
// If 0, will use default network ID
55+
NetworkID uint32 `json:"networkID"`
5556
// May have length 0
5657
// (i.e. network may have no nodes on creation.)
5758
NodeConfigs []node.Config `json:"nodeConfigs"`

rpcpb/rpc.pb.go

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

rpcpb/rpc.proto

+2-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,8 @@ message StartRequest {
351351
// the contents provided here.
352352
map<string, string> subnet_configs = 13;
353353

354-
optional uint32 network_id = 14;
354+
// Network id to assign to the network, instead of default one
355+
uint32 network_id = 14;
355356
}
356357

357358
message RPCVersionRequest {}

rpcpb/rpc_grpc.pb.go

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

server/network.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ type localNetworkOptions struct {
110110

111111
dynamicPorts bool
112112

113-
networkID *uint32
113+
networkID uint32
114114
}
115115

116116
func newLocalNetwork(opts localNetworkOptions) (*localNetwork, error) {
@@ -163,9 +163,7 @@ func (lc *localNetwork) createConfig() error {
163163
cfg.Flags[config.PluginDirKey] = lc.pluginDir
164164
}
165165

166-
if lc.options.networkID != nil {
167-
cfg.NetworkID = lc.options.networkID
168-
}
166+
cfg.NetworkID = lc.options.networkID
169167

170168
for i := range cfg.NodeConfigs {
171169
// NOTE: Naming convention for node names is currently `node` + number, i.e. `node1,node2,node3,...node101`

utils/constants/constants.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ const (
1111
RootDirPrefix = "network-runner-root-data"
1212
DefaultExecPathEnvVar = "AVALANCHEGO_EXEC_PATH"
1313
DefaultPluginDirEnvVar = "AVALANCHEGO_PLUGIN_PATH"
14+
LocalGenesisFile = "genesis.json"
15+
DefaultNetworkID = uint32(1337)
1416
)
1517

1618
var (
17-
LocalConfigDir = filepath.Join("local", "default")
18-
LocalGenesisFile = "genesis.json"
19+
LocalConfigDir = filepath.Join("local", "default")
1920
)

utils/utils.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ const (
2020
dirTimestampFormat = "20060102_150405"
2121
)
2222

23+
var (
24+
ErrEmptyExecPath = errors.New("avalanche exec is not defined")
25+
ErrNotExists = errors.New("avalanche exec not exists")
26+
ErrNotExistsPlugin = errors.New("plugin exec not exists")
27+
)
28+
2329
func ToNodeID(stakingKey, stakingCert []byte) (ids.NodeID, error) {
2430
tlsCert, err := staking.LoadTLSCertFromBytes(stakingKey, stakingCert)
2531
if err != nil {
@@ -61,12 +67,6 @@ func SetGenesisNetworkID(genesis []byte, networkID uint32) ([]byte, error) {
6167
return genesis, nil
6268
}
6369

64-
var (
65-
ErrEmptyExecPath = errors.New("avalanche exec is not defined")
66-
ErrNotExists = errors.New("avalanche exec not exists")
67-
ErrNotExistsPlugin = errors.New("plugin exec not exists")
68-
)
69-
7070
func CheckExecPath(exec string) error {
7171
if exec == "" {
7272
return ErrEmptyExecPath

0 commit comments

Comments
 (0)