p2p: Cherrypick enr inclusion in rlp handshake by @fjl

This commit is contained in:
lash 2019-03-05 16:41:43 +01:00
parent 5697731c0c
commit 2637f249d9
9 changed files with 157 additions and 57 deletions

View file

@ -17,6 +17,7 @@
package p2p package p2p
import ( import (
"encoding/hex"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -429,6 +430,7 @@ type PeerInfo struct {
ID string `json:"id"` // Unique node identifier ID string `json:"id"` // Unique node identifier
Name string `json:"name"` // Name of the node, including client type, version, OS, custom data Name string `json:"name"` // Name of the node, including client type, version, OS, custom data
Caps []string `json:"caps"` // Protocols advertised by this peer Caps []string `json:"caps"` // Protocols advertised by this peer
ENR string `json:"enr"` // Ethereum Node Record
Network struct { Network struct {
LocalAddress string `json:"localAddress"` // Local endpoint of the TCP data connection LocalAddress string `json:"localAddress"` // Local endpoint of the TCP data connection
RemoteAddress string `json:"remoteAddress"` // Remote endpoint of the TCP data connection RemoteAddress string `json:"remoteAddress"` // Remote endpoint of the TCP data connection
@ -454,6 +456,9 @@ func (p *Peer) Info() *PeerInfo {
Caps: caps, Caps: caps,
Protocols: make(map[string]interface{}), Protocols: make(map[string]interface{}),
} }
if enc, err := rlp.EncodeToBytes(p.Node().Record()); err == nil {
info.ENR = "0x" + hex.EncodeToString(enc)
}
info.Network.LocalAddress = p.LocalAddr().String() info.Network.LocalAddress = p.LocalAddr().String()
info.Network.RemoteAddress = p.RemoteAddr().String() info.Network.RemoteAddress = p.RemoteAddr().String()
info.Network.Inbound = p.rw.is(inboundConn) info.Network.Inbound = p.rw.is(inboundConn)

View file

@ -39,6 +39,8 @@ import (
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/ecies" "github.com/ethereum/go-ethereum/crypto/ecies"
"github.com/ethereum/go-ethereum/crypto/secp256k1" "github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/golang/snappy" "github.com/golang/snappy"
"golang.org/x/crypto/sha3" "golang.org/x/crypto/sha3"
@ -77,15 +79,16 @@ var errPlainMessageTooLarge = errors.New("message length >= 16MB")
// rlpx is the transport protocol used by actual (non-test) connections. // rlpx is the transport protocol used by actual (non-test) connections.
// It wraps the frame encoder with locks and read/write deadlines. // It wraps the frame encoder with locks and read/write deadlines.
type rlpx struct { type rlpx struct {
self *enode.Node
fd net.Conn fd net.Conn
rmu, wmu sync.Mutex rmu, wmu sync.Mutex
rw *rlpxFrameRW rw *rlpxFrameRW
} }
func newRLPX(fd net.Conn) transport { func newRLPX(self *enode.Node, fd net.Conn) transport {
fd.SetDeadline(time.Now().Add(handshakeTimeout)) fd.SetDeadline(time.Now().Add(handshakeTimeout))
return &rlpx{fd: fd} return &rlpx{self: self, fd: fd}
} }
func (t *rlpx) ReadMsg() (Msg, error) { func (t *rlpx) ReadMsg() (Msg, error) {
@ -175,23 +178,23 @@ func readProtocolHandshake(rw MsgReader, our *protoHandshake) (*protoHandshake,
// messages. the protocol handshake is the first authenticated message // messages. the protocol handshake is the first authenticated message
// and also verifies whether the encryption handshake 'worked' and the // and also verifies whether the encryption handshake 'worked' and the
// remote side actually provided the right public key. // remote side actually provided the right public key.
func (t *rlpx) doEncHandshake(prv *ecdsa.PrivateKey, dial *ecdsa.PublicKey) (*ecdsa.PublicKey, error) { func (t *rlpx) doEncHandshake(prv *ecdsa.PrivateKey, dial *ecdsa.PublicKey) (*ecdsa.PublicKey, *enode.Node, error) {
var ( var (
sec secrets sec secrets
err error err error
) )
if dial == nil { if dial == nil {
sec, err = receiverEncHandshake(t.fd, prv) sec, err = receiverEncHandshake(t.fd, prv, t.self)
} else { } else {
sec, err = initiatorEncHandshake(t.fd, prv, dial) sec, err = initiatorEncHandshake(t.fd, prv, t.self, dial)
} }
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
t.wmu.Lock() t.wmu.Lock()
t.rw = newRLPXFrameRW(t.fd, sec) t.rw = newRLPXFrameRW(t.fd, sec)
t.wmu.Unlock() t.wmu.Unlock()
return sec.Remote.ExportECDSA(), nil return sec.Remote.ExportECDSA(), sec.RemoteNode, nil
} }
// encHandshake contains the state of the encryption handshake. // encHandshake contains the state of the encryption handshake.
@ -201,6 +204,7 @@ type encHandshake struct {
initNonce, respNonce []byte // nonce initNonce, respNonce []byte // nonce
randomPrivKey *ecies.PrivateKey // ecdhe-random randomPrivKey *ecies.PrivateKey // ecdhe-random
remoteRandomPub *ecies.PublicKey // ecdhe-random-pubk remoteRandomPub *ecies.PublicKey // ecdhe-random-pubk
remoteNode *enr.Record
} }
// secrets represents the connection secrets // secrets represents the connection secrets
@ -210,6 +214,7 @@ type secrets struct {
AES, MAC []byte AES, MAC []byte
EgressMAC, IngressMAC hash.Hash EgressMAC, IngressMAC hash.Hash
Token []byte Token []byte
RemoteNode *enode.Node
} }
// RLPx v4 handshake auth (defined in EIP-8). // RLPx v4 handshake auth (defined in EIP-8).
@ -223,6 +228,7 @@ type authMsgV4 struct {
// Ignore additional fields (forward-compatibility) // Ignore additional fields (forward-compatibility)
Rest []rlp.RawValue `rlp:"tail"` Rest []rlp.RawValue `rlp:"tail"`
// Record *enr.Record -- EIP-XXX RLPx ENR Extension
} }
// RLPx v4 handshake response (defined in EIP-8). // RLPx v4 handshake response (defined in EIP-8).
@ -233,6 +239,7 @@ type authRespV4 struct {
// Ignore additional fields (forward-compatibility) // Ignore additional fields (forward-compatibility)
Rest []rlp.RawValue `rlp:"tail"` Rest []rlp.RawValue `rlp:"tail"`
// Record *enr.Record -- EIP-XXX RLPx ENR Extension
} }
// secrets is called after the handshake is completed. // secrets is called after the handshake is completed.
@ -265,6 +272,14 @@ func (h *encHandshake) secrets(auth, authResp []byte) (secrets, error) {
s.EgressMAC, s.IngressMAC = mac2, mac1 s.EgressMAC, s.IngressMAC = mac2, mac1
} }
// Verify ENR.
if h.remoteNode != nil {
rn, err := enode.New(enode.ValidSchemes, h.remoteNode)
if err != nil {
return s, fmt.Errorf("invalid node record: %v", err)
}
s.RemoteNode = rn
}
return s, nil return s, nil
} }
@ -278,9 +293,9 @@ func (h *encHandshake) staticSharedSecret(prv *ecdsa.PrivateKey) ([]byte, error)
// it should be called on the dialing side of the connection. // it should be called on the dialing side of the connection.
// //
// prv is the local client's private key. // prv is the local client's private key.
func initiatorEncHandshake(conn io.ReadWriter, prv *ecdsa.PrivateKey, remote *ecdsa.PublicKey) (s secrets, err error) { func initiatorEncHandshake(conn io.ReadWriter, prv *ecdsa.PrivateKey, self *enode.Node, remote *ecdsa.PublicKey) (s secrets, err error) {
h := &encHandshake{initiator: true, remote: ecies.ImportECDSAPublic(remote)} h := &encHandshake{initiator: true, remote: ecies.ImportECDSAPublic(remote)}
authMsg, err := h.makeAuthMsg(prv) authMsg, err := h.makeAuthMsg(prv, self)
if err != nil { if err != nil {
return s, err return s, err
} }
@ -304,7 +319,7 @@ func initiatorEncHandshake(conn io.ReadWriter, prv *ecdsa.PrivateKey, remote *ec
} }
// makeAuthMsg creates the initiator handshake message. // makeAuthMsg creates the initiator handshake message.
func (h *encHandshake) makeAuthMsg(prv *ecdsa.PrivateKey) (*authMsgV4, error) { func (h *encHandshake) makeAuthMsg(prv *ecdsa.PrivateKey, self *enode.Node) (*authMsgV4, error) {
// Generate random initiator nonce. // Generate random initiator nonce.
h.initNonce = make([]byte, shaLen) h.initNonce = make([]byte, shaLen)
_, err := rand.Read(h.initNonce) _, err := rand.Read(h.initNonce)
@ -333,12 +348,20 @@ func (h *encHandshake) makeAuthMsg(prv *ecdsa.PrivateKey) (*authMsgV4, error) {
copy(msg.InitiatorPubkey[:], crypto.FromECDSAPub(&prv.PublicKey)[1:]) copy(msg.InitiatorPubkey[:], crypto.FromECDSAPub(&prv.PublicKey)[1:])
copy(msg.Nonce[:], h.initNonce) copy(msg.Nonce[:], h.initNonce)
msg.Version = 4 msg.Version = 4
// Encode node record.
record, err := rlp.EncodeToBytes(self.Record())
if err != nil {
return nil, err
}
msg.Rest = []rlp.RawValue{record}
return msg, nil return msg, nil
} }
func (h *encHandshake) handleAuthResp(msg *authRespV4) (err error) { func (h *encHandshake) handleAuthResp(msg *authRespV4) (err error) {
h.respNonce = msg.Nonce[:] h.respNonce = msg.Nonce[:]
h.remoteRandomPub, err = importPublicKey(msg.RandomPubkey[:]) h.remoteRandomPub, err = importPublicKey(msg.RandomPubkey[:])
h.remoteNode = recordFromTail(msg.Rest)
return err return err
} }
@ -346,7 +369,7 @@ func (h *encHandshake) handleAuthResp(msg *authRespV4) (err error) {
// it should be called on the listening side of the connection. // it should be called on the listening side of the connection.
// //
// prv is the local client's private key. // prv is the local client's private key.
func receiverEncHandshake(conn io.ReadWriter, prv *ecdsa.PrivateKey) (s secrets, err error) { func receiverEncHandshake(conn io.ReadWriter, prv *ecdsa.PrivateKey, self *enode.Node) (s secrets, err error) {
authMsg := new(authMsgV4) authMsg := new(authMsgV4)
authPacket, err := readHandshakeMsg(authMsg, encAuthMsgLen, prv, conn) authPacket, err := readHandshakeMsg(authMsg, encAuthMsgLen, prv, conn)
if err != nil { if err != nil {
@ -357,7 +380,7 @@ func receiverEncHandshake(conn io.ReadWriter, prv *ecdsa.PrivateKey) (s secrets,
return s, err return s, err
} }
authRespMsg, err := h.makeAuthResp() authRespMsg, err := h.makeAuthResp(self)
if err != nil { if err != nil {
return s, err return s, err
} }
@ -405,10 +428,11 @@ func (h *encHandshake) handleAuthMsg(msg *authMsgV4, prv *ecdsa.PrivateKey) erro
return err return err
} }
h.remoteRandomPub, _ = importPublicKey(remoteRandomPub) h.remoteRandomPub, _ = importPublicKey(remoteRandomPub)
h.remoteNode = recordFromTail(msg.Rest)
return nil return nil
} }
func (h *encHandshake) makeAuthResp() (msg *authRespV4, err error) { func (h *encHandshake) makeAuthResp(self *enode.Node) (msg *authRespV4, err error) {
// Generate random nonce. // Generate random nonce.
h.respNonce = make([]byte, shaLen) h.respNonce = make([]byte, shaLen)
if _, err = rand.Read(h.respNonce); err != nil { if _, err = rand.Read(h.respNonce); err != nil {
@ -419,6 +443,13 @@ func (h *encHandshake) makeAuthResp() (msg *authRespV4, err error) {
copy(msg.Nonce[:], h.respNonce) copy(msg.Nonce[:], h.respNonce)
copy(msg.RandomPubkey[:], exportPubkey(&h.randomPrivKey.PublicKey)) copy(msg.RandomPubkey[:], exportPubkey(&h.randomPrivKey.PublicKey))
msg.Version = 4 msg.Version = 4
// Encode node record.
record, err := rlp.EncodeToBytes(self.Record())
if err != nil {
return nil, err
}
msg.Rest = []rlp.RawValue{record}
return msg, nil return msg, nil
} }
@ -454,6 +485,17 @@ func (msg *authRespV4) decodePlain(input []byte) {
msg.Version = 4 msg.Version = 4
} }
func recordFromTail(tail []rlp.RawValue) *enr.Record {
if len(tail) == 0 {
return nil
}
var r enr.Record
if err := rlp.DecodeBytes(tail[0], &r); err != nil {
return nil
}
return &r
}
var padSpace = make([]byte, 300) var padSpace = make([]byte, 300)
func sealEIP8(msg interface{}, h *encHandshake) ([]byte, error) { func sealEIP8(msg interface{}, h *encHandshake) ([]byte, error) {

View file

@ -34,6 +34,8 @@ import (
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/ecies" "github.com/ethereum/go-ethereum/crypto/ecies"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/ethereum/go-ethereum/p2p/simulations/pipes" "github.com/ethereum/go-ethereum/p2p/simulations/pipes"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"golang.org/x/crypto/sha3" "golang.org/x/crypto/sha3"
@ -82,13 +84,36 @@ func testEncHandshake(token []byte) error {
type result struct { type result struct {
side string side string
pubkey *ecdsa.PublicKey pubkey *ecdsa.PublicKey
enode *enode.Node
err error err error
} }
var ( var (
prv0, _ = crypto.GenerateKey() prv0, _ = crypto.GenerateKey()
prv1, _ = crypto.GenerateKey() prv1, _ = crypto.GenerateKey()
)
var rec0 enr.Record
err := enode.SignV4(&rec0, prv0)
if err != nil {
return err
}
enode0, err := enode.New(enode.V4ID{}, &rec0)
if err != nil {
return err
}
var rec1 enr.Record
err = enode.SignV4(&rec1, prv1)
if err != nil {
return err
}
enode1, err := enode.New(enode.V4ID{}, &rec1)
if err != nil {
return err
}
var (
fd0, fd1 = net.Pipe() fd0, fd1 = net.Pipe()
c0, c1 = newRLPX(fd0).(*rlpx), newRLPX(fd1).(*rlpx) c0, c1 = newRLPX(enode0, fd0).(*rlpx), newRLPX(enode1, fd1).(*rlpx)
output = make(chan result) output = make(chan result)
) )
@ -97,7 +122,7 @@ func testEncHandshake(token []byte) error {
defer func() { output <- r }() defer func() { output <- r }()
defer fd0.Close() defer fd0.Close()
r.pubkey, r.err = c0.doEncHandshake(prv0, &prv1.PublicKey) r.pubkey, r.enode, r.err = c0.doEncHandshake(prv0, &prv1.PublicKey)
if r.err != nil { if r.err != nil {
return return
} }
@ -110,7 +135,7 @@ func testEncHandshake(token []byte) error {
defer func() { output <- r }() defer func() { output <- r }()
defer fd1.Close() defer fd1.Close()
r.pubkey, r.err = c1.doEncHandshake(prv1, nil) r.pubkey, r.enode, r.err = c1.doEncHandshake(prv1, nil)
if r.err != nil { if r.err != nil {
return return
} }
@ -157,6 +182,26 @@ func TestProtocolHandshake(t *testing.T) {
wg sync.WaitGroup wg sync.WaitGroup
) )
var rec0 enr.Record
err := enode.SignV4(&rec0, prv0)
if err != nil {
t.Fatal(err)
}
enode0, err := enode.New(enode.V4ID{}, &rec0)
if err != nil {
t.Fatal(err)
}
var rec1 enr.Record
err = enode.SignV4(&rec1, prv1)
if err != nil {
t.Fatal(err)
}
enode1, err := enode.New(enode.V4ID{}, &rec1)
if err != nil {
t.Fatal(err)
}
fd0, fd1, err := pipes.TCPPipe() fd0, fd1, err := pipes.TCPPipe()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -166,8 +211,8 @@ func TestProtocolHandshake(t *testing.T) {
go func() { go func() {
defer wg.Done() defer wg.Done()
defer fd0.Close() defer fd0.Close()
rlpx := newRLPX(fd0) rlpx := newRLPX(enode0, fd0)
rpubkey, err := rlpx.doEncHandshake(prv0, &prv1.PublicKey) rpubkey, _, err := rlpx.doEncHandshake(prv0, &prv1.PublicKey)
if err != nil { if err != nil {
t.Errorf("dial side enc handshake failed: %v", err) t.Errorf("dial side enc handshake failed: %v", err)
return return
@ -192,8 +237,8 @@ func TestProtocolHandshake(t *testing.T) {
go func() { go func() {
defer wg.Done() defer wg.Done()
defer fd1.Close() defer fd1.Close()
rlpx := newRLPX(fd1) rlpx := newRLPX(enode1, fd1)
rpubkey, err := rlpx.doEncHandshake(prv1, nil) rpubkey, _, err := rlpx.doEncHandshake(prv1, nil)
if err != nil { if err != nil {
t.Errorf("listen side enc handshake failed: %v", err) t.Errorf("listen side enc handshake failed: %v", err)
return return

View file

@ -157,7 +157,7 @@ type Server struct {
// Hooks for testing. These are useful because we can inhibit // Hooks for testing. These are useful because we can inhibit
// the whole protocol stack. // the whole protocol stack.
newTransport func(net.Conn) transport newTransport func(self *enode.Node, fd net.Conn) transport
newPeerHook func(*Peer) newPeerHook func(*Peer)
lock sync.Mutex // protects running lock sync.Mutex // protects running
@ -219,7 +219,7 @@ type conn struct {
type transport interface { type transport interface {
// The two handshakes. // The two handshakes.
doEncHandshake(prv *ecdsa.PrivateKey, dialDest *ecdsa.PublicKey) (*ecdsa.PublicKey, error) doEncHandshake(prv *ecdsa.PrivateKey, dialDest *ecdsa.PublicKey) (*ecdsa.PublicKey, *enode.Node, error)
doProtoHandshake(our *protoHandshake) (*protoHandshake, error) doProtoHandshake(our *protoHandshake) (*protoHandshake, error)
// The MsgReadWriter can only be used after the encryption // The MsgReadWriter can only be used after the encryption
// handshake has completed. The code uses conn.id to track this // handshake has completed. The code uses conn.id to track this
@ -880,7 +880,7 @@ func (srv *Server) listenLoop() {
// as a peer. It returns when the connection has been added as a peer // as a peer. It returns when the connection has been added as a peer
// or the handshakes have failed. // or the handshakes have failed.
func (srv *Server) SetupConn(fd net.Conn, flags connFlag, dialDest *enode.Node) error { func (srv *Server) SetupConn(fd net.Conn, flags connFlag, dialDest *enode.Node) error {
c := &conn{fd: fd, transport: srv.newTransport(fd), flags: flags, cont: make(chan error)} c := &conn{fd: fd, transport: srv.newTransport(srv.Self(), fd), flags: flags, cont: make(chan error)}
err := srv.setupConn(c, flags, dialDest) err := srv.setupConn(c, flags, dialDest)
if err != nil { if err != nil {
c.close(err) c.close(err)
@ -906,7 +906,7 @@ func (srv *Server) setupConn(c *conn, flags connFlag, dialDest *enode.Node) erro
} }
} }
// Run the encryption handshake. // Run the encryption handshake.
remotePubkey, err := c.doEncHandshake(srv.PrivateKey, dialPubkey) remotePubkey, rlpxNode, err := c.doEncHandshake(srv.PrivateKey, dialPubkey)
if err != nil { if err != nil {
srv.log.Trace("Failed RLPx handshake", "addr", c.fd.RemoteAddr(), "conn", c.flags, "err", err) srv.log.Trace("Failed RLPx handshake", "addr", c.fd.RemoteAddr(), "conn", c.flags, "err", err)
return err return err
@ -916,10 +916,8 @@ func (srv *Server) setupConn(c *conn, flags connFlag, dialDest *enode.Node) erro
if dialPubkey.X.Cmp(remotePubkey.X) != 0 || dialPubkey.Y.Cmp(remotePubkey.Y) != 0 { if dialPubkey.X.Cmp(remotePubkey.X) != 0 || dialPubkey.Y.Cmp(remotePubkey.Y) != 0 {
return DiscUnexpectedIdentity return DiscUnexpectedIdentity
} }
c.node = dialDest
} else {
c.node = nodeFromConn(remotePubkey, c.fd)
} }
c.node = handshakeNode(dialDest, rlpxNode, remotePubkey, c.fd)
if conn, ok := c.fd.(*meteredConn); ok { if conn, ok := c.fd.(*meteredConn); ok {
conn.handshakeDone(c.node.ID()) conn.handshakeDone(c.node.ID())
} }
@ -951,7 +949,11 @@ func (srv *Server) setupConn(c *conn, flags connFlag, dialDest *enode.Node) erro
return nil return nil
} }
func nodeFromConn(pubkey *ecdsa.PublicKey, conn net.Conn) *enode.Node { // handshakeNode decides which node record is stored in Peer after the handshake.
func handshakeNode(dialDest, rlpxNode *enode.Node, pubkey *ecdsa.PublicKey, conn net.Conn) *enode.Node {
switch {
case dialDest == nil && rlpxNode == nil:
// Fallback: no node record available, fake it
var ip net.IP var ip net.IP
var port int var port int
if tcp, ok := conn.RemoteAddr().(*net.TCPAddr); ok { if tcp, ok := conn.RemoteAddr().(*net.TCPAddr); ok {
@ -959,6 +961,16 @@ func nodeFromConn(pubkey *ecdsa.PublicKey, conn net.Conn) *enode.Node {
port = tcp.Port port = tcp.Port
} }
return enode.NewV4(pubkey, ip, port, port) return enode.NewV4(pubkey, ip, port, port)
case dialDest == nil:
return rlpxNode
case rlpxNode == nil:
return dialDest
default:
if rlpxNode.Seq() > dialDest.Seq() {
return rlpxNode
}
return dialDest
}
} }
func truncateName(s string) string { func truncateName(s string) string {

View file

@ -44,7 +44,8 @@ type testTransport struct {
} }
func newTestTransport(rpub *ecdsa.PublicKey, fd net.Conn) transport { func newTestTransport(rpub *ecdsa.PublicKey, fd net.Conn) transport {
wrapped := newRLPX(fd).(*rlpx) enod := enode.NewV4(rpub, nil, 0, 0)
wrapped := newRLPX(enod, fd).(*rlpx)
wrapped.rw = newRLPXFrameRW(fd, secrets{ wrapped.rw = newRLPXFrameRW(fd, secrets{
MAC: zero16, MAC: zero16,
AES: zero16, AES: zero16,
@ -54,8 +55,8 @@ func newTestTransport(rpub *ecdsa.PublicKey, fd net.Conn) transport {
return &testTransport{rpub: rpub, rlpx: wrapped} return &testTransport{rpub: rpub, rlpx: wrapped}
} }
func (c *testTransport) doEncHandshake(prv *ecdsa.PrivateKey, dialDest *ecdsa.PublicKey) (*ecdsa.PublicKey, error) { func (c *testTransport) doEncHandshake(prv *ecdsa.PrivateKey, dialDest *ecdsa.PublicKey) (*ecdsa.PublicKey, *enode.Node, error) {
return c.rpub, nil return c.rpub, nil, nil
} }
func (c *testTransport) doProtoHandshake(our *protoHandshake) (*protoHandshake, error) { func (c *testTransport) doProtoHandshake(our *protoHandshake) (*protoHandshake, error) {
@ -78,7 +79,7 @@ func startTestServer(t *testing.T, remoteKey *ecdsa.PublicKey, pf func(*Peer)) *
server := &Server{ server := &Server{
Config: config, Config: config,
newPeerHook: pf, newPeerHook: pf,
newTransport: func(fd net.Conn) transport { return newTestTransport(remoteKey, fd) }, newTransport: func(enod *enode.Node, fd net.Conn) transport { return newTestTransport(remoteKey, fd) },
} }
if err := server.Start(); err != nil { if err := server.Start(); err != nil {
t.Fatalf("Could not start server: %v", err) t.Fatalf("Could not start server: %v", err)
@ -435,7 +436,7 @@ func TestServerPeerLimits(t *testing.T) {
NoDial: true, NoDial: true,
Protocols: []Protocol{discard}, Protocols: []Protocol{discard},
}, },
newTransport: func(fd net.Conn) transport { return tp }, newTransport: func(enod *enode.Node, fd net.Conn) transport { return tp },
log: log.New(), log: log.New(),
} }
if err := srv.Start(); err != nil { if err := srv.Start(); err != nil {
@ -548,7 +549,7 @@ func TestServerSetupConn(t *testing.T) {
NoDial: true, NoDial: true,
Protocols: []Protocol{discard}, Protocols: []Protocol{discard},
}, },
newTransport: func(fd net.Conn) transport { return test.tt }, newTransport: func(enod *enode.Node, fd net.Conn) transport { return test.tt },
log: log.New(), log: log.New(),
} }
if !test.dontstart { if !test.dontstart {
@ -577,9 +578,9 @@ type setupTransport struct {
closeErr error closeErr error
} }
func (c *setupTransport) doEncHandshake(prv *ecdsa.PrivateKey, dialDest *ecdsa.PublicKey) (*ecdsa.PublicKey, error) { func (c *setupTransport) doEncHandshake(prv *ecdsa.PrivateKey, dialDest *ecdsa.PublicKey) (*ecdsa.PublicKey, *enode.Node, error) {
c.calls += "doEncHandshake," c.calls += "doEncHandshake,"
return c.pubkey, c.encHandshakeErr return c.pubkey, nil, c.encHandshakeErr
} }
func (c *setupTransport) doProtoHandshake(our *protoHandshake) (*protoHandshake, error) { func (c *setupTransport) doProtoHandshake(our *protoHandshake) (*protoHandshake, error) {

View file

@ -117,7 +117,6 @@ func (s *SimAdapter) NewNode(config *NodeConfig) (Node, error) {
// an in-memory net.Pipe // an in-memory net.Pipe
func (s *SimAdapter) Dial(dest *enode.Node) (conn net.Conn, err error) { func (s *SimAdapter) Dial(dest *enode.Node) (conn net.Conn, err error) {
node, ok := s.GetNode(dest.ID()) node, ok := s.GetNode(dest.ID())
log.Debug("destnode", "dest", dest)
if !ok { if !ok {
return nil, fmt.Errorf("unknown node: %s", dest.ID()) return nil, fmt.Errorf("unknown node: %s", dest.ID())
} }
@ -133,7 +132,7 @@ func (s *SimAdapter) Dial(dest *enode.Node) (conn net.Conn, err error) {
// this is simulated 'listening' // this is simulated 'listening'
// asynchronously call the dialed destination node's p2p server // asynchronously call the dialed destination node's p2p server
// to set up connection on the 'listening' side // to set up connection on the 'listening' side
go srv.SetupConn(pipe1, 0, dest) go srv.SetupConn(pipe1, 0, nil)
return pipe2, nil return pipe2, nil
} }

View file

@ -30,6 +30,7 @@ import (
"github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
) )
@ -99,12 +100,13 @@ type NodeConfig struct {
// services registered by calling the RegisterService function) // services registered by calling the RegisterService function)
Services []string Services []string
// Node record
Record enr.Record
// function to sanction or prevent suggesting a peer // function to sanction or prevent suggesting a peer
Reachable func(id enode.ID) bool Reachable func(id enode.ID) bool
Port uint16 Port uint16
EnodeFunc func() *enode.Node
} }
// nodeConfigJSON is used to encode and decode NodeConfig as JSON by encoding // nodeConfigJSON is used to encode and decode NodeConfig as JSON by encoding
@ -170,9 +172,6 @@ func (n *NodeConfig) UnmarshalJSON(data []byte) error {
// Node returns the node descriptor represented by the config. // Node returns the node descriptor represented by the config.
func (n *NodeConfig) Node() *enode.Node { func (n *NodeConfig) Node() *enode.Node {
if n.EnodeFunc != nil {
return n.EnodeFunc()
}
return enode.NewV4(&n.PrivateKey.PublicKey, net.IP{127, 0, 0, 1}, int(n.Port), int(n.Port)) return enode.NewV4(&n.PrivateKey.PublicKey, net.IP{127, 0, 0, 1}, int(n.Port), int(n.Port))
} }

View file

@ -303,19 +303,17 @@ func (net *Network) Connect(oneID, otherID enode.ID) error {
} }
func (net *Network) connect(oneID, otherID enode.ID) error { func (net *Network) connect(oneID, otherID enode.ID) error {
log.Debug("Connecting nodes with addPeer", "id", oneID, "other", otherID)
conn, err := net.initConn(oneID, otherID) conn, err := net.initConn(oneID, otherID)
if err != nil { if err != nil {
log.Error("connect", "init", err.Error())
return err return err
} }
client, err := conn.one.Client() client, err := conn.one.Client()
if err != nil { if err != nil {
log.Error("connect", "client", err.Error())
return err return err
} }
net.events.Send(ControlEvent(conn)) net.events.Send(ControlEvent(conn))
s := string(conn.other.Addr()) return client.Call(nil, "admin_addPeer", string(conn.other.Addr()))
return client.Call(nil, "admin_addPeer", s)
} }
// Disconnect disconnects two nodes by calling the "admin_removePeer" RPC // Disconnect disconnects two nodes by calling the "admin_removePeer" RPC

View file

@ -405,7 +405,6 @@ func decodeByteSlice(s *Stream, val reflect.Value) error {
} }
func decodeByteArray(s *Stream, val reflect.Value) error { func decodeByteArray(s *Stream, val reflect.Value) error {
log.Warn("in decodebytarray", "v", val)
kind, size, err := s.Kind() kind, size, err := s.Kind()
if err != nil { if err != nil {
return err return err