ensure sending of wantlist to friendly peers
Jeromy committed
Nov 22, 2014 at 22:27 UTC
6d217b53114ec053b0a3df79c544e9d9468437cc
3 files changed
+22
-4
blockservice/blocks_test.go
+4
@@ -10,6 +10,7 @@ import (
10
dssync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
11
blocks "github.com/jbenet/go-ipfs/blocks"
12
blockstore "github.com/jbenet/go-ipfs/blocks/blockstore"
13
+ bitswap "github.com/jbenet/go-ipfs/exchange/bitswap"
14
offline "github.com/jbenet/go-ipfs/exchange/offline"
15
u "github.com/jbenet/go-ipfs/util"
16
)
@@ -58,3 +59,6 @@ func TestBlocks(t *testing.T) {
59
t.Error("Block data is not equal.")
60
}
61
}
62
+
63
+func TestGetBlocks(t *testing.T) {
64
+}
exchange/bitswap/bitswap.go
+16
-3
@@ -262,6 +262,7 @@ func (bs *bitswap) ReceiveMessage(ctx context.Context, p peer.Peer, incoming bsm
262
}
263
}
264
265
+ first := true
266
for _, key := range incoming.Wantlist() {
267
// TODO: might be better to check if we have the block before checking
268
// if we should send it to someone
@@ -272,9 +273,11 @@ func (bs *bitswap) ReceiveMessage(ctx context.Context, p peer.Peer, incoming bsm
273
// Create a separate message to send this block in
274
blkmsg := bsmsg.New()
275
275
- // TODO: only send this the first time
276
- for _, k := range bs.wantlist.Keys() {
277
- blkmsg.AddWanted(k)
276
+ if first {
277
+ for _, k := range bs.wantlist.Keys() {
278
+ blkmsg.AddWanted(k)
279
+ }
280
+ first = false
281
}
282
283
blkmsg.AddBlock(block)
@@ -284,6 +287,16 @@ func (bs *bitswap) ReceiveMessage(ctx context.Context, p peer.Peer, incoming bsm
287
}
288
}
289
290
+ // If they send us a block, we should guarantee that we send
291
+ // them our updated want list one way or another
292
+ if len(incoming.Blocks()) > 0 && first {
293
+ message := bsmsg.New()
294
+ for _, k := range bs.wantlist.Keys() {
295
+ message.AddWanted(k)
296
+ }
297
+ return p, message
298
+ }
299
+
300
return nil, nil
301
}
302
exchange/bitswap/bitswap_test.go
+2
-1
@@ -1,4 +1,4 @@
1
-package bitswap
1
+package bitswap_test
2
3
import (
4
"bytes"
@@ -7,6 +7,7 @@ import (
7
"time"
8
9
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
10
+ . "github.com/jbenet/go-ipfs/exchange/bitswap"
11
12
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
13
ds_sync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"