cmd/mist, eth, javascript, p2p: use Node URLs for peer suggestions

This commit is contained in:
Felix Lange 2015-02-07 00:15:04 +01:00
parent e34d134102
commit 2cf4fed11b
6 changed files with 21 additions and 62 deletions

View File

@ -205,7 +205,7 @@ ApplicationWindow {
Menu { Menu {
title: "Network" title: "Network"
MenuItem { MenuItem {
text: "Add Peer" text: "Connect to Node"
shortcut: "Ctrl+p" shortcut: "Ctrl+p"
onTriggered: { onTriggered: {
addPeerWin.visible = true addPeerWin.visible = true
@ -838,50 +838,24 @@ ApplicationWindow {
Window { Window {
id: addPeerWin id: addPeerWin
visible: false visible: false
minimumWidth: 300 minimumWidth: 400
maximumWidth: 300 maximumWidth: 400
maximumHeight: 50 maximumHeight: 50
minimumHeight: 50 minimumHeight: 50
title: "Connect to peer" title: "Connect to Node"
TextField {
ComboBox {
id: addrField id: addrField
placeholderText: "enode://<hex node id>:<IP address>:<port>"
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
anchors.left: parent.left anchors.left: parent.left
anchors.right: addPeerButton.left anchors.right: addPeerButton.left
anchors.leftMargin: 10 anchors.leftMargin: 10
anchors.rightMargin: 10 anchors.rightMargin: 10
onAccepted: { onAccepted: {
eth.connectToPeer(addrField.currentText) eth.connectToPeer(addrField.text)
addPeerWin.visible = false addPeerWin.visible = false
} }
editable: true
model: ListModel { id: pastPeers }
Component.onCompleted: {
pastPeers.insert(0, {text: "poc-8.ethdev.com:30303"})
/*
var ips = eth.pastPeers()
for(var i = 0; i < ips.length; i++) {
pastPeers.append({text: ips.get(i)})
}
pastPeers.insert(0, {text: "poc-7.ethdev.com:30303"})
*/
}
}
ComboBox {
id: nodeidField
anchors.verticalCenter: parent.verticalCenter
anchors.left: parent.left
anchors.right: addPeerButton.left
anchors.leftMargin: 10
anchors.rightMargin: 10
editable: true
} }
Button { Button {
@ -889,9 +863,9 @@ ApplicationWindow {
anchors.right: parent.right anchors.right: parent.right
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
anchors.rightMargin: 10 anchors.rightMargin: 10
text: "Add" text: "Connect"
onClicked: { onClicked: {
eth.connectToPeer(addrField.currentText, nodeidField.currentText) eth.connectToPeer(addrField.text)
addPeerWin.visible = false addPeerWin.visible = false
} }
} }

View File

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

View File

@ -2,7 +2,6 @@ package eth
import ( import (
"fmt" "fmt"
"net"
"sync" "sync"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
@ -241,13 +240,12 @@ func (s *Ethereum) Start(seedNode string) error {
return nil return nil
} }
func (self *Ethereum) SuggestPeer(addr string, id discover.NodeID) error { func (self *Ethereum) SuggestPeer(nodeURL string) error {
netaddr, err := net.ResolveTCPAddr("tcp", addr) n, err := discover.ParseNode(nodeURL)
if err != nil { if err != nil {
logger.Errorf("couldn't resolve %s:", addr, err) return fmt.Errorf("invalid node URL: %v", err)
return err
} }
self.net.SuggestPeer(netaddr.IP, netaddr.Port, id) self.net.SuggestPeer(n)
return nil return nil
} }

View File

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

View File

@ -135,8 +135,8 @@ func (srv *Server) PeerCount() int {
// SuggestPeer creates a connection to the given Node if it // SuggestPeer creates a connection to the given Node if it
// is not already connected. // is not already connected.
func (srv *Server) SuggestPeer(ip net.IP, port int, id discover.NodeID) { func (srv *Server) SuggestPeer(n *discover.Node) {
srv.peerConnect <- &discover.Node{ID: id, IP: ip, TCPPort: port} srv.peerConnect <- n
} }
// Broadcast sends an RLP-encoded message to all connected peers. // Broadcast sends an RLP-encoded message to all connected peers.

View File

@ -91,7 +91,7 @@ func TestServerDial(t *testing.T) {
// tell the server to connect // tell the server to connect
tcpAddr := listener.Addr().(*net.TCPAddr) tcpAddr := listener.Addr().(*net.TCPAddr)
srv.peerConnect <- &discover.Node{IP: tcpAddr.IP, TCPPort: tcpAddr.Port} srv.SuggestPeer(&discover.Node{IP: tcpAddr.IP, TCPPort: tcpAddr.Port})
select { select {
case conn := <-accepted: case conn := <-accepted: