2019-08-28 12:07:00 +00:00
|
|
|
# beacon_chain
|
2020-04-10 13:59:17 +00:00
|
|
|
# Copyright (c) 2018-2020 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)
|
2020-10-12 08:59:24 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.0-rc.0/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
|
|
|
|
unittest, math,
|
|
|
|
# Specs
|
2020-07-07 23:02:14 +00:00
|
|
|
../../beacon_chain/spec/[beaconstate, datatypes, crypto, presets],
|
2019-08-28 12:07:00 +00:00
|
|
|
# Internals
|
2019-11-14 10:47:55 +00:00
|
|
|
../../beacon_chain/[ssz, extras],
|
2019-08-28 12:07:00 +00:00
|
|
|
# Mock helpers
|
|
|
|
../mocking/[mock_deposits, mock_genesis],
|
|
|
|
../testutil, ../helpers/math_helpers
|
|
|
|
|
2020-03-10 04:00:19 +00:00
|
|
|
suiteReport "[Unit - Spec - Block processing] Deposits " & preset():
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
const NumValidators = uint64 5 * SLOTS_PER_EPOCH
|
2020-05-28 08:28:14 +00:00
|
|
|
let genesisState = newClone(initGenesisState(NumValidators))
|
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 =
|
2019-12-05 10:27:00 +00:00
|
|
|
timedTest "Deposit " & name & " MAX_EFFECTIVE_BALANCE balance (" &
|
2019-08-28 12:07:00 +00:00
|
|
|
$(MAX_EFFECTIVE_BALANCE div 10'u64^9) & " ETH)":
|
2020-07-01 17:00:14 +00:00
|
|
|
var 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:
|
2020-04-30 06:44:19 +00:00
|
|
|
state.data.balances[validator_index]
|
2019-08-28 12:07:00 +00:00
|
|
|
else:
|
|
|
|
0
|
|
|
|
|
|
|
|
# State transition
|
|
|
|
# ----------------------------------------
|
2020-07-08 12:36:03 +00:00
|
|
|
check: process_deposit(defaultRuntimePreset(), 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
|
|
|
|
state.data.balances[validator_index] == pre_balance + deposit.data.amount
|
|
|
|
state.data.validators[validator_index].effective_balance ==
|
2019-08-28 12:07:00 +00:00
|
|
|
round_multiple_down(
|
2020-04-30 06:44:19 +00:00
|
|
|
min(MAX_EFFECTIVE_BALANCE, state.data.balances[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")
|
|
|
|
|
2019-12-05 10:27:00 +00:00
|
|
|
timedTest "Validator top-up":
|
2020-07-01 17:00:14 +00:00
|
|
|
var 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:
|
2020-04-30 06:44:19 +00:00
|
|
|
state.data.balances[validator_index]
|
2019-08-28 12:07:00 +00:00
|
|
|
else:
|
|
|
|
0
|
|
|
|
|
|
|
|
# State transition
|
|
|
|
# ----------------------------------------
|
2020-07-08 12:36:03 +00:00
|
|
|
check: process_deposit(defaultRuntimePreset(), 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
|
|
|
|
state.data.balances[validator_index] == pre_balance + deposit.data.amount
|
|
|
|
state.data.validators[validator_index].effective_balance ==
|
2019-08-28 12:07:00 +00:00
|
|
|
round_multiple_down(
|
2020-04-30 06:44:19 +00:00
|
|
|
min(MAX_EFFECTIVE_BALANCE, state.data.balances[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 =
|
|
|
|
timedTest "Invalid deposit " & name & " MAX_EFFECTIVE_BALANCE balance (" &
|
|
|
|
$(MAX_EFFECTIVE_BALANCE div 10'u64^9) & " ETH)":
|
|
|
|
var state = assignClone(genesisState[])
|
|
|
|
|
|
|
|
# Test configuration
|
|
|
|
# ----------------------------------------
|
|
|
|
let validator_index = state.data.validators.len
|
|
|
|
var deposit = mockUpdateStateForNewDeposit(
|
|
|
|
state.data,
|
|
|
|
uint64 validator_index,
|
|
|
|
deposit_amount,
|
|
|
|
flags = {skipBlsValidation}
|
|
|
|
)
|
|
|
|
|
|
|
|
# Params for sanity checks
|
|
|
|
# ----------------------------------------
|
|
|
|
let pre_val_count = state.data.validators.len
|
|
|
|
|
|
|
|
# State transition
|
|
|
|
# ----------------------------------------
|
|
|
|
check:
|
2020-07-08 12:36:03 +00:00
|
|
|
process_deposit(defaultRuntimePreset(), 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
|