2022-03-17 17:39:24 +00:00
|
|
|
# Nimbus
|
2024-02-28 17:31:45 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2022-03-17 17:39:24 +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-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2022-03-17 17:39:24 +00:00
|
|
|
|
2024-03-19 15:45:32 +00:00
|
|
|
import
|
|
|
|
std/[sequtils, strutils, os, macros],
|
|
|
|
results,
|
|
|
|
stew/io2,
|
|
|
|
chronos/timer,
|
|
|
|
beacon_chain/spec/forks,
|
|
|
|
./network/history/accumulator
|
2022-03-17 17:39:24 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
proc loadBootstrapNodes(path: string): seq[string] {.raises: [IOError].} =
|
2022-03-17 17:39:24 +00:00
|
|
|
# Read a list of ENR URIs from a file containing a flat list of entries.
|
|
|
|
# If the file can't be read, this will raise. This is intentionally.
|
2024-02-28 17:31:45 +00:00
|
|
|
splitLines(readFile(path)).filterIt(it.startsWith("enr:")).mapIt(it.strip())
|
|
|
|
|
|
|
|
proc loadCompileTimeBootstrapNodes(path: string): seq[string] =
|
2022-03-17 17:39:24 +00:00
|
|
|
try:
|
|
|
|
return loadBootstrapNodes(path)
|
|
|
|
# TODO: This error doesn't seem to get printed. It instead dies with an
|
|
|
|
# unhandled exception (IOError)
|
|
|
|
except IOError as err:
|
2024-02-28 17:31:45 +00:00
|
|
|
macros.error "Failed to load bootstrap nodes metadata at '" & path & "': " & err.msg
|
2022-03-17 17:39:24 +00:00
|
|
|
|
|
|
|
const
|
2024-03-22 10:28:44 +00:00
|
|
|
portalConfigDir =
|
2024-02-28 17:31:45 +00:00
|
|
|
currentSourcePath.parentDir.parentDir.replace('\\', '/') / "vendor" /
|
2024-03-22 10:28:44 +00:00
|
|
|
"portal-mainnet" / "config"
|
2022-03-17 17:39:24 +00:00
|
|
|
# Note:
|
|
|
|
# For now it gets called testnet0 but this Portal network serves Eth1 mainnet
|
|
|
|
# data. Giving the actual Portal (test)networks different names might not be
|
|
|
|
# that useful as there is no way to distinguish the networks currently.
|
|
|
|
#
|
|
|
|
# When more config data is required per Portal network, a metadata object can
|
|
|
|
# be created, but right now only bootstrap nodes can be different.
|
|
|
|
# TODO: It would be nice to be able to use `loadBootstrapFile` here, but that
|
|
|
|
# doesn't work at compile time. The main issue seems to be the usage of
|
|
|
|
# rlp.rawData() in the enr code.
|
2024-03-22 10:28:44 +00:00
|
|
|
testnet0BootstrapNodes* =
|
|
|
|
loadCompileTimeBootstrapNodes(portalConfigDir / "bootstrap_nodes.txt")
|
2022-10-17 18:38:51 +00:00
|
|
|
|
2024-03-22 10:28:44 +00:00
|
|
|
finishedAccumulatorSSZ* = slurp(portalConfigDir / "finished_accumulator.ssz")
|
2023-05-30 04:56:54 +00:00
|
|
|
|
2024-03-22 10:28:44 +00:00
|
|
|
historicalRootsSSZ* = slurp(portalConfigDir / "historical_roots.ssz")
|
2024-03-19 15:45:32 +00:00
|
|
|
|
|
|
|
func loadAccumulator*(): FinishedAccumulator =
|
|
|
|
try:
|
|
|
|
SSZ.decode(finishedAccumulatorSSZ, FinishedAccumulator)
|
|
|
|
except SerializationError as err:
|
|
|
|
raiseAssert "Invalid baked-in accumulator: " & err.msg
|
|
|
|
|
|
|
|
func loadHistoricalRoots*(): HashList[Eth2Digest, Limit HISTORICAL_ROOTS_LIMIT] =
|
|
|
|
try:
|
|
|
|
SSZ.decode(historicalRootsSSZ, HashList[Eth2Digest, Limit HISTORICAL_ROOTS_LIMIT])
|
|
|
|
except SerializationError as err:
|
|
|
|
raiseAssert "Invalid baked-in historical_roots: " & err.msg
|
|
|
|
|
2023-05-30 04:56:54 +00:00
|
|
|
type
|
|
|
|
# TODO: I guess we could use the nimbus ChainConfig but:
|
|
|
|
# - Only need some of the values right now
|
|
|
|
# - `EthTime` uses std/times while chronos Moment is sufficient and more
|
|
|
|
# sensible
|
|
|
|
ChainConfig* = object
|
2024-02-28 17:31:45 +00:00
|
|
|
mergeForkBlock*: uint64
|
|
|
|
shanghaiTime*: Opt[Moment]
|
|
|
|
cancunTime*: Opt[Moment]
|
2023-05-30 04:56:54 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
# Allow this to be adjusted at compile time for testing. If more constants
|
|
|
|
# need to be adjusted we can add some more ChainConfig presets either at
|
|
|
|
# compile or runtime.
|
|
|
|
mergeBlockNumber* {.intdefine.}: uint64 = 15537394
|
|
|
|
|
|
|
|
chainConfig* = ChainConfig(
|
|
|
|
mergeForkBlock: mergeBlockNumber,
|
|
|
|
shanghaiTime: Opt.some(Moment.init(1681338455'i64, Second)),
|
2024-02-28 17:31:45 +00:00
|
|
|
cancunTime: Opt.none(Moment),
|
2023-05-30 04:56:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func isTimestampForked(forkTime: Opt[Moment], timestamp: Moment): bool =
|
|
|
|
if forkTime.isNone():
|
|
|
|
false
|
|
|
|
else:
|
|
|
|
forkTime.get() <= timestamp
|
|
|
|
|
|
|
|
func isPoSBlock*(c: ChainConfig, blockNumber: uint64): bool =
|
|
|
|
c.mergeForkBlock <= blockNumber
|
|
|
|
|
|
|
|
func isShanghai*(c: ChainConfig, timestamp: Moment): bool =
|
|
|
|
isTimestampForked(c.shanghaiTime, timestamp)
|
|
|
|
|
|
|
|
func isCancun*(c: ChainConfig, timestamp: Moment): bool =
|
|
|
|
isTimestampForked(c.cancunTime, timestamp)
|