mirror of
https://github.com/status-im/status-go.git
synced 2025-02-08 21:04:27 +00:00
Update vendor Integrate rendezvous into status node Add a test with failover using rendezvous Use multiple servers in client Use discovery V5 by default and test that node can be started with rendezvous discovet Fix linter Update rendezvous client to one with instrumented stream Address feedback Fix test with updated topic limits Apply several suggestions Change log to debug for request errors because we continue execution Remove web3js after rebase Update rendezvous package
110 lines
2.5 KiB
Go
110 lines
2.5 KiB
Go
package peerstore
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/libp2p/go-libp2p-peer"
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
)
|
|
|
|
// PeerInfo is a small struct used to pass around a peer with
|
|
// a set of addresses (and later, keys?). This is not meant to be
|
|
// a complete view of the system, but rather to model updates to
|
|
// the peerstore. It is used by things like the routing system.
|
|
type PeerInfo struct {
|
|
ID peer.ID
|
|
Addrs []ma.Multiaddr
|
|
}
|
|
|
|
var ErrInvalidAddr = fmt.Errorf("invalid p2p multiaddr")
|
|
|
|
func InfoFromP2pAddr(m ma.Multiaddr) (*PeerInfo, error) {
|
|
if m == nil {
|
|
return nil, ErrInvalidAddr
|
|
}
|
|
|
|
// make sure it's an IPFS addr
|
|
parts := ma.Split(m)
|
|
if len(parts) < 1 {
|
|
return nil, ErrInvalidAddr
|
|
}
|
|
|
|
// 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 the /ipfs value parses as a peer.ID
|
|
peerIdParts := strings.Split(ipfspart.String(), "/")
|
|
peerIdStr := peerIdParts[len(peerIdParts)-1]
|
|
id, err := peer.IDB58Decode(peerIdStr)
|
|
if err != nil {
|
|
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: 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(),
|
|
"addrs": pi.Addrs,
|
|
}
|
|
}
|
|
|
|
func (pi *PeerInfo) MarshalJSON() ([]byte, error) {
|
|
out := make(map[string]interface{})
|
|
out["ID"] = pi.ID.Pretty()
|
|
var addrs []string
|
|
for _, a := range pi.Addrs {
|
|
addrs = append(addrs, a.String())
|
|
}
|
|
out["Addrs"] = addrs
|
|
return json.Marshal(out)
|
|
}
|
|
|
|
func (pi *PeerInfo) UnmarshalJSON(b []byte) error {
|
|
var data map[string]interface{}
|
|
err := json.Unmarshal(b, &data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
pid, err := peer.IDB58Decode(data["ID"].(string))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
pi.ID = pid
|
|
addrs, ok := data["Addrs"].([]interface{})
|
|
if ok {
|
|
for _, a := range addrs {
|
|
pi.Addrs = append(pi.Addrs, ma.StringCast(a.(string)))
|
|
}
|
|
}
|
|
return nil
|
|
}
|