bitswap.go 6.18 KB
Newer Older
1 2 3 4
package commands

import (
	"bytes"
Jeromy's avatar
Jeromy committed
5
	"fmt"
6 7
	"io"

8 9
	cmds "github.com/ipfs/go-ipfs/commands"
	bitswap "github.com/ipfs/go-ipfs/exchange/bitswap"
10 11 12
	decision "github.com/ipfs/go-ipfs/exchange/bitswap/decision"

	"gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize"
13
	u "gx/ipfs/QmWbjfz3u6HkAdPh34dgPchGbQjob6LXLhAeCGii2TX69n/go-ipfs-util"
14
	cid "gx/ipfs/Qma4RJSuh7mMeJQYCqMbKzekn6EwBo7HEs5AQYjVRMQATB/go-cid"
15
	peer "gx/ipfs/QmdS9KpbDyPrieswibZhkod1oXqRwZJrUPzxCofAMWpFGq/go-libp2p-peer"
16 17 18 19
)

var BitswapCmd = &cmds.Command{
	Helptext: cmds.HelpText{
20
		Tagline:          "Interact with the bitswap agent.",
21 22 23 24 25
		ShortDescription: ``,
	},
	Subcommands: map[string]*cmds.Command{
		"wantlist": showWantlistCmd,
		"stat":     bitswapStatCmd,
26
		"unwant":   unwantCmd,
27
		"ledger":   ledgerCmd,
28 29 30 31 32
	},
}

var unwantCmd = &cmds.Command{
	Helptext: cmds.HelpText{
Richard Littauer's avatar
Richard Littauer committed
33
		Tagline: "Remove a given block from your wantlist.",
34 35
	},
	Arguments: []cmds.Argument{
36
		cmds.StringArg("key", true, true, "Key(s) to remove from your wantlist.").EnableStdin(),
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
	},
	Run: func(req cmds.Request, res cmds.Response) {
		nd, err := req.InvocContext().GetNode()
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		if !nd.OnlineMode() {
			res.SetError(errNotOnline, cmds.ErrClient)
			return
		}

		bs, ok := nd.Exchange.(*bitswap.Bitswap)
		if !ok {
			res.SetError(u.ErrCast(), cmds.ErrNormal)
			return
		}

56
		var ks []*cid.Cid
57
		for _, arg := range req.Arguments() {
Jeromy's avatar
Jeromy committed
58 59 60
			c, err := cid.Decode(arg)
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
61 62 63
				return
			}

64
			ks = append(ks, c)
65 66
		}

Jeromy's avatar
Jeromy committed
67 68 69 70 71
		// TODO: This should maybe find *all* sessions for this request and cancel them?
		// (why): in reality, i think this command should be removed. Its
		// messing with the internal state of bitswap. You should cancel wants
		// by killing the command that caused the want.
		bs.CancelWants(ks, 0)
72 73 74 75 76
	},
}

var showWantlistCmd = &cmds.Command{
	Helptext: cmds.HelpText{
Richard Littauer's avatar
Richard Littauer committed
77
		Tagline: "Show blocks currently on the wantlist.",
78
		ShortDescription: `
Richard Littauer's avatar
Richard Littauer committed
79
Print out all blocks currently on the bitswap wantlist for the local peer.`,
80
	},
81
	Options: []cmds.Option{
Richard Littauer's avatar
Richard Littauer committed
82
		cmds.StringOption("peer", "p", "Specify which peer to show wantlist for. Default: self."),
83
	},
84 85
	Type: KeyList{},
	Run: func(req cmds.Request, res cmds.Response) {
Jeromy's avatar
Jeromy committed
86
		nd, err := req.InvocContext().GetNode()
87 88 89 90
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}
91 92 93 94 95 96

		if !nd.OnlineMode() {
			res.SetError(errNotOnline, cmds.ErrClient)
			return
		}

97 98 99 100 101
		bs, ok := nd.Exchange.(*bitswap.Bitswap)
		if !ok {
			res.SetError(u.ErrCast(), cmds.ErrNormal)
			return
		}
102

103 104 105 106 107 108 109 110 111 112 113
		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
			}
Jeromy's avatar
Jeromy committed
114 115 116 117 118
			if pid == nd.Identity {
				res.SetOutput(&KeyList{bs.GetWantlist()})
				return
			}

119 120 121 122
			res.SetOutput(&KeyList{bs.WantlistForPeer(pid)})
		} else {
			res.SetOutput(&KeyList{bs.GetWantlist()})
		}
123 124 125 126 127 128 129 130
	},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: KeyListTextMarshaler,
	},
}

var bitswapStatCmd = &cmds.Command{
	Helptext: cmds.HelpText{
Richard Littauer's avatar
Richard Littauer committed
131
		Tagline:          "Show some diagnostic information on the bitswap agent.",
132 133 134 135
		ShortDescription: ``,
	},
	Type: bitswap.Stat{},
	Run: func(req cmds.Request, res cmds.Response) {
Jeromy's avatar
Jeromy committed
136
		nd, err := req.InvocContext().GetNode()
137 138 139 140 141
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

142 143 144 145 146
		if !nd.OnlineMode() {
			res.SetError(errNotOnline, cmds.ErrClient)
			return
		}

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
		bs, ok := nd.Exchange.(*bitswap.Bitswap)
		if !ok {
			res.SetError(u.ErrCast(), cmds.ErrNormal)
			return
		}

		st, err := bs.Stat()
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		res.SetOutput(st)
	},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
			out, ok := res.Output().(*bitswap.Stat)
			if !ok {
				return nil, u.ErrCast()
			}
			buf := new(bytes.Buffer)
Jeromy's avatar
Jeromy committed
168 169
			fmt.Fprintln(buf, "bitswap status")
			fmt.Fprintf(buf, "\tprovides buffer: %d / %d\n", out.ProvideBufLen, bitswap.HasBlockBufferSize)
170
			fmt.Fprintf(buf, "\tblocks received: %d\n", out.BlocksReceived)
Jeromy's avatar
Jeromy committed
171 172 173
			fmt.Fprintf(buf, "\tblocks sent: %d\n", out.BlocksSent)
			fmt.Fprintf(buf, "\tdata received: %d\n", out.DataReceived)
			fmt.Fprintf(buf, "\tdata sent: %d\n", out.DataSent)
174
			fmt.Fprintf(buf, "\tdup blocks received: %d\n", out.DupBlksReceived)
175
			fmt.Fprintf(buf, "\tdup data received: %s\n", humanize.Bytes(out.DupDataReceived))
Jeromy's avatar
Jeromy committed
176 177
			fmt.Fprintf(buf, "\twantlist [%d keys]\n", len(out.Wantlist))
			for _, k := range out.Wantlist {
178
				fmt.Fprintf(buf, "\t\t%s\n", k.String())
Jeromy's avatar
Jeromy committed
179 180 181 182
			}
			fmt.Fprintf(buf, "\tpartners [%d]\n", len(out.Peers))
			for _, p := range out.Peers {
				fmt.Fprintf(buf, "\t\t%s\n", p)
183 184 185 186 187
			}
			return buf, nil
		},
	},
}
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

var ledgerCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline: "Show the current ledger for a peer.",
		ShortDescription: `
The Bitswap decision engine tracks the number of bytes exchanged between IPFS
nodes, and stores this information as a collection of ledgers. This command
prints the ledger associated with a given peer.
`,
	},
	Arguments: []cmds.Argument{
		cmds.StringArg("peer", true, false, "The PeerID (B58) of the ledger to inspect."),
	},
	Type: decision.Receipt{},
	Run: func(req cmds.Request, res cmds.Response) {
		nd, err := req.InvocContext().GetNode()
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		if !nd.OnlineMode() {
			res.SetError(errNotOnline, cmds.ErrClient)
			return
		}

		bs, ok := nd.Exchange.(*bitswap.Bitswap)
		if !ok {
			res.SetError(u.ErrCast(), cmds.ErrNormal)
			return
		}

		partner, err := peer.IDB58Decode(req.Arguments()[0])
		if err != nil {
			res.SetError(err, cmds.ErrClient)
			return
		}
		res.SetOutput(bs.LedgerForPeer(partner))
	},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
			out, ok := res.Output().(*decision.Receipt)
			if !ok {
				return nil, u.ErrCast()
			}
			buf := new(bytes.Buffer)
			fmt.Fprintf(buf, "Ledger for %s\n"+
				"Debt ratio:\t%f\n"+
				"Exchanges:\t%d\n"+
				"Bytes sent:\t%d\n"+
				"Bytes received:\t%d\n\n",
				out.Peer, out.Value, out.Exchanged,
				out.Sent, out.Recv)
			return buf, nil
		},
	},
}