default preset update; rm process_slots(BeaconState); some BLS skipping disabled (#963)

* re-enable BLS validation for block sanity tests

* update default preset to mainnet; remove process_slots(state: var BeaconState)

* bump spec ref to v0.11.1

* bump all v0.11.1 spec references except the actually changed is_valid_indexed_attestation(...) in spec/beaconstate to v0.11.2

* remove stack-risky/low-ROI doAssert
This commit is contained in:
tersec 2020-05-04 21:07:18 +00:00 committed by GitHub
parent 01e9df97cb
commit c548b90a0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 34 additions and 35 deletions

View File

@ -14,7 +14,7 @@ import
./crypto, ./datatypes, ./digest, ./helpers, ./validator,
../../nbench/bench_lab
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/beacon-chain.md#is_valid_merkle_branch
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.2/specs/phase0/beacon-chain.md#is_valid_merkle_branch
func is_valid_merkle_branch*(leaf: Eth2Digest, branch: openarray[Eth2Digest], depth: uint64, index: uint64, root: Eth2Digest): bool {.nbench.}=
## Check if ``leaf`` at ``index`` verifies against the Merkle ``root`` and
## ``branch``.
@ -32,13 +32,13 @@ func is_valid_merkle_branch*(leaf: Eth2Digest, branch: openarray[Eth2Digest], de
value = eth2hash(buf)
value == root
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/beacon-chain.md#increase_balance
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.2/specs/phase0/beacon-chain.md#increase_balance
func increase_balance*(
state: var BeaconState, index: ValidatorIndex, delta: Gwei) =
# Increase the validator balance at index ``index`` by ``delta``.
state.balances[index] += delta
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/beacon-chain.md#decrease_balance
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.2/specs/phase0/beacon-chain.md#decrease_balance
func decrease_balance*(
state: var BeaconState, index: ValidatorIndex, delta: Gwei) =
## Decrease the validator balance at index ``index`` by ``delta``, with
@ -49,7 +49,7 @@ func decrease_balance*(
else:
state.balances[index] - delta
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/beacon-chain.md#deposits
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.2/specs/phase0/beacon-chain.md#deposits
proc process_deposit*(
state: var BeaconState, deposit: Deposit, flags: UpdateFlags = {}): bool {.nbench.}=
# Process an Eth1 deposit, registering a validator or increasing its balance.
@ -109,13 +109,13 @@ proc process_deposit*(
true
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/beacon-chain.md#compute_activation_exit_epoch
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.2/specs/phase0/beacon-chain.md#compute_activation_exit_epoch
func compute_activation_exit_epoch(epoch: Epoch): Epoch =
## Return the epoch during which validator activations and exits initiated in
## ``epoch`` take effect.
epoch + 1 + MAX_SEED_LOOKAHEAD
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/beacon-chain.md#get_validator_churn_limit
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.2/specs/phase0/beacon-chain.md#get_validator_churn_limit
func get_validator_churn_limit(state: BeaconState): uint64 =
# Return the validator churn limit for the current epoch.
let active_validator_indices =
@ -123,7 +123,7 @@ func get_validator_churn_limit(state: BeaconState): uint64 =
max(MIN_PER_EPOCH_CHURN_LIMIT,
len(active_validator_indices) div CHURN_LIMIT_QUOTIENT).uint64
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/beacon-chain.md#initiate_validator_exit
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.2/specs/phase0/beacon-chain.md#initiate_validator_exit
func initiate_validator_exit*(state: var BeaconState,
index: ValidatorIndex) =
# Initiate the exit of the validator with index ``index``.
@ -195,7 +195,7 @@ proc slash_validator*(state: var BeaconState, slashed_index: ValidatorIndex,
increase_balance(
state, whistleblower_index, whistleblowing_reward - proposer_reward)
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/beacon-chain.md#genesis
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.2/specs/phase0/beacon-chain.md#genesis
proc initialize_beacon_state_from_eth1*(
eth1_block_hash: Eth2Digest,
eth1_timestamp: uint64,
@ -297,7 +297,7 @@ func get_initial_beacon_block*(state: BeaconState): SignedBeaconBlock =
# parent_root, randao_reveal, eth1_data, signature, and body automatically
# initialized to default values.
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/beacon-chain.md#get_block_root_at_slot
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.2/specs/phase0/beacon-chain.md#get_block_root_at_slot
func get_block_root_at_slot*(state: BeaconState,
slot: Slot): Eth2Digest =
# Return the block root at a recent ``slot``.
@ -306,12 +306,12 @@ func get_block_root_at_slot*(state: BeaconState,
doAssert slot < state.slot
state.block_roots[slot mod SLOTS_PER_HISTORICAL_ROOT]
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/beacon-chain.md#get_block_root
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.2/specs/phase0/beacon-chain.md#get_block_root
func get_block_root*(state: BeaconState, epoch: Epoch): Eth2Digest =
# Return the block root at the start of a recent ``epoch``.
get_block_root_at_slot(state, compute_start_slot_at_epoch(epoch))
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/beacon-chain.md#get_total_balance
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.2/specs/phase0/beacon-chain.md#get_total_balance
func get_total_balance*(state: BeaconState, validators: auto): Gwei =
## Return the combined effective balance of the ``indices``.
## ``EFFECTIVE_BALANCE_INCREMENT`` Gwei minimum to avoid divisions by zero.
@ -322,13 +322,13 @@ func get_total_balance*(state: BeaconState, validators: auto): Gwei =
# XXX: Move to state_transition_epoch.nim?
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/beacon-chain.md#is_eligible_for_activation_queue
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.2/specs/phase0/beacon-chain.md#is_eligible_for_activation_queue
func is_eligible_for_activation_queue(validator: Validator): bool =
# Check if ``validator`` is eligible to be placed into the activation queue.
validator.activation_eligibility_epoch == FAR_FUTURE_EPOCH and
validator.effective_balance == MAX_EFFECTIVE_BALANCE
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/beacon-chain.md#is_eligible_for_activation
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.2/specs/phase0/beacon-chain.md#is_eligible_for_activation
func is_eligible_for_activation(state: BeaconState, validator: Validator):
bool =
# Check if ``validator`` is eligible for activation.
@ -338,7 +338,7 @@ func is_eligible_for_activation(state: BeaconState, validator: Validator):
# Has not yet been activated
validator.activation_epoch == FAR_FUTURE_EPOCH
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/beacon-chain.md#registry-updates
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.2/specs/phase0/beacon-chain.md#registry-updates
proc process_registry_updates*(state: var BeaconState) {.nbench.}=
## Process activation eligibility and ejections
## Try to avoid caching here, since this could easily become undefined
@ -423,7 +423,7 @@ proc is_valid_indexed_attestation*(
true
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/beacon-chain.md#get_attesting_indices
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.2/specs/phase0/beacon-chain.md#get_attesting_indices
func get_attesting_indices*(state: BeaconState,
data: AttestationData,
bits: CommitteeValidatorsBits,
@ -437,7 +437,7 @@ func get_attesting_indices*(state: BeaconState,
# This shouldn't happen if one begins with a valid BeaconState and applies
# valid updates, but one can construct a BeaconState where it does. Do not
# do anything here since the PendingAttestation wouldn't have made it past
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/beacon-chain.md#attestations
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.2/specs/phase0/beacon-chain.md#attestations
# which checks len(attestation.aggregation_bits) == len(committee) that in
# nim-beacon-chain lives in check_attestation(...).
# Addresses https://github.com/status-im/nim-beacon-chain/issues/922
@ -465,7 +465,7 @@ func get_indexed_attestation*(state: BeaconState, attestation: Attestation,
signature: attestation.signature
)
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/beacon-chain.md#attestations
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.2/specs/phase0/beacon-chain.md#attestations
proc check_attestation*(
state: BeaconState, attestation: Attestation, flags: UpdateFlags,
stateCache: var StateCache): bool =
@ -596,7 +596,7 @@ func makeAttestationData*(
doAssert slot.compute_epoch_at_slot == current_epoch
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/validator.md#attestation-data
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.2/specs/phase0/validator.md#attestation-data
AttestationData(
slot: slot,
index: committee_index,

View File

@ -45,7 +45,7 @@ import
# Constant presets
const const_preset* {.strdefine.} = "minimal"
const const_preset* {.strdefine.} = "mainnet"
when const_preset == "mainnet":
import ./presets/mainnet

View File

@ -173,12 +173,6 @@ proc process_slots*(state: var HashedBeaconState, slot: Slot) {.nbench.} =
while state.data.slot < slot:
advance_slot(state, err(Opt[Eth2Digest]))
# TODO remove this once callers gone
proc process_slots*(state: var BeaconState, slot: Slot) {.deprecated: "Use HashedBeaconState version".} =
var hashedState = HashedBeaconState(data: state, root: hash_tree_root(state))
process_slots(hashedState, slot)
state = hashedState.data
proc noRollback*(state: var HashedBeaconState) =
trace "Skipping rollback of broken state"

View File

@ -12,7 +12,7 @@ import
os, sequtils, unittest,
# Beacon chain internals
../../beacon_chain/spec/[crypto, datatypes],
../../beacon_chain/[ssz, state_transition, extras],
../../beacon_chain/[ssz, state_transition],
# Test utilities
../testutil,
./fixtures_utils
@ -46,9 +46,8 @@ proc runTest(identifier: string) =
let blck = parseTest(testDir/"blocks_" & $i & ".ssz", SSZ, SignedBeaconBlock)
if hasPostState:
# TODO: The EF is using invalid BLS keys so we can't verify them
let success = state_transition(
hashedPreState, blck, flags = {skipBlsValidation}, noRollback)
hashedPreState, blck, flags = {}, noRollback)
doAssert success, "Failure when applying block " & $i
else:
let success = state_transition(

View File

@ -6,7 +6,7 @@
# at your option. This file may not be copied, modified, or distributed except according to those terms.
# process_attestation (beaconstate.nim)
# https://github.com/ethereum/eth2.0-specs/blob/v0.9.4/specs/core/0_beacon-chain.md#attestations
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/beacon-chain.md#attestations
# ---------------------------------------------------------------
{.used.}

View File

@ -94,12 +94,16 @@ when const_preset == "minimal": # Too much stack space used on mainnet
var
db = makeTestDB(SLOTS_PER_EPOCH)
pool = BlockPool.init(db)
state = newClone(pool.loadTailState().data.data)
b1 = addTestBlock(state[], pool.tail.root)
state = newClone(pool.loadTailState().data)
b1 = addTestBlock(state.data, pool.tail.root)
b1Root = hash_tree_root(b1.message)
b2 = addTestBlock(state[], b1Root)
b2 = addTestBlock(state.data, b1Root)
b2Root {.used.} = hash_tree_root(b2.message)
# addTestBlock(...) operates on BeaconState, so doesn't update root
# TODO fix addTestBlock to work on HashedBeaconState directly
state.root = hash_tree_root(state.data)
timedTest "getRef returns nil for missing blocks":
check:
pool.getRef(default Eth2Digest) == nil
@ -135,12 +139,14 @@ when const_preset == "minimal": # Too much stack space used on mainnet
pool.heads[0].blck == b2Add
# Skip one slot to get a gap
process_slots(state[], state.slot + 1)
process_slots(state[], state.data.slot + 1)
let
b4 = addTestBlock(state[], b2Root)
b4 = addTestBlock(state.data, b2Root)
b4Root = hash_tree_root(b4.message)
b4Add = pool.add(b4Root, b4)
# TODO fix addTestBlock to work on HashedBeaconState
state.root = hash_tree_root(state.data)
check:
b4Add.parent == b2Add
@ -343,7 +349,7 @@ when const_preset == "minimal": # Too much stack space used on mainnet
pool.updateHead(added)
# Advance past epoch so that the epoch transition is gapped
process_slots(pool.headState.data.data, Slot(SLOTS_PER_EPOCH * 6 + 2) )
process_slots(pool.headState.data, Slot(SLOTS_PER_EPOCH * 6 + 2) )
var blck = makeTestBlock(
pool.headState.data.data, pool.head.blck.root,