Skip to content

Commit d70ae1b

Browse files
feat: swarm peering command without save option
1 parent 7c76118 commit d70ae1b

File tree

5 files changed

+183
-0
lines changed

5 files changed

+183
-0
lines changed

core/commands/commands_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ func TestCommands(t *testing.T) {
227227
"/swarm/filters/add",
228228
"/swarm/filters/rm",
229229
"/swarm/peers",
230+
"/swarm/peering",
231+
"/swarm/peering/add",
232+
"/swarm/peering/ls",
233+
"/swarm/peering/rm",
230234
"/tar",
231235
"/tar/add",
232236
"/tar/cat",

core/commands/swarm.go

+124
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ ipfs peers in the internet.
5151
"disconnect": swarmDisconnectCmd,
5252
"filters": swarmFiltersCmd,
5353
"peers": swarmPeersCmd,
54+
"peering": swarmPeeringCmd,
5455
},
5556
}
5657

@@ -61,6 +62,129 @@ const (
6162
swarmDirectionOptionName = "direction"
6263
)
6364

65+
var swarmPeeringCmd = &cmds.Command{
66+
Helptext: cmds.HelpText{
67+
Tagline: "modify the peering service.",
68+
ShortDescription: `
69+
'ipfs swarm peering' is a tool to manupulate the peering service.
70+
Peers in the peering service is maintained to be connected, reconnected
71+
on disconnect with a back-off.
72+
`,
73+
},
74+
Subcommands: map[string]*cmds.Command{
75+
"add": swarmPeeringAddCmd,
76+
"ls": swarmPeeringLsCmd,
77+
"rm": swarmPeeringRmCmd,
78+
},
79+
}
80+
81+
var swarmPeeringAddCmd = &cmds.Command{
82+
Helptext: cmds.HelpText{
83+
Tagline: "add peers into the peering service.",
84+
ShortDescription: `
85+
'ipfs swarm peering add' adds peers into the peering service.
86+
`,
87+
},
88+
Arguments: []cmds.Argument{
89+
cmds.StringArg("address", true, true, "address of peer to add into the PeeringService"),
90+
},
91+
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
92+
addrs := make([]ma.Multiaddr, len(req.Arguments))
93+
94+
for i, arg := range req.Arguments {
95+
addr, err := ma.NewMultiaddr(arg)
96+
if err != nil {
97+
return err
98+
}
99+
100+
addrs[i] = addr
101+
}
102+
103+
addInfos, err := peer.AddrInfosFromP2pAddrs(addrs...)
104+
if err != nil {
105+
return err
106+
}
107+
108+
node, err := cmdenv.GetNode(env)
109+
if err != nil {
110+
return err
111+
}
112+
113+
output := make([]string, len(addInfos))
114+
for i, id := range addInfos {
115+
output[i] = "add " + id.ID.Pretty()
116+
node.Peering.AddPeer(id)
117+
118+
output[i] += " success"
119+
}
120+
121+
return cmds.EmitOnce(res, &stringList{output})
122+
},
123+
Encoders: cmds.EncoderMap{
124+
cmds.Text: cmds.MakeTypedEncoder(stringListEncoder),
125+
},
126+
Type: stringList{},
127+
}
128+
129+
var swarmPeeringLsCmd = &cmds.Command{
130+
Helptext: cmds.HelpText{
131+
Tagline: "list peers registered in the peering service.",
132+
ShortDescription: `
133+
'ipfs swarm peering ls' lists peers registered in the peering service.`,
134+
},
135+
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
136+
node, err := cmdenv.GetNode(env)
137+
if err != nil {
138+
return err
139+
}
140+
peers := node.Peering.ListPeer()
141+
return cmds.EmitOnce(res, addrInfos{Peers: peers})
142+
},
143+
Type: addrInfos{},
144+
}
145+
146+
type addrInfos struct {
147+
Peers []peer.AddrInfo
148+
}
149+
150+
var swarmPeeringRmCmd = &cmds.Command{
151+
Helptext: cmds.HelpText{
152+
Tagline: "remove a peer from the peering service.",
153+
ShortDescription: `
154+
'ipfs swarm peering rm' removes peers from the peering service.
155+
`,
156+
},
157+
Arguments: []cmds.Argument{
158+
cmds.StringArg("ID", true, true, "ID of peer to remove from PeeringService"),
159+
},
160+
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
161+
node, err := cmdenv.GetNode(env)
162+
if err != nil {
163+
return err
164+
}
165+
166+
output := make([]string, 0, len(req.Arguments))
167+
for _, arg := range req.Arguments {
168+
id, err := peer.Decode(arg)
169+
if err != nil {
170+
return err
171+
}
172+
173+
msg := "remove " + id.Pretty()
174+
175+
node.Peering.RemovePeer(id)
176+
177+
msg += " success"
178+
output = append(output, msg)
179+
}
180+
return cmds.EmitOnce(res, &stringList{output})
181+
},
182+
Type: stringList{},
183+
Encoders: cmds.EncoderMap{
184+
cmds.Text: cmds.MakeTypedEncoder(stringListEncoder),
185+
},
186+
}
187+
64188
var swarmPeersCmd = &cmds.Command{
65189
Helptext: cmds.HelpText{
66190
Tagline: "List peers with open connections.",

peering/peering.go

+19
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package peering
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"math/rand"
78
"sync"
89
"time"
@@ -212,6 +213,7 @@ func (ps *PeeringService) Stop() error {
212213
// Add peer may also be called multiple times for the same peer. The new
213214
// addresses will replace the old.
214215
func (ps *PeeringService) AddPeer(info peer.AddrInfo) {
216+
fmt.Printf("atempt to add peer %s of addrs %s\n", info.ID, info.Addrs)
215217
ps.mu.Lock()
216218
defer ps.mu.Unlock()
217219

@@ -220,6 +222,7 @@ func (ps *PeeringService) AddPeer(info peer.AddrInfo) {
220222
handler.setAddrs(info.Addrs)
221223
} else {
222224
logger.Infow("peer added", "peer", info.ID, "addrs", info.Addrs)
225+
// in case ps.host is null this line results in a panic.
223226
ps.host.ConnManager().Protect(info.ID, connmgrTag)
224227

225228
handler = &peerHandler{
@@ -242,6 +245,22 @@ func (ps *PeeringService) AddPeer(info peer.AddrInfo) {
242245
}
243246
}
244247

248+
// ListPeer lists peers in the peering service.
249+
func (ps *PeeringService) ListPeer() []peer.AddrInfo {
250+
out := make([]peer.AddrInfo, len(ps.peers))
251+
c := 0
252+
for k, v := range ps.peers {
253+
out_addrs := make([]multiaddr.Multiaddr, len(v.addrs))
254+
copy(out_addrs, v.addrs)
255+
out[c] = peer.AddrInfo{
256+
ID: k,
257+
Addrs: out_addrs,
258+
}
259+
c++
260+
}
261+
return out
262+
}
263+
245264
// RemovePeer removes a peer from the peering service. This function may be
246265
// safely called at any time: before the service is started, while running, or
247266
// after it stops.

peering/peering_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func TestPeeringService(t *testing.T) {
3939

4040
// peer 1 -> 2
4141
ps1.AddPeer(peer.AddrInfo{ID: h2.ID(), Addrs: h2.Addrs()})
42+
require.Contains(t, ps1.ListPeer(), peer.AddrInfo{ID: h2.ID(), Addrs: h2.Addrs()})
4243

4344
// We haven't started so we shouldn't have any peers.
4445
require.Never(t, func() bool {
@@ -109,6 +110,7 @@ func TestPeeringService(t *testing.T) {
109110

110111
// Unprotect 2 from 1.
111112
ps1.RemovePeer(h2.ID())
113+
require.NotContains(t, ps1.ListPeer(), peer.AddrInfo{ID: h2.ID(), Addrs: h2.Addrs()})
112114

113115
// Trim connections.
114116
h1.ConnManager().TrimOpenConns(ctx)
@@ -127,7 +129,9 @@ func TestPeeringService(t *testing.T) {
127129

128130
// Until added back
129131
ps1.AddPeer(peer.AddrInfo{ID: h2.ID(), Addrs: h2.Addrs()})
132+
require.Contains(t, ps1.ListPeer(), peer.AddrInfo{ID: h2.ID(), Addrs: h2.Addrs()})
130133
ps1.AddPeer(peer.AddrInfo{ID: h3.ID(), Addrs: h3.Addrs()})
134+
require.Contains(t, ps1.ListPeer(), peer.AddrInfo{ID: h3.ID(), Addrs: h3.Addrs()})
131135
t.Logf("wait for h1 to connect to h2 and h3 again")
132136
require.Eventually(t, func() bool {
133137
return h1.Network().Connectedness(h2.ID()) == network.Connected
@@ -142,7 +146,9 @@ func TestPeeringService(t *testing.T) {
142146

143147
// Adding and removing should work after stopping.
144148
ps1.AddPeer(peer.AddrInfo{ID: h4.ID(), Addrs: h4.Addrs()})
149+
require.Contains(t, ps1.ListPeer(), peer.AddrInfo{ID: h4.ID(), Addrs: h4.Addrs()})
145150
ps1.RemovePeer(h2.ID())
151+
require.NotContains(t, ps1.ListPeer(), peer.AddrInfo{ID: h2.ID(), Addrs: h2.Addrs()})
146152
}
147153

148154
func TestNextBackoff(t *testing.T) {

test/sharness/t0140-swarm.sh

+30
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,36 @@ test_expect_success "Addresses.NoAnnounce with /ipcidr affects addresses" '
9797

9898
test_kill_ipfs_daemon
9999

100+
test_launch_ipfs_daemon
101+
102+
test_expect_success "'ipfs swarm peering ls' lists peerings" '
103+
ipfs swarm peering ls
104+
'
105+
106+
peeringID='QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5N'
107+
peeringAddr='/ip4/1.2.3.4/tcp/1234/p2p/QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5N'
108+
test_expect_success "'ipfs swarm peering add' adds a peering" '
109+
ipfs swarm peering ls > peeringls &&
110+
! test_should_contain ${peeringID} peeringls
111+
ipfs swarm peering add $peeringAddr
112+
'
113+
114+
test_expect_success 'a peering is added' '
115+
ipfs swarm peering ls > peeringadd &&
116+
test_should_contain ${peeringID} peeringadd
117+
'
118+
119+
test_expect_success "'swarm peering rm' removes a peering" '
120+
ipfs swarm peering rm ${peeringID}
121+
'
122+
123+
test_expect_success 'peering is removed' '
124+
ipfs swarm peering ls > peeringrm &&
125+
! test_should_contain ${peeringID} peeringrm
126+
'
127+
128+
test_kill_ipfs_daemon
129+
100130
test_expect_success "set up tcp testbed" '
101131
iptb testbed create -type localipfs -count 2 -force -init
102132
'

0 commit comments

Comments
 (0)