Просмотр исходного кода

cmd,eth,rpc,tests: default coinbase

Jeffrey Wilcke 10 лет назад
Родитель
Сommit
35cd355c14
10 измененных файлов с 87 добавлено и 51 удалено
  1. 1 16
      cmd/geth/main.go
  2. 24 4
      cmd/utils/flags.go
  3. 7 2
      eth/backend.go
  4. 10 0
      rpc/api/miner.go
  5. 26 0
      rpc/api/miner_args.go
  6. 7 0
      rpc/api/miner_js.go
  7. 1 1
      tests/block_test_util.go
  8. 11 9
      tests/state_test_util.go
  9. 0 10
      xeth/types.go
  10. 0 9
      xeth/xeth.go

+ 1 - 16
cmd/geth/main.go

@@ -461,22 +461,7 @@ func execJSFiles(ctx *cli.Context) {
 
 func unlockAccount(ctx *cli.Context, am *accounts.Manager, addr string, i int) (addrHex, auth string) {
 	var err error
-	// Load startup keys. XXX we are going to need a different format
-
-	if !((len(addr) == 40) || (len(addr) == 42)) { // with or without 0x
-		var index int
-		index, err = strconv.Atoi(addr)
-		if err != nil {
-			utils.Fatalf("Invalid account address '%s'", addr)
-		}
-
-		addrHex, err = am.AddressByIndex(index)
-		if err != nil {
-			utils.Fatalf("%v", err)
-		}
-	} else {
-		addrHex = addr
-	}
+	addrHex = utils.ParamToAddress(addr, am)
 	// Attempt to unlock the account 3 times
 	attempts := 3
 	for tries := 0; tries < attempts; tries++ {

+ 24 - 4
cmd/utils/flags.go

@@ -9,6 +9,7 @@ import (
 	"os"
 	"path/filepath"
 	"runtime"
+	"strconv"
 
 	"github.com/ethereum/go-ethereum/metrics"
 
@@ -122,8 +123,8 @@ var (
 	}
 	EtherbaseFlag = cli.StringFlag{
 		Name:  "etherbase",
-		Usage: "Public address for block mining rewards. By default the address of your primary account is used",
-		Value: "primary",
+		Usage: "Public address for block mining rewards. By default the address first created is used",
+		Value: "0",
 	}
 	GasPriceFlag = cli.StringFlag{
 		Name:  "gasprice",
@@ -351,6 +352,8 @@ func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
 	if len(customName) > 0 {
 		clientID += "/" + customName
 	}
+	am := MakeAccountManager(ctx)
+
 	return &eth.Config{
 		Name:                    common.MakeName(clientID, version),
 		DataDir:                 ctx.GlobalString(DataDirFlag.Name),
@@ -361,9 +364,9 @@ func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
 		LogFile:                 ctx.GlobalString(LogFileFlag.Name),
 		Verbosity:               ctx.GlobalInt(VerbosityFlag.Name),
 		LogJSON:                 ctx.GlobalString(LogJSONFlag.Name),
-		Etherbase:               ctx.GlobalString(EtherbaseFlag.Name),
+		Etherbase:               common.HexToAddress(ParamToAddress(ctx.GlobalString(EtherbaseFlag.Name), am)),
 		MinerThreads:            ctx.GlobalInt(MinerThreadsFlag.Name),
-		AccountManager:          MakeAccountManager(ctx),
+		AccountManager:          am,
 		VmDebug:                 ctx.GlobalBool(VMDebugFlag.Name),
 		MaxPeers:                ctx.GlobalInt(MaxPeersFlag.Name),
 		MaxPendingPeers:         ctx.GlobalInt(MaxPendingPeersFlag.Name),
@@ -488,3 +491,20 @@ func StartPProf(ctx *cli.Context) {
 		log.Println(http.ListenAndServe(address, nil))
 	}()
 }
+
+func ParamToAddress(addr string, am *accounts.Manager) (addrHex string) {
+	if !((len(addr) == 40) || (len(addr) == 42)) { // with or without 0x
+		index, err := strconv.Atoi(addr)
+		if err != nil {
+			Fatalf("Invalid account address '%s'", addr)
+		}
+
+		addrHex, err = am.AddressByIndex(index)
+		if err != nil {
+			Fatalf("%v", err)
+		}
+	} else {
+		addrHex = addr
+	}
+	return
+}

+ 7 - 2
eth/backend.go

@@ -87,7 +87,7 @@ type Config struct {
 	Shh  bool
 	Dial bool
 
-	Etherbase      string
+	Etherbase      common.Address
 	GasPrice       *big.Int
 	MinerThreads   int
 	AccountManager *accounts.Manager
@@ -322,7 +322,7 @@ func New(config *Config) (*Ethereum, error) {
 		eventMux:                &event.TypeMux{},
 		accountManager:          config.AccountManager,
 		DataDir:                 config.DataDir,
-		etherbase:               common.HexToAddress(config.Etherbase),
+		etherbase:               config.Etherbase,
 		clientVersion:           config.Name, // TODO should separate from Name
 		netVersionId:            config.NetworkId,
 		NatSpec:                 config.NatSpec,
@@ -469,6 +469,11 @@ func (s *Ethereum) Etherbase() (eb common.Address, err error) {
 	return
 }
 
+// set in js console via admin interface or wrapper from cli flags
+func (self *Ethereum) SetEtherbase(etherbase common.Address) {
+	self.etherbase = etherbase
+}
+
 func (s *Ethereum) StopMining()         { s.miner.Stop() }
 func (s *Ethereum) IsMining() bool      { return s.miner.Mining() }
 func (s *Ethereum) Miner() *miner.Miner { return s.miner }

+ 10 - 0
rpc/api/miner.go

@@ -19,6 +19,7 @@ var (
 		"miner_makeDAG":      (*minerApi).MakeDAG,
 		"miner_setExtra":     (*minerApi).SetExtra,
 		"miner_setGasPrice":  (*minerApi).SetGasPrice,
+		"admin_setEtherbase": (*minerApi).SetEtherbase,
 		"miner_startAutoDAG": (*minerApi).StartAutoDAG,
 		"miner_start":        (*minerApi).StartMiner,
 		"miner_stopAutoDAG":  (*minerApi).StopAutoDAG,
@@ -119,6 +120,15 @@ func (self *minerApi) SetGasPrice(req *shared.Request) (interface{}, error) {
 	return true, nil
 }
 
+func (self *minerApi) SetEtherbase(req *shared.Request) (interface{}, error) {
+	args := new(SetEtherbaseArgs)
+	if err := self.codec.Decode(req.Params, &args); err != nil {
+		return false, err
+	}
+	self.ethereum.SetEtherbase(args.Etherbase)
+	return nil, nil
+}
+
 func (self *minerApi) StartAutoDAG(req *shared.Request) (interface{}, error) {
 	self.ethereum.StartAutoDAG()
 	return true, nil

+ 26 - 0
rpc/api/miner_args.go

@@ -5,6 +5,7 @@ import (
 
 	"math/big"
 
+	"github.com/ethereum/go-ethereum/common"
 	"github.com/ethereum/go-ethereum/rpc/shared"
 )
 
@@ -76,6 +77,31 @@ func (args *GasPriceArgs) UnmarshalJSON(b []byte) (err error) {
 	return shared.NewInvalidTypeError("Price", "not a string")
 }
 
+type SetEtherbaseArgs struct {
+	Etherbase common.Address
+}
+
+func (args *SetEtherbaseArgs) UnmarshalJSON(b []byte) (err error) {
+	var obj []interface{}
+	if err := json.Unmarshal(b, &obj); err != nil {
+		return shared.NewDecodeParamError(err.Error())
+	}
+
+	if len(obj) < 1 {
+		return shared.NewInsufficientParamsError(len(obj), 1)
+	}
+
+	if addr, ok := obj[0].(string); ok {
+		args.Etherbase = common.HexToAddress(addr)
+		if (args.Etherbase == common.Address{}) {
+			return shared.NewInvalidTypeError("Etherbase", "not a valid address")
+		}
+		return nil
+	}
+
+	return shared.NewInvalidTypeError("Etherbase", "not a string")
+}
+
 type MakeDAGArgs struct {
 	BlockNumber int64
 }

+ 7 - 0
rpc/api/miner_js.go

@@ -19,6 +19,13 @@ web3._extend({
 			inputFormatter: [web3._extend.formatters.formatInputInt],
 			outputFormatter: web3._extend.formatters.formatOutputBool
 		}),
+		new web3._extend.Method({
+			name: 'setEtherbase',
+			call: 'miner_setEtherbase',
+			params: 1,
+			inputFormatter: [web3._extend.formatters.formatInputInt],
+			outputFormatter: web3._extend.formatters.formatOutputBool
+		}),
 		new web3._extend.Method({
 			name: 'setExtra',
 			call: 'miner_setExtra',

+ 1 - 1
tests/block_test_util.go

@@ -180,7 +180,7 @@ func (test *BlockTest) makeEthConfig() *eth.Config {
 	return &eth.Config{
 		DataDir:        common.DefaultDataDir(),
 		Verbosity:      5,
-		Etherbase:      "primary",
+		Etherbase:      common.Address{},
 		AccountManager: accounts.NewManager(ks),
 		NewDB:          func(path string) (common.Database, error) { return ethdb.NewMemDatabase() },
 	}

+ 11 - 9
tests/state_test_util.go

@@ -2,6 +2,7 @@ package tests
 
 import (
 	"bytes"
+	"encoding/hex"
 	"fmt"
 	"io"
 	"math/big"
@@ -147,13 +148,12 @@ func runStateTest(test VmTest) error {
 
 func RunState(statedb *state.StateDB, env, tx map[string]string) ([]byte, state.Logs, *big.Int, error) {
 	var (
-		keyPair, _ = crypto.NewKeyPairFromSec([]byte(common.Hex2Bytes(tx["secretKey"])))
-		data       = common.FromHex(tx["data"])
-		gas        = common.Big(tx["gasLimit"])
-		price      = common.Big(tx["gasPrice"])
-		value      = common.Big(tx["value"])
-		nonce      = common.Big(tx["nonce"]).Uint64()
-		caddr      = common.HexToAddress(env["currentCoinbase"])
+		data  = common.FromHex(tx["data"])
+		gas   = common.Big(tx["gasLimit"])
+		price = common.Big(tx["gasPrice"])
+		value = common.Big(tx["value"])
+		nonce = common.Big(tx["nonce"]).Uint64()
+		caddr = common.HexToAddress(env["currentCoinbase"])
 	)
 
 	var to *common.Address
@@ -168,9 +168,11 @@ func RunState(statedb *state.StateDB, env, tx map[string]string) ([]byte, state.
 	coinbase := statedb.GetOrNewStateObject(caddr)
 	coinbase.SetGasLimit(common.Big(env["currentGasLimit"]))
 
-	message := NewMessage(common.BytesToAddress(keyPair.Address()), to, data, value, gas, price, nonce)
+	key, _ := hex.DecodeString(tx["secretKey"])
+	addr := crypto.PubkeyToAddress(crypto.ToECDSA(key).PublicKey)
+	message := NewMessage(addr, to, data, value, gas, price, nonce)
 	vmenv := NewEnvFromMap(statedb, env, tx)
-	vmenv.origin = common.BytesToAddress(keyPair.Address())
+	vmenv.origin = addr
 	ret, _, err := core.ApplyMessage(vmenv, message, coinbase)
 	if core.IsNonceErr(err) || core.IsInvalidTxErr(err) || state.IsGasLimitErr(err) {
 		statedb.Set(snapshot)

+ 0 - 10
xeth/types.go

@@ -168,16 +168,6 @@ func (self *Transaction) ToString() string {
 	return self.ref.String()
 }
 
-type Key struct {
-	Address    string `json:"address"`
-	PrivateKey string `json:"privateKey"`
-	PublicKey  string `json:"publicKey"`
-}
-
-func NewKey(key *crypto.KeyPair) *Key {
-	return &Key{common.ToHex(key.Address()), common.ToHex(key.PrivateKey), common.ToHex(key.PublicKey)}
-}
-
 type PReceipt struct {
 	CreatedContract bool   `json:"createdContract"`
 	Address         string `json:"address"`

+ 0 - 9
xeth/xeth.go

@@ -504,15 +504,6 @@ func (self *XEth) IsContract(address string) bool {
 	return len(self.State().SafeGet(address).Code()) > 0
 }
 
-func (self *XEth) SecretToAddress(key string) string {
-	pair, err := crypto.NewKeyPairFromSec(common.FromHex(key))
-	if err != nil {
-		return ""
-	}
-
-	return common.ToHex(pair.Address())
-}
-
 func (self *XEth) UninstallFilter(id int) bool {
 	defer self.filterManager.UninstallFilter(id)