2020-05-01 15:51:24 +00:00
|
|
|
import
|
2020-05-28 14:19:25 +00:00
|
|
|
stats, os, strformat, times,
|
2021-04-16 08:49:37 +00:00
|
|
|
../tests/testblockutil,
|
2021-03-03 06:23:05 +00:00
|
|
|
../beacon_chain/[extras, beacon_chain_db],
|
2020-06-03 13:52:02 +00:00
|
|
|
../beacon_chain/ssz/[merkleization, ssz_serialization],
|
2021-06-11 17:51:46 +00:00
|
|
|
../beacon_chain/spec/[
|
|
|
|
beaconstate, crypto, datatypes, digest, forkedbeaconstate_helpers,
|
|
|
|
helpers, presets],
|
|
|
|
../beacon_chain/consensus_object_pools/[blockchain_dag, block_pools_types],
|
2021-03-03 06:23:05 +00:00
|
|
|
../beacon_chain/eth1/eth1_monitor
|
2020-05-01 15:51:24 +00:00
|
|
|
|
|
|
|
template withTimer*(stats: var RunningStat, body: untyped) =
|
|
|
|
# TODO unify timing somehow
|
|
|
|
let start = cpuTime()
|
|
|
|
|
|
|
|
block:
|
|
|
|
body
|
|
|
|
|
|
|
|
let stop = cpuTime()
|
|
|
|
stats.push stop - start
|
|
|
|
|
|
|
|
template withTimerRet*(stats: var RunningStat, body: untyped): untyped =
|
|
|
|
let start = cpuTime()
|
|
|
|
let tmp = block:
|
|
|
|
body
|
|
|
|
let stop = cpuTime()
|
|
|
|
stats.push stop - start
|
|
|
|
|
|
|
|
tmp
|
|
|
|
|
|
|
|
func verifyConsensus*(state: BeaconState, attesterRatio: auto) =
|
|
|
|
if attesterRatio < 0.63:
|
|
|
|
doAssert state.current_justified_checkpoint.epoch == 0
|
|
|
|
doAssert state.finalized_checkpoint.epoch == 0
|
|
|
|
|
|
|
|
# Quorum is 2/3 of validators, and at low numbers, quantization effects
|
|
|
|
# can dominate, so allow for play above/below attesterRatio of 2/3.
|
|
|
|
if attesterRatio < 0.72:
|
|
|
|
return
|
|
|
|
|
|
|
|
let current_epoch = get_current_epoch(state)
|
|
|
|
if current_epoch >= 3:
|
|
|
|
doAssert state.current_justified_checkpoint.epoch + 1 >= current_epoch
|
|
|
|
if current_epoch >= 4:
|
|
|
|
doAssert state.finalized_checkpoint.epoch + 2 >= current_epoch
|
|
|
|
|
2021-06-11 17:51:46 +00:00
|
|
|
func verifyConsensus*(state: ForkedHashedBeaconState, attesterRatio: auto) =
|
2021-04-16 08:49:37 +00:00
|
|
|
if attesterRatio < 0.63:
|
|
|
|
doAssert getStateField(state, current_justified_checkpoint).epoch == 0
|
|
|
|
doAssert getStateField(state, finalized_checkpoint).epoch == 0
|
|
|
|
|
|
|
|
# Quorum is 2/3 of validators, and at low numbers, quantization effects
|
|
|
|
# can dominate, so allow for play above/below attesterRatio of 2/3.
|
|
|
|
if attesterRatio < 0.72:
|
|
|
|
return
|
|
|
|
|
|
|
|
let current_epoch = get_current_epoch(state)
|
|
|
|
if current_epoch >= 3:
|
|
|
|
doAssert getStateField(
|
|
|
|
state, current_justified_checkpoint).epoch + 1 >= current_epoch
|
|
|
|
if current_epoch >= 4:
|
|
|
|
doAssert getStateField(
|
|
|
|
state, finalized_checkpoint).epoch + 2 >= current_epoch
|
|
|
|
|
2020-12-03 04:30:35 +00:00
|
|
|
proc loadGenesis*(validators: Natural, validate: bool):
|
|
|
|
(ref HashedBeaconState, DepositContractSnapshot) =
|
|
|
|
let
|
|
|
|
genesisFn =
|
|
|
|
&"genesis_{const_preset}_{validators}_{SPEC_VERSION}.ssz"
|
|
|
|
contractSnapshotFn =
|
|
|
|
&"deposit_contract_snapshot_{const_preset}_{validators}_{SPEC_VERSION}.ssz"
|
|
|
|
res = (ref HashedBeaconState)()
|
|
|
|
|
|
|
|
if fileExists(genesisFn) and fileExists(contractSnapshotFn):
|
|
|
|
res.data = SSZ.loadFile(genesisFn, BeaconState)
|
2020-05-22 14:21:22 +00:00
|
|
|
res.root = hash_tree_root(res.data)
|
|
|
|
if res.data.slot != GENESIS_SLOT:
|
2020-05-01 15:51:24 +00:00
|
|
|
echo "Can only start from genesis state"
|
|
|
|
quit 1
|
|
|
|
|
2020-05-22 14:21:22 +00:00
|
|
|
if res.data.validators.len != validators:
|
|
|
|
echo &"Supplied genesis file has {res.data.validators.len} validators, while {validators} where requested, running anyway"
|
2020-05-01 15:51:24 +00:00
|
|
|
|
2020-12-03 04:30:35 +00:00
|
|
|
echo &"Loaded {genesisFn}..."
|
|
|
|
|
2020-05-01 15:51:24 +00:00
|
|
|
# TODO check that the private keys are interop keys
|
2020-12-03 04:30:35 +00:00
|
|
|
|
|
|
|
let contractSnapshot = SSZ.loadFile(contractSnapshotFn,
|
|
|
|
DepositContractSnapshot)
|
|
|
|
(res, contractSnapshot)
|
2020-05-01 15:51:24 +00:00
|
|
|
else:
|
2020-11-07 18:00:31 +00:00
|
|
|
echo "Genesis file not found, making one up (use nimbus_beacon_node createTestnet to make one)"
|
2020-05-01 15:51:24 +00:00
|
|
|
|
|
|
|
echo "Preparing validators..."
|
|
|
|
let
|
|
|
|
flags = if validate: {} else: {skipBlsValidation}
|
2020-07-13 14:44:58 +00:00
|
|
|
deposits = makeInitialDeposits(validators.uint64, flags)
|
2020-05-01 15:51:24 +00:00
|
|
|
|
|
|
|
echo "Generating Genesis..."
|
2020-12-03 04:30:35 +00:00
|
|
|
var merkleizer = init DepositsMerkleizer
|
|
|
|
for d in deposits:
|
|
|
|
merkleizer.addChunk hash_tree_root(d).data
|
|
|
|
let contractSnapshot = DepositContractSnapshot(
|
|
|
|
depositContractState: merkleizer.toDepositContractState)
|
2020-05-01 15:51:24 +00:00
|
|
|
|
2021-05-04 10:19:11 +00:00
|
|
|
res.data = initialize_beacon_state_from_eth1(
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
defaultRuntimeConfig,
|
2020-11-14 21:43:27 +00:00
|
|
|
Eth2Digest(),
|
|
|
|
0,
|
|
|
|
deposits,
|
|
|
|
flags)[]
|
|
|
|
|
2020-05-22 14:21:22 +00:00
|
|
|
res.root = hash_tree_root(res.data)
|
2020-05-01 15:51:24 +00:00
|
|
|
|
2020-12-03 04:30:35 +00:00
|
|
|
echo &"Saving to {genesisFn}..."
|
|
|
|
SSZ.saveFile(genesisFn, res.data)
|
|
|
|
echo &"Saving to {contractSnapshotFn}..."
|
|
|
|
SSZ.saveFile(contractSnapshotFn, contractSnapshot)
|
2020-06-16 20:34:34 +00:00
|
|
|
|
2020-12-03 04:30:35 +00:00
|
|
|
(res, contractSnapshot)
|
2020-05-01 15:51:24 +00:00
|
|
|
|
|
|
|
proc printTimers*[Timers: enum](
|
2020-05-28 14:19:25 +00:00
|
|
|
validate: bool,
|
|
|
|
timers: array[Timers, RunningStat]
|
|
|
|
) =
|
2020-05-01 15:51:24 +00:00
|
|
|
proc fmtTime(t: float): string = &"{t * 1000 :>12.3f}, "
|
|
|
|
|
|
|
|
echo "All time are ms"
|
|
|
|
echo &"{\"Average\" :>12}, {\"StdDev\" :>12}, {\"Min\" :>12}, " &
|
|
|
|
&"{\"Max\" :>12}, {\"Samples\" :>12}, {\"Test\" :>12}"
|
|
|
|
|
|
|
|
if not validate:
|
|
|
|
echo "Validation is turned off meaning that no BLS operations are performed"
|
|
|
|
|
|
|
|
for t in Timers:
|
|
|
|
echo fmtTime(timers[t].mean), fmtTime(timers[t].standardDeviationS),
|
|
|
|
fmtTime(timers[t].min), fmtTime(timers[t].max), &"{timers[t].n :>12}, ",
|
|
|
|
$t
|
2020-05-28 14:19:25 +00:00
|
|
|
|
|
|
|
proc printTimers*[Timers: enum](
|
|
|
|
state: BeaconState, attesters: RunningStat, validate: bool,
|
|
|
|
timers: array[Timers, RunningStat]) =
|
|
|
|
echo "Validators: ", state.validators.len, ", epoch length: ", SLOTS_PER_EPOCH
|
|
|
|
echo "Validators per attestation (mean): ", attesters.mean
|
|
|
|
printTimers(validate, timers)
|
2021-04-16 08:49:37 +00:00
|
|
|
|
|
|
|
proc printTimers*[Timers: enum](
|
2021-06-11 17:51:46 +00:00
|
|
|
state: ForkedHashedBeaconState, attesters: RunningStat, validate: bool,
|
2021-04-16 08:49:37 +00:00
|
|
|
timers: array[Timers, RunningStat]) =
|
|
|
|
echo "Validators: ", getStateField(state, validators).len, ", epoch length: ", SLOTS_PER_EPOCH
|
|
|
|
echo "Validators per attestation (mean): ", attesters.mean
|
|
|
|
printTimers(validate, timers)
|