Skip to content

Commit d8e0807

Browse files
MariusVanDerWijdenholimankaralabe
authored
miner: refactor the miner, make the pending block on demand (#28623)
* miner: untangle miner * miner: use common.hash instead of *types.header * cmd/geth: deprecate --mine * eth: get rid of most miner api * console: get rid of coinbase in welcome message * miner/stress: get rid of the miner stress test * eth: get rid of miner.setEtherbase * ethstats: remove miner and hashrate flags * ethstats: remove miner and hashrate flags * cmd: rename pendingBlockProducer to miner.pending.feeRecipient flag * miner: use pendingFeeRecipient instead of etherbase * miner: add mutex to protect the pending block * miner: add mutex to protect the pending block * eth: get rid of etherbase mentions * miner: no need to lock the coinbase * eth, miner: fix linter --------- Co-authored-by: Martin Holst Swende <[email protected]> Co-authored-by: Péter Szilágyi <[email protected]>
1 parent 6e379b6 commit d8e0807

35 files changed

+598
-2488
lines changed

cmd/geth/consolecmd_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ func TestConsoleWelcome(t *testing.T) {
7171
Welcome to the Geth JavaScript console!
7272
7373
instance: Geth/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}}
74-
coinbase: {{.Etherbase}}
7574
at block: 0 ({{niltime}})
7675
datadir: {{.Datadir}}
7776
modules: {{apis}}
@@ -131,7 +130,6 @@ func testAttachWelcome(t *testing.T, geth *testgeth, endpoint, apis string) {
131130
attach.SetTemplateFunc("goarch", func() string { return runtime.GOARCH })
132131
attach.SetTemplateFunc("gover", runtime.Version)
133132
attach.SetTemplateFunc("gethver", func() string { return params.VersionWithCommit("", "") })
134-
attach.SetTemplateFunc("etherbase", func() string { return geth.Etherbase })
135133
attach.SetTemplateFunc("niltime", func() string {
136134
return time.Unix(1548854791, 0).Format("Mon Jan 02 2006 15:04:05 GMT-0700 (MST)")
137135
})
@@ -144,7 +142,6 @@ func testAttachWelcome(t *testing.T, geth *testgeth, endpoint, apis string) {
144142
Welcome to the Geth JavaScript console!
145143
146144
instance: Geth/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}}
147-
coinbase: {{etherbase}}
148145
at block: 0 ({{niltime}}){{if ipc}}
149146
datadir: {{datadir}}{{end}}
150147
modules: {{apis}}

cmd/geth/main.go

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"github.com/ethereum/go-ethereum/cmd/utils"
3131
"github.com/ethereum/go-ethereum/common"
3232
"github.com/ethereum/go-ethereum/console/prompt"
33-
"github.com/ethereum/go-ethereum/eth"
3433
"github.com/ethereum/go-ethereum/eth/downloader"
3534
"github.com/ethereum/go-ethereum/ethclient"
3635
"github.com/ethereum/go-ethereum/internal/debug"
@@ -116,13 +115,14 @@ var (
116115
utils.DiscoveryPortFlag,
117116
utils.MaxPeersFlag,
118117
utils.MaxPendingPeersFlag,
119-
utils.MiningEnabledFlag,
118+
utils.MiningEnabledFlag, // deprecated
120119
utils.MinerGasLimitFlag,
121120
utils.MinerGasPriceFlag,
122-
utils.MinerEtherbaseFlag,
121+
utils.MinerEtherbaseFlag, // deprecated
123122
utils.MinerExtraDataFlag,
124123
utils.MinerRecommitIntervalFlag,
125-
utils.MinerNewPayloadTimeout,
124+
utils.MinerPendingFeeRecipientFlag,
125+
utils.MinerNewPayloadTimeoutFlag, // deprecated
126126
utils.NATFlag,
127127
utils.NoDiscoverFlag,
128128
utils.DiscoveryV4Flag,
@@ -421,24 +421,6 @@ func startNode(ctx *cli.Context, stack *node.Node, backend ethapi.Backend, isCon
421421
}
422422
}()
423423
}
424-
425-
// Start auxiliary services if enabled
426-
if ctx.Bool(utils.MiningEnabledFlag.Name) {
427-
// Mining only makes sense if a full Ethereum node is running
428-
if ctx.String(utils.SyncModeFlag.Name) == "light" {
429-
utils.Fatalf("Light clients do not support mining")
430-
}
431-
ethBackend, ok := backend.(*eth.EthAPIBackend)
432-
if !ok {
433-
utils.Fatalf("Ethereum service not running")
434-
}
435-
// Set the gas price to the limits from the CLI and start mining
436-
gasprice := flags.GlobalBig(ctx, utils.MinerGasPriceFlag.Name)
437-
ethBackend.TxPool().SetGasTip(gasprice)
438-
if err := ethBackend.StartMining(); err != nil {
439-
utils.Fatalf("Failed to start mining: %v", err)
440-
}
441-
}
442424
}
443425

444426
// unlockAccounts unlocks any account specifically requested.

cmd/utils/flags.go

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -425,11 +425,6 @@ var (
425425
}
426426

427427
// Miner settings
428-
MiningEnabledFlag = &cli.BoolFlag{
429-
Name: "mine",
430-
Usage: "Enable mining",
431-
Category: flags.MinerCategory,
432-
}
433428
MinerGasLimitFlag = &cli.Uint64Flag{
434429
Name: "miner.gaslimit",
435430
Usage: "Target gas ceiling for mined blocks",
@@ -442,11 +437,6 @@ var (
442437
Value: ethconfig.Defaults.Miner.GasPrice,
443438
Category: flags.MinerCategory,
444439
}
445-
MinerEtherbaseFlag = &cli.StringFlag{
446-
Name: "miner.etherbase",
447-
Usage: "0x prefixed public address for block mining rewards",
448-
Category: flags.MinerCategory,
449-
}
450440
MinerExtraDataFlag = &cli.StringFlag{
451441
Name: "miner.extradata",
452442
Usage: "Block extra data set by the miner (default = client version)",
@@ -458,10 +448,9 @@ var (
458448
Value: ethconfig.Defaults.Miner.Recommit,
459449
Category: flags.MinerCategory,
460450
}
461-
MinerNewPayloadTimeout = &cli.DurationFlag{
462-
Name: "miner.newpayload-timeout",
463-
Usage: "Specify the maximum time allowance for creating a new payload",
464-
Value: ethconfig.Defaults.Miner.NewPayloadTimeout,
451+
MinerPendingFeeRecipientFlag = &cli.StringFlag{
452+
Name: "miner.pending.feeRecipient",
453+
Usage: "0x prefixed public address for the pending block producer (not used for actual block production)",
465454
Category: flags.MinerCategory,
466455
}
467456

@@ -1268,19 +1257,23 @@ func MakeAddress(ks *keystore.KeyStore, account string) (accounts.Account, error
12681257

12691258
// setEtherbase retrieves the etherbase from the directly specified command line flags.
12701259
func setEtherbase(ctx *cli.Context, cfg *ethconfig.Config) {
1271-
if !ctx.IsSet(MinerEtherbaseFlag.Name) {
1260+
if ctx.IsSet(MinerEtherbaseFlag.Name) {
1261+
log.Warn("Option --miner.etherbase is deprecated as the etherbase is set by the consensus client post-merge")
12721262
return
12731263
}
1274-
addr := ctx.String(MinerEtherbaseFlag.Name)
1264+
if !ctx.IsSet(MinerPendingFeeRecipientFlag.Name) {
1265+
return
1266+
}
1267+
addr := ctx.String(MinerPendingFeeRecipientFlag.Name)
12751268
if strings.HasPrefix(addr, "0x") || strings.HasPrefix(addr, "0X") {
12761269
addr = addr[2:]
12771270
}
12781271
b, err := hex.DecodeString(addr)
12791272
if err != nil || len(b) != common.AddressLength {
1280-
Fatalf("-%s: invalid etherbase address %q", MinerEtherbaseFlag.Name, addr)
1273+
Fatalf("-%s: invalid pending block producer address %q", MinerPendingFeeRecipientFlag.Name, addr)
12811274
return
12821275
}
1283-
cfg.Miner.Etherbase = common.BytesToAddress(b)
1276+
cfg.Miner.PendingFeeRecipient = common.BytesToAddress(b)
12841277
}
12851278

12861279
// MakePasswordList reads password lines from the file specified by the global --password flag.
@@ -1496,6 +1489,9 @@ func setTxPool(ctx *cli.Context, cfg *legacypool.Config) {
14961489
}
14971490

14981491
func setMiner(ctx *cli.Context, cfg *miner.Config) {
1492+
if ctx.Bool(MiningEnabledFlag.Name) {
1493+
log.Warn("The flag --mine is deprecated and will be removed")
1494+
}
14991495
if ctx.IsSet(MinerExtraDataFlag.Name) {
15001496
cfg.ExtraData = []byte(ctx.String(MinerExtraDataFlag.Name))
15011497
}
@@ -1508,8 +1504,9 @@ func setMiner(ctx *cli.Context, cfg *miner.Config) {
15081504
if ctx.IsSet(MinerRecommitIntervalFlag.Name) {
15091505
cfg.Recommit = ctx.Duration(MinerRecommitIntervalFlag.Name)
15101506
}
1511-
if ctx.IsSet(MinerNewPayloadTimeout.Name) {
1512-
cfg.NewPayloadTimeout = ctx.Duration(MinerNewPayloadTimeout.Name)
1507+
if ctx.IsSet(MinerNewPayloadTimeoutFlag.Name) {
1508+
log.Warn("The flag --miner.newpayload-timeout is deprecated and will be removed, please use --miner.recommit")
1509+
cfg.Recommit = ctx.Duration(MinerNewPayloadTimeoutFlag.Name)
15131510
}
15141511
}
15151512

@@ -1786,8 +1783,8 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
17861783

17871784
// Figure out the dev account address.
17881785
// setEtherbase has been called above, configuring the miner address from command line flags.
1789-
if cfg.Miner.Etherbase != (common.Address{}) {
1790-
developer = accounts.Account{Address: cfg.Miner.Etherbase}
1786+
if cfg.Miner.PendingFeeRecipient != (common.Address{}) {
1787+
developer = accounts.Account{Address: cfg.Miner.PendingFeeRecipient}
17911788
} else if accs := ks.Accounts(); len(accs) > 0 {
17921789
developer = ks.Accounts()[0]
17931790
} else {
@@ -1798,7 +1795,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
17981795
}
17991796
// Make sure the address is configured as fee recipient, otherwise
18001797
// the miner will fail to start.
1801-
cfg.Miner.Etherbase = developer.Address
1798+
cfg.Miner.PendingFeeRecipient = developer.Address
18021799

18031800
if err := ks.Unlock(developer, passphrase); err != nil {
18041801
Fatalf("Failed to unlock developer account: %v", err)

cmd/utils/flags_legacy.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ var DeprecatedFlags = []cli.Flag{
4747
LightNoSyncServeFlag,
4848
LogBacktraceAtFlag,
4949
LogDebugFlag,
50+
MinerNewPayloadTimeoutFlag,
51+
MinerEtherbaseFlag,
52+
MiningEnabledFlag,
5053
}
5154

5255
var (
@@ -132,6 +135,23 @@ var (
132135
Usage: "Prepends log messages with call-site location (deprecated)",
133136
Category: flags.DeprecatedCategory,
134137
}
138+
// Deprecated February 2024
139+
MinerNewPayloadTimeoutFlag = &cli.DurationFlag{
140+
Name: "miner.newpayload-timeout",
141+
Usage: "Specify the maximum time allowance for creating a new payload",
142+
Value: ethconfig.Defaults.Miner.Recommit,
143+
Category: flags.MinerCategory,
144+
}
145+
MinerEtherbaseFlag = &cli.StringFlag{
146+
Name: "miner.etherbase",
147+
Usage: "0x prefixed public address for block mining rewards",
148+
Category: flags.MinerCategory,
149+
}
150+
MiningEnabledFlag = &cli.BoolFlag{
151+
Name: "mine",
152+
Usage: "Enable mining",
153+
Category: flags.MinerCategory,
154+
}
135155
)
136156

137157
// showDeprecated displays deprecated flags that will be soon removed from the codebase.

consensus/consensus.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,3 @@ type Engine interface {
119119
// Close terminates any background threads maintained by the consensus engine.
120120
Close() error
121121
}
122-
123-
// PoW is a consensus engine based on proof-of-work.
124-
type PoW interface {
125-
Engine
126-
127-
// Hashrate returns the current mining hashrate of a PoW consensus engine.
128-
Hashrate() float64
129-
}

console/console.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,6 @@ func (c *Console) Welcome() {
325325
// Print some generic Geth metadata
326326
if res, err := c.jsre.Run(`
327327
var message = "instance: " + web3.version.node + "\n";
328-
try {
329-
message += "coinbase: " + eth.coinbase + "\n";
330-
} catch (err) {}
331328
message += "at block: " + eth.blockNumber + " (" + new Date(1000 * eth.getBlock(eth.blockNumber).timestamp) + ")\n";
332329
try {
333330
message += " datadir: " + admin.datadir + "\n";

console/console_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func newTester(t *testing.T, confOverride func(*ethconfig.Config)) *tester {
9696
ethConf := &ethconfig.Config{
9797
Genesis: core.DeveloperGenesisBlock(11_500_000, nil),
9898
Miner: miner.Config{
99-
Etherbase: common.HexToAddress(testAddress),
99+
PendingFeeRecipient: common.HexToAddress(testAddress),
100100
},
101101
}
102102
if confOverride != nil {
@@ -167,9 +167,6 @@ func TestWelcome(t *testing.T) {
167167
if want := fmt.Sprintf("instance: %s", testInstance); !strings.Contains(output, want) {
168168
t.Fatalf("console output missing instance: have\n%s\nwant also %s", output, want)
169169
}
170-
if want := fmt.Sprintf("coinbase: %s", testAddress); !strings.Contains(output, want) {
171-
t.Fatalf("console output missing coinbase: have\n%s\nwant also %s", output, want)
172-
}
173170
if want := "at block: 0"; !strings.Contains(output, want) {
174171
t.Fatalf("console output missing sync status: have\n%s\nwant also %s", output, want)
175172
}

eth/api.go

Lines changed: 0 additions & 52 deletions
This file was deleted.

eth/api_backend.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"github.com/ethereum/go-ethereum/eth/tracers"
3838
"github.com/ethereum/go-ethereum/ethdb"
3939
"github.com/ethereum/go-ethereum/event"
40-
"github.com/ethereum/go-ethereum/miner"
4140
"github.com/ethereum/go-ethereum/params"
4241
"github.com/ethereum/go-ethereum/rpc"
4342
)
@@ -67,7 +66,7 @@ func (b *EthAPIBackend) SetHead(number uint64) {
6766
func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
6867
// Pending block is only known by the miner
6968
if number == rpc.PendingBlockNumber {
70-
block := b.eth.miner.PendingBlock()
69+
block, _, _ := b.eth.miner.Pending()
7170
if block == nil {
7271
return nil, errors.New("pending block is not available")
7372
}
@@ -118,7 +117,7 @@ func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*ty
118117
func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
119118
// Pending block is only known by the miner
120119
if number == rpc.PendingBlockNumber {
121-
block := b.eth.miner.PendingBlock()
120+
block, _, _ := b.eth.miner.Pending()
122121
if block == nil {
123122
return nil, errors.New("pending block is not available")
124123
}
@@ -182,14 +181,14 @@ func (b *EthAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash r
182181
return nil, errors.New("invalid arguments; neither block nor hash specified")
183182
}
184183

185-
func (b *EthAPIBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) {
186-
return b.eth.miner.PendingBlockAndReceipts()
184+
func (b *EthAPIBackend) Pending() (*types.Block, types.Receipts, *state.StateDB) {
185+
return b.eth.miner.Pending()
187186
}
188187

189188
func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
190189
// Pending state is only known by the miner
191190
if number == rpc.PendingBlockNumber {
192-
block, state := b.eth.miner.Pending()
191+
block, _, state := b.eth.miner.Pending()
193192
if block == nil || state == nil {
194193
return nil, nil, errors.New("pending state is not available")
195194
}
@@ -267,10 +266,6 @@ func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEven
267266
return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch)
268267
}
269268

270-
func (b *EthAPIBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription {
271-
return b.eth.miner.SubscribePendingLogs(ch)
272-
}
273-
274269
func (b *EthAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
275270
return b.eth.BlockChain().SubscribeChainEvent(ch)
276271
}
@@ -421,14 +416,6 @@ func (b *EthAPIBackend) CurrentHeader() *types.Header {
421416
return b.eth.blockchain.CurrentHeader()
422417
}
423418

424-
func (b *EthAPIBackend) Miner() *miner.Miner {
425-
return b.eth.Miner()
426-
}
427-
428-
func (b *EthAPIBackend) StartMining() error {
429-
return b.eth.StartMining()
430-
}
431-
432419
func (b *EthAPIBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, readOnly bool, preferDisk bool) (*state.StateDB, tracers.StateReleaseFunc, error) {
433420
return b.eth.stateAtBlock(ctx, block, reexec, base, readOnly, preferDisk)
434421
}

eth/api_debug.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (api *DebugAPI) DumpBlock(blockNr rpc.BlockNumber) (state.Dump, error) {
5656
// If we're dumping the pending state, we need to request
5757
// both the pending block as well as the pending state from
5858
// the miner and operate on those
59-
_, stateDb := api.eth.miner.Pending()
59+
_, _, stateDb := api.eth.miner.Pending()
6060
if stateDb == nil {
6161
return state.Dump{}, errors.New("pending state is not available")
6262
}
@@ -142,7 +142,7 @@ func (api *DebugAPI) AccountRange(blockNrOrHash rpc.BlockNumberOrHash, start hex
142142
// If we're dumping the pending state, we need to request
143143
// both the pending block as well as the pending state from
144144
// the miner and operate on those
145-
_, stateDb = api.eth.miner.Pending()
145+
_, _, stateDb = api.eth.miner.Pending()
146146
if stateDb == nil {
147147
return state.Dump{}, errors.New("pending state is not available")
148148
}

0 commit comments

Comments
 (0)