From 7fab62da4d78ca9dc8e000d1c22563e489f58747 Mon Sep 17 00:00:00 2001 From: Pepper Lebeck-Jobe Date: Fri, 7 Mar 2025 13:13:48 +0100 Subject: [PATCH] Introduce fallibleCommit which can error This allows the inner loop in the case of "on-demand" commits to bail out if the txPool has been terminated before the doCommit channel is closed. This has been tested with a known-flaky test which, before this commit was spinlooping and logging unfathomable reams of warning messages. Now, when the race occurs, only a single instance of the warning is logged. --- eth/catalyst/simulated_beacon.go | 9 ++++++++- eth/catalyst/simulated_beacon_api.go | 12 ++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/eth/catalyst/simulated_beacon.go b/eth/catalyst/simulated_beacon.go index c71add93bc..286757054e 100644 --- a/eth/catalyst/simulated_beacon.go +++ b/eth/catalyst/simulated_beacon.go @@ -302,11 +302,18 @@ func (c *SimulatedBeacon) setCurrentState(headHash, finalizedHash common.Hash) { // Commit seals a block on demand. func (c *SimulatedBeacon) Commit() common.Hash { + hash, _ := c.fallibleCommit() + return hash +} + +// fallibleCommit attempts to seal a block on demand, but may return an error. +func (c *SimulatedBeacon) fallibleCommit() (common.Hash, error) { withdrawals := c.withdrawals.pop(10) if err := c.sealBlock(withdrawals, uint64(time.Now().Unix())); err != nil { log.Warn("Error performing sealing work", "err", err) + return common.Hash{}, err } - return c.eth.BlockChain().CurrentBlock().Hash() + return c.eth.BlockChain().CurrentBlock().Hash(), nil } // Rollback un-sends previously added transactions. diff --git a/eth/catalyst/simulated_beacon_api.go b/eth/catalyst/simulated_beacon_api.go index 515908f86c..5955b033bd 100644 --- a/eth/catalyst/simulated_beacon_api.go +++ b/eth/catalyst/simulated_beacon_api.go @@ -70,11 +70,15 @@ func (a *simulatedBeaconAPI) loop() { if executable, _ := a.sim.eth.TxPool().Stats(); executable == 0 { break } - a.sim.Commit() - // Avoid a race condition where tx pool is terminated during Commit. - if err := a.sim.eth.TxPool().Sync(); err != nil { - break + // Avoid a race condition where doCommit is closed while in this loop. + select { + case _, ok := <-doCommit: + if !ok { + break + } + default: } + a.sim.Commit() } } }()