diff --git a/core/commands/bitswap.go b/core/commands/bitswap.go index f2386a7a3c1e2d2a42ebb185e1c8f92a586706ee..f89f38d7f3d6cb593c85e8516707836dc96d98e0 100644 --- a/core/commands/bitswap.go +++ b/core/commands/bitswap.go @@ -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, diff --git a/exchange/bitswap/bitswap.go b/exchange/bitswap/bitswap.go index ae0c76daa3ee27f1a2a27a1b18312b4f51567410..37826c4928a8bccaf21d7522bf9bfc553d191e44 100644 --- a/exchange/bitswap/bitswap.go +++ b/exchange/bitswap/bitswap.go @@ -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. diff --git a/exchange/bitswap/decision/engine.go b/exchange/bitswap/decision/engine.go index 119869677056532daf5fcefd82d78e7177984eea..60b95e469b7ca9664d7b26c24975f3c88695c9fb 100644 --- a/exchange/bitswap/decision/engine.go +++ b/exchange/bitswap/decision/engine.go @@ -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 {