-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added defaults other apis Added timeout & missing call to disco Error Msgs
- Loading branch information
Showing
8 changed files
with
220 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
when (NimMajor, NimMinor) < (1, 4): | ||
{.push raises: [Defect].} | ||
else: | ||
{.push raises: [].} | ||
|
||
import | ||
chronos, | ||
chronicles, | ||
std/[options, sequtils], | ||
stew/results | ||
import | ||
../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[string]): WakuRelayHandler = | ||
return proc(pubsubTopic: string, msg: WakuMessage): Future[void] {.async, closure.} = | ||
cache.addMessage(PubSubTopic(pubsubTopic), msg) | ||
|
||
proc autoMessageCacheHandler*(cache: MessageCache[string]): WakuRelayHandler = | ||
return proc(pubsubTopic: string, msg: WakuMessage): Future[void] {.async, closure.} = | ||
if cache.isSubscribed(msg.contentTopic): | ||
cache.addMessage(msg.contentTopic, msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.