more speedups

* evaluate block attestations under the epochref of the block - this is
what the state transition function does
* avoid copying attestation seq unnecessarily
* avoid unnecessary hashset for unslashed indices
This commit is contained in:
Jacek Sieka 2020-08-19 11:15:06 +02:00 committed by zah
parent 7de05efaaf
commit 9244ae7a38
4 changed files with 12 additions and 15 deletions

View File

@ -290,8 +290,6 @@ proc process_block*(self: var ForkChoice,
continue
if attestation.data.beacon_block_root in self.backend:
let
epochRef =
dag.getEpochRef(targetBlck, attestation.data.target.epoch)
participants = get_attesting_indices(
epochRef, attestation.data, attestation.aggregation_bits)

View File

@ -595,8 +595,6 @@ proc process_attestation*(
# In the spec, attestation validation is mixed with state mutation, so here
# we've split it into two functions so that the validation logic can be
# reused when looking for suitable blocks to include in attestations.
# TODO don't log warnings when looking for attestations (return
# Result[void, cstring] instead of logging in check_attestation?)
let proposer_index = get_beacon_proposer_index(state, cache)
if proposer_index.isNone:

View File

@ -66,8 +66,8 @@ func get_total_active_balance*(state: BeaconState, cache: var StateCache): Gwei
state, cache.get_shuffled_active_validator_indices(state, epoch))
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/specs/phase0/beacon-chain.md#helper-functions-1
func get_matching_source_attestations(state: BeaconState,
epoch: Epoch): seq[PendingAttestation] =
template get_matching_source_attestations(state: BeaconState,
epoch: Epoch): seq[PendingAttestation] =
doAssert epoch in [get_current_epoch(state), get_previous_epoch(state)]
if epoch == get_current_epoch(state):
state.current_epoch_attestations.asSeq
@ -309,10 +309,11 @@ func get_source_deltas*(
state: BeaconState, total_balance: Gwei, cache: var StateCache):
tuple[a: seq[Gwei], b: seq[Gwei]] =
# Return attester micro-rewards/penalties for source-vote for each validator.
let matching_source_attestations =
get_matching_source_attestations(state, get_previous_epoch(state))
get_attestation_component_deltas(
state, matching_source_attestations, total_balance, cache)
state,
get_matching_source_attestations(state, get_previous_epoch(state)),
total_balance, cache)
func get_target_deltas*(
state: BeaconState, total_balance: Gwei, cache: var StateCache):

View File

@ -31,9 +31,9 @@ func get_attesting_indices*(
func get_unslashed_attesting_indices*(
state: BeaconState, attestations: openArray[PendingAttestation],
cache: var StateCache): HashSet[ValidatorIndex] =
result = get_attesting_indices(state, attestations, cache)
var slashedIndices = initHashSet[ValidatorIndex]()
for index in result:
if state.validators[index].slashed:
slashedIndices.incl index
result.excl slashedIndices
result = initHashSet[ValidatorIndex]()
for a in attestations:
for idx in get_attesting_indices(
state, a.data, a.aggregation_bits, cache):
if not state.validators[idx].slashed:
result.incl idx