Skip to content

Commit dae0f00

Browse files
authored
[tmpnet] Ensure all node runtime methods accept a context (#3894)
1 parent bf50ecf commit dae0f00

File tree

7 files changed

+38
-31
lines changed

7 files changed

+38
-31
lines changed

tests/fixture/e2e/env.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func NewTestEnvironment(tc tests.TestContext, flagVars *FlagVars, desiredNetwork
127127

128128
if len(networkDir) > 0 {
129129
var err error
130-
network, err = tmpnet.ReadNetwork(tc.Log(), networkDir)
130+
network, err = tmpnet.ReadNetwork(tc.DefaultContext(), tc.Log(), networkDir)
131131
require.NoError(err)
132132
tc.Log().Info("loaded a network",
133133
zap.String("networkDir", networkDir),
@@ -223,7 +223,7 @@ func (te *TestEnvironment) GetRandomNodeURI() tmpnet.NodeURI {
223223
// Retrieve the network to target for testing.
224224
func (te *TestEnvironment) GetNetwork() *tmpnet.Network {
225225
tc := te.testContext
226-
network, err := tmpnet.ReadNetwork(tc.Log(), te.NetworkDir)
226+
network, err := tmpnet.ReadNetwork(tc.DefaultContext(), tc.Log(), te.NetworkDir)
227227
require.NoError(tc, err)
228228
return network
229229
}
@@ -238,7 +238,7 @@ func (te *TestEnvironment) StartPrivateNetwork(network *tmpnet.Network) {
238238
tc := te.testContext
239239
require := require.New(tc)
240240
// Use the same configuration as the shared network
241-
sharedNetwork, err := tmpnet.ReadNetwork(tc.Log(), te.NetworkDir)
241+
sharedNetwork, err := tmpnet.ReadNetwork(tc.DefaultContext(), tc.Log(), te.NetworkDir)
242242
require.NoError(err)
243243
network.DefaultRuntimeConfig = sharedNetwork.DefaultRuntimeConfig
244244

tests/fixture/tmpnet/network.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func BootstrapNewNetwork(
175175

176176
// Stops the nodes of the network configured in the provided directory.
177177
func StopNetwork(ctx context.Context, log logging.Logger, dir string) error {
178-
network, err := ReadNetwork(log, dir)
178+
network, err := ReadNetwork(ctx, log, dir)
179179
if err != nil {
180180
return err
181181
}
@@ -184,15 +184,15 @@ func StopNetwork(ctx context.Context, log logging.Logger, dir string) error {
184184

185185
// Restarts the nodes of the network configured in the provided directory.
186186
func RestartNetwork(ctx context.Context, log logging.Logger, dir string) error {
187-
network, err := ReadNetwork(log, dir)
187+
network, err := ReadNetwork(ctx, log, dir)
188188
if err != nil {
189189
return err
190190
}
191191
return network.Restart(ctx)
192192
}
193193

194194
// Reads a network from the provided directory.
195-
func ReadNetwork(log logging.Logger, dir string) (*Network, error) {
195+
func ReadNetwork(ctx context.Context, log logging.Logger, dir string) (*Network, error) {
196196
canonicalDir, err := toCanonicalDir(dir)
197197
if err != nil {
198198
return nil, err
@@ -201,7 +201,7 @@ func ReadNetwork(log logging.Logger, dir string) (*Network, error) {
201201
Dir: canonicalDir,
202202
log: log,
203203
}
204-
if err := network.Read(); err != nil {
204+
if err := network.Read(ctx); err != nil {
205205
return nil, fmt.Errorf("failed to read network: %w", err)
206206
}
207207
if network.DefaultFlags == nil {
@@ -475,7 +475,7 @@ func (n *Network) StartNode(ctx context.Context, node *Node) error {
475475
return fmt.Errorf("writing node flags: %w", err)
476476
}
477477

478-
if err := node.Start(); err != nil {
478+
if err := node.Start(ctx); err != nil {
479479
// Attempt to stop an unhealthy node to provide some assurance to the caller
480480
// that an error condition will not result in a lingering process.
481481
err = errors.Join(err, node.Stop(ctx))
@@ -513,7 +513,7 @@ func (n *Network) RestartNode(ctx context.Context, node *Node) error {
513513
// Stops all nodes in the network.
514514
func (n *Network) Stop(ctx context.Context) error {
515515
// Ensure the node state is up-to-date
516-
if err := n.readNodes(); err != nil {
516+
if err := n.readNodes(ctx); err != nil {
517517
return err
518518
}
519519

tests/fixture/tmpnet/network_config.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package tmpnet
55

66
import (
7+
"context"
78
"encoding/json"
89
"errors"
910
"fmt"
@@ -21,11 +22,11 @@ import (
2122
var errMissingNetworkDir = errors.New("failed to write network: missing network directory")
2223

2324
// Read network and node configuration from disk.
24-
func (n *Network) Read() error {
25+
func (n *Network) Read(ctx context.Context) error {
2526
if err := n.readNetwork(); err != nil {
2627
return err
2728
}
28-
if err := n.readNodes(); err != nil {
29+
if err := n.readNodes(ctx); err != nil {
2930
return err
3031
}
3132
return n.readSubnets()
@@ -57,7 +58,7 @@ func (n *Network) readNetwork() error {
5758
}
5859

5960
// Read the nodes associated with the network from disk.
60-
func (n *Network) readNodes() error {
61+
func (n *Network) readNodes(ctx context.Context) error {
6162
nodes := []*Node{}
6263

6364
// Node configuration is stored in child directories
@@ -72,7 +73,7 @@ func (n *Network) readNodes() error {
7273

7374
node := NewNode()
7475
dataDir := filepath.Join(n.Dir, entry.Name())
75-
err := node.Read(n, dataDir)
76+
err := node.Read(ctx, n, dataDir)
7677
if errors.Is(err, os.ErrNotExist) {
7778
// If no config file exists, assume this is not the path of a node
7879
continue

tests/fixture/tmpnet/network_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package tmpnet
55

66
import (
7+
"context"
78
"testing"
89

910
"github.com/stretchr/testify/require"
@@ -16,6 +17,8 @@ func TestNetworkSerialization(t *testing.T) {
1617

1718
tmpDir := t.TempDir()
1819

20+
ctx := context.Background()
21+
1922
network := NewDefaultNetwork("testnet")
2023
// Validate round-tripping of primary subnet configuration
2124
network.PrimarySubnetConfig = ConfigMap{
@@ -24,9 +27,9 @@ func TestNetworkSerialization(t *testing.T) {
2427
require.NoError(network.EnsureDefaultConfig(logging.NoLog{}))
2528
require.NoError(network.Create(tmpDir))
2629
// Ensure node runtime is initialized
27-
require.NoError(network.readNodes())
30+
require.NoError(network.readNodes(ctx))
2831

29-
loadedNetwork, err := ReadNetwork(logging.NoLog{}, network.Dir)
32+
loadedNetwork, err := ReadNetwork(ctx, logging.NoLog{}, network.Dir)
3033
require.NoError(err)
3134
for _, key := range loadedNetwork.PreFundedKeys {
3235
// Address() enables comparison with the original network by

tests/fixture/tmpnet/node.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ var (
3636

3737
// NodeRuntime defines the methods required to support running a node.
3838
type NodeRuntime interface {
39-
readState() error
39+
readState(ctx context.Context) error
4040
GetLocalURI(ctx context.Context) (string, func(), error)
4141
GetLocalStakingAddress(ctx context.Context) (netip.AddrPort, func(), error)
42-
InitiateStop() error
43-
Start() error
42+
Start(ctx context.Context) error
43+
InitiateStop(ctx context.Context) error
4444
WaitForStopped(ctx context.Context) error
4545
IsHealthy(ctx context.Context) (bool, error)
4646
}
@@ -140,23 +140,23 @@ func (n *Node) IsHealthy(ctx context.Context) (bool, error) {
140140
return n.getRuntime().IsHealthy(ctx)
141141
}
142142

143-
func (n *Node) Start() error {
144-
return n.getRuntime().Start()
143+
func (n *Node) Start(ctx context.Context) error {
144+
return n.getRuntime().Start(ctx)
145145
}
146146

147147
func (n *Node) InitiateStop(ctx context.Context) error {
148148
if err := n.SaveMetricsSnapshot(ctx); err != nil {
149149
return err
150150
}
151-
return n.getRuntime().InitiateStop()
151+
return n.getRuntime().InitiateStop(ctx)
152152
}
153153

154154
func (n *Node) WaitForStopped(ctx context.Context) error {
155155
return n.getRuntime().WaitForStopped(ctx)
156156
}
157157

158-
func (n *Node) readState() error {
159-
return n.getRuntime().readState()
158+
func (n *Node) readState(ctx context.Context) error {
159+
return n.getRuntime().readState(ctx)
160160
}
161161

162162
func (n *Node) GetLocalURI(ctx context.Context) (string, func(), error) {

tests/fixture/tmpnet/node_config.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package tmpnet
55

66
import (
7+
"context"
78
"encoding/json"
89
"fmt"
910
"os"
@@ -70,7 +71,7 @@ func (n *Node) writeConfig() error {
7071
return nil
7172
}
7273

73-
func (n *Node) Read(network *Network, dataDir string) error {
74+
func (n *Node) Read(ctx context.Context, network *Network, dataDir string) error {
7475
n.network = network
7576
n.DataDir = dataDir
7677

@@ -80,7 +81,7 @@ func (n *Node) Read(network *Network, dataDir string) error {
8081
if err := n.EnsureNodeID(); err != nil {
8182
return err
8283
}
83-
return n.readState()
84+
return n.readState(ctx)
8485
}
8586

8687
func (n *Node) Write() error {

tests/fixture/tmpnet/process_runtime.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (p *ProcessRuntime) setProcessContext(processContext node.ProcessContext) {
5858
p.node.StakingAddress = processContext.StakingAddress
5959
}
6060

61-
func (p *ProcessRuntime) readState() error {
61+
func (p *ProcessRuntime) readState(_ context.Context) error {
6262
path := p.getProcessContextPath()
6363
bytes, err := os.ReadFile(path)
6464
if errors.Is(err, fs.ErrNotExist) {
@@ -81,7 +81,7 @@ func (p *ProcessRuntime) readState() error {
8181
// its staking port. The network will start faster with this
8282
// synchronization due to the avoidance of exponential backoff
8383
// if a node tries to connect to a beacon that is not ready.
84-
func (p *ProcessRuntime) Start() error {
84+
func (p *ProcessRuntime) Start(ctx context.Context) error {
8585
log := p.node.network.log
8686

8787
// Avoid attempting to start an already running node.
@@ -122,7 +122,7 @@ func (p *ProcessRuntime) Start() error {
122122
// a configuration error preventing startup. Such a log entry will be provided to the
123123
// cancelWithCause function so that waitForProcessContext can exit early with an error
124124
// that includes the log entry.
125-
ctx, cancelWithCause := context.WithCancelCause(context.Background())
125+
ctx, cancelWithCause := context.WithCancelCause(ctx)
126126
defer cancelWithCause(nil)
127127
logPath := p.node.DataDir + "/logs/main.log"
128128
go watchLogFileForFatal(ctx, cancelWithCause, log, logPath)
@@ -145,7 +145,7 @@ func (p *ProcessRuntime) Start() error {
145145
}
146146

147147
// Signals the node process to stop.
148-
func (p *ProcessRuntime) InitiateStop() error {
148+
func (p *ProcessRuntime) InitiateStop(_ context.Context) error {
149149
proc, err := p.getProcess()
150150
if err != nil {
151151
return fmt.Errorf("failed to retrieve process to stop: %w", err)
@@ -211,7 +211,7 @@ func (p *ProcessRuntime) waitForProcessContext(ctx context.Context) error {
211211
ctx, cancel := context.WithTimeout(ctx, defaultNodeInitTimeout)
212212
defer cancel()
213213
for len(p.node.URI) == 0 {
214-
err := p.readState()
214+
err := p.readState(ctx)
215215
if err != nil {
216216
return fmt.Errorf("failed to read process context for node %q: %w", p.node.NodeID, err)
217217
}
@@ -229,9 +229,11 @@ func (p *ProcessRuntime) waitForProcessContext(ctx context.Context) error {
229229
// process liveness, the node's process context will be refreshed if
230230
// live or cleared if not running.
231231
func (p *ProcessRuntime) getProcess() (*os.Process, error) {
232+
// This context is not used but a non-nil value must be supplied to satisfy the linter
233+
ctx := context.TODO()
232234
// Read the process context to ensure freshness. The node may have
233235
// stopped or been restarted since last read.
234-
if err := p.readState(); err != nil {
236+
if err := p.readState(ctx); err != nil {
235237
return nil, fmt.Errorf("failed to read process context: %w", err)
236238
}
237239

0 commit comments

Comments
 (0)