Skip to content

Commit

Permalink
feat: prefer circuit relay addr over ws, and update cache with enrs w…
Browse files Browse the repository at this point in the history
…ith newer seq number
  • Loading branch information
richard-ramos committed Oct 16, 2023
1 parent 65aa242 commit 66577db
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
11 changes: 5 additions & 6 deletions waku/v2/node/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,20 @@ func TestExternalAddressSelection(t *testing.T) {
a9, _ := ma.NewMultiaddr("/dns4/node-02.gc-us-central1-a.status.prod.statusim.net/tcp/443/wss/p2p/16Uiu2HAmDQugwDHM3YeUp86iGjrUvbdw3JPRgikC7YoGBsT2ymMg/p2p-circuit/p2p/16Uiu2HAmUVVrJo1KMw4QwUANYF7Ws4mfcRqf9xHaaGP87GbMuY2f") // VALID
a10, _ := ma.NewMultiaddr("/dns4/node-01.gc-us-central1-a.wakuv2.test.statusim.net/tcp/8000/wss/p2p/16Uiu2HAmJb2e28qLXxT5kZxVUUoJt72EMzNGXB47Rxx5hw3q4YjS/p2p-circuit/p2p/16Uiu2HAmUVVrJo1KMw4QwUANYF7Ws4mfcRqf9xHaaGP87GbMuY2f") // VALID
a11, _ := ma.NewMultiaddr("/dns4/node-01.gc-us-central1-a.wakuv2.test.statusim.net/tcp/30303/p2p/16Uiu2HAmJb2e28qLXxT5kZxVUUoJt72EMzNGXB47Rxx5hw3q4YjS/p2p-circuit/p2p/16Uiu2HAmUVVrJo1KMw4QwUANYF7Ws4mfcRqf9xHaaGP87GbMuY2f") // VALID
a12, _ := ma.NewMultiaddr("/ip4/188.23.1.8/tcp/30303/p2p/16Uiu2HAmUVVrJo1KMw4QwUANYF7Ws4mfcRqf9xHaaGP87GbMuY2f") // VALID

addrs := []ma.Multiaddr{a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11}
addrs := []ma.Multiaddr{a1, a2, a3, a4, a5, a6, a7}

w := &WakuNode{}
extAddr, multiaddr, err := w.getENRAddresses([]ma.Multiaddr{a1, a2, a3, a4, a5, a6, a7, a8})

extAddr, multiaddr, err := w.getENRAddresses([]ma.Multiaddr{a1, a2, a3, a4, a5, a6, a7})
a4NoP2P, _ := decapsulateP2P(a4)
require.NoError(t, err)
require.Equal(t, extAddr.IP, net.IPv4(192, 168, 0, 106))
require.Equal(t, extAddr.Port, 60000)
require.Equal(t, multiaddr[0].String(), a4NoP2P.String())
require.Len(t, multiaddr, 4) // Should only have 4, without circuit relay
require.Len(t, multiaddr, 4)

a12, _ := ma.NewMultiaddr("/ip4/188.23.1.8/tcp/30303/p2p/16Uiu2HAmUVVrJo1KMw4QwUANYF7Ws4mfcRqf9xHaaGP87GbMuY2f") // VALID
addrs = append(addrs, a12)
addrs = append(addrs, a8, a9, a10, a11, a12)
extAddr, _, err = w.getENRAddresses(addrs)
require.NoError(t, err)
require.Equal(t, extAddr.IP, net.IPv4(188, 23, 1, 8))
Expand Down
18 changes: 9 additions & 9 deletions waku/v2/node/localnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,17 +240,17 @@ func (w *WakuNode) getENRAddresses(addrs []ma.Multiaddr) (extAddr *net.TCPAddr,
return nil, nil, err
}

multiaddr = append(multiaddr, wssAddrs...)

// to use WSS, you should have a valid certificate with a domain name.
// that means you're reachable. So circuit relay addresses are ignored
if len(wssAddrs) == 0 {
circuitAddrs, err := selectCircuitRelayListenAddresses(addrs)
if err != nil {
return nil, nil, err
}
circuitAddrs, err := selectCircuitRelayListenAddresses(addrs)
if err != nil {
return nil, nil, err
}

if len(circuitAddrs) != 0 {
// Node is unreachable, hence why we have circuit relay multiaddr
// We prefer these instead of any ws/s address
multiaddr = append(multiaddr, circuitAddrs...)
} else {
multiaddr = append(multiaddr, wssAddrs...)
}

return
Expand Down
5 changes: 4 additions & 1 deletion waku/v2/protocol/peer_exchange/enr_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ func newEnrCache(size int) (*enrCache, error) {
func (c *enrCache) updateCache(node *enode.Node) {
c.mu.Lock()
defer c.mu.Unlock()
c.data.Add(node.ID(), node)
currNode, ok := c.data.Get(node.ID())
if !ok || node.Seq() > currNode.(*enode.Node).Seq() {
c.data.Add(node.ID(), node)
}
}

// get `numPeers` records of enr
Expand Down

0 comments on commit 66577db

Please sign in to comment.