fix wantlist removal accounting, add tests
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
Jeromy committed
May 2, 2017 at 22:54 UTC
b680f49363f9d699086644f1ad7d3a02c11e8e11
5 files changed
+38
-17
exchange/bitswap/bitswap.go
+3
@@ -285,6 +285,9 @@ func (bs *Bitswap) getNextSessionID() uint64 {
285
286
// CancelWant removes a given key from the wantlist
287
func (bs *Bitswap) CancelWants(cids []*cid.Cid, ses uint64) {
288
+ if len(cids) == 0 {
289
+ return
290
+ }
291
bs.wm.CancelWants(context.Background(), cids, nil, ses)
292
}
293
exchange/bitswap/bitswap_test.go
+5
-1
@@ -318,7 +318,7 @@ func TestBasicBitswap(t *testing.T) {
318
319
t.Log("Test a one node trying to get one block from another")
320
321
- instances := sg.Instances(2)
321
+ instances := sg.Instances(3)
322
blocks := bg.Blocks(1)
323
err := instances[0].Exchange.HasBlock(blocks[0])
324
if err != nil {
@@ -333,6 +333,10 @@ func TestBasicBitswap(t *testing.T) {
333
}
334
335
time.Sleep(time.Millisecond * 20)
336
+ wl := instances[2].Exchange.WantlistForPeer(instances[1].Peer)
337
+ if len(wl) != 0 {
338
+ t.Fatal("should have no items in other peers wantlist")
339
+ }
340
if len(instances[1].Exchange.GetWantlist()) != 0 {
341
t.Fatal("shouldnt have anything in wantlist")
342
}
exchange/bitswap/decision/engine.go
+4
-7
@@ -105,13 +105,10 @@ func NewEngine(ctx context.Context, bs bstore.Blockstore) *Engine {
105
}
106
107
func (e *Engine) WantlistForPeer(p peer.ID) (out []*wl.Entry) {
108
- e.lock.Lock()
109
- partner, ok := e.ledgerMap[p]
110
- if ok {
111
- out = partner.wantList.SortedEntries()
112
- }
113
- e.lock.Unlock()
114
- return out
108
+ partner := e.findOrCreate(p)
109
+ partner.lk.Lock()
110
+ defer partner.lk.Unlock()
111
+ return partner.wantList.SortedEntries()
112
}
113
114
func (e *Engine) LedgerForPeer(p peer.ID) *Receipt {
exchange/bitswap/wantlist/wantlist.go
+1
-1
@@ -170,7 +170,7 @@ func (w *Wantlist) Remove(c *cid.Cid) bool {
170
}
171
172
delete(w.set, k)
173
- return false
173
+ return true
174
}
175
176
func (w *Wantlist) Contains(k *cid.Cid) (*Entry, bool) {
exchange/bitswap/wantlist/wantlist_test.go
+25
-8
@@ -48,9 +48,13 @@ func assertNotHasCid(t *testing.T, w wli, c *cid.Cid) {
48
func TestBasicWantlist(t *testing.T) {
49
wl := New()
50
51
- wl.Add(testcids[0], 5)
51
+ if !wl.Add(testcids[0], 5) {
52
+ t.Fatal("expected true")
53
+ }
54
assertHasCid(t, wl, testcids[0])
53
- wl.Add(testcids[1], 4)
55
+ if !wl.Add(testcids[1], 4) {
56
+ t.Fatal("expected true")
57
+ }
58
assertHasCid(t, wl, testcids[0])
59
assertHasCid(t, wl, testcids[1])
60
@@ -58,7 +62,9 @@ func TestBasicWantlist(t *testing.T) {
62
t.Fatal("should have had two items")
63
}
64
61
- wl.Add(testcids[1], 4)
65
+ if wl.Add(testcids[1], 4) {
66
+ t.Fatal("add shouldnt report success on second add")
67
+ }
68
assertHasCid(t, wl, testcids[0])
69
assertHasCid(t, wl, testcids[1])
70
@@ -66,7 +72,10 @@ func TestBasicWantlist(t *testing.T) {
72
t.Fatal("should have had two items")
73
}
74
69
- wl.Remove(testcids[0])
75
+ if !wl.Remove(testcids[0]) {
76
+ t.Fatal("should have gotten true")
77
+ }
78
+
79
assertHasCid(t, wl, testcids[1])
80
if _, has := wl.Contains(testcids[0]); has {
81
t.Fatal("shouldnt have this cid")
@@ -76,12 +85,20 @@ func TestBasicWantlist(t *testing.T) {
85
func TestSesRefWantlist(t *testing.T) {
86
wl := NewThreadSafe()
87
79
- wl.Add(testcids[0], 5, 1)
88
+ if !wl.Add(testcids[0], 5, 1) {
89
+ t.Fatal("should have added")
90
+ }
91
assertHasCid(t, wl, testcids[0])
81
- wl.Remove(testcids[0], 2)
92
+ if wl.Remove(testcids[0], 2) {
93
+ t.Fatal("shouldnt have removed")
94
+ }
95
assertHasCid(t, wl, testcids[0])
83
- wl.Add(testcids[0], 5, 1)
96
+ if wl.Add(testcids[0], 5, 1) {
97
+ t.Fatal("shouldnt have added")
98
+ }
99
assertHasCid(t, wl, testcids[0])
85
- wl.Remove(testcids[0], 1)
100
+ if !wl.Remove(testcids[0], 1) {
101
+ t.Fatal("should have removed")
102
+ }
103
assertNotHasCid(t, wl, testcids[0])
104
}