fix: enr exceeds 300 bytes (#3171)

This commit is contained in:
RichΛrd 2023-02-07 10:33:26 -04:00 committed by GitHub
parent c38ec00a2d
commit 5b642d3bed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 66 additions and 41 deletions

View File

@ -1 +1 @@
0.129.1
0.129.2

2
go.mod
View File

@ -78,7 +78,7 @@ require (
github.com/ipfs/go-log/v2 v2.5.1
github.com/meirf/gopart v0.0.0-20180520194036-37e9492a85a8
github.com/rmg/iso4217 v1.0.0
github.com/waku-org/go-waku v0.4.1-0.20230202011116-c3b5ab95abae
github.com/waku-org/go-waku v0.4.1-0.20230207135155-cce85913e6cc
github.com/yeqown/go-qrcode/v2 v2.2.1
github.com/yeqown/go-qrcode/writer/standard v1.2.1
)

4
go.sum
View File

@ -2067,8 +2067,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/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-waku v0.4.1-0.20230202011116-c3b5ab95abae h1:YBZycRlAK+T77wRiNzshOq7SJ5Lt/R/uxRcF+Rmn+Xk=
github.com/waku-org/go-waku v0.4.1-0.20230202011116-c3b5ab95abae/go.mod h1:sI14mN/sM8inIb2x2b462wydSEFyOyuDKI1cjiVIIpM=
github.com/waku-org/go-waku v0.4.1-0.20230207135155-cce85913e6cc h1:xRQ+shjjuhklZrjE4xJv7A9sNVoiz25QqYpmNfwKZAU=
github.com/waku-org/go-waku v0.4.1-0.20230207135155-cce85913e6cc/go.mod h1:sI14mN/sM8inIb2x2b462wydSEFyOyuDKI1cjiVIIpM=
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/wealdtech/go-ens/v3 v3.5.0 h1:Huc9GxBgiGweCOGTYomvsg07K2QggAqZpZ5SuiZdC8o=

View File

@ -6,8 +6,10 @@ import (
"encoding/binary"
"errors"
"math"
"math/rand"
"net"
"strconv"
"time"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
@ -24,6 +26,33 @@ func (w *WakuNode) newLocalnode(priv *ecdsa.PrivateKey) (*enode.LocalNode, error
return enode.NewLocalNode(db, priv), nil
}
func writeMultiaddressField(localnode *enode.LocalNode, addrAggr []ma.Multiaddr) (err error) {
defer func() {
if e := recover(); e != nil {
err = errors.New("could not write enr record")
}
}()
var fieldRaw []byte
for _, addr := range addrAggr {
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(utils.MultiaddrENRField, fieldRaw))
}
// This is to trigger the signing record err due to exceeding 300bytes limit
_ = localnode.Node()
return nil
}
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.Set(enr.WithEntry(utils.WakuENRField, wakuFlags))
@ -67,47 +96,37 @@ func (w *WakuNode) updateLocalNode(localnode *enode.LocalNode, multiaddrs []ma.M
}
}
// Adding extra multiaddresses. It will to add as many multiaddresses as possible
// without exceeding the enr max size of 300bytes
var addrAggr []ma.Multiaddr
// Randomly shuffle multiaddresses
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(multiaddrs), func(i, j int) { multiaddrs[i], multiaddrs[j] = multiaddrs[j], multiaddrs[i] })
// Adding extra multiaddresses. Should probably not exceed the enr max size of 300bytes
var err error
failedOnceWritingENR := false
couldWriteENRatLeastOnce := false
successIdx := -1
for i := len(multiaddrs) - 1; i >= 0; i-- {
addrAggr = append(addrAggr, multiaddrs[0:i]...)
err = func() (err error) {
defer func() {
if e := recover(); e != nil {
err = errors.New("could not write enr record")
}
}()
var fieldRaw []byte
for _, addr := range addrAggr {
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(utils.MultiaddrENRField, fieldRaw))
}
// This is to trigger the signing record err due to exceeding 300bytes limit
_ = localnode.Node()
return nil
}()
err = writeMultiaddressField(localnode, multiaddrs[0:i])
if err == nil {
couldWriteENRatLeastOnce = true
successIdx = i
break
} else {
failedOnceWritingENR = true
}
}
// In case multiaddr could not be populated at all
if err != nil {
localnode.Delete(enr.WithEntry(utils.MultiaddrENRField, struct{}{}))
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")
}
}
}
return nil

View File

@ -91,6 +91,7 @@ type WakuNode struct {
protocolEventSub event.Subscription
identificationEventSub event.Subscription
addressChangesSub event.Subscription
enrChangeCh chan struct{}
keepAliveMutex sync.Mutex
keepAliveFails map[peer.ID]int
@ -234,6 +235,8 @@ func New(opts ...WakuNodeOption) (*WakuNode, error) {
return nil, err
}
w.enrChangeCh = make(chan struct{}, 10)
if params.connStatusC != nil {
w.connStatusChan = params.connStatusC
}
@ -253,6 +256,7 @@ func (w *WakuNode) watchMultiaddressChanges(ctx context.Context) {
return
case <-first:
w.log.Info("listening", logging.MultiAddrs("multiaddr", addrs...))
w.enrChangeCh <- struct{}{}
case <-w.addressChangesSub.Out():
newAddrs := w.ListenAddresses()
diff := false
@ -270,6 +274,7 @@ func (w *WakuNode) watchMultiaddressChanges(ctx context.Context) {
addrs = newAddrs
w.log.Info("listening addresses update received", logging.MultiAddrs("multiaddr", addrs...))
_ = w.setupENR(ctx, addrs)
w.enrChangeCh <- struct{}{}
}
}
}
@ -403,6 +408,8 @@ func (w *WakuNode) Stop() {
w.host.Close()
close(w.enrChangeCh)
w.wg.Wait()
}
@ -419,13 +426,12 @@ func (w *WakuNode) ID() string {
func (w *WakuNode) watchENRChanges(ctx context.Context) {
defer w.wg.Done()
timer := time.NewTicker(1 * time.Second)
var prevNodeVal string
for {
select {
case <-ctx.Done():
return
case <-timer.C:
case <-w.enrChangeCh:
if w.localNode != nil {
currNodeVal := w.localNode.Node().String()
if prevNodeVal != currNodeVal {

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/v4wire
github.com/waku-org/go-discover/discover/v5wire
# github.com/waku-org/go-waku v0.4.1-0.20230202011116-c3b5ab95abae
# github.com/waku-org/go-waku v0.4.1-0.20230207135155-cce85913e6cc
## explicit; go 1.18
github.com/waku-org/go-waku/logging
github.com/waku-org/go-waku/waku/persistence