Commit ea2375e8 authored by Jeromy's avatar Jeromy

let wantlist command show other peers wantlists

parent ff1e58a4
......@@ -3,10 +3,12 @@ package commands
import (
"bytes"
"fmt"
"io"
cmds "github.com/ipfs/go-ipfs/commands"
bitswap "github.com/ipfs/go-ipfs/exchange/bitswap"
peer "github.com/ipfs/go-ipfs/p2p/peer"
u "github.com/ipfs/go-ipfs/util"
"io"
)
var BitswapCmd = &cmds.Command{
......@@ -26,6 +28,9 @@ var showWantlistCmd = &cmds.Command{
ShortDescription: `
Print out all blocks currently on the bitswap wantlist for the local peer`,
},
Options: []cmds.Option{
cmds.StringOption("peer", "p", "specify which peer to show wantlist for (default self)"),
},
Type: KeyList{},
Run: func(req cmds.Request, res cmds.Response) {
nd, err := req.Context().GetNode()
......@@ -39,7 +44,21 @@ Print out all blocks currently on the bitswap wantlist for the local peer`,
return
}
res.SetOutput(&KeyList{bs.GetWantlist()})
pstr, found, err := req.Option("peer").String()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
if found {
pid, err := peer.IDB58Decode(pstr)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
res.SetOutput(&KeyList{bs.WantlistForPeer(pid)})
} else {
res.SetOutput(&KeyList{bs.GetWantlist()})
}
},
Marshalers: cmds.MarshalerMap{
cmds.Text: KeyListTextMarshaler,
......
......@@ -175,6 +175,14 @@ func (bs *Bitswap) GetBlock(parent context.Context, k u.Key) (*blocks.Block, err
}
}
func (bs *Bitswap) WantlistForPeer(p peer.ID) []u.Key {
var out []u.Key
for _, e := range bs.engine.WantlistForPeer(p) {
out = append(out, e.Key)
}
return out
}
// GetBlocks returns a channel where the caller may receive blocks that
// correspond to the provided |keys|. Returns an error if BitSwap is unable to
// begin this request within the deadline enforced by the context.
......
......@@ -96,6 +96,16 @@ func NewEngine(ctx context.Context, bs bstore.Blockstore) *Engine {
return e
}
func (e *Engine) WantlistForPeer(p peer.ID) (out []wl.Entry) {
e.lock.Lock()
partner, ok := e.ledgerMap[p]
if ok {
out = partner.wantList.SortedEntries()
}
e.lock.Unlock()
return out
}
func (e *Engine) taskWorker(ctx context.Context) {
defer close(e.outbox) // because taskWorker uses the channel exclusively
for {
......
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