From d08db9566428731f3e4bb88193c2b8340d83bc8f Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Mon, 20 Aug 2018 21:15:22 +0800 Subject: [PATCH] cmd, eth, les, miner: polish a bit --- cmd/utils/flags.go | 22 +++++++------- eth/backend.go | 8 ++--- eth/config.go | 16 +++++----- eth/gen_config.go | 30 +++++++++---------- les/backend.go | 2 +- miner/miner.go | 5 ++-- miner/worker.go | 74 +++++++++++++++++++++++----------------------- 7 files changed, 79 insertions(+), 78 deletions(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 57769fdcda..ae51ea8530 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -335,12 +335,12 @@ var ( MinerGasPriceFlag = BigFlag{ Name: "miner.gasprice", Usage: "Minimal gas price for mining a transactions", - Value: eth.DefaultConfig.GasPrice, + Value: eth.DefaultConfig.MinerGasPrice, } MinerLegacyGasPriceFlag = BigFlag{ Name: "gasprice", Usage: "Minimal gas price for mining a transactions (deprecated, use --miner.gasprice)", - Value: eth.DefaultConfig.GasPrice, + Value: eth.DefaultConfig.MinerGasPrice, } MinerEtherbaseFlag = cli.StringFlag{ Name: "miner.etherbase", @@ -360,10 +360,10 @@ var ( Name: "extradata", Usage: "Block extra data set by the miner (default = client version, deprecated, use --miner.extradata)", } - MinerRecommitIntervalFlag = cli.IntFlag{ + MinerRecommitIntervalFlag = cli.DurationFlag{ Name: "miner.recommit", - Usage: "Sealing work recommit interval(ms), will be dynamically adjusted by the system if it is too short", - Value: 3000, + Usage: "Time interval to recreate the block being mined.", + Value: time.Duration(3 * time.Second), } // Account settings UnlockedAccountFlag = cli.StringFlag{ @@ -1129,19 +1129,19 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) { cfg.DocRoot = ctx.GlobalString(DocRootFlag.Name) } if ctx.GlobalIsSet(MinerLegacyExtraDataFlag.Name) { - cfg.ExtraData = []byte(ctx.GlobalString(MinerLegacyExtraDataFlag.Name)) + cfg.MinerExtraData = []byte(ctx.GlobalString(MinerLegacyExtraDataFlag.Name)) } if ctx.GlobalIsSet(MinerExtraDataFlag.Name) { - cfg.ExtraData = []byte(ctx.GlobalString(MinerExtraDataFlag.Name)) + cfg.MinerExtraData = []byte(ctx.GlobalString(MinerExtraDataFlag.Name)) } if ctx.GlobalIsSet(MinerLegacyGasPriceFlag.Name) { - cfg.GasPrice = GlobalBig(ctx, MinerLegacyGasPriceFlag.Name) + cfg.MinerGasPrice = GlobalBig(ctx, MinerLegacyGasPriceFlag.Name) } if ctx.GlobalIsSet(MinerGasPriceFlag.Name) { - cfg.GasPrice = GlobalBig(ctx, MinerGasPriceFlag.Name) + cfg.MinerGasPrice = GlobalBig(ctx, MinerGasPriceFlag.Name) } if ctx.GlobalIsSet(MinerRecommitIntervalFlag.Name) { - cfg.RecommitInterval = time.Duration(ctx.GlobalInt(MinerRecommitIntervalFlag.Name)) * time.Millisecond + cfg.MinerRecommit = ctx.Duration(MinerRecommitIntervalFlag.Name) } if ctx.GlobalIsSet(VMEnableDebugFlag.Name) { // TODO(fjl): force-enable this in --dev mode @@ -1184,7 +1184,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) { cfg.Genesis = core.DeveloperGenesisBlock(uint64(ctx.GlobalInt(DeveloperPeriodFlag.Name)), developer.Address) if !ctx.GlobalIsSet(MinerGasPriceFlag.Name) && !ctx.GlobalIsSet(MinerLegacyGasPriceFlag.Name) { - cfg.GasPrice = big.NewInt(1) + cfg.MinerGasPrice = big.NewInt(1) } } // TODO(fjl): move trie cache generations into config diff --git a/eth/backend.go b/eth/backend.go index 2242a881f5..f788b1724a 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -127,7 +127,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { engine: CreateConsensusEngine(ctx, chainConfig, &config.Ethash, config.MinerNotify, chainDb), shutdownChan: make(chan bool), networkID: config.NetworkId, - gasPrice: config.GasPrice, + gasPrice: config.MinerGasPrice, etherbase: config.Etherbase, bloomRequests: make(chan chan *bloombits.Retrieval), bloomIndexer: NewBloomIndexer(chainDb, params.BloomBitsBlocks, bloomConfirms), @@ -167,13 +167,13 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { return nil, err } - eth.miner = miner.New(eth, eth.chainConfig, eth.EventMux(), eth.engine, config.RecommitInterval) - eth.miner.SetExtra(makeExtraData(config.ExtraData)) + eth.miner = miner.New(eth, eth.chainConfig, eth.EventMux(), eth.engine, config.MinerRecommit) + eth.miner.SetExtra(makeExtraData(config.MinerExtraData)) eth.APIBackend = &EthAPIBackend{eth, nil} gpoParams := config.GPO if gpoParams.Default == nil { - gpoParams.Default = config.GasPrice + gpoParams.Default = config.MinerGasPrice } eth.APIBackend.gpo = gasprice.NewOracle(eth.APIBackend, gpoParams) diff --git a/eth/config.go b/eth/config.go index 66db57c5e3..cbd02416be 100644 --- a/eth/config.go +++ b/eth/config.go @@ -48,7 +48,7 @@ var DefaultConfig = Config{ DatabaseCache: 768, TrieCache: 256, TrieTimeout: 60 * time.Minute, - GasPrice: big.NewInt(18 * params.Shannon), + MinerGasPrice: big.NewInt(18 * params.Shannon), TxPool: core.DefaultTxPoolConfig, GPO: gasprice.Config{ @@ -95,12 +95,12 @@ type Config struct { TrieTimeout time.Duration // Mining-related options - Etherbase common.Address `toml:",omitempty"` - MinerThreads int `toml:",omitempty"` - MinerNotify []string `toml:",omitempty"` - ExtraData []byte `toml:",omitempty"` - GasPrice *big.Int - RecommitInterval time.Duration + Etherbase common.Address `toml:",omitempty"` + MinerThreads int `toml:",omitempty"` + MinerNotify []string `toml:",omitempty"` + MinerExtraData []byte `toml:",omitempty"` + MinerGasPrice *big.Int + MinerRecommit time.Duration // Ethash options Ethash ethash.Config @@ -119,5 +119,5 @@ type Config struct { } type configMarshaling struct { - ExtraData hexutil.Bytes + MinerExtraData hexutil.Bytes } diff --git a/eth/gen_config.go b/eth/gen_config.go index 39c668119e..62556be7e1 100644 --- a/eth/gen_config.go +++ b/eth/gen_config.go @@ -33,9 +33,9 @@ func (c Config) MarshalTOML() (interface{}, error) { Etherbase common.Address `toml:",omitempty"` MinerThreads int `toml:",omitempty"` MinerNotify []string `toml:",omitempty"` - ExtraData hexutil.Bytes `toml:",omitempty"` - GasPrice *big.Int - RecommitInterval time.Duration + MinerExtraData hexutil.Bytes `toml:",omitempty"` + MinerGasPrice *big.Int + MinerRecommit time.Duration Ethash ethash.Config TxPool core.TxPoolConfig GPO gasprice.Config @@ -57,9 +57,9 @@ func (c Config) MarshalTOML() (interface{}, error) { enc.Etherbase = c.Etherbase enc.MinerThreads = c.MinerThreads enc.MinerNotify = c.MinerNotify - enc.ExtraData = c.ExtraData - enc.GasPrice = c.GasPrice - enc.RecommitInterval = c.RecommitInterval + enc.MinerExtraData = c.MinerExtraData + enc.MinerGasPrice = c.MinerGasPrice + enc.MinerRecommit = c.MinerRecommit enc.Ethash = c.Ethash enc.TxPool = c.TxPool enc.GPO = c.GPO @@ -85,9 +85,9 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { Etherbase *common.Address `toml:",omitempty"` MinerThreads *int `toml:",omitempty"` MinerNotify []string `toml:",omitempty"` - ExtraData *hexutil.Bytes `toml:",omitempty"` - GasPrice *big.Int - RecommitInterval *time.Duration + MinerExtraData *hexutil.Bytes `toml:",omitempty"` + MinerGasPrice *big.Int + MinerRecommit *time.Duration Ethash *ethash.Config TxPool *core.TxPoolConfig GPO *gasprice.Config @@ -140,14 +140,14 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { if dec.MinerNotify != nil { c.MinerNotify = dec.MinerNotify } - if dec.ExtraData != nil { - c.ExtraData = *dec.ExtraData + if dec.MinerExtraData != nil { + c.MinerExtraData = *dec.MinerExtraData } - if dec.GasPrice != nil { - c.GasPrice = dec.GasPrice + if dec.MinerGasPrice != nil { + c.MinerGasPrice = dec.MinerGasPrice } - if dec.RecommitInterval != nil { - c.RecommitInterval = *dec.RecommitInterval + if dec.MinerRecommit != nil { + c.MinerRecommit = *dec.MinerRecommit } if dec.Ethash != nil { c.Ethash = *dec.Ethash diff --git a/les/backend.go b/les/backend.go index d26c1470fe..00025ba634 100644 --- a/les/backend.go +++ b/les/backend.go @@ -141,7 +141,7 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) { leth.ApiBackend = &LesApiBackend{leth, nil} gpoParams := config.GPO if gpoParams.Default == nil { - gpoParams.Default = config.GasPrice + gpoParams.Default = config.MinerGasPrice } leth.ApiBackend.gpo = gasprice.NewOracle(leth.ApiBackend, gpoParams) return leth, nil diff --git a/miner/miner.go b/miner/miner.go index b6f9616a20..c5a0c9d62a 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -52,13 +52,13 @@ type Miner struct { shouldStart int32 // should start indicates whether we should start after sync } -func New(eth Backend, config *params.ChainConfig, mux *event.TypeMux, engine consensus.Engine, recommitInterval time.Duration) *Miner { +func New(eth Backend, config *params.ChainConfig, mux *event.TypeMux, engine consensus.Engine, recommit time.Duration) *Miner { miner := &Miner{ eth: eth, mux: mux, engine: engine, exitCh: make(chan struct{}), - worker: newWorker(config, engine, eth, mux, recommitInterval), + worker: newWorker(config, engine, eth, mux, recommit), canStart: 1, } go miner.update() @@ -145,6 +145,7 @@ func (self *Miner) SetExtra(extra []byte) error { return nil } +// SetRecommitInterval sets the interval for sealing work resubmitting. func (self *Miner) SetRecommitInterval(interval time.Duration) { self.worker.setRecommitInterval(interval) } diff --git a/miner/worker.go b/miner/worker.go index 713ec8ab15..5325f046f8 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -164,7 +164,7 @@ type worker struct { resubmitHook func(time.Duration, time.Duration) // Method to call upon updating resubmitting interval. } -func newWorker(config *params.ChainConfig, engine consensus.Engine, eth Backend, mux *event.TypeMux, recommitInterval time.Duration) *worker { +func newWorker(config *params.ChainConfig, engine consensus.Engine, eth Backend, mux *event.TypeMux, recommit time.Duration) *worker { worker := &worker{ config: config, engine: engine, @@ -190,14 +190,14 @@ func newWorker(config *params.ChainConfig, engine consensus.Engine, eth Backend, worker.chainHeadSub = eth.BlockChain().SubscribeChainHeadEvent(worker.chainHeadCh) worker.chainSideSub = eth.BlockChain().SubscribeChainSideEvent(worker.chainSideCh) - // Recap recommit interval if the user-specified one is too short. - if recommitInterval < minRecommitInterval { - log.Info("Recap miner recommit interval", "from", recommitInterval, "to", minRecommitInterval) - recommitInterval = minRecommitInterval + // Sanitize recommit interval if the user-specified one is too short. + if recommit < minRecommitInterval { + log.Warn("Sanitizing miner recommit interval", "provided", recommit, "updated", minRecommitInterval) + recommit = minRecommitInterval } go worker.mainLoop() - go worker.newWorkLoop(recommitInterval) + go worker.newWorkLoop(recommit) go worker.resultLoop() go worker.taskLoop() @@ -276,92 +276,92 @@ func (w *worker) close() { } // newWorkLoop is a standalone goroutine to submit new mining work upon received events. -func (w *worker) newWorkLoop(recommitInterval time.Duration) { +func (w *worker) newWorkLoop(recommit time.Duration) { var ( interrupt *int32 - minInterval = recommitInterval // minimal resubmit interval specified by user. + minRecommit = recommit // minimal resubmit interval specified by user. ) timer := time.NewTimer(0) <-timer.C // discard the initial tick - // recommit aborts in-flight transaction execution with given signal and resubmits a new one. - recommit := func(noempty bool, s int32) { + // commit aborts in-flight transaction execution with given signal and resubmits a new one. + commit := func(noempty bool, s int32) { if interrupt != nil { atomic.StoreInt32(interrupt, s) } interrupt = new(int32) w.newWorkCh <- &newWorkReq{interrupt: interrupt, noempty: noempty} - timer.Reset(recommitInterval) + timer.Reset(recommit) } - // recalcuInterval recalculates the resubmitting interval upon feedback. - recalcuInterval := func(target float64, inc bool) { + // recalcRecommit recalculates the resubmitting interval upon feedback. + recalcRecommit := func(target float64, inc bool) { var ( - old = float64(recommitInterval.Nanoseconds()) - new float64 + prev = float64(recommit.Nanoseconds()) + next float64 ) if inc { - new = old*(1-intervalAdjustRatio) + intervalAdjustRatio*(target+intervalAdjustBias) + next = prev*(1-intervalAdjustRatio) + intervalAdjustRatio*(target+intervalAdjustBias) // Recap if interval is larger than the maximum time interval - if new > float64(maxRecommitInterval.Nanoseconds()) { - new = float64(maxRecommitInterval.Nanoseconds()) + if next > float64(maxRecommitInterval.Nanoseconds()) { + next = float64(maxRecommitInterval.Nanoseconds()) } } else { // Short circuit if the interval not larger than the minimal interval specified by user. - if recommitInterval <= minInterval { + if recommit <= minRecommit { return } - new = old*(1-intervalAdjustRatio) + intervalAdjustRatio*(target-intervalAdjustBias) + next = prev*(1-intervalAdjustRatio) + intervalAdjustRatio*(target-intervalAdjustBias) // Recap if interval is less than the user specified minimum - if new < float64(minInterval.Nanoseconds()) { - new = float64(minInterval.Nanoseconds()) + if next < float64(minRecommit.Nanoseconds()) { + next = float64(minRecommit.Nanoseconds()) } } - recommitInterval = time.Duration(int64(new)) + recommit = time.Duration(int64(next)) } for { select { case <-w.startCh: - recommit(false, commitInterruptNewHead) + commit(false, commitInterruptNewHead) case <-w.chainHeadCh: - recommit(false, commitInterruptNewHead) + commit(false, commitInterruptNewHead) case <-timer.C: // If mining is running resubmit a new work cycle periodically to pull in // higher priced transactions. Disable this overhead for pending blocks. if w.isRunning() && (w.config.Clique == nil || w.config.Clique.Period > 0) { - recommit(true, commitInterruptResubmit) + commit(true, commitInterruptResubmit) } case interval := <-w.resubmitIntervalCh: // Adjust resubmit interval explicitly by user. if interval < minRecommitInterval { - log.Info("Recap miner recommit interval", "from", interval, "to", minRecommitInterval) + log.Warn("Sanitizing miner recommit interval", "provided", interval, "updated", minRecommitInterval) interval = minRecommitInterval } - log.Info("Miner recommit interval update", "from", minInterval, "to", interval) - minInterval, recommitInterval = interval, interval + log.Info("Miner recommit interval update", "from", minRecommit, "to", interval) + minRecommit, recommit = interval, interval if w.resubmitHook != nil { - w.resubmitHook(minInterval, recommitInterval) + w.resubmitHook(minRecommit, recommit) } case adjust := <-w.resubmitAdjustCh: // Adjust resubmit interval by feedback. if adjust.inc { - before := recommitInterval - recalcuInterval(float64(recommitInterval.Nanoseconds())/adjust.ratio, true) - log.Trace("Increase miner recommit interval", "from", before, "to", recommitInterval) + before := recommit + recalcRecommit(float64(recommit.Nanoseconds())/adjust.ratio, true) + log.Trace("Increase miner recommit interval", "from", before, "to", recommit) } else { - before := recommitInterval - recalcuInterval(float64(minInterval.Nanoseconds()), false) - log.Trace("Decrease miner recommit interval", "from", before, "to", recommitInterval) + before := recommit + recalcRecommit(float64(minRecommit.Nanoseconds()), false) + log.Trace("Decrease miner recommit interval", "from", before, "to", recommit) } if w.resubmitHook != nil { - w.resubmitHook(minInterval, recommitInterval) + w.resubmitHook(minRecommit, recommit) } case <-w.exitCh: