let wantlist command show other peers wantlists
Jeromy committed
Apr 28, 2015 at 01:51 UTC
ea2375e8b40c01019b0f8eb5dff6fd77a046c90f
3 files changed
+39
-2
core/commands/bitswap.go
+21
-2
@@ -3,10 +3,12 @@ package commands
3
import (
4
"bytes"
5
"fmt"
6
+ "io"
7
+
8
cmds "github.com/ipfs/go-ipfs/commands"
9
bitswap "github.com/ipfs/go-ipfs/exchange/bitswap"
10
+ peer "github.com/ipfs/go-ipfs/p2p/peer"
11
u "github.com/ipfs/go-ipfs/util"
9
- "io"
12
)
13
14
var BitswapCmd = &cmds.Command{
@@ -26,6 +28,9 @@ var showWantlistCmd = &cmds.Command{
28
ShortDescription: `
29
Print out all blocks currently on the bitswap wantlist for the local peer`,
30
},
31
+ Options: []cmds.Option{
32
+ cmds.StringOption("peer", "p", "specify which peer to show wantlist for (default self)"),
33
+ },
34
Type: KeyList{},
35
Run: func(req cmds.Request, res cmds.Response) {
36
nd, err := req.Context().GetNode()
@@ -39,7 +44,21 @@ Print out all blocks currently on the bitswap wantlist for the local peer`,
44
return
45
}
46
42
- res.SetOutput(&KeyList{bs.GetWantlist()})
47
+ pstr, found, err := req.Option("peer").String()
48
+ if err != nil {
49
+ res.SetError(err, cmds.ErrNormal)
50
+ return
51
+ }
52
+ if found {
53
+ pid, err := peer.IDB58Decode(pstr)
54
+ if err != nil {
55
+ res.SetError(err, cmds.ErrNormal)
56
+ return
57
+ }
58
+ res.SetOutput(&KeyList{bs.WantlistForPeer(pid)})
59
+ } else {
60
+ res.SetOutput(&KeyList{bs.GetWantlist()})
61
+ }
62
},
63
Marshalers: cmds.MarshalerMap{
64
cmds.Text: KeyListTextMarshaler,
exchange/bitswap/bitswap.go
+8
@@ -175,6 +175,14 @@ func (bs *Bitswap) GetBlock(parent context.Context, k u.Key) (*blocks.Block, err
175
}
176
}
177
178
+func (bs *Bitswap) WantlistForPeer(p peer.ID) []u.Key {
179
+ var out []u.Key
180
+ for _, e := range bs.engine.WantlistForPeer(p) {
181
+ out = append(out, e.Key)
182
+ }
183
+ return out
184
+}
185
+
186
// GetBlocks returns a channel where the caller may receive blocks that
187
// correspond to the provided |keys|. Returns an error if BitSwap is unable to
188
// begin this request within the deadline enforced by the context.
exchange/bitswap/decision/engine.go
+10
@@ -96,6 +96,16 @@ func NewEngine(ctx context.Context, bs bstore.Blockstore) *Engine {
96
return e
97
}
98
99
+func (e *Engine) WantlistForPeer(p peer.ID) (out []wl.Entry) {
100
+ e.lock.Lock()
101
+ partner, ok := e.ledgerMap[p]
102
+ if ok {
103
+ out = partner.wantList.SortedEntries()
104
+ }
105
+ e.lock.Unlock()
106
+ return out
107
+}
108
+
109
func (e *Engine) taskWorker(ctx context.Context) {
110
defer close(e.outbox) // because taskWorker uses the channel exclusively
111
for {