From 1a77ee1c67b09d7b4ec6619a5cd7d3624db6f357 Mon Sep 17 00:00:00 2001 From: Gabriel mermelstein Date: Wed, 30 Oct 2024 14:31:46 +0200 Subject: [PATCH] fixing test and adding missing procs --- wakuv2/nwaku.go | 40 ++++++++++++++++++++++++++++++++++++++++ wakuv2/nwaku_test.go | 5 +++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/wakuv2/nwaku.go b/wakuv2/nwaku.go index 58f04bb4b97..da92e1ea770 100644 --- a/wakuv2/nwaku.go +++ b/wakuv2/nwaku.go @@ -238,6 +238,10 @@ package wakuv2 WAKU_CALL (waku_relay_get_num_connected_peers(ctx, pubSubTopic, (WakuCallBack) callback, resp) ); } + static void cGoWakuGetConnectedPeers(void* wakuCtx, void* resp) { + WAKU_CALL (waku_get_connected_peers(wakuCtx, (WakuCallBack) callback, resp) ); + } + static void cGoWakuGetPeerIdsFromPeerStore(void* wakuCtx, void* resp) { WAKU_CALL (waku_get_peerids_from_peerstore(wakuCtx, (WakuCallBack) callback, resp) ); } @@ -2821,6 +2825,42 @@ func (self *Waku) GetNumConnectedRelayPeers(paramPubsubTopic ...string) (int, er return 0, errors.New(errMsg) } +func (self *Waku) GetConnectedPeers() (peer.IDSlice, error) { + var resp = C.allocResp() + defer C.freeResp(resp) + C.cGoWakuGetConnectedPeers(self.wakuCtx, resp) + + if C.getRet(resp) == C.RET_OK { + peersStr := C.GoStringN(C.getMyCharPtr(resp), C.int(C.getMyCharLen(resp))) + if peersStr == "" { + return peer.IDSlice{}, nil + } + // peersStr contains a comma-separated list of peer ids + itemsPeerIds := strings.Split(peersStr, ",") + + var peers peer.IDSlice + for _, peerId := range itemsPeerIds { + id, err := peer.Decode(peerId) + if err != nil { + return nil, fmt.Errorf("GetConnectedPeers - decoding peerId: %w", err) + } + peers = append(peers, id) + } + + return peers, nil + } + errMsg := C.GoStringN(C.getMyCharPtr(resp), C.int(C.getMyCharLen(resp))) + return nil, fmt.Errorf("GetConnectedPeers: %s", errMsg) +} + +func (self *Waku) GetNumConnectedPeers() (int, error) { + connecterPeers, err := self.GetConnectedPeers() + if err != nil { + return 0, err + } + return len(connecterPeers), nil +} + func (self *Waku) GetPeerIdsFromPeerStore() (peer.IDSlice, error) { var resp = C.allocResp() defer C.freeResp(resp) diff --git a/wakuv2/nwaku_test.go b/wakuv2/nwaku_test.go index 8c2d1952ff1..f1853ed8063 100644 --- a/wakuv2/nwaku_test.go +++ b/wakuv2/nwaku_test.go @@ -12,6 +12,7 @@ import ( "github.com/cenkalti/backoff/v3" "github.com/libp2p/go-libp2p/core/peer" + "github.com/libp2p/go-libp2p/p2p/protocol/ping" "github.com/waku-org/go-waku/waku/v2/protocol/store" "go.uber.org/zap" @@ -229,7 +230,7 @@ func TestBasicWakuV2(t *testing.T) { require.True(t, isDisconnected, "nwaku should be disconnected from the store node") // Re-connect - err = w.DialPeerByID(storeNode.ID) + err = w.DialPeerByID(storeNode.ID, ping.ID) require.NoError(t, err) time.Sleep(1 * time.Second) @@ -609,7 +610,7 @@ func TestDial(t *testing.T) { require.True(t, receiverPeerCount == 0, "Receiver node should have no connected peers") // Dial - err = dialerNode.DialPeer(receiverMultiaddr[0]) + err = dialerNode.DialPeer(receiverMultiaddr[0], ping.ID) require.NoError(t, err) time.Sleep(1 * time.Second)