Skip to content

Commit

Permalink
fixing test and adding missing procs
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmer committed Oct 30, 2024
1 parent c1924c6 commit 1a77ee1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
40 changes: 40 additions & 0 deletions wakuv2/nwaku.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) );
}
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions wakuv2/nwaku_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1a77ee1

Please sign in to comment.