瀏覽代碼

Merge pull request #1595 from obscuren/extra-data

cmd/geth, eth: added canonical extra data
Jeffrey Wilcke 10 年之前
父節點
當前提交
d7580f21f6
共有 4 個文件被更改,包括 42 次插入16 次删除
  1. 30 2
      cmd/geth/main.go
  2. 2 7
      eth/backend.go
  3. 8 1
      miner/miner.go
  4. 2 6
      rpc/api/miner.go

+ 30 - 2
cmd/geth/main.go

@@ -42,6 +42,8 @@ import (
 	"github.com/ethereum/go-ethereum/logger"
 	"github.com/ethereum/go-ethereum/logger/glog"
 	"github.com/ethereum/go-ethereum/metrics"
+	"github.com/ethereum/go-ethereum/params"
+	"github.com/ethereum/go-ethereum/rlp"
 	"github.com/ethereum/go-ethereum/rpc/codec"
 	"github.com/ethereum/go-ethereum/rpc/comms"
 	"github.com/mattn/go-colorable"
@@ -49,11 +51,14 @@ import (
 )
 
 const (
-	ClientIdentifier = "Geth"
-	Version          = "1.0.1"
+	ClientIdentifier = "Geth                                                                                                   "
+	VersionMajor     = 1
+	VersionMinor     = 0
+	VersionPatch     = 1
 )
 
 var (
+	Version         = fmt.Sprintf("%d.%d.%d", VersionMajor, VersionMinor, VersionPatch)
 	gitCommit       string // set via linker flagg
 	nodeNameVersion string
 	app             *cli.App
@@ -346,6 +351,27 @@ func main() {
 	}
 }
 
+func makeDefaultExtra() []byte {
+	var clientInfo = struct {
+		Version   uint
+		Name      string
+		GoVersion string
+		Os        string
+	}{uint(VersionMajor<<16 | VersionMinor<<8 | VersionPatch), ClientIdentifier, runtime.Version(), runtime.GOOS}
+	extra, err := rlp.EncodeToBytes(clientInfo)
+	if err != nil {
+		glog.V(logger.Warn).Infoln("error setting canonical miner information:", err)
+	}
+
+	if uint64(len(extra)) > params.MaximumExtraDataSize.Uint64() {
+		glog.V(logger.Warn).Infoln("error setting canonical miner information: extra exceeds", params.MaximumExtraDataSize)
+		glog.V(logger.Debug).Infof("extra: %x\n", extra)
+		return nil
+	}
+
+	return extra
+}
+
 func run(ctx *cli.Context) {
 	utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
 	if ctx.GlobalBool(utils.OlympicFlag.Name) {
@@ -353,6 +379,8 @@ func run(ctx *cli.Context) {
 	}
 
 	cfg := utils.MakeEthConfig(ClientIdentifier, nodeNameVersion, ctx)
+	cfg.ExtraData = makeDefaultExtra()
+
 	ethereum, err := eth.New(cfg)
 	if err != nil {
 		utils.Fatalf("%v", err)

+ 2 - 7
eth/backend.go

@@ -45,7 +45,6 @@ import (
 	"github.com/ethereum/go-ethereum/p2p"
 	"github.com/ethereum/go-ethereum/p2p/discover"
 	"github.com/ethereum/go-ethereum/p2p/nat"
-	"github.com/ethereum/go-ethereum/params"
 	"github.com/ethereum/go-ethereum/whisper"
 )
 
@@ -92,6 +91,7 @@ type Config struct {
 	NatSpec   bool
 	AutoDAG   bool
 	PowTest   bool
+	ExtraData []byte
 
 	MaxPeers        int
 	MaxPendingPeers int
@@ -378,12 +378,7 @@ func New(config *Config) (*Ethereum, error) {
 
 	eth.miner = miner.New(eth, eth.EventMux(), eth.pow)
 	eth.miner.SetGasPrice(config.GasPrice)
-
-	extra := config.Name
-	if uint64(len(extra)) > params.MaximumExtraDataSize.Uint64() {
-		extra = extra[:params.MaximumExtraDataSize.Uint64()]
-	}
-	eth.miner.SetExtra([]byte(extra))
+	eth.miner.SetExtra(config.ExtraData)
 
 	if config.Shh {
 		eth.whisper = whisper.New()

+ 8 - 1
miner/miner.go

@@ -18,6 +18,7 @@
 package miner
 
 import (
+	"fmt"
 	"math/big"
 	"sync/atomic"
 
@@ -29,6 +30,7 @@ import (
 	"github.com/ethereum/go-ethereum/event"
 	"github.com/ethereum/go-ethereum/logger"
 	"github.com/ethereum/go-ethereum/logger/glog"
+	"github.com/ethereum/go-ethereum/params"
 	"github.com/ethereum/go-ethereum/pow"
 )
 
@@ -150,8 +152,13 @@ func (self *Miner) HashRate() (tot int64) {
 	return
 }
 
-func (self *Miner) SetExtra(extra []byte) {
+func (self *Miner) SetExtra(extra []byte) error {
+	if uint64(len(extra)) > params.MaximumExtraDataSize.Uint64() {
+		return fmt.Errorf("Extra exceeds max length. %d > %v", len(extra), params.MaximumExtraDataSize)
+	}
+
 	self.worker.extra = extra
+	return nil
 }
 
 func (self *Miner) PendingState() *state.StateDB {

+ 2 - 6
rpc/api/miner.go

@@ -17,12 +17,9 @@
 package api
 
 import (
-	"fmt"
-
 	"github.com/ethereum/ethash"
 	"github.com/ethereum/go-ethereum/common"
 	"github.com/ethereum/go-ethereum/eth"
-	"github.com/ethereum/go-ethereum/params"
 	"github.com/ethereum/go-ethereum/rpc/codec"
 	"github.com/ethereum/go-ethereum/rpc/shared"
 )
@@ -126,11 +123,10 @@ func (self *minerApi) SetExtra(req *shared.Request) (interface{}, error) {
 		return nil, err
 	}
 
-	if uint64(len(args.Data)) > params.MaximumExtraDataSize.Uint64()*2 {
-		return false, fmt.Errorf("extra datasize can be no longer than %v bytes", params.MaximumExtraDataSize)
+	if err := self.ethereum.Miner().SetExtra([]byte(args.Data)); err != nil {
+		return false, err
 	}
 
-	self.ethereum.Miner().SetExtra([]byte(args.Data))
 	return true, nil
 }