Skip to content

Commit 6fa2ab0

Browse files
committed
coreapi swarm: rewire address listing cmds
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
1 parent df9f101 commit 6fa2ab0

File tree

6 files changed

+102
-46
lines changed

6 files changed

+102
-46
lines changed

core/commands/swarm.go

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"gx/ipfs/QmVBUpxsHh53rNcufqxMpLAmz37eGyLJUaexDy1W9YkiNk/go-ipfs-config"
2222
"gx/ipfs/QmXTmUCBtDUrzDYVzASogLiNph7EBuYqEgPL7QoHNMzUnz/go-ipfs-cmds"
2323
ma "gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr"
24-
"gx/ipfs/QmbNepETomvmXfz1X5pHNFD2QuPqnqi47dTd94QJWSorQ3/go-libp2p-peer"
24+
peer "gx/ipfs/QmbNepETomvmXfz1X5pHNFD2QuPqnqi47dTd94QJWSorQ3/go-libp2p-peer"
2525
pstore "gx/ipfs/QmfAQMFpgDU2U4BXG64qVr8HSiictfWvkSBz7Y2oDj65st/go-libp2p-peerstore"
2626
inet "gx/ipfs/QmfDPh144WGBqRxZb1TGDHerbMnZATrHZggAPw7putNnBq/go-libp2p-net"
2727
)
@@ -66,56 +66,53 @@ var swarmPeersCmd = &cmds.Command{
6666
cmdkit.BoolOption("direction", "Also list information about the direction of connection"),
6767
},
6868
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
69-
n, err := cmdenv.GetNode(env)
69+
api, err := cmdenv.GetApi(env)
7070
if err != nil {
7171
return err
7272
}
7373

74-
if n.PeerHost == nil {
75-
return err
76-
}
77-
7874
verbose, _ := req.Options["verbose"].(bool)
7975
latency, _ := req.Options["latency"].(bool)
8076
streams, _ := req.Options["streams"].(bool)
8177
direction, _ := req.Options["direction"].(bool)
8278

83-
conns := n.PeerHost.Network().Conns()
79+
conns, err := api.Swarm().Peers(req.Context)
80+
if err != nil {
81+
return err
82+
}
83+
8484
var out connInfos
8585
for _, c := range conns {
86-
pid := c.RemotePeer()
87-
addr := c.RemoteMultiaddr()
8886
ci := connInfo{
89-
Addr: addr.String(),
90-
Peer: pid.Pretty(),
87+
Addr: c.Address().String(),
88+
Peer: c.ID().Pretty(),
9189
}
9290

93-
/*
94-
// FIXME(steb):
95-
swcon, ok := c.(*swarm.Conn)
96-
if ok {
97-
ci.Muxer = fmt.Sprintf("%T", swcon.StreamConn().Conn())
98-
}
99-
*/
100-
10191
if verbose || direction {
10292
// set direction
103-
ci.Direction = c.Stat().Direction
93+
ci.Direction = c.Direction()
10494
}
10595

10696
if verbose || latency {
107-
lat := n.Peerstore.LatencyEWMA(pid)
97+
lat, err := c.Latency(req.Context)
98+
if err != nil {
99+
return err
100+
}
101+
108102
if lat == 0 {
109103
ci.Latency = "n/a"
110104
} else {
111105
ci.Latency = lat.String()
112106
}
113107
}
114108
if verbose || streams {
115-
strs := c.GetStreams()
109+
strs, err := c.Streams(req.Context)
110+
if err != nil {
111+
return err
112+
}
116113

117114
for _, s := range strs {
118-
ci.Streams = append(ci.Streams, streamInfo{Protocol: string(s.Protocol())})
115+
ci.Streams = append(ci.Streams, streamInfo{Protocol: string(s)})
119116
}
120117
}
121118
sort.Sort(&ci)
@@ -229,26 +226,25 @@ var swarmAddrsCmd = &cmds.Command{
229226
"listen": swarmAddrsListenCmd,
230227
},
231228
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
232-
n, err := cmdenv.GetNode(env)
229+
api, err := cmdenv.GetApi(env)
233230
if err != nil {
234231
return err
235232
}
236233

237-
if n.PeerHost == nil {
234+
addrs, err := api.Swarm().KnownAddrs(req.Context)
235+
if err != nil {
238236
return err
239237
}
240238

241-
addrs := make(map[string][]string)
242-
ps := n.PeerHost.Network().Peerstore()
243-
for _, p := range ps.Peers() {
239+
out := make(map[string][]string)
240+
for p, paddrs := range addrs {
244241
s := p.Pretty()
245-
for _, a := range ps.Addrs(p) {
246-
addrs[s] = append(addrs[s], a.String())
242+
for _, a := range paddrs {
243+
out[s] = append(out[s], a.String())
247244
}
248-
sort.Sort(sort.StringSlice(addrs[s]))
249245
}
250246

251-
return cmds.EmitOnce(res, &addrMap{Addrs: addrs})
247+
return cmds.EmitOnce(res, &addrMap{Addrs: out})
252248
},
253249
Encoders: cmds.EncoderMap{
254250
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
@@ -288,23 +284,27 @@ var swarmAddrsLocalCmd = &cmds.Command{
288284
cmdkit.BoolOption("id", "Show peer ID in addresses."),
289285
},
290286
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
291-
n, err := cmdenv.GetNode(env)
287+
api, err := cmdenv.GetApi(env)
292288
if err != nil {
293289
return err
294290
}
295291

296-
if n.PeerHost == nil {
292+
showid, _ := req.Options["id"].(bool)
293+
self, err := api.Key().Self(req.Context)
294+
if err != nil {
297295
return err
298296
}
299297

300-
showid, _ := req.Options["id"].(bool)
301-
id := n.Identity.Pretty()
298+
maddrs, err := api.Swarm().LocalAddrs(req.Context)
299+
if err != nil {
300+
return err
301+
}
302302

303303
var addrs []string
304-
for _, addr := range n.PeerHost.Addrs() {
304+
for _, addr := range maddrs {
305305
saddr := addr.String()
306306
if showid {
307-
saddr = path.Join(saddr, "ipfs", id)
307+
saddr = path.Join(saddr, "ipfs", self.ID().Pretty())
308308
}
309309
addrs = append(addrs, saddr)
310310
}
@@ -325,17 +325,13 @@ var swarmAddrsListenCmd = &cmds.Command{
325325
`,
326326
},
327327
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
328-
n, err := cmdenv.GetNode(env)
328+
api, err := cmdenv.GetApi(env)
329329
if err != nil {
330330
return err
331331
}
332332

333-
if n.PeerHost == nil {
334-
return err
335-
}
336-
337333
var addrs []string
338-
maddrs, err := n.PeerHost.Network().InterfaceListenAddresses()
334+
maddrs, err := api.Swarm().ListenAddrs(req.Context)
339335
if err != nil {
340336
return err
341337
}

core/coreapi/interface/key.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ type KeyAPI interface {
3333
// List lists keys stored in keystore
3434
List(ctx context.Context) ([]Key, error)
3535

36+
// Self returns the 'main' node key
37+
Self(ctx context.Context) (Key, error)
38+
3639
// Remove removes keys from keystore. Returns ipns path of the removed key
3740
Remove(ctx context.Context, name string) (Key, error)
3841
}

core/coreapi/interface/swarm.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import (
99
"gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
1010
"gx/ipfs/QmbNepETomvmXfz1X5pHNFD2QuPqnqi47dTd94QJWSorQ3/go-libp2p-peer"
1111
pstore "gx/ipfs/QmfAQMFpgDU2U4BXG64qVr8HSiictfWvkSBz7Y2oDj65st/go-libp2p-peerstore"
12+
net "gx/ipfs/QmfDPh144WGBqRxZb1TGDHerbMnZATrHZggAPw7putNnBq/go-libp2p-net"
1213
)
1314

1415
var (
1516
ErrNotConnected = errors.New("not connected")
1617
ErrConnNotFound = errors.New("conn not found")
17-
)
18+
)
1819

1920
// ConnectionInfo contains information about a peer
2021
type ConnectionInfo interface {
@@ -24,6 +25,9 @@ type ConnectionInfo interface {
2425
// Address returns the multiaddress via which we are connected with the peer
2526
Address() ma.Multiaddr
2627

28+
// Direction returns which way the connection was established
29+
Direction() net.Direction
30+
2731
// Latency returns last known round trip time to the peer
2832
Latency(context.Context) (time.Duration, error)
2933

@@ -41,4 +45,8 @@ type SwarmAPI interface {
4145

4246
// Peers returns the list of peers we are connected to
4347
Peers(context.Context) ([]ConnectionInfo, error)
48+
49+
KnownAddrs(context.Context) (map[peer.ID][]ma.Multiaddr, error)
50+
LocalAddrs(context.Context) ([]ma.Multiaddr, error)
51+
ListenAddrs(context.Context) ([]ma.Multiaddr, error)
4452
}

core/coreapi/key.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,7 @@ func (api *KeyAPI) Remove(ctx context.Context, name string) (coreiface.Key, erro
216216

217217
return &key{"", pid}, nil
218218
}
219+
220+
func (api *KeyAPI) Self(ctx context.Context) (coreiface.Key, error) {
221+
return &key{"self", api.node.Identity}, nil
222+
}

core/coreapi/swarm.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package coreapi
33
import (
44
"context"
55
"fmt"
6+
"sort"
67
"time"
78

89
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
@@ -24,6 +25,7 @@ type SwarmAPI struct {
2425
type connInfo struct {
2526
api *CoreAPI
2627
conn net.Conn
28+
dir net.Direction
2729

2830
addr ma.Multiaddr
2931
peer peer.ID
@@ -80,6 +82,41 @@ func (api *SwarmAPI) Disconnect(ctx context.Context, addr ma.Multiaddr) error {
8082
return nil
8183
}
8284

85+
func (api *SwarmAPI) KnownAddrs(context.Context) (map[peer.ID][]ma.Multiaddr, error) {
86+
if api.node.PeerHost == nil {
87+
return nil, coreiface.ErrOffline
88+
}
89+
90+
addrs := make(map[peer.ID][]ma.Multiaddr)
91+
ps := api.node.PeerHost.Network().Peerstore()
92+
for _, p := range ps.Peers() {
93+
for _, a := range ps.Addrs(p) {
94+
addrs[p] = append(addrs[p], a)
95+
}
96+
sort.Slice(addrs[p], func(i, j int) bool {
97+
return addrs[p][i].String() < addrs[p][j].String()
98+
})
99+
}
100+
101+
return addrs, nil
102+
}
103+
104+
func (api *SwarmAPI) LocalAddrs(context.Context) ([]ma.Multiaddr, error) {
105+
if api.node.PeerHost == nil {
106+
return nil, coreiface.ErrOffline
107+
}
108+
109+
return api.node.PeerHost.Addrs(), nil
110+
}
111+
112+
func (api *SwarmAPI) ListenAddrs(context.Context) ([]ma.Multiaddr, error) {
113+
if api.node.PeerHost == nil {
114+
return nil, coreiface.ErrOffline
115+
}
116+
117+
return api.node.PeerHost.Network().InterfaceListenAddresses()
118+
}
119+
83120
func (api *SwarmAPI) Peers(context.Context) ([]coreiface.ConnectionInfo, error) {
84121
if api.node.PeerHost == nil {
85122
return nil, coreiface.ErrOffline
@@ -95,6 +132,7 @@ func (api *SwarmAPI) Peers(context.Context) ([]coreiface.ConnectionInfo, error)
95132
ci := &connInfo{
96133
api: api.CoreAPI,
97134
conn: c,
135+
dir: c.Stat().Direction,
98136

99137
addr: addr,
100138
peer: pid,
@@ -122,6 +160,10 @@ func (ci *connInfo) Address() ma.Multiaddr {
122160
return ci.addr
123161
}
124162

163+
func (ci *connInfo) Direction() net.Direction {
164+
return ci.dir
165+
}
166+
125167
func (ci *connInfo) Latency(context.Context) (time.Duration, error) {
126168
return ci.api.node.Peerstore.LatencyEWMA(peer.ID(ci.ID())), nil
127169
}

test/sharness/lib/test-lib.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,11 @@ test_set_address_vars() {
199199
'
200200

201201
if ipfs swarm addrs local >/dev/null 2>&1; then
202+
test_expect_success "get swarm addresses" '
203+
ipfs swarm addrs local > addrs_out
204+
'
205+
202206
test_expect_success "set swarm address vars" '
203-
ipfs swarm addrs local > addrs_out &&
204207
SWARM_MADDR=$(grep "127.0.0.1" addrs_out) &&
205208
SWARM_PORT=$(port_from_maddr $SWARM_MADDR)
206209
'

0 commit comments

Comments
 (0)