Skip to content

Commit 1636d95

Browse files
gluk256zelig
authored andcommitted
swarm/pot: pot.remove fixed (#18431)
* swarm/pot: refactored pot.remove(), updated comments * swarm/pot: comments updated
1 parent 88168ff commit 1636d95

File tree

3 files changed

+86
-20
lines changed

3 files changed

+86
-20
lines changed

swarm/pot/address.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ func ToBytes(v Val) []byte {
162162
}
163163

164164
// DefaultPof returns a proximity order comparison operator function
165-
// where all
166165
func DefaultPof(max int) func(one, other Val, pos int) (int, bool) {
167166
return func(one, other Val, pos int) (int, bool) {
168167
po, eq := proximityOrder(ToBytes(one), ToBytes(other), pos)
@@ -174,6 +173,9 @@ func DefaultPof(max int) func(one, other Val, pos int) (int, bool) {
174173
}
175174
}
176175

176+
// proximityOrder returns two parameters:
177+
// 1. relative proximity order of the arguments one & other;
178+
// 2. boolean indicating whether the full match occurred (one == other).
177179
func proximityOrder(one, other []byte, pos int) (int, bool) {
178180
for i := pos / 8; i < len(one); i++ {
179181
if one[i] == other[i] {

swarm/pot/pot.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,10 @@ func add(t *Pot, val Val, pof Pof) (*Pot, int, bool) {
144144
return r, po, found
145145
}
146146

147-
// Remove called on (v) deletes v from the Pot and returns
148-
// the proximity order of v and a boolean value indicating
149-
// if the value was found
150-
// Remove called on (t, v) returns a new Pot that contains all the elements of t
151-
// minus the value v, using the applicative remove
152-
// the second return value is the proximity order of the inserted element
153-
// the third is boolean indicating if the item was found
147+
// Remove deletes element v from the Pot t and returns three parameters:
148+
// 1. new Pot that contains all the elements of t minus the element v;
149+
// 2. proximity order of the removed element v;
150+
// 3. boolean indicating whether the item was found.
154151
func Remove(t *Pot, v Val, pof Pof) (*Pot, int, bool) {
155152
return remove(t, v, pof)
156153
}
@@ -161,10 +158,7 @@ func remove(t *Pot, val Val, pof Pof) (r *Pot, po int, found bool) {
161158
if found {
162159
size--
163160
if size == 0 {
164-
r = &Pot{
165-
po: t.po,
166-
}
167-
return r, po, true
161+
return &Pot{}, po, true
168162
}
169163
i := len(t.bins) - 1
170164
last := t.bins[i]
@@ -201,7 +195,7 @@ func remove(t *Pot, val Val, pof Pof) (r *Pot, po int, found bool) {
201195
}
202196
bins = append(bins, t.bins[j:]...)
203197
r = &Pot{
204-
pin: val,
198+
pin: t.pin,
205199
size: size,
206200
po: t.po,
207201
bins: bins,

swarm/pot/pot_test.go

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,72 @@ func testAdd(t *Pot, pof Pof, j int, values ...string) (_ *Pot, n int, f bool) {
8282
return t, n, f
8383
}
8484

85+
// removing non-existing element from pot
86+
func TestPotRemoveNonExisting(t *testing.T) {
87+
pof := DefaultPof(8)
88+
n := NewPot(newTestAddr("00111100", 0), 0)
89+
n, _, _ = Remove(n, newTestAddr("00000101", 0), pof)
90+
exp := "00111100"
91+
got := Label(n.Pin())
92+
if got[:8] != exp {
93+
t.Fatalf("incorrect pinned value. Expected %v, got %v", exp, got[:8])
94+
}
95+
}
96+
97+
// this test creates hierarchical pot tree, and therefore any child node will have
98+
// child_po = parent_po + 1.
99+
// then removes a node from the middle of the tree.
100+
func TestPotRemoveSameBin(t *testing.T) {
101+
pof := DefaultPof(8)
102+
n := NewPot(newTestAddr("11111111", 0), 0)
103+
n, _, _ = testAdd(n, pof, 1, "00000000", "01000000", "01100000", "01110000", "01111000")
104+
n, _, _ = Remove(n, newTestAddr("01110000", 0), pof)
105+
inds, po := indexes(n)
106+
goti := n.Size()
107+
expi := 5
108+
if goti != expi {
109+
t.Fatalf("incorrect number of elements in Pot. Expected %v, got %v", expi, goti)
110+
}
111+
inds, po = indexes(n)
112+
got := fmt.Sprintf("%v", inds)
113+
exp := "[5 3 2 1 0]"
114+
if got != exp {
115+
t.Fatalf("incorrect indexes in iteration over Pot. Expected %v, got %v", exp, got)
116+
}
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+
}
122+
}
123+
124+
// this test creates a flat pot tree (all the elements are leafs of one root),
125+
// and therefore they all have the same po.
126+
// then removes an arbitrary element from the pot.
127+
func TestPotRemoveDifferentBins(t *testing.T) {
128+
pof := DefaultPof(8)
129+
n := NewPot(newTestAddr("11111111", 0), 0)
130+
n, _, _ = testAdd(n, pof, 1, "00000000", "10000000", "11000000", "11100000", "11110000")
131+
n, _, _ = Remove(n, newTestAddr("11100000", 0), pof)
132+
inds, po := indexes(n)
133+
goti := n.Size()
134+
expi := 5
135+
if goti != expi {
136+
t.Fatalf("incorrect number of elements in Pot. Expected %v, got %v", expi, goti)
137+
}
138+
inds, po = indexes(n)
139+
got := fmt.Sprintf("%v", inds)
140+
exp := "[1 2 3 5 0]"
141+
if got != exp {
142+
t.Fatalf("incorrect indexes in iteration over Pot. Expected %v, got %v", exp, got)
143+
}
144+
got = fmt.Sprintf("%v", po)
145+
exp = "[0 1 2 4 0]"
146+
if got != exp {
147+
t.Fatalf("incorrect po-s in iteration over Pot. Expected %v, got %v", exp, got)
148+
}
149+
}
150+
85151
func TestPotAdd(t *testing.T) {
86152
pof := DefaultPof(8)
87153
n := NewPot(newTestAddr("00111100", 0), 0)
@@ -135,25 +201,29 @@ func TestPotRemove(t *testing.T) {
135201
t.Fatalf("incorrect number of elements in Pot. Expected %v, got %v", expi, goti)
136202
}
137203
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+
}
138209
got = fmt.Sprintf("%v", inds)
139-
exp = "[2 4 0]"
210+
exp = "[2 4 1]"
140211
if got != exp {
141212
t.Fatalf("incorrect indexes in iteration over Pot. Expected %v, got %v", exp, got)
142213
}
143-
got = fmt.Sprintf("%v", po)
144-
exp = "[1 3 0]"
214+
n, _, _ = Remove(n, newTestAddr("00111100", 0), pof) // remove again same element
215+
inds, _ = indexes(n)
216+
got = fmt.Sprintf("%v", inds)
145217
if got != exp {
146-
t.Fatalf("incorrect po-s in iteration over Pot. Expected %v, got %v", exp, got)
218+
t.Fatalf("incorrect indexes in iteration over Pot. Expected %v, got %v", exp, got)
147219
}
148-
// remove again
149-
n, _, _ = Remove(n, newTestAddr("00111100", 0), pof)
220+
n, _, _ = Remove(n, newTestAddr("00000000", 0), pof) // remove the first element
150221
inds, _ = indexes(n)
151222
got = fmt.Sprintf("%v", inds)
152223
exp = "[2 4]"
153224
if got != exp {
154225
t.Fatalf("incorrect indexes in iteration over Pot. Expected %v, got %v", exp, got)
155226
}
156-
157227
}
158228

159229
func TestPotSwap(t *testing.T) {

0 commit comments

Comments
 (0)