2019-12-10 19:49:42 +00:00
|
|
|
import
|
2020-07-03 19:29:23 +00:00
|
|
|
macros, strutils, parseutils, tables,
|
2020-07-13 14:44:58 +00:00
|
|
|
stew/endians2,
|
|
|
|
preset_values
|
2020-04-27 19:03:03 +00:00
|
|
|
|
2020-07-07 23:02:14 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2020-04-27 19:03:03 +00:00
|
|
|
export
|
2020-07-13 14:44:58 +00:00
|
|
|
PresetValue, toBytesBE
|
2019-12-10 19:49:42 +00:00
|
|
|
|
|
|
|
type
|
2020-07-07 23:02:14 +00:00
|
|
|
Slot* = distinct uint64
|
|
|
|
Epoch* = distinct uint64
|
|
|
|
Version* = distinct array[4, byte]
|
|
|
|
|
|
|
|
RuntimePreset* = object
|
|
|
|
GENESIS_FORK_VERSION*: Version
|
|
|
|
GENESIS_DELAY*: uint64
|
|
|
|
MIN_GENESIS_ACTIVE_VALIDATOR_COUNT*: uint64
|
|
|
|
MIN_GENESIS_TIME*: uint64
|
2020-08-06 09:08:54 +00:00
|
|
|
ETH1_FOLLOW_DISTANCE*: uint64
|
2020-07-07 23:02:14 +00:00
|
|
|
|
|
|
|
PresetFile* = object
|
|
|
|
values*: Table[PresetValue, TaintedString]
|
|
|
|
missingValues*: set[PresetValue]
|
|
|
|
|
|
|
|
PresetFileError* = object of CatchableError
|
|
|
|
|
2019-12-10 19:49:42 +00:00
|
|
|
const
|
2020-07-07 23:02:14 +00:00
|
|
|
const_preset* {.strdefine.} = "mainnet"
|
|
|
|
|
2020-07-03 19:29:23 +00:00
|
|
|
runtimeValues* = {
|
|
|
|
MIN_GENESIS_ACTIVE_VALIDATOR_COUNT,
|
|
|
|
MIN_GENESIS_TIME,
|
|
|
|
GENESIS_FORK_VERSION,
|
|
|
|
GENESIS_DELAY,
|
2020-08-06 09:08:54 +00:00
|
|
|
ETH1_FOLLOW_DISTANCE,
|
2020-07-03 19:29:23 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 19:49:42 +00:00
|
|
|
# These constants cannot really be overriden in a preset.
|
|
|
|
# If we encounter them, we'll just ignore the preset value.
|
2020-07-07 23:02:14 +00:00
|
|
|
ignoredValues* = {
|
2020-07-03 19:29:23 +00:00
|
|
|
# The deposit contract address is loaded through a dedicated
|
|
|
|
# metadata file. It would break the property we are exploiting
|
|
|
|
# right now that all preset values can be parsed as uint64
|
2020-07-07 23:02:14 +00:00
|
|
|
DEPOSIT_CONTRACT_ADDRESS,
|
2019-12-10 19:49:42 +00:00
|
|
|
|
|
|
|
# These are defined as an enum in datatypes.nim:
|
|
|
|
DOMAIN_BEACON_PROPOSER,
|
2020-04-27 19:03:03 +00:00
|
|
|
DOMAIN_BEACON_ATTESTER,
|
2019-12-10 19:49:42 +00:00
|
|
|
DOMAIN_RANDAO,
|
2020-04-27 19:03:03 +00:00
|
|
|
DOMAIN_DEPOSIT,
|
2019-12-10 19:49:42 +00:00
|
|
|
DOMAIN_VOLUNTARY_EXIT,
|
2020-04-27 19:03:03 +00:00
|
|
|
DOMAIN_SELECTION_PROOF,
|
|
|
|
DOMAIN_AGGREGATE_AND_PROOF,
|
2020-09-29 08:12:57 +00:00
|
|
|
CONFIG_NAME
|
2019-12-10 19:49:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 19:29:23 +00:00
|
|
|
presetValueTypes* = {
|
|
|
|
BLS_WITHDRAWAL_PREFIX: "byte",
|
|
|
|
GENESIS_FORK_VERSION: "Version",
|
2019-12-10 19:49:42 +00:00
|
|
|
}.toTable
|
|
|
|
|
2020-07-03 19:29:23 +00:00
|
|
|
func parse*(T: type uint64, input: string): T
|
|
|
|
{.raises: [ValueError, Defect].} =
|
2020-07-07 23:02:14 +00:00
|
|
|
var res: BiggestUInt
|
2020-07-03 19:29:23 +00:00
|
|
|
if input.len > 2 and input[0] == '0' and input[1] == 'x':
|
2020-07-07 23:02:14 +00:00
|
|
|
if parseHex(input, res) != input.len:
|
|
|
|
raise newException(ValueError, "The constant value should be a valid hex integer")
|
2020-07-03 19:29:23 +00:00
|
|
|
else:
|
2020-07-07 23:02:14 +00:00
|
|
|
if parseBiggestUInt(input, res) != input.len:
|
|
|
|
raise newException(ValueError, "The constant value should be a valid unsigned integer")
|
|
|
|
|
|
|
|
result = uint64(res)
|
2020-07-03 19:29:23 +00:00
|
|
|
|
|
|
|
template parse*(T: type byte, input: string): T =
|
|
|
|
byte parse(uint64, input)
|
|
|
|
|
2020-07-07 23:02:14 +00:00
|
|
|
proc parse*(T: type Version, input: string): T
|
|
|
|
{.raises: [ValueError, Defect].} =
|
|
|
|
Version toBytesBE(uint32 parse(uint64, input))
|
2020-07-03 19:29:23 +00:00
|
|
|
|
|
|
|
template parse*(T: type Slot, input: string): T =
|
|
|
|
Slot parse(uint64, input)
|
|
|
|
|
|
|
|
template getType*(presetValue: PresetValue): string =
|
2020-07-08 11:00:40 +00:00
|
|
|
presetValueTypes.getOrDefault(presetValue, "uint64")
|
2020-07-03 19:29:23 +00:00
|
|
|
|
2020-07-07 23:02:14 +00:00
|
|
|
template toUInt64*(v: Version): uint64 =
|
|
|
|
fromBytesBE(uint64, array[4, byte](v))
|
|
|
|
|
2019-12-10 19:49:42 +00:00
|
|
|
template entireSet(T: type enum): untyped =
|
|
|
|
{low(T) .. high(T)}
|
|
|
|
|
2020-07-07 23:02:14 +00:00
|
|
|
proc readPresetFile*(path: string): PresetFile
|
|
|
|
{.raises: [IOError, PresetFileError, Defect].} =
|
2019-12-10 19:49:42 +00:00
|
|
|
var
|
|
|
|
lineNum = 0
|
2020-07-03 19:29:23 +00:00
|
|
|
presetValues = ignoredValues
|
|
|
|
|
|
|
|
template lineinfo: string =
|
2020-07-07 23:02:14 +00:00
|
|
|
try: "$1($2) " % [path, $lineNum]
|
|
|
|
except ValueError: path
|
2020-07-03 19:29:23 +00:00
|
|
|
|
|
|
|
template fail(msg) =
|
2020-07-07 23:02:14 +00:00
|
|
|
raise newException(PresetFileError, lineinfo() & msg)
|
2019-12-10 19:49:42 +00:00
|
|
|
|
2020-07-03 19:29:23 +00:00
|
|
|
for line in splitLines(readFile(path)):
|
2019-12-10 19:49:42 +00:00
|
|
|
inc lineNum
|
|
|
|
if line.len == 0 or line[0] == '#': continue
|
|
|
|
|
2020-07-03 19:29:23 +00:00
|
|
|
var lineParts = line.split(":")
|
|
|
|
if lineParts.len != 2:
|
|
|
|
fail "Invalid syntax: A preset file should include only assignments in the form 'ConstName: Value'"
|
|
|
|
|
|
|
|
let value = try: parseEnum[PresetValue](lineParts[0])
|
|
|
|
except ValueError: fail "Unrecognized constant in a preset: " & lineParts[0]
|
|
|
|
|
|
|
|
if value in ignoredValues: continue
|
|
|
|
presetValues.incl value
|
2020-07-07 23:02:14 +00:00
|
|
|
result.values.add value, lineParts[1].strip
|
2020-07-03 19:29:23 +00:00
|
|
|
|
|
|
|
result.missingValues = PresetValue.entireSet - presetValues
|
|
|
|
|
2020-11-20 13:49:49 +00:00
|
|
|
const mainnetRuntimePreset* = RuntimePreset(
|
|
|
|
MIN_GENESIS_ACTIVE_VALIDATOR_COUNT: 16384,
|
|
|
|
MIN_GENESIS_TIME: 1606824000, # Dec 1, 2020, 12pm UTC
|
|
|
|
GENESIS_FORK_VERSION: Version [byte 0, 0, 0, 0],
|
|
|
|
GENESIS_DELAY: 604800,
|
|
|
|
ETH1_FOLLOW_DISTANCE: 2048)
|
2020-07-07 23:02:14 +00:00
|
|
|
|
2020-10-13 17:21:25 +00:00
|
|
|
const
|
2020-07-07 23:02:14 +00:00
|
|
|
minimalRuntimePreset* = RuntimePreset(
|
|
|
|
MIN_GENESIS_ACTIVE_VALIDATOR_COUNT: 64,
|
2020-11-12 15:29:32 +00:00
|
|
|
MIN_GENESIS_TIME: 1606824000, # Dec 1, 2020, 12pm UTC
|
2020-07-07 23:02:14 +00:00
|
|
|
GENESIS_FORK_VERSION: Version [byte 0, 0, 0, 1],
|
2020-08-06 09:08:54 +00:00
|
|
|
GENESIS_DELAY: 300,
|
|
|
|
ETH1_FOLLOW_DISTANCE: 16)
|
2020-07-07 23:02:14 +00:00
|
|
|
|
|
|
|
when const_preset == "mainnet":
|
|
|
|
template defaultRuntimePreset*: auto = mainnetRuntimePreset
|
2020-11-20 13:49:49 +00:00
|
|
|
import ./presets/v1_0_0/mainnet
|
2020-07-07 23:02:14 +00:00
|
|
|
export mainnet
|
|
|
|
|
|
|
|
elif const_preset == "minimal":
|
|
|
|
template defaultRuntimePreset*: auto = minimalRuntimePreset
|
2020-11-20 13:49:49 +00:00
|
|
|
import ./presets/v1_0_0/minimal
|
2020-07-07 23:02:14 +00:00
|
|
|
export minimal
|
|
|
|
|
|
|
|
else:
|
|
|
|
macro createConstantsFromPreset*(path: static string): untyped =
|
|
|
|
result = newStmtList()
|
|
|
|
|
|
|
|
let preset = try: readPresetFile(path)
|
|
|
|
except CatchableError as err:
|
|
|
|
error err.msg # TODO: This should be marked as noReturn
|
|
|
|
return
|
|
|
|
|
|
|
|
for name, value in preset.values:
|
2020-07-13 14:44:58 +00:00
|
|
|
let
|
|
|
|
typ = getType(name)
|
|
|
|
value = if typ in ["int64", "uint64", "byte"]: typ & "(" & value & ")"
|
|
|
|
else: "parse(" & typ & ", \"" & value & "\")"
|
|
|
|
try:
|
|
|
|
result.add parseStmt("const $1* {.intdefine.} = $2" % [$name, value])
|
|
|
|
except ValueError:
|
|
|
|
doAssert false, "All values in the presets are printable"
|
2020-07-07 23:02:14 +00:00
|
|
|
|
|
|
|
if preset.missingValues.card > 0:
|
|
|
|
warning "Missing constants in preset: " & $preset.missingValues
|
|
|
|
|
|
|
|
createConstantsFromPreset const_preset
|
|
|
|
|
|
|
|
const defaultRuntimePreset* = RuntimePreset(
|
|
|
|
MIN_GENESIS_ACTIVE_VALIDATOR_COUNT: MIN_GENESIS_ACTIVE_VALIDATOR_COUNT,
|
|
|
|
MIN_GENESIS_TIME: MIN_GENESIS_TIME,
|
|
|
|
GENESIS_FORK_VERSION: GENESIS_FORK_VERSION,
|
2020-08-06 09:08:54 +00:00
|
|
|
GENESIS_DELAY: GENESIS_DELAY,
|
|
|
|
ETH1_FOLLOW_DISTANCE: ETH1_FOLLOW_DISTANCE)
|
2019-12-10 19:49:42 +00:00
|
|
|
|