Skip to content

[tmpnet] Ensure Node has a reference to Network #3870

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions tests/fixture/tmpnet/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ func (n *Network) RestartNode(ctx context.Context, log logging.Logger, node *Nod
// Stops all nodes in the network.
func (n *Network) Stop(ctx context.Context) error {
// Target all nodes, including the ephemeral ones
nodes, err := ReadNodes(n.Dir, true /* includeEphemeral */)
nodes, err := ReadNodes(n, true /* includeEphemeral */)
if err != nil {
return err
}
Expand Down Expand Up @@ -585,11 +585,8 @@ func (n *Network) Restart(ctx context.Context, log logging.Logger) error {
// no action will be taken.
// TODO(marun) Reword or refactor to account for the differing behavior pre- vs post-start
func (n *Network) EnsureNodeConfig(node *Node) error {
// Ensure nodes can label their metrics with the network uuid
node.NetworkUUID = n.UUID

// Ensure nodes can label metrics with an indication of the shared/private nature of the network
node.NetworkOwner = n.Owner
// Ensure the node has access to network configuration
node.network = n

if err := node.EnsureKeys(); err != nil {
return err
Expand Down Expand Up @@ -818,7 +815,7 @@ func (n *Network) GetNodeURIs() []NodeURI {
// For consumption outside of avalanchego. Needs to be kept exported.
func (n *Network) GetBootstrapIPsAndIDs(skippedNode *Node) ([]string, []string, error) {
// Collect staking addresses of non-ephemeral nodes for use in bootstrapping a node
nodes, err := ReadNodes(n.Dir, false /* includeEphemeral */)
nodes, err := ReadNodes(n, false /* includeEphemeral */)
if err != nil {
return nil, nil, fmt.Errorf("failed to read network's nodes: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/fixture/tmpnet/network_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (n *Network) readNetwork() error {

// Read the non-ephemeral nodes associated with the network from disk.
func (n *Network) readNodes() error {
nodes, err := ReadNodes(n.Dir, false /* includeEphemeral */)
nodes, err := ReadNodes(n, false /* includeEphemeral */)
if err != nil {
return err
}
Expand Down
23 changes: 9 additions & 14 deletions tests/fixture/tmpnet/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,6 @@ type NodeRuntimeConfig struct {

// Node supports configuring and running a node participating in a temporary network.
type Node struct {
// Uniquely identifies the network the node is part of to enable monitoring.
NetworkUUID string

// Identify the entity associated with this network. This is
// intended to be used to label metrics to enable filtering
// results for a test run between the primary/shared network used
// by the majority of tests and private networks used by
// individual tests.
NetworkOwner string

// Set by EnsureNodeID which is also called when the node is read.
NodeID ids.NodeID

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

// Initialized on demand
runtime NodeRuntime

// Intended to be set by the network
network *Network
}

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

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

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

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

node.network = network

nodes = append(nodes, node)
}

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

// Saves the currently allocated API port to the node's configuration
Expand Down
4 changes: 0 additions & 4 deletions tests/fixture/tmpnet/node_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,13 @@ func (n *Node) readConfig() error {
}

type serializedNodeConfig struct {
NetworkUUID string
NetworkOwner string
IsEphemeral bool
Flags FlagsMap
RuntimeConfig *NodeRuntimeConfig
}

func (n *Node) writeConfig() error {
config := serializedNodeConfig{
NetworkUUID: n.NetworkUUID,
NetworkOwner: n.NetworkOwner,
IsEphemeral: n.IsEphemeral,
Flags: n.Flags,
RuntimeConfig: n.RuntimeConfig,
Expand Down
6 changes: 3 additions & 3 deletions tests/fixture/tmpnet/node_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,10 @@ func (p *NodeProcess) writeMonitoringConfig() error {
// behavior of using the node's URI since the URI isn't
// guaranteed stable (e.g. port may change after restart).
"instance": p.node.GetUniqueID(),
"network_uuid": p.node.NetworkUUID,
"network_uuid": p.node.network.UUID,
"node_id": p.node.NodeID,
"is_ephemeral_node": strconv.FormatBool(p.node.IsEphemeral),
"network_owner": p.node.NetworkOwner,
"network_owner": p.node.network.Owner,
}
commonLabels.SetDefaults(githubLabelsFromEnv())

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

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