diff --git a/core/commands/bitswap.go b/core/commands/bitswap.go index 3d2288af6..75bfc7877 100644 --- a/core/commands/bitswap.go +++ b/core/commands/bitswap.go @@ -5,6 +5,7 @@ import ( "fmt" "io" + key "github.com/ipfs/go-ipfs/blocks/key" cmds "github.com/ipfs/go-ipfs/commands" bitswap "github.com/ipfs/go-ipfs/exchange/bitswap" peer "github.com/ipfs/go-ipfs/p2p/peer" @@ -19,6 +20,47 @@ var BitswapCmd = &cmds.Command{ Subcommands: map[string]*cmds.Command{ "wantlist": showWantlistCmd, "stat": bitswapStatCmd, + "unwant": unwantCmd, + }, +} + +var unwantCmd = &cmds.Command{ + Helptext: cmds.HelpText{ + Tagline: "Remove a given block from your wantlist", + }, + Arguments: []cmds.Argument{ + cmds.StringArg("key", true, true, "key to remove from your wantlist").EnableStdin(), + }, + Run: func(req cmds.Request, res cmds.Response) { + nd, err := req.InvocContext().GetNode() + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + + if !nd.OnlineMode() { + res.SetError(errNotOnline, cmds.ErrClient) + return + } + + bs, ok := nd.Exchange.(*bitswap.Bitswap) + if !ok { + res.SetError(u.ErrCast(), cmds.ErrNormal) + return + } + + var ks []key.Key + for _, arg := range req.Arguments() { + dec := key.B58KeyDecode(arg) + if dec == "" { + res.SetError(fmt.Errorf("incorrectly formatted key: %s", arg), cmds.ErrNormal) + return + } + + ks = append(ks, dec) + } + + bs.CancelWants(ks) }, } diff --git a/exchange/bitswap/bitswap.go b/exchange/bitswap/bitswap.go index 8bc88481b..28582fe82 100644 --- a/exchange/bitswap/bitswap.go +++ b/exchange/bitswap/bitswap.go @@ -221,6 +221,11 @@ func (bs *Bitswap) GetBlocks(ctx context.Context, keys []key.Key) (<-chan *block } } +// CancelWant removes a given key from the wantlist +func (bs *Bitswap) CancelWants(ks []key.Key) { + bs.wm.CancelWants(ks) +} + // HasBlock announces the existance of a block to this bitswap service. The // service will potentially notify its peers. func (bs *Bitswap) HasBlock(ctx context.Context, blk *blocks.Block) error { diff --git a/test/sharness/t0220-bitswap.sh b/test/sharness/t0220-bitswap.sh new file mode 100755 index 000000000..3632705b7 --- /dev/null +++ b/test/sharness/t0220-bitswap.sh @@ -0,0 +1,32 @@ +#!/bin/sh +# +# Copyright (c) 2015 Jeromy Johnson +# MIT Licensed; see the LICENSE file in this repository. +# + +test_description="test bitswap commands" + +. lib/test-lib.sh + +test_init_ipfs +test_launch_ipfs_daemon + +test_expect_success "'ipfs block get' adds hash to wantlist" ' + export NONEXIST=QmeXxaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && + test_expect_code 1 ipfs block get $NONEXIST --timeout=10ms && + ipfs bitswap wantlist | grep $NONEXIST +' + +test_expect_success "'ipfs bitswap unwant' succeeds" ' + ipfs bitswap unwant $NONEXIST +' + +test_expect_success "hash was removed from wantlist" ' + ipfs bitswap wantlist > wantlist_out && + printf "" > wantlist_exp && + test_cmp wantlist_out wantlist_exp +' + +test_kill_ipfs_daemon + +test_done