Browse Source

cmd/ethereum, cmd/mist, core, eth, javascript, xeth: fixes for new p2p API

Felix Lange 10 years ago
parent
commit
56f777b2fc

+ 2 - 3
cmd/ethereum/main.go

@@ -31,6 +31,7 @@ import (
 	"github.com/ethereum/go-ethereum/eth"
 	"github.com/ethereum/go-ethereum/ethutil"
 	"github.com/ethereum/go-ethereum/logger"
+	"github.com/ethereum/go-ethereum/p2p"
 	"github.com/ethereum/go-ethereum/state"
 )
 
@@ -61,13 +62,11 @@ func main() {
 	utils.InitConfig(VmType, ConfigFile, Datadir, "ETH")
 
 	ethereum, err := eth.New(&eth.Config{
-		Name:       ClientIdentifier,
-		Version:    Version,
+		Name:       p2p.MakeName(ClientIdentifier, Version),
 		KeyStore:   KeyStore,
 		DataDir:    Datadir,
 		LogFile:    LogFile,
 		LogLevel:   LogLevel,
-		Identifier: Identifier,
 		MaxPeers:   MaxPeer,
 		Port:       OutboundPort,
 		NATType:    PMPGateway,

+ 13 - 1
cmd/mist/assets/qml/main.qml

@@ -844,6 +844,7 @@ ApplicationWindow {
 		     minimumHeight: 50
 		     title: "Connect to peer"
 
+
 		     ComboBox {
 			     id: addrField
 			     anchors.verticalCenter: parent.verticalCenter
@@ -872,6 +873,17 @@ ApplicationWindow {
 			     }
 		     }
 
+ 		     ComboBox {
+			     id: nodeidField
+			     anchors.verticalCenter: parent.verticalCenter
+			     anchors.left: parent.left
+			     anchors.right: addPeerButton.left
+			     anchors.leftMargin: 10
+			     anchors.rightMargin: 10
+			
+			     editable: true
+  			}
+
 		     Button {
 			     id: addPeerButton
 			     anchors.right: parent.right
@@ -879,7 +891,7 @@ ApplicationWindow {
 			     anchors.rightMargin: 10
 			     text: "Add"
 			     onClicked: {
-				     eth.connectToPeer(addrField.currentText)
+				     eth.connectToPeer(addrField.currentText, nodeidField.currentText)
 				     addPeerWin.visible = false
 			     }
 		     }

+ 0 - 12
cmd/mist/assets/qml/views/info.qml

@@ -32,18 +32,6 @@ Rectangle {
 			width: 500
 		}
 
-		Label {
-			text: "Client ID"
-		}
-		TextField {
-			text: gui.getCustomIdentifier()
-			width: 500
-			placeholderText: "Anonymous"
-			onTextChanged: {
-				gui.setCustomIdentifier(text)
-			}
-		}
-
 		TextArea {
 			objectName: "statsPane"
 			width: parent.width

+ 0 - 9
cmd/mist/bindings.go

@@ -64,15 +64,6 @@ func (gui *Gui) Transact(recipient, value, gas, gasPrice, d string) (string, err
 	return gui.xeth.Transact(recipient, value, gas, gasPrice, data)
 }
 
-func (gui *Gui) SetCustomIdentifier(customIdentifier string) {
-	gui.clientIdentity.SetCustomIdentifier(customIdentifier)
-	gui.config.Save("id", customIdentifier)
-}
-
-func (gui *Gui) GetCustomIdentifier() string {
-	return gui.clientIdentity.GetCustomIdentifier()
-}
-
 // functions that allow Gui to implement interface guilogger.LogSystem
 func (gui *Gui) SetLogLevel(level logger.LogLevel) {
 	gui.logLevel = level

+ 11 - 14
cmd/mist/gui.go

@@ -41,7 +41,6 @@ import (
 	"github.com/ethereum/go-ethereum/ethutil"
 	"github.com/ethereum/go-ethereum/logger"
 	"github.com/ethereum/go-ethereum/miner"
-	"github.com/ethereum/go-ethereum/p2p"
 	"github.com/ethereum/go-ethereum/ui/qt/qwhisper"
 	"github.com/ethereum/go-ethereum/xeth"
 	"github.com/obscuren/qml"
@@ -77,9 +76,8 @@ type Gui struct {
 
 	xeth *xeth.XEth
 
-	Session        string
-	clientIdentity *p2p.SimpleClientIdentity
-	config         *ethutil.ConfigManager
+	Session string
+	config  *ethutil.ConfigManager
 
 	plugins map[string]plugin
 
@@ -87,7 +85,7 @@ type Gui struct {
 }
 
 // Create GUI, but doesn't start it
-func NewWindow(ethereum *eth.Ethereum, config *ethutil.ConfigManager, clientIdentity *p2p.SimpleClientIdentity, session string, logLevel int) *Gui {
+func NewWindow(ethereum *eth.Ethereum, config *ethutil.ConfigManager, session string, logLevel int) *Gui {
 	db, err := ethdb.NewLDBDatabase("tx_database")
 	if err != nil {
 		panic(err)
@@ -95,15 +93,14 @@ func NewWindow(ethereum *eth.Ethereum, config *ethutil.ConfigManager, clientIden
 
 	xeth := xeth.New(ethereum)
 	gui := &Gui{eth: ethereum,
-		txDb:           db,
-		xeth:           xeth,
-		logLevel:       logger.LogLevel(logLevel),
-		Session:        session,
-		open:           false,
-		clientIdentity: clientIdentity,
-		config:         config,
-		plugins:        make(map[string]plugin),
-		serviceEvents:  make(chan ServEv, 1),
+		txDb:          db,
+		xeth:          xeth,
+		logLevel:      logger.LogLevel(logLevel),
+		Session:       session,
+		open:          false,
+		config:        config,
+		plugins:       make(map[string]plugin),
+		serviceEvents: make(chan ServEv, 1),
 	}
 	data, _ := ethutil.ReadAllFile(path.Join(ethutil.Config.ExecPath, "plugins.json"))
 	json.Unmarshal([]byte(data), &gui.plugins)

+ 2 - 4
cmd/mist/main.go

@@ -52,13 +52,11 @@ func run() error {
 	config := utils.InitConfig(VmType, ConfigFile, Datadir, "ETH")
 
 	ethereum, err := eth.New(&eth.Config{
-		Name:       ClientIdentifier,
-		Version:    Version,
+		Name:       p2p.MakeName(ClientIdentifier, Version),
 		KeyStore:   KeyStore,
 		DataDir:    Datadir,
 		LogFile:    LogFile,
 		LogLevel:   LogLevel,
-		Identifier: Identifier,
 		MaxPeers:   MaxPeer,
 		Port:       OutboundPort,
 		NATType:    PMPGateway,
@@ -79,7 +77,7 @@ func run() error {
 		utils.StartWebSockets(ethereum, WsPort)
 	}
 
-	gui := NewWindow(ethereum, config, ethereum.ClientIdentity().(*p2p.SimpleClientIdentity), KeyRing, LogLevel)
+	gui := NewWindow(ethereum, config, KeyRing, LogLevel)
 
 	utils.RegisterInterrupt(func(os.Signal) {
 		gui.Stop()

+ 8 - 2
cmd/mist/ui_lib.go

@@ -31,6 +31,7 @@ import (
 	"github.com/ethereum/go-ethereum/event/filter"
 	"github.com/ethereum/go-ethereum/javascript"
 	"github.com/ethereum/go-ethereum/miner"
+	"github.com/ethereum/go-ethereum/p2p/discover"
 	"github.com/ethereum/go-ethereum/xeth"
 	"github.com/obscuren/qml"
 )
@@ -142,8 +143,13 @@ func (ui *UiLib) Connect(button qml.Object) {
 	}
 }
 
-func (ui *UiLib) ConnectToPeer(addr string) {
-	if err := ui.eth.SuggestPeer(addr); err != nil {
+func (ui *UiLib) ConnectToPeer(addr string, hexid string) {
+	id, err := discover.HexID(hexid)
+	if err != nil {
+		guilogger.Errorf("bad node ID: %v", err)
+		return
+	}
+	if err := ui.eth.SuggestPeer(addr, id); err != nil {
 		guilogger.Infoln(err)
 	}
 }

+ 2 - 4
cmd/utils/cmd.go

@@ -122,12 +122,10 @@ func exit(err error) {
 }
 
 func StartEthereum(ethereum *eth.Ethereum, SeedNode string) {
-	clilogger.Infof("Starting %s", ethereum.ClientIdentity())
-	err := ethereum.Start(SeedNode)
-	if err != nil {
+	clilogger.Infoln("Starting ", ethereum.Name())
+	if err := ethereum.Start(SeedNode); err != nil {
 		exit(err)
 	}
-
 	RegisterInterrupt(func(sig os.Signal) {
 		ethereum.Stop()
 		logger.Flush()

+ 0 - 1
core/block_processor.go

@@ -34,7 +34,6 @@ type EthManager interface {
 	IsListening() bool
 	Peers() []*p2p.Peer
 	KeyManager() *crypto.KeyManager
-	ClientIdentity() p2p.ClientIdentity
 	Db() ethutil.Database
 	EventMux() *event.TypeMux
 }

+ 0 - 8
core/helper_test.go

@@ -9,7 +9,6 @@ import (
 	"github.com/ethereum/go-ethereum/ethdb"
 	"github.com/ethereum/go-ethereum/ethutil"
 	"github.com/ethereum/go-ethereum/event"
-	"github.com/ethereum/go-ethereum/p2p"
 )
 
 // Implement our EthTest Manager
@@ -54,13 +53,6 @@ func (tm *TestManager) TxPool() *TxPool {
 func (tm *TestManager) EventMux() *event.TypeMux {
 	return tm.eventMux
 }
-func (tm *TestManager) Broadcast(msgType p2p.Msg, data []interface{}) {
-	fmt.Println("Broadcast not implemented")
-}
-
-func (tm *TestManager) ClientIdentity() p2p.ClientIdentity {
-	return nil
-}
 func (tm *TestManager) KeyManager() *crypto.KeyManager {
 	return nil
 }

+ 30 - 43
eth/backend.go

@@ -12,20 +12,19 @@ import (
 	"github.com/ethereum/go-ethereum/event"
 	ethlogger "github.com/ethereum/go-ethereum/logger"
 	"github.com/ethereum/go-ethereum/p2p"
+	"github.com/ethereum/go-ethereum/p2p/discover"
 	"github.com/ethereum/go-ethereum/pow/ezp"
 	"github.com/ethereum/go-ethereum/rpc"
 	"github.com/ethereum/go-ethereum/whisper"
 )
 
 type Config struct {
-	Name       string
-	Version    string
-	Identifier string
-	KeyStore   string
-	DataDir    string
-	LogFile    string
-	LogLevel   int
-	KeyRing    string
+	Name     string
+	KeyStore string
+	DataDir  string
+	LogFile  string
+	LogLevel int
+	KeyRing  string
 
 	MaxPeers   int
 	Port       string
@@ -66,8 +65,7 @@ type Ethereum struct {
 	WsServer   rpc.RpcServer
 	keyManager *crypto.KeyManager
 
-	clientIdentity p2p.ClientIdentity
-	logger         ethlogger.LogSystem
+	logger ethlogger.LogSystem
 
 	synclock  sync.Mutex
 	syncGroup sync.WaitGroup
@@ -103,21 +101,17 @@ func New(config *Config) (*Ethereum, error) {
 	// Initialise the keyring
 	keyManager.Init(config.KeyRing, 0, false)
 
-	// Create a new client id for this instance. This will help identifying the node on the network
-	clientId := p2p.NewSimpleClientIdentity(config.Name, config.Version, config.Identifier, keyManager.PublicKey())
-
 	saveProtocolVersion(db)
 	//ethutil.Config.Db = db
 
 	eth := &Ethereum{
-		shutdownChan:   make(chan bool),
-		quit:           make(chan bool),
-		db:             db,
-		keyManager:     keyManager,
-		clientIdentity: clientId,
-		blacklist:      p2p.NewBlacklist(),
-		eventMux:       &event.TypeMux{},
-		logger:         logger,
+		shutdownChan: make(chan bool),
+		quit:         make(chan bool),
+		db:           db,
+		keyManager:   keyManager,
+		blacklist:    p2p.NewBlacklist(),
+		eventMux:     &event.TypeMux{},
+		logger:       logger,
 	}
 
 	eth.chainManager = core.NewChainManager(db, eth.EventMux())
@@ -132,21 +126,23 @@ func New(config *Config) (*Ethereum, error) {
 
 	ethProto := EthProtocol(eth.txPool, eth.chainManager, eth.blockPool)
 	protocols := []p2p.Protocol{ethProto, eth.whisper.Protocol()}
-
 	nat, err := p2p.ParseNAT(config.NATType, config.PMPGateway)
 	if err != nil {
 		return nil, err
 	}
-
+	netprv, err := crypto.GenerateKey()
+	if err != nil {
+		return nil, fmt.Errorf("could not generate server key: %v", err)
+	}
 	eth.net = &p2p.Server{
-		Identity:  clientId,
-		MaxPeers:  config.MaxPeers,
-		Protocols: protocols,
-		Blacklist: eth.blacklist,
-		NAT:       nat,
-		NoDial:    !config.Dial,
+		PrivateKey: netprv,
+		Name:       config.Name,
+		MaxPeers:   config.MaxPeers,
+		Protocols:  protocols,
+		Blacklist:  eth.blacklist,
+		NAT:        nat,
+		NoDial:     !config.Dial,
 	}
-
 	if len(config.Port) > 0 {
 		eth.net.ListenAddr = ":" + config.Port
 	}
@@ -162,8 +158,8 @@ func (s *Ethereum) Logger() ethlogger.LogSystem {
 	return s.logger
 }
 
-func (s *Ethereum) ClientIdentity() p2p.ClientIdentity {
-	return s.clientIdentity
+func (s *Ethereum) Name() string {
+	return s.net.Name
 }
 
 func (s *Ethereum) ChainManager() *core.ChainManager {
@@ -241,26 +237,17 @@ func (s *Ethereum) Start(seedNode string) error {
 	s.blockSub = s.eventMux.Subscribe(core.NewMinedBlockEvent{})
 	go s.blockBroadcastLoop()
 
-	// TODO: read peers here
-	if len(seedNode) > 0 {
-		logger.Infof("Connect to seed node %v", seedNode)
-		if err := s.SuggestPeer(seedNode); err != nil {
-			logger.Infoln(err)
-		}
-	}
-
 	logger.Infoln("Server started")
 	return nil
 }
 
-func (self *Ethereum) SuggestPeer(addr string) error {
+func (self *Ethereum) SuggestPeer(addr string, id discover.NodeID) error {
 	netaddr, err := net.ResolveTCPAddr("tcp", addr)
 	if err != nil {
 		logger.Errorf("couldn't resolve %s:", addr, err)
 		return err
 	}
-
-	self.net.SuggestPeer(netaddr.IP, netaddr.Port, nil)
+	self.net.SuggestPeer(netaddr.IP, netaddr.Port, id)
 	return nil
 }
 

+ 2 - 1
eth/protocol.go

@@ -92,13 +92,14 @@ func EthProtocol(txPool txPool, chainManager chainManager, blockPool blockPool)
 // the main loop that handles incoming messages
 // note RemovePeer in the post-disconnect hook
 func runEthProtocol(txPool txPool, chainManager chainManager, blockPool blockPool, peer *p2p.Peer, rw p2p.MsgReadWriter) (err error) {
+	id := peer.ID()
 	self := &ethProtocol{
 		txPool:       txPool,
 		chainManager: chainManager,
 		blockPool:    blockPool,
 		rw:           rw,
 		peer:         peer,
-		id:           fmt.Sprintf("%x", peer.Identity().Pubkey()[:8]),
+		id:           fmt.Sprintf("%x", id[:8]),
 	}
 	err = self.handleStatus()
 	if err == nil {

+ 5 - 19
eth/protocol_test.go

@@ -14,6 +14,7 @@ import (
 	"github.com/ethereum/go-ethereum/ethutil"
 	ethlogger "github.com/ethereum/go-ethereum/logger"
 	"github.com/ethereum/go-ethereum/p2p"
+	"github.com/ethereum/go-ethereum/p2p/discover"
 )
 
 var sys = ethlogger.NewStdLogSystem(os.Stdout, log.LstdFlags, ethlogger.LogLevel(ethlogger.DebugDetailLevel))
@@ -128,26 +129,11 @@ func (self *testBlockPool) RemovePeer(peerId string) {
 	}
 }
 
-// TODO: refactor this into p2p/client_identity
-type peerId struct {
-	pubkey []byte
-}
-
-func (self *peerId) String() string {
-	return "test peer"
-}
-
-func (self *peerId) Pubkey() (pubkey []byte) {
-	pubkey = self.pubkey
-	if len(pubkey) == 0 {
-		pubkey = crypto.GenerateNewKeyPair().PublicKey
-		self.pubkey = pubkey
-	}
-	return
-}
-
 func testPeer() *p2p.Peer {
-	return p2p.NewPeer(&peerId{}, []p2p.Cap{})
+	var id discover.NodeID
+	pk := crypto.GenerateNewKeyPair().PublicKey
+	copy(id[:], pk)
+	return p2p.NewPeer(id, "test peer", []p2p.Cap{})
 }
 
 type ethProtocolTester struct {

+ 10 - 2
javascript/javascript_runtime.go

@@ -14,6 +14,7 @@ import (
 	"github.com/ethereum/go-ethereum/ethutil"
 	"github.com/ethereum/go-ethereum/event"
 	"github.com/ethereum/go-ethereum/logger"
+	"github.com/ethereum/go-ethereum/p2p/discover"
 	"github.com/ethereum/go-ethereum/state"
 	"github.com/ethereum/go-ethereum/xeth"
 	"github.com/obscuren/otto"
@@ -201,8 +202,15 @@ func (self *JSRE) addPeer(call otto.FunctionCall) otto.Value {
 	if err != nil {
 		return otto.FalseValue()
 	}
-	self.ethereum.SuggestPeer(host)
-
+	idstr, err := call.Argument(0).ToString()
+	if err != nil {
+		return otto.FalseValue()
+	}
+	id, err := discover.HexID(idstr)
+	if err != nil {
+		return otto.FalseValue()
+	}
+	self.ethereum.SuggestPeer(host, id)
 	return otto.TrueValue()
 }
 

+ 1 - 1
xeth/types.go

@@ -215,7 +215,7 @@ func NewPeer(peer *p2p.Peer) *Peer {
 	return &Peer{
 		ref:     peer,
 		Ip:      fmt.Sprintf("%v", peer.RemoteAddr()),
-		Version: fmt.Sprintf("%v", peer.Identity()),
+		Version: fmt.Sprintf("%v", peer.ID()),
 		Caps:    fmt.Sprintf("%v", caps),
 	}
 }

+ 0 - 1
xeth/xeth.go

@@ -31,7 +31,6 @@ type Backend interface {
 	IsListening() bool
 	Peers() []*p2p.Peer
 	KeyManager() *crypto.KeyManager
-	ClientIdentity() p2p.ClientIdentity
 	Db() ethutil.Database
 	EventMux() *event.TypeMux
 	Whisper() *whisper.Whisper