-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* cwaku_example: add discoveryv5-discovery bool option * libwaku: implement discovery capabilities * node_lifecycle_request.nim: better control of possible errors when parsing config
- Loading branch information
1 parent
5ee4cba
commit 7464684
Showing
7 changed files
with
231 additions
and
13 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 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
99 changes: 99 additions & 0 deletions
99
library/waku_thread/inter_thread_communication/requests/discovery_request.nim
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,99 @@ | ||
import std/[json, sequtils] | ||
import chronos, stew/results, libp2p/multiaddress | ||
import | ||
../../../../waku/factory/waku, | ||
../../../../waku/discovery/waku_dnsdisc, | ||
../../../../waku/discovery/waku_discv5, | ||
../../../../waku/waku_core/peers, | ||
../../../alloc | ||
|
||
type DiscoveryMsgType* = enum | ||
GET_BOOTSTRAP_NODES | ||
UPDATE_DISCV5_BOOTSTRAP_NODES | ||
|
||
type DiscoveryRequest* = object | ||
operation: DiscoveryMsgType | ||
|
||
## used in GET_BOOTSTRAP_NODES | ||
enrTreeUrl: cstring | ||
nameDnsServer: cstring | ||
timeoutMs: cint | ||
|
||
## used in UPDATE_DISCV5_BOOTSTRAP_NODES | ||
nodes: cstring | ||
|
||
proc createShared( | ||
T: type DiscoveryRequest, | ||
op: DiscoveryMsgType, | ||
enrTreeUrl: cstring, | ||
nameDnsServer: cstring, | ||
timeoutMs: cint, | ||
nodes: cstring, | ||
): ptr type T = | ||
var ret = createShared(T) | ||
ret[].operation = op | ||
ret[].enrTreeUrl = enrTreeUrl.alloc() | ||
ret[].nameDnsServer = nameDnsServer.alloc() | ||
ret[].timeoutMs = timeoutMs | ||
ret[].nodes = nodes.alloc() | ||
return ret | ||
|
||
proc createRetrieveBootstrapNodesRequest*( | ||
T: type DiscoveryRequest, | ||
op: DiscoveryMsgType, | ||
enrTreeUrl: cstring, | ||
nameDnsServer: cstring, | ||
timeoutMs: cint, | ||
): ptr type T = | ||
return T.createShared(op, enrTreeUrl, nameDnsServer, timeoutMs, "") | ||
|
||
proc createUpdateBootstrapNodesRequest*( | ||
T: type DiscoveryRequest, op: DiscoveryMsgType, nodes: cstring | ||
): ptr type T = | ||
return T.createShared(op, "", "", 0, nodes) | ||
|
||
proc destroyShared(self: ptr DiscoveryRequest) = | ||
deallocShared(self[].enrTreeUrl) | ||
deallocShared(self[].nameDnsServer) | ||
deallocShared(self) | ||
|
||
proc retrieveBootstrapNodes( | ||
enrTreeUrl: string, ipDnsServer: string | ||
): Result[seq[string], string] = | ||
let dnsNameServers = @[parseIpAddress(ipDnsServer)] | ||
let discoveredPeers: seq[RemotePeerInfo] = retrieveDynamicBootstrapNodes( | ||
true, enrTreeUrl, dnsNameServers | ||
).valueOr: | ||
return err("failed discovering peers from DNS: " & $error) | ||
|
||
var multiAddresses = newSeq[string]() | ||
|
||
for discPeer in discoveredPeers: | ||
for address in discPeer.addrs: | ||
multiAddresses.add($address & "/" & $discPeer) | ||
|
||
return ok(multiAddresses) | ||
|
||
proc updateDiscv5BootstrapNodes(nodes: string, waku: ptr Waku): Result[void, string] = | ||
waku.wakuDiscv5.updateBootstrapRecords(nodes).isOkOr: | ||
return err("error in updateDiscv5BootstrapNodes: " & $error) | ||
return ok() | ||
|
||
proc process*( | ||
self: ptr DiscoveryRequest, waku: ptr Waku | ||
): Future[Result[string, string]] {.async.} = | ||
defer: | ||
destroyShared(self) | ||
|
||
case self.operation | ||
of GET_BOOTSTRAP_NODES: | ||
let nodes = retrieveBootstrapNodes($self[].enrTreeUrl, $self[].nameDnsServer).valueOr: | ||
return err($error) | ||
|
||
return ok($(%*nodes)) | ||
of UPDATE_DISCV5_BOOTSTRAP_NODES: | ||
updateDiscv5BootstrapNodes($self[].nodes, waku).isOkOr: | ||
return err($error) | ||
return ok("discovery request processed correctly") | ||
|
||
return err("discovery request not handled") |
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
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