Skip to content

Commit e5fba8f

Browse files
committed
Merge pull request #1428 from obscuren/coinbase-fixes
cmd,eth,rpc,tests: default coinbase
2 parents 916d155 + 37c1a8f commit e5fba8f

File tree

12 files changed

+99
-51
lines changed

12 files changed

+99
-51
lines changed

cmd/geth/main.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -461,22 +461,7 @@ func execJSFiles(ctx *cli.Context) {
461461

462462
func unlockAccount(ctx *cli.Context, am *accounts.Manager, addr string, i int) (addrHex, auth string) {
463463
var err error
464-
// Load startup keys. XXX we are going to need a different format
465-
466-
if !((len(addr) == 40) || (len(addr) == 42)) { // with or without 0x
467-
var index int
468-
index, err = strconv.Atoi(addr)
469-
if err != nil {
470-
utils.Fatalf("Invalid account address '%s'", addr)
471-
}
472-
473-
addrHex, err = am.AddressByIndex(index)
474-
if err != nil {
475-
utils.Fatalf("%v", err)
476-
}
477-
} else {
478-
addrHex = addr
479-
}
464+
addrHex = utils.ParamToAddress(addr, am)
480465
// Attempt to unlock the account 3 times
481466
attempts := 3
482467
for tries := 0; tries < attempts; tries++ {

cmd/utils/flags.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"path/filepath"
1111
"runtime"
12+
"strconv"
1213

1314
"github.com/ethereum/go-ethereum/metrics"
1415

@@ -122,8 +123,8 @@ var (
122123
}
123124
EtherbaseFlag = cli.StringFlag{
124125
Name: "etherbase",
125-
Usage: "Public address for block mining rewards. By default the address of your primary account is used",
126-
Value: "primary",
126+
Usage: "Public address for block mining rewards. By default the address first created is used",
127+
Value: "0",
127128
}
128129
GasPriceFlag = cli.StringFlag{
129130
Name: "gasprice",
@@ -351,6 +352,8 @@ func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
351352
if len(customName) > 0 {
352353
clientID += "/" + customName
353354
}
355+
am := MakeAccountManager(ctx)
356+
354357
return &eth.Config{
355358
Name: common.MakeName(clientID, version),
356359
DataDir: ctx.GlobalString(DataDirFlag.Name),
@@ -361,9 +364,9 @@ func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
361364
LogFile: ctx.GlobalString(LogFileFlag.Name),
362365
Verbosity: ctx.GlobalInt(VerbosityFlag.Name),
363366
LogJSON: ctx.GlobalString(LogJSONFlag.Name),
364-
Etherbase: ctx.GlobalString(EtherbaseFlag.Name),
367+
Etherbase: common.HexToAddress(ParamToAddress(ctx.GlobalString(EtherbaseFlag.Name), am)),
365368
MinerThreads: ctx.GlobalInt(MinerThreadsFlag.Name),
366-
AccountManager: MakeAccountManager(ctx),
369+
AccountManager: am,
367370
VmDebug: ctx.GlobalBool(VMDebugFlag.Name),
368371
MaxPeers: ctx.GlobalInt(MaxPeersFlag.Name),
369372
MaxPendingPeers: ctx.GlobalInt(MaxPendingPeersFlag.Name),
@@ -488,3 +491,20 @@ func StartPProf(ctx *cli.Context) {
488491
log.Println(http.ListenAndServe(address, nil))
489492
}()
490493
}
494+
495+
func ParamToAddress(addr string, am *accounts.Manager) (addrHex string) {
496+
if !((len(addr) == 40) || (len(addr) == 42)) { // with or without 0x
497+
index, err := strconv.Atoi(addr)
498+
if err != nil {
499+
Fatalf("Invalid account address '%s'", addr)
500+
}
501+
502+
addrHex, err = am.AddressByIndex(index)
503+
if err != nil {
504+
Fatalf("%v", err)
505+
}
506+
} else {
507+
addrHex = addr
508+
}
509+
return
510+
}

eth/backend.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ type Config struct {
8888
Shh bool
8989
Dial bool
9090

91-
Etherbase string
91+
Etherbase common.Address
9292
GasPrice *big.Int
9393
MinerThreads int
9494
AccountManager *accounts.Manager
@@ -324,7 +324,7 @@ func New(config *Config) (*Ethereum, error) {
324324
eventMux: &event.TypeMux{},
325325
accountManager: config.AccountManager,
326326
DataDir: config.DataDir,
327-
etherbase: common.HexToAddress(config.Etherbase),
327+
etherbase: config.Etherbase,
328328
clientVersion: config.Name, // TODO should separate from Name
329329
netVersionId: config.NetworkId,
330330
NatSpec: config.NatSpec,
@@ -480,6 +480,12 @@ func (s *Ethereum) Etherbase() (eb common.Address, err error) {
480480
return
481481
}
482482

483+
// set in js console via admin interface or wrapper from cli flags
484+
func (self *Ethereum) SetEtherbase(etherbase common.Address) {
485+
self.etherbase = etherbase
486+
self.miner.SetEtherbase(etherbase)
487+
}
488+
483489
func (s *Ethereum) StopMining() { s.miner.Stop() }
484490
func (s *Ethereum) IsMining() bool { return s.miner.Mining() }
485491
func (s *Ethereum) Miner() *miner.Miner { return s.miner }

miner/miner.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,8 @@ func (self *Miner) PendingState() *state.StateDB {
137137
func (self *Miner) PendingBlock() *types.Block {
138138
return self.worker.pendingBlock()
139139
}
140+
141+
func (self *Miner) SetEtherbase(addr common.Address) {
142+
self.coinbase = addr
143+
self.worker.setEtherbase(addr)
144+
}

miner/worker.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ func newWorker(coinbase common.Address, eth core.Backend) *worker {
124124
return worker
125125
}
126126

127+
func (self *worker) setEtherbase(addr common.Address) {
128+
self.mu.Lock()
129+
defer self.mu.Unlock()
130+
self.coinbase = addr
131+
}
132+
127133
func (self *worker) pendingState() *state.StateDB {
128134
self.currentMu.Lock()
129135
defer self.currentMu.Unlock()

rpc/api/miner.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var (
1919
"miner_makeDAG": (*minerApi).MakeDAG,
2020
"miner_setExtra": (*minerApi).SetExtra,
2121
"miner_setGasPrice": (*minerApi).SetGasPrice,
22+
"miner_setEtherbase": (*minerApi).SetEtherbase,
2223
"miner_startAutoDAG": (*minerApi).StartAutoDAG,
2324
"miner_start": (*minerApi).StartMiner,
2425
"miner_stopAutoDAG": (*minerApi).StopAutoDAG,
@@ -119,6 +120,15 @@ func (self *minerApi) SetGasPrice(req *shared.Request) (interface{}, error) {
119120
return true, nil
120121
}
121122

123+
func (self *minerApi) SetEtherbase(req *shared.Request) (interface{}, error) {
124+
args := new(SetEtherbaseArgs)
125+
if err := self.codec.Decode(req.Params, &args); err != nil {
126+
return false, err
127+
}
128+
self.ethereum.SetEtherbase(args.Etherbase)
129+
return nil, nil
130+
}
131+
122132
func (self *minerApi) StartAutoDAG(req *shared.Request) (interface{}, error) {
123133
self.ethereum.StartAutoDAG()
124134
return true, nil

rpc/api/miner_args.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"math/big"
77

8+
"github.com/ethereum/go-ethereum/common"
89
"github.com/ethereum/go-ethereum/rpc/shared"
910
)
1011

@@ -76,6 +77,31 @@ func (args *GasPriceArgs) UnmarshalJSON(b []byte) (err error) {
7677
return shared.NewInvalidTypeError("Price", "not a string")
7778
}
7879

80+
type SetEtherbaseArgs struct {
81+
Etherbase common.Address
82+
}
83+
84+
func (args *SetEtherbaseArgs) UnmarshalJSON(b []byte) (err error) {
85+
var obj []interface{}
86+
if err := json.Unmarshal(b, &obj); err != nil {
87+
return shared.NewDecodeParamError(err.Error())
88+
}
89+
90+
if len(obj) < 1 {
91+
return shared.NewInsufficientParamsError(len(obj), 1)
92+
}
93+
94+
if addr, ok := obj[0].(string); ok {
95+
args.Etherbase = common.HexToAddress(addr)
96+
if (args.Etherbase == common.Address{}) {
97+
return shared.NewInvalidTypeError("Etherbase", "not a valid address")
98+
}
99+
return nil
100+
}
101+
102+
return shared.NewInvalidTypeError("Etherbase", "not a string")
103+
}
104+
79105
type MakeDAGArgs struct {
80106
BlockNumber int64
81107
}

rpc/api/miner_js.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ web3._extend({
1717
params: 1,
1818
inputFormatter: [null]
1919
}),
20+
new web3._extend.Method({
21+
name: 'setEtherbase',
22+
call: 'miner_setEtherbase',
23+
params: 1,
24+
inputFormatter: [web3._extend.formatters.formatInputInt],
25+
outputFormatter: web3._extend.formatters.formatOutputBool
26+
}),
2027
new web3._extend.Method({
2128
name: 'setExtra',
2229
call: 'miner_setExtra',

tests/block_test_util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func (test *BlockTest) makeEthConfig() *eth.Config {
180180
return &eth.Config{
181181
DataDir: common.DefaultDataDir(),
182182
Verbosity: 5,
183-
Etherbase: "primary",
183+
Etherbase: common.Address{},
184184
AccountManager: accounts.NewManager(ks),
185185
NewDB: func(path string) (common.Database, error) { return ethdb.NewMemDatabase() },
186186
}

tests/state_test_util.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tests
22

33
import (
44
"bytes"
5+
"encoding/hex"
56
"fmt"
67
"io"
78
"math/big"
@@ -147,13 +148,12 @@ func runStateTest(test VmTest) error {
147148

148149
func RunState(statedb *state.StateDB, env, tx map[string]string) ([]byte, state.Logs, *big.Int, error) {
149150
var (
150-
keyPair, _ = crypto.NewKeyPairFromSec([]byte(common.Hex2Bytes(tx["secretKey"])))
151-
data = common.FromHex(tx["data"])
152-
gas = common.Big(tx["gasLimit"])
153-
price = common.Big(tx["gasPrice"])
154-
value = common.Big(tx["value"])
155-
nonce = common.Big(tx["nonce"]).Uint64()
156-
caddr = common.HexToAddress(env["currentCoinbase"])
151+
data = common.FromHex(tx["data"])
152+
gas = common.Big(tx["gasLimit"])
153+
price = common.Big(tx["gasPrice"])
154+
value = common.Big(tx["value"])
155+
nonce = common.Big(tx["nonce"]).Uint64()
156+
caddr = common.HexToAddress(env["currentCoinbase"])
157157
)
158158

159159
var to *common.Address
@@ -168,9 +168,11 @@ func RunState(statedb *state.StateDB, env, tx map[string]string) ([]byte, state.
168168
coinbase := statedb.GetOrNewStateObject(caddr)
169169
coinbase.SetGasLimit(common.Big(env["currentGasLimit"]))
170170

171-
message := NewMessage(common.BytesToAddress(keyPair.Address()), to, data, value, gas, price, nonce)
171+
key, _ := hex.DecodeString(tx["secretKey"])
172+
addr := crypto.PubkeyToAddress(crypto.ToECDSA(key).PublicKey)
173+
message := NewMessage(addr, to, data, value, gas, price, nonce)
172174
vmenv := NewEnvFromMap(statedb, env, tx)
173-
vmenv.origin = common.BytesToAddress(keyPair.Address())
175+
vmenv.origin = addr
174176
ret, _, err := core.ApplyMessage(vmenv, message, coinbase)
175177
if core.IsNonceErr(err) || core.IsInvalidTxErr(err) || state.IsGasLimitErr(err) {
176178
statedb.Set(snapshot)

0 commit comments

Comments
 (0)