2021-03-26 06:52:01 +00:00
|
|
|
# beacon_chain
|
2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2021-03-26 06:52:01 +00:00
|
|
|
# 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.
|
|
|
|
|
2023-01-20 14:14:37 +00:00
|
|
|
{.push raises: [].}
|
2020-04-05 19:07:13 +00:00
|
|
|
|
2019-12-13 17:30:39 +00:00
|
|
|
import
|
2024-01-13 09:54:24 +00:00
|
|
|
std/[algorithm, sequtils],
|
2024-06-11 02:34:56 +00:00
|
|
|
chronos, chronicles,
|
2024-01-13 09:54:24 +00:00
|
|
|
eth/p2p/discoveryv5/[enr, protocol, node, random2],
|
|
|
|
../spec/datatypes/altair,
|
|
|
|
../spec/eth2_ssz_serialization,
|
2022-05-31 10:45:37 +00:00
|
|
|
".."/[conf, conf_light_client]
|
2019-12-13 17:30:39 +00:00
|
|
|
|
2023-10-06 19:26:30 +00:00
|
|
|
from std/os import splitFile
|
|
|
|
from std/strutils import cmpIgnoreCase, split, startsWith, strip, toLowerAscii
|
|
|
|
|
2024-01-13 09:54:24 +00:00
|
|
|
export protocol, node
|
2020-10-27 09:00:57 +00:00
|
|
|
|
2019-12-13 17:30:39 +00:00
|
|
|
type
|
|
|
|
Eth2DiscoveryProtocol* = protocol.Protocol
|
|
|
|
Eth2DiscoveryId* = NodeId
|
|
|
|
|
2023-10-06 19:26:30 +00:00
|
|
|
func parseBootstrapAddress*(address: string):
|
2024-07-12 15:18:24 +00:00
|
|
|
Result[enr.Record, string] =
|
2024-05-22 11:56:37 +00:00
|
|
|
let lowerCaseAddress = toLowerAscii(address)
|
2022-02-25 08:22:44 +00:00
|
|
|
if lowerCaseAddress.startsWith("enr:"):
|
2024-07-12 15:18:24 +00:00
|
|
|
let res = enr.Record.fromURI(address)
|
|
|
|
if res.isOk():
|
|
|
|
return ok res.value
|
|
|
|
return err "Invalid bootstrap ENR: " & $res.error
|
2022-02-25 08:22:44 +00:00
|
|
|
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
|
|
|
|
|
2023-10-06 19:26:30 +00:00
|
|
|
# Ignore comments in
|
2024-05-30 09:28:16 +00:00
|
|
|
# https://github.com/eth-clients/mainnet/blob/main/metadata/bootstrap_nodes.txt
|
2023-10-06 19:26:30 +00:00
|
|
|
let enrRes = parseBootstrapAddress(bootstrapAddr.split(" # ")[0])
|
2020-02-19 09:59:38 +00:00
|
|
|
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,
|
2022-05-31 10:45:37 +00:00
|
|
|
config: BeaconNodeConf | LightClientConf,
|
2024-06-18 21:46:51 +00:00
|
|
|
enrIp: Opt[IpAddress], enrTcpPort, enrUdpPort: Opt[Port],
|
2020-06-22 19:40:19 +00:00
|
|
|
pk: PrivateKey,
|
2022-06-21 08:29:16 +00:00
|
|
|
enrFields: openArray[(string, seq[byte])], rng: ref HmacDrbgContext):
|
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
|
|
|
|
2022-05-31 10:45:37 +00:00
|
|
|
when config is BeaconNodeConf:
|
|
|
|
let persistentBootstrapFile = config.dataDir / "bootstrap_nodes.txt"
|
|
|
|
if fileExists(persistentBootstrapFile):
|
|
|
|
loadBootstrapFile(persistentBootstrapFile, bootstrapEnrs)
|
2020-03-24 09:54:17 +00:00
|
|
|
|
2024-05-01 19:29:39 +00:00
|
|
|
let listenAddress =
|
|
|
|
if config.listenAddress.isSome():
|
|
|
|
Opt.some(config.listenAddress.get())
|
|
|
|
else:
|
|
|
|
Opt.none(IpAddress)
|
|
|
|
|
2021-02-16 20:35:10 +00:00
|
|
|
newProtocol(pk, enrIp, enrTcpPort, enrUdpPort, enrFields, bootstrapEnrs,
|
2024-05-01 19:29:39 +00:00
|
|
|
bindPort = config.udpPort, bindIp = listenAddress,
|
2021-02-16 20:35:10 +00:00
|
|
|
enrAutoUpdate = config.enrAutoUpdate, rng = rng)
|
2024-01-13 09:54:24 +00:00
|
|
|
|
|
|
|
func isCompatibleForkId*(discoveryForkId: ENRForkID, peerForkId: ENRForkID): bool =
|
|
|
|
if discoveryForkId.fork_digest == peerForkId.fork_digest:
|
|
|
|
if discoveryForkId.next_fork_version < peerForkId.next_fork_version:
|
|
|
|
# Peer knows about a fork and we don't
|
|
|
|
true
|
|
|
|
elif discoveryForkId.next_fork_version == peerForkId.next_fork_version:
|
|
|
|
# We should have the same next_fork_epoch
|
|
|
|
discoveryForkId.next_fork_epoch == peerForkId.next_fork_epoch
|
|
|
|
|
|
|
|
else:
|
|
|
|
# Our next fork version is bigger than the peer's one
|
|
|
|
false
|
|
|
|
else:
|
|
|
|
# Wrong fork digest
|
|
|
|
false
|
|
|
|
|
|
|
|
proc queryRandom*(
|
|
|
|
d: Eth2DiscoveryProtocol,
|
|
|
|
forkId: ENRForkID,
|
|
|
|
wantedAttnets: AttnetBits,
|
|
|
|
wantedSyncnets: SyncnetBits,
|
2024-06-11 17:50:08 +00:00
|
|
|
minScore: int): Future[seq[Node]] {.async: (raises: [CancelledError]).} =
|
2024-01-13 09:54:24 +00:00
|
|
|
## Perform a discovery query for a random target
|
|
|
|
## (forkId) and matching at least one of the attestation subnets.
|
|
|
|
|
|
|
|
let nodes = await d.queryRandom()
|
|
|
|
|
|
|
|
var filtered: seq[(int, Node)]
|
|
|
|
for n in nodes:
|
|
|
|
var score: int = 0
|
|
|
|
|
|
|
|
let
|
|
|
|
eth2FieldBytes = n.record.get(enrForkIdField, seq[byte]).valueOr:
|
|
|
|
continue
|
|
|
|
peerForkId =
|
|
|
|
try:
|
|
|
|
SSZ.decode(eth2FieldBytes, ENRForkID)
|
2024-06-11 17:50:08 +00:00
|
|
|
except SerializationError as e:
|
2024-01-13 09:54:24 +00:00
|
|
|
debug "Could not decode the eth2 field of peer",
|
|
|
|
peer = n.record.toURI(), exception = e.name, msg = e.msg
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not forkId.isCompatibleForkId(peerForkId):
|
|
|
|
continue
|
|
|
|
|
|
|
|
let attnetsBytes = n.record.get(enrAttestationSubnetsField, seq[byte])
|
|
|
|
if attnetsBytes.isOk():
|
|
|
|
let attnetsNode =
|
|
|
|
try:
|
|
|
|
SSZ.decode(attnetsBytes.get(), AttnetBits)
|
2024-06-11 17:50:08 +00:00
|
|
|
except SerializationError as e:
|
2024-01-13 09:54:24 +00:00
|
|
|
debug "Could not decode the attnets ERN bitfield of peer",
|
|
|
|
peer = n.record.toURI(), exception = e.name, msg = e.msg
|
|
|
|
continue
|
|
|
|
|
|
|
|
for i in 0..<ATTESTATION_SUBNET_COUNT:
|
|
|
|
if wantedAttnets[i] and attnetsNode[i]:
|
|
|
|
score += 1
|
|
|
|
|
|
|
|
let syncnetsBytes = n.record.get(enrSyncSubnetsField, seq[byte])
|
|
|
|
if syncnetsBytes.isOk():
|
|
|
|
let syncnetsNode =
|
|
|
|
try:
|
|
|
|
SSZ.decode(syncnetsBytes.get(), SyncnetBits)
|
2024-06-11 17:50:08 +00:00
|
|
|
except SerializationError as e:
|
2024-01-13 09:54:24 +00:00
|
|
|
debug "Could not decode the syncnets ENR bitfield of peer",
|
|
|
|
peer = n.record.toURI(), exception = e.name, msg = e.msg
|
|
|
|
continue
|
|
|
|
|
|
|
|
for i in SyncSubcommitteeIndex:
|
|
|
|
if wantedSyncnets[i] and syncnetsNode[i]:
|
|
|
|
score += 10 # connecting to the right syncnet is urgent
|
|
|
|
|
|
|
|
if score >= minScore:
|
|
|
|
filtered.add((score, n))
|
|
|
|
|
|
|
|
d.rng[].shuffle(filtered)
|
|
|
|
return filtered.sortedByIt(-it[0]).mapIt(it[1])
|