Skip to content

Commit c79b855

Browse files
authored
properly use Electra attestations in Electra attestation tests (#7551)
1 parent 060d27e commit c79b855

File tree

2 files changed

+29
-39
lines changed

2 files changed

+29
-39
lines changed

beacon_chain/consensus_object_pools/attestation_pool.nim

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -680,9 +680,10 @@ iterator electraAttestations*(
680680

681681
type
682682
AttestationCacheKey = (Slot, uint64)
683-
AttestationCache[CVBType] = Table[AttestationCacheKey, CVBType] ##\
684-
## Cache for quick lookup during beacon block construction of attestations
685-
## which have already been included, and therefore should be skipped.
683+
AttestationCache =
684+
Table[AttestationCacheKey, ElectraCommitteeValidatorsBits] ##\
685+
## Cache for quick lookup during beacon block construction of attestations
686+
## which have already been included, and therefore should be skipped.
686687

687688
func getAttestationCacheKey(ad: AttestationData): AttestationCacheKey =
688689
# The committee is unique per slot and committee index which means we can use
@@ -691,7 +692,7 @@ func getAttestationCacheKey(ad: AttestationData): AttestationCacheKey =
691692

692693
func add(
693694
attCache: var AttestationCache, data: AttestationData,
694-
aggregation_bits: CommitteeValidatorsBits | ElectraCommitteeValidatorsBits) =
695+
aggregation_bits: ElectraCommitteeValidatorsBits) =
695696
let key = data.getAttestationCacheKey()
696697
attCache.withValue(key, v) do:
697698
v[].incl(aggregation_bits)
@@ -754,8 +755,8 @@ func score(
754755
func check_attestation_compatible*(
755756
dag: ChainDAGRef,
756757
state: ForkyHashedBeaconState,
757-
attestation: SomeAttestation | electra.Attestation |
758-
electra.TrustedAttestation): Result[void, cstring] =
758+
attestation: electra.Attestation | electra.TrustedAttestation):
759+
Result[void, cstring] =
759760
let
760761
targetEpoch = attestation.data.target.epoch
761762
compatibleRoot = state.dependent_root(targetEpoch.get_previous_epoch)
@@ -806,7 +807,7 @@ proc getAttestationsForBlock*(
806807
candidates: seq[tuple[
807808
score: int, slot: Slot, entry: ptr ElectraAttestationEntry,
808809
validation: int]]
809-
attCache = AttestationCache[ElectraCommitteeValidatorsBits].init(state, cache)
810+
attCache = AttestationCache.init(state, cache)
810811

811812
for i in 0..<ATTESTATION_LOOKBACK:
812813
if i > maxAttestationSlot: # Around genesis..
@@ -868,8 +869,7 @@ proc getAttestationsForBlock*(
868869
#
869870
# For each round, we'll look for the best attestation and add it to the result
870871
# then re-score the other candidates.
871-
var
872-
candidatesPerBlock: OrderedTable[CandidateKey, seq[electra.Attestation]]
872+
var candidatesPerBlock: OrderedTable[CandidateKey, seq[electra.Attestation]]
873873

874874
let totalCandidates = candidates.len()
875875
while candidates.len > 0 and candidatesPerBlock.lenu64() <
@@ -974,9 +974,7 @@ func getElectraAggregatedAttestation*(
974974
pool: var AttestationPool, slot: Slot,
975975
attestationDataRoot: Eth2Digest, committeeIndex: CommitteeIndex):
976976
Opt[electra.Attestation] =
977-
978-
let
979-
candidateIdx = pool.candidateIdx(slot, CandidateIdxType.electraIdx)
977+
let candidateIdx = pool.candidateIdx(slot, CandidateIdxType.electraIdx)
980978
if candidateIdx.isNone:
981979
return Opt.none(electra.Attestation)
982980

tests/test_attestation_pool.nim

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,6 @@ suite "Attestation pool processing" & preset():
175175
check_attestation(forkyState.data, att1, flags, cache2).isOk
176176
check_attestation(forkyState.data, att2, flags, cache2).isOk
177177

178-
# An additional compatibility check catches that (used in block production)
179-
withState(state[]):
180-
check:
181-
check_attestation_compatible(dag, forkyState, att1).isOk
182-
check_attestation_compatible(dag, forkyState, att2).isErr
183-
withState(state2[]):
184-
check:
185-
check_attestation_compatible(dag, forkyState, att1).isErr
186-
check_attestation_compatible(dag, forkyState, att2).isOk
187-
188178
test "Can add and retrieve simple attestations" & preset():
189179
let
190180
# Create an attestation for slot 1!
@@ -468,8 +458,6 @@ suite "Attestation pool processing" & preset():
468458
# signature: ValidatorSig()
469459
)
470460

471-
cache = StateCache()
472-
473461
# -------------------------------------------------------------
474462
# Prune
475463

@@ -569,13 +557,13 @@ suite "Attestation pool electra processing" & preset():
569557
slot = getStateField(state[], slot)
570558
parent_root = withState(state[]): forkyState.latest_block_root
571559
committee = get_beacon_committee(state[], slot, cIndex, cache)
572-
makeAttestation(state[], parent_root, committee[0], cache)
560+
makeElectraAttestation(state[], parent_root, committee[0], cache)
573561
att2 = block:
574562
let
575563
slot = getStateField(state2[], slot)
576564
parent_root = withState(state2[]): forkyState.latest_block_root
577565
committee = get_beacon_committee(state2[], slot, cIndex, cache2)
578-
makeAttestation(state2[], parent_root, committee[0], cache2)
566+
makeElectraAttestation(state2[], parent_root, committee[0], cache2)
579567
maxSlot = max(att1.data.slot, att2.data.slot)
580568

581569
# Advance time so attestations become valid
@@ -589,24 +577,28 @@ suite "Attestation pool electra processing" & preset():
589577

590578
# They should remain valid only within a compatible state
591579
withState(state[]):
592-
check:
593-
check_attestation(forkyState.data, att1, {}, cache).isOk
594-
check_attestation(forkyState.data, att2, {}, cache).isErr
580+
when consensusFork >= ConsensusFork.Electra:
581+
check:
582+
check_attestation(forkyState.data, att1, {}, cache, true).isOk
583+
check_attestation(forkyState.data, att2, {}, cache, true).isErr
595584
withState(state2[]):
596-
check:
597-
check_attestation(forkyState.data, att1, {}, cache2).isErr
598-
check_attestation(forkyState.data, att2, {}, cache2).isOk
585+
when consensusFork >= ConsensusFork.Electra:
586+
check:
587+
check_attestation(forkyState.data, att1, {}, cache2, true).isErr
588+
check_attestation(forkyState.data, att2, {}, cache2, true).isOk
599589

600590
# If signature checks are skipped, state incompatibility is not detected
601-
let flags = {skipBlsValidation}
591+
const flags = {skipBlsValidation}
602592
withState(state[]):
603-
check:
604-
check_attestation(forkyState.data, att1, flags, cache).isOk
605-
check_attestation(forkyState.data, att2, flags, cache).isOk
593+
when consensusFork >= ConsensusFork.Electra:
594+
check:
595+
check_attestation(forkyState.data, att1, flags, cache, true).isOk
596+
check_attestation(forkyState.data, att2, flags, cache, true).isOk
606597
withState(state2[]):
607-
check:
608-
check_attestation(forkyState.data, att1, flags, cache2).isOk
609-
check_attestation(forkyState.data, att2, flags, cache2).isOk
598+
when consensusFork >= ConsensusFork.Electra:
599+
check:
600+
check_attestation(forkyState.data, att1, flags, cache2, true).isOk
601+
check_attestation(forkyState.data, att2, flags, cache2, true).isOk
610602

611603
# An additional compatibility check catches that (used in block production)
612604
withState(state[]):

0 commit comments

Comments
 (0)