From 627e9c70b0cf2abede6be0a5e8b02e7efe2fcf53 Mon Sep 17 00:00:00 2001 From: Maru Newby Date: Mon, 5 Aug 2024 11:54:17 -0700 Subject: [PATCH] [tmpnet] Add Network.GetNetworkID() to get ID of a running network As per @aaronbuchwald, having Network.NetworkID return zero for a running network is unexpected behavior. --- tests/fixture/tmpnet/network.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/tests/fixture/tmpnet/network.go b/tests/fixture/tmpnet/network.go index 332da0a6214..afa59e653c1 100644 --- a/tests/fixture/tmpnet/network.go +++ b/tests/fixture/tmpnet/network.go @@ -84,7 +84,9 @@ type Network struct { // Path where network configuration and data is stored Dir string - // Id of the network. If zero, must be set in Genesis. + // Id of the network. If zero, must be set in Genesis. Consider + // using the GetNetworkID method if needing to retrieve the ID of + // a running network. NetworkID uint32 // Configuration common across nodes @@ -562,10 +564,7 @@ func (n *Network) EnsureNodeConfig(node *Node) error { node.NetworkOwner = n.Owner // Set the network name if available - networkID := n.NetworkID - if networkID == 0 && n.Genesis != nil && n.Genesis.NetworkID > 0 { - networkID = n.Genesis.NetworkID - } + networkID := n.GetNetworkID() if networkID > 0 { // Convert the network id to a string to ensure consistency in JSON round-tripping. flags[config.NetworkNameKey] = strconv.FormatUint(uint64(networkID), 10) @@ -845,6 +844,18 @@ func (n *Network) getBootstrapIPsAndIDs(skippedNode *Node) ([]string, []string, return bootstrapIPs, bootstrapIDs, nil } +// GetNetworkID returns the effective ID of the network. If the network +// defines a genesis, the network ID in the genesis will be returned. If a +// genesis is not present (i.e. a network with a genesis included in the +// avalanchego binary - mainnet, testnet and local), the value of the +// NetworkID field will be returned +func (n *Network) GetNetworkID() uint32 { + if n.Genesis != nil && n.Genesis.NetworkID > 0 { + return n.Genesis.NetworkID + } + return n.NetworkID +} + // Waits until the provided nodes are healthy. func waitForHealthy(ctx context.Context, w io.Writer, nodes []*Node) error { ticker := time.NewTicker(networkHealthCheckInterval)