Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Optimized Attestation Selection and Aggregation Strategy #9186

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.IntStream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.bls.BLSSignature;
import tech.pegasys.teku.ethereum.performance.trackers.BlockProductionPerformance;
Expand Down Expand Up @@ -70,8 +72,10 @@
import tech.pegasys.teku.statetransition.attestation.AttestationForkChecker;
import tech.pegasys.teku.statetransition.forkchoice.ForkChoiceNotifier;
import tech.pegasys.teku.statetransition.synccommittee.SyncCommitteeContributionPool;
import tech.pegasys.teku.validator.coordinator.OptimizedAttestationAggregationStrategy;

public class BlockOperationSelectorFactory {
private static final Logger LOG = LogManager.getLogger();
private final Spec spec;
private final AggregatingAttestationPool attestationPool;
private final OperationPool<AttesterSlashing> attesterSlashingPool;
Expand All @@ -84,6 +88,7 @@ public class BlockOperationSelectorFactory {
private final GraffitiBuilder graffitiBuilder;
private final ForkChoiceNotifier forkChoiceNotifier;
private final ExecutionLayerBlockProductionManager executionLayerBlockProductionManager;
private final OptimizedAttestationAggregationStrategy attestationAggregationStrategy;

public BlockOperationSelectorFactory(
final Spec spec,
Expand All @@ -110,6 +115,7 @@ public BlockOperationSelectorFactory(
this.graffitiBuilder = graffitiBuilder;
this.forkChoiceNotifier = forkChoiceNotifier;
this.executionLayerBlockProductionManager = executionLayerBlockProductionManager;
this.attestationAggregationStrategy = new OptimizedAttestationAggregationStrategy(spec, attestationPool);
}

public Function<BeaconBlockBodyBuilder, SafeFuture<Void>> createSelector(
Expand All @@ -121,11 +127,23 @@ public Function<BeaconBlockBodyBuilder, SafeFuture<Void>> createSelector(
final BlockProductionPerformance blockProductionPerformance) {

return bodyBuilder -> {
final UInt64 slot = blockSlotState.getSlot();
final SchemaDefinitions schemaDefinitions = spec.atSlot(slot).getSchemaDefinitions();

// Set randao reveal
bodyBuilder.randaoReveal(randaoReveal);

// Set eth1 data
final Eth1Data eth1Data = eth1DataCache.getEth1Vote(blockSlotState);
bodyBuilder.eth1Data(eth1Data);

// Set graffiti
final Bytes32 graffiti = graffitiBuilder.buildGraffiti(optionalGraffiti);
bodyBuilder.graffiti(graffiti);

final SszList<Attestation> attestations =
attestationPool.getAttestationsForBlock(
blockSlotState, new AttestationForkChecker(spec, blockSlotState));
// Set attestations
final List<Attestation> attestations = selectOptimizedAttestations(blockSlotState);
bodyBuilder.attestations(attestations);

// Collect slashings to include
final Set<UInt64> exitedValidators = new HashSet<>();
Expand All @@ -151,10 +169,6 @@ public Function<BeaconBlockBodyBuilder, SafeFuture<Void>> createSelector(
exit -> exitedValidators.add(exit.getMessage().getValidatorIndex()));

bodyBuilder
.randaoReveal(randaoReveal)
.eth1Data(eth1Data)
.graffiti(graffitiBuilder.buildGraffiti(optionalGraffiti))
.attestations(attestations)
.proposerSlashings(proposerSlashings)
.attesterSlashings(attesterSlashings)
.deposits(depositProvider.getDeposits(blockSlotState, eth1Data))
Expand All @@ -174,9 +188,6 @@ public Function<BeaconBlockBodyBuilder, SafeFuture<Void>> createSelector(
blsToExecutionChangePool.getItemsForBlock(blockSlotState));
}

final SchemaDefinitions schemaDefinitions =
spec.atSlot(blockSlotState.getSlot()).getSchemaDefinitions();

final SafeFuture<Void> blockProductionComplete;

// In `setExecutionData` the following fields are set:
Expand Down Expand Up @@ -548,4 +559,18 @@ private void verifyBuilderBlobsBundle(
blockCommitments.size() == blobsBundle.getBlobs().size(),
"The number of blobs in the builder BlobsBundle doesn't match the number of commitments in the block");
}

/**
* Selects attestations for inclusion in a block using an optimized priority-based algorithm.
* This method prioritizes attestations based on:
* 1. Inclusion delay (older attestations that are about to expire get higher priority)
* 2. Aggregation count (attestations with more aggregated signatures get higher priority)
* 3. Committee index (to ensure diversity of committees)
*
* @param blockSlotState The beacon state at the block's slot
* @return A list of attestations to include in the block
*/
private List<Attestation> selectOptimizedAttestations(final BeaconState blockSlotState) {
return attestationAggregationStrategy.selectOptimizedAttestations(blockSlotState);
}
}
Loading