Skip to content

Commit 7240f4d

Browse files
nolashzelig
authored andcommitted
swarm/network: Rename minproxbinsize, add as member of simulation (#18408)
* swarm/network: Rename minproxbinsize, add as member of simulation * swarm/network: Deactivate WaitTillHealthy, unreliable pending suggestpeer
1 parent 7ca4030 commit 7240f4d

19 files changed

+93
-84
lines changed

swarm/network/kademlia.go

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,27 @@ var Pof = pot.DefaultPof(256)
5454
// KadParams holds the config params for Kademlia
5555
type KadParams struct {
5656
// adjustable parameters
57-
MaxProxDisplay int // number of rows the table shows
58-
MinProxBinSize int // nearest neighbour core minimum cardinality
59-
MinBinSize int // minimum number of peers in a row
60-
MaxBinSize int // maximum number of peers in a row before pruning
61-
RetryInterval int64 // initial interval before a peer is first redialed
62-
RetryExponent int // exponent to multiply retry intervals with
63-
MaxRetries int // maximum number of redial attempts
57+
MaxProxDisplay int // number of rows the table shows
58+
NeighbourhoodSize int // nearest neighbour core minimum cardinality
59+
MinBinSize int // minimum number of peers in a row
60+
MaxBinSize int // maximum number of peers in a row before pruning
61+
RetryInterval int64 // initial interval before a peer is first redialed
62+
RetryExponent int // exponent to multiply retry intervals with
63+
MaxRetries int // maximum number of redial attempts
6464
// function to sanction or prevent suggesting a peer
6565
Reachable func(*BzzAddr) bool `json:"-"`
6666
}
6767

6868
// NewKadParams returns a params struct with default values
6969
func NewKadParams() *KadParams {
7070
return &KadParams{
71-
MaxProxDisplay: 16,
72-
MinProxBinSize: 2,
73-
MinBinSize: 2,
74-
MaxBinSize: 4,
75-
RetryInterval: 4200000000, // 4.2 sec
76-
MaxRetries: 42,
77-
RetryExponent: 2,
71+
MaxProxDisplay: 16,
72+
NeighbourhoodSize: 2,
73+
MinBinSize: 2,
74+
MaxBinSize: 4,
75+
RetryInterval: 4200000000, // 4.2 sec
76+
MaxRetries: 42,
77+
RetryExponent: 2,
7878
}
7979
}
8080

@@ -175,7 +175,7 @@ func (k *Kademlia) SuggestPeer() (a *BzzAddr, o int, want bool) {
175175
k.lock.Lock()
176176
defer k.lock.Unlock()
177177
minsize := k.MinBinSize
178-
depth := depthForPot(k.conns, k.MinProxBinSize, k.base)
178+
depth := depthForPot(k.conns, k.NeighbourhoodSize, k.base)
179179
// if there is a callable neighbour within the current proxBin, connect
180180
// this makes sure nearest neighbour set is fully connected
181181
var ppo int
@@ -306,7 +306,7 @@ func (k *Kademlia) sendNeighbourhoodDepthChange() {
306306
// It provides signaling of neighbourhood depth change.
307307
// This part of the code is sending new neighbourhood depth to nDepthC if that condition is met.
308308
if k.nDepthC != nil {
309-
nDepth := depthForPot(k.conns, k.MinProxBinSize, k.base)
309+
nDepth := depthForPot(k.conns, k.NeighbourhoodSize, k.base)
310310
if nDepth != k.nDepth {
311311
k.nDepth = nDepth
312312
k.nDepthC <- nDepth
@@ -366,7 +366,7 @@ func (k *Kademlia) EachBin(base []byte, pof pot.Pof, o int, eachBinFunc func(con
366366

367367
var startPo int
368368
var endPo int
369-
kadDepth := depthForPot(k.conns, k.MinProxBinSize, k.base)
369+
kadDepth := depthForPot(k.conns, k.NeighbourhoodSize, k.base)
370370

371371
k.conns.EachBin(base, Pof, o, func(po, size int, f func(func(val pot.Val, i int) bool) bool) bool {
372372
if startPo > 0 && endPo != k.MaxProxDisplay {
@@ -432,23 +432,23 @@ func (k *Kademlia) eachAddr(base []byte, o int, f func(*BzzAddr, int) bool) {
432432
func (k *Kademlia) NeighbourhoodDepth() (depth int) {
433433
k.lock.RLock()
434434
defer k.lock.RUnlock()
435-
return depthForPot(k.conns, k.MinProxBinSize, k.base)
435+
return depthForPot(k.conns, k.NeighbourhoodSize, k.base)
436436
}
437437

438438
// depthForPot returns the proximity order that defines the distance of
439-
// the nearest neighbour set with cardinality >= MinProxBinSize
440-
// if there is altogether less than MinProxBinSize peers it returns 0
439+
// the nearest neighbour set with cardinality >= NeighbourhoodSize
440+
// if there is altogether less than NeighbourhoodSize peers it returns 0
441441
// caller must hold the lock
442-
func depthForPot(p *pot.Pot, minProxBinSize int, pivotAddr []byte) (depth int) {
443-
if p.Size() <= minProxBinSize {
442+
func depthForPot(p *pot.Pot, neighbourhoodSize int, pivotAddr []byte) (depth int) {
443+
if p.Size() <= neighbourhoodSize {
444444
return 0
445445
}
446446

447447
// total number of peers in iteration
448448
var size int
449449

450450
// determining the depth is a two-step process
451-
// first we find the proximity bin of the shallowest of the MinProxBinSize peers
451+
// first we find the proximity bin of the shallowest of the NeighbourhoodSize peers
452452
// the numeric value of depth cannot be higher than this
453453
var maxDepth int
454454

@@ -461,7 +461,7 @@ func depthForPot(p *pot.Pot, minProxBinSize int, pivotAddr []byte) (depth int) {
461461

462462
// this means we have all nn-peers.
463463
// depth is by default set to the bin of the farthest nn-peer
464-
if size == minProxBinSize {
464+
if size == neighbourhoodSize {
465465
maxDepth = i
466466
return false
467467
}
@@ -538,12 +538,12 @@ func (k *Kademlia) string() string {
538538

539539
rows = append(rows, "=========================================================================")
540540
rows = append(rows, fmt.Sprintf("%v KΛÐΞMLIΛ hive: queen's address: %x", time.Now().UTC().Format(time.UnixDate), k.BaseAddr()[:3]))
541-
rows = append(rows, fmt.Sprintf("population: %d (%d), MinProxBinSize: %d, MinBinSize: %d, MaxBinSize: %d", k.conns.Size(), k.addrs.Size(), k.MinProxBinSize, k.MinBinSize, k.MaxBinSize))
541+
rows = append(rows, fmt.Sprintf("population: %d (%d), NeighbourhoodSize: %d, MinBinSize: %d, MaxBinSize: %d", k.conns.Size(), k.addrs.Size(), k.NeighbourhoodSize, k.MinBinSize, k.MaxBinSize))
542542

543543
liverows := make([]string, k.MaxProxDisplay)
544544
peersrows := make([]string, k.MaxProxDisplay)
545545

546-
depth := depthForPot(k.conns, k.MinProxBinSize, k.base)
546+
depth := depthForPot(k.conns, k.NeighbourhoodSize, k.base)
547547
rest := k.conns.Size()
548548
k.conns.EachBin(k.base, Pof, 0, func(po, size int, f func(func(val pot.Val, i int) bool) bool) bool {
549549
var rowlen int
@@ -611,10 +611,10 @@ type PeerPot struct {
611611

612612
// NewPeerPotMap creates a map of pot record of *BzzAddr with keys
613613
// as hexadecimal representations of the address.
614-
// the MinProxBinSize of the passed kademlia is used
614+
// the NeighbourhoodSize of the passed kademlia is used
615615
// used for testing only
616616
// TODO move to separate testing tools file
617-
func NewPeerPotMap(minProxBinSize int, addrs [][]byte) map[string]*PeerPot {
617+
func NewPeerPotMap(neighbourhoodSize int, addrs [][]byte) map[string]*PeerPot {
618618

619619
// create a table of all nodes for health check
620620
np := pot.NewPot(nil, 0)
@@ -628,7 +628,7 @@ func NewPeerPotMap(minProxBinSize int, addrs [][]byte) map[string]*PeerPot {
628628
for i, a := range addrs {
629629

630630
// actual kademlia depth
631-
depth := depthForPot(np, minProxBinSize, a)
631+
depth := depthForPot(np, neighbourhoodSize, a)
632632

633633
// all nn-peers
634634
var nns [][]byte
@@ -670,7 +670,7 @@ func (k *Kademlia) saturation() int {
670670
return prev == po && size >= k.MinBinSize
671671
})
672672
// TODO evaluate whether this check cannot just as well be done within the eachbin
673-
depth := depthForPot(k.conns, k.MinProxBinSize, k.base)
673+
depth := depthForPot(k.conns, k.NeighbourhoodSize, k.base)
674674
if depth < prev {
675675
return depth
676676
}
@@ -683,7 +683,7 @@ func (k *Kademlia) saturation() int {
683683
// TODO move to separate testing tools file
684684
func (k *Kademlia) knowNeighbours(addrs [][]byte) (got bool, n int, missing [][]byte) {
685685
pm := make(map[string]bool)
686-
depth := depthForPot(k.conns, k.MinProxBinSize, k.base)
686+
depth := depthForPot(k.conns, k.NeighbourhoodSize, k.base)
687687
// create a map with all peers at depth and deeper known in the kademlia
688688
k.eachAddr(nil, 255, func(p *BzzAddr, po int) bool {
689689
// in order deepest to shallowest compared to the kademlia base address
@@ -719,7 +719,11 @@ func (k *Kademlia) knowNeighbours(addrs [][]byte) (got bool, n int, missing [][]
719719
// It is used in Healthy function for testing only
720720
func (k *Kademlia) connectedNeighbours(peers [][]byte) (got bool, n int, missing [][]byte) {
721721
pm := make(map[string]bool)
722-
depth := depthForPot(k.conns, k.MinProxBinSize, k.base)
722+
723+
// create a map with all peers at depth and deeper that are connected in the kademlia
724+
// in order deepest to shallowest compared to the kademlia base address
725+
// all bins (except self) are included (0 <= bin <= 255)
726+
depth := depthForPot(k.conns, k.NeighbourhoodSize, k.base)
723727
k.eachConn(nil, 255, func(p *Peer, po int) bool {
724728
if po < depth {
725729
return false
@@ -772,7 +776,7 @@ func (k *Kademlia) Healthy(pp *PeerPot) *Health {
772776
defer k.lock.RUnlock()
773777
gotnn, countgotnn, culpritsgotnn := k.connectedNeighbours(pp.NNSet)
774778
knownn, countknownn, culpritsknownn := k.knowNeighbours(pp.NNSet)
775-
depth := depthForPot(k.conns, k.MinProxBinSize, k.base)
779+
depth := depthForPot(k.conns, k.NeighbourhoodSize, k.base)
776780
saturated := k.saturation() < depth
777781
log.Trace(fmt.Sprintf("%08x: healthy: knowNNs: %v, gotNNs: %v, saturated: %v\n", k.base, knownn, gotnn, saturated))
778782
return &Health{

swarm/network/kademlia_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func newTestKademliaParams() *KadParams {
4545
params := NewKadParams()
4646
// TODO why is this 1?
4747
params.MinBinSize = 1
48-
params.MinProxBinSize = 2
48+
params.NeighbourhoodSize = 2
4949
return params
5050
}
5151

@@ -87,7 +87,7 @@ func Register(k *Kademlia, regs ...string) {
8787
// empty bins above the farthest "nearest neighbor-peer" then
8888
// the depth should be set at the farthest of those empty bins
8989
//
90-
// TODO: Make test adapt to change in MinProxBinSize
90+
// TODO: Make test adapt to change in NeighbourhoodSize
9191
func TestNeighbourhoodDepth(t *testing.T) {
9292
baseAddressBytes := RandomAddr().OAddr
9393
kad := NewKademlia(baseAddressBytes, NewKadParams())
@@ -237,7 +237,7 @@ func assertHealth(t *testing.T, k *Kademlia, expectHealthy bool, expectSaturatio
237237
return true
238238
})
239239

240-
pp := NewPeerPotMap(k.MinProxBinSize, addrs)
240+
pp := NewPeerPotMap(k.NeighbourhoodSize, addrs)
241241
healthParams := k.Healthy(pp[kid])
242242

243243
// definition of health, all conditions but be true:
@@ -605,7 +605,7 @@ func TestKademliaHiveString(t *testing.T) {
605605
Register(k, "10000000", "10000001")
606606
k.MaxProxDisplay = 8
607607
h := k.String()
608-
expH := "\n=========================================================================\nMon Feb 27 12:10:28 UTC 2017 KΛÐΞMLIΛ hive: queen's address: 000000\npopulation: 2 (4), MinProxBinSize: 2, MinBinSize: 1, MaxBinSize: 4\n============ DEPTH: 0 ==========================================\n000 0 | 2 8100 (0) 8000 (0)\n001 1 4000 | 1 4000 (0)\n002 1 2000 | 1 2000 (0)\n003 0 | 0\n004 0 | 0\n005 0 | 0\n006 0 | 0\n007 0 | 0\n========================================================================="
608+
expH := "\n=========================================================================\nMon Feb 27 12:10:28 UTC 2017 KΛÐΞMLIΛ hive: queen's address: 000000\npopulation: 2 (4), NeighbourhoodSize: 2, MinBinSize: 1, MaxBinSize: 4\n============ DEPTH: 0 ==========================================\n000 0 | 2 8100 (0) 8000 (0)\n001 1 4000 | 1 4000 (0)\n002 1 2000 | 1 2000 (0)\n003 0 | 0\n004 0 | 0\n005 0 | 0\n006 0 | 0\n007 0 | 0\n========================================================================="
609609
if expH[104:] != h[104:] {
610610
t.Fatalf("incorrect hive output. expected %v, got %v", expH, h)
611611
}
@@ -636,7 +636,7 @@ func testKademliaCase(t *testing.T, pivotAddr string, addrs ...string) {
636636
}
637637
}
638638

639-
ppmap := NewPeerPotMap(k.MinProxBinSize, byteAddrs)
639+
ppmap := NewPeerPotMap(k.NeighbourhoodSize, byteAddrs)
640640

641641
pp := ppmap[pivotAddr]
642642

@@ -662,7 +662,7 @@ in higher level tests for streaming. They were generated randomly.
662662
663663
=========================================================================
664664
Mon Apr 9 12:18:24 UTC 2018 KΛÐΞMLIΛ hive: queen's address: 7efef1
665-
population: 9 (49), MinProxBinSize: 2, MinBinSize: 2, MaxBinSize: 4
665+
population: 9 (49), NeighbourhoodSize: 2, MinBinSize: 2, MaxBinSize: 4
666666
000 2 d7e5 ec56 | 18 ec56 (0) d7e5 (0) d9e0 (0) c735 (0)
667667
001 2 18f1 3176 | 14 18f1 (0) 10bb (0) 10d1 (0) 0421 (0)
668668
002 2 52aa 47cd | 11 52aa (0) 51d9 (0) 5161 (0) 5130 (0)
@@ -745,7 +745,7 @@ in higher level tests for streaming. They were generated randomly.
745745
746746
=========================================================================
747747
Mon Apr 9 18:43:48 UTC 2018 KΛÐΞMLIΛ hive: queen's address: bc7f3b
748-
population: 9 (49), MinProxBinSize: 2, MinBinSize: 2, MaxBinSize: 4
748+
population: 9 (49), NeighbourhoodSize: 2, MinBinSize: 2, MaxBinSize: 4
749749
000 2 0f49 67ff | 28 0f49 (0) 0211 (0) 07b2 (0) 0703 (0)
750750
001 2 e84b f3a4 | 13 f3a4 (0) e84b (0) e58b (0) e60b (0)
751751
002 1 8dba | 1 8dba (0)
@@ -779,7 +779,7 @@ in higher level tests for streaming. They were generated randomly.
779779
780780
=========================================================================
781781
Mon Apr 9 19:04:35 UTC 2018 KΛÐΞMLIΛ hive: queen's address: b4822e
782-
population: 8 (49), MinProxBinSize: 2, MinBinSize: 2, MaxBinSize: 4
782+
population: 8 (49), NeighbourhoodSize: 2, MinBinSize: 2, MaxBinSize: 4
783783
000 2 786c 774b | 29 774b (0) 786c (0) 7a79 (0) 7d2f (0)
784784
001 2 d9de cf19 | 10 cf19 (0) d9de (0) d2ff (0) d2a2 (0)
785785
002 2 8ca1 8d74 | 5 8d74 (0) 8ca1 (0) 9793 (0) 9f51 (0)
@@ -813,7 +813,7 @@ in higher level tests for streaming. They were generated randomly.
813813
814814
=========================================================================
815815
Mon Apr 9 19:16:25 UTC 2018 KΛÐΞMLIΛ hive: queen's address: 9a90fe
816-
population: 8 (49), MinProxBinSize: 2, MinBinSize: 2, MaxBinSize: 4
816+
population: 8 (49), NeighbourhoodSize: 2, MinBinSize: 2, MaxBinSize: 4
817817
000 2 72ef 4e6c | 24 0b1e (0) 0d66 (0) 17f5 (0) 17e8 (0)
818818
001 2 fc2b fa47 | 13 fa47 (0) fc2b (0) fffd (0) ecef (0)
819819
002 2 b847 afa8 | 6 afa8 (0) ad77 (0) bb7c (0) b847 (0)
@@ -848,7 +848,7 @@ in higher level tests for streaming. They were generated randomly.
848848
849849
=========================================================================
850850
Mon Apr 9 19:25:18 UTC 2018 KΛÐΞMLIΛ hive: queen's address: 5dd5c7
851-
population: 13 (49), MinProxBinSize: 2, MinBinSize: 2, MaxBinSize: 4
851+
population: 13 (49), NeighbourhoodSize: 2, MinBinSize: 2, MaxBinSize: 4
852852
000 2 e528 fad0 | 22 fad0 (0) e528 (0) e3bb (0) ed13 (0)
853853
001 3 3f30 18e0 1dd3 | 7 3f30 (0) 23db (0) 10b6 (0) 18e0 (0)
854854
002 4 7c54 7804 61e4 60f9 | 10 61e4 (0) 60f9 (0) 636c (0) 7186 (0)

swarm/network/networkid_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func newServices() adapters.Services {
188188
return k
189189
}
190190
params := NewKadParams()
191-
params.MinProxBinSize = 2
191+
params.NeighbourhoodSize = 2
192192
params.MaxBinSize = 3
193193
params.MinBinSize = 1
194194
params.MaxRetries = 1000

swarm/network/simulation/example_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@ package simulation_test
1818

1919
import (
2020
"context"
21+
"fmt"
22+
"sync"
23+
"time"
2124

2225
"github.com/ethereum/go-ethereum/log"
26+
"github.com/ethereum/go-ethereum/node"
27+
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
28+
"github.com/ethereum/go-ethereum/swarm/network"
2329
"github.com/ethereum/go-ethereum/swarm/network/simulation"
2430
)
2531

@@ -28,10 +34,6 @@ import (
2834
// all nodes have the their Kademlias healthy.
2935
func ExampleSimulation_WaitTillHealthy() {
3036

31-
log.Error("temporarily disabled as simulations.WaitTillHealthy cannot be trusted")
32-
33-
/* Commented out to avoid go vet errors/warnings
34-
3537
sim := simulation.New(map[string]simulation.ServiceFunc{
3638
"bzz": func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) {
3739
addr := network.NewAddr(ctx.Config.Node())
@@ -59,7 +61,7 @@ func ExampleSimulation_WaitTillHealthy() {
5961

6062
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
6163
defer cancel()
62-
ill, err := sim.WaitTillHealthy(ctx, 2)
64+
ill, err := sim.WaitTillHealthy(ctx)
6365
if err != nil {
6466
// inspect the latest detected not healthy kademlias
6567
for id, kad := range ill {
@@ -71,7 +73,6 @@ func ExampleSimulation_WaitTillHealthy() {
7173

7274
// continue with the test
7375

74-
*/
7576
}
7677

7778
// Watch all peer events in the simulation network, buy receiving from a channel.

swarm/network/simulation/kademlia.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var BucketKeyKademlia BucketKey = "kademlia"
3434
// WaitTillHealthy is blocking until the health of all kademlias is true.
3535
// If error is not nil, a map of kademlia that was found not healthy is returned.
3636
// TODO: Check correctness since change in kademlia depth calculation logic
37-
func (s *Simulation) WaitTillHealthy(ctx context.Context, kadMinProxSize int) (ill map[enode.ID]*network.Kademlia, err error) {
37+
func (s *Simulation) WaitTillHealthy(ctx context.Context) (ill map[enode.ID]*network.Kademlia, err error) {
3838
// Prepare PeerPot map for checking Kademlia health
3939
var ppmap map[string]*network.PeerPot
4040
kademlias := s.kademlias()
@@ -43,7 +43,7 @@ func (s *Simulation) WaitTillHealthy(ctx context.Context, kadMinProxSize int) (i
4343
for _, k := range kademlias {
4444
addrs = append(addrs, k.BaseAddr())
4545
}
46-
ppmap = network.NewPeerPotMap(kadMinProxSize, addrs)
46+
ppmap = network.NewPeerPotMap(s.neighbourhoodSize, addrs)
4747

4848
// Wait for healthy Kademlia on every node before checking files
4949
ticker := time.NewTicker(200 * time.Millisecond)

swarm/network/simulation/kademlia_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
)
2929

3030
func TestWaitTillHealthy(t *testing.T) {
31+
t.Skip("WaitTillHealthy depends on discovery, which relies on a reliable SuggestPeer, which is not reliable")
3132

3233
sim := New(map[string]ServiceFunc{
3334
"bzz": func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) {
@@ -54,7 +55,7 @@ func TestWaitTillHealthy(t *testing.T) {
5455

5556
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
5657
defer cancel()
57-
ill, err := sim.WaitTillHealthy(ctx, 2)
58+
ill, err := sim.WaitTillHealthy(ctx)
5859
if err != nil {
5960
for id, kad := range ill {
6061
t.Log("Node", id)

0 commit comments

Comments
 (0)