@cryptotaxi247 / kubo / commits / c38ca0038

implement unwant command to remove blocks from wantlist

License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>

Jeromy committed Sep 9, 2015 at 10:50 UTC c38ca0038d9b58be27ecb786699f25377aab90a1
2 files changed +47
core/commands/bitswap.go
+42
@@ -5,6 +5,7 @@ import (
5 "fmt"
6 "io"
7
8 + key "github.com/ipfs/go-ipfs/blocks/key"
9 cmds "github.com/ipfs/go-ipfs/commands"
10 bitswap "github.com/ipfs/go-ipfs/exchange/bitswap"
11 peer "github.com/ipfs/go-ipfs/p2p/peer"
@@ -19,6 +20,47 @@ var BitswapCmd = &cmds.Command{
20 Subcommands: map[string]*cmds.Command{
21 "wantlist": showWantlistCmd,
22 "stat": bitswapStatCmd,
23 + "unwant": unwantCmd,
24 + },
25 +}
26 +
27 +var unwantCmd = &cmds.Command{
28 + Helptext: cmds.HelpText{
29 + Tagline: "Remove a given block from your wantlist",
30 + },
31 + Arguments: []cmds.Argument{
32 + cmds.StringArg("key", true, true, "key to remove from your wantlist").EnableStdin(),
33 + },
34 + Run: func(req cmds.Request, res cmds.Response) {
35 + nd, err := req.InvocContext().GetNode()
36 + if err != nil {
37 + res.SetError(err, cmds.ErrNormal)
38 + return
39 + }
40 +
41 + if !nd.OnlineMode() {
42 + res.SetError(errNotOnline, cmds.ErrClient)
43 + return
44 + }
45 +
46 + bs, ok := nd.Exchange.(*bitswap.Bitswap)
47 + if !ok {
48 + res.SetError(u.ErrCast(), cmds.ErrNormal)
49 + return
50 + }
51 +
52 + var ks []key.Key
53 + for _, arg := range req.Arguments() {
54 + dec := key.B58KeyDecode(arg)
55 + if dec == "" {
56 + res.SetError(fmt.Errorf("incorrectly formatted key: %s", arg), cmds.ErrNormal)
57 + return
58 + }
59 +
60 + ks = append(ks, dec)
61 + }
62 +
63 + bs.CancelWants(ks)
64 },
65 }
66
exchange/bitswap/bitswap.go
+5
@@ -221,6 +221,11 @@ func (bs *Bitswap) GetBlocks(ctx context.Context, keys []key.Key) (<-chan *block
221 }
222 }
223
224 +// CancelWant removes a given key from the wantlist
225 +func (bs *Bitswap) CancelWants(ks []key.Key) {
226 + bs.wm.CancelWants(ks)
227 +}
228 +
229 // HasBlock announces the existance of a block to this bitswap service. The
230 // service will potentially notify its peers.
231 func (bs *Bitswap) HasBlock(ctx context.Context, blk *blocks.Block) error {