From 6b299402184a6c5a65889acf8be97b64b22bf924 Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Mon, 8 Jan 2024 16:16:09 +0100 Subject: [PATCH] eth/catalyst: apply lightclients suggestions moves the loopOnDemand outside making it part of the api --- eth/catalyst/simulated_beacon.go | 40 +++++----------------------- eth/catalyst/simulated_beacon_api.go | 31 ++++++++++++++++++--- ethclient/simulated/backend.go | 3 +-- 3 files changed, 36 insertions(+), 38 deletions(-) diff --git a/eth/catalyst/simulated_beacon.go b/eth/catalyst/simulated_beacon.go index 291f91874c..3c081074cc 100644 --- a/eth/catalyst/simulated_beacon.go +++ b/eth/catalyst/simulated_beacon.go @@ -19,14 +19,12 @@ package catalyst import ( "crypto/rand" "errors" - "math" "math/big" "sync" "time" "github.com/ethereum/go-ethereum/beacon/engine" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/log" @@ -88,7 +86,6 @@ type SimulatedBeacon struct { // Period sets the period in which blocks should be produced. // // - If period is set to 0, a block is produced on every transaction. -// - If period is set to math.MaxUint64, blocks can only be produced // via Commit, Fork and AdjustTime. func NewSimulatedBeacon(period uint64, eth *eth.Ethereum) (*SimulatedBeacon, error) { block := eth.BlockChain().CurrentBlock() @@ -125,9 +122,7 @@ func (c *SimulatedBeacon) setFeeRecipient(feeRecipient common.Address) { // Start invokes the SimulatedBeacon life-cycle function in a goroutine. func (c *SimulatedBeacon) Start() error { if c.period == 0 { - go c.loopOnDemand() - } else if c.period == math.MaxUint64 { - // if period is set to MaxUint, do not mine at all + // if period is set to 0, do not mine at all // this is used in the simulated backend where blocks // are explicitly mined via Commit, AdjustTime and Fork } else { @@ -204,32 +199,6 @@ func (c *SimulatedBeacon) sealBlock(withdrawals []*types.Withdrawal, timestamp u return nil } -// loopOnDemand runs the block production loop for "on-demand" configuration (period = 0) -func (c *SimulatedBeacon) loopOnDemand() { - var ( - newTxs = make(chan core.NewTxsEvent) - sub = c.eth.TxPool().SubscribeTransactions(newTxs, true) - ) - defer sub.Unsubscribe() - - for { - select { - case <-c.shutdownCh: - return - case w := <-c.withdrawals.pending: - withdrawals := append(c.withdrawals.gatherPending(9), w) - if err := c.sealBlock(withdrawals, uint64(time.Now().Unix())); err != nil { - log.Warn("Error performing sealing work", "err", err) - } - case <-newTxs: - withdrawals := c.withdrawals.gatherPending(10) - if err := c.sealBlock(withdrawals, uint64(time.Now().Unix())); err != nil { - log.Warn("Error performing sealing work", "err", err) - } - } - } -} - // loop runs the block production loop for non-zero period configuration func (c *SimulatedBeacon) loop() { timer := time.NewTimer(0) @@ -318,10 +287,15 @@ func (c *SimulatedBeacon) AdjustTime(adjustment time.Duration) error { } func RegisterSimulatedBeaconAPIs(stack *node.Node, sim *SimulatedBeacon) { + api := &api{sim} + if sim.period == 0 { + // mine on demand if period is set to 0 + go api.loop() + } stack.RegisterAPIs([]rpc.API{ { Namespace: "dev", - Service: &api{sim}, + Service: api, Version: "1.0", }, }) diff --git a/eth/catalyst/simulated_beacon_api.go b/eth/catalyst/simulated_beacon_api.go index 93670257f6..73d0a5921d 100644 --- a/eth/catalyst/simulated_beacon_api.go +++ b/eth/catalyst/simulated_beacon_api.go @@ -18,19 +18,44 @@ package catalyst import ( "context" + "time" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/log" ) type api struct { - simBeacon *SimulatedBeacon + sim *SimulatedBeacon +} + +func (a *api) loop() { + var ( + newTxs = make(chan core.NewTxsEvent) + sub = a.sim.eth.TxPool().SubscribeTransactions(newTxs, true) + ) + defer sub.Unsubscribe() + + for { + select { + case <-a.sim.shutdownCh: + return + case w := <-a.sim.withdrawals.pending: + withdrawals := append(a.sim.withdrawals.gatherPending(9), w) + if err := a.sim.sealBlock(withdrawals, uint64(time.Now().Unix())); err != nil { + log.Warn("Error performing sealing work", "err", err) + } + case <-newTxs: + a.sim.Commit() + } + } } func (a *api) AddWithdrawal(ctx context.Context, withdrawal *types.Withdrawal) error { - return a.simBeacon.withdrawals.add(withdrawal) + return a.sim.withdrawals.add(withdrawal) } func (a *api) SetFeeRecipient(ctx context.Context, feeRecipient common.Address) { - a.simBeacon.setFeeRecipient(feeRecipient) + a.sim.setFeeRecipient(feeRecipient) } diff --git a/ethclient/simulated/backend.go b/ethclient/simulated/backend.go index 68b69c0aad..aeec973647 100644 --- a/ethclient/simulated/backend.go +++ b/ethclient/simulated/backend.go @@ -17,7 +17,6 @@ package simulated import ( - "math" "time" "github.com/ethereum/go-ethereum" @@ -90,7 +89,7 @@ func New(alloc core.GenesisAlloc, gasLimit uint64) *Backend { conf.Genesis = &genesis conf.SyncMode = downloader.FullSync conf.TxPool.NoLocals = true - sim, err := newWithNode(stack, &conf, math.MaxUint64) + sim, err := newWithNode(stack, &conf, 0) if err != nil { // This should never happen, if it does, please open an issue panic(err)