Skip to content

Commit d09c25c

Browse files
authored
Miner: refactor miner, make pending block on demand (#100)
* refactor miner * format
1 parent c01dd2e commit d09c25c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1386
-4294
lines changed

accounts/abi/bind/backends/simulated.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -939,8 +939,8 @@ func (fb *filterBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*t
939939
return fb.bc.GetHeaderByHash(hash), nil
940940
}
941941

942-
func (fb *filterBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) {
943-
return fb.backend.pendingBlock, fb.backend.pendingReceipts
942+
func (fb *filterBackend) Pending() (*types.Block, types.Receipts, *state.StateDB) {
943+
return fb.backend.pendingBlock, fb.backend.pendingReceipts, fb.backend.pendingState
944944
}
945945

946946
func (fb *filterBackend) StateAt(root common.Hash) (*state.StateDB, error) {
@@ -976,10 +976,6 @@ func (fb *filterBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscr
976976
return fb.bc.SubscribeLogsEvent(ch)
977977
}
978978

979-
func (fb *filterBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription {
980-
return nullSubscription()
981-
}
982-
983979
func (fb *filterBackend) BloomStatus() (uint64, uint64) { return 4096, 0 }
984980

985981
func (fb *filterBackend) ServiceFilter(ctx context.Context, ms *bloombits.MatcherSession) {

cmd/geth/consolecmd_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ func TestConsoleWelcome(t *testing.T) {
7070
Welcome to the Geth JavaScript console!
7171
7272
instance: Geth/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}}
73-
coinbase: {{.Etherbase}}
7473
at block: 0 ({{niltime}})
7574
datadir: {{.Datadir}}
7675
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(0, 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: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"github.com/scroll-tech/go-ethereum/cmd/utils"
3131
"github.com/scroll-tech/go-ethereum/common"
3232
"github.com/scroll-tech/go-ethereum/console/prompt"
33-
"github.com/scroll-tech/go-ethereum/eth"
3433
"github.com/scroll-tech/go-ethereum/eth/downloader"
3534
"github.com/scroll-tech/go-ethereum/ethclient"
3635
"github.com/scroll-tech/go-ethereum/internal/debug"
@@ -122,16 +121,14 @@ var (
122121
utils.ListenPortFlag,
123122
utils.MaxPeersFlag,
124123
utils.MaxPendingPeersFlag,
125-
utils.MiningEnabledFlag,
126-
utils.MinerThreadsFlag,
127-
utils.MinerNotifyFlag,
124+
utils.MiningEnabledFlag, // deprecated
128125
utils.LegacyMinerGasTargetFlag,
129126
utils.MinerGasLimitFlag,
130127
utils.MinerGasPriceFlag,
131-
utils.MinerEtherbaseFlag,
128+
utils.MinerEtherbaseFlag, // deprecated
132129
utils.MinerExtraDataFlag,
130+
utils.MinerPendingFeeRecipientFlag,
133131
utils.MinerRecommitIntervalFlag,
134-
utils.MinerNoVerifyFlag,
135132
utils.MinerStoreSkippedTxTracesFlag,
136133
utils.MinerMaxAccountsNumFlag,
137134
utils.NATFlag,
@@ -162,7 +159,6 @@ var (
162159
utils.GpoPercentileFlag,
163160
utils.GpoMaxGasPriceFlag,
164161
utils.GpoIgnoreGasPriceFlag,
165-
utils.MinerNotifyFullFlag,
166162
configFileFlag,
167163
utils.CatalystFlag,
168164
utils.CircuitCapacityCheckEnabledFlag,
@@ -427,24 +423,24 @@ func startNode(ctx *cli.Context, stack *node.Node, backend ethapi.Backend) {
427423
}
428424

429425
// Start auxiliary services if enabled
430-
if ctx.GlobalBool(utils.MiningEnabledFlag.Name) || ctx.GlobalBool(utils.DeveloperFlag.Name) {
431-
// Mining only makes sense if a full Ethereum node is running
432-
if ctx.GlobalString(utils.SyncModeFlag.Name) == "light" {
433-
utils.Fatalf("Light clients do not support mining")
434-
}
435-
ethBackend, ok := backend.(*eth.EthAPIBackend)
436-
if !ok {
437-
utils.Fatalf("Ethereum service not running")
438-
}
439-
// Set the gas price to the limits from the CLI and start mining
440-
gasprice := utils.GlobalBig(ctx, utils.MinerGasPriceFlag.Name)
441-
ethBackend.TxPool().SetGasPrice(gasprice)
442-
// start mining
443-
threads := ctx.GlobalInt(utils.MinerThreadsFlag.Name)
444-
if err := ethBackend.StartMining(threads); err != nil {
445-
utils.Fatalf("Failed to start mining: %v", err)
446-
}
447-
}
426+
//if ctx.GlobalBool(utils.MiningEnabledFlag.Name) || ctx.GlobalBool(utils.DeveloperFlag.Name) {
427+
// // Mining only makes sense if a full Ethereum node is running
428+
// if ctx.GlobalString(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 := utils.GlobalBig(ctx, utils.MinerGasPriceFlag.Name)
437+
// ethBackend.TxPool().SetGasPrice(gasprice)
438+
// // start mining
439+
// threads := ctx.GlobalInt(utils.MinerThreadsFlag.Name)
440+
// if err := ethBackend.StartMining(threads); err != nil {
441+
// utils.Fatalf("Failed to start mining: %v", err)
442+
// }
443+
//}
448444
}
449445

450446
// unlockAccounts unlocks any account specifically requested.

cmd/geth/usage.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,11 @@ var AppHelpFlagGroups = []flags.FlagGroup{
186186
Flags: []cli.Flag{
187187
utils.MiningEnabledFlag,
188188
utils.MinerThreadsFlag,
189-
utils.MinerNotifyFlag,
190-
utils.MinerNotifyFullFlag,
191189
utils.MinerGasPriceFlag,
192190
utils.MinerGasLimitFlag,
193191
utils.MinerEtherbaseFlag,
194192
utils.MinerExtraDataFlag,
195193
utils.MinerRecommitIntervalFlag,
196-
utils.MinerNoVerifyFlag,
197194
utils.MinerStoreSkippedTxTracesFlag,
198195
utils.MinerMaxAccountsNumFlag,
199196
},

cmd/utils/flags.go

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package utils
1919

2020
import (
2121
"crypto/ecdsa"
22+
"encoding/hex"
2223
"fmt"
2324
"io"
2425
"io/ioutil"
@@ -464,22 +465,15 @@ var (
464465
}
465466

466467
// Miner settings
467-
MiningEnabledFlag = cli.BoolFlag{
468-
Name: "mine",
469-
Usage: "Enable mining",
470-
}
471468
MinerThreadsFlag = cli.IntFlag{
472469
Name: "miner.threads",
473470
Usage: "Number of CPU threads to use for mining",
474471
Value: 0,
475472
}
476-
MinerNotifyFlag = cli.StringFlag{
477-
Name: "miner.notify",
478-
Usage: "Comma separated HTTP URL list to notify of new work packages",
479-
}
480-
MinerNotifyFullFlag = cli.BoolFlag{
481-
Name: "miner.notify.full",
482-
Usage: "Notify with pending block headers instead of work packages",
473+
MinerPendingFeeRecipientFlag = &cli.StringFlag{
474+
Name: "miner.pending.feeRecipient",
475+
Usage: "0x prefixed public address for the pending block producer (not used for actual block production)",
476+
Value: "0",
483477
}
484478
MinerGasLimitFlag = cli.Uint64Flag{
485479
Name: "miner.gaslimit",
@@ -491,11 +485,6 @@ var (
491485
Usage: "Minimum gas price for mining a transaction",
492486
Value: ethconfig.Defaults.Miner.GasPrice,
493487
}
494-
MinerEtherbaseFlag = cli.StringFlag{
495-
Name: "miner.etherbase",
496-
Usage: "Public address for block mining rewards (default = first account)",
497-
Value: "0",
498-
}
499488
MinerExtraDataFlag = cli.StringFlag{
500489
Name: "miner.extradata",
501490
Usage: "Block extra data set by the miner (default = client version)",
@@ -505,10 +494,6 @@ var (
505494
Usage: "Time interval to recreate the block being mined",
506495
Value: ethconfig.Defaults.Miner.Recommit,
507496
}
508-
MinerNoVerifyFlag = cli.BoolFlag{
509-
Name: "miner.noverify",
510-
Usage: "Disable remote sealing verification",
511-
}
512497

513498
MinerNewBlockTimeout = &cli.DurationFlag{
514499
Name: "miner.newblock-timeout",
@@ -1235,22 +1220,23 @@ func MakeAddress(ks *keystore.KeyStore, account string) (accounts.Account, error
12351220
// command line flags or from the keystore if CLI indexed.
12361221
func setEtherbase(ctx *cli.Context, ks *keystore.KeyStore, cfg *ethconfig.Config) {
12371222
// Extract the current etherbase
1238-
var etherbase string
1239-
if ctx.GlobalIsSet(MinerEtherbaseFlag.Name) {
1240-
etherbase = ctx.GlobalString(MinerEtherbaseFlag.Name)
1241-
}
1242-
// Convert the etherbase into an address and configure it
1243-
if etherbase != "" {
1244-
if ks != nil {
1245-
account, err := MakeAddress(ks, etherbase)
1246-
if err != nil {
1247-
Fatalf("Invalid miner etherbase: %v", err)
1248-
}
1249-
cfg.Miner.Etherbase = account.Address
1250-
} else {
1251-
Fatalf("No etherbase configured")
1252-
}
1223+
if ctx.IsSet(MinerEtherbaseFlag.Name) {
1224+
log.Warn("Option --miner.etherbase is deprecated")
1225+
return
12531226
}
1227+
if !ctx.IsSet(MinerPendingFeeRecipientFlag.Name) {
1228+
return
1229+
}
1230+
addr := ctx.String(MinerPendingFeeRecipientFlag.Name)
1231+
if strings.HasPrefix(addr, "0x") || strings.HasPrefix(addr, "0X") {
1232+
addr = addr[2:]
1233+
}
1234+
b, err := hex.DecodeString(addr)
1235+
if err != nil || len(b) != common.AddressLength {
1236+
Fatalf("-%s: invalid pending block producer address %q", MinerPendingFeeRecipientFlag.Name, addr)
1237+
return
1238+
}
1239+
cfg.Miner.PendingFeeRecipient = common.BytesToAddress(b)
12541240
}
12551241

12561242
// MakePasswordList reads password lines from the file specified by the global --password flag.
@@ -1530,10 +1516,9 @@ func setEthash(ctx *cli.Context, cfg *ethconfig.Config) {
15301516
}
15311517

15321518
func setMiner(ctx *cli.Context, cfg *miner.Config) {
1533-
if ctx.GlobalIsSet(MinerNotifyFlag.Name) {
1534-
cfg.Notify = strings.Split(ctx.GlobalString(MinerNotifyFlag.Name), ",")
1519+
if ctx.Bool(MiningEnabledFlag.Name) {
1520+
log.Warn("The flag --mine is deprecated and will be removed")
15351521
}
1536-
cfg.NotifyFull = ctx.GlobalBool(MinerNotifyFullFlag.Name)
15371522
if ctx.GlobalIsSet(MinerExtraDataFlag.Name) {
15381523
cfg.ExtraData = []byte(ctx.GlobalString(MinerExtraDataFlag.Name))
15391524
}
@@ -1546,9 +1531,6 @@ func setMiner(ctx *cli.Context, cfg *miner.Config) {
15461531
if ctx.GlobalIsSet(MinerRecommitIntervalFlag.Name) {
15471532
cfg.Recommit = ctx.GlobalDuration(MinerRecommitIntervalFlag.Name)
15481533
}
1549-
if ctx.GlobalIsSet(MinerNoVerifyFlag.Name) {
1550-
cfg.Noverify = ctx.GlobalBool(MinerNoVerifyFlag.Name)
1551-
}
15521534
if ctx.GlobalIsSet(MinerStoreSkippedTxTracesFlag.Name) {
15531535
cfg.StoreSkippedTxTraces = ctx.GlobalBool(MinerStoreSkippedTxTracesFlag.Name)
15541536
}
@@ -1878,9 +1860,18 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
18781860
// when we're definitely concerned with only one account.
18791861
passphrase = list[0]
18801862
}
1863+
// Unlock the developer account by local keystore.
1864+
var ks *keystore.KeyStore
1865+
if keystores := stack.AccountManager().Backends(keystore.KeyStoreType); len(keystores) > 0 {
1866+
ks = keystores[0].(*keystore.KeyStore)
1867+
}
1868+
if ks == nil {
1869+
Fatalf("Keystore is not available")
1870+
}
1871+
18811872
// setEtherbase has been called above, configuring the miner address from command line flags.
1882-
if cfg.Miner.Etherbase != (common.Address{}) {
1883-
developer = accounts.Account{Address: cfg.Miner.Etherbase}
1873+
if cfg.Miner.PendingFeeRecipient != (common.Address{}) {
1874+
developer = accounts.Account{Address: cfg.Miner.PendingFeeRecipient}
18841875
} else if accs := ks.Accounts(); len(accs) > 0 {
18851876
developer = ks.Accounts()[0]
18861877
} else {
@@ -1889,6 +1880,10 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
18891880
Fatalf("Failed to create developer account: %v", err)
18901881
}
18911882
}
1883+
// Make sure the address is configured as fee recipient, otherwise
1884+
// the miner will fail to start.
1885+
cfg.Miner.PendingFeeRecipient = developer.Address
1886+
18921887
if err := ks.Unlock(developer, passphrase); err != nil {
18931888
Fatalf("Failed to unlock developer account: %v", err)
18941889
}

cmd/utils/flags_legacy.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ var (
5050
Usage: "Target gas floor for mined blocks (deprecated)",
5151
Value: ethconfig.Defaults.Miner.GasFloor,
5252
}
53+
54+
MinerEtherbaseFlag = cli.StringFlag{
55+
Name: "miner.etherbase",
56+
Usage: "Public address for block mining rewards (default = first account)",
57+
Value: "0",
58+
}
59+
MiningEnabledFlag = &cli.BoolFlag{
60+
Name: "mine",
61+
Usage: "Enable mining",
62+
}
5363
)
5464

5565
// 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
@@ -117,11 +117,3 @@ type Engine interface {
117117
// Close terminates any background threads maintained by the consensus engine.
118118
Close() error
119119
}
120-
121-
// PoW is a consensus engine based on proof-of-work.
122-
type PoW interface {
123-
Engine
124-
125-
// Hashrate returns the current mining hashrate of a PoW consensus engine.
126-
Hashrate() float64
127-
}

console/console.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,6 @@ func (c *Console) Welcome() {
305305
// Print some generic Geth metadata
306306
if res, err := c.jsre.Run(`
307307
var message = "instance: " + web3.version.node + "\n";
308-
try {
309-
message += "coinbase: " + eth.coinbase + "\n";
310-
} catch (err) {}
311308
message += "at block: " + eth.blockNumber + " (" + new Date(1000 * eth.getBlock(eth.blockNumber).timestamp) + ")\n";
312309
try {
313310
message += " datadir: " + admin.datadir + "\n";

console/console_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func newTester(t *testing.T, confOverride func(*ethconfig.Config)) *tester {
101101
ethConf := &ethconfig.Config{
102102
Genesis: core.DeveloperGenesisBlock(15, 11_500_000, common.Address{}),
103103
Miner: miner.Config{
104-
Etherbase: common.HexToAddress(testAddress),
104+
PendingFeeRecipient: common.HexToAddress(testAddress),
105105
},
106106
Ethash: ethash.Config{
107107
PowMode: ethash.ModeTest,
@@ -174,9 +174,6 @@ func TestWelcome(t *testing.T) {
174174
if want := fmt.Sprintf("instance: %s", testInstance); !strings.Contains(output, want) {
175175
t.Fatalf("console output missing instance: have\n%s\nwant also %s", output, want)
176176
}
177-
if want := fmt.Sprintf("coinbase: %s", testAddress); !strings.Contains(output, want) {
178-
t.Fatalf("console output missing coinbase: have\n%s\nwant also %s", output, want)
179-
}
180177
if want := "at block: 0"; !strings.Contains(output, want) {
181178
t.Fatalf("console output missing sync status: have\n%s\nwant also %s", output, want)
182179
}

core/blockchain.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,13 @@ type BlockChain struct {
216216
processor Processor // Block transaction processor interface
217217
vmConfig vm.Config
218218

219-
shouldPreserve func(*types.Block) bool // Function used to determine whether should preserve the given block.
219+
shouldPreserve func(*types.Header) bool // Function used to determine whether should preserve the given block.
220220
}
221221

222222
// NewBlockChain returns a fully initialised block chain using information
223223
// available in the database. It initialises the default Ethereum Validator and
224224
// Processor.
225-
func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *params.ChainConfig, engine consensus.Engine, vmConfig vm.Config, shouldPreserve func(block *types.Block) bool, txLookupLimit *uint64) (*BlockChain, error) {
225+
func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *params.ChainConfig, engine consensus.Engine, vmConfig vm.Config, shouldPreserve func(block *types.Header) bool, txLookupLimit *uint64) (*BlockChain, error) {
226226
if cacheConfig == nil {
227227
cacheConfig = defaultCacheConfig
228228
}

0 commit comments

Comments
 (0)