Skip to content

Commit

Permalink
eth: parse quic ENR entry
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis-tra committed Oct 10, 2024
1 parent 8548260 commit 38e4489
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
28 changes: 24 additions & 4 deletions discv5/crawler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/cenkalti/backoff/v4"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
basichost "github.com/libp2p/go-libp2p/p2p/host/basic"
ma "github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -65,6 +64,10 @@ func (c *Crawler) Work(ctx context.Context, task PeerInfo) (core.CrawlResult[Pee

properties := c.PeerProperties(task.Node)

if libp2pResult.Transport != "" {
properties["transport"] = libp2pResult.Transport
}

if libp2pResult.ConnClosedImmediately {
properties["direct_close"] = true
}
Expand Down Expand Up @@ -174,8 +177,9 @@ type Libp2pResult struct {
Agent string
Protocols []string
ListenAddrs []ma.Multiaddr
ConnClosedImmediately bool // whether conn was no error but still unconnected
GenTCPAddr bool // whether a TCP address was generated
Transport string // the transport of a successful connection
ConnClosedImmediately bool // whether conn was no error but still unconnected
GenTCPAddr bool // whether a TCP address was generated
}

func (c *Crawler) crawlLibp2p(ctx context.Context, pi PeerInfo) chan Libp2pResult {
Expand Down Expand Up @@ -203,8 +207,10 @@ func (c *Crawler) crawlLibp2p(ctx context.Context, pi PeerInfo) chan Libp2pResul
// If we could successfully connect to the peer we actually crawl it.
if result.ConnectError == nil {

conns := c.host.Network().ConnsToPeer(pi.ID())

// check if we're connected
if c.host.Network().Connectedness(pi.ID()) == network.NotConnected {
if len(conns) == 0 {
// this is a weird behavior I was obesrving. Libp2p reports a
// successful connection establishment but isn't connected right
// after the call returned. This point is not a big problem at this
Expand All @@ -217,6 +223,20 @@ func (c *Crawler) crawlLibp2p(ctx context.Context, pi PeerInfo) chan Libp2pResul
if !c.isIdentified(addrInfo.ID) {
_ = c.connect(ctx, addrInfo)
}
} else if len(conns) == 1 {
// handle happy-path separately
result.Transport = conns[0].ConnState().Transport
} else {
transports := map[string]struct{}{}
for _, conn := range conns {
transports[conn.ConnState().Transport] = struct{}{}
}

if len(transports) == 1 {
result.Transport = conns[0].ConnState().Transport
} else if len(transports) != 0 {
result.Transport = "multi"
}
}

// wait for the Identify exchange to complete
Expand Down
17 changes: 17 additions & 0 deletions discv5/driver_crawler.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ func NewPeerInfo(node *enode.Node) (PeerInfo, error) {
maddrs = append(maddrs, maddr)
}

if quicAddr, ok := node.QUICEndpoint(); ok {
addr := quicAddr.Addr()
if addr.Is4() {
ipScheme = "ip4"
} else {
ipScheme = "ip6"
}

maddrStr := fmt.Sprintf("/%s/%s/udp/%d/quic-v1", ipScheme, addr.String(), quicAddr.Port())
maddr, err := ma.NewMultiaddr(maddrStr)
if err != nil {
return PeerInfo{}, fmt.Errorf("parse multiaddress %s: %w", maddrStr, err)
}

maddrs = append(maddrs, maddr)
}

pi := PeerInfo{
Node: node,
peerID: peerID,
Expand Down

0 comments on commit 38e4489

Please sign in to comment.