Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: libwaku retrieve my enr and adapt golang example #2987

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 listenAddresses = C.GoStringN(C.getMyCharPtr(resp), C.int(C.getMyCharLen(resp)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a bit misleading to call it listenAddresses and not the ENR.
I remember that is different also in /debug/info API result.

return listenAddresses, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we change the variable name to enr or something like that? looks like we kept the same variable name after copy-pasting from WakuListenAddresses()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooh @NagyZoltanPeter added the same comment a few minutes before I did lol. Nice!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much guys ! I'll change that

}
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")
Loading