|
|
|
@ -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,
|
|
|
|
|