bitswap.go 6.04 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
	oldcmds "github.com/ipfs/go-ipfs/commands"
9
	lgc "github.com/ipfs/go-ipfs/commands/legacy"
10
	cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
Jan Winkelmann's avatar
Jan Winkelmann committed
11
	e "github.com/ipfs/go-ipfs/core/commands/e"
Łukasz Magiera's avatar
Łukasz Magiera committed
12 13
	bitswap "gx/ipfs/QmenvDer6Lfv9Q8mDWS2hcYscvEv1RJgvXkPjZX3thLr6g/go-bitswap"
	decision "gx/ipfs/QmenvDer6Lfv9Q8mDWS2hcYscvEv1RJgvXkPjZX3thLr6g/go-bitswap/decision"
14 15

	"gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize"
16
	cmds "gx/ipfs/QmPXR4tNdLbp8HsZiPMjpsgqphX9Vhw2J6Jh5MKH2ovW3D/go-ipfs-cmds"
17 18
	peer "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
	cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
19 20 21
)

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

27
	Subcommands: map[string]*cmds.Command{
28 29 30 31
		"stat":      bitswapStatCmd,
		"wantlist":  lgc.NewCommand(showWantlistCmd),
		"ledger":    lgc.NewCommand(ledgerCmd),
		"reprovide": lgc.NewCommand(reprovideCmd),
32 33 34
	},
}

Jan Winkelmann's avatar
Jan Winkelmann committed
35 36
var showWantlistCmd = &oldcmds.Command{
	Helptext: cmdkit.HelpText{
Richard Littauer's avatar
Richard Littauer committed
37
		Tagline: "Show blocks currently on the wantlist.",
38
		ShortDescription: `
Richard Littauer's avatar
Richard Littauer committed
39
Print out all blocks currently on the bitswap wantlist for the local peer.`,
40
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
41 42
	Options: []cmdkit.Option{
		cmdkit.StringOption("peer", "p", "Specify which peer to show wantlist for. Default: self."),
43
	},
44
	Type: KeyList{},
Jan Winkelmann's avatar
Jan Winkelmann committed
45
	Run: func(req oldcmds.Request, res oldcmds.Response) {
Jeromy's avatar
Jeromy committed
46
		nd, err := req.InvocContext().GetNode()
47
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
48
			res.SetError(err, cmdkit.ErrNormal)
49 50
			return
		}
51 52

		if !nd.OnlineMode() {
53
			res.SetError(ErrNotOnline, cmdkit.ErrClient)
54 55 56
			return
		}

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

63 64
		pstr, found, err := req.Option("peer").String()
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
65
			res.SetError(err, cmdkit.ErrNormal)
66 67 68 69 70
			return
		}
		if found {
			pid, err := peer.IDB58Decode(pstr)
			if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
71
				res.SetError(err, cmdkit.ErrNormal)
72 73
				return
			}
Jeromy's avatar
Jeromy committed
74 75 76 77 78
			if pid == nd.Identity {
				res.SetOutput(&KeyList{bs.GetWantlist()})
				return
			}

79 80 81 82
			res.SetOutput(&KeyList{bs.WantlistForPeer(pid)})
		} else {
			res.SetOutput(&KeyList{bs.GetWantlist()})
		}
83
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
84 85
	Marshalers: oldcmds.MarshalerMap{
		oldcmds.Text: KeyListTextMarshaler,
86 87 88 89
	},
}

var bitswapStatCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
90
	Helptext: cmdkit.HelpText{
Richard Littauer's avatar
Richard Littauer committed
91
		Tagline:          "Show some diagnostic information on the bitswap agent.",
92 93 94
		ShortDescription: ``,
	},
	Type: bitswap.Stat{},
keks's avatar
keks committed
95
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
96
		nd, err := cmdenv.GetNode(env)
97
		if err != nil {
keks's avatar
keks committed
98
			return err
99 100
		}

101
		if !nd.OnlineMode() {
keks's avatar
keks committed
102
			return cmdkit.Errorf(cmdkit.ErrClient, ErrNotOnline.Error())
103 104
		}

105 106
		bs, ok := nd.Exchange.(*bitswap.Bitswap)
		if !ok {
keks's avatar
keks committed
107
			return e.TypeErr(bs, nd.Exchange)
108 109 110 111
		}

		st, err := bs.Stat()
		if err != nil {
keks's avatar
keks committed
112
			return err
113 114
		}

keks's avatar
keks committed
115
		return cmds.EmitOnce(res, st)
116
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
117
	Encoders: cmds.EncoderMap{
118
		cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
Jan Winkelmann's avatar
Jan Winkelmann committed
119
			out, ok := v.(*bitswap.Stat)
120
			if !ok {
Jan Winkelmann's avatar
Jan Winkelmann committed
121
				return e.TypeErr(out, v)
122
			}
Jan Winkelmann's avatar
Jan Winkelmann committed
123 124 125 126 127 128 129 130 131 132

			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
133
			for _, k := range out.Wantlist {
Jan Winkelmann's avatar
Jan Winkelmann committed
134
				fmt.Fprintf(w, "\t\t%s\n", k.String())
Jeromy's avatar
Jeromy committed
135
			}
Jan Winkelmann's avatar
Jan Winkelmann committed
136
			fmt.Fprintf(w, "\tpartners [%d]\n", len(out.Peers))
Jeromy's avatar
Jeromy committed
137
			for _, p := range out.Peers {
Jan Winkelmann's avatar
Jan Winkelmann committed
138
				fmt.Fprintf(w, "\t\t%s\n", p)
139
			}
Jan Winkelmann's avatar
Jan Winkelmann committed
140 141 142

			return nil
		}),
143 144
	},
}
145

Jan Winkelmann's avatar
Jan Winkelmann committed
146 147
var ledgerCmd = &oldcmds.Command{
	Helptext: cmdkit.HelpText{
148 149 150 151 152 153 154
		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
155 156
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("peer", true, false, "The PeerID (B58) of the ledger to inspect."),
157 158
	},
	Type: decision.Receipt{},
Jan Winkelmann's avatar
Jan Winkelmann committed
159
	Run: func(req oldcmds.Request, res oldcmds.Response) {
160 161
		nd, err := req.InvocContext().GetNode()
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
162
			res.SetError(err, cmdkit.ErrNormal)
163 164 165 166
			return
		}

		if !nd.OnlineMode() {
167
			res.SetError(ErrNotOnline, cmdkit.ErrClient)
168 169 170 171 172
			return
		}

		bs, ok := nd.Exchange.(*bitswap.Bitswap)
		if !ok {
Jan Winkelmann's avatar
Jan Winkelmann committed
173
			res.SetError(e.TypeErr(bs, nd.Exchange), cmdkit.ErrNormal)
174 175 176 177 178
			return
		}

		partner, err := peer.IDB58Decode(req.Arguments()[0])
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
179
			res.SetError(err, cmdkit.ErrClient)
180 181 182 183
			return
		}
		res.SetOutput(bs.LedgerForPeer(partner))
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
184 185 186 187 188 189 190 191
	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)
192
			if !ok {
Jan Winkelmann's avatar
Jan Winkelmann committed
193
				return nil, e.TypeErr(out, v)
194
			}
Jan Winkelmann's avatar
Jan Winkelmann committed
195

196 197 198 199 200 201 202 203 204 205 206 207
			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
		},
	},
}
208

Jan Winkelmann's avatar
Jan Winkelmann committed
209 210
var reprovideCmd = &oldcmds.Command{
	Helptext: cmdkit.HelpText{
211 212 213 214 215
		Tagline: "Trigger reprovider.",
		ShortDescription: `
Trigger reprovider to announce our data to network.
`,
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
216
	Run: func(req oldcmds.Request, res oldcmds.Response) {
217 218
		nd, err := req.InvocContext().GetNode()
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
219
			res.SetError(err, cmdkit.ErrNormal)
220 221 222 223
			return
		}

		if !nd.OnlineMode() {
224
			res.SetError(ErrNotOnline, cmdkit.ErrClient)
225 226 227 228 229
			return
		}

		err = nd.Reprovider.Trigger(req.Context())
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
230
			res.SetError(err, cmdkit.ErrNormal)
231 232
			return
		}
Jan Winkelmann's avatar
Jan Winkelmann committed
233 234

		res.SetOutput(nil)
235 236
	},
}