fix: use byte array to decode ENRs uint8 fields (#1227)

This commit is contained in:
richΛrd 2024-10-03 10:12:31 -04:00 committed by GitHub
parent ae423936ed
commit 15b4aee808
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 26 additions and 23 deletions

View File

@ -21,7 +21,6 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
"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/nat" "github.com/ethereum/go-ethereum/p2p/nat"
) )
@ -255,19 +254,13 @@ func (d *DiscoveryV5) Stop() {
} }
func isWakuNode(node *enode.Node) bool { func isWakuNode(node *enode.Node) bool {
enrField := new(wenr.WakuEnrBitfield) enrField, err := wenr.GetWakuEnrBitField(node)
if err := node.Record().Load(enr.WithEntry(wenr.WakuENRField, &enrField)); err != nil { if err != nil {
if !enr.IsNotFound(err) { utils.Logger().Named("discv5").Error("could not retrieve waku2 ENR field for enr ", zap.Error(err))
utils.Logger().Named("discv5").Error("could not retrieve waku2 ENR field for enr ", zap.Any("node", node))
}
return false return false
} }
if enrField != nil { return enrField != uint8(0) // #RFC 31 requirement
return *enrField != uint8(0) // #RFC 31 requirement
}
return false
} }
func (d *DiscoveryV5) evaluateNode() func(node *enode.Node) bool { func (d *DiscoveryV5) evaluateNode() func(node *enode.Node) bool {

View File

@ -4,7 +4,6 @@ import (
wenr "github.com/waku-org/go-waku/waku/v2/protocol/enr" wenr "github.com/waku-org/go-waku/waku/v2/protocol/enr"
"github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
) )
// FilterPredicate is to create a Predicate using a custom function // FilterPredicate is to create a Predicate using a custom function
@ -36,16 +35,11 @@ func FilterShard(cluster, index uint16) Predicate {
func FilterCapabilities(flags wenr.WakuEnrBitfield) Predicate { func FilterCapabilities(flags wenr.WakuEnrBitfield) Predicate {
return func(iterator enode.Iterator) enode.Iterator { return func(iterator enode.Iterator) enode.Iterator {
predicate := func(node *enode.Node) bool { predicate := func(node *enode.Node) bool {
enrField := new(wenr.WakuEnrBitfield) enrField, err := wenr.GetWakuEnrBitField(node)
if err := node.Record().Load(enr.WithEntry(wenr.WakuENRField, &enrField)); err != nil { if err != nil {
return false return false
} }
return enrField&flags == flags
if enrField == nil {
return false
}
return *enrField&flags == flags
} }
return enode.Filter(iterator, predicate) return enode.Filter(iterator, predicate)
} }

View File

@ -6,7 +6,6 @@ import (
"sync" "sync"
"time" "time"
"github.com/ethereum/go-ethereum/p2p/enr"
pubsub "github.com/libp2p/go-libp2p-pubsub" pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/event" "github.com/libp2p/go-libp2p/core/event"
"github.com/libp2p/go-libp2p/core/host" "github.com/libp2p/go-libp2p/core/host"
@ -547,8 +546,8 @@ func (pm *PeerManager) processPeerENR(p *service.PeerData) []protocol.ID {
} }
supportedProtos := []protocol.ID{} supportedProtos := []protocol.ID{}
//Identify and specify protocols supported by the peer based on the discovered peer's ENR //Identify and specify protocols supported by the peer based on the discovered peer's ENR
var enrField wenr.WakuEnrBitfield enrField, err := wenr.GetWakuEnrBitField(p.ENR)
if err := p.ENR.Record().Load(enr.WithEntry(wenr.WakuENRField, &enrField)); err == nil { if err == nil {
for proto, protoENR := range pm.wakuprotoToENRFieldMap { for proto, protoENR := range pm.wakuprotoToENRFieldMap {
protoENRField := protoENR.waku2ENRBitField protoENRField := protoENR.waku2ENRBitField
if protoENRField&enrField != 0 { if protoENRField&enrField != 0 {

View File

@ -28,6 +28,23 @@ const ShardingBitVectorEnrField = "rsv"
// WakuEnrBitfield is a8-bit flag field to indicate Waku capabilities. Only the 4 LSBs are currently defined according to RFC31 (https://rfc.vac.dev/spec/31/). // WakuEnrBitfield is a8-bit flag field to indicate Waku capabilities. Only the 4 LSBs are currently defined according to RFC31 (https://rfc.vac.dev/spec/31/).
type WakuEnrBitfield = uint8 type WakuEnrBitfield = uint8
func GetWakuEnrBitField(node *enode.Node) (WakuEnrBitfield, error) {
enrField := []byte{}
err := node.Record().Load(enr.WithEntry(WakuENRField, &enrField))
if err != nil {
if enr.IsNotFound(err) {
return 0, nil
}
return 0, err
}
if len(enrField) == 0 {
return 0, err
}
return WakuEnrBitfield(enrField[0]), nil
}
// NewWakuEnrBitfield creates a WakuEnrBitField whose value will depend on which protocols are enabled in the node // NewWakuEnrBitfield creates a WakuEnrBitField whose value will depend on which protocols are enabled in the node
func NewWakuEnrBitfield(lightpush, filter, store, relay bool) WakuEnrBitfield { func NewWakuEnrBitfield(lightpush, filter, store, relay bool) WakuEnrBitfield {
var v uint8 var v uint8