mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
eth/catalyst: apply lightclients suggestions
moves the loopOnDemand outside making it part of the api
This commit is contained in:
parent
a3276479b2
commit
6b29940218
3 changed files with 36 additions and 38 deletions
|
|
@ -19,14 +19,12 @@ package catalyst
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"errors"
|
"errors"
|
||||||
"math"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/engine"
|
"github.com/ethereum/go-ethereum/beacon/engine"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/core"
|
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/eth"
|
"github.com/ethereum/go-ethereum/eth"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
|
|
@ -88,7 +86,6 @@ type SimulatedBeacon struct {
|
||||||
// Period sets the period in which blocks should be produced.
|
// 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 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.
|
// via Commit, Fork and AdjustTime.
|
||||||
func NewSimulatedBeacon(period uint64, eth *eth.Ethereum) (*SimulatedBeacon, error) {
|
func NewSimulatedBeacon(period uint64, eth *eth.Ethereum) (*SimulatedBeacon, error) {
|
||||||
block := eth.BlockChain().CurrentBlock()
|
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.
|
// Start invokes the SimulatedBeacon life-cycle function in a goroutine.
|
||||||
func (c *SimulatedBeacon) Start() error {
|
func (c *SimulatedBeacon) Start() error {
|
||||||
if c.period == 0 {
|
if c.period == 0 {
|
||||||
go c.loopOnDemand()
|
// if period is set to 0, do not mine at all
|
||||||
} else if c.period == math.MaxUint64 {
|
|
||||||
// if period is set to MaxUint, do not mine at all
|
|
||||||
// this is used in the simulated backend where blocks
|
// this is used in the simulated backend where blocks
|
||||||
// are explicitly mined via Commit, AdjustTime and Fork
|
// are explicitly mined via Commit, AdjustTime and Fork
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -204,32 +199,6 @@ func (c *SimulatedBeacon) sealBlock(withdrawals []*types.Withdrawal, timestamp u
|
||||||
return nil
|
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
|
// loop runs the block production loop for non-zero period configuration
|
||||||
func (c *SimulatedBeacon) loop() {
|
func (c *SimulatedBeacon) loop() {
|
||||||
timer := time.NewTimer(0)
|
timer := time.NewTimer(0)
|
||||||
|
|
@ -318,10 +287,15 @@ func (c *SimulatedBeacon) AdjustTime(adjustment time.Duration) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterSimulatedBeaconAPIs(stack *node.Node, sim *SimulatedBeacon) {
|
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{
|
stack.RegisterAPIs([]rpc.API{
|
||||||
{
|
{
|
||||||
Namespace: "dev",
|
Namespace: "dev",
|
||||||
Service: &api{sim},
|
Service: api,
|
||||||
Version: "1.0",
|
Version: "1.0",
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -18,19 +18,44 @@ package catalyst
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/core"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
|
"github.com/ethereum/go-ethereum/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type api struct {
|
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 {
|
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) {
|
func (a *api) SetFeeRecipient(ctx context.Context, feeRecipient common.Address) {
|
||||||
a.simBeacon.setFeeRecipient(feeRecipient)
|
a.sim.setFeeRecipient(feeRecipient)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,6 @@
|
||||||
package simulated
|
package simulated
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum"
|
"github.com/ethereum/go-ethereum"
|
||||||
|
|
@ -90,7 +89,7 @@ func New(alloc core.GenesisAlloc, gasLimit uint64) *Backend {
|
||||||
conf.Genesis = &genesis
|
conf.Genesis = &genesis
|
||||||
conf.SyncMode = downloader.FullSync
|
conf.SyncMode = downloader.FullSync
|
||||||
conf.TxPool.NoLocals = true
|
conf.TxPool.NoLocals = true
|
||||||
sim, err := newWithNode(stack, &conf, math.MaxUint64)
|
sim, err := newWithNode(stack, &conf, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// This should never happen, if it does, please open an issue
|
// This should never happen, if it does, please open an issue
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue