Skip to content

Commit b2c644f

Browse files
rjl493456442karalabe
authored andcommitted
cmd, eth, miner: make recommit configurable (#17444)
* cmd, eth, miner: make recommit configurable * cmd, eth, les, miner: polish a bit * miner: filter duplicate sealing work * cmd: remove uncessary conversion * miner: avoid microptimization in favor of cleaner code
1 parent 522cfc6 commit b2c644f

File tree

12 files changed

+360
-71
lines changed

12 files changed

+360
-71
lines changed

cmd/geth/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ var (
106106
utils.MinerLegacyEtherbaseFlag,
107107
utils.MinerExtraDataFlag,
108108
utils.MinerLegacyExtraDataFlag,
109+
utils.MinerRecommitIntervalFlag,
109110
utils.NATFlag,
110111
utils.NoDiscoverFlag,
111112
utils.DiscoveryV5Flag,

cmd/geth/usage.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ var AppHelpFlagGroups = []flagGroup{
190190
utils.MinerGasTargetFlag,
191191
utils.MinerEtherbaseFlag,
192192
utils.MinerExtraDataFlag,
193+
utils.MinerRecommitIntervalFlag,
193194
},
194195
},
195196
{

cmd/utils/flags.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,12 +335,12 @@ var (
335335
MinerGasPriceFlag = BigFlag{
336336
Name: "miner.gasprice",
337337
Usage: "Minimal gas price for mining a transactions",
338-
Value: eth.DefaultConfig.GasPrice,
338+
Value: eth.DefaultConfig.MinerGasPrice,
339339
}
340340
MinerLegacyGasPriceFlag = BigFlag{
341341
Name: "gasprice",
342342
Usage: "Minimal gas price for mining a transactions (deprecated, use --miner.gasprice)",
343-
Value: eth.DefaultConfig.GasPrice,
343+
Value: eth.DefaultConfig.MinerGasPrice,
344344
}
345345
MinerEtherbaseFlag = cli.StringFlag{
346346
Name: "miner.etherbase",
@@ -360,6 +360,11 @@ var (
360360
Name: "extradata",
361361
Usage: "Block extra data set by the miner (default = client version, deprecated, use --miner.extradata)",
362362
}
363+
MinerRecommitIntervalFlag = cli.DurationFlag{
364+
Name: "miner.recommit",
365+
Usage: "Time interval to recreate the block being mined.",
366+
Value: 3 * time.Second,
367+
}
363368
// Account settings
364369
UnlockedAccountFlag = cli.StringFlag{
365370
Name: "unlock",
@@ -1124,16 +1129,19 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
11241129
cfg.DocRoot = ctx.GlobalString(DocRootFlag.Name)
11251130
}
11261131
if ctx.GlobalIsSet(MinerLegacyExtraDataFlag.Name) {
1127-
cfg.ExtraData = []byte(ctx.GlobalString(MinerLegacyExtraDataFlag.Name))
1132+
cfg.MinerExtraData = []byte(ctx.GlobalString(MinerLegacyExtraDataFlag.Name))
11281133
}
11291134
if ctx.GlobalIsSet(MinerExtraDataFlag.Name) {
1130-
cfg.ExtraData = []byte(ctx.GlobalString(MinerExtraDataFlag.Name))
1135+
cfg.MinerExtraData = []byte(ctx.GlobalString(MinerExtraDataFlag.Name))
11311136
}
11321137
if ctx.GlobalIsSet(MinerLegacyGasPriceFlag.Name) {
1133-
cfg.GasPrice = GlobalBig(ctx, MinerLegacyGasPriceFlag.Name)
1138+
cfg.MinerGasPrice = GlobalBig(ctx, MinerLegacyGasPriceFlag.Name)
11341139
}
11351140
if ctx.GlobalIsSet(MinerGasPriceFlag.Name) {
1136-
cfg.GasPrice = GlobalBig(ctx, MinerGasPriceFlag.Name)
1141+
cfg.MinerGasPrice = GlobalBig(ctx, MinerGasPriceFlag.Name)
1142+
}
1143+
if ctx.GlobalIsSet(MinerRecommitIntervalFlag.Name) {
1144+
cfg.MinerRecommit = ctx.Duration(MinerRecommitIntervalFlag.Name)
11371145
}
11381146
if ctx.GlobalIsSet(VMEnableDebugFlag.Name) {
11391147
// TODO(fjl): force-enable this in --dev mode
@@ -1176,7 +1184,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
11761184

11771185
cfg.Genesis = core.DeveloperGenesisBlock(uint64(ctx.GlobalInt(DeveloperPeriodFlag.Name)), developer.Address)
11781186
if !ctx.GlobalIsSet(MinerGasPriceFlag.Name) && !ctx.GlobalIsSet(MinerLegacyGasPriceFlag.Name) {
1179-
cfg.GasPrice = big.NewInt(1)
1187+
cfg.MinerGasPrice = big.NewInt(1)
11801188
}
11811189
}
11821190
// TODO(fjl): move trie cache generations into config

eth/api.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"math/big"
2626
"os"
2727
"strings"
28+
"time"
2829

2930
"github.com/ethereum/go-ethereum/common"
3031
"github.com/ethereum/go-ethereum/common/hexutil"
@@ -160,6 +161,11 @@ func (api *PrivateMinerAPI) SetEtherbase(etherbase common.Address) bool {
160161
return true
161162
}
162163

164+
// SetRecommitInterval updates the interval for miner sealing work recommitting.
165+
func (api *PrivateMinerAPI) SetRecommitInterval(interval int) {
166+
api.e.Miner().SetRecommitInterval(time.Duration(interval) * time.Millisecond)
167+
}
168+
163169
// GetHashrate returns the current hashrate of the miner.
164170
func (api *PrivateMinerAPI) GetHashrate() uint64 {
165171
return api.e.miner.HashRate()

eth/backend.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
127127
engine: CreateConsensusEngine(ctx, chainConfig, &config.Ethash, config.MinerNotify, chainDb),
128128
shutdownChan: make(chan bool),
129129
networkID: config.NetworkId,
130-
gasPrice: config.GasPrice,
130+
gasPrice: config.MinerGasPrice,
131131
etherbase: config.Etherbase,
132132
bloomRequests: make(chan chan *bloombits.Retrieval),
133133
bloomIndexer: NewBloomIndexer(chainDb, params.BloomBitsBlocks, bloomConfirms),
@@ -167,13 +167,13 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
167167
return nil, err
168168
}
169169

170-
eth.miner = miner.New(eth, eth.chainConfig, eth.EventMux(), eth.engine)
171-
eth.miner.SetExtra(makeExtraData(config.ExtraData))
170+
eth.miner = miner.New(eth, eth.chainConfig, eth.EventMux(), eth.engine, config.MinerRecommit)
171+
eth.miner.SetExtra(makeExtraData(config.MinerExtraData))
172172

173173
eth.APIBackend = &EthAPIBackend{eth, nil}
174174
gpoParams := config.GPO
175175
if gpoParams.Default == nil {
176-
gpoParams.Default = config.GasPrice
176+
gpoParams.Default = config.MinerGasPrice
177177
}
178178
eth.APIBackend.gpo = gasprice.NewOracle(eth.APIBackend, gpoParams)
179179

eth/config.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var DefaultConfig = Config{
4848
DatabaseCache: 768,
4949
TrieCache: 256,
5050
TrieTimeout: 60 * time.Minute,
51-
GasPrice: big.NewInt(18 * params.Shannon),
51+
MinerGasPrice: big.NewInt(18 * params.Shannon),
5252

5353
TxPool: core.DefaultTxPoolConfig,
5454
GPO: gasprice.Config{
@@ -95,11 +95,12 @@ type Config struct {
9595
TrieTimeout time.Duration
9696

9797
// Mining-related options
98-
Etherbase common.Address `toml:",omitempty"`
99-
MinerThreads int `toml:",omitempty"`
100-
MinerNotify []string `toml:",omitempty"`
101-
ExtraData []byte `toml:",omitempty"`
102-
GasPrice *big.Int
98+
Etherbase common.Address `toml:",omitempty"`
99+
MinerThreads int `toml:",omitempty"`
100+
MinerNotify []string `toml:",omitempty"`
101+
MinerExtraData []byte `toml:",omitempty"`
102+
MinerGasPrice *big.Int
103+
MinerRecommit time.Duration
103104

104105
// Ethash options
105106
Ethash ethash.Config
@@ -118,5 +119,5 @@ type Config struct {
118119
}
119120

120121
type configMarshaling struct {
121-
ExtraData hexutil.Bytes
122+
MinerExtraData hexutil.Bytes
122123
}

eth/gen_config.go

Lines changed: 43 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/web3ext/web3ext.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,11 @@ web3._extend({
519519
params: 1,
520520
inputFormatter: [web3._extend.utils.fromDecimal]
521521
}),
522+
new web3._extend.Method({
523+
name: 'setRecommitInterval',
524+
call: 'miner_setRecommitInterval',
525+
params: 1,
526+
}),
522527
new web3._extend.Method({
523528
name: 'getHashrate',
524529
call: 'miner_getHashrate'

les/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
141141
leth.ApiBackend = &LesApiBackend{leth, nil}
142142
gpoParams := config.GPO
143143
if gpoParams.Default == nil {
144-
gpoParams.Default = config.GasPrice
144+
gpoParams.Default = config.MinerGasPrice
145145
}
146146
leth.ApiBackend.gpo = gasprice.NewOracle(leth.ApiBackend, gpoParams)
147147
return leth, nil

miner/miner.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package miner
2020
import (
2121
"fmt"
2222
"sync/atomic"
23+
"time"
2324

2425
"github.com/ethereum/go-ethereum/common"
2526
"github.com/ethereum/go-ethereum/consensus"
@@ -51,13 +52,13 @@ type Miner struct {
5152
shouldStart int32 // should start indicates whether we should start after sync
5253
}
5354

54-
func New(eth Backend, config *params.ChainConfig, mux *event.TypeMux, engine consensus.Engine) *Miner {
55+
func New(eth Backend, config *params.ChainConfig, mux *event.TypeMux, engine consensus.Engine, recommit time.Duration) *Miner {
5556
miner := &Miner{
5657
eth: eth,
5758
mux: mux,
5859
engine: engine,
5960
exitCh: make(chan struct{}),
60-
worker: newWorker(config, engine, eth, mux),
61+
worker: newWorker(config, engine, eth, mux, recommit),
6162
canStart: 1,
6263
}
6364
go miner.update()
@@ -144,6 +145,11 @@ func (self *Miner) SetExtra(extra []byte) error {
144145
return nil
145146
}
146147

148+
// SetRecommitInterval sets the interval for sealing work resubmitting.
149+
func (self *Miner) SetRecommitInterval(interval time.Duration) {
150+
self.worker.setRecommitInterval(interval)
151+
}
152+
147153
// Pending returns the currently pending block and associated state.
148154
func (self *Miner) Pending() (*types.Block, *state.StateDB) {
149155
return self.worker.pending()

0 commit comments

Comments
 (0)