Skip to content

Commit

Permalink
extract Electra logic and add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rkapka committed Oct 25, 2024
1 parent 3402608 commit 5f27d17
Showing 1 changed file with 47 additions and 24 deletions.
71 changes: 47 additions & 24 deletions beacon-chain/rpc/prysm/v1alpha1/validator/proposer_attestations.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,34 +86,14 @@ func (vs *Server) packAttestations(ctx context.Context, latestState state.Beacon
if err != nil {
return nil, err
}
attsById[id], err = proposerAtts(as).sort()
if err != nil {
return nil, err
}
attsById[id] = as
}

var attsForInclusion proposerAtts
if postElectra {
idx := 0
for {
topAggregates := make([]ethpb.Att, 0, len(attsById))
for _, v := range attsById {
if len(v) > idx {
topAggregates = append(topAggregates, v[idx])
}
}

if len(topAggregates) == 0 {
break
}

onChainAggregates, err := computeOnChainAggregate(topAggregates)
if err != nil {
return nil, err
}
attsForInclusion = append(attsForInclusion, onChainAggregates...)

idx++
attsForInclusion, err = onChainAggregates(attsById)
if err != nil {
return nil, err
}
} else {
attsForInclusion = make([]ethpb.Att, 0)
Expand Down Expand Up @@ -144,6 +124,49 @@ func (vs *Server) packAttestations(ctx context.Context, latestState state.Beacon
return vs.filterAttestationBySignature(ctx, atts, latestState)
}

func onChainAggregates(attsById map[attestation.Id][]ethpb.Att) (proposerAtts, error) {
var result proposerAtts
var err error

// When constructing on-chain aggregates, we want to combine the most profitable
// aggregate for each ID, then the second most profitable, and so on and so forth.
// Because of this we sort attestations at the beginning.
for id, as := range attsById {
attsById[id], err = proposerAtts(as).sort()
if err != nil {
return nil, err
}
}

// We construct the first on-chain aggregate by taking the first aggregate for each ID.
// We construct the second on-chain aggregate by taking the second aggregate for each ID.
idx := 0
for {
topAggregates := make([]ethpb.Att, 0, len(attsById))
for _, as := range attsById {
// In case there are no more aggregates for an ID, we skip that ID.
if len(as) > idx {
topAggregates = append(topAggregates, as[idx])
}
}

// Once there are no more aggregates for any ID, we are done.
if len(topAggregates) == 0 {
break
}

onChainAggs, err := computeOnChainAggregate(topAggregates)
if err != nil {
return nil, err
}
result = append(result, onChainAggs...)

idx++
}

return result, nil
}

// filter separates attestation list into two groups: valid and invalid attestations.
// The first group passes the all the required checks for attestation to be considered for proposing.
// And attestations from the second group should be deleted.
Expand Down

0 comments on commit 5f27d17

Please sign in to comment.