ethclient/simulated: clean up Node resources when simulated backend is closed (#29316)

This commit is contained in:
Roberto Bayardo 2024-03-25 10:03:44 -07:00 committed by GitHub
parent 14cc967d19
commit 5cea7a6230
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 7 deletions

@ -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 {

@ -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.