bitswap.go 5.7 KB
Newer Older
1 2 3
package commands

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

7
	cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
Jan Winkelmann's avatar
Jan Winkelmann committed
8
	e "github.com/ipfs/go-ipfs/core/commands/e"
Overbool's avatar
Overbool committed
9

Steven Allen's avatar
Steven Allen committed
10 11
	humanize "gx/ipfs/QmQMxG9D52TirZd9eLA37nxiNspnMRkKbyPWrVAa1gvtSy/go-humanize"
	cmds "gx/ipfs/QmQtQrtNioesAWtrx8csBvfY37gTe94d6wQ3VikZUjxD39/go-ipfs-cmds"
Jakub Sztandera's avatar
Jakub Sztandera committed
12
	peer "gx/ipfs/QmTu65MVbemtUxJEWgsTtzv9Zv9P8rvmqNA4eG9TrTRGYc/go-libp2p-peer"
Steven Allen's avatar
Steven Allen committed
13 14
	bitswap "gx/ipfs/QmXckKH4fiPT966YPPxoqXJFof7ovxwGxBdhM3CRpGzgDa/go-bitswap"
	decision "gx/ipfs/QmXckKH4fiPT966YPPxoqXJFof7ovxwGxBdhM3CRpGzgDa/go-bitswap/decision"
15
	cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
Steven Allen's avatar
Steven Allen committed
16
	cidutil "gx/ipfs/Qmf3gRH2L1QZy92gJHJEwKmBJKJGVf8RpN2kPPD2NQWg8G/go-cidutil"
17 18 19
)

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

25
	Subcommands: map[string]*cmds.Command{
26
		"stat":      bitswapStatCmd,
Overbool's avatar
Overbool committed
27 28 29
		"wantlist":  showWantlistCmd,
		"ledger":    ledgerCmd,
		"reprovide": reprovideCmd,
30 31 32
	},
}

Kejie Zhang's avatar
Kejie Zhang committed
33 34 35 36
const (
	peerOptionName = "peer"
)

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

		if !nd.OnlineMode() {
Overbool's avatar
Overbool committed
54
			return ErrNotOnline
55 56
		}

57 58
		bs, ok := nd.Exchange.(*bitswap.Bitswap)
		if !ok {
Overbool's avatar
Overbool committed
59
			return e.TypeErr(bs, nd.Exchange)
60
		}
61

Overbool's avatar
Overbool committed
62
		pstr, found := req.Options[peerOptionName].(string)
63 64 65
		if found {
			pid, err := peer.IDB58Decode(pstr)
			if err != nil {
Overbool's avatar
Overbool committed
66
				return err
67
			}
Overbool's avatar
Overbool committed
68 69
			if pid != nd.Identity {
				return cmds.EmitOnce(res, &KeyList{bs.WantlistForPeer(pid)})
Jeromy's avatar
Jeromy committed
70
			}
71
		}
Overbool's avatar
Overbool committed
72

73
		return cmds.EmitOnce(res, &KeyList{bs.GetWantlist()})
74
	},
Overbool's avatar
Overbool committed
75 76
	Encoders: cmds.EncoderMap{
		cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *KeyList) error {
77 78 79 80
			enc, err := cmdenv.GetLowLevelCidEncoder(req)
			if err != nil {
				return err
			}
Overbool's avatar
Overbool committed
81 82
			// sort the keys first
			cidutil.Sort(out.Keys)
Overbool's avatar
Overbool committed
83
			for _, key := range out.Keys {
84
				fmt.Fprintln(w, enc.Encode(key))
Overbool's avatar
Overbool committed
85 86 87
			}
			return nil
		}),
88 89 90 91
	},
}

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

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

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

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

keks's avatar
keks committed
117
		return cmds.EmitOnce(res, st)
118
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
119
	Encoders: cmds.EncoderMap{
Overbool's avatar
Overbool committed
120
		cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, s *bitswap.Stat) error {
121 122 123 124
			enc, err := cmdenv.GetLowLevelCidEncoder(req)
			if err != nil {
				return err
			}
Jan Winkelmann's avatar
Jan Winkelmann committed
125
			fmt.Fprintln(w, "bitswap status")
Overbool's avatar
Overbool committed
126 127 128 129 130 131 132 133 134
			fmt.Fprintf(w, "\tprovides buffer: %d / %d\n", s.ProvideBufLen, bitswap.HasBlockBufferSize)
			fmt.Fprintf(w, "\tblocks received: %d\n", s.BlocksReceived)
			fmt.Fprintf(w, "\tblocks sent: %d\n", s.BlocksSent)
			fmt.Fprintf(w, "\tdata received: %d\n", s.DataReceived)
			fmt.Fprintf(w, "\tdata sent: %d\n", s.DataSent)
			fmt.Fprintf(w, "\tdup blocks received: %d\n", s.DupBlksReceived)
			fmt.Fprintf(w, "\tdup data received: %s\n", humanize.Bytes(s.DupDataReceived))
			fmt.Fprintf(w, "\twantlist [%d keys]\n", len(s.Wantlist))
			for _, k := range s.Wantlist {
135
				fmt.Fprintf(w, "\t\t%s\n", enc.Encode(k))
Jeromy's avatar
Jeromy committed
136
			}
Overbool's avatar
Overbool committed
137 138
			fmt.Fprintf(w, "\tpartners [%d]\n", len(s.Peers))
			for _, p := range s.Peers {
Jan Winkelmann's avatar
Jan Winkelmann committed
139
				fmt.Fprintf(w, "\t\t%s\n", p)
140
			}
Jan Winkelmann's avatar
Jan Winkelmann committed
141 142 143

			return nil
		}),
144 145
	},
}
146

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

		if !nd.OnlineMode() {
Overbool's avatar
Overbool committed
167
			return ErrNotOnline
168 169 170 171
		}

		bs, ok := nd.Exchange.(*bitswap.Bitswap)
		if !ok {
Overbool's avatar
Overbool committed
172
			return e.TypeErr(bs, nd.Exchange)
173 174
		}

Overbool's avatar
Overbool committed
175
		partner, err := peer.IDB58Decode(req.Arguments[0])
176
		if err != nil {
Overbool's avatar
Overbool committed
177
			return err
178
		}
Overbool's avatar
Overbool committed
179

180
		return cmds.EmitOnce(res, bs.LedgerForPeer(partner))
181
	},
Overbool's avatar
Overbool committed
182 183 184
	Encoders: cmds.EncoderMap{
		cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *decision.Receipt) error {
			fmt.Fprintf(w, "Ledger for %s\n"+
185 186 187 188 189 190
				"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)
Overbool's avatar
Overbool committed
191 192
			return nil
		}),
193 194
	},
}
195

Overbool's avatar
Overbool committed
196
var reprovideCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
197
	Helptext: cmdkit.HelpText{
198 199 200 201 202
		Tagline: "Trigger reprovider.",
		ShortDescription: `
Trigger reprovider to announce our data to network.
`,
	},
Overbool's avatar
Overbool committed
203 204
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
		nd, err := cmdenv.GetNode(env)
205
		if err != nil {
Overbool's avatar
Overbool committed
206
			return err
207 208 209
		}

		if !nd.OnlineMode() {
Overbool's avatar
Overbool committed
210
			return ErrNotOnline
211 212
		}

Overbool's avatar
Overbool committed
213
		err = nd.Reprovider.Trigger(req.Context)
214
		if err != nil {
Overbool's avatar
Overbool committed
215
			return err
216
		}
Jan Winkelmann's avatar
Jan Winkelmann committed
217

Overbool's avatar
Overbool committed
218
		return nil
219 220
	},
}