Skip to content

Commit 58a34e0

Browse files
authored
fix inconsistent aggregation bits len in Electra (#6679)
1 parent 8a6eab7 commit 58a34e0

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

beacon_chain/spec/validator.nim

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -565,21 +565,32 @@ func get_committee_index_one*(bits: AttestationCommitteeBits): Opt[CommitteeInde
565565

566566
proc compute_on_chain_aggregate*(
567567
network_aggregates: openArray[electra.Attestation]): Opt[electra.Attestation] =
568-
# aggregates = sorted(network_aggregates, key=lambda a: get_committee_indices(a.committee_bits)[0])
569-
let aggregates = network_aggregates.sortedByIt(it.committee_bits.get_committee_index_one().expect("just one"))
570-
571-
let data = aggregates[0].data
572-
573-
var agg: AggregateSignature
574-
var committee_bits: AttestationCommitteeBits
568+
let
569+
aggregates = network_aggregates.sortedByIt(
570+
it.committee_bits.get_committee_index_one().expect("just one"))
571+
data = aggregates[0].data
575572

576-
var totalLen = 0
573+
var
574+
agg: AggregateSignature
575+
committee_bits: AttestationCommitteeBits
576+
prev_committee_index: Opt[CommitteeIndex]
577+
totalLen = 0
577578
for i, a in aggregates:
579+
let committee_index = ? get_committee_index_one(a.committee_bits)
580+
if prev_committee_index.isNone:
581+
prev_committee_index = Opt.some committee_index
582+
elif committee_index.distinctBase <= prev_committee_index.get.distinctBase:
583+
continue
584+
prev_committee_index = Opt.some committee_index
585+
578586
totalLen += a.aggregation_bits.len
579587

580-
var aggregation_bits = ElectraCommitteeValidatorsBits.init(totalLen)
581-
var pos = 0
582-
var prev_committee_index: Opt[CommitteeIndex]
588+
prev_committee_index.reset()
589+
590+
var
591+
aggregation_bits = ElectraCommitteeValidatorsBits.init(totalLen)
592+
pos = 0
593+
filledLen = 0
583594
for i, a in aggregates:
584595
let
585596
committee_index = ? get_committee_index_one(a.committee_bits)
@@ -594,6 +605,7 @@ proc compute_on_chain_aggregate*(
594605
for b in a.aggregation_bits:
595606
aggregation_bits[pos] = b
596607
pos += 1
608+
filledLen += a.aggregation_bits.len
597609

598610
let sig = ? a.signature.load() # Expensive
599611
if first:
@@ -603,6 +615,8 @@ proc compute_on_chain_aggregate*(
603615

604616
committee_bits[int(committee_index)] = true
605617

618+
doAssert totalLen == filledLen
619+
606620
let signature = agg.finish()
607621

608622
ok electra.Attestation(

tests/test_attestation_pool.nim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,8 @@ suite "Attestation pool electra processing" & preset():
961961

962962
check:
963963
verifyAttestationSignature(attestations[0])
964+
check_attestation(
965+
state[].electraData.data, attestations[0], {}, cache, true).isOk
964966

965967
# A single final chain aggregated attestation should be created
966968
# with same data, 2 committee bits and 3 aggregation bits
@@ -1048,6 +1050,8 @@ suite "Attestation pool electra processing" & preset():
10481050
check:
10491051
attestations.len() == 1
10501052
attestations[0].aggregation_bits.countOnes() == 3
1053+
check_attestation(
1054+
state[].electraData.data, attestations[0], {}, cache, true).isOk
10511055
verifyAttestationSignature(attestations[0])
10521056
# Can get either aggregate here, random!
10531057
verifyAttestationSignature(pool[].getElectraAggregatedAttestation(
@@ -1063,6 +1067,8 @@ suite "Attestation pool electra processing" & preset():
10631067
check:
10641068
attestations.len() == 1
10651069
attestations[0].aggregation_bits.countOnes() == 4
1070+
check_attestation(
1071+
state[].electraData.data, attestations[0], {}, cache, true).isOk
10661072
verifyAttestationSignature(attestations[0])
10671073
verifyAttestationSignature(pool[].getElectraAggregatedAttestation(
10681074
1.Slot, hash_tree_root(attestations[0].data), 0.CommitteeIndex).get)

0 commit comments

Comments
 (0)