2024-06-28 10:34:57 +00:00
|
|
|
{.push raises: [].}
|
2023-10-27 19:43:54 +00:00
|
|
|
|
2024-07-09 11:14:28 +00:00
|
|
|
import chronos, std/[options, sequtils], results
|
2024-04-17 19:48:20 +00:00
|
|
|
import ../discovery/waku_discv5, ../waku_relay, ../waku_core, ./message_cache
|
2023-10-27 19:43:54 +00:00
|
|
|
|
|
|
|
### Discovery
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
type DiscoveryHandler* =
|
|
|
|
proc(): Future[Result[Option[RemotePeerInfo], string]] {.async, closure.}
|
2023-10-27 19:43:54 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc defaultDiscoveryHandler*(
|
|
|
|
discv5: WakuDiscoveryV5, cap: Capabilities
|
|
|
|
): DiscoveryHandler =
|
2023-10-27 19:43:54 +00:00
|
|
|
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()
|
2024-03-15 23:08:47 +00:00
|
|
|
|
2023-10-27 19:43:54 +00:00
|
|
|
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
|
|
|
|
|
2023-11-28 12:21:41 +00:00
|
|
|
proc messageCacheHandler*(cache: MessageCache): WakuRelayHandler =
|
2023-10-27 19:43:54 +00:00
|
|
|
return proc(pubsubTopic: string, msg: WakuMessage): Future[void] {.async, closure.} =
|
2024-03-15 23:08:47 +00:00
|
|
|
cache.addMessage(pubsubTopic, msg)
|