Skip to content

Commit 6df3e4e

Browse files
authored
swarm/network: remove isproxbin bool from kad.Each* iterfunc (#18239)
* swarm/network, swarm/pss: remove isproxbin bool from kad.Each* iterfunc * swarm/network: restore comment and unskip snapshot sync tests
1 parent d70c4fa commit 6df3e4e

File tree

10 files changed

+25
-35
lines changed

10 files changed

+25
-35
lines changed

swarm/network/discovery.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (d *Peer) HandleMsg(ctx context.Context, msg interface{}) error {
6565

6666
// NotifyDepth sends a message to all connections if depth of saturation is changed
6767
func NotifyDepth(depth uint8, kad *Kademlia) {
68-
f := func(val *Peer, po int, _ bool) bool {
68+
f := func(val *Peer, po int) bool {
6969
val.NotifyDepth(depth)
7070
return true
7171
}
@@ -74,7 +74,7 @@ func NotifyDepth(depth uint8, kad *Kademlia) {
7474

7575
// NotifyPeer informs all peers about a newly added node
7676
func NotifyPeer(p *BzzAddr, k *Kademlia) {
77-
f := func(val *Peer, po int, _ bool) bool {
77+
f := func(val *Peer, po int) bool {
7878
val.NotifyPeer(p, uint8(po))
7979
return true
8080
}
@@ -160,7 +160,7 @@ func (d *Peer) handleSubPeersMsg(msg *subPeersMsg) error {
160160
if !d.sentPeers {
161161
d.setDepth(msg.Depth)
162162
var peers []*BzzAddr
163-
d.kad.EachConn(d.Over(), 255, func(p *Peer, po int, isproxbin bool) bool {
163+
d.kad.EachConn(d.Over(), 255, func(p *Peer, po int) bool {
164164
if pob, _ := Pof(d, d.kad.BaseAddr(), 0); pob > po {
165165
return false
166166
}

swarm/network/hive.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (h *Hive) Stop() error {
114114
}
115115
}
116116
log.Info(fmt.Sprintf("%08x hive stopped, dropping peers", h.BaseAddr()[:4]))
117-
h.EachConn(nil, 255, func(p *Peer, _ int, _ bool) bool {
117+
h.EachConn(nil, 255, func(p *Peer, _ int) bool {
118118
log.Info(fmt.Sprintf("%08x dropping peer %08x", h.BaseAddr()[:4], p.Address()[:4]))
119119
p.Drop(nil)
120120
return true
@@ -228,7 +228,7 @@ func (h *Hive) loadPeers() error {
228228
// savePeers, savePeer implement persistence callback/
229229
func (h *Hive) savePeers() error {
230230
var peers []*BzzAddr
231-
h.Kademlia.EachAddr(nil, 256, func(pa *BzzAddr, i int, _ bool) bool {
231+
h.Kademlia.EachAddr(nil, 256, func(pa *BzzAddr, i int) bool {
232232
if pa == nil {
233233
log.Warn(fmt.Sprintf("empty addr: %v", i))
234234
return true

swarm/network/hive_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func TestHiveStatePersistance(t *testing.T) {
103103

104104
pp.Start(s1.Server)
105105
i := 0
106-
pp.Kademlia.EachAddr(nil, 256, func(addr *BzzAddr, po int, nn bool) bool {
106+
pp.Kademlia.EachAddr(nil, 256, func(addr *BzzAddr, po int) bool {
107107
delete(peers, addr.String())
108108
i++
109109
return true

swarm/network/kademlia.go

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -390,46 +390,42 @@ func (k *Kademlia) EachBin(base []byte, pof pot.Pof, o int, eachBinFunc func(con
390390
// EachConn is an iterator with args (base, po, f) applies f to each live peer
391391
// that has proximity order po or less as measured from the base
392392
// if base is nil, kademlia base address is used
393-
// It returns peers in order deepest to shallowest
394-
func (k *Kademlia) EachConn(base []byte, o int, f func(*Peer, int, bool) bool) {
393+
func (k *Kademlia) EachConn(base []byte, o int, f func(*Peer, int) bool) {
395394
k.lock.RLock()
396395
defer k.lock.RUnlock()
397396
k.eachConn(base, o, f)
398397
}
399398

400-
func (k *Kademlia) eachConn(base []byte, o int, f func(*Peer, int, bool) bool) {
399+
func (k *Kademlia) eachConn(base []byte, o int, f func(*Peer, int) bool) {
401400
if len(base) == 0 {
402401
base = k.base
403402
}
404-
depth := depthForPot(k.conns, k.MinProxBinSize, k.base)
405403
k.conns.EachNeighbour(base, Pof, func(val pot.Val, po int) bool {
406404
if po > o {
407405
return true
408406
}
409-
return f(val.(*Peer), po, po >= depth)
407+
return f(val.(*Peer), po)
410408
})
411409
}
412410

413411
// EachAddr called with (base, po, f) is an iterator applying f to each known peer
414412
// that has proximity order o or less as measured from the base
415413
// if base is nil, kademlia base address is used
416-
// It returns peers in order deepest to shallowest
417-
func (k *Kademlia) EachAddr(base []byte, o int, f func(*BzzAddr, int, bool) bool) {
414+
func (k *Kademlia) EachAddr(base []byte, o int, f func(*BzzAddr, int) bool) {
418415
k.lock.RLock()
419416
defer k.lock.RUnlock()
420417
k.eachAddr(base, o, f)
421418
}
422419

423-
func (k *Kademlia) eachAddr(base []byte, o int, f func(*BzzAddr, int, bool) bool) {
420+
func (k *Kademlia) eachAddr(base []byte, o int, f func(*BzzAddr, int) bool) {
424421
if len(base) == 0 {
425422
base = k.base
426423
}
427-
depth := depthForPot(k.conns, k.MinProxBinSize, k.base)
428424
k.addrs.EachNeighbour(base, Pof, func(val pot.Val, po int) bool {
429425
if po > o {
430426
return true
431427
}
432-
return f(val.(*entry).BzzAddr, po, po >= depth)
428+
return f(val.(*entry).BzzAddr, po)
433429
})
434430
}
435431

@@ -687,12 +683,11 @@ func (k *Kademlia) saturation() int {
687683
// TODO move to separate testing tools file
688684
func (k *Kademlia) knowNeighbours(addrs [][]byte) (got bool, n int, missing [][]byte) {
689685
pm := make(map[string]bool)
690-
686+
depth := depthForPot(k.conns, k.MinProxBinSize, k.base)
691687
// create a map with all peers at depth and deeper known in the kademlia
692-
// in order deepest to shallowest compared to the kademlia base address
693-
// all bins (except self) are included (0 <= bin <= 255)
694-
depth := depthForPot(k.addrs, k.MinProxBinSize, k.base)
695-
k.eachAddr(nil, 255, func(p *BzzAddr, po int, nn bool) bool {
688+
k.eachAddr(nil, 255, func(p *BzzAddr, po int) bool {
689+
// in order deepest to shallowest compared to the kademlia base address
690+
// all bins (except self) are included (0 <= bin <= 255)
696691
if po < depth {
697692
return false
698693
}
@@ -724,12 +719,8 @@ func (k *Kademlia) knowNeighbours(addrs [][]byte) (got bool, n int, missing [][]
724719
// It is used in Healthy function for testing only
725720
func (k *Kademlia) connectedNeighbours(peers [][]byte) (got bool, n int, missing [][]byte) {
726721
pm := make(map[string]bool)
727-
728-
// create a map with all peers at depth and deeper that are connected in the kademlia
729-
// in order deepest to shallowest compared to the kademlia base address
730-
// all bins (except self) are included (0 <= bin <= 255)
731722
depth := depthForPot(k.conns, k.MinProxBinSize, k.base)
732-
k.eachConn(nil, 255, func(p *Peer, po int, nn bool) bool {
723+
k.eachConn(nil, 255, func(p *Peer, po int) bool {
733724
if po < depth {
734725
return false
735726
}

swarm/network/kademlia_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func assertHealth(t *testing.T, k *Kademlia, expectHealthy bool, expectSaturatio
232232
t.Helper()
233233
kid := common.Bytes2Hex(k.BaseAddr())
234234
addrs := [][]byte{k.BaseAddr()}
235-
k.EachAddr(nil, 255, func(addr *BzzAddr, po int, _ bool) bool {
235+
k.EachAddr(nil, 255, func(addr *BzzAddr, po int) bool {
236236
addrs = append(addrs, addr.Address())
237237
return true
238238
})

swarm/network/networkid_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func TestNetworkID(t *testing.T) {
9292
if kademlias[node].addrs.Size() != len(netIDGroup)-1 {
9393
t.Fatalf("Kademlia size has not expected peer size. Kademlia size: %d, expected size: %d", kademlias[node].addrs.Size(), len(netIDGroup)-1)
9494
}
95-
kademlias[node].EachAddr(nil, 0, func(addr *BzzAddr, _ int, _ bool) bool {
95+
kademlias[node].EachAddr(nil, 0, func(addr *BzzAddr, _ int) bool {
9696
found := false
9797
for _, nd := range netIDGroup {
9898
if bytes.Equal(kademlias[nd].BaseAddr(), addr.Address()) {

swarm/network/stream/delivery.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package stream
1919
import (
2020
"context"
2121
"errors"
22-
2322
"fmt"
2423

2524
"github.com/ethereum/go-ethereum/metrics"
@@ -245,7 +244,7 @@ func (d *Delivery) RequestFromPeers(ctx context.Context, req *network.Request) (
245244
return nil, nil, fmt.Errorf("source peer %v not found", spID.String())
246245
}
247246
} else {
248-
d.kad.EachConn(req.Addr[:], 255, func(p *network.Peer, po int, nn bool) bool {
247+
d.kad.EachConn(req.Addr[:], 255, func(p *network.Peer, po int) bool {
249248
id := p.ID()
250249
if p.LightNode {
251250
// skip light nodes

swarm/network/stream/messages.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ func (p *Peer) handleWantedHashesMsg(ctx context.Context, req *WantedHashesMsg)
336336
// launch in go routine since GetBatch blocks until new hashes arrive
337337
go func() {
338338
if err := p.SendOfferedHashes(s, req.From, req.To); err != nil {
339-
log.Warn("SendOfferedHashes error", "err", err)
339+
log.Warn("SendOfferedHashes error", "peer", p.ID().TerminalString(), "err", err)
340340
}
341341
}()
342342
// go p.SendOfferedHashes(s, req.From, req.To)

swarm/pss/pss.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ func (p *Pss) forward(msg *PssMsg) error {
964964
onlySendOnce = true
965965
}
966966

967-
p.Kademlia.EachConn(to, addressLength*8, func(sp *network.Peer, po int, _ bool) bool {
967+
p.Kademlia.EachConn(to, addressLength*8, func(sp *network.Peer, po int) bool {
968968
if po < broadcastThreshold && sent > 0 {
969969
return false // stop iterating
970970
}

swarm/pss/pss_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,12 +491,12 @@ func TestAddressMatchProx(t *testing.T) {
491491
// meanwhile test regression for kademlia since we are compiling the test parameters from different packages
492492
var proxes int
493493
var conns int
494-
kad.EachConn(nil, peerCount, func(p *network.Peer, po int, prox bool) bool {
494+
depth := kad.NeighbourhoodDepth()
495+
kad.EachConn(nil, peerCount, func(p *network.Peer, po int) bool {
495496
conns++
496-
if prox {
497+
if po >= depth {
497498
proxes++
498499
}
499-
log.Trace("kadconn", "po", po, "peer", p, "prox", prox)
500500
return true
501501
})
502502
if proxes != nnPeerCount {

0 commit comments

Comments
 (0)