1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-30 18:13:54 +08:00

Merge pull request #1155 from ipfs/feat/peer-wantlist

let wantlist command show other peers wantlists
This commit is contained in:
Juan Batiz-Benet
2015-04-28 02:18:29 -07:00
3 changed files with 39 additions and 2 deletions

View File

@ -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,

View File

@ -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.

View File

@ -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 {