From 5cea7a6230a6f070dd484aa6d883605f148445a4 Mon Sep 17 00:00:00 2001 From: Roberto Bayardo Date: Mon, 25 Mar 2024 10:03:44 -0700 Subject: [PATCH] ethclient/simulated: clean up Node resources when simulated backend is closed (#29316) --- eth/backend.go | 4 ++-- ethclient/simulated/backend.go | 15 ++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/eth/backend.go b/eth/backend.go index e6f9c05950..db3209aee2 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -101,8 +101,8 @@ type Ethereum struct { shutdownTracker *shutdowncheck.ShutdownTracker // Tracks if and when the node has shutdown ungracefully } -// New creates a new Ethereum object (including the -// initialisation of the common Ethereum object) +// New creates a new Ethereum object (including the initialisation of the common Ethereum object), +// whose lifecycle will be managed by the provided node. func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { // Ensure configuration values are compatible and sane if config.SyncMode == downloader.LightSync { diff --git a/ethclient/simulated/backend.go b/ethclient/simulated/backend.go index 0c2a0b453c..d6fb886ad8 100644 --- a/ethclient/simulated/backend.go +++ b/ethclient/simulated/backend.go @@ -17,6 +17,7 @@ package simulated import ( + "errors" "time" "github.com/ethereum/go-ethereum" @@ -62,7 +63,7 @@ type simClient struct { // Backend is a simulated blockchain. You can use it to test your contracts or // other code that interacts with the Ethereum chain. type Backend struct { - eth *eth.Ethereum + node *node.Node beacon *catalyst.SimulatedBeacon client simClient } @@ -129,7 +130,7 @@ func newWithNode(stack *node.Node, conf *eth.Config, blockPeriod uint64) (*Backe return nil, err } return &Backend{ - eth: backend, + node: stack, beacon: beacon, client: simClient{ethclient.NewClient(stack.Attach())}, }, nil @@ -142,12 +143,16 @@ func (n *Backend) Close() error { n.client.Close() n.client = simClient{} } + var err error if n.beacon != nil { - err := n.beacon.Stop() + err = n.beacon.Stop() n.beacon = nil - return err } - return nil + if n.node != nil { + err = errors.Join(err, n.node.Close()) + n.node = nil + } + return err } // Commit seals a block and moves the chain forward to a new empty block.