Skip to content

Commit

Permalink
fixes + fCU optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Feb 3, 2025
1 parent 3ecea72 commit b111fa0
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ public SafeFuture<InternalValidationResult> sendExecutionPayload(
@SuppressWarnings("FutureReturnValueIgnored")
private void publishExecutionPayloadAndBlobSidecars(
final SignedExecutionPayloadEnvelope executionPayload, final List<BlobSidecar> blobSidecars) {
LOG.info(
"Publishing execution payload and {} blob sidecars for block {}",
blobSidecars.size(),
executionPayload.getMessage().getBeaconBlockRoot());
executionPayloadGossipChannel.publishExecutionPayload(executionPayload);
blobSidecarGossipChannel.publishBlobSidecars(blobSidecars);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ default UInt64 getGenesisTimeMillis() {

boolean containsBlock(Bytes32 blockRoot);

boolean containsExecutionPayloadEnvelope(Bytes32 blockRoot);

/**
* @return A collection of block roots ordered to guarantee that parent roots will be sorted
* earlier than child roots
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,6 @@ public boolean containsBlock(final Bytes32 blockRoot) {
return blocks.containsKey(blockRoot);
}

// EIP-7732 TODO: implement (test)
@Override
public boolean containsExecutionPayloadEnvelope(final Bytes32 blockRoot) {
return false;
}

@Override
public List<Bytes32> getOrderedBlockRoots() {
return blocks.values().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
import tech.pegasys.teku.spec.datastructures.operations.PayloadAttestationMessage;
import tech.pegasys.teku.spec.datastructures.state.Checkpoint;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.eip7732.BeaconStateEip7732;
import tech.pegasys.teku.spec.datastructures.util.AttestationProcessingResult;
import tech.pegasys.teku.spec.datastructures.util.AttestationProcessingResult.Status;
import tech.pegasys.teku.spec.executionlayer.ExecutionLayerChannel;
Expand Down Expand Up @@ -225,19 +224,14 @@ public SafeFuture<BlockImportResult> onBlock(
final SlotAndBlockRoot slotAndBlockRoot =
new SlotAndBlockRoot(block.getSlot(), block.getParentRoot());
if (spec.atSlot(block.getSlot()).getMilestone().isGreaterThanOrEqualTo(SpecMilestone.EIP7732)) {
// EIP-7732 TODO: hacky way of retrieving the pre state of the block
blockSlotStateFuture =
recentChainData
.retrieveExecutionPayloadStateAtSlot(slotAndBlockRoot)
.thenCombine(
recentChainData.retrieveStateAtSlot(slotAndBlockRoot),
(executionPayloadState, state) -> {
if (executionPayloadState.isPresent()) {
LOG.info(
"Using a state with latest full slot {} and latest block hash {} for fork choice processing",
BeaconStateEip7732.required(executionPayloadState.get())
.getLatestFullSlot(),
BeaconStateEip7732.required(executionPayloadState.get())
.getLatestBlockHash());
return executionPayloadState;
}
return state;
Expand Down Expand Up @@ -737,7 +731,13 @@ private BlockImportResult importBlockAndState(
result = BlockImportResult.optimisticallySuccessful(block);
}
updateForkChoiceForImportedBlock(block, result, forkChoiceStrategy);
notifyForkChoiceUpdatedAndOptimisticSyncingChanged(Optional.empty());

// no fCU call required during onBlock for ePBS
if (!spec.atSlot(block.getSlot())
.getMilestone()
.isGreaterThanOrEqualTo(SpecMilestone.EIP7732)) {
notifyForkChoiceUpdatedAndOptimisticSyncingChanged(Optional.empty());
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public interface ForkChoiceNotifier {

void onAttestationsDue(UInt64 slot);

void onPayloadAttestationsDue(UInt64 slot);

void onSyncingStatusChanged(boolean inSync);

SafeFuture<Optional<ExecutionPayloadContext>> getPayloadId(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import tech.pegasys.teku.infrastructure.time.TimeProvider;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadContext;
import tech.pegasys.teku.spec.executionlayer.ExecutionLayerChannel;
import tech.pegasys.teku.spec.executionlayer.ForkChoiceState;
Expand Down Expand Up @@ -82,6 +83,11 @@ public void onAttestationsDue(final UInt64 slot) {
eventThread.execute(() -> internalAttestationsDue(slot));
}

@Override
public void onPayloadAttestationsDue(final UInt64 slot) {
eventThread.execute(() -> internalPayloadAttestationsDue(slot));
}

@Override
public void onSyncingStatusChanged(final boolean inSync) {
eventThread.execute(
Expand Down Expand Up @@ -258,6 +264,19 @@ private void internalAttestationsDue(final UInt64 slot) {

LOG.debug("internalAttestationsDue slot {}", slot);

// for ePBS update payload attributes is moved to "internalPayloadAttestationsDue"
if (spec.atSlot(slot).getMilestone().isGreaterThanOrEqualTo(SpecMilestone.EIP7732)) {
return;
}
// Assume `slot` is empty and check if we need to prepare to propose in the next slot
updatePayloadAttributes(slot.plus(1));
}

private void internalPayloadAttestationsDue(final UInt64 slot) {
eventThread.checkOnEventThread();

LOG.debug("internalPayloadAttestationsDue slot {}", slot);

// Assume `slot` is empty and check if we need to prepare to propose in the next slot
updatePayloadAttributes(slot.plus(1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,6 @@ public void onNewExecutionPayload(
if (block.getMessage().getBody().toVersionEip7732().isEmpty()) {
return;
}
if (recentChainData.containsExecutionPayloadEnvelope(
executionPayload.getMessage().getBeaconBlockRoot())) {
return;
}
if (shouldIgnoreItemAtSlot(block.getSlot())) {
return;
}
Expand Down Expand Up @@ -384,9 +380,6 @@ public void onCompletedExecutionPayloadAndBlobSidecars(
final SignedBeaconBlock block,
final SignedExecutionPayloadEnvelope executionPayload,
final List<BlobSidecar> blobSidecars) {
if (recentChainData.containsExecutionPayloadEnvelope(block.getRoot())) {
return;
}
final SlotAndBlockRoot slotAndBlockRoot = block.getSlotAndBlockRoot();

final BlockBlobSidecarsTracker blobSidecarsTracker =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public void onForkChoiceUpdated(
@Override
public void onAttestationsDue(final UInt64 slot) {}

@Override
public void onPayloadAttestationsDue(final UInt64 slot) {}

@Override
public void onSyncingStatusChanged(final boolean inSync) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,16 @@ private String formatBlockRoots(final Set<Bytes32> blockRoots) {
public void logPublishedBid(
final UInt64 slot,
final UInt64 builderIndex,
final Bytes32 blockHash,
final Bytes32 parentBlockHash,
final Bytes32 parentBlockRoot,
final String ethValue) {
log.info(
ColorConsolePrinter.print(
String.format(
"%sPublished bid Slot: %s, Builder: %s, Parent Block Root: %s, Value: %s ETH",
PREFIX, slot, builderIndex, parentBlockRoot, ethValue),
Color.CYAN));
"%sPublished bid Slot: %s, Builder: %s, Block Hash: %s, Parent Block Hash: %s, Parent Block Root: %s, Value: %s ETH",
PREFIX, slot, builderIndex, blockHash, parentBlockHash, parentBlockRoot, ethValue),
Color.PURPLE));
}

public void logPublishedExecutionPayload(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ public void onTick(
processSlotStart(epoch);
performanceRecord.ifPresent(TickProcessingPerformance::startSlotComplete);
}
if (isSlotPayloadAttestationDue(calculatedSlot, currentTimeMillis, nodeSlotStartTimeMillis)) {
forkChoiceNotifier.onPayloadAttestationsDue(nodeSlot.getValue());
}
if (isSlotAttestationDue(calculatedSlot, currentTimeMillis, nodeSlotStartTimeMillis)) {
processSlotAttestation(performanceRecord);
nodeSlot.inc();
Expand Down Expand Up @@ -182,6 +185,18 @@ boolean isSlotStartDue(final UInt64 calculatedSlot) {
return isProcessingDueForSlot(calculatedSlot, onTickSlotStart);
}

// Payload attestations are due 3/4 in Eip7732 of the way through the slots time period
boolean isSlotPayloadAttestationDue(
final UInt64 calculatedSlot,
final UInt64 currentTimeMillis,
final UInt64 nodeSlotStartTimeMillis) {
final UInt64 earliestTimeInMillis =
nodeSlotStartTimeMillis.plus(payloadAttestationDueMillis(calculatedSlot));
final boolean processingDueForSlot =
isProcessingDueForSlot(calculatedSlot, onTickSlotAttestation);
return processingDueForSlot && isTimeReached(currentTimeMillis, earliestTimeInMillis);
}

// Attestations are due 1/3 (1/4 in Eip7732) of the way through the slots time period
boolean isSlotAttestationDue(
final UInt64 calculatedSlot,
Expand Down Expand Up @@ -220,6 +235,11 @@ private UInt64 attestationDueMillis(final UInt64 slot) {
return spec.getMillisPerSlot(slot).dividedBy(INTERVALS_PER_SLOT);
}

private UInt64 payloadAttestationDueMillis(final UInt64 slot) {
// 3/4 of the slot in ePBS
return spec.getMillisPerSlot(slot).dividedBy(INTERVALS_PER_SLOT_EIP7732).times(3);
}

boolean isTimeReached(final UInt64 currentTime, final UInt64 earliestTime) {
return currentTime.isGreaterThanOrEqualTo(earliestTime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import tech.pegasys.teku.spec.datastructures.state.CommitteeAssignment;
import tech.pegasys.teku.spec.datastructures.state.ForkInfo;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.eip7732.BeaconStateEip7732;
import tech.pegasys.teku.spec.datastructures.util.SlotAndBlockRootAndBlobIndex;
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.EpochProcessingException;
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.SlotProcessingException;
Expand Down Expand Up @@ -243,7 +242,7 @@ public SafeFuture<Optional<BeaconState>> getStateAtSlotExact(final UInt64 slot)
return regenerateStateAndSlotExact(slot);
}

// EIP-7732 TODO: FIX implement regenerate
// EIP-7732 TODO: FIX implement regenerate similar to getStateAtSlotExact
public SafeFuture<Optional<BeaconState>> getExecutionPayloadStateAtSlotExact(final UInt64 slot) {
final Optional<Bytes32> recentBlockRoot = recentChainData.getBlockRootInEffectBySlot(slot);
if (recentBlockRoot.isPresent()) {
Expand All @@ -258,6 +257,7 @@ public SafeFuture<Optional<BeaconState>> getExecutionPayloadStateAtSlotExact(fin
return completedFuture(Optional.empty());
}

// EIP-7732 TODO: hacky way of retrieving the pre state of the block
public SafeFuture<Optional<BeaconState>> getStateAtSlotExactForBlockProduction(
final UInt64 slot) {
if (spec.atSlot(slot).getMilestone().isGreaterThanOrEqualTo(SpecMilestone.EIP7732)) {
Expand All @@ -266,11 +266,6 @@ public SafeFuture<Optional<BeaconState>> getStateAtSlotExactForBlockProduction(
getStateAtSlotExact(slot),
(executionPayloadState, state) -> {
if (executionPayloadState.isPresent()) {
LOG.info(
"Using a state with latest full slot {} and latest block hash {}",
BeaconStateEip7732.required(executionPayloadState.get()).getLatestFullSlot(),
BeaconStateEip7732.required(executionPayloadState.get())
.getLatestBlockHash());
return executionPayloadState;
}
return state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,12 +552,6 @@ public boolean containsBlock(final Bytes32 root) {
return Optional.ofNullable(store).map(s -> s.containsBlock(root)).orElse(false);
}

public boolean containsExecutionPayloadEnvelope(final Bytes32 root) {
return Optional.ofNullable(store)
.map(s -> s.containsExecutionPayloadEnvelope(root))
.orElse(false);
}

public boolean containsExecutionBlockHash(final Bytes32 blockHash) {
return getForkChoiceStrategy()
.map(forkChoice -> forkChoice.containsExecutionBlockHash(blockHash))
Expand Down
16 changes: 0 additions & 16 deletions storage/src/main/java/tech/pegasys/teku/storage/store/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -583,19 +583,6 @@ public boolean containsBlock(final Bytes32 blockRoot) {
}
}

@Override
public boolean containsExecutionPayloadEnvelope(final Bytes32 blockRoot) {
readLock.lock();
try {
return forkChoiceStrategy
.executionBlockHash(blockRoot)
.map(blockHash -> !blockHash.isZero())
.orElse(false);
} finally {
readLock.unlock();
}
}

@Override
public Collection<Bytes32> getOrderedBlockRoots() {
readLock.lock();
Expand Down Expand Up @@ -744,9 +731,6 @@ public SafeFuture<Optional<SignedBeaconBlock>> retrieveSignedBlock(final Bytes32
@Override
public SafeFuture<Optional<SignedExecutionPayloadEnvelope>> retrieveExecutionPayloadEnvelope(
final Bytes32 blockRoot) {
if (!containsExecutionPayloadEnvelope(blockRoot)) {
return EmptyStoreResults.EMPTY_EXECUTION_PAYLOAD_ENVELOPE_FUTURE;
}
return executionPayloadEnvelopeProvider.getExecutionPayloadEnvelope(blockRoot);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,6 @@ public boolean containsBlock(final Bytes32 blockRoot) {
return blockData.containsKey(blockRoot) || store.containsBlock(blockRoot);
}

@Override
public boolean containsExecutionPayloadEnvelope(final Bytes32 blockRoot) {
return executionPayloadEnvelopeData.containsKey(blockRoot)
|| store.containsExecutionPayloadEnvelope(blockRoot);
}

@Override
public Collection<Bytes32> getOrderedBlockRoots() {
if (this.blockData.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ private SafeFuture<Void> processExecutionPayloadHeader(
VALIDATOR_LOGGER.logPublishedBid(
bid.getSlot(),
bid.getBuilderIndex(),
bid.getBlockHash(),
bid.getParentBlockHash(),
bid.getParentBlockRoot(),
gweiToEth(bid.getValue()));
});
Expand Down

0 comments on commit b111fa0

Please sign in to comment.