nwaku/waku/waku_api/handlers.nim
Ivan FB fd6a71cdd7
chore: Bump dependencies for v0.31.0 (#2885)
* bump_dependencies.md: add nim-results dependency
* change imports stew/results to results
* switching to Nim 2.0.8
* waku.nimble: reflect the requirement nim 1.6.0 to 2.0.8
  Adding --mm:refc as nim 2.0 enables a new garbage collector that we're
  not yet ready to support
* adapt waku code to Nim 2.0
* gcsafe adaptations because Nim 2.0 is more strict
2024-07-09 13:14:28 +02:00

38 lines
1.1 KiB
Nim

{.push raises: [].}
import chronos, std/[options, sequtils], results
import ../discovery/waku_discv5, ../waku_relay, ../waku_core, ./message_cache
### Discovery
type DiscoveryHandler* =
proc(): Future[Result[Option[RemotePeerInfo], string]] {.async, closure.}
proc defaultDiscoveryHandler*(
discv5: WakuDiscoveryV5, cap: Capabilities
): DiscoveryHandler =
proc(): Future[Result[Option[RemotePeerInfo], string]] {.async, closure.} =
#Discv5 is already filtering peers by shards no need to pass a predicate.
let findPeers = discv5.findRandomPeers()
if not await findPeers.withTimeout(60.seconds):
return err("discovery process timed out!")
var peers = findPeers.read()
peers.keepItIf(it.supportsCapability(cap))
if peers.len == 0:
return ok(none(RemotePeerInfo))
let remotePeerInfo = peers[0].toRemotePeerInfo().valueOr:
return err($error)
return ok(some(remotePeerInfo))
### Message Cache
proc messageCacheHandler*(cache: MessageCache): WakuRelayHandler =
return proc(pubsubTopic: string, msg: WakuMessage): Future[void] {.async, closure.} =
cache.addMessage(pubsubTopic, msg)