mirror of
https://github.com/status-im/nimbus-eth2.git
synced 2025-02-17 00:47:03 +00:00
switch withStateAndBlck
usage to forkyState
; rm unused tests/mocking/
modules (#4206)
This commit is contained in:
parent
86800066e4
commit
0a4aa5fdb3
@ -353,14 +353,14 @@ proc initLightClientUpdateForPeriod(
|
||||
withStateAndBlck(state, bdata):
|
||||
when stateFork >= BeaconStateFork.Altair:
|
||||
update.attested_header = blck.toBeaconBlockHeader()
|
||||
update.next_sync_committee = state.data.next_sync_committee
|
||||
update.next_sync_committee = forkyState.data.next_sync_committee
|
||||
update.next_sync_committee_branch =
|
||||
state.data.build_proof(altair.NEXT_SYNC_COMMITTEE_INDEX).get
|
||||
forkyState.data.build_proof(altair.NEXT_SYNC_COMMITTEE_INDEX).get
|
||||
if finalizedBid.slot == FAR_FUTURE_SLOT:
|
||||
update.finality_branch.reset()
|
||||
else:
|
||||
update.finality_branch =
|
||||
state.data.build_proof(altair.FINALIZED_ROOT_INDEX).get
|
||||
forkyState.data.build_proof(altair.FINALIZED_ROOT_INDEX).get
|
||||
else: raiseAssert "Unreachable"
|
||||
do:
|
||||
dag.handleUnexpectedLightClientError(attestedBid.slot)
|
||||
@ -666,11 +666,12 @@ proc initLightClientDataCache*(dag: ChainDAGRef) =
|
||||
when stateFork >= BeaconStateFork.Altair:
|
||||
# Cache light client data (non-finalized blocks may refer to this)
|
||||
if i != blocks.low:
|
||||
dag.cacheLightClientData(state, bid) # `dag.head` already cached
|
||||
dag.cacheLightClientData(forkyState, bid) # `dag.head` already cached
|
||||
|
||||
# Create `LightClientUpdate` instances
|
||||
if i < blocks.high:
|
||||
dag.createLightClientUpdates(state, blck, parentBid = blocks[i + 1])
|
||||
dag.createLightClientUpdates(
|
||||
forkyState, blck, parentBid = blocks[i + 1])
|
||||
else: raiseAssert "Unreachable"
|
||||
|
||||
let lightClientEndTick = Moment.now()
|
||||
|
@ -532,19 +532,16 @@ template withStateAndBlck*(
|
||||
case s.kind
|
||||
of BeaconStateFork.Bellatrix:
|
||||
const stateFork {.inject.} = BeaconStateFork.Bellatrix
|
||||
template state: untyped {.inject.} = s.bellatrixData
|
||||
template forkyState: untyped {.inject.} = s.bellatrixData
|
||||
template blck: untyped {.inject.} = b.bellatrixData
|
||||
body
|
||||
of BeaconStateFork.Altair:
|
||||
const stateFork {.inject.} = BeaconStateFork.Altair
|
||||
template state: untyped {.inject.} = s.altairData
|
||||
template forkyState: untyped {.inject.} = s.altairData
|
||||
template blck: untyped {.inject.} = b.altairData
|
||||
body
|
||||
of BeaconStateFork.Phase0:
|
||||
const stateFork {.inject.} = BeaconStateFork.Phase0
|
||||
template state: untyped {.inject.} = s.phase0Data
|
||||
template forkyState: untyped {.inject.} = s.phase0Data
|
||||
template blck: untyped {.inject.} = b.phase0Data
|
||||
body
|
||||
|
@ -284,10 +284,13 @@ func collectFromProposerSlashings(
|
||||
forkedBlock: ForkedTrustedSignedBeaconBlock) =
|
||||
withStateAndBlck(forkedState, forkedBlock):
|
||||
for proposer_slashing in blck.message.body.proposer_slashings:
|
||||
doAssert check_proposer_slashing(state.data, proposer_slashing, {}).isOk
|
||||
let slashedIndex = proposer_slashing.signed_header_1.message.proposer_index
|
||||
rewardsAndPenalties.collectFromSlashedValidator(state.data,
|
||||
slashedIndex.ValidatorIndex, blck.message.proposer_index.ValidatorIndex)
|
||||
doAssert check_proposer_slashing(
|
||||
forkyState.data, proposer_slashing, {}).isOk
|
||||
let slashedIndex =
|
||||
proposer_slashing.signed_header_1.message.proposer_index
|
||||
rewardsAndPenalties.collectFromSlashedValidator(
|
||||
forkyState.data, slashedIndex.ValidatorIndex,
|
||||
blck.message.proposer_index.ValidatorIndex)
|
||||
|
||||
func collectFromAttesterSlashings(
|
||||
rewardsAndPenalties: var seq[RewardsAndPenalties],
|
||||
@ -296,11 +299,12 @@ func collectFromAttesterSlashings(
|
||||
withStateAndBlck(forkedState, forkedBlock):
|
||||
for attester_slashing in blck.message.body.attester_slashings:
|
||||
let attester_slashing_validity = check_attester_slashing(
|
||||
state.data, attester_slashing, {})
|
||||
forkyState.data, attester_slashing, {})
|
||||
doAssert attester_slashing_validity.isOk
|
||||
for slashedIndex in attester_slashing_validity.value:
|
||||
rewardsAndPenalties.collectFromSlashedValidator(
|
||||
state.data, slashedIndex, blck.message.proposer_index.ValidatorIndex)
|
||||
forkyState.data, slashedIndex,
|
||||
blck.message.proposer_index.ValidatorIndex)
|
||||
|
||||
func collectFromAttestations(
|
||||
rewardsAndPenalties: var seq[RewardsAndPenalties],
|
||||
@ -311,24 +315,26 @@ func collectFromAttestations(
|
||||
withStateAndBlck(forkedState, forkedBlock):
|
||||
when stateFork > BeaconStateFork.Phase0:
|
||||
let base_reward_per_increment = get_base_reward_per_increment(
|
||||
get_total_active_balance(state.data, cache))
|
||||
get_total_active_balance(forkyState.data, cache))
|
||||
doAssert base_reward_per_increment > 0
|
||||
for attestation in blck.message.body.attestations:
|
||||
doAssert check_attestation(state.data, attestation, {}, cache).isOk
|
||||
doAssert check_attestation(
|
||||
forkyState.data, attestation, {}, cache).isOk
|
||||
let proposerReward =
|
||||
if attestation.data.target.epoch == get_current_epoch(state.data):
|
||||
if attestation.data.target.epoch == get_current_epoch(forkyState.data):
|
||||
get_proposer_reward(
|
||||
state.data, attestation, base_reward_per_increment, cache,
|
||||
forkyState.data, attestation, base_reward_per_increment, cache,
|
||||
epochParticipationFlags.currentEpochParticipation)
|
||||
else:
|
||||
get_proposer_reward(
|
||||
state.data, attestation, base_reward_per_increment, cache,
|
||||
forkyState.data, attestation, base_reward_per_increment, cache,
|
||||
epochParticipationFlags.previousEpochParticipation)
|
||||
rewardsAndPenalties[blck.message.proposer_index].proposer_outcome +=
|
||||
proposerReward.int64
|
||||
let inclusionDelay = state.data.slot - attestation.data.slot
|
||||
let inclusionDelay = forkyState.data.slot - attestation.data.slot
|
||||
for index in get_attesting_indices(
|
||||
state.data, attestation.data, attestation.aggregation_bits, cache):
|
||||
forkyState.data, attestation.data, attestation.aggregation_bits,
|
||||
cache):
|
||||
rewardsAndPenalties[index].inclusion_delay = some(inclusionDelay.uint64)
|
||||
|
||||
proc collectFromDeposits(
|
||||
@ -341,7 +347,7 @@ proc collectFromDeposits(
|
||||
for deposit in blck.message.body.deposits:
|
||||
let pubkey = deposit.data.pubkey
|
||||
let amount = deposit.data.amount
|
||||
var index = findValidatorIndex(state.data, pubkey)
|
||||
var index = findValidatorIndex(forkyState.data, pubkey)
|
||||
if index.isNone:
|
||||
if pubkey in pubkeyToIndex:
|
||||
index = Opt[ValidatorIndex].ok(pubkeyToIndex[pubkey])
|
||||
@ -359,17 +365,20 @@ func collectFromSyncAggregate(
|
||||
cache: var StateCache) =
|
||||
withStateAndBlck(forkedState, forkedBlock):
|
||||
when stateFork > BeaconStateFork.Phase0:
|
||||
let total_active_balance = get_total_active_balance(state.data, cache)
|
||||
let participant_reward = get_participant_reward(total_active_balance)
|
||||
let proposer_reward =
|
||||
state_transition_block.get_proposer_reward(participant_reward)
|
||||
let indices = get_sync_committee_cache(state.data, cache).current_sync_committee
|
||||
let
|
||||
total_active_balance = get_total_active_balance(forkyState.data, cache)
|
||||
participant_reward = get_participant_reward(total_active_balance)
|
||||
proposer_reward =
|
||||
state_transition_block.get_proposer_reward(participant_reward)
|
||||
indices = get_sync_committee_cache(
|
||||
forkyState.data, cache).current_sync_committee
|
||||
|
||||
template aggregate: untyped = blck.message.body.sync_aggregate
|
||||
|
||||
doAssert indices.len == SYNC_COMMITTEE_SIZE
|
||||
doAssert aggregate.sync_committee_bits.len == SYNC_COMMITTEE_SIZE
|
||||
doAssert state.data.current_sync_committee.pubkeys.len == SYNC_COMMITTEE_SIZE
|
||||
doAssert forkyState.data.current_sync_committee.pubkeys.len ==
|
||||
SYNC_COMMITTEE_SIZE
|
||||
|
||||
for i in 0 ..< SYNC_COMMITTEE_SIZE:
|
||||
rewardsAndPenalties[indices[i]].max_sync_committee_reward +=
|
||||
|
@ -1,90 +0,0 @@
|
||||
# beacon_chain
|
||||
# Copyright (c) 2018-2022 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.
|
||||
|
||||
import
|
||||
# Beacon chain internals
|
||||
../../beacon_chain/spec/
|
||||
[forks, helpers, signatures, state_transition, validator],
|
||||
../../beacon_chain/spec/datatypes/[phase0, altair, bellatrix],
|
||||
# Test utilities
|
||||
../testblockutil
|
||||
|
||||
# Routines for mocking blocks
|
||||
# ---------------------------------------------------------------
|
||||
|
||||
# https://github.com/ethereum/consensus-specs/blob/v1.2.0/tests/core/pyspec/eth2spec/test/helpers/block.py#L26-L35
|
||||
func apply_randao_reveal(state: ForkyBeaconState, blck: var ForkySignedBeaconBlock) =
|
||||
doAssert state.slot <= blck.message.slot
|
||||
let
|
||||
proposer_index = blck.message.proposer_index.ValidatorIndex
|
||||
privkey = MockPrivKeys[proposer_index]
|
||||
|
||||
blck.message.body.randao_reveal = get_epoch_signature(
|
||||
state.fork,
|
||||
state.genesis_validators_root,
|
||||
blck.message.slot.epoch,
|
||||
privkey).toValidatorSig()
|
||||
|
||||
# https://github.com/ethereum/consensus-specs/blob/v1.2.0/tests/core/pyspec/eth2spec/test/helpers/block.py#L38-L54
|
||||
func sign_block(state: ForkyBeaconState, blck: var ForkySignedBeaconBlock) =
|
||||
let
|
||||
proposer_index = blck.message.proposer_index.ValidatorIndex
|
||||
privkey = MockPrivKeys[proposer_index]
|
||||
|
||||
blck.root = blck.message.hash_tree_root()
|
||||
blck.signature = get_block_signature(
|
||||
state.fork,
|
||||
state.genesis_validators_root,
|
||||
blck.message.slot,
|
||||
blck.root,
|
||||
privkey).toValidatorSig()
|
||||
|
||||
# https://github.com/ethereum/consensus-specs/blob/v1.2.0/tests/core/pyspec/eth2spec/test/helpers/block.py#L75-L104
|
||||
proc mockBlock*(
|
||||
state: ForkedHashedBeaconState,
|
||||
slot: Slot,
|
||||
cfg = defaultRuntimeConfig): ForkedSignedBeaconBlock =
|
||||
## TODO don't do this gradual construction, for exception safety
|
||||
## Mock a BeaconBlock for the specific slot
|
||||
var
|
||||
cache = StateCache()
|
||||
tmpState = assignClone(state)
|
||||
if getStateField(state, slot) != slot:
|
||||
var info = ForkedEpochInfo()
|
||||
process_slots(cfg, tmpState[], slot, cache, info, flags = {}).expect("no failure")
|
||||
|
||||
result.kind = case tmpState[].kind
|
||||
of BeaconStateFork.Phase0: BeaconBlockFork.Phase0
|
||||
of BeaconStateFork.Altair: BeaconBlockFork.Altair
|
||||
of BeaconStateFork.Bellatrix: BeaconBlockFork.Bellatrix
|
||||
withStateAndBlck(tmpState[], result):
|
||||
blck.message.slot = slot
|
||||
blck.message.proposer_index =
|
||||
get_beacon_proposer_index(state.data, cache, slot).get.uint64
|
||||
blck.message.body.eth1_data.deposit_count = state.data.eth1_deposit_index
|
||||
blck.message.parent_root = block:
|
||||
var previous_block_header = state.data.latest_block_header
|
||||
if previous_block_header.state_root == ZERO_HASH:
|
||||
previous_block_header.state_root = state.data.hash_tree_root()
|
||||
previous_block_header.hash_tree_root()
|
||||
|
||||
apply_randao_reveal(state.data, blck)
|
||||
|
||||
when stateFork >= BeaconStateFork.Altair:
|
||||
blck.message.body.sync_aggregate = SyncAggregate.init()
|
||||
|
||||
when stateFork >= BeaconStateFork.Bellatrix:
|
||||
blck.message.body.execution_payload =
|
||||
build_empty_execution_payload(state.data)
|
||||
|
||||
sign_block(state.data, blck)
|
||||
|
||||
# https://github.com/ethereum/consensus-specs/blob/v1.2.0/tests/core/pyspec/eth2spec/test/helpers/block.py#L107-L108
|
||||
proc mockBlockForNextSlot*(
|
||||
state: ForkedHashedBeaconState): ForkedSignedBeaconBlock =
|
||||
## Mock a BeaconBlock for the next slot
|
||||
mockBlock(state, getStateField(state, slot) + 1)
|
@ -1,33 +0,0 @@
|
||||
# beacon_chain
|
||||
# Copyright (c) 2018-2021 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.
|
||||
|
||||
# Mocking helpers for BeaconState
|
||||
# ---------------------------------------------------------------
|
||||
|
||||
import
|
||||
# Specs
|
||||
../../beacon_chain/spec/[forks, presets, state_transition],
|
||||
../../beacon_chain/spec/datatypes/base
|
||||
|
||||
proc nextEpoch*(state: var ForkedHashedBeaconState) =
|
||||
## Transition to the start of the next epoch
|
||||
var
|
||||
cache = StateCache()
|
||||
info = ForkedEpochInfo()
|
||||
let slot =
|
||||
getStateField(state, slot) + SLOTS_PER_EPOCH -
|
||||
(getStateField(state, slot) mod SLOTS_PER_EPOCH)
|
||||
doAssert process_slots(defaultRuntimeConfig, state, slot, cache, info, {})
|
||||
|
||||
proc nextSlot*(state: var ForkedHashedBeaconState) =
|
||||
## Transition to the next slot
|
||||
var
|
||||
cache = StateCache()
|
||||
info = ForkedEpochInfo()
|
||||
|
||||
doAssert process_slots(
|
||||
defaultRuntimeConfig, state, getStateField(state, slot) + 1, cache, info, {})
|
Loading…
x
Reference in New Issue
Block a user