Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve swarm connect/disconnect commands #5107

Merged
merged 3 commits into from
Jun 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 42 additions & 17 deletions core/commands/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (
mafilter "gx/ipfs/QmSMZwvs3n4GBikZ7hKzT17c3bk65FmyZo2JqtJ16swqCv/multiaddr-filter"
swarm "gx/ipfs/QmSvhbgtjQJKdT5avEeb7cvjYs7YrhebJyM1K6GAnkKgfd/go-libp2p-swarm"
ma "gx/ipfs/QmUxSEGbv2nmYNnfXi7839wwQqTN3kwQeUxe8dTjZWZs7J/go-multiaddr"
peer "gx/ipfs/QmVf8hTAsLLFtn4WPCRNdnaF2Eag2qTBS6uR8AiHPZARXy/go-libp2p-peer"
inet "gx/ipfs/QmXdgNhVEgjLxjUoMs5ViQL7pboAt3Y7V7eGHRiE4qrmTE/go-libp2p-net"
pstore "gx/ipfs/QmZhsmorLpD9kmQ4ynbAu4vbKv2goMUnXazwGA4gnWHDjB/go-libp2p-peerstore"
iaddr "gx/ipfs/QmckPUj15AbTcLh6MpDEsQpfVCx34tmP2Xg1aNwLb5fiRF/go-ipfs-addr"
iaddr "gx/ipfs/QmaKviZCLQrpuyFdSjteik7kJFcQpcyZgb1VuuwaCBBaEa/go-ipfs-addr"
cmdkit "gx/ipfs/QmdE4gMduCKCGAcczM2F5ioYDfdeKuPix138wrES1YSr7f/go-ipfs-cmdkit"
)

Expand Down Expand Up @@ -457,26 +459,38 @@ it will reconnect.
output := make([]string, len(iaddrs))
for i, addr := range iaddrs {
taddr := addr.Transport()
output[i] = "disconnect " + addr.ID().Pretty()
id := addr.ID()
output[i] = "disconnect " + id.Pretty()

found := false
conns := n.PeerHost.Network().ConnsToPeer(addr.ID())
for _, conn := range conns {
if !conn.RemoteMultiaddr().Equal(taddr) {
continue
}
net := n.PeerHost.Network()

if err := conn.Close(); err != nil {
if taddr == nil {
if net.Connectedness(id) != inet.Connected {
output[i] += " failure: not connected"
} else if err := net.ClosePeer(id); err != nil {
output[i] += " failure: " + err.Error()
} else {
output[i] += " success"
}
found = true
break
}
} else {
found := false
for _, conn := range net.ConnsToPeer(id) {
if !conn.RemoteMultiaddr().Equal(taddr) {
continue
}

if !found {
output[i] += " failure: conn not found"
if err := conn.Close(); err != nil {
output[i] += " failure: " + err.Error()
} else {
output[i] += " success"
}
found = true
break
}

if !found {
output[i] += " failure: conn not found"
}
}
}
res.SetOutput(&stringList{output})
Expand Down Expand Up @@ -522,16 +536,27 @@ func parseAddresses(addrs []string) (iaddrs []iaddr.IPFSAddr, err error) {

// peersWithAddresses is a function that takes in a slice of string peer addresses
// (multiaddr + peerid) and returns a slice of properly constructed peers
func peersWithAddresses(addrs []string) (pis []pstore.PeerInfo, err error) {
func peersWithAddresses(addrs []string) ([]pstore.PeerInfo, error) {
iaddrs, err := parseAddresses(addrs)
if err != nil {
return nil, err
}

peers := make(map[peer.ID][]ma.Multiaddr, len(iaddrs))
for _, iaddr := range iaddrs {
id := iaddr.ID()
current, ok := peers[id]
if tpt := iaddr.Transport(); tpt != nil {
peers[id] = append(current, tpt)
} else if !ok {
peers[id] = nil
}
}
pis := make([]pstore.PeerInfo, 0, len(peers))
for id, maddrs := range peers {
pis = append(pis, pstore.PeerInfo{
ID: iaddr.ID(),
Addrs: []ma.Multiaddr{iaddr.Transport()},
ID: id,
Addrs: maddrs,
})
}
return pis, nil
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,9 @@
},
{
"author": "why",
"hash": "QmckPUj15AbTcLh6MpDEsQpfVCx34tmP2Xg1aNwLb5fiRF",
"hash": "QmaKviZCLQrpuyFdSjteik7kJFcQpcyZgb1VuuwaCBBaEa",
"name": "go-ipfs-addr",
"version": "0.1.12"
"version": "0.1.13"
},
{
"author": "The Go Authors",
Expand Down
2 changes: 1 addition & 1 deletion repo/config/bootstrap_peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"fmt"

iaddr "gx/ipfs/QmckPUj15AbTcLh6MpDEsQpfVCx34tmP2Xg1aNwLb5fiRF/go-ipfs-addr"
iaddr "gx/ipfs/QmaKviZCLQrpuyFdSjteik7kJFcQpcyZgb1VuuwaCBBaEa/go-ipfs-addr"
)

// DefaultBootstrapAddresses are the hardcoded bootstrap addresses
Expand Down
22 changes: 22 additions & 0 deletions test/sharness/t0140-swarm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,26 @@ test_expect_success "Addresses.NoAnnounce with /ipcidr affects addresses" '

test_kill_ipfs_daemon

test_expect_success "set up tcp testbed" '
iptb init -n 2 -p 0 -f --bootstrap=none
'

startup_cluster 2

test_expect_success "disconnect work without specifying a transport address" '
[ $(ipfsi 0 swarm peers | wc -l) -eq 1 ] &&
ipfsi 0 swarm disconnect "/ipfs/$(iptb get id 1)" &&
[ $(ipfsi 0 swarm peers | wc -l) -eq 0 ]
'

test_expect_success "connect work without specifying a transport address" '
[ $(ipfsi 0 swarm peers | wc -l) -eq 0 ] &&
ipfsi 0 swarm connect "/ipfs/$(iptb get id 1)" &&
[ $(ipfsi 0 swarm peers | wc -l) -eq 1 ]
'

test_expect_success "stopping cluster" '
iptb stop
'

test_done