-
Notifications
You must be signed in to change notification settings - Fork 56
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
feat(cbindings): first commit - waku relay (#1632) #1714
Changes from 2 commits
55c7b38
8ef3002
84ebb32
b21b52d
43557e0
c89b6c4
c2760a4
2c23e65
b9eae6e
13b1368
9eab90e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -37,6 +37,7 @@ import | |||
../../waku/v2/waku_enr, | ||||
../../waku/v2/waku_discv5, | ||||
../../waku/v2/waku_peer_exchange, | ||||
../../waku/v2/waku_relay/protocol, | ||||
../../waku/v2/waku_store, | ||||
../../waku/v2/waku_lightpush, | ||||
../../waku/v2/waku_filter, | ||||
|
@@ -66,7 +67,8 @@ logScope: | |||
const git_version* {.strdefine.} = "n/a" | ||||
|
||||
type | ||||
App* = object | ||||
|
||||
App* = ref object | ||||
version: string | ||||
conf: WakuNodeConf | ||||
|
||||
|
@@ -94,7 +96,16 @@ func version*(app: App): string = | |||
|
||||
## Initialisation | ||||
|
||||
proc init*(T: type App, rng: ref HmacDrbgContext, conf: WakuNodeConf): T = | ||||
proc new*(T: type App, | ||||
rng: ref HmacDrbgContext = nil, | ||||
conf: WakuNodeConf = WakuNodeConf( | ||||
listenAddress: ValidIpAddress.init("127.0.0.1"), | ||||
rpcAddress: ValidIpAddress.init("127.0.0.1"), | ||||
restAddress: ValidIpAddress.init("127.0.0.1"), | ||||
metricsServerAddress: ValidIpAddress.init("127.0.0.1"), | ||||
nat: "any", | ||||
maxConnections: 50, | ||||
)): T = | ||||
App(version: git_version, conf: conf, rng: rng, node: nil) | ||||
|
||||
|
||||
|
@@ -125,7 +136,6 @@ proc setupDatabaseConnection(dbUrl: string): AppResult[Option[SqliteDatabase]] = | |||
|
||||
ok(some(connRes.value)) | ||||
|
||||
|
||||
## Peer persistence | ||||
|
||||
const PeerPersistenceDbUrl = "sqlite://peers.db" | ||||
|
@@ -547,6 +557,7 @@ proc setupProtocols(node: WakuNode, conf: WakuNodeConf, | |||
peerExchangeHandler = some(handlePeerExchange) | ||||
|
||||
if conf.relay: | ||||
|
||||
let pubsubTopics = conf.topics.split(" ") | ||||
try: | ||||
await mountRelay(node, pubsubTopics, peerExchangeHandler = peerExchangeHandler) | ||||
|
@@ -726,6 +737,20 @@ proc startNode*(app: App): Future[AppResult[void]] {.async.} = | |||
app.dynamicBootstrapNodes | ||||
) | ||||
|
||||
proc subscribeCallbackToTopic*(app: App, pubSubTopic: cstring, | ||||
callback: PubsubRawHandler) {.gcsafe.} = | ||||
app.node.wakuRelay.subscribe(PubsubTopic($pubSubTopic), callback) | ||||
|
||||
proc unsubscribeCallbackFromTopic*(app: App, pubSubTopic: cstring, | ||||
callback: PubsubRawHandler) {.gcsafe.} = | ||||
app.node.wakuRelay.unsubscribe(PubsubTopic($pubSubTopic), callback) | ||||
|
||||
proc unsubscribeAllCallbackFromTopic*(app: App, pubSubTopic: cstring) {.gcsafe.} = | ||||
app.node.wakuRelay.unsubscribeAll(PubsubTopic($pubSubTopic)) | ||||
|
||||
proc publishMessage*(app: App, pubSubTopic: cstring, message: WakuMessage): Future[int] {.gcsafe, async.} = | ||||
# Returns the number of peers connected to the given pubSubTopic. | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
return await app.node.wakuRelay.publish(PubsubTopic($pubSubTopic), message) | ||||
|
||||
## Monitoring and external interfaces | ||||
|
||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ else: | |
|
||
import | ||
std/[options, strutils, os], | ||
stew/results, | ||
stew/shims/net as stewNet, | ||
chronicles, | ||
chronos, | ||
|
@@ -13,30 +14,32 @@ import | |
system/ansi_c, | ||
libp2p/crypto/crypto | ||
import | ||
../../waku/v2/waku_core/message/message, | ||
../../waku/common/logging, | ||
./config, | ||
./app | ||
|
||
logScope: | ||
topics = "wakunode main" | ||
|
||
const wakuNode2VersionString* = "version / git commit hash: " & git_version | ||
|
||
{.pop.} # @TODO confutils.nim(775, 17) Error: can raise an unlisted exception: ref IOError | ||
when isMainModule: | ||
## Node setup happens in 6 phases: | ||
## 1. Set up storage | ||
## 2. Initialize node | ||
## 3. Mount and initialize configured protocols | ||
## 4. Start node and mounted protocols | ||
## 5. Start monitoring tools and external interfaces | ||
## 6. Setup graceful shutdown hooks | ||
var wakunode2 {.threadvar.}: App | ||
var confRes:ConfResult[WakuNodeConf] | ||
|
||
const versionString = "version / git commit hash: " & app.git_version | ||
{.pop.} # @TODO confutils.nim(775, 17) Error: can raise an unlisted exception: ref IOError | ||
proc init*(configFilePath = "") = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
IMO using a configuration is better. A config file should be used for a binary, a configuration object for a library. @jm-clius Where can I find the nim api documentation? Don't know if Im understanding correctly, but, does it mean that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The application simply provides a way to configure and instantiate the node, so I guess more similar to "embedded within the application". The Nim API documentation is sparse and outdated (since it's only used internally), but in theory lives here: https://github.com/waku-org/nwaku/blob/master/docs/api/v2/node.md There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, it's very outdated, in fact. 😟 @Ivansete-status perhaps a good idea that we clean the Nim API doc up at some point during the development of the C bindings, since we're likely to expand/change it in any case. |
||
let rng = crypto.newRng() | ||
|
||
let confRes = WakuNodeConf.load(version=versionString) | ||
if confRes.isErr(): | ||
error "failure while loading the configuration", error=confRes.error | ||
try: | ||
confRes = WakuNodeConf.load(version=wakuNode2VersionString, | ||
configFile=configFilePath) | ||
if confRes.isErr(): | ||
error "failure while loading the configuration", error=confRes.error | ||
quit(QuitFailure) | ||
|
||
except ValueError: | ||
error "Exception loading the configuration", error=getCurrentExceptionMsg() | ||
quit(QuitFailure) | ||
|
||
let conf = confRes.get() | ||
|
@@ -50,8 +53,7 @@ when isMainModule: | |
logging.setupLogLevel(conf.logLevel) | ||
logging.setupLogFormat(conf.logFormat, color) | ||
|
||
|
||
var wakunode2 = App.init(rng, conf) | ||
wakunode2 = App.new(rng, conf) | ||
|
||
############## | ||
# Node setup # | ||
|
@@ -92,20 +94,19 @@ when isMainModule: | |
error "4/7 Mounting protocols failed", error=res5.error | ||
quit(QuitFailure) | ||
|
||
proc startNode*() = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quite confused about this. Does this split the application's setup into an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exaclty, that is a simple split. However that might not be needed, and I will likely revert it back, when I wrap the |
||
debug "5/7 Starting node and mounted protocols" | ||
|
||
let res6 = waitFor wakunode2.startNode() | ||
if res6.isErr(): | ||
error "5/7 Starting node and protocols failed", error=res6.error | ||
quit(QuitFailure) | ||
|
||
debug "6/7 Starting monitoring and external interfaces" | ||
|
||
let res7 = wakunode2.setupMonitoringAndExternalInterfaces() | ||
if res7.isErr(): | ||
error "6/7 Starting monitoring and external interfaces failed", error=res7.error | ||
quit(QuitFailure) | ||
|
||
debug "7/7 Setting up shutdown hooks" | ||
## Setup shutdown hooks for this process. | ||
## Stop node gracefully on shutdown. | ||
|
@@ -149,3 +150,30 @@ when isMainModule: | |
info "Node setup complete" | ||
|
||
runForever() | ||
|
||
proc subscribeCallbackToTopic*(pubSubTopic: cstring, | ||
callback: proc(pubsubTopic: string, data: seq[byte]): Future[void] {.gcsafe, raises: [Defect].}) = | ||
wakunode2.subscribeCallbackToTopic(pubSubTopic, callback) | ||
|
||
proc unsubscribeCallbackFromTopic*(pubSubTopic: cstring, | ||
callback: proc(pubsubTopic: string, data: seq[byte]): Future[void] {.gcsafe, raises: [Defect].}) = | ||
wakunode2.unsubscribeCallbackFromTopic(pubSubTopic, callback) | ||
|
||
proc unsubscribeAllCallbacksFromTopic*(pubSubTopic: cstring) = | ||
wakunode2.unsubscribeAllCallbackFromTopic(pubSubTopic) | ||
|
||
proc publishMessage*(pubSubTopic: cstring, message: WakuMessage): Future[int] {.gcsafe, async.} = | ||
return await wakunode2.publishMessage(pubSubTopic, message) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The way I see it, these do not belong here. The C bindings should be a wrapper around the Nim API defined in |
||
|
||
|
||
when isMainModule: | ||
## Node setup happens in 6 phases: | ||
## 1. Set up storage | ||
## 2. Initialize node | ||
## 3. Mount and initialize configured protocols | ||
## 4. Start node and mounted protocols | ||
## 5. Start monitoring tools and external interfaces | ||
## 6. Setup graceful shutdown hooks | ||
|
||
init() | ||
startNode() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should move away from always having to specify
v2
for Waku. Waku v1 is deprecated, in a sense, and not used anywhere. We even want to remove Waku v1 code from this repo, so it becomes simpler if we start referring to Waku v2 simply as the Waku. In other words, I think this can belibwaku
(just replacing the name occupied by previous unused target).