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

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

Jan Winkelmann's avatar
Jan Winkelmann committed
8 9
	oldcmds "github.com/ipfs/go-ipfs/commands"
	e "github.com/ipfs/go-ipfs/core/commands/e"
10
	bitswap "github.com/ipfs/go-ipfs/exchange/bitswap"
11 12
	decision "github.com/ipfs/go-ipfs/exchange/bitswap/decision"

13
	cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
14
	"gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize"
Jan Winkelmann's avatar
Jan Winkelmann committed
15 16
	cmds "gx/ipfs/QmQVvuDwXUGbtYmbmTcbLtGRYXnEbymaR2zEj38GVysqWe/go-ipfs-cmds"
	cmdkit "gx/ipfs/QmSNbH2A1evCCbJSDC6u3RV3GGDhgu6pRGbXHvrN89tMKf/go-ipfs-cmdkit"
17
	peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
18 19 20
)

var BitswapCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
21
	Helptext: cmdkit.HelpText{
22
		Tagline:          "Interact with the bitswap agent.",
23 24
		ShortDescription: ``,
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
25

26
	Subcommands: map[string]*cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
27 28 29
		"stat": bitswapStatCmd,
	},
	OldSubcommands: map[string]*oldcmds.Command{
30 31 32 33
		"wantlist":  showWantlistCmd,
		"unwant":    unwantCmd,
		"ledger":    ledgerCmd,
		"reprovide": reprovideCmd,
34 35 36
	},
}

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

		if !nd.OnlineMode() {
Jan Winkelmann's avatar
Jan Winkelmann committed
52
			res.SetError(errNotOnline, cmdkit.ErrClient)
53 54 55 56 57
			return
		}

		bs, ok := nd.Exchange.(*bitswap.Bitswap)
		if !ok {
Jan Winkelmann's avatar
Jan Winkelmann committed
58
			res.SetError(e.TypeErr(bs, nd.Exchange), cmdkit.ErrNormal)
59 60 61
			return
		}

62
		var ks []*cid.Cid
63
		for _, arg := range req.Arguments() {
Jeromy's avatar
Jeromy committed
64 65
			c, err := cid.Decode(arg)
			if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
66
				res.SetError(err, cmdkit.ErrNormal)
67 68 69
				return
			}

70
			ks = append(ks, c)
71 72
		}

Jeromy's avatar
Jeromy committed
73 74 75 76 77
		// 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)
78 79 80
	},
}

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

		if !nd.OnlineMode() {
Jan Winkelmann's avatar
Jan Winkelmann committed
99
			res.SetError(errNotOnline, cmdkit.ErrClient)
100 101 102
			return
		}

103 104
		bs, ok := nd.Exchange.(*bitswap.Bitswap)
		if !ok {
Jan Winkelmann's avatar
Jan Winkelmann committed
105
			res.SetError(e.TypeErr(bs, nd.Exchange), cmdkit.ErrNormal)
106 107
			return
		}
108

109 110
		pstr, found, err := req.Option("peer").String()
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
111
			res.SetError(err, cmdkit.ErrNormal)
112 113 114 115 116
			return
		}
		if found {
			pid, err := peer.IDB58Decode(pstr)
			if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
117
				res.SetError(err, cmdkit.ErrNormal)
118 119
				return
			}
Jeromy's avatar
Jeromy committed
120 121 122 123 124
			if pid == nd.Identity {
				res.SetOutput(&KeyList{bs.GetWantlist()})
				return
			}

125 126 127 128
			res.SetOutput(&KeyList{bs.WantlistForPeer(pid)})
		} else {
			res.SetOutput(&KeyList{bs.GetWantlist()})
		}
129
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
130 131
	Marshalers: oldcmds.MarshalerMap{
		oldcmds.Text: KeyListTextMarshaler,
132 133 134 135
	},
}

var bitswapStatCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
136
	Helptext: cmdkit.HelpText{
Richard Littauer's avatar
Richard Littauer committed
137
		Tagline:          "Show some diagnostic information on the bitswap agent.",
138 139 140
		ShortDescription: ``,
	},
	Type: bitswap.Stat{},
Jan Winkelmann's avatar
Jan Winkelmann committed
141
	Run: func(req cmds.Request, res cmds.ResponseEmitter) {
Jeromy's avatar
Jeromy committed
142
		nd, err := req.InvocContext().GetNode()
143
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
144
			res.SetError(err, cmdkit.ErrNormal)
145 146 147
			return
		}

148
		if !nd.OnlineMode() {
Jan Winkelmann's avatar
Jan Winkelmann committed
149
			res.SetError(errNotOnline, cmdkit.ErrClient)
150 151 152
			return
		}

153 154
		bs, ok := nd.Exchange.(*bitswap.Bitswap)
		if !ok {
Jan Winkelmann's avatar
Jan Winkelmann committed
155
			res.SetError(e.TypeErr(bs, nd.Exchange), cmdkit.ErrNormal)
156 157 158 159 160
			return
		}

		st, err := bs.Stat()
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
161
			res.SetError(err, cmdkit.ErrNormal)
162 163 164
			return
		}

Jan Winkelmann's avatar
Jan Winkelmann committed
165
		res.Emit(st)
166
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
167 168 169
	Encoders: cmds.EncoderMap{
		cmds.Text: cmds.MakeEncoder(func(req cmds.Request, w io.Writer, v interface{}) error {
			out, ok := v.(*bitswap.Stat)
170
			if !ok {
Jan Winkelmann's avatar
Jan Winkelmann committed
171
				return e.TypeErr(out, v)
172
			}
Jan Winkelmann's avatar
Jan Winkelmann committed
173 174 175 176 177 178 179 180 181 182

			fmt.Fprintln(w, "bitswap status")
			fmt.Fprintf(w, "\tprovides buffer: %d / %d\n", out.ProvideBufLen, bitswap.HasBlockBufferSize)
			fmt.Fprintf(w, "\tblocks received: %d\n", out.BlocksReceived)
			fmt.Fprintf(w, "\tblocks sent: %d\n", out.BlocksSent)
			fmt.Fprintf(w, "\tdata received: %d\n", out.DataReceived)
			fmt.Fprintf(w, "\tdata sent: %d\n", out.DataSent)
			fmt.Fprintf(w, "\tdup blocks received: %d\n", out.DupBlksReceived)
			fmt.Fprintf(w, "\tdup data received: %s\n", humanize.Bytes(out.DupDataReceived))
			fmt.Fprintf(w, "\twantlist [%d keys]\n", len(out.Wantlist))
Jeromy's avatar
Jeromy committed
183
			for _, k := range out.Wantlist {
Jan Winkelmann's avatar
Jan Winkelmann committed
184
				fmt.Fprintf(w, "\t\t%s\n", k.String())
Jeromy's avatar
Jeromy committed
185
			}
Jan Winkelmann's avatar
Jan Winkelmann committed
186
			fmt.Fprintf(w, "\tpartners [%d]\n", len(out.Peers))
Jeromy's avatar
Jeromy committed
187
			for _, p := range out.Peers {
Jan Winkelmann's avatar
Jan Winkelmann committed
188
				fmt.Fprintf(w, "\t\t%s\n", p)
189
			}
Jan Winkelmann's avatar
Jan Winkelmann committed
190 191 192

			return nil
		}),
193 194
	},
}
195

Jan Winkelmann's avatar
Jan Winkelmann committed
196 197
var ledgerCmd = &oldcmds.Command{
	Helptext: cmdkit.HelpText{
198 199 200 201 202 203 204
		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.
`,
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
205 206
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("peer", true, false, "The PeerID (B58) of the ledger to inspect."),
207 208
	},
	Type: decision.Receipt{},
Jan Winkelmann's avatar
Jan Winkelmann committed
209
	Run: func(req oldcmds.Request, res oldcmds.Response) {
210 211
		nd, err := req.InvocContext().GetNode()
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
212
			res.SetError(err, cmdkit.ErrNormal)
213 214 215 216
			return
		}

		if !nd.OnlineMode() {
Jan Winkelmann's avatar
Jan Winkelmann committed
217
			res.SetError(errNotOnline, cmdkit.ErrClient)
218 219 220 221 222
			return
		}

		bs, ok := nd.Exchange.(*bitswap.Bitswap)
		if !ok {
Jan Winkelmann's avatar
Jan Winkelmann committed
223
			res.SetError(e.TypeErr(bs, nd.Exchange), cmdkit.ErrNormal)
224 225 226 227 228
			return
		}

		partner, err := peer.IDB58Decode(req.Arguments()[0])
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
229
			res.SetError(err, cmdkit.ErrClient)
230 231 232 233
			return
		}
		res.SetOutput(bs.LedgerForPeer(partner))
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
234 235 236 237 238 239 240 241
	Marshalers: oldcmds.MarshalerMap{
		oldcmds.Text: func(res oldcmds.Response) (io.Reader, error) {
			v, err := unwrapOutput(res.Output())
			if err != nil {
				return nil, err
			}

			out, ok := v.(*decision.Receipt)
242
			if !ok {
Jan Winkelmann's avatar
Jan Winkelmann committed
243
				return nil, e.TypeErr(out, v)
244
			}
Jan Winkelmann's avatar
Jan Winkelmann committed
245

246 247 248 249 250 251 252 253 254 255 256 257
			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
		},
	},
}
258

Jan Winkelmann's avatar
Jan Winkelmann committed
259 260
var reprovideCmd = &oldcmds.Command{
	Helptext: cmdkit.HelpText{
261 262 263 264 265
		Tagline: "Trigger reprovider.",
		ShortDescription: `
Trigger reprovider to announce our data to network.
`,
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
266
	Run: func(req oldcmds.Request, res oldcmds.Response) {
267 268
		nd, err := req.InvocContext().GetNode()
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
269
			res.SetError(err, cmdkit.ErrNormal)
270 271 272 273
			return
		}

		if !nd.OnlineMode() {
Jan Winkelmann's avatar
Jan Winkelmann committed
274
			res.SetError(errNotOnline, cmdkit.ErrClient)
275 276 277 278 279
			return
		}

		err = nd.Reprovider.Trigger(req.Context())
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
280
			res.SetError(err, cmdkit.ErrNormal)
281 282
			return
		}
Jan Winkelmann's avatar
Jan Winkelmann committed
283 284

		res.SetOutput(nil)
285 286
	},
}