2023-05-11 11:11:00 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2019-2023 Status Research & Development GmbH
|
|
|
|
# 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.
|
|
|
|
|
2020-06-03 13:52:02 +00:00
|
|
|
# Required for deserialisation of ValidatorSig in Attestation due to
|
|
|
|
# https://github.com/nim-lang/Nim/issues/11225
|
|
|
|
|
2019-11-27 20:43:02 +00:00
|
|
|
import
|
2023-05-11 11:11:00 +00:00
|
|
|
stew/ptrops, chronicles,
|
2022-08-18 16:25:51 +00:00
|
|
|
../beacon_chain/networking/network_metadata,
|
2021-08-12 13:08:20 +00:00
|
|
|
../beacon_chain/spec/datatypes/phase0,
|
2021-06-11 17:51:46 +00:00
|
|
|
../beacon_chain/spec/[
|
2021-08-18 18:57:58 +00:00
|
|
|
beaconstate, eth2_ssz_serialization, forks, validator, state_transition,
|
|
|
|
state_transition_block]
|
2019-11-27 20:43:02 +00:00
|
|
|
|
|
|
|
type
|
2019-12-17 03:01:30 +00:00
|
|
|
AttestationInput = object
|
2021-08-12 13:08:20 +00:00
|
|
|
state: phase0.BeaconState
|
2019-12-17 03:01:30 +00:00
|
|
|
attestation: Attestation
|
|
|
|
AttesterSlashingInput = object
|
2021-08-12 13:08:20 +00:00
|
|
|
state: phase0.BeaconState
|
2019-12-17 03:01:30 +00:00
|
|
|
attesterSlashing: AttesterSlashing
|
2019-11-27 20:43:02 +00:00
|
|
|
BlockInput = object
|
2021-08-12 13:08:20 +00:00
|
|
|
state: phase0.BeaconState
|
|
|
|
beaconBlock: phase0.SignedBeaconBlock
|
2019-12-17 04:50:47 +00:00
|
|
|
BlockHeaderInput = BlockInput
|
2019-12-17 03:01:30 +00:00
|
|
|
DepositInput = object
|
2021-08-12 13:08:20 +00:00
|
|
|
state: phase0.BeaconState
|
2019-12-17 03:01:30 +00:00
|
|
|
deposit: Deposit
|
|
|
|
ProposerSlashingInput = object
|
2021-08-12 13:08:20 +00:00
|
|
|
state: phase0.BeaconState
|
2019-12-17 03:01:30 +00:00
|
|
|
proposerSlashing: ProposerSlashing
|
|
|
|
VoluntaryExitInput = object
|
2021-08-12 13:08:20 +00:00
|
|
|
state: phase0.BeaconState
|
2020-03-05 00:29:27 +00:00
|
|
|
exit: SignedVoluntaryExit
|
2019-12-16 05:46:58 +00:00
|
|
|
# This and AssertionError are raised to indicate programming bugs
|
2020-01-08 01:03:33 +00:00
|
|
|
# A wrapper to allow exception tracking to identify unexpected exceptions
|
2020-03-24 11:13:07 +00:00
|
|
|
FuzzCrashError = object of CatchableError
|
2019-11-27 20:43:02 +00:00
|
|
|
|
2019-11-28 22:01:12 +00:00
|
|
|
# TODO: change ptr uint to ptr csize_t when available in newer Nim version.
|
2021-08-12 13:08:20 +00:00
|
|
|
proc copyState(state: phase0.BeaconState, xoutput: ptr byte,
|
2020-06-25 10:23:10 +00:00
|
|
|
xoutput_size: ptr uint): bool {.raises: [FuzzCrashError, Defect].} =
|
2020-03-05 00:29:27 +00:00
|
|
|
var resultState =
|
|
|
|
try:
|
|
|
|
SSZ.encode(state)
|
|
|
|
except IOError as e:
|
|
|
|
# Shouldn't occur as the writer isn't a file
|
|
|
|
raise newException(FuzzCrashError, "Unexpected failure to serialize.", e)
|
2019-12-17 03:01:30 +00:00
|
|
|
|
2020-06-25 10:23:10 +00:00
|
|
|
if unlikely(resultState.len.uint > xoutput_size[]):
|
2020-01-08 01:03:33 +00:00
|
|
|
let msg = (
|
2020-06-25 10:23:10 +00:00
|
|
|
"Not enough xoutput buffer provided to nimbus harness. Provided: " &
|
|
|
|
$(xoutput_size[]) &
|
2020-01-08 01:03:33 +00:00
|
|
|
"Required: " &
|
|
|
|
$resultState.len.uint
|
|
|
|
)
|
|
|
|
raise newException(FuzzCrashError, msg)
|
2020-06-25 10:23:10 +00:00
|
|
|
xoutput_size[] = resultState.len.uint
|
|
|
|
# TODO: improvement might be to write directly to buffer with xoutputStream
|
2019-12-17 03:01:30 +00:00
|
|
|
# and SszWriter (but then need to ensure length doesn't overflow)
|
2020-06-25 10:23:10 +00:00
|
|
|
copyMem(xoutput, unsafeAddr resultState[0], xoutput_size[])
|
2019-12-17 03:01:30 +00:00
|
|
|
result = true
|
2019-11-27 20:43:02 +00:00
|
|
|
|
2020-03-05 00:29:27 +00:00
|
|
|
template decodeAndProcess(typ, process: untyped): bool =
|
|
|
|
let flags {.inject.} = if disable_bls: {skipBlsValidation} else: {}
|
2019-12-17 04:50:47 +00:00
|
|
|
|
2020-03-05 00:29:27 +00:00
|
|
|
var
|
2020-07-15 10:44:18 +00:00
|
|
|
cache {.used, inject.} = StateCache()
|
2020-04-29 20:12:07 +00:00
|
|
|
data {.inject.} = newClone(
|
2020-03-05 00:29:27 +00:00
|
|
|
try:
|
|
|
|
SSZ.decode(input, typ)
|
2023-06-15 16:29:13 +00:00
|
|
|
except SerializationError as e:
|
2020-03-05 00:29:27 +00:00
|
|
|
raise newException(
|
|
|
|
FuzzCrashError,
|
|
|
|
"Malformed SSZ, likely bug in preprocessing.", e)
|
2020-04-29 20:12:07 +00:00
|
|
|
)
|
2020-03-05 00:29:27 +00:00
|
|
|
let processOk =
|
|
|
|
try:
|
|
|
|
process
|
|
|
|
except IOError as e:
|
|
|
|
raise newException(
|
|
|
|
FuzzCrashError, "Unexpected (logging?) IOError in state transition", e,
|
|
|
|
)
|
|
|
|
except ValueError as e:
|
|
|
|
raise newException(
|
|
|
|
FuzzCrashError,
|
|
|
|
"Unexpected (logging?) IOError in state transition", e)
|
|
|
|
except Exception as e:
|
|
|
|
# TODO why an Exception?
|
|
|
|
# Lots of vendor code looks like it might raise a bare exception type
|
|
|
|
raise newException(FuzzCrashError, "Unexpected Exception in state transition", e)
|
|
|
|
|
|
|
|
if processOk:
|
2020-06-25 10:23:10 +00:00
|
|
|
copyState(data.state, xoutput, xoutput_size)
|
2020-03-05 00:29:27 +00:00
|
|
|
else:
|
|
|
|
false
|
2019-12-17 04:50:47 +00:00
|
|
|
|
2020-06-25 10:23:10 +00:00
|
|
|
proc nfuzz_attestation(input: openArray[byte], xoutput: ptr byte,
|
|
|
|
xoutput_size: ptr uint, disable_bls: bool): bool {.exportc, raises: [FuzzCrashError, Defect].} =
|
2020-03-05 00:29:27 +00:00
|
|
|
decodeAndProcess(AttestationInput):
|
2021-07-06 06:19:06 +00:00
|
|
|
process_attestation(data.state, data.attestation, flags, 0.Gwei, cache).isOk
|
2019-12-17 04:50:47 +00:00
|
|
|
|
2020-06-25 10:23:10 +00:00
|
|
|
proc nfuzz_attester_slashing(input: openArray[byte], xoutput: ptr byte,
|
|
|
|
xoutput_size: ptr uint, disable_bls: bool): bool {.exportc, raises: [FuzzCrashError, Defect].} =
|
2020-03-05 00:29:27 +00:00
|
|
|
decodeAndProcess(AttesterSlashingInput):
|
2023-05-11 11:11:00 +00:00
|
|
|
process_attester_slashing(getRuntimeConfig(some "mainnet"), data.state, data.attesterSlashing, flags, cache).isOk
|
2019-12-17 04:50:47 +00:00
|
|
|
|
2020-06-25 10:23:10 +00:00
|
|
|
proc nfuzz_block(input: openArray[byte], xoutput: ptr byte,
|
|
|
|
xoutput_size: ptr uint, disable_bls: bool): bool {.exportc, raises: [FuzzCrashError, Defect].} =
|
2020-04-30 16:27:17 +00:00
|
|
|
# There's not a perfect approach here, but it's not worth switching the rest
|
|
|
|
# and requiring HashedBeaconState (yet). So to keep consistent, puts wrapper
|
|
|
|
# only in one function.
|
|
|
|
proc state_transition(
|
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
|
|
|
cfg: RuntimeConfig, data: auto, blck: auto, flags: auto,
|
2021-06-11 17:51:46 +00:00
|
|
|
rollback: RollbackForkedHashedProc): auto =
|
2020-11-13 12:57:48 +00:00
|
|
|
var
|
2021-06-11 17:51:46 +00:00
|
|
|
fhState = (ref ForkedHashedBeaconState)(
|
2021-10-18 16:37:27 +00:00
|
|
|
phase0Data: phase0.HashedBeaconState(
|
2021-06-11 17:51:46 +00:00
|
|
|
data: data.state, root: hash_tree_root(data.state)),
|
2023-01-28 19:53:41 +00:00
|
|
|
kind: ConsensusFork.Phase0)
|
2020-11-13 12:57:48 +00:00
|
|
|
cache = StateCache()
|
2021-10-13 14:24:36 +00:00
|
|
|
info = ForkedEpochInfo()
|
2020-11-13 12:57:48 +00:00
|
|
|
result =
|
2021-05-07 11:36:21 +00:00
|
|
|
state_transition(
|
2021-10-13 14:24:36 +00:00
|
|
|
cfg, fhState[], blck, cache, info, flags, rollback)
|
2021-10-18 16:37:27 +00:00
|
|
|
data.state = fhState.phase0Data.data
|
2020-04-30 16:27:17 +00:00
|
|
|
|
2020-03-05 00:29:27 +00:00
|
|
|
decodeAndProcess(BlockInput):
|
2022-01-17 11:19:58 +00:00
|
|
|
state_transition(
|
2023-05-11 11:11:00 +00:00
|
|
|
getRuntimeConfig(some "mainnet"), data, data.beaconBlock, flags, noRollback).isOk
|
2019-11-27 20:43:02 +00:00
|
|
|
|
2020-06-25 10:23:10 +00:00
|
|
|
proc nfuzz_block_header(input: openArray[byte], xoutput: ptr byte,
|
|
|
|
xoutput_size: ptr uint, disable_bls: bool): bool {.exportc, raises: [FuzzCrashError, Defect].} =
|
2020-03-05 00:29:27 +00:00
|
|
|
decodeAndProcess(BlockHeaderInput):
|
2020-07-03 17:03:14 +00:00
|
|
|
process_block_header(data.state, data.beaconBlock.message, flags, cache).isOk
|
2019-12-17 04:50:47 +00:00
|
|
|
|
2020-06-25 10:23:10 +00:00
|
|
|
proc nfuzz_deposit(input: openArray[byte], xoutput: ptr byte,
|
|
|
|
xoutput_size: ptr uint, disable_bls: bool): bool {.exportc, raises: [FuzzCrashError, Defect].} =
|
2020-03-05 00:29:27 +00:00
|
|
|
decodeAndProcess(DepositInput):
|
2023-05-11 11:11:00 +00:00
|
|
|
process_deposit(getRuntimeConfig(some "mainnet"), data.state, data.deposit, flags).isOk
|
2019-12-17 03:01:30 +00:00
|
|
|
|
2020-06-25 10:23:10 +00:00
|
|
|
proc nfuzz_proposer_slashing(input: openArray[byte], xoutput: ptr byte,
|
|
|
|
xoutput_size: ptr uint, disable_bls: bool): bool {.exportc, raises: [FuzzCrashError, Defect].} =
|
2020-03-05 00:29:27 +00:00
|
|
|
decodeAndProcess(ProposerSlashingInput):
|
2023-05-11 11:11:00 +00:00
|
|
|
process_proposer_slashing(getRuntimeConfig(some "mainnet"), data.state, data.proposerSlashing, flags, cache).isOk
|
2019-12-17 03:01:30 +00:00
|
|
|
|
2020-06-25 10:23:10 +00:00
|
|
|
proc nfuzz_voluntary_exit(input: openArray[byte], xoutput: ptr byte,
|
|
|
|
xoutput_size: ptr uint, disable_bls: bool): bool {.exportc, raises: [FuzzCrashError, Defect].} =
|
2020-03-05 00:29:27 +00:00
|
|
|
decodeAndProcess(VoluntaryExitInput):
|
2023-05-11 11:11:00 +00:00
|
|
|
process_voluntary_exit(getRuntimeConfig(some "mainnet"), data.state, data.exit, flags, cache).isOk
|
2019-12-17 03:01:30 +00:00
|
|
|
|
2019-11-27 20:43:02 +00:00
|
|
|
# Note: Could also accept raw input pointer and access list_size + seed here.
|
2020-06-25 10:23:10 +00:00
|
|
|
# However, list_size needs to be known also outside this proc to allocate xoutput.
|
2019-11-27 20:43:02 +00:00
|
|
|
# TODO: rework to copy immediatly in an uint8 openArray, considering we have to
|
|
|
|
# go over the list anyhow?
|
2020-06-25 10:23:10 +00:00
|
|
|
proc nfuzz_shuffle(input_seed: ptr byte, xoutput: var openArray[uint64]): bool
|
2020-01-08 01:03:33 +00:00
|
|
|
{.exportc, raises: [Defect].} =
|
2019-11-27 20:43:02 +00:00
|
|
|
var seed: Eth2Digest
|
2019-11-28 22:01:12 +00:00
|
|
|
# Should be OK as max 2 bytes are passed by the framework.
|
2020-07-26 18:55:48 +00:00
|
|
|
let list_size = xoutput.len
|
2019-11-27 20:43:02 +00:00
|
|
|
|
2019-11-28 22:01:12 +00:00
|
|
|
copyMem(addr(seed.data), input_seed, sizeof(seed.data))
|
2019-11-27 20:43:02 +00:00
|
|
|
|
2019-11-28 22:01:12 +00:00
|
|
|
var shuffled_seq: seq[ValidatorIndex]
|
speed up shuffling
Replace shuffling function with zrnt version - `get_shuffled_seq` in
particular puts more strain on the GC by allocating superfluous seq's
which turns out to have a significant impact on block processing (when
replaying blocks for example) - 4x improvement on non-epoch, 1.5x on
epoch blocks (replay is done without signature checking)
Medalla, first 10k slots - pre:
```
Loaded 68973 blocks, head slot 117077
All time are ms
Average, StdDev, Min, Max, Samples,
Test
Validation is turned off meaning that no BLS operations are performed
76855.848, 0.000, 76855.848, 76855.848, 1,
Initialize DB
1.073, 0.914, 0.071, 12.454, 7831,
Load block from database
31.382, 0.000, 31.382, 31.382, 1,
Load state from database
85.644, 30.350, 3.056, 466.136, 7519,
Apply block
506.569, 91.129, 130.654, 874.786, 312,
Apply epoch block
```
post:
```
Loaded 68973 blocks, head slot 117077
All time are ms
Average, StdDev, Min, Max, Samples,
Test
Validation is turned off meaning that no BLS operations are performed
72457.303, 0.000, 72457.303, 72457.303, 1,
Initialize DB
1.015, 0.858, 0.070, 11.231, 7831,
Load block from database
28.983, 0.000, 28.983, 28.983, 1,
Load state from database
21.725, 17.461, 2.659, 393.217, 7519,
Apply block
324.012, 33.954, 45.452, 440.532, 312,
Apply epoch block
```
2020-08-21 10:06:26 +00:00
|
|
|
for i in 0..<list_size:
|
|
|
|
shuffled_seq.add i.ValidatorIndex
|
|
|
|
shuffle_list(shuffled_seq, seed)
|
2019-11-27 20:43:02 +00:00
|
|
|
|
|
|
|
for i in 0..<list_size:
|
|
|
|
# ValidatorIndex is currently wrongly uint32 so we copy this 1 by 1,
|
2020-06-25 10:23:10 +00:00
|
|
|
# assumes passed xoutput is zeroed.
|
2020-07-26 18:55:48 +00:00
|
|
|
copyMem(offset(addr xoutput, i), shuffled_seq[i].unsafeAddr,
|
2019-11-27 20:43:02 +00:00
|
|
|
sizeof(ValidatorIndex))
|
2019-11-28 22:01:12 +00:00
|
|
|
|
2020-07-26 18:55:48 +00:00
|
|
|
true
|