Skip to content

Commit 4aeeecf

Browse files
gluk256nonsense
authored andcommitted
swarm/pot: each() functions refactored (#18452)
1 parent 1636d95 commit 4aeeecf

File tree

3 files changed

+58
-82
lines changed

3 files changed

+58
-82
lines changed

swarm/network/kademlia.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (k *Kademlia) SuggestPeer() (a *BzzAddr, o int, want bool) {
198198

199199
var bpo []int
200200
prev := -1
201-
k.conns.EachBin(k.base, Pof, 0, func(po, size int, f func(func(val pot.Val, i int) bool) bool) bool {
201+
k.conns.EachBin(k.base, Pof, 0, func(po, size int, f func(func(val pot.Val) bool) bool) bool {
202202
prev++
203203
for ; prev < po; prev++ {
204204
bpo = append(bpo, prev)
@@ -219,12 +219,12 @@ func (k *Kademlia) SuggestPeer() (a *BzzAddr, o int, want bool) {
219219
// try to select a candidate peer
220220
// find the first callable peer
221221
nxt := bpo[0]
222-
k.addrs.EachBin(k.base, Pof, nxt, func(po, _ int, f func(func(pot.Val, int) bool) bool) bool {
222+
k.addrs.EachBin(k.base, Pof, nxt, func(po, _ int, f func(func(pot.Val) bool) bool) bool {
223223
// for each bin (up until depth) we find callable candidate peers
224224
if po >= depth {
225225
return false
226226
}
227-
return f(func(val pot.Val, _ int) bool {
227+
return f(func(val pot.Val) bool {
228228
e := val.(*entry)
229229
c := k.callable(e)
230230
if c {
@@ -442,7 +442,7 @@ func depthForPot(p *pot.Pot, neighbourhoodSize int, pivotAddr []byte) (depth int
442442
// the second step is to test for empty bins in order from shallowest to deepest
443443
// if an empty bin is found, this will be the actual depth
444444
// we stop iterating if we hit the maxDepth determined in the first step
445-
p.EachBin(pivotAddr, Pof, 0, func(po int, _ int, f func(func(pot.Val, int) bool) bool) bool {
445+
p.EachBin(pivotAddr, Pof, 0, func(po int, _ int, f func(func(pot.Val) bool) bool) bool {
446446
if po == depth {
447447
if maxDepth == depth {
448448
return false
@@ -514,14 +514,14 @@ func (k *Kademlia) string() string {
514514

515515
depth := depthForPot(k.conns, k.NeighbourhoodSize, k.base)
516516
rest := k.conns.Size()
517-
k.conns.EachBin(k.base, Pof, 0, func(po, size int, f func(func(val pot.Val, i int) bool) bool) bool {
517+
k.conns.EachBin(k.base, Pof, 0, func(po, size int, f func(func(val pot.Val) bool) bool) bool {
518518
var rowlen int
519519
if po >= k.MaxProxDisplay {
520520
po = k.MaxProxDisplay - 1
521521
}
522522
row := []string{fmt.Sprintf("%2d", size)}
523523
rest -= size
524-
f(func(val pot.Val, vpo int) bool {
524+
f(func(val pot.Val) bool {
525525
e := val.(*Peer)
526526
row = append(row, fmt.Sprintf("%x", e.Address()[:2]))
527527
rowlen++
@@ -533,7 +533,7 @@ func (k *Kademlia) string() string {
533533
return true
534534
})
535535

536-
k.addrs.EachBin(k.base, Pof, 0, func(po, size int, f func(func(val pot.Val, i int) bool) bool) bool {
536+
k.addrs.EachBin(k.base, Pof, 0, func(po, size int, f func(func(val pot.Val) bool) bool) bool {
537537
var rowlen int
538538
if po >= k.MaxProxDisplay {
539539
po = k.MaxProxDisplay - 1
@@ -543,7 +543,7 @@ func (k *Kademlia) string() string {
543543
}
544544
row := []string{fmt.Sprintf("%2d", size)}
545545
// we are displaying live peers too
546-
f(func(val pot.Val, vpo int) bool {
546+
f(func(val pot.Val) bool {
547547
e := val.(*entry)
548548
row = append(row, Label(e))
549549
rowlen++
@@ -634,7 +634,7 @@ func NewPeerPotMap(neighbourhoodSize int, addrs [][]byte) map[string]*PeerPot {
634634
// TODO this function will stop at the first bin with less than MinBinSize peers, even if there are empty bins between that bin and the depth. This may not be correct behavior
635635
func (k *Kademlia) saturation() int {
636636
prev := -1
637-
k.addrs.EachBin(k.base, Pof, 0, func(po, size int, f func(func(val pot.Val, i int) bool) bool) bool {
637+
k.addrs.EachBin(k.base, Pof, 0, func(po, size int, f func(func(val pot.Val) bool) bool) bool {
638638
prev++
639639
return prev == po && size >= k.MinBinSize
640640
})

swarm/pot/pot.go

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -447,60 +447,50 @@ func union(t0, t1 *Pot, pof Pof) (*Pot, int) {
447447
return n, common
448448
}
449449

450-
// Each called with (f) is a synchronous iterator over the bins of a node
451-
// respecting an ordering
452-
// proximity > pinnedness
453-
func (t *Pot) Each(f func(Val, int) bool) bool {
450+
// Each is a synchronous iterator over the elements of pot with function f.
451+
func (t *Pot) Each(f func(Val) bool) bool {
454452
return t.each(f)
455453
}
456454

457-
func (t *Pot) each(f func(Val, int) bool) bool {
458-
var next bool
455+
// each is a synchronous iterator over the elements of pot with function f.
456+
// the iteration ends if the function return false or there are no more elements.
457+
func (t *Pot) each(f func(Val) bool) bool {
458+
if t == nil || t.size == 0 {
459+
return false
460+
}
459461
for _, n := range t.bins {
460-
if n == nil {
461-
return true
462-
}
463-
next = n.each(f)
464-
if !next {
462+
if !n.each(f) {
465463
return false
466464
}
467465
}
468-
if t.size == 0 {
469-
return false
470-
}
471-
return f(t.pin, t.po)
466+
return f(t.pin)
472467
}
473468

474-
// eachFrom called with (f, start) is a synchronous iterator over the elements of a Pot
475-
// within the inclusive range starting from proximity order start
476-
// the function argument is passed the value and the proximity order wrt the root pin
477-
// it does NOT include the pinned item of the root
478-
// respecting an ordering
479-
// proximity > pinnedness
480-
// the iteration ends if the function return false or there are no more elements
481-
// end of a po range can be implemented since po is passed to the function
482-
func (t *Pot) eachFrom(f func(Val, int) bool, po int) bool {
483-
var next bool
484-
_, lim := t.getPos(po)
485-
for i := lim; i < len(t.bins); i++ {
486-
n := t.bins[i]
487-
next = n.each(f)
488-
if !next {
469+
// eachFrom is a synchronous iterator over the elements of pot with function f,
470+
// starting from certain proximity order po, which is passed as a second parameter.
471+
// the iteration ends if the function return false or there are no more elements.
472+
func (t *Pot) eachFrom(f func(Val) bool, po int) bool {
473+
if t == nil || t.size == 0 {
474+
return false
475+
}
476+
_, beg := t.getPos(po)
477+
for i := beg; i < len(t.bins); i++ {
478+
if !t.bins[i].each(f) {
489479
return false
490480
}
491481
}
492-
return f(t.pin, t.po)
482+
return f(t.pin)
493483
}
494484

495485
// EachBin iterates over bins of the pivot node and offers iterators to the caller on each
496486
// subtree passing the proximity order and the size
497487
// the iteration continues until the function's return value is false
498488
// or there are no more subtries
499-
func (t *Pot) EachBin(val Val, pof Pof, po int, f func(int, int, func(func(val Val, i int) bool) bool) bool) {
489+
func (t *Pot) EachBin(val Val, pof Pof, po int, f func(int, int, func(func(val Val) bool) bool) bool) {
500490
t.eachBin(val, pof, po, f)
501491
}
502492

503-
func (t *Pot) eachBin(val Val, pof Pof, po int, f func(int, int, func(func(val Val, i int) bool) bool) bool) {
493+
func (t *Pot) eachBin(val Val, pof Pof, po int, f func(int, int, func(func(val Val) bool) bool) bool) {
504494
if t == nil || t.size == 0 {
505495
return
506496
}
@@ -520,8 +510,8 @@ func (t *Pot) eachBin(val Val, pof Pof, po int, f func(int, int, func(func(val V
520510
}
521511
if lim == len(t.bins) {
522512
if spr >= po {
523-
f(spr, 1, func(g func(Val, int) bool) bool {
524-
return g(t.pin, spr)
513+
f(spr, 1, func(g func(Val) bool) bool {
514+
return g(t.pin)
525515
})
526516
}
527517
return
@@ -535,9 +525,9 @@ func (t *Pot) eachBin(val Val, pof Pof, po int, f func(int, int, func(func(val V
535525
size += n.size
536526
}
537527
if spr >= po {
538-
if !f(spr, t.size-size, func(g func(Val, int) bool) bool {
539-
return t.eachFrom(func(v Val, j int) bool {
540-
return g(v, spr)
528+
if !f(spr, t.size-size, func(g func(Val) bool) bool {
529+
return t.eachFrom(func(v Val) bool {
530+
return g(v)
541531
}, spo)
542532
}) {
543533
return
@@ -585,7 +575,7 @@ func (t *Pot) eachNeighbour(val Val, pof Pof, f func(Val, int) bool) bool {
585575
}
586576

587577
for i := l - 1; i > ir; i-- {
588-
next = t.bins[i].each(func(v Val, _ int) bool {
578+
next = t.bins[i].each(func(v Val) bool {
589579
return f(v, po)
590580
})
591581
if !next {
@@ -595,7 +585,7 @@ func (t *Pot) eachNeighbour(val Val, pof Pof, f func(Val, int) bool) bool {
595585

596586
for i := il - 1; i >= 0; i-- {
597587
n := t.bins[i]
598-
next = n.each(func(v Val, _ int) bool {
588+
next = n.each(func(v Val) bool {
599589
return f(v, n.po)
600590
})
601591
if !next {
@@ -709,7 +699,7 @@ func (t *Pot) eachNeighbourAsync(val Val, pof Pof, max int, maxPos int, f func(V
709699
wg.Add(m)
710700
}
711701
go func(pn *Pot, pm int) {
712-
pn.each(func(v Val, _ int) bool {
702+
pn.each(func(v Val) bool {
713703
if wg != nil {
714704
defer wg.Done()
715705
}
@@ -736,7 +726,7 @@ func (t *Pot) eachNeighbourAsync(val Val, pof Pof, max int, maxPos int, f func(V
736726
wg.Add(m)
737727
}
738728
go func(pn *Pot, pm int) {
739-
pn.each(func(v Val, _ int) bool {
729+
pn.each(func(v Val) bool {
740730
if wg != nil {
741731
defer wg.Done()
742732
}

swarm/pot/pot_test.go

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,13 @@ func randomtestAddr(n int, i int) *testAddr {
6565
return newTestAddr(v, i)
6666
}
6767

68-
func indexes(t *Pot) (i []int, po []int) {
69-
t.Each(func(v Val, p int) bool {
68+
func indexes(t *Pot) (i []int) {
69+
t.Each(func(v Val) bool {
7070
a := v.(*testAddr)
7171
i = append(i, a.i)
72-
po = append(po, p)
7372
return true
7473
})
75-
return i, po
74+
return i
7675
}
7776

7877
func testAdd(t *Pot, pof Pof, j int, values ...string) (_ *Pot, n int, f bool) {
@@ -102,23 +101,18 @@ func TestPotRemoveSameBin(t *testing.T) {
102101
n := NewPot(newTestAddr("11111111", 0), 0)
103102
n, _, _ = testAdd(n, pof, 1, "00000000", "01000000", "01100000", "01110000", "01111000")
104103
n, _, _ = Remove(n, newTestAddr("01110000", 0), pof)
105-
inds, po := indexes(n)
104+
inds := indexes(n)
106105
goti := n.Size()
107106
expi := 5
108107
if goti != expi {
109108
t.Fatalf("incorrect number of elements in Pot. Expected %v, got %v", expi, goti)
110109
}
111-
inds, po = indexes(n)
110+
inds = indexes(n)
112111
got := fmt.Sprintf("%v", inds)
113112
exp := "[5 3 2 1 0]"
114113
if got != exp {
115114
t.Fatalf("incorrect indexes in iteration over Pot. Expected %v, got %v", exp, got)
116115
}
117-
got = fmt.Sprintf("%v", po)
118-
exp = "[3 2 1 0 0]"
119-
if got != exp {
120-
t.Fatalf("incorrect po-s in iteration over Pot. Expected %v, got %v", exp, got)
121-
}
122116
}
123117

124118
// this test creates a flat pot tree (all the elements are leafs of one root),
@@ -129,22 +123,24 @@ func TestPotRemoveDifferentBins(t *testing.T) {
129123
n := NewPot(newTestAddr("11111111", 0), 0)
130124
n, _, _ = testAdd(n, pof, 1, "00000000", "10000000", "11000000", "11100000", "11110000")
131125
n, _, _ = Remove(n, newTestAddr("11100000", 0), pof)
132-
inds, po := indexes(n)
126+
inds := indexes(n)
133127
goti := n.Size()
134128
expi := 5
135129
if goti != expi {
136130
t.Fatalf("incorrect number of elements in Pot. Expected %v, got %v", expi, goti)
137131
}
138-
inds, po = indexes(n)
132+
inds = indexes(n)
139133
got := fmt.Sprintf("%v", inds)
140134
exp := "[1 2 3 5 0]"
141135
if got != exp {
142136
t.Fatalf("incorrect indexes in iteration over Pot. Expected %v, got %v", exp, got)
143137
}
144-
got = fmt.Sprintf("%v", po)
145-
exp = "[0 1 2 4 0]"
138+
n, _, _ = testAdd(n, pof, 4, "11100000")
139+
inds = indexes(n)
140+
got = fmt.Sprintf("%v", inds)
141+
exp = "[1 2 3 4 5 0]"
146142
if got != exp {
147-
t.Fatalf("incorrect po-s in iteration over Pot. Expected %v, got %v", exp, got)
143+
t.Fatalf("incorrect indexes in iteration over Pot. Expected %v, got %v", exp, got)
148144
}
149145
}
150146

@@ -171,17 +167,12 @@ func TestPotAdd(t *testing.T) {
171167
if goti != expi {
172168
t.Fatalf("incorrect number of elements in Pot. Expected %v, got %v", expi, goti)
173169
}
174-
inds, po := indexes(n)
170+
inds := indexes(n)
175171
got = fmt.Sprintf("%v", inds)
176172
exp = "[3 4 2]"
177173
if got != exp {
178174
t.Fatalf("incorrect indexes in iteration over Pot. Expected %v, got %v", exp, got)
179175
}
180-
got = fmt.Sprintf("%v", po)
181-
exp = "[1 2 0]"
182-
if got != exp {
183-
t.Fatalf("incorrect po-s in iteration over Pot. Expected %v, got %v", exp, got)
184-
}
185176
}
186177

187178
func TestPotRemove(t *testing.T) {
@@ -200,25 +191,20 @@ func TestPotRemove(t *testing.T) {
200191
if goti != expi {
201192
t.Fatalf("incorrect number of elements in Pot. Expected %v, got %v", expi, goti)
202193
}
203-
inds, po := indexes(n)
204-
got = fmt.Sprintf("%v", po)
205-
exp = "[1 3 0]"
206-
if got != exp {
207-
t.Fatalf("incorrect po-s in iteration over Pot. Expected %v, got %v", exp, got)
208-
}
194+
inds := indexes(n)
209195
got = fmt.Sprintf("%v", inds)
210196
exp = "[2 4 1]"
211197
if got != exp {
212198
t.Fatalf("incorrect indexes in iteration over Pot. Expected %v, got %v", exp, got)
213199
}
214200
n, _, _ = Remove(n, newTestAddr("00111100", 0), pof) // remove again same element
215-
inds, _ = indexes(n)
201+
inds = indexes(n)
216202
got = fmt.Sprintf("%v", inds)
217203
if got != exp {
218204
t.Fatalf("incorrect indexes in iteration over Pot. Expected %v, got %v", exp, got)
219205
}
220206
n, _, _ = Remove(n, newTestAddr("00000000", 0), pof) // remove the first element
221-
inds, _ = indexes(n)
207+
inds = indexes(n)
222208
got = fmt.Sprintf("%v", inds)
223209
exp = "[2 4]"
224210
if got != exp {
@@ -272,7 +258,7 @@ func TestPotSwap(t *testing.T) {
272258
})
273259
}
274260
sum := 0
275-
n.Each(func(v Val, i int) bool {
261+
n.Each(func(v Val) bool {
276262
if v == nil {
277263
return true
278264
}

0 commit comments

Comments
 (0)