mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 10:22:23 +00:00
eth/catalyst: minor refactoring
This commit is contained in:
parent
16fad83071
commit
2e96810a68
1 changed files with 24 additions and 31 deletions
|
|
@ -27,14 +27,13 @@ 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()
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-a.sim.shutdownCh:
|
|
||||||
return
|
|
||||||
case <-newWxs:
|
|
||||||
a.commit()
|
|
||||||
case <-newTxs:
|
|
||||||
a.commit()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// commit is a non-blocking method to initate Commit() on the simulator.
|
// commit is a non-blocking method to initate Commit() on the simulator.
|
||||||
func (a *simulatedBeaconAPI) commit() {
|
commit := func() {
|
||||||
select {
|
select {
|
||||||
case a.doCommit <- struct{}{}:
|
case doCommit <- struct{}{}:
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// a background thread which signals to the simulator when to commit
|
||||||
// worker runs in the background and signals to the simulator when to commit
|
|
||||||
// based on messages over doCommit.
|
// based on messages over doCommit.
|
||||||
func (a *simulatedBeaconAPI) worker() {
|
go func() {
|
||||||
for {
|
for _ = range doCommit {
|
||||||
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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue