Skip to content

Commit

Permalink
chore: libwaku retrieve my enr and adapt golang example (#2987)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivansete-status authored Aug 22, 2024
1 parent 53adfb0 commit 1ff9f1d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
28 changes: 27 additions & 1 deletion examples/golang/waku.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package main

/*
#cgo LDFLAGS: -L../../build/ -lwaku -Wl,--allow-multiple-definition
#cgo LDFLAGS: -L../../build/ -lwaku -lnegentropy -Wl,--allow-multiple-definition
#cgo LDFLAGS: -L../../ -Wl,-rpath,../../
#include "../../library/libwaku.h"
#include <stdio.h>
Expand Down Expand Up @@ -171,6 +172,10 @@ package main
WAKU_CALL (waku_listen_addresses(ctx, (WakuCallBack) callback, resp) );
}
void cGoWakuGetMyENR(void* ctx, void* resp) {
WAKU_CALL (waku_get_my_enr(ctx, (WakuCallBack) callback, resp) );
}
*/
import "C"

Expand Down Expand Up @@ -446,6 +451,20 @@ func (self *WakuNode) WakuListenAddresses() (string, error) {
return "", errors.New(errMsg)
}

func (self *WakuNode) WakuGetMyENR() (string, error) {
var resp = C.allocResp()
defer C.freeResp(resp)
C.cGoWakuGetMyENR(self.ctx, resp)

if C.getRet(resp) == C.RET_OK {
var myENR = C.GoStringN(C.getMyCharPtr(resp), C.int(C.getMyCharLen(resp)))
return myENR, nil
}
errMsg := "error WakuGetMyENR: " +
C.GoStringN(C.getMyCharPtr(resp), C.int(C.getMyCharLen(resp)))
return "", errors.New(errMsg)
}

func main() {
WakuSetup()

Expand Down Expand Up @@ -516,11 +535,18 @@ func main() {
return
}

myENR, err := node.WakuGetMyENR()
if err != nil {
fmt.Println("Error happened:", err.Error())
return
}

fmt.Println("Version:", version)
fmt.Println("Custom content topic:", formattedContentTopic)
fmt.Println("Custom pubsub topic:", formattedPubsubTopic)
fmt.Println("Default pubsub topic:", defaultPubsubTopic)
fmt.Println("Listen addresses:", listenAddresses)
fmt.Println("My ENR:", myENR)

// Wait for a SIGINT or SIGTERM signal
ch := make(chan os.Signal, 1)
Expand Down
5 changes: 5 additions & 0 deletions library/libwaku.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ int waku_discv5_update_bootnodes(void* ctx,
WakuCallBack callback,
void* userData);

// Retrieves the ENR information
int waku_get_my_enr(void* ctx,
WakuCallBack callback,
void* userData);

#ifdef __cplusplus
}
#endif
Expand Down
19 changes: 19 additions & 0 deletions library/libwaku.nim
Original file line number Diff line number Diff line change
Expand Up @@ -491,5 +491,24 @@ proc waku_discv5_update_bootnodes(
callback(RET_OK, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_OK

proc waku_get_my_enr(
ctx: ptr Context, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
ctx[].userData = userData

let connRes = waku_thread.sendRequestToWakuThread(
ctx,
RequestType.DEBUG,
DebugNodeRequest.createShared(DebugNodeMsgType.RETRIEVE_MY_ENR),
)
if connRes.isErr():
let msg = $connRes.error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
else:
let msg = $connRes.value
callback(RET_OK, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_OK

### End of exported procs
################################################################################
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import std/json
import chronicles, chronos, results
import chronicles, chronos, results, eth/p2p/discoveryv5/enr
import ../../../../waku/factory/waku, ../../../../waku/node/waku_node

type DebugNodeMsgType* = enum
RETRIEVE_LISTENING_ADDRESSES
RETRIEVE_MY_ENR

type DebugNodeRequest* = object
operation: DebugNodeMsgType
Expand All @@ -28,5 +29,7 @@ proc process*(
case self.operation
of RETRIEVE_LISTENING_ADDRESSES:
return ok($(%*waku.node.getMultiaddresses()))
of RETRIEVE_MY_ENR:
return ok($(%*waku.node.enr.toURI()))

return err("unsupported operation in DebugNodeRequest")

0 comments on commit 1ff9f1d

Please sign in to comment.