2019-11-22 19:56:39 +00:00
|
|
|
# Sanity check on constants
|
|
|
|
# ----------------------------------------------------------------
|
|
|
|
|
2019-11-25 06:31:37 +00:00
|
|
|
{.used.}
|
|
|
|
|
2019-11-22 19:56:39 +00:00
|
|
|
import
|
|
|
|
# Standard library
|
|
|
|
macros, os, strutils, tables, math, json, streams,
|
2021-04-28 16:41:02 +00:00
|
|
|
strformat,
|
2019-11-22 19:56:39 +00:00
|
|
|
# Third party
|
|
|
|
yaml,
|
|
|
|
# Status libraries
|
2021-04-28 16:41:02 +00:00
|
|
|
unittest2,
|
2019-11-22 19:56:39 +00:00
|
|
|
stew/[byteutils, endians2],
|
|
|
|
# Internals
|
2021-03-04 09:13:23 +00:00
|
|
|
../../beacon_chain/spec/[datatypes, presets],
|
2019-11-22 19:56:39 +00:00
|
|
|
# Test utilities
|
2020-02-14 10:29:45 +00:00
|
|
|
../testutil, ./fixtures_utils
|
2019-11-22 19:56:39 +00:00
|
|
|
|
|
|
|
const
|
2021-05-30 09:51:01 +00:00
|
|
|
# TODO NimYAML issue in Altair re 2^64 - 1 and parseBiggestInt
|
2019-11-22 19:56:39 +00:00
|
|
|
SpecDir = currentSourcePath.rsplit(DirSep, 1)[0] /
|
|
|
|
".."/".."/"beacon_chain"/"spec"
|
2021-05-30 09:51:01 +00:00
|
|
|
|
|
|
|
# v1.1.0-alpha.6 doesn't seem to have this file
|
|
|
|
OldSszTestsDir* = FixturesDir / "tests-v1.1.0-alpha.5"
|
|
|
|
Config = OldSszTestsDir/const_preset/"config"/"phase0.yaml"
|
2019-11-22 19:56:39 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
CheckedType = SomeInteger or Slot or Epoch
|
|
|
|
# Only test numerical types, constants
|
|
|
|
# defined for other type will get a placeholder low(int64) value
|
|
|
|
|
|
|
|
macro parseNumConsts(file: static string): untyped =
|
|
|
|
## Important: All const are assumed to be top-level
|
|
|
|
## i.e. not hidden behind a "when" statement
|
|
|
|
|
|
|
|
var constsToCheck: seq[(string, NimNode)]
|
|
|
|
# We can't create a table directly and quote do it
|
|
|
|
# Nim complains about the "data" field not being accessible
|
|
|
|
|
|
|
|
let fileAST = parseStmt(slurp(file))
|
|
|
|
for statement in fileAST:
|
|
|
|
if statement.kind == nnkConstSection:
|
|
|
|
for constDef in statement:
|
|
|
|
if constDef.len == 0 or
|
|
|
|
constDef[0].kind notin {nnkPragmaExpr, nnkPostfix}:
|
|
|
|
# Comments in a const section need to be skipped.
|
|
|
|
# And we only want exported constants.
|
|
|
|
continue
|
|
|
|
# Note: we assume that all const with a pragma are exported
|
|
|
|
# 1. Simple statement
|
|
|
|
#
|
|
|
|
# ConstDef
|
|
|
|
# Postfix
|
|
|
|
# Ident "*"
|
|
|
|
# Ident "HISTORICAL_ROOTS_LIMIT"
|
|
|
|
# Empty
|
|
|
|
# IntLit 16777216
|
|
|
|
#
|
|
|
|
# 2. with intdefine pragma
|
|
|
|
#
|
|
|
|
# ConstDef
|
|
|
|
# PragmaExpr
|
|
|
|
# Postfix
|
|
|
|
# Ident "*"
|
|
|
|
# Ident "MIN_GENESIS_ACTIVE_VALIDATOR_COUNT"
|
|
|
|
# Pragma
|
|
|
|
# Ident "intdefine"
|
|
|
|
# Empty
|
|
|
|
# IntLit 99
|
|
|
|
let name = if constDef[0].kind == nnkPostfix: $constDef[0][1]
|
|
|
|
else: $constDef[0][0][1]
|
|
|
|
|
|
|
|
# ConstsToCheck["HISTORICAL_ROOTS_LIMIT"} = uint64(16777216)
|
|
|
|
# Put a placeholder values for strings
|
|
|
|
let value = block:
|
|
|
|
let node = constDef[2]
|
|
|
|
quote do:
|
|
|
|
when `node` is CheckedType:
|
|
|
|
uint64(`node`)
|
|
|
|
else:
|
|
|
|
high(uint64)
|
|
|
|
constsToCheck.add (name, value)
|
|
|
|
|
|
|
|
result = quote do: `constsToCheck`
|
|
|
|
|
2020-06-20 07:12:45 +00:00
|
|
|
const
|
|
|
|
datatypesConsts = @(parseNumConsts(SpecDir/"datatypes.nim"))
|
|
|
|
specVersionPresetDir = SpecDir/"presets"/("v"&replace(SPEC_VERSION, ".", "_"))
|
2020-04-15 07:59:47 +00:00
|
|
|
|
|
|
|
when const_preset == "minimal":
|
2020-06-20 07:12:45 +00:00
|
|
|
const minimalConsts = @(parseNumConsts(specVersionPresetDir/"minimal.nim"))
|
2020-04-15 07:59:47 +00:00
|
|
|
else:
|
2020-06-20 07:12:45 +00:00
|
|
|
const mainnetConsts = @(parseNumConsts(specVersionPresetDir/"mainnet.nim"))
|
2019-11-22 19:56:39 +00:00
|
|
|
|
|
|
|
const IgnoreKeys = [
|
|
|
|
# Ignore all non-numeric types
|
2020-09-21 15:58:35 +00:00
|
|
|
"CONFIG_NAME",
|
2020-03-14 21:54:45 +00:00
|
|
|
"DEPOSIT_CONTRACT_ADDRESS",
|
2020-04-06 18:18:42 +00:00
|
|
|
"GENESIS_FORK_VERSION",
|
2020-07-07 23:02:14 +00:00
|
|
|
"SHARD_BLOCK_OFFSETS",
|
|
|
|
|
|
|
|
# TODO
|
|
|
|
# There constants were removed from the preset modules
|
|
|
|
# because we consider them run-time settings now, but
|
|
|
|
# it would be still useful to check them.
|
|
|
|
"MIN_GENESIS_ACTIVE_VALIDATOR_COUNT",
|
|
|
|
"MIN_GENESIS_TIME",
|
|
|
|
"GENESIS_FORK_VERSION",
|
|
|
|
"GENESIS_DELAY",
|
2020-10-19 13:53:23 +00:00
|
|
|
"ETH1_FOLLOW_DISTANCE",
|
2020-07-07 23:02:14 +00:00
|
|
|
|
2020-08-04 15:53:39 +00:00
|
|
|
# TODO remove when possible
|
|
|
|
"DEPOSIT_CHAIN_ID",
|
|
|
|
"DEPOSIT_NETWORK_ID"
|
2019-11-22 19:56:39 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
func parseU32LEHex(hexValue: string): uint32 =
|
|
|
|
## Parse little-endian uint32 hex string
|
|
|
|
result = uint32.fromBytesLE hexToByteArray[4](hexValue)
|
|
|
|
|
|
|
|
proc checkConfig() =
|
|
|
|
let ConstsToCheck = toTable(
|
|
|
|
when const_preset == "minimal":
|
|
|
|
minimalConsts & datatypesConsts
|
|
|
|
else:
|
|
|
|
mainnetConsts & datatypesConsts
|
|
|
|
)
|
|
|
|
|
|
|
|
var yamlStream = openFileStream(Config)
|
|
|
|
defer: yamlStream.close()
|
|
|
|
var config = yamlStream.loadToJson()
|
|
|
|
doAssert config.len == 1
|
|
|
|
for constant, value in config[0]:
|
2021-04-28 16:41:02 +00:00
|
|
|
test &"{constant:<50}{value:<20}{preset()}":
|
2019-11-22 19:56:39 +00:00
|
|
|
if constant in IgnoreKeys:
|
|
|
|
echo &" ↶↶ Skipping {constant}"
|
2020-05-21 17:56:09 +00:00
|
|
|
skip()
|
|
|
|
elif constant.startsWith("DOMAIN"):
|
2019-11-22 19:56:39 +00:00
|
|
|
let domain = parseEnum[DomainType](constant)
|
|
|
|
let value = parseU32LEHex(value.getStr())
|
|
|
|
check: uint32(domain) == value
|
2020-03-14 21:54:45 +00:00
|
|
|
elif constant.endsWith("_FORK_VERSION"):
|
2020-01-14 17:46:58 +00:00
|
|
|
let value = parseU32LEHex(value.getStr())
|
|
|
|
check: ConstsToCheck[constant] == value
|
2019-11-22 19:56:39 +00:00
|
|
|
else:
|
|
|
|
check: ConstsToCheck[constant] == value.getBiggestInt().uint64()
|
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
suite "Official - constants & config " & preset():
|
2019-11-22 19:56:39 +00:00
|
|
|
checkConfig()
|