Skip to content

Commit

Permalink
libwaku: adapt to vendor bump
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivansete-status committed Dec 12, 2023
1 parent 71ebad8 commit 95b4e06
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 33 deletions.
2 changes: 1 addition & 1 deletion library/callback.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
type
WakuCallBack* = proc(callerRet: cint,
msg: ptr cchar,
len: csize_t) {.cdecl, gcsafe.}
len: csize_t) {.cdecl, gcsafe, raises: [].}
2 changes: 1 addition & 1 deletion library/events/json_error_event.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ proc new*(T: type JsonErrorEvent,
eventType: "error",
message: message)

method `$`*(jsonError: JsonErrorEvent): string =
method `$`*(jsonError: JsonErrorEvent): string {.raises: [].}=
$( %* jsonError )
2 changes: 1 addition & 1 deletion library/libwaku.nim
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const RET_MISSING_CALLBACK: cint = 2
var extEventCallback*: WakuCallBack = nil

proc relayEventCallback(pubsubTopic: PubsubTopic,
msg: WakuMessage): Future[void] {.async, gcsafe.} =
msg: WakuMessage): Future[void] {.async.} =
# Callback that hadles the Waku Relay events. i.e. messages or errors.
if not isNil(extEventCallback):
try:
Expand Down
51 changes: 38 additions & 13 deletions library/waku_thread/config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ proc parsePrivateKey(jsonNode: JsonNode,
return true

proc parseListenAddr(jsonNode: JsonNode,
listenAddr: var ValidIpAddress,
listenAddr: var IpAddress,
jsonResp: var JsonEvent): bool =

if not jsonNode.contains("host"):
Expand All @@ -51,7 +51,7 @@ proc parseListenAddr(jsonNode: JsonNode,
let host = jsonNode["host"].getStr()

try:
listenAddr = ValidIpAddress.init(host)
listenAddr = parseIpAddress(host)
except CatchableError:
let msg = "Invalid host IP address: " & getCurrentExceptionMsg()
jsonResp = JsonErrorEvent.new(msg)
Expand Down Expand Up @@ -175,7 +175,7 @@ proc parseConfig*(configNodeJson: string,
storeVacuum: var bool,
storeDbMigration: var bool,
storeMaxNumDbConnections: var int,
jsonResp: var JsonEvent): bool =
jsonResp: var JsonEvent): bool {.raises: [].} =

if configNodeJson.len == 0:
jsonResp = JsonErrorEvent.new("The configNodeJson is empty")
Expand All @@ -184,29 +184,42 @@ proc parseConfig*(configNodeJson: string,
var jsonNode: JsonNode
try:
jsonNode = parseJson(configNodeJson)
except JsonParsingError:
except Exception, IOError, JsonParsingError:
jsonResp = JsonErrorEvent.new("Exception: " & getCurrentExceptionMsg())
return false

# key
if not parsePrivateKey(jsonNode, privateKey, jsonResp):
try:
if not parsePrivateKey(jsonNode, privateKey, jsonResp):
return false
except Exception, KeyError:
jsonResp = JsonErrorEvent.new("Exception calling parsePrivateKey: " & getCurrentExceptionMsg())
return false

# listenAddr
var listenAddr = ValidIpAddress.init("127.0.0.1")
if not parseListenAddr(jsonNode, listenAddr, jsonResp):
var listenAddr: IpAddress
try:
listenAddr = parseIpAddress("127.0.0.1")
if not parseListenAddr(jsonNode, listenAddr, jsonResp):
return false
except Exception, ValueError:
jsonResp = JsonErrorEvent.new("Exception calling parseIpAddress: " & getCurrentExceptionMsg())
return false

# port
var port = 0
if not parsePort(jsonNode, port, jsonResp):
try:
if not parsePort(jsonNode, port, jsonResp):
return false
except Exception, ValueError:
jsonResp = JsonErrorEvent.new("Exception calling parsePort: " & getCurrentExceptionMsg())
return false

let natRes = setupNat("any", clientId,
Port(uint16(port)),
Port(uint16(port)))
if natRes.isErr():
jsonResp = JsonErrorEvent.new(fmt"failed to setup NAT: {$natRes.error}")
jsonResp = JsonErrorEvent.new("failed to setup NAT: " & $natRes.error)
return false

let (extIp, extTcpPort, _) = natRes.get()
Expand All @@ -217,15 +230,27 @@ proc parseConfig*(configNodeJson: string,
extTcpPort

# relay
if not parseRelay(jsonNode, relay, jsonResp):
try:
if not parseRelay(jsonNode, relay, jsonResp):
return false
except Exception, KeyError:
jsonResp = JsonErrorEvent.new("Exception calling parseRelay: " & getCurrentExceptionMsg())
return false

# topics
parseTopics(jsonNode, topics)
try:
parseTopics(jsonNode, topics)
except Exception, KeyError:
jsonResp = JsonErrorEvent.new("Exception calling parseTopics: " & getCurrentExceptionMsg())
return false

# store
if not parseStore(jsonNode, store, storeNode, storeRetentionPolicy, storeDbUrl,
storeVacuum, storeDbMigration, storeMaxNumDbConnections, jsonResp):
try:
if not parseStore(jsonNode, store, storeNode, storeRetentionPolicy, storeDbUrl,
storeVacuum, storeDbMigration, storeMaxNumDbConnections, jsonResp):
return false
except Exception, KeyError:
jsonResp = JsonErrorEvent.new("Exception calling parseStore: " & getCurrentExceptionMsg())
return false

let wakuFlags = CapabilitiesBitfield.init(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,8 @@ proc createNode(configJson: cstring):
Future[Result[WakuNode, string]] {.async.} =

var privateKey: PrivateKey
var netConfig = NetConfig.init(ValidIpAddress.init("127.0.0.1"),
var netConfig = NetConfig.init(parseIpAddress("127.0.0.1"),
Port(60000'u16)).value

## relay
var relay: bool
var topics = @[""]
Expand All @@ -125,20 +124,23 @@ proc createNode(configJson: cstring):

var jsonResp: JsonEvent

if not parseConfig($configJson,
privateKey,
netConfig,
relay,
topics,
store,
storeNode,
storeRetentionPolicy,
storeDbUrl,
storeVacuum,
storeDbMigration,
storeMaxNumDbConnections,
jsonResp):
return err($jsonResp)
try:
if not parseConfig($configJson,
privateKey,
netConfig,
relay,
topics,
store,
storeNode,
storeRetentionPolicy,
storeDbUrl,
storeVacuum,
storeDbMigration,
storeMaxNumDbConnections,
jsonResp):
return err($jsonResp)
except Exception:
return err("exception calling parseConfig: " & getCurrentExceptionMsg())

var enrBuilder = EnrBuilder.init(privateKey)

Expand Down Expand Up @@ -217,6 +219,9 @@ proc process*(self: ptr NodeLifecycleRequest,
await node[].start()

of STOP_NODE:
await node[].stop()
try:
await node[].stop()
except Exception:
return err("exception stopping node: " & getCurrentExceptionMsg())

return ok("")

0 comments on commit 95b4e06

Please sign in to comment.