Skip to content

Commit

Permalink
[ePBS] "Naive" block production in VC (and fixed blobs creation in BN) (
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Oct 2, 2024
1 parent 7c57050 commit a6cb2af
Show file tree
Hide file tree
Showing 18 changed files with 338 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadEnvelope;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadHeader;
import tech.pegasys.teku.spec.datastructures.execution.GetPayloadResponse;
import tech.pegasys.teku.spec.datastructures.execution.SignedExecutionPayloadEnvelope;
import tech.pegasys.teku.spec.datastructures.execution.versions.eip7732.ExecutionPayloadHeaderEip7732;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.datastructures.type.SszKZGCommitment;
import tech.pegasys.teku.spec.datastructures.type.SszKZGProof;
import tech.pegasys.teku.spec.logic.versions.deneb.helpers.MiscHelpersDeneb;
import tech.pegasys.teku.spec.logic.versions.eip7732.helpers.MiscHelpersEip7732;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsEip7732;

public class CachingExecutionPayloadAndBlobSidecarsRevealer
Expand Down Expand Up @@ -94,7 +95,9 @@ public Optional<ExecutionPayloadEnvelope> revealExecutionPayload(
}

@Override
public List<BlobSidecar> revealBlobSidecars(final SignedBeaconBlock block) {
public List<BlobSidecar> revealBlobSidecars(
final SignedBeaconBlock block,
final SignedExecutionPayloadEnvelope executionPayloadEnvelope) {
final Bytes32 committedHeaderRoot =
BeaconBlockBodyEip7732.required(block.getMessage().getBody())
.getSignedExecutionPayloadHeader()
Expand All @@ -106,20 +109,20 @@ public List<BlobSidecar> revealBlobSidecars(final SignedBeaconBlock block) {
logMissingPayload(block);
return List.of();
}
final MiscHelpersDeneb miscHelpersDeneb =
MiscHelpersDeneb.required(spec.atSlot(block.getSlot()).miscHelpers());
final MiscHelpersEip7732 miscHelpersEip7732 =
MiscHelpersEip7732.required(spec.atSlot(block.getSlot()).miscHelpers());

final BlobsBundle blobsBundle = getPayloadResponse.getBlobsBundle().orElseThrow();

final List<Blob> blobs = blobsBundle.getBlobs();
final List<KZGProof> proofs = blobsBundle.getProofs();

// EIP7732 TODO: need to modify the constructBlobSidecar method in ePBS
return IntStream.range(0, blobsBundle.getNumberOfBlobs())
.mapToObj(
index ->
miscHelpersDeneb.constructBlobSidecar(
miscHelpersEip7732.constructBlobSidecar(
block,
executionPayloadEnvelope,
UInt64.valueOf(index),
blobs.get(index),
new SszKZGProof(proofs.get(index))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadEnvelope;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadHeader;
import tech.pegasys.teku.spec.datastructures.execution.GetPayloadResponse;
import tech.pegasys.teku.spec.datastructures.execution.SignedExecutionPayloadEnvelope;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;

public interface ExecutionPayloadAndBlobSidecarsRevealer {
Expand All @@ -32,5 +33,6 @@ Optional<ExecutionPayloadEnvelope> revealExecutionPayload(
SignedBeaconBlock block, BeaconState state);

/** 3. builder reveals the blob sidecars */
List<BlobSidecar> revealBlobSidecars(SignedBeaconBlock block);
List<BlobSidecar> revealBlobSidecars(
SignedBeaconBlock block, SignedExecutionPayloadEnvelope executionPayloadEnvelope);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

package tech.pegasys.teku.validator.coordinator;

import static tech.pegasys.teku.infrastructure.logging.Converter.weiToGwei;

import java.util.Optional;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.bls.BLSPublicKey;
Expand Down Expand Up @@ -121,9 +123,8 @@ private ExecutionPayloadHeader createLocalBid(
.gasLimit(getPayloadResponse.getExecutionPayload().getGasLimit())
.builderIndex(() -> builderIndex)
.slot(() -> slot)
.value(
() ->
UInt64.valueOf(getPayloadResponse.getExecutionPayloadValue().toLong()))
// Engine API returns the block value in wei, so conversion to gwei is required
.value(() -> weiToGwei(getPayloadResponse.getExecutionPayloadValue()))
.blobKzgCommitmentsRoot(blobKzgCommitments::hashTreeRoot));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static tech.pegasys.teku.infrastructure.metrics.Validator.DutyType.ATTESTATION_PRODUCTION;
import static tech.pegasys.teku.infrastructure.metrics.Validator.ValidatorDutyMetricUtils.startTimer;
import static tech.pegasys.teku.infrastructure.metrics.Validator.ValidatorDutyMetricsSteps.CREATE;
import static tech.pegasys.teku.infrastructure.time.SystemTimeProvider.SYSTEM_TIME_PROVIDER;
import static tech.pegasys.teku.spec.config.SpecConfig.GENESIS_SLOT;
import static tech.pegasys.teku.spec.datastructures.validator.BroadcastValidationLevel.EQUIVOCATION;
import static tech.pegasys.teku.spec.datastructures.validator.BroadcastValidationLevel.GOSSIP;
Expand Down Expand Up @@ -236,7 +237,8 @@ public ValidatorApiHandler(
blockBlobSidecarsTrackersPool,
executionPayloadGossipChannel,
executionPayloadAndBlobSidecarsRevealer,
blobSidecarGossipChannel);
blobSidecarGossipChannel,
SYSTEM_TIME_PROVIDER);
this.executionPayloadAndBlobSidecarsRevealer = executionPayloadAndBlobSidecarsRevealer;
this.attesterDutiesGenerator = new AttesterDutiesGenerator(spec);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private List<PayloadAttesterDuty> createPayloadAttesterDuties(
final List<PayloadAttesterDuty> payloadAttesterDutyList = new ArrayList<>();

final Int2ObjectMap<UInt64> validatorIndexToCommitteeAssignmentMap =
spec.getValidatorIndexToPctAssignmentMap(state, epoch);
spec.getValidatorIndexToPtcAssignmentMap(state, epoch);
for (final int validatorIndex : validatorIndices) {
final UInt64 slot = validatorIndexToCommitteeAssignmentMap.get(validatorIndex);
if (slot != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.List;
import java.util.Optional;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.time.TimeProvider;
import tech.pegasys.teku.networking.eth2.gossip.BlobSidecarGossipChannel;
import tech.pegasys.teku.networking.eth2.gossip.ExecutionPayloadGossipChannel;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlobSidecar;
Expand All @@ -33,29 +34,32 @@ public class ExecutionPayloadPublisher {
private final ExecutionPayloadGossipChannel executionPayloadGossipChannel;
private final ExecutionPayloadAndBlobSidecarsRevealer executionPayloadAndBlobSidecarsRevealer;
private final BlobSidecarGossipChannel blobSidecarGossipChannel;
private final TimeProvider timeProvider;

public ExecutionPayloadPublisher(
final ExecutionPayloadManager executionPayloadManager,
final BlockBlobSidecarsTrackersPool blockBlobSidecarsTrackersPool,
final ExecutionPayloadGossipChannel executionPayloadGossipChannel,
final ExecutionPayloadAndBlobSidecarsRevealer executionPayloadAndBlobSidecarsRevealer,
final BlobSidecarGossipChannel blobSidecarGossipChannel) {
final BlobSidecarGossipChannel blobSidecarGossipChannel,
final TimeProvider timeProvider) {
this.executionPayloadManager = executionPayloadManager;
this.blockBlobSidecarsTrackersPool = blockBlobSidecarsTrackersPool;
this.executionPayloadGossipChannel = executionPayloadGossipChannel;
this.executionPayloadAndBlobSidecarsRevealer = executionPayloadAndBlobSidecarsRevealer;
this.blobSidecarGossipChannel = blobSidecarGossipChannel;
this.timeProvider = timeProvider;
}

public SafeFuture<InternalValidationResult> sendExecutionPayload(
final SignedBeaconBlock block, final SignedExecutionPayloadEnvelope executionPayload) {
final List<BlobSidecar> blobSidecars =
executionPayloadAndBlobSidecarsRevealer.revealBlobSidecars(block);
executionPayloadAndBlobSidecarsRevealer.revealBlobSidecars(block, executionPayload);
publishExecutionPayloadAndBlobSidecars(executionPayload, blobSidecars);
// provide blobs for the execution payload before importing it
blockBlobSidecarsTrackersPool.onCompletedBlockAndBlobSidecars(block, blobSidecars);
return executionPayloadManager.validateAndImportExecutionPayload(
executionPayload, Optional.empty());
executionPayload, Optional.of(timeProvider.getTimeInMillis()));
}

private void publishExecutionPayloadAndBlobSidecars(
Expand Down
4 changes: 2 additions & 2 deletions ethereum/spec/src/main/java/tech/pegasys/teku/spec/Spec.java
Original file line number Diff line number Diff line change
Expand Up @@ -935,9 +935,9 @@ public Int2ObjectMap<CommitteeAssignment> getValidatorIndexToCommitteeAssignment
.getValidatorIndexToCommitteeAssignmentMap(state, epoch);
}

public Int2ObjectMap<UInt64> getValidatorIndexToPctAssignmentMap(
public Int2ObjectMap<UInt64> getValidatorIndexToPtcAssignmentMap(
final BeaconState state, final UInt64 epoch) {
return atEpoch(epoch).getValidatorsUtil().getValidatorIndexToPctAssignmentMap(state, epoch);
return atEpoch(epoch).getValidatorsUtil().getValidatorIndexToPtcAssignmentMap(state, epoch);
}

// Attestation helpers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public Optional<UInt64> getPtcAssignment(
return Optional.empty();
}

public Int2ObjectMap<UInt64> getValidatorIndexToPctAssignmentMap(
public Int2ObjectMap<UInt64> getValidatorIndexToPtcAssignmentMap(
final BeaconState state, final UInt64 epoch) {
final Int2ObjectMap<UInt64> assignmentMap = new Int2ObjectArrayMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
public class MiscHelpersDeneb extends MiscHelpersCapella {
protected final Predicates predicates;
protected final BeaconBlockBodySchemaDeneb<?> beaconBlockBodySchema;
private final BlobSidecarSchema blobSidecarSchema;
protected final BlobSidecarSchema blobSidecarSchema;

public static MiscHelpersDeneb required(final MiscHelpers miscHelpers) {
return miscHelpers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ public BeaconStateEip7732 upgrade(final BeaconState preState) {
}
});

state.setLatestBlockHash(Bytes32.ZERO);
state.setLatestBlockHash(
preStateElectra.getLatestExecutionPayloadHeader().getBlockHash());
state.setLatestFullSlot(UInt64.ZERO);
state.setLatestWithdrawalsRoot(Bytes32.ZERO);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,22 @@

package tech.pegasys.teku.spec.logic.versions.eip7732.helpers;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.infrastructure.ssz.tree.GIndexUtil;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.config.SpecConfigEip7732;
import tech.pegasys.teku.spec.config.SpecConfigElectra;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.Blob;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlobSidecar;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBody;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.eip7732.BeaconBlockBodySchemaEip7732;
import tech.pegasys.teku.spec.datastructures.execution.SignedExecutionPayloadEnvelope;
import tech.pegasys.teku.spec.datastructures.type.SszKZGCommitment;
import tech.pegasys.teku.spec.datastructures.type.SszKZGProof;
import tech.pegasys.teku.spec.logic.common.helpers.MiscHelpers;
import tech.pegasys.teku.spec.logic.versions.electra.helpers.MiscHelpersElectra;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsEip7732;
Expand Down Expand Up @@ -77,6 +86,35 @@ public boolean verifyBlobSidecarMerkleProof(final BlobSidecar blobSidecar) {
blobSidecar.getBlockBodyRoot());
}

public BlobSidecar constructBlobSidecar(
final SignedBeaconBlock signedBeaconBlock,
final SignedExecutionPayloadEnvelope signedExecutionPayloadEnvelope,
final UInt64 index,
final Blob blob,
final SszKZGProof proof) {
final BeaconBlockBody beaconBlockBody = signedBeaconBlock.getMessage().getBody();
final SszKZGCommitment commitment;
try {
commitment =
signedExecutionPayloadEnvelope.getMessage().getBlobKzgCommitments().get(index.intValue());
} catch (final IndexOutOfBoundsException | NoSuchElementException ex) {
final int commitmentsCount = getBlobKzgCommitmentsCount(signedExecutionPayloadEnvelope);
throw new IllegalArgumentException(
String.format(
"Can't create blob sidecar with index %s because there are %d commitment(s) in the execution payload envelope",
index, commitmentsCount));
}
final List<Bytes32> kzgCommitmentInclusionProof =
computeKzgCommitmentInclusionProof(index, beaconBlockBody);
return blobSidecarSchema.create(
index, blob, commitment, proof, signedBeaconBlock.asHeader(), kzgCommitmentInclusionProof);
}

public int getBlobKzgCommitmentsCount(
final SignedExecutionPayloadEnvelope signedExecutionPayloadEnvelope) {
return signedExecutionPayloadEnvelope.getMessage().getBlobKzgCommitments().size();
}

@Override
public Optional<MiscHelpersEip7732> toVersionEip7732() {
return Optional.of(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;
import java.util.Objects;
import net.jqwik.api.ForAll;
import net.jqwik.api.From;
import net.jqwik.api.Property;
Expand All @@ -26,13 +25,12 @@
import tech.pegasys.teku.kzg.KZGCommitment;
import tech.pegasys.teku.kzg.KZGException;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.TestSpecFactory;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.Blob;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlobSidecar;
import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.datastructures.type.SszKZGProof;
import tech.pegasys.teku.spec.propertytest.suppliers.SpecSupplier;
import tech.pegasys.teku.spec.propertytest.suppliers.blobs.versions.deneb.BlobSidecarIndexSupplier;
import tech.pegasys.teku.spec.propertytest.suppliers.blobs.versions.deneb.BlobSidecarSupplier;
import tech.pegasys.teku.spec.propertytest.suppliers.blobs.versions.deneb.BlobSupplier;
Expand All @@ -43,8 +41,7 @@

public class MiscHelpersDenebPropertyTest {

private final Spec spec =
Objects.requireNonNull(new SpecSupplier(SpecMilestone.DENEB).get()).sample();
private final Spec spec = TestSpecFactory.createMainnetDeneb();
private final MiscHelpersDeneb miscHelpers =
MiscHelpersDeneb.required(spec.getGenesisSpec().miscHelpers());

Expand Down
Loading

0 comments on commit a6cb2af

Please sign in to comment.