fix: use IP addresses instead of dns to store multiaddresses (#1134)

Co-authored-by: Prem Chaitanya Prathi <chaitanyaprem@gmail.com>
This commit is contained in:
richΛrd 2024-06-20 03:52:43 -04:00 committed by GitHub
parent 93331b483e
commit 8303c592d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 20 deletions

View File

@ -1,6 +1,7 @@
package node package node
import ( import (
"context"
"net" "net"
"testing" "testing"
@ -25,7 +26,7 @@ func TestExternalAddressSelection(t *testing.T) {
addrs := []ma.Multiaddr{a1, a2, a3, a4, a5, a6, a7} addrs := []ma.Multiaddr{a1, a2, a3, a4, a5, a6, a7}
w := &WakuNode{} w := &WakuNode{}
extAddr, multiaddr, err := w.getENRAddresses([]ma.Multiaddr{a1, a2, a3, a4, a5, a6, a7}) extAddr, multiaddr, err := w.getENRAddresses(context.Background(), []ma.Multiaddr{a1, a2, a3, a4, a5, a6, a7})
a4NoP2P, _ := decapsulateP2P(a4) a4NoP2P, _ := decapsulateP2P(a4)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, extAddr.IP, net.IPv4(192, 168, 0, 106)) require.Equal(t, extAddr.IP, net.IPv4(192, 168, 0, 106))
@ -34,13 +35,13 @@ func TestExternalAddressSelection(t *testing.T) {
require.Len(t, multiaddr, 4) require.Len(t, multiaddr, 4)
addrs = append(addrs, a8, a9, a10, a11, a12) addrs = append(addrs, a8, a9, a10, a11, a12)
extAddr, _, err = w.getENRAddresses(addrs) extAddr, _, err = w.getENRAddresses(context.Background(), addrs)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, extAddr.IP, net.IPv4(188, 23, 1, 8)) require.Equal(t, extAddr.IP, net.IPv4(188, 23, 1, 8))
require.Equal(t, extAddr.Port, 30303) require.Equal(t, extAddr.Port, 30303)
a8RelayNode, _ := decapsulateCircuitRelayAddr(a8) a8RelayNode, _ := decapsulateCircuitRelayAddr(context.Background(), a8)
_, multiaddr, err = w.getENRAddresses([]ma.Multiaddr{a1, a8}) _, multiaddr, err = w.getENRAddresses(context.Background(), []ma.Multiaddr{a1, a8})
require.NoError(t, err) require.NoError(t, err)
require.Len(t, multiaddr, 1) require.Len(t, multiaddr, 1)
require.Equal(t, multiaddr[0].String(), a8RelayNode.String()) // Should have included circuit-relay addr require.Equal(t, multiaddr[0].String(), a8RelayNode.String()) // Should have included circuit-relay addr

View File

@ -11,6 +11,7 @@ import (
"github.com/libp2p/go-libp2p/core/event" "github.com/libp2p/go-libp2p/core/event"
"github.com/multiformats/go-multiaddr" "github.com/multiformats/go-multiaddr"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
madns "github.com/multiformats/go-multiaddr-dns"
"github.com/waku-org/go-waku/waku/v2/protocol" "github.com/waku-org/go-waku/waku/v2/protocol"
wenr "github.com/waku-org/go-waku/waku/v2/protocol/enr" wenr "github.com/waku-org/go-waku/waku/v2/protocol/enr"
"github.com/waku-org/go-waku/waku/v2/protocol/relay" "github.com/waku-org/go-waku/waku/v2/protocol/relay"
@ -183,7 +184,7 @@ func decapsulateP2P(addr ma.Multiaddr) (ma.Multiaddr, error) {
return addr, nil return addr, nil
} }
func decapsulateCircuitRelayAddr(addr ma.Multiaddr) (ma.Multiaddr, error) { func decapsulateCircuitRelayAddr(ctx context.Context, addr ma.Multiaddr) (ma.Multiaddr, error) {
_, err := addr.ValueForProtocol(ma.P_CIRCUIT) _, err := addr.ValueForProtocol(ma.P_CIRCUIT)
if err != nil { if err != nil {
return nil, errors.New("not a circuit relay address") return nil, errors.New("not a circuit relay address")
@ -194,6 +195,16 @@ func decapsulateCircuitRelayAddr(addr ma.Multiaddr) (ma.Multiaddr, error) {
return c.Protocol().Code == ma.P_CIRCUIT return c.Protocol().Code == ma.P_CIRCUIT
}) })
// If the multiaddress is a dns4 address, we resolve it
addrs, err := madns.DefaultResolver.Resolve(ctx, addr)
if err != nil {
return nil, err
}
if len(addrs) > 0 {
return addrs[0], nil
}
return addr, nil return addr, nil
} }
@ -221,10 +232,10 @@ func selectWSListenAddresses(addresses []ma.Multiaddr) ([]ma.Multiaddr, error) {
return result, nil return result, nil
} }
func selectCircuitRelayListenAddresses(addresses []ma.Multiaddr) ([]ma.Multiaddr, error) { func selectCircuitRelayListenAddresses(ctx context.Context, addresses []ma.Multiaddr) ([]ma.Multiaddr, error) {
var result []ma.Multiaddr var result []ma.Multiaddr
for _, addr := range addresses { for _, addr := range addresses {
addr, err := decapsulateCircuitRelayAddr(addr) addr, err := decapsulateCircuitRelayAddr(ctx, addr)
if err != nil { if err != nil {
continue continue
} }
@ -234,6 +245,7 @@ func selectCircuitRelayListenAddresses(addresses []ma.Multiaddr) ([]ma.Multiaddr
return result, nil return result, nil
} }
func filter0Port(addresses []ma.Multiaddr) ([]ma.Multiaddr, error) { func filter0Port(addresses []ma.Multiaddr) ([]ma.Multiaddr, error) {
var result []ma.Multiaddr var result []ma.Multiaddr
for _, addr := range addresses { for _, addr := range addresses {
@ -255,7 +267,7 @@ func filter0Port(addresses []ma.Multiaddr) ([]ma.Multiaddr, error) {
return result, nil return result, nil
} }
func (w *WakuNode) getENRAddresses(addrs []ma.Multiaddr) (extAddr *net.TCPAddr, multiaddr []ma.Multiaddr, err error) { func (w *WakuNode) getENRAddresses(ctx context.Context, addrs []ma.Multiaddr) (extAddr *net.TCPAddr, multiaddr []ma.Multiaddr, err error) {
extAddr, err = selectMostExternalAddress(addrs) extAddr, err = selectMostExternalAddress(addrs)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@ -266,7 +278,7 @@ func (w *WakuNode) getENRAddresses(addrs []ma.Multiaddr) (extAddr *net.TCPAddr,
return nil, nil, err return nil, nil, err
} }
circuitAddrs, err := selectCircuitRelayListenAddresses(addrs) circuitAddrs, err := selectCircuitRelayListenAddresses(ctx, addrs)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -288,7 +300,7 @@ func (w *WakuNode) getENRAddresses(addrs []ma.Multiaddr) (extAddr *net.TCPAddr,
} }
func (w *WakuNode) setupENR(ctx context.Context, addrs []ma.Multiaddr) error { func (w *WakuNode) setupENR(ctx context.Context, addrs []ma.Multiaddr) error {
ipAddr, multiaddresses, err := w.getENRAddresses(addrs) ipAddr, multiaddresses, err := w.getENRAddresses(ctx, addrs)
if err != nil { if err != nil {
w.log.Error("obtaining external address", zap.Error(err)) w.log.Error("obtaining external address", zap.Error(err))
return err return err