Skip to content

Commit

Permalink
Bump chronos and fix exception tracking issues (#436)
Browse files Browse the repository at this point in the history
* Bump nim-chronos and fix exception tracking issues

* Bump other Nim submodules to latest

* Fix repeatMessage properly through proc type fix in nim-eth

Also add and use unittest2 through testutils to avoid extra
annotations.
  • Loading branch information
kdeme authored Mar 26, 2021
1 parent 5102576 commit d1c1a0c
Show file tree
Hide file tree
Showing 29 changed files with 152 additions and 140 deletions.
10 changes: 10 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,13 @@
path = vendor/rln
url = https://github.com/kilic/rln
branch = full-node
[submodule "vendor/nim-testutils"]
path = vendor/nim-testutils
url = https://github.com/status-im/nim-testutils.git
ignore = untracked
branch = master
[submodule "vendor/nim-unittest2"]
path = vendor/nim-unittest2
url = https://github.com/status-im/nim-unittest2.git
ignore = untracked
branch = master
180 changes: 92 additions & 88 deletions examples/v1/example.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,99 +10,103 @@ import

const clientId = "Waku example v1"

let
# Load the cli configuration from `config_example.nim`.
config = WakuNodeConf.load()
# Seed the rng.
rng = keys.newRng()
proc run(config: WakuNodeConf, rng: ref BrHmacDrbgContext) =
# Set up the address according to NAT information.
(ipExt, tcpPortExt, udpPortExt) = setupNat(config.nat, clientId,
let (ipExt, tcpPortExt, udpPortExt) = setupNat(config.nat, clientId,
Port(config.tcpPort + config.portsShift),
Port(config.udpPort + config.portsShift))
# TODO: EthereumNode should have a better split of binding address and
# external address. Also, can't have different ports as it stands now.
address = if ipExt.isNone():
Address(ip: parseIpAddress("0.0.0.0"),
tcpPort: Port(config.tcpPort + config.portsShift),
udpPort: Port(config.udpPort + config.portsShift))
else:
Address(ip: ipExt.get(),
tcpPort: Port(config.tcpPort + config.portsShift),
udpPort: Port(config.udpPort + config.portsShift))

# Create Ethereum Node
var node = newEthereumNode(config.nodekey, # Node identifier
address, # Address reachable for incoming requests
NetworkId(1), # Network Id, only applicable for ETH protocol
nil, # Database, not required for Waku
clientId, # Client id string
addAllCapabilities = false, # Disable default all RLPx capabilities
rng = rng)

node.addCapability Waku # Enable only the Waku protocol.

# Set up the Waku configuration.
let wakuConfig = WakuConfig(powRequirement: 0.002,
bloom: some(fullBloom()), # Full bloom filter
isLightNode: false, # Full node
maxMsgSize: waku_protocol.defaultMaxMsgSize,
topics: none(seq[waku_protocol.Topic]) # empty topic interest
)
node.configureWaku(wakuConfig)

# Optionally direct connect to a set of nodes.
if config.staticnodes.len > 0:
connectToNodes(node, config.staticnodes)

# Connect to the network, which will make the node start listening and/or
# connect to bootnodes, and/or start discovery.
# This will block until first connection is made, which in this case can only
# happen if we directly connect to nodes (step above) or if an incoming
# connection occurs, which is why we use a callback to exit on errors instead of
# using `await`.
# TODO: This looks a bit awkward and the API should perhaps be altered here.
let connectedFut = node.connectToNetwork(@[],
true, # Enable listening
false # Disable discovery (only discovery v4 is currently supported)
)
connectedFut.callback = proc(data: pointer) {.gcsafe.} =
{.gcsafe.}:
if connectedFut.failed:
fatal "connectToNetwork failed", msg = connectedFut.readError.msg
quit(1)

# Using a hardcoded symmetric key for encryption of the payload for the sake of
# simplicity.
var symKey: SymKey
symKey[31] = 1
# Asymmetric keypair to sign the payload.
let signKeyPair = KeyPair.random(rng[])

# Code to be executed on receival of a message on filter.
proc handler(msg: ReceivedMessage) =
if msg.decoded.src.isSome():
echo "Received message from ", $msg.decoded.src.get(), ": ",
string.fromBytes(msg.decoded.payload)

# Create and subscribe filter with above handler.
let
topic = [byte 0, 0, 0, 0]
filter = initFilter(symKey = some(symKey), topics = @[topic])
discard node.subscribeFilter(filter, handler)

# Repeat the posting of a message every 5 seconds.
proc repeatMessage(udata: pointer) {.gcsafe.} =
{.gcsafe.}:
# Post a waku message on the network, encrypted with provided symmetric key,
# signed with asymmetric key, on topic and with ttl of 30 seconds.
let posted = node.postMessage(
symKey = some(symKey), src = some(signKeyPair.seckey),
ttl = 30, topic = topic, payload = @[byte 0x48, 0x65, 0x6C, 0x6C, 0x6F])

if posted: echo "Posted message as ", $signKeyPair.pubkey
else: echo "Posting message failed."
let address = if ipExt.isNone():
Address(ip: parseIpAddress("0.0.0.0"),
tcpPort: Port(config.tcpPort + config.portsShift),
udpPort: Port(config.udpPort + config.portsShift))
else:
Address(ip: ipExt.get(),
tcpPort: Port(config.tcpPort + config.portsShift),
udpPort: Port(config.udpPort + config.portsShift))

# Create Ethereum Node
var node = newEthereumNode(config.nodekey, # Node identifier
address, # Address reachable for incoming requests
NetworkId(1), # Network Id, only applicable for ETH protocol
nil, # Database, not required for Waku
clientId, # Client id string
addAllCapabilities = false, # Disable default all RLPx capabilities
rng = rng)

node.addCapability Waku # Enable only the Waku protocol.

# Set up the Waku configuration.
let wakuConfig = WakuConfig(powRequirement: 0.002,
bloom: some(fullBloom()), # Full bloom filter
isLightNode: false, # Full node
maxMsgSize: waku_protocol.defaultMaxMsgSize,
topics: none(seq[waku_protocol.Topic]) # empty topic interest
)
node.configureWaku(wakuConfig)

# Optionally direct connect to a set of nodes.
if config.staticnodes.len > 0:
connectToNodes(node, config.staticnodes)

# Connect to the network, which will make the node start listening and/or
# connect to bootnodes, and/or start discovery.
# This will block until first connection is made, which in this case can only
# happen if we directly connect to nodes (step above) or if an incoming
# connection occurs, which is why we use a callback to exit on errors instead of
# using `await`.
# TODO: This looks a bit awkward and the API should perhaps be altered here.
let connectedFut = node.connectToNetwork(@[],
true, # Enable listening
false # Disable discovery (only discovery v4 is currently supported)
)
connectedFut.callback = proc(data: pointer) {.gcsafe.} =
{.gcsafe.}:
if connectedFut.failed:
fatal "connectToNetwork failed", msg = connectedFut.readError.msg
quit(1)

# Using a hardcoded symmetric key for encryption of the payload for the sake of
# simplicity.
var symKey: SymKey
symKey[31] = 1
# Asymmetric keypair to sign the payload.
let signKeyPair = KeyPair.random(rng[])

# Code to be executed on receival of a message on filter.
proc handler(msg: ReceivedMessage) =
if msg.decoded.src.isSome():
echo "Received message from ", $msg.decoded.src.get(), ": ",
string.fromBytes(msg.decoded.payload)

# Create and subscribe filter with above handler.
let
topic = [byte 0, 0, 0, 0]
filter = initFilter(symKey = some(symKey), topics = @[topic])
discard node.subscribeFilter(filter, handler)

# Repeat the posting of a message every 5 seconds.
# https://github.com/nim-lang/Nim/issues/17369
var repeatMessage: proc(udata: pointer) {.gcsafe, raises: [Defect].}
repeatMessage = proc(udata: pointer) =
{.gcsafe.}:
# Post a waku message on the network, encrypted with provided symmetric key,
# signed with asymmetric key, on topic and with ttl of 30 seconds.
let posted = node.postMessage(
symKey = some(symKey), src = some(signKeyPair.seckey),
ttl = 30, topic = topic, payload = @[byte 0x48, 0x65, 0x6C, 0x6C, 0x6F])

if posted: echo "Posted message as ", $signKeyPair.pubkey
else: echo "Posting message failed."

discard setTimer(Moment.fromNow(5.seconds), repeatMessage)
discard setTimer(Moment.fromNow(5.seconds), repeatMessage)
discard setTimer(Moment.fromNow(5.seconds), repeatMessage)

runForever()
runForever()

when isMainModule:
let
rng = keys.newRng()
conf = WakuNodeConf.load()
run(conf, rng)
14 changes: 1 addition & 13 deletions tests/test_helpers.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import
unittest, chronos, bearssl,
chronos, bearssl,
eth/[keys, p2p]

import libp2p/crypto/crypto
Expand All @@ -21,18 +21,6 @@ proc setupTestNode*(
for capability in capabilities:
result.addCapability capability

template asyncTest*(name, body: untyped) =
test name:
proc scenario {.async.} = body
waitFor scenario()

template procSuite*(name, body: untyped) =
proc suitePayload =
suite name:
body

suitePayload()

# Copied from here: https://github.com/status-im/nim-libp2p/blob/d522537b19a532bc4af94fcd146f779c1f23bad0/tests/helpers.nim#L28
type RngWrap = object
rng: ref BrHmacDrbgContext
Expand Down
4 changes: 2 additions & 2 deletions tests/v1/test_waku_bridge.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
{.used.}

import
std/[sequtils, unittest, tables],
chronos, eth/p2p, eth/p2p/peer_pool,
std/[sequtils, tables],
chronos, testutils/unittests, eth/p2p, eth/p2p/peer_pool,
eth/p2p/rlpx_protocols/whisper_protocol as whisper,
../../waku/v1/protocol/waku_protocol as waku,
../../waku/v1/protocol/waku_bridge,
Expand Down
4 changes: 2 additions & 2 deletions tests/v1/test_waku_connect.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
{.used.}

import
std/[sequtils, tables, unittest],
chronos, eth/[keys, p2p], eth/p2p/peer_pool,
std/[sequtils, tables],
chronos, testutils/unittests, eth/[keys, p2p], eth/p2p/peer_pool,
../../waku/v1/protocol/waku_protocol,
../test_helpers

Expand Down
4 changes: 2 additions & 2 deletions tests/v1/test_waku_mail.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{.used.}

import
std/[unittest, tables, sequtils, times],
chronos, eth/[p2p, async_utils], eth/p2p/peer_pool,
std/[tables, sequtils, times],
chronos, testutils/unittests, eth/[p2p, async_utils], eth/p2p/peer_pool,
../../waku/v1/protocol/[waku_protocol, waku_mail],
../test_helpers

Expand Down
4 changes: 2 additions & 2 deletions tests/v2/test_jsonrpc_waku.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{.used.}

import
std/[unittest, options, sets, tables, os, strutils, sequtils],
stew/shims/net as stewNet,
std/[options, sets, tables, os, strutils, sequtils],
testutils/unittests, stew/shims/net as stewNet,
json_rpc/[rpcserver, rpcclient],
eth/[keys, rlp], eth/common/eth_types,
libp2p/[standard_setup, switch, multiaddress],
Expand Down
4 changes: 2 additions & 2 deletions tests/v2/test_peer_manager.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{.used.}

import
std/[unittest, options, sets, tables, sequtils],
stew/shims/net as stewNet,
std/[options, sets, tables, sequtils],
testutils/unittests, stew/shims/net as stewNet,
json_rpc/[rpcserver, rpcclient],
eth/[keys, rlp], eth/common/eth_types,
libp2p/[standard_setup, switch, multiaddress],
Expand Down
3 changes: 2 additions & 1 deletion tests/v2/test_waku_bridge.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{.used.}

import
std/[unittest, strutils],
std/strutils,
testutils/unittests,
chronicles, chronos, stew/shims/net as stewNet, stew/byteutils,
libp2p/crypto/crypto,
libp2p/crypto/secp,
Expand Down
4 changes: 2 additions & 2 deletions tests/v2/test_waku_filter.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{.used.}

import
std/[unittest, options, tables, sets],
chronos, chronicles,
std/[options, tables, sets],
testutils/unittests, chronos, chronicles,
libp2p/switch,
libp2p/protobuf/minprotobuf,
libp2p/stream/[bufferstream, connection],
Expand Down
4 changes: 2 additions & 2 deletions tests/v2/test_waku_pagination.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{.used.}
import
std/[unittest,algorithm,options],
nimcrypto/sha2,
std/[algorithm, options],
testutils/unittests, nimcrypto/sha2,
../../waku/v2/protocol/waku_store/waku_store,
../test_helpers

Expand Down
2 changes: 1 addition & 1 deletion tests/v2/test_waku_payload.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{.used.}

import
std/unittest,
testutils/unittests,
../../waku/v2/protocol/waku_message,
../../waku/v2/node/waku_payload,
../test_helpers
Expand Down
9 changes: 3 additions & 6 deletions tests/v2/test_waku_rln_relay.nim
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
{.used.}

import
chronos, chronicles, options, stint, unittest,
web3,
std/options,
testutils/unittests, chronos, chronicles, stint, web3,
stew/byteutils, stew/shims/net as stewNet,
libp2p/crypto/crypto,
../../waku/v2/protocol/waku_rln_relay/[rln, waku_rln_relay_utils],
../../waku/v2/node/wakunode2,
../test_helpers,
test_utils



./test_utils

# the address of Ethereum client (ganache-cli for now)
# TODO this address in hardcoded in the code, we may need to take it as input from the user
Expand Down
4 changes: 2 additions & 2 deletions tests/v2/test_waku_store.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{.used.}

import
std/[unittest, options, tables, sets],
chronos, chronicles,
std/[options, tables, sets],
testutils/unittests, chronos, chronicles,
libp2p/switch,
libp2p/protobuf/minprotobuf,
libp2p/stream/[bufferstream, connection],
Expand Down
3 changes: 2 additions & 1 deletion tests/v2/test_waku_swap.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{.used.}

import
std/[unittest, options, tables, sets],
std/[options, tables, sets],
testutils/unittests,
chronos, chronicles, stew/shims/net as stewNet, stew/byteutils,
libp2p/switch,
libp2p/protobuf/minprotobuf,
Expand Down
2 changes: 1 addition & 1 deletion tests/v2/test_wakunode.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{.used.}

import
std/unittest,
testutils/unittests,
chronicles, chronos, stew/shims/net as stewNet, stew/byteutils,
libp2p/crypto/crypto,
libp2p/crypto/secp,
Expand Down
2 changes: 1 addition & 1 deletion vendor/nim-chronicles
2 changes: 1 addition & 1 deletion vendor/nim-chronos
2 changes: 1 addition & 1 deletion vendor/nim-http-utils
Submodule nim-http-utils updated 1 files
+51 −0 httputils.nim
2 changes: 1 addition & 1 deletion vendor/nim-libp2p
Loading

0 comments on commit d1c1a0c

Please sign in to comment.