Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #24 from ipfs/bugs/dont-receive-unwanted-blocks-21
Browse files Browse the repository at this point in the history
fix(Receiver): Ignore unwanted blocks
  • Loading branch information
hannahhoward authored Nov 15, 2018
2 parents edf2496 + 779c923 commit c5b071d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions bitswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg

wg := sync.WaitGroup{}
for _, block := range iblocks {

wg.Add(1)
go func(b blocks.Block) { // TODO: this probably doesnt need to be a goroutine...
defer wg.Done()
Expand All @@ -396,6 +397,11 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg

log.Debugf("got block %s from %s", b, p)

// skip received blocks that are not in the wantlist
if _, contains := bs.wm.wl.Contains(b.Cid()); !contains {
return
}

if err := bs.receiveBlockFrom(b, p); err != nil {
log.Warningf("ReceiveMessage recvBlockFrom error: %s", err)
}
Expand Down
33 changes: 33 additions & 0 deletions bitswap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

decision "github.com/ipfs/go-bitswap/decision"
"github.com/ipfs/go-bitswap/message"
tn "github.com/ipfs/go-bitswap/testnet"

blocks "github.com/ipfs/go-block-format"
Expand Down Expand Up @@ -98,6 +99,38 @@ func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {
}
}

func TestUnwantedBlockNotAdded(t *testing.T) {

net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
block := blocks.NewBlock([]byte("block"))
bsMessage := message.New(true)
bsMessage.AddBlock(block)

g := NewTestSessionGenerator(net)
defer g.Close()

peers := g.Instances(2)
hasBlock := peers[0]
defer hasBlock.Exchange.Close()

if err := hasBlock.Exchange.HasBlock(block); err != nil {
t.Fatal(err)
}

doesNotWantBlock := peers[1]
defer doesNotWantBlock.Exchange.Close()

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

doesNotWantBlock.Exchange.ReceiveMessage(ctx, hasBlock.Peer, bsMessage)

blockInStore, err := doesNotWantBlock.blockstore.Has(block.Cid())
if err != nil || blockInStore {
t.Fatal("Unwanted block added to block store")
}
}

func TestLargeSwarm(t *testing.T) {
if testing.Short() {
t.SkipNow()
Expand Down

0 comments on commit c5b071d

Please sign in to comment.