fix: set a limit to the multiaddr field of enr (#3230)

This commit is contained in:
RichΛrd 2023-02-24 08:24:54 -04:00 committed by GitHub
parent 11db9f2119
commit 5773616757
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 34 deletions

2
go.mod
View File

@ -78,7 +78,7 @@ require (
github.com/ipfs/go-log/v2 v2.5.1 github.com/ipfs/go-log/v2 v2.5.1
github.com/ladydascalie/currency v1.6.0 github.com/ladydascalie/currency v1.6.0
github.com/meirf/gopart v0.0.0-20180520194036-37e9492a85a8 github.com/meirf/gopart v0.0.0-20180520194036-37e9492a85a8
github.com/waku-org/go-waku v0.5.2-0.20230221221637-74b734c337e9 github.com/waku-org/go-waku v0.5.2-0.20230223234511-57f6110074d0
github.com/yeqown/go-qrcode/v2 v2.2.1 github.com/yeqown/go-qrcode/v2 v2.2.1
github.com/yeqown/go-qrcode/writer/standard v1.2.1 github.com/yeqown/go-qrcode/writer/standard v1.2.1
) )

4
go.sum
View File

@ -2105,8 +2105,8 @@ github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae/go.mod h1:DD4vA1
github.com/vishvananda/netns v0.0.0-20210104183010-2eb08e3e575f/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0= github.com/vishvananda/netns v0.0.0-20210104183010-2eb08e3e575f/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0=
github.com/waku-org/go-discover v0.0.0-20221209174356-61c833f34d98 h1:xwY0kW5XZFimdqfZb9cZwT1S3VJP9j3AE6bdNd9boXM= github.com/waku-org/go-discover v0.0.0-20221209174356-61c833f34d98 h1:xwY0kW5XZFimdqfZb9cZwT1S3VJP9j3AE6bdNd9boXM=
github.com/waku-org/go-discover v0.0.0-20221209174356-61c833f34d98/go.mod h1:eBHgM6T4EG0RZzxpxKy+rGz/6Dw2Nd8DWxS0lm9ESDw= github.com/waku-org/go-discover v0.0.0-20221209174356-61c833f34d98/go.mod h1:eBHgM6T4EG0RZzxpxKy+rGz/6Dw2Nd8DWxS0lm9ESDw=
github.com/waku-org/go-waku v0.5.2-0.20230221221637-74b734c337e9 h1:9AiqlpVucwUx+Lm1gBLOwk1VUgozGYmnJ4KM9mH/Irk= github.com/waku-org/go-waku v0.5.2-0.20230223234511-57f6110074d0 h1:WfPpenX+U8pzYrT14Ei1thbU6aGSbf4AM048EF75nX8=
github.com/waku-org/go-waku v0.5.2-0.20230221221637-74b734c337e9/go.mod h1:hupqK7cKFAmY+r/EW7LLaPnMLsPrm0fW1OuR6KrO8a8= github.com/waku-org/go-waku v0.5.2-0.20230223234511-57f6110074d0/go.mod h1:hupqK7cKFAmY+r/EW7LLaPnMLsPrm0fW1OuR6KrO8a8=
github.com/waku-org/go-zerokit-rln v0.1.7-wakuorg h1:2vVIBCtBih2w1K9ll8YnToTDZvbxcgbsClsPlJS/kkg= github.com/waku-org/go-zerokit-rln v0.1.7-wakuorg h1:2vVIBCtBih2w1K9ll8YnToTDZvbxcgbsClsPlJS/kkg=
github.com/waku-org/go-zerokit-rln v0.1.7-wakuorg/go.mod h1:GlyaVeEWNEBxVJrWC6jFTvb4LNb9d9qnjdS6EiWVUvk= github.com/waku-org/go-zerokit-rln v0.1.7-wakuorg/go.mod h1:GlyaVeEWNEBxVJrWC6jFTvb4LNb9d9qnjdS6EiWVUvk=
github.com/wealdtech/go-ens/v3 v3.5.0 h1:Huc9GxBgiGweCOGTYomvsg07K2QggAqZpZ5SuiZdC8o= github.com/wealdtech/go-ens/v3 v3.5.0 h1:Huc9GxBgiGweCOGTYomvsg07K2QggAqZpZ5SuiZdC8o=

View File

@ -5,7 +5,6 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"encoding/binary" "encoding/binary"
"errors" "errors"
"fmt"
"math" "math"
"math/rand" "math/rand"
"net" "net"
@ -30,6 +29,8 @@ func (w *WakuNode) newLocalnode(priv *ecdsa.PrivateKey) (*enode.LocalNode, error
func writeMultiaddressField(localnode *enode.LocalNode, addrAggr []ma.Multiaddr) (err error) { func writeMultiaddressField(localnode *enode.LocalNode, addrAggr []ma.Multiaddr) (err error) {
defer func() { defer func() {
if e := recover(); e != nil { if e := recover(); e != nil {
// Deleting the multiaddr entry, as we could not write it succesfully
localnode.Delete(enr.WithEntry(utils.MultiaddrENRField, struct{}{}))
err = errors.New("could not write enr record") err = errors.New("could not write enr record")
} }
}() }()
@ -44,7 +45,7 @@ func writeMultiaddressField(localnode *enode.LocalNode, addrAggr []ma.Multiaddr)
fieldRaw = append(fieldRaw, maRaw...) fieldRaw = append(fieldRaw, maRaw...)
} }
if len(fieldRaw) != 0 { if len(fieldRaw) != 0 && len(fieldRaw) <= 100 { // Max length for multiaddr field before triggering the 300 bytes limit
localnode.Set(enr.WithEntry(utils.MultiaddrENRField, fieldRaw)) localnode.Set(enr.WithEntry(utils.MultiaddrENRField, fieldRaw))
} }
@ -75,13 +76,13 @@ func (w *WakuNode) updateLocalNode(localnode *enode.LocalNode, multiaddrs []ma.M
localnode.SetStaticIP(ipAddr.IP) localnode.SetStaticIP(ipAddr.IP)
localnode.Set(enr.TCP(uint16(ipAddr.Port))) // TODO: ipv6? localnode.Set(enr.TCP(uint16(ipAddr.Port))) // TODO: ipv6?
return writeMultiaddresses(localnode, multiaddrs, w.log) return writeMultiaddresses(localnode, multiaddrs)
} else if !shouldAutoUpdate { } else if !shouldAutoUpdate {
// We received a libp2p address update. Autoupdate is disabled // We received a libp2p address update. Autoupdate is disabled
// Using a static ip will disable endpoint prediction. // Using a static ip will disable endpoint prediction.
localnode.SetStaticIP(ipAddr.IP) localnode.SetStaticIP(ipAddr.IP)
localnode.Set(enr.TCP(uint16(ipAddr.Port))) // TODO: ipv6? localnode.Set(enr.TCP(uint16(ipAddr.Port))) // TODO: ipv6?
return writeMultiaddresses(localnode, multiaddrs, w.log) return writeMultiaddresses(localnode, multiaddrs)
} else { } else {
// We received a libp2p address update, but we should still // We received a libp2p address update, but we should still
// allow discv5 to update the enr record. We set the localnode // allow discv5 to update the enr record. We set the localnode
@ -97,7 +98,7 @@ func (w *WakuNode) updateLocalNode(localnode *enode.LocalNode, multiaddrs []ma.M
localnode.Delete(enr.TCP(0)) localnode.Delete(enr.TCP(0))
} }
if ip6 != nil && !ip6.IsUnspecified() { if ip4 == nil && ip6 != nil && !ip6.IsUnspecified() {
localnode.Set(enr.IPv6(ip6)) localnode.Set(enr.IPv6(ip6))
localnode.Set(enr.TCP6(ipAddr.Port)) localnode.Set(enr.TCP6(ipAddr.Port))
} else { } else {
@ -105,12 +106,12 @@ func (w *WakuNode) updateLocalNode(localnode *enode.LocalNode, multiaddrs []ma.M
localnode.Delete(enr.TCP6(0)) localnode.Delete(enr.TCP6(0))
} }
return writeMultiaddresses(localnode, multiaddrs, w.log) return writeMultiaddresses(localnode, multiaddrs)
} }
} }
func writeMultiaddresses(localnode *enode.LocalNode, multiaddrs []ma.Multiaddr, logger *zap.Logger) error { func writeMultiaddresses(localnode *enode.LocalNode, multiaddrs []ma.Multiaddr) error {
// Randomly shuffle multiaddresses // Randomly shuffle multiaddresses
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(multiaddrs), func(i, j int) { multiaddrs[i], multiaddrs[j] = multiaddrs[j], multiaddrs[i] }) rand.Shuffle(len(multiaddrs), func(i, j int) { multiaddrs[i], multiaddrs[j] = multiaddrs[j], multiaddrs[i] })
@ -128,27 +129,14 @@ func writeMultiaddresses(localnode *enode.LocalNode, multiaddrs []ma.Multiaddr,
break break
} else { } else {
failedOnceWritingENR = true failedOnceWritingENR = true
localnode.Delete(enr.WithEntry(utils.MultiaddrENRField, struct{}{}))
} }
} }
fmt.Println("WriteMultiaddresses ================================================================") if failedOnceWritingENR && couldWriteENRatLeastOnce {
fmt.Println("Multiaddresses:", multiaddrs) // Could write a subset of multiaddresses but not all
fmt.Println("Failed to write ENR?", failedOnceWritingENR) err = writeMultiaddressField(localnode, multiaddrs[0:successIdx])
fmt.Println("SuccessIDX:", successIdx) if err != nil {
logger.Info("Updating ENR ---", zap.Any("multiaddr", multiaddrs), zap.Int("successIDX", successIdx), zap.Bool("Failed to write enr", failedOnceWritingENR)) return errors.New("could not write new ENR")
fmt.Println("------------------------------------------------------------")
if failedOnceWritingENR {
if !couldWriteENRatLeastOnce {
// In case multiaddr could not be populated at all
localnode.Delete(enr.WithEntry(utils.MultiaddrENRField, struct{}{}))
} else {
// Could write a subset of multiaddresses but not all
err = writeMultiaddressField(localnode, multiaddrs[0:successIdx])
if err != nil {
return errors.New("could not write new ENR")
}
} }
} }
@ -353,11 +341,6 @@ 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 {
fmt.Println("SetupENR ================================================================")
fmt.Println("Multiaddresses", addrs)
w.log.Info("Setup ENR", zap.Any("multiaddresses", addrs))
fmt.Println("------------------------------------------------------------")
ipAddr, multiaddresses, err := w.getENRAddresses(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))

2
vendor/modules.txt vendored
View File

@ -985,7 +985,7 @@ github.com/vacp2p/mvds/transport
github.com/waku-org/go-discover/discover github.com/waku-org/go-discover/discover
github.com/waku-org/go-discover/discover/v4wire github.com/waku-org/go-discover/discover/v4wire
github.com/waku-org/go-discover/discover/v5wire github.com/waku-org/go-discover/discover/v5wire
# github.com/waku-org/go-waku v0.5.2-0.20230221221637-74b734c337e9 # github.com/waku-org/go-waku v0.5.2-0.20230223234511-57f6110074d0
## explicit; go 1.19 ## explicit; go 1.19
github.com/waku-org/go-waku/logging github.com/waku-org/go-waku/logging
github.com/waku-org/go-waku/waku/persistence github.com/waku-org/go-waku/waku/persistence