fix: adding circuit relay information to enr

This commit is contained in:
Richard Ramos 2023-01-27 16:33:37 -04:00 committed by RichΛrd
parent 61cba076bb
commit 676a1ab361
4 changed files with 250 additions and 197 deletions

View File

@ -0,0 +1,45 @@
package node
import (
"net"
"testing"
ma "github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require"
)
func TestExternalAddressSelection(t *testing.T) {
a1, _ := ma.NewMultiaddr("/ip4/192.168.0.106/tcp/60000/p2p/16Uiu2HAmUVVrJo1KMw4QwUANYF7Ws4mfcRqf9xHaaGP87GbMuY2f") // Valid
a2, _ := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/60000/p2p/16Uiu2HAmUVVrJo1KMw4QwUANYF7Ws4mfcRqf9xHaaGP87GbMuY2f") // Valid but should not be prefered
a3, _ := ma.NewMultiaddr("/ip4/192.168.1.20/tcp/19710/p2p/16Uiu2HAmUVVrJo1KMw4QwUANYF7Ws4mfcRqf9xHaaGP87GbMuY2f") // Valid
a4, _ := ma.NewMultiaddr("/dns4/www.status.im/tcp/2012/ws/p2p/16Uiu2HAmUVVrJo1KMw4QwUANYF7Ws4mfcRqf9xHaaGP87GbMuY2f") // Invalid (it's useless)
a5, _ := ma.NewMultiaddr("/dns4/www.status.im/tcp/443/wss/p2p/16Uiu2HAmUVVrJo1KMw4QwUANYF7Ws4mfcRqf9xHaaGP87GbMuY2f") // Valid
a6, _ := ma.NewMultiaddr("/ip4/192.168.1.20/tcp/19710/wss/p2p/16Uiu2HAmUVVrJo1KMw4QwUANYF7Ws4mfcRqf9xHaaGP87GbMuY2f") // Invalid (local + wss)
a7, _ := ma.NewMultiaddr("/ip4/192.168.1.20/tcp/19710/ws/p2p/16Uiu2HAmUVVrJo1KMw4QwUANYF7Ws4mfcRqf9xHaaGP87GbMuY2f") // Invalid (it's useless)
a8, _ := ma.NewMultiaddr("/dns4/node-02.gc-us-central1-a.status.prod.statusim.net/tcp/30303/p2p/16Uiu2HAmDQugwDHM3YeUp86iGjrUvbdw3JPRgikC7YoGBsT2ymMg/p2p-circuit/p2p/16Uiu2HAmUVVrJo1KMw4QwUANYF7Ws4mfcRqf9xHaaGP87GbMuY2f") // VALID
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
addrs := []ma.Multiaddr{a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11}
w := &WakuNode{}
extAddr, multiaddr, err := w.getENRAddresses(addrs)
a5NoP2P, _ := decapsulateP2P(a5)
a8RelayNode, _ := decapsulateCircuitRelayAddr(a8)
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.Equal(t, multiaddr[1].String(), a8RelayNode.String())
require.Len(t, multiaddr, 5)
a12, _ := ma.NewMultiaddr("/ip4/188.23.1.8/tcp/30303/p2p/16Uiu2HAmUVVrJo1KMw4QwUANYF7Ws4mfcRqf9xHaaGP87GbMuY2f") // VALID
addrs = append(addrs, a12)
extAddr, _, err = w.getENRAddresses(addrs)
require.NoError(t, err)
require.Equal(t, extAddr.IP, net.IPv4(188, 23, 1, 8))
require.Equal(t, extAddr.Port, 30303)
}

View File

@ -13,7 +13,6 @@ import (
"github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr" "github.com/ethereum/go-ethereum/p2p/enr"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
"github.com/waku-org/go-waku/logging"
"github.com/waku-org/go-waku/waku/v2/utils" "github.com/waku-org/go-waku/waku/v2/utils"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -26,7 +25,7 @@ func (w *WakuNode) newLocalnode(priv *ecdsa.PrivateKey) (*enode.LocalNode, error
return enode.NewLocalNode(db, priv), nil return enode.NewLocalNode(db, priv), nil
} }
func (w *WakuNode) updateLocalNode(localnode *enode.LocalNode, wsAddr []ma.Multiaddr, ipAddr *net.TCPAddr, udpPort uint, wakuFlags utils.WakuEnrBitfield, advertiseAddr *net.IP, shouldAutoUpdate bool, log *zap.Logger) error { func (w *WakuNode) updateLocalNode(localnode *enode.LocalNode, multiaddrs []ma.Multiaddr, ipAddr *net.TCPAddr, udpPort uint, wakuFlags utils.WakuEnrBitfield, advertiseAddr *net.IP, shouldAutoUpdate bool, log *zap.Logger) error {
localnode.SetFallbackUDP(int(udpPort)) localnode.SetFallbackUDP(int(udpPort))
localnode.Set(enr.WithEntry(utils.WakuENRField, wakuFlags)) localnode.Set(enr.WithEntry(utils.WakuENRField, wakuFlags))
localnode.SetFallbackIP(net.IP{127, 0, 0, 1}) localnode.SetFallbackIP(net.IP{127, 0, 0, 1})
@ -69,21 +68,10 @@ func (w *WakuNode) updateLocalNode(localnode *enode.LocalNode, wsAddr []ma.Multi
} }
} }
// Adding websocket multiaddresses // Adding extra multiaddresses
var fieldRaw []byte var fieldRaw []byte
for _, addr := range multiaddrs {
for _, addr := range wsAddr { maRaw := addr.Bytes()
p2p, err := addr.ValueForProtocol(ma.P_P2P)
if err != nil {
return err
}
p2pAddr, err := ma.NewMultiaddr("/p2p/" + p2p)
if err != nil {
return fmt.Errorf("could not create p2p addr: %w", err)
}
maRaw := addr.Decapsulate(p2pAddr).Bytes()
maSize := make([]byte, 2) maSize := make([]byte, 2)
binary.BigEndian.PutUint16(maSize, uint16(len(maRaw))) binary.BigEndian.PutUint16(maSize, uint16(len(maRaw)))
@ -98,19 +86,19 @@ func (w *WakuNode) updateLocalNode(localnode *enode.LocalNode, wsAddr []ma.Multi
return nil return nil
} }
func isPrivate(addr candidateAddr) bool { func isPrivate(addr *net.TCPAddr) bool {
return addr.ip.IP.IsPrivate() return addr.IP.IsPrivate()
} }
func isExternal(addr candidateAddr) bool { func isExternal(addr *net.TCPAddr) bool {
return !isPrivate(addr) && !addr.ip.IP.IsLoopback() && !addr.ip.IP.IsUnspecified() return !isPrivate(addr) && !addr.IP.IsLoopback() && !addr.IP.IsUnspecified()
} }
func isLoopback(addr candidateAddr) bool { func isLoopback(addr *net.TCPAddr) bool {
return addr.ip.IP.IsLoopback() return addr.IP.IsLoopback()
} }
func filterIP(ss []candidateAddr, fn func(candidateAddr) bool) (ret []candidateAddr) { func filterIP(ss []*net.TCPAddr, fn func(*net.TCPAddr) bool) (ret []*net.TCPAddr) {
for _, s := range ss { for _, s := range ss {
if fn(s) { if fn(s) {
ret = append(ret, s) ret = append(ret, s)
@ -119,12 +107,25 @@ func filterIP(ss []candidateAddr, fn func(candidateAddr) bool) (ret []candidateA
return return
} }
type candidateAddr struct { func extractIPAddressForENR(addr ma.Multiaddr) (*net.TCPAddr, error) {
ip *net.TCPAddr // It's a p2p-circuit address. We shouldnt use these
maddr ma.Multiaddr // for building the ENR record default keys
} _, err := addr.ValueForProtocol(ma.P_CIRCUIT)
if err == nil {
return nil, errors.New("can't use IP address from a p2p-circuit address")
}
// ws and wss addresses are handled by the multiaddr key
// they shouldnt be used for building the ENR record default keys
_, err = addr.ValueForProtocol(ma.P_WS)
if err == nil {
return nil, errors.New("can't use IP address from a ws address")
}
_, err = addr.ValueForProtocol(ma.P_WSS)
if err == nil {
return nil, errors.New("can't use IP address from a wss address")
}
func extractIP(addr ma.Multiaddr) (*net.TCPAddr, error) {
var ipStr string var ipStr string
dns4, err := addr.ValueForProtocol(ma.P_DNS4) dns4, err := addr.ValueForProtocol(ma.P_DNS4)
if err != nil { if err != nil {
@ -154,76 +155,88 @@ func extractIP(addr ma.Multiaddr) (*net.TCPAddr, error) {
}, nil }, nil
} }
func selectMostExternalAddress(addresses []ma.Multiaddr) (ma.Multiaddr, *net.TCPAddr, error) { func selectMostExternalAddress(addresses []ma.Multiaddr) (*net.TCPAddr, error) {
var ipAddrs []candidateAddr var ipAddrs []*net.TCPAddr
for _, addr := range addresses { for _, addr := range addresses {
ipAddr, err := extractIP(addr) ipAddr, err := extractIPAddressForENR(addr)
if err != nil { if err != nil {
continue continue
} }
ipAddrs = append(ipAddrs, candidateAddr{ fmt.Println(ipAddr, addr)
ip: ipAddr, ipAddrs = append(ipAddrs, ipAddr)
maddr: addr,
})
} }
externalIPs := filterIP(ipAddrs, isExternal) externalIPs := filterIP(ipAddrs, isExternal)
if len(externalIPs) > 0 { if len(externalIPs) > 0 {
return externalIPs[0].maddr, externalIPs[0].ip, nil return externalIPs[0], nil
} }
privateIPs := filterIP(ipAddrs, isPrivate) privateIPs := filterIP(ipAddrs, isPrivate)
if len(privateIPs) > 0 { if len(privateIPs) > 0 {
return privateIPs[0].maddr, privateIPs[0].ip, nil return privateIPs[0], nil
} }
loopback := filterIP(ipAddrs, isLoopback) loopback := filterIP(ipAddrs, isLoopback)
if len(loopback) > 0 { if len(loopback) > 0 {
return loopback[0].maddr, loopback[0].ip, nil return loopback[0], nil
} }
return nil, nil, errors.New("could not obtain ip address") return nil, errors.New("could not obtain ip address")
} }
func selectWSListenAddress(addresses []ma.Multiaddr, extAddr ma.Multiaddr) ([]ma.Multiaddr, error) { func decapsulateP2P(addr ma.Multiaddr) (ma.Multiaddr, error) {
extAddrDNS, err := extAddr.ValueForProtocol(ma.P_DNS4) p2p, err := addr.ValueForProtocol(ma.P_P2P)
var extAddrIP string
if err != nil { if err != nil {
extAddrIP, err = extAddr.ValueForProtocol(ma.P_IP4) return nil, err
if err != nil {
return nil, err
}
} }
p2pAddr, err := ma.NewMultiaddr("/p2p/" + p2p)
if err != nil {
return nil, err
}
addr = addr.Decapsulate(p2pAddr)
return addr, nil
}
func decapsulateCircuitRelayAddr(addr ma.Multiaddr) (ma.Multiaddr, error) {
_, err := addr.ValueForProtocol(ma.P_CIRCUIT)
if err != nil {
return nil, errors.New("not a circuit relay address")
}
// We remove the node's multiaddress from the addr
addr, _ = ma.SplitFunc(addr, func(c ma.Component) bool {
return c.Protocol().Code == ma.P_CIRCUIT
})
return addr, nil
}
func selectWSSListenAddresses(addresses []ma.Multiaddr) ([]ma.Multiaddr, error) {
var result []ma.Multiaddr var result []ma.Multiaddr
for _, addr := range addresses { for _, addr := range addresses {
// Filter addresses that match the extAddr // It's a p2p-circuit address. We dont use these at this stage yet
if extAddrDNS != "" { _, err := addr.ValueForProtocol(ma.P_CIRCUIT)
dns4, err := addr.ValueForProtocol(ma.P_DNS4) if err == nil {
if err != nil { continue
continue
}
if dns4 != extAddrDNS {
continue
}
} else {
ip4, err := addr.ValueForProtocol(ma.P_IP4)
if err != nil {
continue
}
if ip4 != extAddrIP {
continue
}
} }
_, err := addr.ValueForProtocol(ma.P_WS) // Only WSS with a domain name are allowed
if err == nil { _, err = addr.ValueForProtocol(ma.P_DNS4)
result = append(result, addr) if err != nil {
continue
} }
_, err = addr.ValueForProtocol(ma.P_WSS) _, err = addr.ValueForProtocol(ma.P_WSS)
if err != nil {
continue
}
addr, err = decapsulateP2P(addr)
if err == nil { if err == nil {
result = append(result, addr) result = append(result, addr)
} }
@ -232,22 +245,52 @@ func selectWSListenAddress(addresses []ma.Multiaddr, extAddr ma.Multiaddr) ([]ma
return result, nil return result, nil
} }
func selectCircuitRelayListenAddresses(addresses []ma.Multiaddr) ([]ma.Multiaddr, error) {
var result []ma.Multiaddr
for _, addr := range addresses {
addr, err := decapsulateCircuitRelayAddr(addr)
if err != nil {
continue
}
result = append(result, addr)
}
return result, nil
}
func (w *WakuNode) getENRAddresses(addrs []ma.Multiaddr) (extAddr *net.TCPAddr, multiaddr []ma.Multiaddr, err error) {
extAddr, err = selectMostExternalAddress(addrs)
if err != nil {
return nil, nil, err
}
wssAddrs, err := selectWSSListenAddresses(addrs)
if err != nil {
return nil, nil, err
}
circuitAddrs, err := selectCircuitRelayListenAddresses(addrs)
if err != nil {
return nil, nil, err
}
multiaddr = append(multiaddr, wssAddrs...)
multiaddr = append(multiaddr, circuitAddrs...)
return
}
func (w *WakuNode) setupENR(ctx context.Context, addrs []ma.Multiaddr) error { func (w *WakuNode) setupENR(ctx context.Context, addrs []ma.Multiaddr) error {
extAddr, ipAddr, err := selectMostExternalAddress(addrs) ipAddr, multiaddresses, err := w.getENRAddresses(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
} }
wsAddresses, err := selectWSListenAddress(addrs, extAddr) err = w.updateLocalNode(w.localNode, multiaddresses, ipAddr, w.opts.udpPort, w.wakuFlag, w.opts.advertiseAddr, w.opts.discV5autoUpdate, w.log)
if err != nil { if err != nil {
w.log.Error("obtaining websocket addresses", zap.Error(err)) w.log.Error("updating localnode ENR record", zap.Error(err))
return err
}
err = w.updateLocalNode(w.localNode, wsAddresses, ipAddr, w.opts.udpPort, w.wakuFlag, w.opts.advertiseAddr, w.opts.discV5autoUpdate, w.log)
if err != nil {
w.log.Error("obtaining ENR record from multiaddress", logging.MultiAddrs("multiaddr", extAddr), zap.Error(err))
return err return err
} }

View File

@ -1,13 +1,9 @@
package utils package utils
import ( import (
"crypto/ecdsa"
"encoding/binary" "encoding/binary"
"errors" "errors"
"fmt" "fmt"
"math"
"net"
"strconv"
"github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr" "github.com/ethereum/go-ethereum/p2p/enr"
@ -48,95 +44,6 @@ func NewWakuEnrBitfield(lightpush, filter, store, relay bool) WakuEnrBitfield {
return v return v
} }
// GetENRandIP returns a enr Node and TCP address obtained from a multiaddress. priv key and protocols supported
func GetENRandIP(addrs []multiaddr.Multiaddr, wakuFlags WakuEnrBitfield, privK *ecdsa.PrivateKey) (*enode.Node, error) {
r := &enr.Record{}
for _, addr := range addrs {
storeInMultiaddrsKey := false
var multiaddrItems []multiaddr.Multiaddr
_, err := addr.ValueForProtocol(multiaddr.P_WS)
if err == nil {
storeInMultiaddrsKey = true
multiaddrItems = append(multiaddrItems, addr)
}
_, err = addr.ValueForProtocol(multiaddr.P_WSS)
if err == nil {
storeInMultiaddrsKey = true
multiaddrItems = append(multiaddrItems, addr)
}
if !storeInMultiaddrsKey {
var ip string
dns4, err := addr.ValueForProtocol(multiaddr.P_DNS4)
if err != nil {
ip, err = addr.ValueForProtocol(multiaddr.P_IP4)
if err != nil {
return nil, err
}
} else {
netIP, err := net.ResolveIPAddr("ip4", dns4)
if err != nil {
return nil, err
}
ip = netIP.String()
}
portStr, err := addr.ValueForProtocol(multiaddr.P_TCP)
if err != nil {
return nil, err
}
port, err := strconv.Atoi(portStr)
if err != nil {
return nil, err
}
if port > 0 && port <= math.MaxUint16 {
r.Set(enr.TCP(uint16(port))) // lgtm [go/incorrect-integer-conversion]
} else {
return nil, fmt.Errorf("could not set port %d", port)
}
r.Set(enr.IP(net.ParseIP(ip)))
} else {
p2p, err := addr.ValueForProtocol(multiaddr.P_P2P)
if err != nil {
return nil, err
}
p2pAddr, err := multiaddr.NewMultiaddr("/p2p/" + p2p)
if err != nil {
return nil, fmt.Errorf("could not create p2p addr: %w", err)
}
var fieldRaw []byte
for _, ma := range multiaddrItems {
maRaw := ma.Decapsulate(p2pAddr).Bytes()
maSize := make([]byte, 2)
binary.BigEndian.PutUint16(maSize, uint16(len(maRaw)))
fieldRaw = append(fieldRaw, maSize...)
fieldRaw = append(fieldRaw, maRaw...)
}
if len(fieldRaw) != 0 {
r.Set(enr.WithEntry(MultiaddrENRField, fieldRaw))
}
}
}
r.Set(enr.WithEntry(WakuENRField, wakuFlags))
err := enode.SignV4(r, privK)
if err != nil {
return nil, err
}
node, err := enode.New(enode.ValidSchemes, r)
return node, err
}
// EnodeToMultiaddress converts an enode into a multiaddress // EnodeToMultiaddress converts an enode into a multiaddress
func enodeToMultiAddr(node *enode.Node) (multiaddr.Multiaddr, error) { func enodeToMultiAddr(node *enode.Node) (multiaddr.Multiaddr, error) {
pubKey := EcdsaPubKeyToSecp256k1PublicKey(node.Pubkey()) pubKey := EcdsaPubKeyToSecp256k1PublicKey(node.Pubkey())

View File

@ -1,16 +1,19 @@
package utils package utils
import ( import (
"encoding/binary"
"errors"
"fmt" "fmt"
"math"
"net" "net"
"testing" "testing"
gcrypto "github.com/ethereum/go-ethereum/crypto" gcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enode"
"github.com/libp2p/go-libp2p/core/peer" "github.com/ethereum/go-ethereum/p2p/enr"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"go.uber.org/zap"
) )
func TestEnodeToMultiAddr(t *testing.T) { func TestEnodeToMultiAddr(t *testing.T) {
@ -23,44 +26,99 @@ func TestEnodeToMultiAddr(t *testing.T) {
require.Equal(t, expectedMultiAddr, actualMultiAddr.String()) require.Equal(t, expectedMultiAddr, actualMultiAddr.String())
} }
func TestGetENRandIP(t *testing.T) { // TODO: this function is duplicated in localnode.go. Extract to utils
key, _ := gcrypto.GenerateKey() func updateLocalNode(localnode *enode.LocalNode, multiaddrs []ma.Multiaddr, ipAddr *net.TCPAddr, udpPort uint, wakuFlags WakuEnrBitfield, advertiseAddr *net.IP, shouldAutoUpdate bool, log *zap.Logger) error {
pubKey := EcdsaPubKeyToSecp256k1PublicKey(&key.PublicKey) localnode.SetFallbackUDP(int(udpPort))
id, _ := peer.IDFromPublicKey(pubKey) localnode.Set(enr.WithEntry(WakuENRField, wakuFlags))
localnode.SetFallbackIP(net.IP{127, 0, 0, 1})
hostAddr := &net.TCPAddr{IP: net.ParseIP("192.168.0.1"), Port: 9999} if udpPort > math.MaxUint16 {
hostMultiAddr, _ := manet.FromNetAddr(hostAddr) return errors.New("invalid udp port number")
hostInfo, _ := ma.NewMultiaddr(fmt.Sprintf("/p2p/%s", id.Pretty())) }
ogMultiaddress := hostMultiAddr.Encapsulate(hostInfo)
wakuFlag := NewWakuEnrBitfield(true, true, true, true) if advertiseAddr != nil {
// An advertised address disables libp2p address updates
// and discv5 predictions
localnode.SetStaticIP(*advertiseAddr)
localnode.Set(enr.TCP(uint16(ipAddr.Port))) // TODO: ipv6?
} else if !shouldAutoUpdate {
// We received a libp2p address update. Autoupdate is disabled
// Using a static ip will disable endpoint prediction.
localnode.SetStaticIP(ipAddr.IP)
localnode.Set(enr.TCP(uint16(ipAddr.Port))) // TODO: ipv6?
} else {
// We received a libp2p address update, but we should still
// allow discv5 to update the enr record. We set the localnode
// keys manually. It's possible that the ENR record might get
// updated automatically
ip4 := ipAddr.IP.To4()
ip6 := ipAddr.IP.To16()
if ip4 != nil && !ip4.IsUnspecified() {
localnode.Set(enr.IPv4(ip4))
localnode.Set(enr.TCP(uint16(ipAddr.Port)))
} else {
localnode.Delete(enr.IPv4{})
localnode.Delete(enr.TCP(0))
}
node, err := GetENRandIP([]ma.Multiaddr{ogMultiaddress}, wakuFlag, key) if ip6 != nil && !ip6.IsUnspecified() {
require.NoError(t, err) localnode.Set(enr.IPv6(ip6))
localnode.Set(enr.TCP6(ipAddr.Port))
} else {
localnode.Delete(enr.IPv6{})
localnode.Delete(enr.TCP6(0))
}
}
parsedNode := enode.MustParse(node.String()) // Adding extra multiaddresses
resMultiaddress, err := enodeToMultiAddr(parsedNode) var fieldRaw []byte
require.NoError(t, err) for _, addr := range multiaddrs {
require.Equal(t, ogMultiaddress.String(), resMultiaddress.String()) maRaw := addr.Bytes()
maSize := make([]byte, 2)
binary.BigEndian.PutUint16(maSize, uint16(len(maRaw)))
fieldRaw = append(fieldRaw, maSize...)
fieldRaw = append(fieldRaw, maRaw...)
}
if len(fieldRaw) != 0 {
localnode.Set(enr.WithEntry(MultiaddrENRField, fieldRaw))
}
return nil
} }
func TestMultiaddr(t *testing.T) { func TestMultiaddr(t *testing.T) {
key, _ := gcrypto.GenerateKey() key, _ := gcrypto.GenerateKey()
pubKey := EcdsaPubKeyToSecp256k1PublicKey(&key.PublicKey)
id, _ := peer.IDFromPublicKey(pubKey)
wakuFlag := NewWakuEnrBitfield(true, true, true, true) wakuFlag := NewWakuEnrBitfield(true, true, true, true)
normalMultiaddr, _ := ma.NewMultiaddr("/ip4/192.1.168.241/tcp/60000/p2p/" + id.Pretty()) //wss, _ := ma.NewMultiaddr("/dns4/www.somedomainname.com/tcp/443/wss")
wsMultiaddress, _ := ma.NewMultiaddr("/ip4/10.0.0.241/tcp/60001/ws/p2p/" + id.Pretty()) circuit1, _ := ma.NewMultiaddr("/dns4/node-02.gc-us-central1-a.status.prod.statusim.net/tcp/30303/p2p/16Uiu2HAmDQugwDHM3YeUp86iGjrUvbdw3JPRgikC7YoGBsT2ymMg/p2p-circuit")
circuit2, _ := ma.NewMultiaddr("/dns4/node-01.gc-us-central1-a.status.prod.statusim.net/tcp/30303/p2p/16Uiu2HAmDQugwDHM3YeUp86iGjrUvbdw3JPRgikC7YoGBsT2ymMg/p2p-circuit")
node, err := GetENRandIP([]ma.Multiaddr{normalMultiaddr, wsMultiaddress}, wakuFlag, key) multiaddrValues := []ma.Multiaddr{
require.NoError(t, err) //wss,
circuit1,
circuit2,
}
multiaddresses, err := Multiaddress(node) db, _ := enode.OpenDB("")
fmt.Println(multiaddresses) localNode := enode.NewLocalNode(db, key)
err := updateLocalNode(localNode, multiaddrValues, &net.TCPAddr{IP: net.IPv4(192, 168, 1, 241), Port: 60000}, 50000, wakuFlag, nil, false, Logger())
if err != nil {
fmt.Println("ERROR WRITING ENR", err)
} else {
fmt.Println("==========================================")
fmt.Println(localNode.Node())
fmt.Println("==========================================")
multiaddresses, err := Multiaddress(localNode.Node())
require.NoError(t, err)
for _, a := range multiaddresses {
fmt.Println(a)
}
}
require.NoError(t, err)
require.Len(t, multiaddresses, 2)
require.True(t, normalMultiaddr.Equal(multiaddresses[0]))
require.True(t, wsMultiaddress.Equal(multiaddresses[1]))
} }