Skip to content

Commit

Permalink
feat: dont' filter out bootnodes
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos committed Nov 2, 2023
1 parent 02f2800 commit 00f208d
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions waku/v2/discv5/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type DiscoveryV5 struct {
type discV5Parameters struct {
autoUpdate bool
autoFindPeers bool
bootnodes []*enode.Node
bootnodes map[enode.ID]*enode.Node
udpPort uint
advertiseAddr []multiaddr.Multiaddr
loopPredicate func(*enode.Node) bool
Expand All @@ -74,7 +74,10 @@ func WithAutoUpdate(autoUpdate bool) DiscoveryV5Option {
// WithBootnodes is an option used to specify the bootstrap nodes to use with DiscV5
func WithBootnodes(bootnodes []*enode.Node) DiscoveryV5Option {
return func(params *discV5Parameters) {
params.bootnodes = bootnodes
params.bootnodes = make(map[enode.ID]*enode.Node)
for _, b := range bootnodes {
params.bootnodes[b.ID()] = b
}
}
}

Expand Down Expand Up @@ -126,6 +129,11 @@ func NewDiscoveryV5(priv *ecdsa.PrivateKey, localnode *enode.LocalNode, peerConn
NAT = nat.Any()
}

var bootnodes []*enode.Node
for _, bootnode := range params.bootnodes {
bootnodes = append(bootnodes, bootnode)
}

return &DiscoveryV5{
params: params,
peerConnector: peerConnector,
Expand All @@ -135,7 +143,7 @@ func NewDiscoveryV5(priv *ecdsa.PrivateKey, localnode *enode.LocalNode, peerConn
metrics: newMetrics(reg),
config: discover.Config{
PrivateKey: priv,
Bootnodes: params.bootnodes,
Bootnodes: bootnodes,
V5Config: discover.V5Config{
ProtocolID: &protocolID,
},
Expand Down Expand Up @@ -379,7 +387,8 @@ func delayedHasNext(ctx context.Context, iterator enode.Iterator) bool {
return true
}

func (d *DiscoveryV5) defaultPredicate() Predicate {
// DefaultPredicate contains the conditions to be applied when filtering peers discovered via discv5
func (d *DiscoveryV5) DefaultPredicate() Predicate {
return FilterPredicate(func(n *enode.Node) bool {
localRS, err := wenr.RelaySharding(d.localnode.Node().Record())
if err != nil {
Expand All @@ -390,6 +399,10 @@ func (d *DiscoveryV5) defaultPredicate() Predicate {
return true
}

if _, ok := d.params.bootnodes[n.ID()]; ok {
return true // The record is a bootnode. Assume it's valid and dont filter it out
}

nodeRS, err := wenr.RelaySharding(n.Record())
if err != nil {
return false
Expand Down Expand Up @@ -417,7 +430,7 @@ func (d *DiscoveryV5) defaultPredicate() Predicate {

// Iterates over the nodes found via discv5 belonging to the node's current shard, and sends them to peerConnector
func (d *DiscoveryV5) peerLoop(ctx context.Context) error {
iterator, err := d.PeerIterator(d.defaultPredicate())
iterator, err := d.PeerIterator(d.DefaultPredicate())
if err != nil {
d.metrics.RecordError(iteratorFailure)
return fmt.Errorf("obtaining iterator: %w", err)
Expand Down Expand Up @@ -448,7 +461,7 @@ func (d *DiscoveryV5) runDiscoveryV5Loop(ctx context.Context) {
if len(d.config.Bootnodes) > 0 {
localRS, err := wenr.RelaySharding(d.localnode.Node().Record())
if err == nil && localRS != nil {
iterator := d.defaultPredicate()(enode.IterNodes(d.config.Bootnodes))
iterator := d.DefaultPredicate()(enode.IterNodes(d.config.Bootnodes))
validBootCount := 0
for iterator.Next() {
validBootCount++
Expand Down

0 comments on commit 00f208d

Please sign in to comment.