Unverified Commit c5b071da authored by Hannah Howard's avatar Hannah Howard Committed by GitHub

Merge pull request #24 from ipfs/bugs/dont-receive-unwanted-blocks-21

fix(Receiver): Ignore unwanted blocks
parents edf24960 779c923a
......@@ -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()
......@@ -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)
}
......
......@@ -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"
......@@ -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()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment