2021-03-26 06:52:01 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2020-05-29 10:03:29 +00:00
|
|
|
{.push raises: [Defect].}
|
2020-04-05 19:07:13 +00:00
|
|
|
|
2019-12-13 17:30:39 +00:00
|
|
|
import
|
2020-09-11 17:46:48 +00:00
|
|
|
std/[os, strutils],
|
|
|
|
chronicles, stew/shims/net, stew/results, bearssl,
|
|
|
|
eth/keys, eth/p2p/discoveryv5/[enr, protocol, node],
|
2021-03-05 13:12:00 +00:00
|
|
|
../conf
|
2019-12-13 17:30:39 +00:00
|
|
|
|
2020-10-27 09:00:57 +00:00
|
|
|
export protocol, keys
|
|
|
|
|
2019-12-13 17:30:39 +00:00
|
|
|
type
|
|
|
|
Eth2DiscoveryProtocol* = protocol.Protocol
|
|
|
|
Eth2DiscoveryId* = NodeId
|
|
|
|
|
|
|
|
export
|
2021-01-14 07:58:13 +00:00
|
|
|
Eth2DiscoveryProtocol, open, start, close, closeWait, queryRandom,
|
2020-12-09 09:13:51 +00:00
|
|
|
updateRecord, results
|
2019-12-13 17:30:39 +00:00
|
|
|
|
2020-05-29 10:03:29 +00:00
|
|
|
proc parseBootstrapAddress*(address: TaintedString):
|
|
|
|
Result[enr.Record, cstring] =
|
2020-02-05 20:40:14 +00:00
|
|
|
logScope:
|
|
|
|
address = string(address)
|
|
|
|
|
2022-02-25 08:22:44 +00:00
|
|
|
let lowerCaseAddress = toLowerAscii(string address)
|
|
|
|
if lowerCaseAddress.startsWith("enr:"):
|
|
|
|
var enrRec: enr.Record
|
|
|
|
if enrRec.fromURI(string address):
|
|
|
|
return ok enrRec
|
|
|
|
return err "Invalid ENR bootstrap record"
|
|
|
|
elif lowerCaseAddress.startsWith("enode:"):
|
|
|
|
return err "ENode bootstrap addresses are not supported"
|
2020-02-05 20:40:14 +00:00
|
|
|
else:
|
2022-02-25 08:22:44 +00:00
|
|
|
return err "Ignoring unrecognized bootstrap address type"
|
2020-02-05 20:40:14 +00:00
|
|
|
|
2020-10-29 11:09:03 +00:00
|
|
|
iterator strippedLines(filename: string): string {.raises: [ref IOError].} =
|
|
|
|
for line in lines(filename):
|
|
|
|
let stripped = strip(line)
|
|
|
|
if stripped.startsWith('#'): # Comments
|
|
|
|
continue
|
|
|
|
|
|
|
|
if stripped.len > 0:
|
|
|
|
yield stripped
|
|
|
|
|
2020-02-12 13:41:07 +00:00
|
|
|
proc addBootstrapNode*(bootstrapAddr: string,
|
2020-09-11 17:46:48 +00:00
|
|
|
bootstrapEnrs: var seq[enr.Record]) =
|
2020-10-27 07:44:46 +00:00
|
|
|
# Ignore empty lines or lines starting with #
|
|
|
|
if bootstrapAddr.len == 0 or bootstrapAddr[0] == '#':
|
|
|
|
return
|
|
|
|
|
2020-02-19 09:59:38 +00:00
|
|
|
let enrRes = parseBootstrapAddress(bootstrapAddr)
|
|
|
|
if enrRes.isOk:
|
2020-05-29 10:03:29 +00:00
|
|
|
bootstrapEnrs.add enrRes.value
|
2020-02-05 20:40:14 +00:00
|
|
|
else:
|
|
|
|
warn "Ignoring invalid bootstrap address",
|
2020-02-19 09:59:38 +00:00
|
|
|
bootstrapAddr, reason = enrRes.error
|
2020-02-05 20:40:14 +00:00
|
|
|
|
2020-02-12 13:41:07 +00:00
|
|
|
proc loadBootstrapFile*(bootstrapFile: string,
|
2020-09-11 17:46:48 +00:00
|
|
|
bootstrapEnrs: var seq[enr.Record]) =
|
2020-02-05 20:40:14 +00:00
|
|
|
if bootstrapFile.len == 0: return
|
|
|
|
let ext = splitFile(bootstrapFile).ext
|
2020-06-19 17:42:28 +00:00
|
|
|
if cmpIgnoreCase(ext, ".txt") == 0 or cmpIgnoreCase(ext, ".enr") == 0 :
|
2020-05-29 10:03:29 +00:00
|
|
|
try:
|
2020-10-29 11:09:03 +00:00
|
|
|
for ln in strippedLines(bootstrapFile):
|
2020-09-11 17:46:48 +00:00
|
|
|
addBootstrapNode(ln, bootstrapEnrs)
|
2020-05-29 10:03:29 +00:00
|
|
|
except IOError as e:
|
|
|
|
error "Could not read bootstrap file", msg = e.msg
|
|
|
|
quit 1
|
2020-02-05 20:40:14 +00:00
|
|
|
else:
|
|
|
|
error "Unknown bootstrap file format", ext
|
|
|
|
quit 1
|
|
|
|
|
2020-03-24 09:54:17 +00:00
|
|
|
proc new*(T: type Eth2DiscoveryProtocol,
|
2021-02-22 16:17:48 +00:00
|
|
|
config: BeaconNodeConf,
|
2021-02-16 20:35:10 +00:00
|
|
|
enrIp: Option[ValidIpAddress], enrTcpPort, enrUdpPort: Option[Port],
|
2020-06-22 19:40:19 +00:00
|
|
|
pk: PrivateKey,
|
2020-10-28 18:35:31 +00:00
|
|
|
enrFields: openArray[(string, seq[byte])], rng: ref BrHmacDrbgContext):
|
2021-03-26 06:52:01 +00:00
|
|
|
T =
|
2020-03-24 09:54:17 +00:00
|
|
|
# TODO
|
|
|
|
# Implement more configuration options:
|
|
|
|
# * for setting up a specific key
|
|
|
|
# * for using a persistent database
|
2020-05-29 10:03:29 +00:00
|
|
|
var bootstrapEnrs: seq[enr.Record]
|
2021-02-22 16:17:48 +00:00
|
|
|
for node in config.bootstrapNodes:
|
2020-09-11 17:46:48 +00:00
|
|
|
addBootstrapNode(node, bootstrapEnrs)
|
2021-02-22 16:17:48 +00:00
|
|
|
loadBootstrapFile(string config.bootstrapNodesFile, bootstrapEnrs)
|
2020-03-24 09:54:17 +00:00
|
|
|
|
2021-02-22 16:17:48 +00:00
|
|
|
let persistentBootstrapFile = config.dataDir / "bootstrap_nodes.txt"
|
2020-03-24 09:54:17 +00:00
|
|
|
if fileExists(persistentBootstrapFile):
|
2020-09-11 17:46:48 +00:00
|
|
|
loadBootstrapFile(persistentBootstrapFile, bootstrapEnrs)
|
2020-03-24 09:54:17 +00:00
|
|
|
|
2021-02-16 20:35:10 +00:00
|
|
|
newProtocol(pk, enrIp, enrTcpPort, enrUdpPort, enrFields, bootstrapEnrs,
|
|
|
|
bindPort = config.udpPort, bindIp = config.listenAddress,
|
|
|
|
enrAutoUpdate = config.enrAutoUpdate, rng = rng)
|