diff --git a/config.nims b/config.nims index 0f6052c9b..0a61a086d 100644 --- a/config.nims +++ b/config.nims @@ -90,6 +90,9 @@ if not defined(macosx) and not defined(android): nimStackTraceOverride switch("import", "libbacktrace") +# Shim to provide valueOr and withValue for Option[T] +switch("import", "waku/common/option_shim") + --define: nimOldCaseObjects # https://github.com/status-im/nim-confutils/issues/9 diff --git a/tests/test_wakunode.nim b/tests/test_wakunode.nim index a7f1084fb..39452890d 100644 --- a/tests/test_wakunode.nim +++ b/tests/test_wakunode.nim @@ -1,7 +1,7 @@ {.used.} import - std/[sequtils, strutils, net], + std/[options, sequtils, strutils, net], stew/byteutils, testutils/unittests, chronicles, diff --git a/tests/waku_relay/test_wakunode_relay.nim b/tests/waku_relay/test_wakunode_relay.nim index a687119bd..ebfeef22c 100644 --- a/tests/waku_relay/test_wakunode_relay.nim +++ b/tests/waku_relay/test_wakunode_relay.nim @@ -1,7 +1,7 @@ {.used.} import - std/[os, strutils, sequtils, sysrand, math], + std/[options, os, strutils, sequtils, sysrand, math], stew/byteutils, testutils/unittests, chronos, diff --git a/tests/waku_store/test_wakunode_store.nim b/tests/waku_store/test_wakunode_store.nim index fa73cd16d..b7732aa65 100644 --- a/tests/waku_store/test_wakunode_store.nim +++ b/tests/waku_store/test_wakunode_store.nim @@ -1,7 +1,7 @@ {.used.} import - std/sequtils, + std/[options, sequtils], testutils/unittests, chronicles, chronos, diff --git a/waku/common/option_shim.nim b/waku/common/option_shim.nim new file mode 100644 index 000000000..6827cd856 --- /dev/null +++ b/waku/common/option_shim.nim @@ -0,0 +1,26 @@ +# Shim to provide valueOr and withValue for Option[T] + +{.push raises: [].} + +import std/options + +template valueOr*[T](self: Option[T], def: untyped): T = + let s = self + if s.isSome(): + s.get() + else: + def + +template withValue*[T](self: Option[T], value, body: untyped) = + let s = self + if s.isSome(): + let value {.inject.} = s.get() + body + +template withValue*[T](self: Option[T], value, body, elseStmt: untyped) = + let s = self + if s.isSome(): + let value {.inject.} = s.get() + body + else: + elseStmt diff --git a/waku/discovery/waku_kademlia.nim b/waku/discovery/waku_kademlia.nim index 88076dbb2..26f987439 100644 --- a/waku/discovery/waku_kademlia.nim +++ b/waku/discovery/waku_kademlia.nim @@ -24,6 +24,7 @@ const DefaultKademliaDiscoveryInterval* = chronos.seconds(10) type WakuKademlia* = ref object protocol*: KademliaDiscovery peerManager: PeerManager + loopInterval: Duration walkIntervalFut: Future[void] proc toRemotePeerInfo(record: ExtendedPeerRecord): Option[RemotePeerInfo] = @@ -128,6 +129,7 @@ proc new*( peerManager: PeerManager, bootstrapNodes: seq[(PeerId, seq[MultiAddress])], providedServices: var seq[ServiceInfo], + loopInterval: Duration = DefaultKademliaDiscoveryInterval, ): T = if bootstrapNodes.len == 0: debug "creating kademlia discovery as seed node (no bootstrap nodes)" @@ -141,11 +143,11 @@ proc new*( services = providedServices, ) - return WakuKademlia(protocol: kademlia, peerManager: peerManager) + return WakuKademlia( + protocol: kademlia, peerManager: peerManager, loopInterval: loopInterval + ) -proc start*( - self: WakuKademlia, interval: Duration = DefaultKademliaDiscoveryInterval -) {.async.} = +proc start*(self: WakuKademlia) {.async.} = if self.protocol.started: warn "Starting waku kad twice" return @@ -154,7 +156,7 @@ proc start*( await self.protocol.start() - self.walkIntervalFut = self.runDiscoveryLoop(interval) + self.walkIntervalFut = self.runDiscoveryLoop(self.loopInterval) info "Waku Kademlia Started" diff --git a/waku/node/waku_node.nim b/waku/node/waku_node.nim index defbe404b..03b2f05fb 100644 --- a/waku/node/waku_node.nim +++ b/waku/node/waku_node.nim @@ -638,9 +638,6 @@ proc stop*(node: WakuNode) {.async.} = if not node.wakuRendezvousClient.isNil(): await node.wakuRendezvousClient.stopWait() - if not node.wakuKademlia.isNil(): - await node.wakuKademlia.stop() - node.started = false proc isReady*(node: WakuNode): Future[bool] {.async: (raises: [Exception]).} =