p2p: move rlpx into separate package (#21464)
This change moves the RLPx protocol implementation into a separate package, p2p/rlpx. The new package can be used to establish RLPx connections for protocol testing purposes. Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
@ -166,7 +166,7 @@ type Server struct {
|
||||
|
||||
// Hooks for testing. These are useful because we can inhibit
|
||||
// the whole protocol stack.
|
||||
newTransport func(net.Conn) transport
|
||||
newTransport func(net.Conn, *ecdsa.PublicKey) transport
|
||||
newPeerHook func(*Peer)
|
||||
listenFunc func(network, addr string) (net.Listener, error)
|
||||
|
||||
@ -231,7 +231,7 @@ type conn struct {
|
||||
|
||||
type transport interface {
|
||||
// The two handshakes.
|
||||
doEncHandshake(prv *ecdsa.PrivateKey, dialDest *ecdsa.PublicKey) (*ecdsa.PublicKey, error)
|
||||
doEncHandshake(prv *ecdsa.PrivateKey) (*ecdsa.PublicKey, error)
|
||||
doProtoHandshake(our *protoHandshake) (*protoHandshake, error)
|
||||
// The MsgReadWriter can only be used after the encryption
|
||||
// handshake has completed. The code uses conn.id to track this
|
||||
@ -914,7 +914,13 @@ func (srv *Server) checkInboundConn(fd net.Conn, remoteIP net.IP) error {
|
||||
// as a peer. It returns when the connection has been added as a peer
|
||||
// or the handshakes have failed.
|
||||
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, flags: flags, cont: make(chan error)}
|
||||
if dialDest == nil {
|
||||
c.transport = srv.newTransport(fd, nil)
|
||||
} else {
|
||||
c.transport = srv.newTransport(fd, dialDest.Pubkey())
|
||||
}
|
||||
|
||||
err := srv.setupConn(c, flags, dialDest)
|
||||
if err != nil {
|
||||
c.close(err)
|
||||
@ -943,16 +949,12 @@ func (srv *Server) setupConn(c *conn, flags connFlag, dialDest *enode.Node) erro
|
||||
}
|
||||
|
||||
// Run the RLPx handshake.
|
||||
remotePubkey, err := c.doEncHandshake(srv.PrivateKey, dialPubkey)
|
||||
remotePubkey, err := c.doEncHandshake(srv.PrivateKey)
|
||||
if err != nil {
|
||||
srv.log.Trace("Failed RLPx handshake", "addr", c.fd.RemoteAddr(), "conn", c.flags, "err", err)
|
||||
return err
|
||||
}
|
||||
if dialDest != nil {
|
||||
// For dialed connections, check that the remote public key matches.
|
||||
if dialPubkey.X.Cmp(remotePubkey.X) != 0 || dialPubkey.Y.Cmp(remotePubkey.Y) != 0 {
|
||||
return DiscUnexpectedIdentity
|
||||
}
|
||||
c.node = dialDest
|
||||
} else {
|
||||
c.node = nodeFromConn(remotePubkey, c.fd)
|
||||
|
Reference in New Issue
Block a user