eth/catalyst: minor refactoring

This commit is contained in:
Martin Holst Swende 2024-08-19 14:12:51 +02:00
parent 16fad83071
commit 2e96810a68
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0

View file

@ -26,15 +26,14 @@ import (
// simulatedBeaconAPI provides a RPC API for SimulatedBeacon. // simulatedBeaconAPI provides a RPC API for SimulatedBeacon.
type simulatedBeaconAPI struct { type simulatedBeaconAPI struct {
sim *SimulatedBeacon sim *SimulatedBeacon
doCommit chan struct{}
} }
// newSimulatedBeaconAPI returns an instance of simulatedBeaconAPI with a // newSimulatedBeaconAPI returns an instance of simulatedBeaconAPI with a
// buffered commit channel. If period is zero, it starts a goroutine to handle // buffered commit channel. If period is zero, it starts a goroutine to handle
// new tx events. // new tx events.
func newSimulatedBeaconAPI(sim *SimulatedBeacon) *simulatedBeaconAPI { func newSimulatedBeaconAPI(sim *SimulatedBeacon) *simulatedBeaconAPI {
api := &simulatedBeaconAPI{sim: sim, doCommit: make(chan struct{}, 1)} api := &simulatedBeaconAPI{sim: sim}
if sim.period == 0 { if sim.period == 0 {
// mine on demand if period is set to 0 // mine on demand if period is set to 0
go api.loop() go api.loop()
@ -51,47 +50,41 @@ func (a *simulatedBeaconAPI) loop() {
newWxs = make(chan newWithdrawalsEvent) newWxs = make(chan newWithdrawalsEvent)
newTxsSub = a.sim.eth.TxPool().SubscribeTransactions(newTxs, true) newTxsSub = a.sim.eth.TxPool().SubscribeTransactions(newTxs, true)
newWxsSub = a.sim.withdrawals.subscribe(newWxs) newWxsSub = a.sim.withdrawals.subscribe(newWxs)
doCommit = make(chan struct{}, 1)
) )
defer newTxsSub.Unsubscribe() defer newTxsSub.Unsubscribe()
defer newWxsSub.Unsubscribe() defer newWxsSub.Unsubscribe()
go a.worker() // commit is a non-blocking method to initate Commit() on the simulator.
commit := func() {
for {
select { select {
case <-a.sim.shutdownCh: case doCommit <- struct{}{}:
return default:
case <-newWxs:
a.commit()
case <-newTxs:
a.commit()
} }
} }
} // a background thread which signals to the simulator when to commit
// based on messages over doCommit.
// commit is a non-blocking method to initate Commit() on the simulator. go func() {
func (a *simulatedBeaconAPI) commit() { for _ = range doCommit {
select {
case a.doCommit <- struct{}{}:
default:
}
}
// worker runs in the background and signals to the simulator when to commit
// based on messages over doCommit.
func (a *simulatedBeaconAPI) worker() {
for {
select {
case <-a.sim.shutdownCh:
return
case <-a.doCommit:
a.sim.Commit() a.sim.Commit()
a.sim.eth.TxPool().Sync() a.sim.eth.TxPool().Sync()
executable, _ := a.sim.eth.TxPool().Stats() executable, _ := a.sim.eth.TxPool().Stats()
if executable != 0 { if executable != 0 {
a.commit() commit()
} }
} }
}()
for {
select {
case <-a.sim.shutdownCh:
close(doCommit)
return
case <-newWxs:
commit()
case <-newTxs:
commit()
}
} }
} }