diff options
author | Felix Lange <fjl@twurst.com> | 2015-02-05 10:16:16 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2015-02-06 07:03:59 +0800 |
commit | 56f777b2fc77275bc636562b66a08b19afe2ec56 (patch) | |
tree | c9a34bd23e13adb0d850957b9a66cd748d34b3e3 | |
parent | 8e8ec8f5f8974aafeac5e4a9ead76878fe22cefd (diff) | |
download | go-tangerine-56f777b2fc77275bc636562b66a08b19afe2ec56.tar.gz go-tangerine-56f777b2fc77275bc636562b66a08b19afe2ec56.tar.zst go-tangerine-56f777b2fc77275bc636562b66a08b19afe2ec56.zip |
cmd/ethereum, cmd/mist, core, eth, javascript, xeth: fixes for new p2p API
-rw-r--r-- | cmd/ethereum/main.go | 5 | ||||
-rw-r--r-- | cmd/mist/assets/qml/main.qml | 14 | ||||
-rw-r--r-- | cmd/mist/assets/qml/views/info.qml | 12 | ||||
-rw-r--r-- | cmd/mist/bindings.go | 9 | ||||
-rw-r--r-- | cmd/mist/gui.go | 25 | ||||
-rw-r--r-- | cmd/mist/main.go | 6 | ||||
-rw-r--r-- | cmd/mist/ui_lib.go | 10 | ||||
-rw-r--r-- | cmd/utils/cmd.go | 6 | ||||
-rw-r--r-- | core/block_processor.go | 1 | ||||
-rw-r--r-- | core/helper_test.go | 8 | ||||
-rw-r--r-- | eth/backend.go | 73 | ||||
-rw-r--r-- | eth/protocol.go | 3 | ||||
-rw-r--r-- | eth/protocol_test.go | 24 | ||||
-rw-r--r-- | javascript/javascript_runtime.go | 12 | ||||
-rw-r--r-- | xeth/types.go | 2 | ||||
-rw-r--r-- | xeth/xeth.go | 1 |
16 files changed, 86 insertions, 125 deletions
diff --git a/cmd/ethereum/main.go b/cmd/ethereum/main.go index 4b16fb79f..faac0bc5d 100644 --- a/cmd/ethereum/main.go +++ b/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(ð.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, diff --git a/cmd/mist/assets/qml/main.qml b/cmd/mist/assets/qml/main.qml index 357e40846..b1d3f2d19 100644 --- a/cmd/mist/assets/qml/main.qml +++ b/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 } } diff --git a/cmd/mist/assets/qml/views/info.qml b/cmd/mist/assets/qml/views/info.qml index 14ee0bce1..b2d2f521c 100644 --- a/cmd/mist/assets/qml/views/info.qml +++ b/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 diff --git a/cmd/mist/bindings.go b/cmd/mist/bindings.go index 706c789b1..9623538a3 100644 --- a/cmd/mist/bindings.go +++ b/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 diff --git a/cmd/mist/gui.go b/cmd/mist/gui.go index edc799abc..dee99859c 100644 --- a/cmd/mist/gui.go +++ b/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) diff --git a/cmd/mist/main.go b/cmd/mist/main.go index 66872a241..17ab9467a 100644 --- a/cmd/mist/main.go +++ b/cmd/mist/main.go @@ -52,13 +52,11 @@ func run() error { config := utils.InitConfig(VmType, ConfigFile, Datadir, "ETH") ethereum, err := eth.New(ð.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() diff --git a/cmd/mist/ui_lib.go b/cmd/mist/ui_lib.go index 7c5802076..2e557dba9 100644 --- a/cmd/mist/ui_lib.go +++ b/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) } } diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index 70df289c3..2f48bced5 100644 --- a/cmd/utils/cmd.go +++ b/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() diff --git a/core/block_processor.go b/core/block_processor.go index 6db3c25f5..e6d4a1cda 100644 --- a/core/block_processor.go +++ b/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 } diff --git a/core/helper_test.go b/core/helper_test.go index 7b41b86f1..473576e3f 100644 --- a/core/helper_test.go +++ b/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 } diff --git a/eth/backend.go b/eth/backend.go index 43e757435..08052c15d 100644 --- a/eth/backend.go +++ b/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 } diff --git a/eth/protocol.go b/eth/protocol.go index d7a7fa910..fb694c877 100644 --- a/eth/protocol.go +++ b/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 := ðProtocol{ 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 { diff --git a/eth/protocol_test.go b/eth/protocol_test.go index 1fe6d8f6b..a91806a1c 100644 --- a/eth/protocol_test.go +++ b/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 { diff --git a/javascript/javascript_runtime.go b/javascript/javascript_runtime.go index 398daf43a..a09aff027 100644 --- a/javascript/javascript_runtime.go +++ b/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() } diff --git a/xeth/types.go b/xeth/types.go index 34caf5cbc..2c6c52675 100644 --- a/xeth/types.go +++ b/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), } } diff --git a/xeth/xeth.go b/xeth/xeth.go index 0e71a0ed9..02e68e697 100644 --- a/xeth/xeth.go +++ b/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 |