From 4ad3734091da59d91747706a5f49ff0a5cfdad92 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Fri, 6 Jan 2023 21:48:12 -0800 Subject: [PATCH] circuitv2: correctly set the transport in the ConnectionState (#1972) --- p2p/protocol/circuitv2/client/conn.go | 24 +++++++++++++++++++--- p2p/protocol/circuitv2/client/transport.go | 6 +++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/p2p/protocol/circuitv2/client/conn.go b/p2p/protocol/circuitv2/client/conn.go index 23bde93d5..ed01be3b3 100644 --- a/p2p/protocol/circuitv2/client/conn.go +++ b/p2p/protocol/circuitv2/client/conn.go @@ -7,6 +7,7 @@ import ( "github.com/libp2p/go-libp2p/core/network" "github.com/libp2p/go-libp2p/core/peer" + tpt "github.com/libp2p/go-libp2p/core/transport" ma "github.com/multiformats/go-multiaddr" manet "github.com/multiformats/go-multiaddr/net" @@ -79,7 +80,7 @@ func (c *Conn) RemoteMultiaddr() ma.Multiaddr { // TODO: We should be able to do this directly without converting to/from a string. relayAddr, err := ma.NewComponent( ma.ProtocolWithCode(ma.P_P2P).Name, - c.stream.Conn().RemotePeer().Pretty(), + c.stream.Conn().RemotePeer().String(), ) if err != nil { panic(err) @@ -102,8 +103,8 @@ func (c *Conn) LocalAddr() net.Addr { func (c *Conn) RemoteAddr() net.Addr { return &NetAddr{ - Relay: c.stream.Conn().RemotePeer().Pretty(), - Remote: c.remote.ID.Pretty(), + Relay: c.stream.Conn().RemotePeer().String(), + Remote: c.remote.ID.String(), } } @@ -143,3 +144,20 @@ func (c *Conn) untagHop() { delete(c.client.hopCount, p) } } + +type capableConnWithStat interface { + tpt.CapableConn + network.ConnStat +} + +type capableConn struct { + capableConnWithStat +} + +var transportName = ma.ProtocolWithCode(ma.P_CIRCUIT).Name + +func (c capableConn) ConnState() network.ConnectionState { + return network.ConnectionState{ + Transport: transportName, + } +} diff --git a/p2p/protocol/circuitv2/client/transport.go b/p2p/protocol/circuitv2/client/transport.go index 97fc1ce1a..5c94fa7eb 100644 --- a/p2p/protocol/circuitv2/client/transport.go +++ b/p2p/protocol/circuitv2/client/transport.go @@ -63,7 +63,11 @@ func (c *Client) Dial(ctx context.Context, a ma.Multiaddr, p peer.ID) (transport return nil, err } conn.tagHop() - return c.upgrader.Upgrade(ctx, c, conn, network.DirOutbound, p, connScope) + cc, err := c.upgrader.Upgrade(ctx, c, conn, network.DirOutbound, p, connScope) + if err != nil { + return nil, err + } + return capableConn{cc.(capableConnWithStat)}, nil } func (c *Client) CanDial(addr ma.Multiaddr) bool {