From a6c7067f3b8074bc35f92f48cf0d0deff523a52c Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Thu, 24 Nov 2022 11:00:44 -0400 Subject: [PATCH] feat: add multiaddresses to waku2 peers --- eth-node/bridge/geth/waku.go | 4 ++-- eth-node/bridge/geth/wakuv2.go | 2 +- eth-node/types/waku.go | 13 +++++++++---- protocol/messenger_peers.go | 4 +++- protocol/transport/transport.go | 2 +- services/ext/api.go | 2 +- wakuv2/waku.go | 23 +++++++++++++++-------- 7 files changed, 32 insertions(+), 18 deletions(-) diff --git a/eth-node/bridge/geth/waku.go b/eth-node/bridge/geth/waku.go index 754f38152..abff353d3 100644 --- a/eth-node/bridge/geth/waku.go +++ b/eth-node/bridge/geth/waku.go @@ -84,8 +84,8 @@ func (w *gethWakuWrapper) SubscribeToConnStatusChanges() (*types.ConnStatusSubsc } // Peers function only added for compatibility with waku V2 -func (w *gethWakuWrapper) Peers() map[string][]string { - p := make(map[string][]string) +func (w *gethWakuWrapper) Peers() map[string]types.WakuV2Peer { + p := make(map[string]types.WakuV2Peer) return p } diff --git a/eth-node/bridge/geth/wakuv2.go b/eth-node/bridge/geth/wakuv2.go index 5eccdd211..d48c6f98a 100644 --- a/eth-node/bridge/geth/wakuv2.go +++ b/eth-node/bridge/geth/wakuv2.go @@ -241,7 +241,7 @@ func (w *gethWakuV2Wrapper) AddRelayPeer(address string) (string, error) { return w.waku.AddRelayPeer(address) } -func (w *gethWakuV2Wrapper) Peers() map[string][]string { +func (w *gethWakuV2Wrapper) Peers() map[string]types.WakuV2Peer { return w.waku.Peers() } diff --git a/eth-node/types/waku.go b/eth-node/types/waku.go index ce1c3f39b..63506e534 100644 --- a/eth-node/types/waku.go +++ b/eth-node/types/waku.go @@ -11,9 +11,14 @@ import ( ) type ConnStatus struct { - IsOnline bool `json:"isOnline"` - HasHistory bool `json:"hasHistory"` - Peers map[string][]string `json:"peers"` + IsOnline bool `json:"isOnline"` + HasHistory bool `json:"hasHistory"` + Peers map[string]WakuV2Peer `json:"peers"` +} + +type WakuV2Peer struct { + Protocols []string `json:"protocols"` + Addresses []string `json:"addresses"` } type ConnStatusSubscription struct { @@ -71,7 +76,7 @@ type Waku interface { // PeerCount PeerCount() int - Peers() map[string][]string + Peers() map[string]WakuV2Peer StartDiscV5() error diff --git a/protocol/messenger_peers.go b/protocol/messenger_peers.go index 5f56cfedd..d0b9c6812 100644 --- a/protocol/messenger_peers.go +++ b/protocol/messenger_peers.go @@ -1,5 +1,7 @@ package protocol +import "github.com/status-im/status-go/eth-node/types" + func (m *Messenger) AddStorePeer(address string) (string, error) { return m.transport.AddStorePeer(address) } @@ -20,6 +22,6 @@ func (m *Messenger) DropPeer(peerID string) error { return m.transport.DropPeer(peerID) } -func (m *Messenger) Peers() map[string][]string { +func (m *Messenger) Peers() map[string]types.WakuV2Peer { return m.transport.Peers() } diff --git a/protocol/transport/transport.go b/protocol/transport/transport.go index 97d65b99a..d34336d2b 100644 --- a/protocol/transport/transport.go +++ b/protocol/transport/transport.go @@ -435,7 +435,7 @@ func (t *Transport) PeerCount() int { return t.waku.PeerCount() } -func (t *Transport) Peers() map[string][]string { +func (t *Transport) Peers() map[string]types.WakuV2Peer { return t.waku.Peers() } diff --git a/services/ext/api.go b/services/ext/api.go index 937f4b9df..7b155c9de 100644 --- a/services/ext/api.go +++ b/services/ext/api.go @@ -1159,7 +1159,7 @@ func (api *PublicAPI) DropPeer(peerID string) error { return api.service.messenger.DropPeer(peerID) } -func (api *PublicAPI) Peers() map[string][]string { +func (api *PublicAPI) Peers() map[string]types.WakuV2Peer { return api.service.messenger.Peers() } diff --git a/wakuv2/waku.go b/wakuv2/waku.go index 1ff167897..53466172e 100644 --- a/wakuv2/waku.go +++ b/wakuv2/waku.go @@ -263,7 +263,7 @@ func New(nodeKey string, cfg *Config, logger *zap.Logger, appDB *sql.DB) (*Waku, return case c := <-connStatusChan: waku.connStatusMu.Lock() - latestConnStatus := formatConnStatus(c) + latestConnStatus := formatConnStatus(waku.node, c) for k, subs := range waku.connStatusSubscriptions { if subs.Active() { subs.C <- latestConnStatus @@ -1121,8 +1121,8 @@ func (w *Waku) PeerCount() int { return w.node.PeerCount() } -func (w *Waku) Peers() map[string][]string { - return FormatPeerStats(w.node.PeerStats()) +func (w *Waku) Peers() map[string]types.WakuV2Peer { + return FormatPeerStats(w.node, w.node.PeerStats()) } func (w *Waku) StartDiscV5() error { @@ -1231,18 +1231,25 @@ func toDeterministicID(id string, expectedLen int) (string, error) { return id, nil } -func FormatPeerStats(peers node.PeerStats) map[string][]string { - p := make(map[string][]string) +func FormatPeerStats(wakuNode *node.WakuNode, peers node.PeerStats) map[string]types.WakuV2Peer { + p := make(map[string]types.WakuV2Peer) for k, v := range peers { - p[k.Pretty()] = v + peerInfo := wakuNode.Host().Peerstore().PeerInfo(k) + wakuV2Peer := types.WakuV2Peer{} + wakuV2Peer.Protocols = v + hostInfo, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/p2p/%s", k.Pretty())) + for _, addr := range peerInfo.Addrs { + wakuV2Peer.Addresses = append(wakuV2Peer.Addresses, addr.Encapsulate(hostInfo).String()) + } + p[k.Pretty()] = wakuV2Peer } return p } -func formatConnStatus(c node.ConnStatus) types.ConnStatus { +func formatConnStatus(wakuNode *node.WakuNode, c node.ConnStatus) types.ConnStatus { return types.ConnStatus{ IsOnline: c.IsOnline, HasHistory: c.HasHistory, - Peers: FormatPeerStats(c.Peers), + Peers: FormatPeerStats(wakuNode, c.Peers), } }