bitswap.go 5.51 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 10

	humanize "gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize"
Steven Allen's avatar
Steven Allen committed
11 12 13
	bitswap "gx/ipfs/QmUYXFM46WgGs5AScfL4FSZXa9p5nAhddueyM5auAVZGCQ/go-bitswap"
	decision "gx/ipfs/QmUYXFM46WgGs5AScfL4FSZXa9p5nAhddueyM5auAVZGCQ/go-bitswap/decision"
	peer "gx/ipfs/QmY5Grm8pJdiSSVsYxx4uNRgweY72EmYwuSDbRnbFok3iY/go-libp2p-peer"
Steven Allen's avatar
Steven Allen committed
14
	cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
Overbool's avatar
Overbool committed
15
	cidutil "gx/ipfs/QmbfKu17LbMWyGUxHEUns9Wf5Dkm8PT6be4uPhTkk4YvaV/go-cidutil"
16
	cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
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 {
Overbool's avatar
Overbool committed
77 78
			// sort the keys first
			cidutil.Sort(out.Keys)
Overbool's avatar
Overbool committed
79
			for _, key := range out.Keys {
Overbool's avatar
Overbool committed
80
				fmt.Fprintln(w, key)
Overbool's avatar
Overbool committed
81 82 83 84
			}

			return nil
		}),
85 86 87 88
	},
}

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

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

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

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

keks's avatar
keks committed
114
		return cmds.EmitOnce(res, st)
115
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
116
	Encoders: cmds.EncoderMap{
Overbool's avatar
Overbool committed
117
		cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, s *bitswap.Stat) error {
Jan Winkelmann's avatar
Jan Winkelmann committed
118
			fmt.Fprintln(w, "bitswap status")
Overbool's avatar
Overbool committed
119 120 121 122 123 124 125 126 127
			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 {
Jan Winkelmann's avatar
Jan Winkelmann committed
128
				fmt.Fprintf(w, "\t\t%s\n", k.String())
Jeromy's avatar
Jeromy committed
129
			}
Overbool's avatar
Overbool committed
130 131
			fmt.Fprintf(w, "\tpartners [%d]\n", len(s.Peers))
			for _, p := range s.Peers {
Jan Winkelmann's avatar
Jan Winkelmann committed
132
				fmt.Fprintf(w, "\t\t%s\n", p)
133
			}
Jan Winkelmann's avatar
Jan Winkelmann committed
134 135 136

			return nil
		}),
137 138
	},
}
139

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

		if !nd.OnlineMode() {
Overbool's avatar
Overbool committed
160
			return ErrNotOnline
161 162 163 164
		}

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

Overbool's avatar
Overbool committed
168
		partner, err := peer.IDB58Decode(req.Arguments[0])
169
		if err != nil {
Overbool's avatar
Overbool committed
170
			return err
171
		}
Overbool's avatar
Overbool committed
172

173
		return cmds.EmitOnce(res, bs.LedgerForPeer(partner))
174
	},
Overbool's avatar
Overbool committed
175 176 177
	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"+
178 179 180 181 182 183
				"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
184 185
			return nil
		}),
186 187
	},
}
188

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

		if !nd.OnlineMode() {
Overbool's avatar
Overbool committed
203
			return ErrNotOnline
204 205
		}

Overbool's avatar
Overbool committed
206
		err = nd.Reprovider.Trigger(req.Context)
207
		if err != nil {
Overbool's avatar
Overbool committed
208
			return err
209
		}
Jan Winkelmann's avatar
Jan Winkelmann committed
210

Overbool's avatar
Overbool committed
211
		return nil
212 213
	},
}