diff --git a/waku/v2/node/address_test.go b/waku/v2/node/address_test.go index 172f963d3..da48206dc 100644 --- a/waku/v2/node/address_test.go +++ b/waku/v2/node/address_test.go @@ -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}) - - a5NoP2P, _ := decapsulateP2P(a5) + 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(), a5NoP2P.String()) - require.Len(t, multiaddr, 1) // Should only have 1, without circuit relay + require.Equal(t, multiaddr[0].String(), a4NoP2P.String()) + 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)) diff --git a/waku/v2/node/localnode.go b/waku/v2/node/localnode.go index b9f92ead4..acf636f41 100644 --- a/waku/v2/node/localnode.go +++ b/waku/v2/node/localnode.go @@ -191,7 +191,7 @@ func decapsulateCircuitRelayAddr(addr ma.Multiaddr) (ma.Multiaddr, error) { return addr, nil } -func selectWSSListenAddresses(addresses []ma.Multiaddr) ([]ma.Multiaddr, error) { +func selectWSListenAddresses(addresses []ma.Multiaddr) ([]ma.Multiaddr, error) { var result []ma.Multiaddr for _, addr := range addresses { // It's a p2p-circuit address. We dont use these at this stage yet @@ -200,14 +200,9 @@ func selectWSSListenAddresses(addresses []ma.Multiaddr) ([]ma.Multiaddr, error) continue } - // Only WSS with a domain name are allowed - _, err = addr.ValueForProtocol(ma.P_DNS4) - if err != nil { - continue - } - - _, err = addr.ValueForProtocol(ma.P_WSS) - if err != nil { + _, noWS := addr.ValueForProtocol(ma.P_WSS) + _, noWSS := addr.ValueForProtocol(ma.P_WS) + if noWS != nil && noWSS != nil { // Neither WS or WSS found continue } @@ -240,22 +235,22 @@ func (w *WakuNode) getENRAddresses(addrs []ma.Multiaddr) (extAddr *net.TCPAddr, return nil, nil, err } - wssAddrs, err := selectWSSListenAddresses(addrs) + wssAddrs, err := selectWSListenAddresses(addrs) if err != nil { 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 diff --git a/waku/v2/protocol/peer_exchange/enr_cache.go b/waku/v2/protocol/peer_exchange/enr_cache.go index af8f1423b..ae23128d3 100644 --- a/waku/v2/protocol/peer_exchange/enr_cache.go +++ b/waku/v2/protocol/peer_exchange/enr_cache.go @@ -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