-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: discv5 re-org clean-up #1823
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -160,17 +160,18 @@ proc populateInfoFromIp(allPeersRef: CustomPeersTableRef, | |||||||||
# crawls the network discovering peers and trying to connect to them | ||||||||||
# metrics are processed and exposed | ||||||||||
proc crawlNetwork(node: WakuNode, | ||||||||||
wakuDiscv5: WakuDiscoveryV5, | ||||||||||
restClient: RestClientRef, | ||||||||||
conf: NetworkMonitorConf, | ||||||||||
allPeersRef: CustomPeersTableRef) {.async.} = | ||||||||||
|
||||||||||
let crawlInterval = conf.refreshInterval * 1000 | ||||||||||
while true: | ||||||||||
# discover new random nodes | ||||||||||
let discoveredNodes = await node.wakuDiscv5.protocol.queryRandom() | ||||||||||
let discoveredNodes = await wakuDiscv5.protocol.queryRandom() | ||||||||||
|
||||||||||
# nodes are nested into bucket, flat it | ||||||||||
let flatNodes = node.wakuDiscv5.protocol.routingTable.buckets.mapIt(it.nodes).flatten() | ||||||||||
let flatNodes = wakuDiscv5.protocol.routingTable.buckets.mapIt(it.nodes).flatten() | ||||||||||
|
||||||||||
# populate metrics related to capabilities as advertised by the ENR (see waku field) | ||||||||||
setDiscoveredPeersCapabilities(flatNodes) | ||||||||||
|
@@ -242,44 +243,82 @@ proc getBootstrapFromDiscDns(conf: NetworkMonitorConf): Result[seq[enr.Record], | |||||||||
except CatchableError: | ||||||||||
error("failed discovering peers from DNS") | ||||||||||
|
||||||||||
proc initAndStartNode(conf: NetworkMonitorConf): Result[WakuNode, string] = | ||||||||||
proc initAndStartApp(conf: NetworkMonitorConf): Result[(WakuNode, WakuDiscoveryV5), string] = | ||||||||||
let bindIp = try: | ||||||||||
ValidIpAddress.init("0.0.0.0") | ||||||||||
except CatchableError: | ||||||||||
return err("could not start node: " & getCurrentExceptionMsg()) | ||||||||||
|
||||||||||
let extIp = try: | ||||||||||
ValidIpAddress.init("127.0.0.1") | ||||||||||
except CatchableError: | ||||||||||
return err("could not start node: " & getCurrentExceptionMsg()) | ||||||||||
|
||||||||||
let | ||||||||||
# some hardcoded parameters | ||||||||||
rng = keys.newRng() | ||||||||||
nodeKey = crypto.PrivateKey.random(Secp256k1, rng[])[] | ||||||||||
key = crypto.PrivateKey.random(Secp256k1, rng[])[] | ||||||||||
nodeTcpPort = Port(60000) | ||||||||||
nodeUdpPort = Port(9000) | ||||||||||
flags = CapabilitiesBitfield.init(lightpush = false, filter = false, store = false, relay = true) | ||||||||||
|
||||||||||
try: | ||||||||||
let | ||||||||||
bindIp = ValidIpAddress.init("0.0.0.0") | ||||||||||
extIp = ValidIpAddress.init("127.0.0.1") | ||||||||||
var builder = EnrBuilder.init(key) | ||||||||||
|
||||||||||
var builder = WakuNodeBuilder.init() | ||||||||||
builder.withNodeKey(nodeKey) | ||||||||||
? builder.withNetworkConfigurationDetails(bindIp, nodeTcpPort) | ||||||||||
let node = ? builder.build() | ||||||||||
builder.withIpAddressAndPorts( | ||||||||||
ipAddr = some(extIp), | ||||||||||
tcpPort = some(nodeTcpPort), | ||||||||||
udpPort = some(nodeUdpPort), | ||||||||||
) | ||||||||||
builder.withWakuCapabilities(flags) | ||||||||||
#builder.withMultiaddrs(???) | ||||||||||
|
||||||||||
var discv5BootstrapEnrsRes = getBootstrapFromDiscDns(conf) | ||||||||||
if not discv5BootstrapEnrsRes.isOk(): | ||||||||||
error("failed discovering peers from DNS") | ||||||||||
var discv5BootstrapEnrs = discv5BootstrapEnrsRes.get() | ||||||||||
let recordRes = builder.build() | ||||||||||
let record = | ||||||||||
if recordRes.isErr(): | ||||||||||
return err("cannot build record: " & $recordRes.error) | ||||||||||
else: recordRes.get() | ||||||||||
|
||||||||||
# parse enrURIs from the configuration and add the resulting ENRs to the discv5BootstrapEnrs seq | ||||||||||
for enrUri in conf.bootstrapNodes: | ||||||||||
addBootstrapNode(enrUri, discv5BootstrapEnrs) | ||||||||||
var nodeBuilder = WakuNodeBuilder.init() | ||||||||||
|
||||||||||
nodeBuilder.withNodeKey(key) | ||||||||||
nodeBuilder.withRecord(record) | ||||||||||
let res = nodeBuilder.withNetworkConfigurationDetails(bindIp, nodeTcpPort) | ||||||||||
if res.isErr(): | ||||||||||
return err("node building error" & $ res.error) | ||||||||||
|
||||||||||
# mount discv5 | ||||||||||
node.wakuDiscv5 = WakuDiscoveryV5.new( | ||||||||||
some(extIp), some(nodeTcpPort), some(nodeUdpPort), | ||||||||||
bindIp, nodeUdpPort, discv5BootstrapEnrs, false, | ||||||||||
keys.PrivateKey(nodeKey.skkey), flags, @[], node.rng, @[]) | ||||||||||
let nodeRes = nodeBuilder.build() | ||||||||||
let node = | ||||||||||
if nodeRes.isErr(): | ||||||||||
return err("node building error" & $ res.error) | ||||||||||
else: nodeRes.get() | ||||||||||
|
||||||||||
node.wakuDiscv5.protocol.open() | ||||||||||
return ok(node) | ||||||||||
var discv5BootstrapEnrsRes = getBootstrapFromDiscDns(conf) | ||||||||||
if not discv5BootstrapEnrsRes.isOk(): | ||||||||||
error("failed discovering peers from DNS") | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
var discv5BootstrapEnrs = discv5BootstrapEnrsRes.get() | ||||||||||
|
||||||||||
# parse enrURIs from the configuration and add the resulting ENRs to the discv5BootstrapEnrs seq | ||||||||||
for enrUri in conf.bootstrapNodes: | ||||||||||
addBootstrapNode(enrUri, discv5BootstrapEnrs) | ||||||||||
|
||||||||||
# discv5 | ||||||||||
let discv5Conf = WakuDiscoveryV5Config( | ||||||||||
discv5Config: none(DiscoveryConfig), | ||||||||||
address: bindIp, | ||||||||||
port: nodeUdpPort, | ||||||||||
privateKey: keys.PrivateKey(key.skkey), | ||||||||||
bootstrapRecords: discv5BootstrapEnrs, | ||||||||||
autoupdateRecord: false | ||||||||||
) | ||||||||||
|
||||||||||
let wakuDiscv5 = WakuDiscoveryV5.new(node.rng, discv5Conf, some(record)) | ||||||||||
|
||||||||||
try: | ||||||||||
wakuDiscv5.protocol.open() | ||||||||||
except CatchableError: | ||||||||||
error("could not start node") | ||||||||||
return err("could not start node: " & getCurrentExceptionMsg()) | ||||||||||
|
||||||||||
ok((node, wakuDiscv5)) | ||||||||||
|
||||||||||
proc startRestApiServer(conf: NetworkMonitorConf, | ||||||||||
allPeersInfo: CustomPeersTableRef, | ||||||||||
|
@@ -366,12 +405,12 @@ when isMainModule: | |||||||||
let restClient = clientRest.get() | ||||||||||
|
||||||||||
# start waku node | ||||||||||
let nodeRes = initAndStartNode(conf) | ||||||||||
let nodeRes = initAndStartApp(conf) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we change this we'd need to change the previous comment and the next error message There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's exactly the kind of errors I wish the tooling would catch... :( |
||||||||||
if nodeRes.isErr(): | ||||||||||
error "could not start node" | ||||||||||
quit 1 | ||||||||||
|
||||||||||
let node = nodeRes.get() | ||||||||||
let (node, discv5) = nodeRes.get() | ||||||||||
|
||||||||||
waitFor node.mountRelay() | ||||||||||
|
||||||||||
|
@@ -380,6 +419,6 @@ when isMainModule: | |||||||||
|
||||||||||
# spawn the routine that crawls the network | ||||||||||
# TODO: split into 3 routines (discovery, connections, ip2location) | ||||||||||
asyncSpawn crawlNetwork(node, restClient, conf, allPeersInfo) | ||||||||||
asyncSpawn crawlNetwork(node, discv5, restClient, conf, allPeersInfo) | ||||||||||
|
||||||||||
runForever() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big deal but I'd delete this comment