Merge pull request #878 from ipfs/feat/sort-responses
Sort addresses in `/id`
This commit is contained in:
commit
c1ff0651d3
14
api/util.go
14
api/util.go
|
@ -2,7 +2,6 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
peer "github.com/libp2p/go-libp2p-core/peer"
|
peer "github.com/libp2p/go-libp2p-core/peer"
|
||||||
ma "github.com/multiformats/go-multiaddr"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// PeersToStrings IDB58Encodes a list of peers.
|
// PeersToStrings IDB58Encodes a list of peers.
|
||||||
|
@ -29,16 +28,3 @@ func StringsToPeers(strs []string) []peer.ID {
|
||||||
}
|
}
|
||||||
return peers
|
return peers
|
||||||
}
|
}
|
||||||
|
|
||||||
// MustLibp2pMultiaddrJoin takes a LibP2P multiaddress and a peer ID and
|
|
||||||
// encapsulates a new /p2p/<peerID> address. It will panic if the given
|
|
||||||
// peer ID is bad.
|
|
||||||
func MustLibp2pMultiaddrJoin(addr Multiaddr, p peer.ID) Multiaddr {
|
|
||||||
v := addr.Value()
|
|
||||||
pidAddr, err := ma.NewMultiaddr("/p2p/" + peer.IDB58Encode(p))
|
|
||||||
// let this break badly
|
|
||||||
if err != nil {
|
|
||||||
panic("called MustLibp2pMultiaddrJoin with bad peer!")
|
|
||||||
}
|
|
||||||
return Multiaddr{Multiaddr: v.Encapsulate(pidAddr)}
|
|
||||||
}
|
|
||||||
|
|
19
cluster.go
19
cluster.go
|
@ -741,15 +741,13 @@ func (c *Cluster) ID(ctx context.Context) *api.ID {
|
||||||
Error: err.Error(),
|
Error: err.Error(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var addrs []api.Multiaddr
|
|
||||||
|
|
||||||
addrsSet := make(map[string]struct{}) // to filter dups
|
var addrs []api.Multiaddr
|
||||||
for _, addr := range c.host.Addrs() {
|
mAddrs, err := peer.AddrInfoToP2pAddrs(&peer.AddrInfo{ID: c.id, Addrs: c.host.Addrs()})
|
||||||
addrsSet[addr.String()] = struct{}{}
|
if err == nil {
|
||||||
|
for _, mAddr := range mAddrs {
|
||||||
|
addrs = append(addrs, api.NewMultiaddrWithValue(mAddr))
|
||||||
}
|
}
|
||||||
for k := range addrsSet {
|
|
||||||
addr, _ := api.NewMultiaddr(k)
|
|
||||||
addrs = append(addrs, api.MustLibp2pMultiaddrJoin(addr, c.id))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
peers := []peer.ID{}
|
peers := []peer.ID{}
|
||||||
|
@ -771,7 +769,7 @@ func (c *Cluster) ID(ctx context.Context) *api.ID {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &api.ID{
|
id := &api.ID{
|
||||||
ID: c.id,
|
ID: c.id,
|
||||||
//PublicKey: c.host.Peerstore().PubKey(c.id),
|
//PublicKey: c.host.Peerstore().PubKey(c.id),
|
||||||
Addresses: addrs,
|
Addresses: addrs,
|
||||||
|
@ -782,6 +780,11 @@ func (c *Cluster) ID(ctx context.Context) *api.ID {
|
||||||
IPFS: ipfsID,
|
IPFS: ipfsID,
|
||||||
Peername: c.config.Peername,
|
Peername: c.config.Peername,
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
|
id.Error = err.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
// PeerAdd adds a new peer to this Cluster.
|
// PeerAdd adds a new peer to this Cluster.
|
||||||
|
|
|
@ -3,6 +3,7 @@ package crdt
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -404,6 +405,8 @@ func (css *Consensus) Peers(ctx context.Context) ([]peer.ID, error) {
|
||||||
peers = append(peers, css.host.ID())
|
peers = append(peers, css.host.ID())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sort.Sort(peer.IDSlice(peers))
|
||||||
|
|
||||||
return peers, nil
|
return peers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package ipfscluster
|
package ipfscluster
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -565,6 +567,34 @@ func TestClustersPeers(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestClustersPeersRetainOrder(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
clusters, mock := createClusters(t)
|
||||||
|
defer shutdownClusters(t, clusters, mock)
|
||||||
|
|
||||||
|
delay()
|
||||||
|
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
j := rand.Intn(nClusters) // choose a random cluster peer
|
||||||
|
peers1, err := json.Marshal(clusters[j].Peers(ctx))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
waitForLeaderAndMetrics(t, clusters)
|
||||||
|
|
||||||
|
k := rand.Intn(nClusters)
|
||||||
|
peers2, err := json.Marshal(clusters[k].Peers(ctx))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if bytes.Compare(peers1, peers2) != 0 {
|
||||||
|
t.Error("expected both results to be same")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestClustersPin(t *testing.T) {
|
func TestClustersPin(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
clusters, mock := createClusters(t)
|
clusters, mock := createClusters(t)
|
||||||
|
|
|
@ -15,6 +15,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
logging "github.com/ipfs/go-log"
|
logging "github.com/ipfs/go-log"
|
||||||
|
utils "github.com/ipfs/ipfs-cluster/utils"
|
||||||
host "github.com/libp2p/go-libp2p-core/host"
|
host "github.com/libp2p/go-libp2p-core/host"
|
||||||
net "github.com/libp2p/go-libp2p-core/network"
|
net "github.com/libp2p/go-libp2p-core/network"
|
||||||
peer "github.com/libp2p/go-libp2p-core/peer"
|
peer "github.com/libp2p/go-libp2p-core/peer"
|
||||||
|
@ -142,6 +143,7 @@ func (pm *Manager) filteredPeerAddrs(p peer.ID) []ma.Multiaddr {
|
||||||
return peerDNSAddrs
|
return peerDNSAddrs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sort.Sort(utils.ByString(peerAddrs))
|
||||||
return peerAddrs
|
return peerAddrs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
12
utils/utils.go
Normal file
12
utils/utils.go
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
ma "github.com/multiformats/go-multiaddr"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ByString can sort multiaddresses by its string
|
||||||
|
type ByString []ma.Multiaddr
|
||||||
|
|
||||||
|
func (m ByString) Len() int { return len(m) }
|
||||||
|
func (m ByString) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
|
||||||
|
func (m ByString) Less(i, j int) bool { return m[i].String() < m[j].String() }
|
Loading…
Reference in New Issue
Block a user