diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 0bf1ad5333..e276e86c6a 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -129,7 +129,6 @@ var ( utils.MinerLegacyExtraDataFlag, utils.MinerRecommitIntervalFlag, utils.MinerNoVerfiyFlag, - utils.MinerNoEmptyPrecommitFlag, utils.NATFlag, utils.NoDiscoverFlag, utils.DiscoveryV5Flag, diff --git a/cmd/geth/usage.go b/cmd/geth/usage.go index eca52a3618..51e9f91848 100644 --- a/cmd/geth/usage.go +++ b/cmd/geth/usage.go @@ -210,7 +210,6 @@ var AppHelpFlagGroups = []flagGroup{ utils.MinerExtraDataFlag, utils.MinerRecommitIntervalFlag, utils.MinerNoVerfiyFlag, - utils.MinerNoEmptyPrecommitFlag, }, }, { diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index d07063196f..dbefc9d935 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -483,10 +483,6 @@ var ( Name: "miner.noverify", Usage: "Disable remote sealing verification", } - MinerNoEmptyPrecommitFlag = cli.BoolFlag{ - Name: "miner.noempty-precommit", - Usage: "Disable empty mining solution precommit", - } // Account settings UnlockedAccountFlag = cli.StringFlag{ Name: "unlock", @@ -1363,9 +1359,6 @@ func setMiner(ctx *cli.Context, cfg *miner.Config) { if ctx.GlobalIsSet(MinerNoVerfiyFlag.Name) { cfg.Noverify = ctx.Bool(MinerNoVerfiyFlag.Name) } - if ctx.GlobalIsSet(MinerNoEmptyPrecommitFlag.Name) { - cfg.NoEmptyPrecommit = ctx.Bool(MinerNoEmptyPrecommitFlag.Name) - } } func setWhitelist(ctx *cli.Context, cfg *eth.Config) { diff --git a/miner/miner.go b/miner/miner.go index b365fd833e..3094682cfc 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -43,15 +43,14 @@ type Backend interface { // Config is the configuration parameters of mining. type Config struct { - Etherbase common.Address `toml:",omitempty"` // Public address for block mining rewards (default = first account) - Notify []string `toml:",omitempty"` // HTTP URL list to be notified of new work packages(only useful in ethash). - ExtraData hexutil.Bytes `toml:",omitempty"` // Block extra data set by the miner - GasFloor uint64 // Target gas floor for mined blocks. - GasCeil uint64 // Target gas ceiling for mined blocks. - GasPrice *big.Int // Minimum gas price for mining a transaction - Recommit time.Duration // The time interval for miner to re-create mining work. - Noverify bool // Disable remote mining solution verification(only useful in ethash). - NoEmptyPrecommit bool // Disable pre-commit empty mining solution + Etherbase common.Address `toml:",omitempty"` // Public address for block mining rewards (default = first account) + Notify []string `toml:",omitempty"` // HTTP URL list to be notified of new work packages(only useful in ethash). + ExtraData hexutil.Bytes `toml:",omitempty"` // Block extra data set by the miner + GasFloor uint64 // Target gas floor for mined blocks. + GasCeil uint64 // Target gas ceiling for mined blocks. + GasPrice *big.Int // Minimum gas price for mining a transaction + Recommit time.Duration // The time interval for miner to re-create mining work. + Noverify bool // Disable remote mining solution verification(only useful in ethash). } // Miner creates blocks and searches for proof-of-work values. @@ -184,6 +183,23 @@ func (miner *Miner) SetEtherbase(addr common.Address) { 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 // to the given channel. func (self *Miner) SubscribePendingLogs(ch chan<- []*types.Log) event.Subscription { diff --git a/miner/worker.go b/miner/worker.go index 16ec5b47e7..346dd8ccb1 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -169,6 +169,13 @@ 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. + // 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 bool + // External functions 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 } +// disablePreseal disables pre-sealing mining feature +func (w *worker) disablePreseal() { + w.noempty = true +} + +// enablePreseal enables pre-sealing mining feature +func (w *worker) enablePreseal() { + w.noempty = false +} + // 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 @@ -305,10 +322,6 @@ func (w *worker) newWorkLoop(recommit time.Duration) { if interrupt != nil { atomic.StoreInt32(interrupt, s) } - // Disable empty mining solution precommit if required. - if !noempty && w.config.NoEmptyPrecommit { - noempty = true - } interrupt = new(int32) w.newWorkCh <- &newWorkReq{interrupt: interrupt, noempty: noempty, timestamp: timestamp} timer.Reset(recommit) @@ -484,8 +497,9 @@ func (w *worker) mainLoop() { w.updateSnapshot() } } else { - // If clique is running in dev mode(period is 0), disable - // advance sealing here. + // Speical case, if the consensus engine is 0 period clique(dev mode), + // 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 { w.commitNewWork(nil, true, time.Now().Unix()) } @@ -914,9 +928,9 @@ func (w *worker) commitNewWork(interrupt *int32, noempty bool, timestamp int64) commitUncles(w.localUncles) commitUncles(w.remoteUncles) - if !noempty { - // Create an empty block based on temporary copied state for sealing in advance without waiting block - // execution finished. + // Create an empty block based on temporary copied state for + // sealing in advance without waiting block execution finished. + if !noempty && !w.noempty { w.commit(uncles, nil, false, tstart) } @@ -929,7 +943,7 @@ func (w *worker) commitNewWork(interrupt *int32, noempty bool, timestamp int64) // Short circuit if there is no available pending transactions. // 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 && !w.config.NoEmptyPrecommit { + if len(pending) == 0 && !w.noempty { w.updateSnapshot() return }