Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

worker: remove pre-seal empty block #1184

Merged
merged 1 commit into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,23 +240,6 @@ func (miner *Miner) SetGasCeil(ceil uint64) {
miner.worker.setGasCeil(ceil)
}

// 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()
}

// GetSealingBlock retrieves a sealing block based on the given parameters.
// The returned block is not sealed but all other fields should be filled.
func (miner *Miner) GetSealingBlock(parent common.Hash, timestamp uint64, coinbase common.Address, random common.Hash) (*types.Block, error) {
Expand Down
39 changes: 8 additions & 31 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ const (
// newWorkReq represents a request for new sealing work submitting with relative interrupt notifier.
type newWorkReq struct {
interruptCh chan int32
noempty bool
timestamp int64
}

Expand Down Expand Up @@ -214,13 +213,6 @@ type worker struct {
running int32 // The indicator whether the consensus engine is running or not.
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
isLocalBlock func(header *types.Header) bool // Function used to determine whether the specified block is mined by local miner.

Expand Down Expand Up @@ -310,16 +302,6 @@ func (w *worker) setRecommitInterval(interval time.Duration) {
}
}

// disablePreseal disables pre-sealing feature
func (w *worker) disablePreseal() {
atomic.StoreUint32(&w.noempty, 1)
}

// enablePreseal enables pre-sealing feature
func (w *worker) enablePreseal() {
atomic.StoreUint32(&w.noempty, 0)
}

// pending returns the pending state and corresponding block.
func (w *worker) pending() (*types.Block, *state.StateDB) {
// return a snapshot to avoid contention on currentMu mutex
Expand Down Expand Up @@ -385,15 +367,15 @@ func (w *worker) newWorkLoop(recommit time.Duration) {
<-timer.C // discard the initial tick

// commit aborts in-flight transaction execution with given signal and resubmits a new one.
commit := func(noempty bool, reason int32) {
commit := func(reason int32) {
if interruptCh != nil {
// each commit work will have its own interruptCh to stop work with a reason
interruptCh <- reason
close(interruptCh)
}
interruptCh = make(chan int32, 1)
select {
case w.newWorkCh <- &newWorkReq{interruptCh: interruptCh, noempty: noempty, timestamp: timestamp}:
case w.newWorkCh <- &newWorkReq{interruptCh: interruptCh, timestamp: timestamp}:
case <-w.exitCh:
return
}
Expand All @@ -416,7 +398,7 @@ func (w *worker) newWorkLoop(recommit time.Duration) {
case <-w.startCh:
clearPending(w.chain.CurrentBlock().NumberU64())
timestamp = time.Now().Unix()
commit(true, commitInterruptNewHead)
commit(commitInterruptNewHead)

case head := <-w.chainHeadCh:
if !w.isRunning() {
Expand All @@ -435,7 +417,7 @@ func (w *worker) newWorkLoop(recommit time.Duration) {
continue
}
}
commit(true, commitInterruptNewHead)
commit(commitInterruptNewHead)

case <-timer.C:
// If sealing is running resubmit a new work cycle periodically to pull in
Expand All @@ -447,7 +429,7 @@ func (w *worker) newWorkLoop(recommit time.Duration) {
timer.Reset(recommit)
continue
}
commit(true, commitInterruptResubmit)
commit(commitInterruptResubmit)
}

case interval := <-w.resubmitIntervalCh:
Expand Down Expand Up @@ -489,7 +471,7 @@ func (w *worker) mainLoop() {
for {
select {
case req := <-w.newWorkCh:
w.commitWork(req.interruptCh, req.noempty, req.timestamp)
w.commitWork(req.interruptCh, req.timestamp)

case req := <-w.getWorkCh:
block, err := w.generateWork(req.params)
Expand Down Expand Up @@ -547,7 +529,7 @@ func (w *worker) mainLoop() {
// by clique. Of course the advance sealing(empty submission) is disabled.
if (w.chainConfig.Clique != nil && w.chainConfig.Clique.Period == 0) ||
(w.chainConfig.Parlia != nil && w.chainConfig.Parlia.Period == 0) {
w.commitWork(nil, true, time.Now().Unix())
w.commitWork(nil, time.Now().Unix())
}
}

Expand Down Expand Up @@ -1054,7 +1036,7 @@ func (w *worker) generateWork(params *generateParams) (*types.Block, error) {

// commitWork generates several new sealing tasks based on the parent block
// and submit them to the sealer.
func (w *worker) commitWork(interruptCh chan int32, noempty bool, timestamp int64) {
func (w *worker) commitWork(interruptCh chan int32, timestamp int64) {
start := time.Now()

// Set the coinbase if the worker is running or it's required
Expand All @@ -1074,11 +1056,6 @@ func (w *worker) commitWork(interruptCh chan int32, noempty bool, timestamp int6
return
}

// Create an empty block based on temporary copied state for
// sealing in advance without waiting block execution finished.
if !noempty && atomic.LoadUint32(&w.noempty) == 0 {
w.commit(work, nil, false, start)
}
// Fill pending transactions from the txpool
w.fillTransactions(interruptCh, work)
w.commit(work, w.fullTaskHook, true, start)
Expand Down