Skip to content

Commit 7b7e592

Browse files
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 <[email protected]>
1 parent 7540c53 commit 7b7e592

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

miner/miner.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,23 @@ func (miner *Miner) SetEtherbase(addr common.Address) {
183183
miner.worker.setEtherbase(addr)
184184
}
185185

186+
// EnablePreseal turns on the preseal mining feature. It's enabled by default.
187+
// Note this function shouldn't be exposed to API, it's unnecessary for users
188+
// (miners) to actually know the underlying detail. It's only for outside project
189+
// which uses this library.
190+
func (miner *Miner) EnablePreseal() {
191+
miner.worker.enablePreseal()
192+
}
193+
194+
// DisablePreseal turns off the preseal mining feature. It's necessary for some
195+
// fake consensus engine which can seal blocks instantaneously.
196+
// Note this function shouldn't be exposed to API, it's unnecessary for users
197+
// (miners) to actually know the underlying detail. It's only for outside project
198+
// which uses this library.
199+
func (miner *Miner) DisablePreseal() {
200+
miner.worker.disablePreseal()
201+
}
202+
186203
// SubscribePendingLogs starts delivering logs from pending transactions
187204
// to the given channel.
188205
func (self *Miner) SubscribePendingLogs(ch chan<- []*types.Log) event.Subscription {

miner/worker.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ type worker struct {
169169
running int32 // The indicator whether the consensus engine is running or not.
170170
newTxs int32 // New arrival transaction count since last sealing work submitting.
171171

172+
// noempty is the flag used to control whether the feature of pre-seal empty
173+
// block is enabled. The default value is false(pre-seal is enabled by default).
174+
// But in some special scenario the consensus engine will seal blocks instantaneously,
175+
// in this case this feature will add all empty blocks into canonical chain
176+
// non-stop and no real transaction will be included.
177+
noempty uint32
178+
172179
// External functions
173180
isLocalBlock func(block *types.Block) bool // Function used to determine whether the specified block is mined by local miner.
174181

@@ -247,6 +254,16 @@ func (w *worker) setRecommitInterval(interval time.Duration) {
247254
w.resubmitIntervalCh <- interval
248255
}
249256

257+
// disablePreseal disables pre-sealing mining feature
258+
func (w *worker) disablePreseal() {
259+
atomic.StoreUint32(&w.noempty, 1)
260+
}
261+
262+
// enablePreseal enables pre-sealing mining feature
263+
func (w *worker) enablePreseal() {
264+
atomic.StoreUint32(&w.noempty, 0)
265+
}
266+
250267
// pending returns the pending state and corresponding block.
251268
func (w *worker) pending() (*types.Block, *state.StateDB) {
252269
// return a snapshot to avoid contention on currentMu mutex
@@ -480,8 +497,9 @@ func (w *worker) mainLoop() {
480497
w.updateSnapshot()
481498
}
482499
} else {
483-
// If clique is running in dev mode(period is 0), disable
484-
// advance sealing here.
500+
// Special case, if the consensus engine is 0 period clique(dev mode),
501+
// submit mining work here since all empty submission will be rejected
502+
// by clique. Of course the advance sealing(empty submission) is disabled.
485503
if w.chainConfig.Clique != nil && w.chainConfig.Clique.Period == 0 {
486504
w.commitNewWork(nil, true, time.Now().Unix())
487505
}
@@ -910,9 +928,9 @@ func (w *worker) commitNewWork(interrupt *int32, noempty bool, timestamp int64)
910928
commitUncles(w.localUncles)
911929
commitUncles(w.remoteUncles)
912930

913-
if !noempty {
914-
// Create an empty block based on temporary copied state for sealing in advance without waiting block
915-
// execution finished.
931+
// Create an empty block based on temporary copied state for
932+
// sealing in advance without waiting block execution finished.
933+
if !noempty && atomic.LoadUint32(&w.noempty) == 0 {
916934
w.commit(uncles, nil, false, tstart)
917935
}
918936

@@ -922,8 +940,10 @@ func (w *worker) commitNewWork(interrupt *int32, noempty bool, timestamp int64)
922940
log.Error("Failed to fetch pending transactions", "err", err)
923941
return
924942
}
925-
// Short circuit if there is no available pending transactions
926-
if len(pending) == 0 {
943+
// Short circuit if there is no available pending transactions.
944+
// But if we disable empty precommit already, ignore it. Since
945+
// empty block is necessary to keep the liveness of the network.
946+
if len(pending) == 0 && atomic.LoadUint32(&w.noempty) == 0 {
927947
w.updateSnapshot()
928948
return
929949
}

0 commit comments

Comments
 (0)