add blocks to the blockstore before returning them from blockservice sessions.
fixes #4062 (yay!) License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
Steven Allen committed
Aug 23, 2017 at 21:02 UTC
b22d3fbfd9a8976d44e713190211c4592786120a
1 file changed
+17
-15
exchange/bitswap/bitswap.go
+17
-15
@@ -298,6 +298,14 @@ func (bs *Bitswap) CancelWants(cids []*cid.Cid, ses uint64) {
298
// HasBlock announces the existance of a block to this bitswap service. The
299
// service will potentially notify its peers.
300
func (bs *Bitswap) HasBlock(blk blocks.Block) error {
301
+ return bs.receiveBlockFrom(blk, "")
302
+}
303
+
304
+// TODO: Some of this stuff really only needs to be done when adding a block
305
+// from the user, not when receiving it from the network.
306
+// In case you run `git blame` on this comment, I'll save you some time: ask
307
+// @whyrusleeping, I don't know the answers you seek.
308
+func (bs *Bitswap) receiveBlockFrom(blk blocks.Block, from peer.ID) error {
309
select {
310
case <-bs.process.Closing():
311
return errors.New("bitswap is closed")
@@ -317,8 +325,11 @@ func (bs *Bitswap) HasBlock(blk blocks.Block) error {
325
// it now as it requires more thought and isnt causing immediate problems.
326
bs.notifications.Publish(blk)
327
320
- for _, s := range bs.SessionsForBlock(blk.Cid()) {
321
- s.receiveBlockFrom("", blk)
328
+ k := blk.Cid()
329
+ ks := []*cid.Cid{k}
330
+ for _, s := range bs.SessionsForBlock(k) {
331
+ s.receiveBlockFrom(from, blk)
332
+ bs.CancelWants(ks, s.id)
333
}
334
335
bs.engine.AddBlock(blk)
@@ -379,21 +390,12 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg
390
391
bs.updateReceiveCounters(b)
392
382
- k := b.Cid()
383
- log.Event(ctx, "Bitswap.GetBlockRequest.End", k)
384
-
385
- for _, ses := range bs.SessionsForBlock(k) {
386
- ses.receiveBlockFrom(p, b)
387
- bs.CancelWants([]*cid.Cid{k}, ses.id)
388
- }
389
-
393
log.Debugf("got block %s from %s", b, p)
391
- // TODO: rework this to not call 'HasBlock'. 'HasBlock' is really
392
- // designed to be called when blocks are coming in from non-bitswap
393
- // places (like the user manually adding data)
394
- if err := bs.HasBlock(b); err != nil {
395
- log.Warningf("ReceiveMessage HasBlock error: %s", err)
394
+
395
+ if err := bs.receiveBlockFrom(b, p); err != nil {
396
+ log.Warningf("ReceiveMessage recvBlockFrom error: %s", err)
397
}
398
+ log.Event(ctx, "Bitswap.GetBlockRequest.End", b.Cid())
399
}(block)
400
}
401
wg.Wait()