Skip to content

Commit 5cea7a6

Browse files
author
Roberto Bayardo
authored
ethclient/simulated: clean up Node resources when simulated backend is closed (#29316)
1 parent 14cc967 commit 5cea7a6

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

eth/backend.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ type Ethereum struct {
101101
shutdownTracker *shutdowncheck.ShutdownTracker // Tracks if and when the node has shutdown ungracefully
102102
}
103103

104-
// New creates a new Ethereum object (including the
105-
// initialisation of the common Ethereum object)
104+
// New creates a new Ethereum object (including the initialisation of the common Ethereum object),
105+
// whose lifecycle will be managed by the provided node.
106106
func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
107107
// Ensure configuration values are compatible and sane
108108
if config.SyncMode == downloader.LightSync {

ethclient/simulated/backend.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package simulated
1818

1919
import (
20+
"errors"
2021
"time"
2122

2223
"github.com/ethereum/go-ethereum"
@@ -62,7 +63,7 @@ type simClient struct {
6263
// Backend is a simulated blockchain. You can use it to test your contracts or
6364
// other code that interacts with the Ethereum chain.
6465
type Backend struct {
65-
eth *eth.Ethereum
66+
node *node.Node
6667
beacon *catalyst.SimulatedBeacon
6768
client simClient
6869
}
@@ -129,7 +130,7 @@ func newWithNode(stack *node.Node, conf *eth.Config, blockPeriod uint64) (*Backe
129130
return nil, err
130131
}
131132
return &Backend{
132-
eth: backend,
133+
node: stack,
133134
beacon: beacon,
134135
client: simClient{ethclient.NewClient(stack.Attach())},
135136
}, nil
@@ -142,12 +143,16 @@ func (n *Backend) Close() error {
142143
n.client.Close()
143144
n.client = simClient{}
144145
}
146+
var err error
145147
if n.beacon != nil {
146-
err := n.beacon.Stop()
148+
err = n.beacon.Stop()
147149
n.beacon = nil
148-
return err
149150
}
150-
return nil
151+
if n.node != nil {
152+
err = errors.Join(err, n.node.Close())
153+
n.node = nil
154+
}
155+
return err
151156
}
152157

153158
// Commit seals a block and moves the chain forward to a new empty block.

0 commit comments

Comments
 (0)