Skip to content

Commit 38e86cc

Browse files
authored
[tmpnet] Ensure Node has a reference to Network (#3870)
1 parent 0160c98 commit 38e86cc

File tree

5 files changed

+17
-29
lines changed

5 files changed

+17
-29
lines changed

tests/fixture/tmpnet/network.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ func (n *Network) RestartNode(ctx context.Context, log logging.Logger, node *Nod
543543
// Stops all nodes in the network.
544544
func (n *Network) Stop(ctx context.Context) error {
545545
// Target all nodes, including the ephemeral ones
546-
nodes, err := ReadNodes(n.Dir, true /* includeEphemeral */)
546+
nodes, err := ReadNodes(n, true /* includeEphemeral */)
547547
if err != nil {
548548
return err
549549
}
@@ -586,11 +586,8 @@ func (n *Network) Restart(ctx context.Context, log logging.Logger) error {
586586
// no action will be taken.
587587
// TODO(marun) Reword or refactor to account for the differing behavior pre- vs post-start
588588
func (n *Network) EnsureNodeConfig(node *Node) error {
589-
// Ensure nodes can label their metrics with the network uuid
590-
node.NetworkUUID = n.UUID
591-
592-
// Ensure nodes can label metrics with an indication of the shared/private nature of the network
593-
node.NetworkOwner = n.Owner
589+
// Ensure the node has access to network configuration
590+
node.network = n
594591

595592
if err := node.EnsureKeys(); err != nil {
596593
return err
@@ -819,7 +816,7 @@ func (n *Network) GetNodeURIs() []NodeURI {
819816
// For consumption outside of avalanchego. Needs to be kept exported.
820817
func (n *Network) GetBootstrapIPsAndIDs(skippedNode *Node) ([]string, []string, error) {
821818
// Collect staking addresses of non-ephemeral nodes for use in bootstrapping a node
822-
nodes, err := ReadNodes(n.Dir, false /* includeEphemeral */)
819+
nodes, err := ReadNodes(n, false /* includeEphemeral */)
823820
if err != nil {
824821
return nil, nil, fmt.Errorf("failed to read network's nodes: %w", err)
825822
}

tests/fixture/tmpnet/network_config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (n *Network) readNetwork() error {
5959

6060
// Read the non-ephemeral nodes associated with the network from disk.
6161
func (n *Network) readNodes() error {
62-
nodes, err := ReadNodes(n.Dir, false /* includeEphemeral */)
62+
nodes, err := ReadNodes(n, false /* includeEphemeral */)
6363
if err != nil {
6464
return err
6565
}

tests/fixture/tmpnet/node.go

+9-14
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,6 @@ type NodeRuntimeConfig struct {
5959

6060
// Node supports configuring and running a node participating in a temporary network.
6161
type Node struct {
62-
// Uniquely identifies the network the node is part of to enable monitoring.
63-
NetworkUUID string
64-
65-
// Identify the entity associated with this network. This is
66-
// intended to be used to label metrics to enable filtering
67-
// results for a test run between the primary/shared network used
68-
// by the majority of tests and private networks used by
69-
// individual tests.
70-
NetworkOwner string
71-
7262
// Set by EnsureNodeID which is also called when the node is read.
7363
NodeID ids.NodeID
7464

@@ -89,6 +79,9 @@ type Node struct {
8979

9080
// Initialized on demand
9181
runtime NodeRuntime
82+
83+
// Intended to be set by the network
84+
network *Network
9285
}
9386

9487
// Initializes a new node with only the data dir set
@@ -129,11 +122,11 @@ func ReadNode(dataDir string) (*Node, error) {
129122
}
130123

131124
// Reads nodes from the specified network directory.
132-
func ReadNodes(networkDir string, includeEphemeral bool) ([]*Node, error) {
125+
func ReadNodes(network *Network, includeEphemeral bool) ([]*Node, error) {
133126
nodes := []*Node{}
134127

135128
// Node configuration is stored in child directories
136-
entries, err := os.ReadDir(networkDir)
129+
entries, err := os.ReadDir(network.Dir)
137130
if err != nil {
138131
return nil, fmt.Errorf("failed to read dir: %w", err)
139132
}
@@ -142,7 +135,7 @@ func ReadNodes(networkDir string, includeEphemeral bool) ([]*Node, error) {
142135
continue
143136
}
144137

145-
nodeDir := filepath.Join(networkDir, entry.Name())
138+
nodeDir := filepath.Join(network.Dir, entry.Name())
146139
node, err := ReadNode(nodeDir)
147140
if errors.Is(err, os.ErrNotExist) {
148141
// If no config file exists, assume this is not the path of a node
@@ -159,6 +152,8 @@ func ReadNodes(networkDir string, includeEphemeral bool) ([]*Node, error) {
159152
return nil, fmt.Errorf("failed to ensure NodeID: %w", err)
160153
}
161154

155+
node.network = network
156+
162157
nodes = append(nodes, node)
163158
}
164159

@@ -382,7 +377,7 @@ func (n *Node) GetUniqueID() string {
382377
nodeIDString := n.NodeID.String()
383378
startIndex := len(ids.NodeIDPrefix)
384379
endIndex := startIndex + 8 // 8 characters should be enough to identify a node in the context of its network
385-
return n.NetworkUUID + "-" + strings.ToLower(nodeIDString[startIndex:endIndex])
380+
return n.network.UUID + "-" + strings.ToLower(nodeIDString[startIndex:endIndex])
386381
}
387382

388383
// Saves the currently allocated API port to the node's configuration

tests/fixture/tmpnet/node_config.go

-4
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,13 @@ func (n *Node) readConfig() error {
4949
}
5050

5151
type serializedNodeConfig struct {
52-
NetworkUUID string
53-
NetworkOwner string
5452
IsEphemeral bool
5553
Flags FlagsMap
5654
RuntimeConfig *NodeRuntimeConfig
5755
}
5856

5957
func (n *Node) writeConfig() error {
6058
config := serializedNodeConfig{
61-
NetworkUUID: n.NetworkUUID,
62-
NetworkOwner: n.NetworkOwner,
6359
IsEphemeral: n.IsEphemeral,
6460
Flags: n.Flags,
6561
RuntimeConfig: n.RuntimeConfig,

tests/fixture/tmpnet/node_process.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,10 @@ func (p *NodeProcess) writeMonitoringConfig() error {
258258
// behavior of using the node's URI since the URI isn't
259259
// guaranteed stable (e.g. port may change after restart).
260260
"instance": p.node.GetUniqueID(),
261-
"network_uuid": p.node.NetworkUUID,
261+
"network_uuid": p.node.network.UUID,
262262
"node_id": p.node.NodeID,
263263
"is_ephemeral_node": strconv.FormatBool(p.node.IsEphemeral),
264-
"network_owner": p.node.NetworkOwner,
264+
"network_owner": p.node.network.Owner,
265265
}
266266
commonLabels.SetDefaults(githubLabelsFromEnv())
267267

@@ -296,7 +296,7 @@ func (p *NodeProcess) getMonitoringConfigPath(name string) (string, error) {
296296
if err != nil {
297297
return "", err
298298
}
299-
return filepath.Join(serviceDiscoveryDir, fmt.Sprintf("%s_%s.json", p.node.NetworkUUID, p.node.NodeID)), nil
299+
return filepath.Join(serviceDiscoveryDir, fmt.Sprintf("%s_%s.json", p.node.network.UUID, p.node.NodeID)), nil
300300
}
301301

302302
// Ensure the removal of the monitoring configuration files for this node.

0 commit comments

Comments
 (0)