mirror of
https://github.com/libp2p/go-libp2p-peerstore.git
synced 2024-12-28 23:50:12 +08:00
Merge pull request #14 from libp2p/feat/p2paddr
Add func to construct /ipfs addrs from PeerInfo
This commit is contained in:
commit
a1adbf426a
26
peerinfo.go
26
peerinfo.go
@ -31,12 +31,13 @@ func InfoFromP2pAddr(m ma.Multiaddr) (*PeerInfo, error) {
|
||||
return nil, ErrInvalidAddr
|
||||
}
|
||||
|
||||
ipfspart := parts[len(parts)-1] // last part
|
||||
// TODO(lgierth): we shouldn't assume /ipfs is the last part
|
||||
ipfspart := parts[len(parts)-1]
|
||||
if ipfspart.Protocols()[0].Code != ma.P_IPFS {
|
||||
return nil, ErrInvalidAddr
|
||||
}
|
||||
|
||||
// make sure 'ipfs id' parses as a peer.ID
|
||||
// make sure the /ipfs value parses as a peer.ID
|
||||
peerIdParts := strings.Split(ipfspart.String(), "/")
|
||||
peerIdStr := peerIdParts[len(peerIdParts)-1]
|
||||
id, err := peer.IDB58Decode(peerIdStr)
|
||||
@ -44,12 +45,31 @@ func InfoFromP2pAddr(m ma.Multiaddr) (*PeerInfo, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// we might have received just an /ipfs part, which means there's no addr.
|
||||
var addrs []ma.Multiaddr
|
||||
if len(parts) > 1 {
|
||||
addrs = append(addrs, ma.Join(parts[:len(parts)-1]...))
|
||||
}
|
||||
|
||||
return &PeerInfo{
|
||||
ID: id,
|
||||
Addrs: []ma.Multiaddr{ma.Join(parts[:len(parts)-1]...)},
|
||||
Addrs: addrs,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func InfoToP2pAddrs(pi *PeerInfo) ([]ma.Multiaddr, error) {
|
||||
addrs := []ma.Multiaddr{}
|
||||
tpl := "/" + ma.ProtocolWithCode(ma.P_IPFS).Name + "/"
|
||||
for _, addr := range pi.Addrs {
|
||||
p2paddr, err := ma.NewMultiaddr(tpl + peer.IDB58Encode(pi.ID))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
addrs = append(addrs, addr.Encapsulate(p2paddr))
|
||||
}
|
||||
return addrs, nil
|
||||
}
|
||||
|
||||
func (pi *PeerInfo) Loggable() map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"peerID": pi.ID.Pretty(),
|
||||
|
@ -58,28 +58,92 @@ func TestPeerInfoMarshal(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestP2pAddrParsing(t *testing.T) {
|
||||
a := mustAddr(t, "/ip4/1.2.3.4/tcp/4536")
|
||||
id, err := peer.IDB58Decode("QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
t.Error(err)
|
||||
}
|
||||
addr := ma.StringCast("/ip4/1.2.3.4/tcp/4536")
|
||||
p2paddr := ma.Join(addr, ma.StringCast("/ipfs/"+peer.IDB58Encode(id)))
|
||||
|
||||
p2pa := a.String() + "/ipfs/" + id.Pretty()
|
||||
p2pma, err := ma.NewMultiaddr(p2pa)
|
||||
pinfo, err := InfoFromP2pAddr(p2paddr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
pinfo, err := InfoFromP2pAddr(p2pma)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if pinfo.ID != id {
|
||||
t.Fatal("didnt get expected peerID")
|
||||
t.Fatalf("expected PeerID [%s], got [%s]", id, pinfo.ID)
|
||||
}
|
||||
|
||||
if !a.Equal(pinfo.Addrs[0]) {
|
||||
t.Fatal("didnt get expected address")
|
||||
if len(pinfo.Addrs) != 1 {
|
||||
t.Fatalf("expected 1 addr, got %d", len(pinfo.Addrs))
|
||||
}
|
||||
|
||||
if !addr.Equal(pinfo.Addrs[0]) {
|
||||
t.Fatalf("expected addr [%s], got [%s]", addr, pinfo.Addrs[0])
|
||||
}
|
||||
|
||||
addr = ma.StringCast("/ipfs/" + peer.IDB58Encode(id))
|
||||
pinfo, err = InfoFromP2pAddr(addr)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if pinfo.ID != id {
|
||||
t.Fatalf("expected PeerID [%s], got [%s]", id, pinfo.ID)
|
||||
}
|
||||
|
||||
if len(pinfo.Addrs) > 0 {
|
||||
t.Fatalf("expected 0 addrs, got %d", len(pinfo.Addrs))
|
||||
}
|
||||
|
||||
addr = ma.StringCast("/ip4/1.2.3.4/tcp/4536")
|
||||
pinfo, err = InfoFromP2pAddr(addr)
|
||||
if err == nil {
|
||||
t.Fatalf("expected error, got none")
|
||||
}
|
||||
|
||||
addr = ma.StringCast("/ip4/1.2.3.4/tcp/4536/http")
|
||||
pinfo, err = InfoFromP2pAddr(addr)
|
||||
if err == nil {
|
||||
t.Fatalf("expected error, got none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestP2pAddrConstruction(t *testing.T) {
|
||||
id, err := peer.IDB58Decode("QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
addr := ma.StringCast("/ip4/1.2.3.4/tcp/4536")
|
||||
p2paddr := ma.Join(addr, ma.StringCast("/ipfs/"+peer.IDB58Encode(id)))
|
||||
|
||||
pi := &PeerInfo{ID: id, Addrs: []ma.Multiaddr{addr}}
|
||||
p2paddrs, err := InfoToP2pAddrs(pi)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if len(p2paddrs) != 1 {
|
||||
t.Fatalf("expected 1 addr, got %d", len(p2paddrs))
|
||||
}
|
||||
|
||||
if !p2paddr.Equal(p2paddrs[0]) {
|
||||
t.Fatalf("expected [%s], got [%s]", p2paddr, p2paddrs[0])
|
||||
}
|
||||
|
||||
pi = &PeerInfo{ID: id}
|
||||
p2paddrs, err = InfoToP2pAddrs(pi)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if len(p2paddrs) > 0 {
|
||||
t.Fatalf("expected 0 addrs, got %d", len(p2paddrs))
|
||||
}
|
||||
|
||||
pi = &PeerInfo{Addrs: []ma.Multiaddr{ma.StringCast("/ip4/1.2.3.4/tcp/4536")}}
|
||||
_, err = InfoToP2pAddrs(pi)
|
||||
if err == nil {
|
||||
t.Fatalf("expected error, got none")
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user