Skip to content

Commit

Permalink
[Builder API] Update for Electra spec (Consensys#8721)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov authored and tbenr committed Oct 14, 2024
1 parent 5bc9c13 commit fcf3257
Show file tree
Hide file tree
Showing 20 changed files with 404 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -347,21 +347,35 @@ private SafeFuture<Void> setExecutionRequests(
if (!bodyBuilder.supportsExecutionRequests()) {
return SafeFuture.COMPLETE;
}

final SafeFuture<ExecutionRequests> executionRequestsFuture;
final SafeFuture<ExecutionRequests> executionRequests;
if (executionPayloadResult.isFromLocalFlow()) {
executionRequestsFuture =
// local, non-blinded flow
executionRequests =
executionPayloadResult
.getExecutionRequestsFromLocalFlow()
.getExecutionRequestsFutureFromLocalFlow()
.orElseThrow()
.thenApply(Optional::orElseThrow);
} else {
// TODO Add support for builder flow in Electra
// (https://github.com/Consensys/teku/issues/8624)
executionRequestsFuture = SafeFuture.completedFuture(null);
// builder, blinded flow
executionRequests =
executionPayloadResult
.getBuilderBidOrFallbackDataFuture()
.orElseThrow()
.thenApply(this::getExecutionRequestsFromBuilderFlow);
}

return executionRequestsFuture.thenAccept(bodyBuilder::executionRequests);
return executionRequests.thenAccept(bodyBuilder::executionRequests);
}

private ExecutionRequests getExecutionRequestsFromBuilderFlow(
final BuilderBidOrFallbackData builderBidOrFallbackData) {
return builderBidOrFallbackData
.getBuilderBid()
// from the builder bid
.flatMap(BuilderBid::getOptionalExecutionRequests)
// from the local fallback
.or(() -> builderBidOrFallbackData.getFallbackDataRequired().getExecutionRequests())
.orElseThrow();
}

public Consumer<SignedBeaconBlockUnblinder> createBlockUnblinderSelector(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,45 @@ void shouldGetExecutionRequestsForLocallyProducedBlocks() {
assertThat(bodyBuilder.executionRequests).isEqualTo(expectedExecutionRequests);
}

@Test
void shouldIncludeExecutionRequestsInBlindedBlock() {
final UInt64 slot = UInt64.valueOf(2);
final BeaconState blockSlotState = dataStructureUtil.randomBeaconState(slot);

final ExecutionRequests executionRequests = dataStructureUtil.randomExecutionRequests();

final ExecutionPayloadContext executionPayloadContextWithValidatorRegistration =
dataStructureUtil.randomPayloadExecutionContext(false, true);
when(forkChoiceNotifier.getPayloadId(any(), any()))
.thenReturn(
SafeFuture.completedFuture(
Optional.of(executionPayloadContextWithValidatorRegistration)));

prepareBlindedBlockWithBlobsAndExecutionRequestsProduction(
dataStructureUtil.randomExecutionPayloadHeader(),
executionPayloadContextWithValidatorRegistration,
blockSlotState,
dataStructureUtil.randomBlobKzgCommitments(),
executionRequests,
dataStructureUtil.randomUInt256());

final CapturingBeaconBlockBodyBuilder bodyBuilder =
new CapturingBeaconBlockBodyBuilder(true, true);

safeJoin(
factory
.createSelector(
parentRoot,
blockSlotState,
dataStructureUtil.randomSignature(),
Optional.empty(),
Optional.empty(),
BlockProductionPerformance.NOOP)
.apply(bodyBuilder));

assertThat(bodyBuilder.executionRequests).isEqualTo(executionRequests);
}

private void prepareBlockProductionWithPayload(
final ExecutionPayload executionPayload,
final ExecutionPayloadContext executionPayloadContext,
Expand Down Expand Up @@ -1158,6 +1197,33 @@ private void prepareBlindedBlockAndBlobsProduction(
executionPayloadContext, SafeFuture.completedFuture(builderBidOrFallbackData)));
}

private void prepareBlindedBlockWithBlobsAndExecutionRequestsProduction(
final ExecutionPayloadHeader executionPayloadHeader,
final ExecutionPayloadContext executionPayloadContext,
final BeaconState blockSlotState,
final SszList<SszKZGCommitment> blobKzgCommitments,
final ExecutionRequests executionRequests,
final UInt256 executionPayloadValue) {
final BuilderBidOrFallbackData builderBidOrFallbackData =
BuilderBidOrFallbackData.create(
dataStructureUtil.randomBuilderBid(
builder -> {
builder.header(executionPayloadHeader);
builder.blobKzgCommitments(blobKzgCommitments);
builder.executionRequests(executionRequests);
builder.value(executionPayloadValue);
}));
when(executionLayer.initiateBlockProduction(
executionPayloadContext,
blockSlotState,
true,
Optional.empty(),
BlockProductionPerformance.NOOP))
.thenReturn(
ExecutionPayloadResult.createForBuilderFlow(
executionPayloadContext, SafeFuture.completedFuture(builderBidOrFallbackData)));
}

private void prepareBlindedBlockAndBlobsProductionWithFallBack(
final ExecutionPayload executionPayload,
final ExecutionPayloadContext executionPayloadContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
package tech.pegasys.teku.api.schema.electra;

import static com.google.common.base.Preconditions.checkNotNull;
import static tech.pegasys.teku.api.schema.SchemaConstants.DESCRIPTION_BYTES32;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.api.schema.Attestation;
Expand All @@ -39,6 +37,7 @@
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBody;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.electra.BlindedBeaconBlockBodySchemaElectra;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadHeaderSchema;
import tech.pegasys.teku.spec.datastructures.execution.versions.electra.ExecutionRequestsSchema;
import tech.pegasys.teku.spec.datastructures.type.SszKZGCommitment;

public class BlindedBeaconBlockBodyElectra extends BeaconBlockBodyAltair {
Expand All @@ -52,9 +51,8 @@ public class BlindedBeaconBlockBodyElectra extends BeaconBlockBodyAltair {
@JsonProperty("blob_kzg_commitments")
public final List<KZGCommitment> blobKZGCommitments;

@JsonProperty("execution_requests_root")
@Schema(type = "string", format = "byte", description = DESCRIPTION_BYTES32)
public final Bytes32 executionRequestsRoot;
@JsonProperty("execution_requests")
public final ExecutionRequests executionRequests;

@JsonCreator
public BlindedBeaconBlockBodyElectra(
Expand All @@ -72,7 +70,7 @@ public BlindedBeaconBlockBodyElectra(
@JsonProperty("bls_to_execution_changes")
final List<SignedBlsToExecutionChange> blsToExecutionChanges,
@JsonProperty("blob_kzg_commitments") final List<KZGCommitment> blobKZGCommitments,
@JsonProperty("execution_requests_root") final Bytes32 executionRequestsRoot) {
@JsonProperty("execution_requests") final ExecutionRequests executionRequests) {
super(
randaoReveal,
eth1Data,
Expand All @@ -91,9 +89,8 @@ public BlindedBeaconBlockBodyElectra(
this.blsToExecutionChanges = blsToExecutionChanges;
checkNotNull(blobKZGCommitments, "BlobKZGCommitments is required for Electra blinded blocks");
this.blobKZGCommitments = blobKZGCommitments;
checkNotNull(
executionRequestsRoot, "ExecutionRequestsRoot is required for Electra blinded blocks");
this.executionRequestsRoot = executionRequestsRoot;
checkNotNull(executionRequests, "ExecutionRequests is required for Electra blinded blocks");
this.executionRequests = executionRequests;
}

public BlindedBeaconBlockBodyElectra(
Expand All @@ -110,9 +107,7 @@ public BlindedBeaconBlockBodyElectra(
.map(SszKZGCommitment::getKZGCommitment)
.map(KZGCommitment::new)
.toList();
// TODO Electra Builder API changes
// (https://github.com/Consensys/teku/issues/8624)
this.executionRequestsRoot = Bytes32.ZERO;
this.executionRequests = new ExecutionRequests(blockBody.getExecutionRequests());
}

@Override
Expand All @@ -139,6 +134,9 @@ public BeaconBlockBody asInternalBeaconBlockBody(final SpecVersion spec) {
final SszListSchema<SszKZGCommitment, ?> blobKZGCommitmentsSchema =
getBeaconBlockBodySchema(spec).getBlobKzgCommitmentsSchema();

final ExecutionRequestsSchema executionRequestsSchema =
getBeaconBlockBodySchema(spec).getExecutionRequestsSchema();

return super.asInternalBeaconBlockBody(
spec,
builder -> {
Expand All @@ -154,7 +152,8 @@ public BeaconBlockBody asInternalBeaconBlockBody(final SpecVersion spec) {
.map(KZGCommitment::asInternalKZGCommitment)
.map(SszKZGCommitment::new)
.collect(blobKZGCommitmentsSchema.collector()));
// TODO Electra Builder API changes (https://github.com/Consensys/teku/pull/8600)
builder.executionRequests(
this.executionRequests.asInternalConsolidationRequest(executionRequestsSchema));
return SafeFuture.COMPLETE;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static tech.pegasys.teku.spec.SpecMilestone.BELLATRIX;
import static tech.pegasys.teku.spec.SpecMilestone.CAPELLA;
import static tech.pegasys.teku.spec.SpecMilestone.DENEB;
import static tech.pegasys.teku.spec.SpecMilestone.ELECTRA;
import static tech.pegasys.teku.spec.schemas.ApiSchemas.SIGNED_VALIDATOR_REGISTRATIONS_SCHEMA;

import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down Expand Up @@ -60,9 +61,8 @@
import tech.pegasys.teku.spec.networks.Eth2Network;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsBellatrix;

// TODO Re-enable electra as part of https://github.com/Consensys/teku/issues/8624
@TestSpecContext(
milestone = {BELLATRIX, CAPELLA, DENEB},
milestone = {BELLATRIX, CAPELLA, DENEB, ELECTRA},
network = Eth2Network.MAINNET)
class RestBuilderClientTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,31 @@
"blob_kzg_commitments": [
"0xa94170080872584e54a1cf092d845703b13907f2e6b3b1c0ad573b910530499e3bcd48c6378846b80d2bfa58c81cf3d5"
],
"execution_requests": {
"deposits": [
{
"pubkey": "0x93247f2209abcacf57b75a51dafae777f9dd38bc7053d1af526f220a7489a6d3a2753e5f3e8b1cfe39b56f43611df74a",
"withdrawal_credentials": "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"amount": "1",
"signature": "0x1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505cc411d61252fb6cb3fa0017b679f8bb2305b26a285fa2737f175668d0dff91cc1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505",
"index": "1"
}
],
"withdrawals": [
{
"source_address": "0xabcf8e0d4e9587369b2301d0790347320302cc09",
"validator_pubkey": "0x93247f2209abcacf57b75a51dafae777f9dd38bc7053d1af526f220a7489a6d3a2753e5f3e8b1cfe39b56f43611df74a",
"amount": "1"
}
],
"consolidations": [
{
"source_address": "0xabcf8e0d4e9587369b2301d0790347320302cc09",
"source_pubkey": "0x93247f2209abcacf57b75a51dafae777f9dd38bc7053d1af526f220a7489a6d3a2753e5f3e8b1cfe39b56f43611df74a",
"target_pubkey": "0x93247f2209abcacf57b75a51dafae777f9dd38bc7053d1af526f220a7489a6d3a2753e5f3e8b1cfe39b56f43611df74a"
}
]
},
"value": "1",
"pubkey": "0x93247f2209abcacf57b75a51dafae777f9dd38bc7053d1af526f220a7489a6d3a2753e5f3e8b1cfe39b56f43611df74a"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,6 @@
"address": "0xabcf8e0d4e9587369b2301d0790347320302cc09",
"amount": "32000000000"
}
],
"deposit_requests": [
{
"pubkey": "0x94e2d4cf94fb757578c496885af2075c26e2483eeffa6e894ac791f7c1945b0fbf9a6f7860736db93e03d511c4b08516",
"withdrawal_credentials": "0x010000000000000000000000a2a06043d17ac951519956f43432e6811a0a4c5e",
"amount": "4853404529811048049",
"signature": "0x85cc5356a9646f0ffd512b7d2e7d3242c81303a415e61b490d28635896aef1f2db03ae8a1439908d03cc131515ef83f003dd7b36ce480c43f4495ffd339b2b9d1e5461309a02ce193202f27d216a4f0e13f7b47295f3e1a44c8f0e8ae8e1e5a8",
"index": "4843489626215975984"
},
{
"pubkey": "0xa5041469fc5f6a944fda64e7ab422c1479ab9d0de12a2f3ac7292dfe368408cbc6d2b0ff519b521427da731e7378806e",
"withdrawal_credentials": "0x010000000000000000000000fdc8e14312fb98663ed87639047022e291c761d2",
"amount": "4889759173463000754",
"signature": "0xa19b99131e621d31846245039e99d6540418acc08844a3996544cca2d9965f8a24cc49cf695bd78959a784e1b5646b480b88aec749a62ea934ed3001af50bb8babfb15bb9df1f3d57abd738f65de02b77398c82302f500218675cd96ee3b2357",
"index": "4893064144191336306"
}
],
"withdrawal_requests": [
{
"source_address": "0x240bd643529257f0285e4590ab814b7c9dcd5ff6",
"validator_pubkey": "0x802775e14fda679e4594ca8ccda68a706957fe82c10e66d258eafd21fe5b2877c226092004f82874fb4022d73b74eea6",
"amount": "4878191786651244561"
}
],
"consolidation_requests": [
{
"source_address": "0x240bd643529257f0285e4590ab814b7c9dcd5ff6",
"source_pubkey": "0x802775e14fda679e4594ca8ccda68a706957fe82c10e66d258eafd21fe5b2877c226092004f82874fb4022d73b74eea6",
"target_pubkey": "0x802775e14fda679e4594ca8ccda68a706957fe82c10e66d258eafd21fe5b2877c226092004f82874fb4022d73b74eea6"
}
]
},
"blobs_bundle": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import tech.pegasys.teku.infrastructure.ssz.SszContainer;
import tech.pegasys.teku.infrastructure.ssz.SszList;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadHeader;
import tech.pegasys.teku.spec.datastructures.execution.versions.electra.ExecutionRequests;
import tech.pegasys.teku.spec.datastructures.type.SszKZGCommitment;

public interface BuilderBid extends SszContainer {
Expand All @@ -27,6 +28,8 @@ public interface BuilderBid extends SszContainer {

Optional<SszList<SszKZGCommitment>> getOptionalBlobKzgCommitments();

Optional<ExecutionRequests> getOptionalExecutionRequests();

UInt256 getValue();

BLSPublicKey getPublicKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import tech.pegasys.teku.bls.BLSPublicKey;
import tech.pegasys.teku.infrastructure.ssz.SszList;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadHeader;
import tech.pegasys.teku.spec.datastructures.execution.versions.electra.ExecutionRequests;
import tech.pegasys.teku.spec.datastructures.type.SszKZGCommitment;

public interface BuilderBidBuilder {
Expand All @@ -25,6 +26,8 @@ public interface BuilderBidBuilder {

BuilderBidBuilder blobKzgCommitments(SszList<SszKZGCommitment> blobKzgCommitments);

BuilderBidBuilder executionRequests(ExecutionRequests executionRequests);

BuilderBidBuilder value(UInt256 value);

BuilderBidBuilder publicKey(BLSPublicKey publicKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ public class ExecutionPayloadAndBlobsBundleSchema
extends ContainerSchema2<ExecutionPayloadAndBlobsBundle, ExecutionPayload, BlobsBundle>
implements BuilderPayloadSchema<ExecutionPayloadAndBlobsBundle> {

// TODO Update as part of Builder API Electra updates
// (https://github.com/Consensys/teku/issues/8624)

public ExecutionPayloadAndBlobsBundleSchema(
final ExecutionPayloadSchema<? extends ExecutionPayload> executionPayloadSchema,
final BlobsBundleSchema blobsBundleSchema) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode;
import tech.pegasys.teku.spec.datastructures.builder.BuilderBid;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadHeader;
import tech.pegasys.teku.spec.datastructures.execution.versions.electra.ExecutionRequests;
import tech.pegasys.teku.spec.datastructures.type.SszKZGCommitment;
import tech.pegasys.teku.spec.datastructures.type.SszPublicKey;

Expand Down Expand Up @@ -51,6 +52,11 @@ public Optional<SszList<SszKZGCommitment>> getOptionalBlobKzgCommitments() {
return Optional.empty();
}

@Override
public Optional<ExecutionRequests> getOptionalExecutionRequests() {
return Optional.empty();
}

@Override
public UInt256 getValue() {
return getField1().get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import tech.pegasys.teku.spec.datastructures.builder.BuilderBid;
import tech.pegasys.teku.spec.datastructures.builder.BuilderBidBuilder;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadHeader;
import tech.pegasys.teku.spec.datastructures.execution.versions.electra.ExecutionRequests;
import tech.pegasys.teku.spec.datastructures.type.SszKZGCommitment;
import tech.pegasys.teku.spec.datastructures.type.SszPublicKey;

Expand Down Expand Up @@ -49,6 +50,11 @@ public BuilderBidBuilder blobKzgCommitments(final SszList<SszKZGCommitment> blob
return this;
}

@Override
public BuilderBidBuilder executionRequests(final ExecutionRequests executionRequests) {
return this;
}

@Override
public BuilderBidBuilder value(final UInt256 value) {
this.value = value;
Expand Down
Loading

0 comments on commit fcf3257

Please sign in to comment.