Skip to content

Commit

Permalink
miner, cmd, eth: revert require explicit etherbase address (ethereum#…
Browse files Browse the repository at this point in the history
…26413) / commit 2b44ef5
  • Loading branch information
jakub-freebit committed Nov 6, 2023
1 parent f3ec700 commit 8eba19c
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 58 deletions.
4 changes: 3 additions & 1 deletion cmd/geth/les_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ func startLightServer(t *testing.T) *gethrpc {
t.Logf("Importing keys to geth")
runGeth(t, "account", "import", "--datadir", datadir, "--password", "./testdata/password.txt", "--lightkdf", "./testdata/key.prv").WaitExit()
account := "0x02f0d131f1f97aef08aec6e3291b957d9efe7105"
server := startGethWithIpc(t, "lightserver", "--allow-insecure-unlock", "--datadir", datadir, "--password", "./testdata/password.txt", "--unlock", account, "--miner.etherbase=0x02f0d131f1f97aef08aec6e3291b957d9efe7105", "--mine", "--light.serve=100", "--light.maxpeers=1", "--nodiscover", "--nat=extip:127.0.0.1", "--verbosity=4")
// MODIFIED by Jakub Pajek (revert require explicit etherbase address)
//server := startGethWithIpc(t, "lightserver", "--allow-insecure-unlock", "--datadir", datadir, "--password", "./testdata/password.txt", "--unlock", account, "--miner.etherbase=0x02f0d131f1f97aef08aec6e3291b957d9efe7105", "--mine", "--light.serve=100", "--light.maxpeers=1", "--nodiscover", "--nat=extip:127.0.0.1", "--verbosity=4")
server := startGethWithIpc(t, "lightserver", "--allow-insecure-unlock", "--datadir", datadir, "--password", "./testdata/password.txt", "--unlock", account, "--mine", "--light.serve=100", "--light.maxpeers=1", "--nodiscover", "--nat=extip:127.0.0.1", "--verbosity=4")
return server
}

Expand Down
76 changes: 60 additions & 16 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"bytes"
"context"
"crypto/ecdsa"
"encoding/hex"
"errors"
"fmt"
"math"
Expand Down Expand Up @@ -539,8 +538,12 @@ var (
Category: flags.MinerCategory,
}
MinerEtherbaseFlag = &cli.StringFlag{
Name: "miner.etherbase",
Usage: "0x prefixed public address for block mining rewards",
Name: "miner.etherbase",
// MODIFIED by Jakub Pajek BEG (revert require explicit etherbase address)
//Usage: "0x prefixed public address for block mining rewards",
Usage: "Public address for block mining rewards (default = first account)",
Value: "0",
// MODIFIED by Jakub Pajek END (revert require explicit etherbase address)
Category: flags.MinerCategory,
}
MinerExtraDataFlag = &cli.StringFlag{
Expand Down Expand Up @@ -1347,6 +1350,8 @@ func MakeAddress(ks *keystore.KeyStore, account string) (accounts.Account, error
return accs[index], nil
}

// MODIFIED by Jakub Pajek BEG (revert require explicit etherbase address)
/*
// setEtherbase retrieves the etherbase from the directly specified command line flags.
func setEtherbase(ctx *cli.Context, cfg *ethconfig.Config) {
if !ctx.IsSet(MinerEtherbaseFlag.Name) {
Expand All @@ -1363,6 +1368,31 @@ func setEtherbase(ctx *cli.Context, cfg *ethconfig.Config) {
}
cfg.Miner.Etherbase = common.BytesToAddress(b)
}
*/

// setEtherbase retrieves the etherbase either from the directly specified
// command line flags or from the keystore if CLI indexed.
func setEtherbase(ctx *cli.Context, ks *keystore.KeyStore, cfg *ethconfig.Config) {
// Extract the current etherbase
var etherbase string
if ctx.IsSet(MinerEtherbaseFlag.Name) {
etherbase = ctx.String(MinerEtherbaseFlag.Name)
}
// Convert the etherbase into an address and configure it
if etherbase != "" {
if ks != nil {
account, err := MakeAddress(ks, etherbase)
if err != nil {
Fatalf("Invalid miner etherbase: %v", err)
}
cfg.Miner.Etherbase = account.Address
} else {
Fatalf("No etherbase configured")
}
}
}

// MODIFIED by Jakub Pajek END (revert require explicit etherbase address)

// MakePasswordList reads password lines from the file specified by the global --password flag.
func MakePasswordList(ctx *cli.Context) []string {
Expand Down Expand Up @@ -1738,7 +1768,14 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
if ctx.IsSet(LightServeFlag.Name) && ctx.Uint64(TxLookupLimitFlag.Name) != 0 {
log.Warn("LES server cannot serve old transaction status and cannot connect below les/4 protocol version if transaction lookup index is limited")
}
setEtherbase(ctx, cfg)
// MODIFIED by Jakub Pajek BEG (revert require explicit etherbase address)
//setEtherbase(ctx, cfg)
var ks *keystore.KeyStore
if keystores := stack.AccountManager().Backends(keystore.KeyStoreType); len(keystores) > 0 {
ks = keystores[0].(*keystore.KeyStore)
}
setEtherbase(ctx, ks, cfg)
// MODIFIED by Jakub Pajek END (revert require explicit etherbase address)
setGPO(ctx, &cfg.GPO, ctx.String(SyncModeFlag.Name) == "light")
setTxPool(ctx, &cfg.TxPool)
setEthash(ctx, cfg)
Expand Down Expand Up @@ -1912,16 +1949,20 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
passphrase = list[0]
}

// Unlock the developer account by local keystore.
var ks *keystore.KeyStore
if keystores := stack.AccountManager().Backends(keystore.KeyStoreType); len(keystores) > 0 {
ks = keystores[0].(*keystore.KeyStore)
}
if ks == nil {
Fatalf("Keystore is not available")
}
// COMMENTED by Jakub Pajek (revert require explicit etherbase address)
/*
// Unlock the developer account by local keystore.
var ks *keystore.KeyStore
if keystores := stack.AccountManager().Backends(keystore.KeyStoreType); len(keystores) > 0 {
ks = keystores[0].(*keystore.KeyStore)
}
if ks == nil {
Fatalf("Keystore is not available")
}
// Figure out the dev account address.
*/

// Figure out the dev account address.
// setEtherbase has been called above, configuring the miner address from command line flags.
if cfg.Miner.Etherbase != (common.Address{}) {
developer = accounts.Account{Address: cfg.Miner.Etherbase}
Expand All @@ -1933,9 +1974,12 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
Fatalf("Failed to create developer account: %v", err)
}
}
// Make sure the address is configured as fee recipient, otherwise
// the miner will fail to start.
cfg.Miner.Etherbase = developer.Address
// COMMENTED by Jakub Pajek (revert require explicit etherbase address)
/*
// Make sure the address is configured as fee recipient, otherwise
// the miner will fail to start.
cfg.Miner.Etherbase = developer.Address
*/

if err := ks.Unlock(developer, passphrase); err != nil {
Fatalf("Failed to unlock developer account: %v", err)
Expand Down
18 changes: 17 additions & 1 deletion eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,20 @@ func (s *Ethereum) Etherbase() (eb common.Address, err error) {
if etherbase != (common.Address{}) {
return etherbase, nil
}
// ADDED by Jakub Pajek BEG (revert require explicit etherbase address)
if wallets := s.AccountManager().Wallets(); len(wallets) > 0 {
if accounts := wallets[0].Accounts(); len(accounts) > 0 {
etherbase := accounts[0].Address

s.lock.Lock()
s.etherbase = etherbase
s.lock.Unlock()

log.Info("Etherbase automatically configured", "address", etherbase)
return etherbase, nil
}
}
// ADDED by Jakub Pajek END (revert require explicit etherbase address)
return common.Address{}, fmt.Errorf("etherbase must be explicitly specified")
}

Expand Down Expand Up @@ -444,7 +458,9 @@ func (s *Ethereum) StartMining(threads int) error {
// introduced to speed sync times.
atomic.StoreUint32(&s.handler.acceptTxs, 1)

go s.miner.Start()
// MODIFIED by Jakub Pajek (revert require explicit etherbase address)
//go s.miner.Start()
go s.miner.Start(eb)
}
return nil
}
Expand Down
51 changes: 39 additions & 12 deletions miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ type Backend interface {

// Config is the configuration parameters of mining.
type Config struct {
Etherbase common.Address `toml:",omitempty"` // Public address for block mining rewards
// MODIFIED by Jakub Pajek (revert require explicit etherbase address)
//Etherbase common.Address `toml:",omitempty"` // Public address for block mining rewards
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).
NotifyFull bool `toml:",omitempty"` // Notify with pending block headers instead of work packages
ExtraData hexutil.Bytes `toml:",omitempty"` // Block extra data set by the miner
Expand Down Expand Up @@ -73,11 +75,15 @@ var DefaultConfig = Config{

// Miner creates blocks and searches for proof-of-work values.
type Miner struct {
mux *event.TypeMux
eth Backend
engine consensus.Engine
exitCh chan struct{}
startCh chan struct{}
mux *event.TypeMux
// ADDED by Jakub Pajek (revert require explicit etherbase address)
coinbase common.Address
eth Backend
engine consensus.Engine
exitCh chan struct{}
// MODIFIED by Jakub Pajek (revert require explicit etherbase address)
//startCh chan struct{}
startCh chan common.Address
stopCh chan struct{}
worker *worker

Expand All @@ -86,11 +92,13 @@ type Miner struct {

func New(eth Backend, config *Config, chainConfig *params.ChainConfig, mux *event.TypeMux, engine consensus.Engine, isLocalBlock func(header *types.Header) bool) *Miner {
miner := &Miner{
mux: mux,
eth: eth,
engine: engine,
exitCh: make(chan struct{}),
startCh: make(chan struct{}),
mux: mux,
eth: eth,
engine: engine,
exitCh: make(chan struct{}),
// MODIFIED by Jakub Pajek (revert require explicit etherbase address)
//startCh: make(chan struct{}),
startCh: make(chan common.Address),
stopCh: make(chan struct{}),
worker: newWorker(config, chainConfig, engine, eth, mux, isLocalBlock, true),
}
Expand Down Expand Up @@ -137,17 +145,25 @@ func (miner *Miner) update() {
case downloader.FailedEvent:
canStart = true
if shouldStart {
// ADDED by Jakub Pajek (revert require explicit etherbase address)
miner.SetEtherbase(miner.coinbase)
miner.worker.start()
}
case downloader.DoneEvent:
canStart = true
if shouldStart {
// ADDED by Jakub Pajek (revert require explicit etherbase address)
miner.SetEtherbase(miner.coinbase)
miner.worker.start()
}
// Stop reacting to downloader events
events.Unsubscribe()
}
case <-miner.startCh:
// MODIFIED by Jakub Pajek BEG (revert require explicit etherbase address)
//case <-miner.startCh:
case addr := <-miner.startCh:
miner.SetEtherbase(addr)
// MODIFIED by Jakub Pajek END (revert require explicit etherbase address)
if canStart {
miner.worker.start()
}
Expand All @@ -162,9 +178,18 @@ func (miner *Miner) update() {
}
}

// MODIFIED by Jakub Pajek BEG (revert require explicit etherbase address)
/*
func (miner *Miner) Start() {
miner.startCh <- struct{}{}
}
*/

func (miner *Miner) Start(coinbase common.Address) {
miner.startCh <- coinbase
}

// MODIFIED by Jakub Pajek END (revert require explicit etherbase address)

func (miner *Miner) Stop() {
miner.stopCh <- struct{}{}
Expand Down Expand Up @@ -219,6 +244,8 @@ func (miner *Miner) PendingBlockAndReceipts() (*types.Block, types.Receipts) {
}

func (miner *Miner) SetEtherbase(addr common.Address) {
// ADDED by Jakub Pajek (revert require explicit etherbase address)
miner.coinbase = addr
miner.worker.setEtherbase(addr)
}

Expand Down
Loading

0 comments on commit 8eba19c

Please sign in to comment.