From 9cedbc0cc800eec7740998f63186ff7f0ee53ea8 Mon Sep 17 00:00:00 2001 From: kdeme Date: Thu, 7 Jan 2021 10:15:48 +0100 Subject: [PATCH] Move code into seedTable and populateTable proc --- eth/p2p/discoveryv5/dcli.nim | 13 ++++++++++- eth/p2p/discoveryv5/protocol.nim | 38 +++++++++++++++++++++++++------- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/eth/p2p/discoveryv5/dcli.nim b/eth/p2p/discoveryv5/dcli.nim index bd6d083..879ea64 100644 --- a/eth/p2p/discoveryv5/dcli.nim +++ b/eth/p2p/discoveryv5/dcli.nim @@ -22,6 +22,11 @@ type desc: "UDP listening port." name: "udp-port" .}: uint16 + listenAddress* {. + defaultValue: defaultListenAddress(config) + desc: "Listening address for the Discovery v5 traffic" + name: "listen-address" }: ValidIpAddress + bootnodes* {. desc: "ENR URI of node to bootstrap discovery with. Argument may be repeated." name: "bootnode" .}: seq[enr.Record] @@ -42,7 +47,7 @@ type name: "metrics" .}: bool metricsAddress* {. - defaultValue: ValidIpAddress.init("127.0.0.1") + defaultValue: defaultAdminListenAddress(config) desc: "Listening address of the metrics server." name: "metrics-address" .}: ValidIpAddress @@ -78,6 +83,12 @@ type desc: "ENR URI of the node to send a talkreq message" name: "node" .}: Node +func defaultListenAddress*(conf: DiscoveryConf): ValidIpAddress = + (static ValidIpAddress.init("0.0.0.0")) + +func defaultAdminListenAddress*(conf: DiscoveryConf): ValidIpAddress = + (static ValidIpAddress.init("127.0.0.1")) + proc parseCmdArg*(T: type enr.Record, p: TaintedString): T = if not fromURI(result, p): raise newException(ConfigurationError, "Invalid ENR") diff --git a/eth/p2p/discoveryv5/protocol.nim b/eth/p2p/discoveryv5/protocol.nim index 0295561..580b998 100644 --- a/eth/p2p/discoveryv5/protocol.nim +++ b/eth/p2p/discoveryv5/protocol.nim @@ -102,6 +102,7 @@ const ## refresh the routing table. revalidateMax = 10000 ## Revalidation of a peer is done between 0 and this ## value in milliseconds + initialLookups = 1 ## Amount of lookups done when populating the routing table handshakeTimeout* = 2.seconds ## timeout for the reply on the ## whoareyou message responseTimeout* = 4.seconds ## timeout for the response of a request-response @@ -818,6 +819,32 @@ proc resolve*(d: Protocol, id: NodeId): Future[Option[Node]] return node +proc seedTable*(d: Protocol) = + ## Seed the table with known nodes. + for record in d.bootstrapRecords: + if d.addNode(record): + debug "Added bootstrap node", uri = toURI(record) + else: + debug "Bootstrap node could not be added", uri = toURI(record) + + # TODO: + # Persistent stored nodes could be added to seed from here + # See: https://github.com/status-im/nim-eth/issues/189 + +proc populateTable*(d: Protocol) {.async, raises: [Exception, Defect].} = + ## Do a set of initial lookups to quickly populate the table. + # start with a self target query (neighbour nodes) + let selfQuery = await d.query(d.localNode.id) + trace "Discovered nodes in self target query", nodes = selfQuery.len + + # `initialLookups` random queries + for i in 0.. (d.lastLookup + refreshInterval): @@ -915,11 +941,7 @@ proc open*(d: Protocol) {.raises: [Exception, Defect].} = # object of Exception. In Nim devel this got changed to CatchableError. d.transp = newDatagramTransport(processClient, udata = d, local = ta) - for record in d.bootstrapRecords: - if d.addNode(record): - debug "Added bootstrap node", uri = toURI(record) - else: - debug "Bootstrap node could not be added", uri = toURI(record) + d.seedTable() proc start*(d: Protocol) {.raises: [Exception, Defect].} = d.refreshLoop = refreshLoop(d)