Skip to content

Commit

Permalink
Correct attestation aggregation while producing block (#11388)
Browse files Browse the repository at this point in the history
The `aggregationBitsByAttestationData ` was not correctly utilized.
Maybe it works but a bit implicitly, so trying to fix it
  • Loading branch information
domiwei authored Jul 29, 2024
1 parent 9337209 commit 7abf606
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
25 changes: 14 additions & 11 deletions cl/beacon/handler/block_production.go
Original file line number Diff line number Diff line change
Expand Up @@ -1080,36 +1080,39 @@ func (a *ApiHandler) findBestAttestationsForBlockProduction(
sort.Slice(attestationCandidates, func(i, j int) bool {
return attestationCandidates[i].reward > attestationCandidates[j].reward
})

// Some aggregates can be supersets of existing ones so let's filter out the supersets
// this MAP is HashTreeRoot(AttestationData) => AggregationBits
aggregationBitsByAttestationData := make(map[libcommon.Hash][]byte)
hashToMergedAtt := make(map[libcommon.Hash]*solid.Attestation)
for _, candidate := range attestationCandidates {
// Check if it is a superset of a pre-included attestation with higher reward
attestationDataRoot, err := candidate.attestation.AttestantionData().HashSSZ()
if err != nil {
log.Warn("[Block Production] Cannot compute attestation data root", "err", err)
continue
}
currAggregationBits, exists := aggregationBitsByAttestationData[attestationDataRoot]
if exists {
if utils.IsNonStrictSupersetBitlist(
if curAtt, exists := hashToMergedAtt[attestationDataRoot]; exists {
currAggregationBits := curAtt.AggregationBits()
if !utils.IsNonStrictSupersetBitlist(
currAggregationBits,
candidate.attestation.AggregationBits(),
) {
continue
// merge if not a superset
utils.MergeBitlists(currAggregationBits, candidate.attestation.AggregationBits())
curAtt.SetAggregationBits(currAggregationBits)
}
utils.MergeBitlists(currAggregationBits, candidate.attestation.AggregationBits())
} else {
currAggregationBits = candidate.attestation.AggregationBits()
// Update the currently built superset
hashToMergedAtt[attestationDataRoot] = candidate.attestation.Copy()
}
// Update the currently built superset
aggregationBitsByAttestationData[attestationDataRoot] = currAggregationBits

ret.Append(candidate.attestation)
if ret.Len() >= int(a.beaconChainCfg.MaxAttestations) {
if len(hashToMergedAtt) >= int(a.beaconChainCfg.MaxAttestations) {
break
}
}
for _, att := range hashToMergedAtt {
ret.Append(att)
}
return ret
}

Expand Down
8 changes: 6 additions & 2 deletions cl/cltypes/solid/attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,16 @@ func (a *Attestation) UnmarshalJSON(buf []byte) error {

// AggregationBits returns the aggregation bits buffer of the Attestation instance.
func (a *Attestation) AggregationBits() []byte {
return a.aggregationBitsBuffer
buf := make([]byte, len(a.aggregationBitsBuffer))
copy(buf, a.aggregationBitsBuffer)
return buf
}

// SetAggregationBits sets the aggregation bits buffer of the Attestation instance.
func (a *Attestation) SetAggregationBits(bits []byte) {
a.aggregationBitsBuffer = bits
buf := make([]byte, len(bits))
copy(buf, bits)
a.aggregationBitsBuffer = buf
}

// AttestantionData returns the attestation data of the Attestation instance.
Expand Down

0 comments on commit 7abf606

Please sign in to comment.