2019-08-28 12:07:00 +00:00
|
|
|
# beacon_chain
|
2022-01-29 13:53:31 +00:00
|
|
|
# Copyright (c) 2018-2022 Status Research & Development GmbH
|
2019-08-28 12:07:00 +00:00
|
|
|
# Licensed and distributed under either of
|
2019-11-25 15:30:02 +00:00
|
|
|
# * 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).
|
2019-08-28 12:07:00 +00:00
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
|
|
|
|
# process_deposit (beaconstate.nim)
|
2022-08-20 16:03:32 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.3/specs/phase0/beacon-chain.md#deposits
|
2019-08-28 12:07:00 +00:00
|
|
|
# ---------------------------------------------------------------
|
|
|
|
|
2019-11-14 10:47:55 +00:00
|
|
|
{.used.}
|
|
|
|
|
2019-08-28 12:07:00 +00:00
|
|
|
import
|
|
|
|
# Standard library
|
2021-04-28 16:41:02 +00:00
|
|
|
std/math,
|
2019-08-28 12:07:00 +00:00
|
|
|
# Specs
|
2021-09-13 22:56:37 +00:00
|
|
|
../../beacon_chain/spec/[forks, state_transition_block],
|
2021-06-21 08:35:24 +00:00
|
|
|
../../beacon_chain/spec/datatypes/base,
|
2019-08-28 12:07:00 +00:00
|
|
|
# Internals
|
|
|
|
# Mock helpers
|
|
|
|
../mocking/[mock_deposits, mock_genesis],
|
|
|
|
../testutil, ../helpers/math_helpers
|
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
suite "[Unit - Spec - Block processing] Deposits " & preset():
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
const NumValidators = uint64 5 * SLOTS_PER_EPOCH
|
2021-10-18 16:37:27 +00:00
|
|
|
let genesisState = newClone(initGenesisState(NumValidators).phase0Data)
|
2020-07-26 18:55:48 +00:00
|
|
|
doAssert genesisState.data.validators.lenu64 == NumValidators
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
template valid_deposit(deposit_amount: uint64, name: string): untyped =
|
2021-04-28 16:41:02 +00:00
|
|
|
test "Deposit " & name & " MAX_EFFECTIVE_BALANCE balance (" &
|
2019-08-28 12:07:00 +00:00
|
|
|
$(MAX_EFFECTIVE_BALANCE div 10'u64^9) & " ETH)":
|
2022-02-20 20:13:06 +00:00
|
|
|
let state = assignClone(genesisState[])
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Test configuration
|
|
|
|
# ----------------------------------------
|
2020-04-30 06:44:19 +00:00
|
|
|
let validator_index = state.data.validators.len
|
2019-08-28 12:07:00 +00:00
|
|
|
let deposit = mockUpdateStateForNewDeposit(
|
2020-04-30 06:44:19 +00:00
|
|
|
state.data,
|
2019-08-28 12:07:00 +00:00
|
|
|
uint64 validator_index,
|
|
|
|
deposit_amount,
|
2020-07-08 04:55:23 +00:00
|
|
|
flags = {}
|
2019-08-28 12:07:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Params for sanity checks
|
|
|
|
# ----------------------------------------
|
2020-04-30 06:44:19 +00:00
|
|
|
let pre_val_count = state.data.validators.len
|
2019-08-28 12:07:00 +00:00
|
|
|
let pre_balance = if validator_index < pre_val_count:
|
2022-05-30 13:30:42 +00:00
|
|
|
state.data.balances.item(validator_index)
|
2019-08-28 12:07:00 +00:00
|
|
|
else:
|
|
|
|
0
|
|
|
|
|
|
|
|
# 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
|
|
|
check: process_deposit(defaultRuntimeConfig, state.data, deposit, {}).isOk
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Check invariants
|
|
|
|
# ----------------------------------------
|
|
|
|
check:
|
2020-04-30 06:44:19 +00:00
|
|
|
state.data.validators.len == pre_val_count + 1
|
|
|
|
state.data.balances.len == pre_val_count + 1
|
2022-05-30 13:30:42 +00:00
|
|
|
state.data.balances.item(validator_index) == pre_balance + deposit.data.amount
|
|
|
|
state.data.validators.item(validator_index).effective_balance ==
|
2019-08-28 12:07:00 +00:00
|
|
|
round_multiple_down(
|
2022-05-30 13:30:42 +00:00
|
|
|
min(MAX_EFFECTIVE_BALANCE, state.data.balances.item(validator_index)),
|
2019-08-28 12:07:00 +00:00
|
|
|
EFFECTIVE_BALANCE_INCREMENT
|
|
|
|
)
|
|
|
|
|
|
|
|
valid_deposit(MAX_EFFECTIVE_BALANCE - 1, "under")
|
|
|
|
valid_deposit(MAX_EFFECTIVE_BALANCE, "at")
|
|
|
|
valid_deposit(MAX_EFFECTIVE_BALANCE + 1, "over")
|
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
test "Validator top-up":
|
2022-02-20 20:13:06 +00:00
|
|
|
let state = assignClone(genesisState[])
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Test configuration
|
|
|
|
# ----------------------------------------
|
|
|
|
let validator_index = 0
|
|
|
|
let deposit_amount = MAX_EFFECTIVE_BALANCE div 4
|
|
|
|
let deposit = mockUpdateStateForNewDeposit(
|
2020-04-30 06:44:19 +00:00
|
|
|
state.data,
|
2019-08-28 12:07:00 +00:00
|
|
|
uint64 validator_index,
|
|
|
|
deposit_amount,
|
2020-07-08 04:55:23 +00:00
|
|
|
flags = {}
|
2019-08-28 12:07:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Params for sanity checks
|
|
|
|
# ----------------------------------------
|
2020-04-30 06:44:19 +00:00
|
|
|
let pre_val_count = state.data.validators.len
|
2019-08-28 12:07:00 +00:00
|
|
|
let pre_balance = if validator_index < pre_val_count:
|
2022-05-30 13:30:42 +00:00
|
|
|
state.data.balances.mitem(validator_index)
|
2019-08-28 12:07:00 +00:00
|
|
|
else:
|
|
|
|
0
|
|
|
|
|
|
|
|
# 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
|
|
|
check: process_deposit(defaultRuntimeConfig, state.data, deposit, {}).isOk
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Check invariants
|
|
|
|
# ----------------------------------------
|
|
|
|
check:
|
2020-04-30 06:44:19 +00:00
|
|
|
state.data.validators.len == pre_val_count
|
|
|
|
state.data.balances.len == pre_val_count
|
2022-05-30 13:30:42 +00:00
|
|
|
state.data.balances.item(validator_index) == pre_balance + deposit.data.amount
|
|
|
|
state.data.validators.item(validator_index).effective_balance ==
|
2019-08-28 12:07:00 +00:00
|
|
|
round_multiple_down(
|
2022-05-30 13:30:42 +00:00
|
|
|
min(MAX_EFFECTIVE_BALANCE, state.data.balances.item(validator_index)),
|
2019-08-28 12:07:00 +00:00
|
|
|
EFFECTIVE_BALANCE_INCREMENT
|
|
|
|
)
|
|
|
|
|
2020-07-08 04:55:23 +00:00
|
|
|
template invalid_signature(deposit_amount: uint64, name: string): untyped =
|
2021-04-28 16:41:02 +00:00
|
|
|
test "Invalid deposit " & name & " MAX_EFFECTIVE_BALANCE balance (" &
|
2020-07-08 04:55:23 +00:00
|
|
|
$(MAX_EFFECTIVE_BALANCE div 10'u64^9) & " ETH)":
|
2022-02-20 20:13:06 +00:00
|
|
|
let state = assignClone(genesisState[])
|
2020-07-08 04:55:23 +00:00
|
|
|
|
|
|
|
# Test configuration
|
|
|
|
# ----------------------------------------
|
|
|
|
let validator_index = state.data.validators.len
|
2022-02-20 20:13:06 +00:00
|
|
|
let deposit = mockUpdateStateForNewDeposit(
|
2020-07-08 04:55:23 +00:00
|
|
|
state.data,
|
|
|
|
uint64 validator_index,
|
|
|
|
deposit_amount,
|
|
|
|
flags = {skipBlsValidation}
|
|
|
|
)
|
|
|
|
|
|
|
|
# Params for sanity checks
|
|
|
|
# ----------------------------------------
|
|
|
|
let pre_val_count = state.data.validators.len
|
|
|
|
|
|
|
|
# State transition
|
|
|
|
# ----------------------------------------
|
|
|
|
check:
|
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
|
|
|
process_deposit(defaultRuntimeConfig, state.data, deposit, {}).isOk
|
2020-07-08 04:55:23 +00:00
|
|
|
|
|
|
|
# Check invariants
|
|
|
|
# ----------------------------------------
|
|
|
|
check:
|
|
|
|
state.data.validators.len == pre_val_count
|
|
|
|
state.data.balances.len == pre_val_count
|
|
|
|
|
|
|
|
invalid_signature(MAX_EFFECTIVE_BALANCE, "at")
|
|
|
|
|
2019-08-28 12:07:00 +00:00
|
|
|
# TODO, tests with:
|
|
|
|
# - invalid withdrawal credential
|
|
|
|
# - invalid deposit root
|
|
|
|
# - invalid merkle proof
|