Skip to content

Commit

Permalink
integrating waku_get_connected_peers and fixing test
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmer committed Oct 28, 2024
1 parent aa23033 commit d7290d4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 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_get_peerids_from_peerstore(wakuCtx, (WakuCallBack) callback, resp) );
}
static void cGoWakuGetConnectedPeers(void* wakuCtx, void* resp) {
WAKU_CALL (waku_get_connected_peers(wakuCtx, (WakuCallBack) callback, resp) );
}
static void cGoWakuLightpushPublish(void* wakuCtx,
const char* pubSubTopic,
const char* jsonWakuMessage,
Expand Down Expand Up @@ -2818,6 +2822,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: 1 addition & 4 deletions wakuv2/nwaku_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package wakuv2
import (
"context"
"errors"
"fmt"
"slices"
"testing"
"time"
Expand Down Expand Up @@ -613,12 +612,10 @@ func TestDial(t *testing.T) {

// Check that both nodes now have one connected peer
dialerPeerCount = dialerNode.PeerCount()
peerIds, _ := dialerNode.GetPeerIdsFromPeerStore()
fmt.Println("------------- dialerNode.GetPeerIdsFromPeerStore(): ", peerIds)
require.True(t, dialerPeerCount == 1, "Dialer node should have 1 peer")

receiverPeerCount = receiverNode.PeerCount()
require.True(t, receiverPeerCount == 0, "Receiver node should have 1 peer")
require.True(t, receiverPeerCount == 1, "Receiver node should have 1 peer")

// Stop nodes
require.NoError(t, dialerNode.Stop())
Expand Down

0 comments on commit d7290d4

Please sign in to comment.