forked from cerc-io/plugeth
miner: support disabling empty blockprecommits form the Go API (#20736)
* cmd, miner: add noempty-precommit flag * cmd, miner: get rid of external flag * miner: change bool to atomic int * miner: fix tiny typo Co-authored-by: Péter Szilágyi <peterke@gmail.com>
This commit is contained in:
parent
7540c53e72
commit
7b7e5921a4
@ -183,6 +183,23 @@ func (miner *Miner) SetEtherbase(addr common.Address) {
|
|||||||
miner.worker.setEtherbase(addr)
|
miner.worker.setEtherbase(addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EnablePreseal turns on the preseal mining feature. It's enabled by default.
|
||||||
|
// Note this function shouldn't be exposed to API, it's unnecessary for users
|
||||||
|
// (miners) to actually know the underlying detail. It's only for outside project
|
||||||
|
// which uses this library.
|
||||||
|
func (miner *Miner) EnablePreseal() {
|
||||||
|
miner.worker.enablePreseal()
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisablePreseal turns off the preseal mining feature. It's necessary for some
|
||||||
|
// fake consensus engine which can seal blocks instantaneously.
|
||||||
|
// Note this function shouldn't be exposed to API, it's unnecessary for users
|
||||||
|
// (miners) to actually know the underlying detail. It's only for outside project
|
||||||
|
// which uses this library.
|
||||||
|
func (miner *Miner) DisablePreseal() {
|
||||||
|
miner.worker.disablePreseal()
|
||||||
|
}
|
||||||
|
|
||||||
// SubscribePendingLogs starts delivering logs from pending transactions
|
// SubscribePendingLogs starts delivering logs from pending transactions
|
||||||
// to the given channel.
|
// to the given channel.
|
||||||
func (self *Miner) SubscribePendingLogs(ch chan<- []*types.Log) event.Subscription {
|
func (self *Miner) SubscribePendingLogs(ch chan<- []*types.Log) event.Subscription {
|
||||||
|
@ -169,6 +169,13 @@ type worker struct {
|
|||||||
running int32 // The indicator whether the consensus engine is running or not.
|
running int32 // The indicator whether the consensus engine is running or not.
|
||||||
newTxs int32 // New arrival transaction count since last sealing work submitting.
|
newTxs int32 // New arrival transaction count since last sealing work submitting.
|
||||||
|
|
||||||
|
// noempty is the flag used to control whether the feature of pre-seal empty
|
||||||
|
// block is enabled. The default value is false(pre-seal is enabled by default).
|
||||||
|
// But in some special scenario the consensus engine will seal blocks instantaneously,
|
||||||
|
// in this case this feature will add all empty blocks into canonical chain
|
||||||
|
// non-stop and no real transaction will be included.
|
||||||
|
noempty uint32
|
||||||
|
|
||||||
// External functions
|
// External functions
|
||||||
isLocalBlock func(block *types.Block) bool // Function used to determine whether the specified block is mined by local miner.
|
isLocalBlock func(block *types.Block) bool // Function used to determine whether the specified block is mined by local miner.
|
||||||
|
|
||||||
@ -247,6 +254,16 @@ func (w *worker) setRecommitInterval(interval time.Duration) {
|
|||||||
w.resubmitIntervalCh <- interval
|
w.resubmitIntervalCh <- interval
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// disablePreseal disables pre-sealing mining feature
|
||||||
|
func (w *worker) disablePreseal() {
|
||||||
|
atomic.StoreUint32(&w.noempty, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// enablePreseal enables pre-sealing mining feature
|
||||||
|
func (w *worker) enablePreseal() {
|
||||||
|
atomic.StoreUint32(&w.noempty, 0)
|
||||||
|
}
|
||||||
|
|
||||||
// pending returns the pending state and corresponding block.
|
// pending returns the pending state and corresponding block.
|
||||||
func (w *worker) pending() (*types.Block, *state.StateDB) {
|
func (w *worker) pending() (*types.Block, *state.StateDB) {
|
||||||
// return a snapshot to avoid contention on currentMu mutex
|
// return a snapshot to avoid contention on currentMu mutex
|
||||||
@ -480,8 +497,9 @@ func (w *worker) mainLoop() {
|
|||||||
w.updateSnapshot()
|
w.updateSnapshot()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// If clique is running in dev mode(period is 0), disable
|
// Special case, if the consensus engine is 0 period clique(dev mode),
|
||||||
// advance sealing here.
|
// submit mining work here since all empty submission will be rejected
|
||||||
|
// by clique. Of course the advance sealing(empty submission) is disabled.
|
||||||
if w.chainConfig.Clique != nil && w.chainConfig.Clique.Period == 0 {
|
if w.chainConfig.Clique != nil && w.chainConfig.Clique.Period == 0 {
|
||||||
w.commitNewWork(nil, true, time.Now().Unix())
|
w.commitNewWork(nil, true, time.Now().Unix())
|
||||||
}
|
}
|
||||||
@ -910,9 +928,9 @@ func (w *worker) commitNewWork(interrupt *int32, noempty bool, timestamp int64)
|
|||||||
commitUncles(w.localUncles)
|
commitUncles(w.localUncles)
|
||||||
commitUncles(w.remoteUncles)
|
commitUncles(w.remoteUncles)
|
||||||
|
|
||||||
if !noempty {
|
// Create an empty block based on temporary copied state for
|
||||||
// Create an empty block based on temporary copied state for sealing in advance without waiting block
|
// sealing in advance without waiting block execution finished.
|
||||||
// execution finished.
|
if !noempty && atomic.LoadUint32(&w.noempty) == 0 {
|
||||||
w.commit(uncles, nil, false, tstart)
|
w.commit(uncles, nil, false, tstart)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -922,8 +940,10 @@ func (w *worker) commitNewWork(interrupt *int32, noempty bool, timestamp int64)
|
|||||||
log.Error("Failed to fetch pending transactions", "err", err)
|
log.Error("Failed to fetch pending transactions", "err", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Short circuit if there is no available pending transactions
|
// Short circuit if there is no available pending transactions.
|
||||||
if len(pending) == 0 {
|
// But if we disable empty precommit already, ignore it. Since
|
||||||
|
// empty block is necessary to keep the liveness of the network.
|
||||||
|
if len(pending) == 0 && atomic.LoadUint32(&w.noempty) == 0 {
|
||||||
w.updateSnapshot()
|
w.updateSnapshot()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user