Skip to content

Commit

Permalink
Introduce execution payload builder (#6518)
Browse files Browse the repository at this point in the history
Removes toVersionX() methods from ExecutionPayloadSchema as their behaviour didn't match those of ExecutionPayload.toVersionX methods (ExecutionPayloadSchema.toVersionCapella would fail for EIP4844 but ExecutionPayload.toVersionCapella would succeed for EIP4844).

Avoids the need to branch by milestone everywhere we create an ExecutionPayload.
  • Loading branch information
ajsutton authored Nov 29, 2022
1 parent e2f9ba8 commit 2d60d6a
Show file tree
Hide file tree
Showing 23 changed files with 643 additions and 657 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,7 @@ public void waitForNonDefaultExecutionPayload() {
.getMessage()
.getBody()
.executionPayload
.toVersionBellatrix()
.orElseThrow()
.asInternalExecutionPayload(spec, bellatrixBlock.getMessage().slot)
.orElseThrow();
.asInternalExecutionPayload(spec, bellatrixBlock.getMessage().slot);

assertThat(executionPayload.isDefault()).isFalse();
LOG.debug(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBody;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBodyBuilder;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.bellatrix.BeaconBlockBodySchemaBellatrix;
import tech.pegasys.teku.spec.datastructures.execution.versions.bellatrix.ExecutionPayloadSchemaBellatrix;

public class BeaconBlockBodyBellatrix extends BeaconBlockBodyAltair {
@JsonProperty("execution_payload")
Expand Down Expand Up @@ -85,35 +84,12 @@ public BeaconBlockBodySchemaBellatrix<?> getBeaconBlockBodySchema(final SpecVers
@Override
public BeaconBlockBody asInternalBeaconBlockBody(
final SpecVersion spec, Consumer<BeaconBlockBodyBuilder> builderRef) {

final ExecutionPayloadSchemaBellatrix executionPayloadSchemaBellatrix =
getBeaconBlockBodySchema(spec)
.getExecutionPayloadSchema()
.toVersionBellatrix()
.orElseThrow();

return super.asInternalBeaconBlockBody(
spec,
(builder) -> {
builderRef.accept(builder);
builder.executionPayload(
() ->
SafeFuture.completedFuture(
executionPayloadSchemaBellatrix.create(
executionPayload.parentHash,
executionPayload.feeRecipient,
executionPayload.stateRoot,
executionPayload.receiptsRoot,
executionPayload.logsBloom,
executionPayload.prevRandao,
executionPayload.blockNumber,
executionPayload.gasLimit,
executionPayload.gasUsed,
executionPayload.timestamp,
executionPayload.extraData,
executionPayload.baseFeePerGas,
executionPayload.blockHash,
executionPayload.transactions)));
() -> SafeFuture.completedFuture(executionPayload.asInternalExecutionPayload(spec)));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
import tech.pegasys.teku.infrastructure.bytes.Bytes20;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecVersion;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadBuilder;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadSchema;
import tech.pegasys.teku.spec.datastructures.execution.Transaction;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsBellatrix;

Expand Down Expand Up @@ -93,39 +96,45 @@ public ExecutionPayloadBellatrix(
.collect(Collectors.toList());
}

public Optional<tech.pegasys.teku.spec.datastructures.execution.ExecutionPayload>
public tech.pegasys.teku.spec.datastructures.execution.ExecutionPayload
asInternalExecutionPayload(final Spec spec, final UInt64 slot) {
return asInternalExecutionPayload(spec.atSlot(slot));
}

public tech.pegasys.teku.spec.datastructures.execution.ExecutionPayload
asInternalExecutionPayload(final SpecVersion spec) {
final Optional<SchemaDefinitionsBellatrix> maybeSchema =
spec.atSlot(slot).getSchemaDefinitions().toVersionBellatrix();
spec.getSchemaDefinitions().toVersionBellatrix();

if (maybeSchema.isEmpty()) {
final String message =
String.format("Could not create execution payload at non-bellatrix slot %s", slot);
throw new IllegalArgumentException(message);
throw new IllegalArgumentException(
"Could not create execution payload at pre-bellatrix slot");
}

return maybeSchema.map(
schema ->
schema
.getExecutionPayloadSchema()
.toVersionBellatrix()
.orElseThrow()
.create(
parentHash,
feeRecipient,
stateRoot,
receiptsRoot,
logsBloom,
prevRandao,
blockNumber,
gasLimit,
gasUsed,
timestamp,
extraData,
baseFeePerGas,
blockHash,
transactions));
final ExecutionPayloadSchema<?> executionPayloadSchema =
maybeSchema.get().getExecutionPayloadSchema();
return executionPayloadSchema.createExecutionPayload(
builder -> applyToBuilder(executionPayloadSchema, builder));
}

protected ExecutionPayloadBuilder applyToBuilder(
final ExecutionPayloadSchema<?> executionPayloadSchema,
final ExecutionPayloadBuilder builder) {
return builder
.parentHash(parentHash)
.feeRecipient(feeRecipient)
.stateRoot(stateRoot)
.receiptsRoot(receiptsRoot)
.logsBloom(logsBloom)
.prevRandao(prevRandao)
.blockNumber(blockNumber)
.gasLimit(gasLimit)
.gasUsed(gasUsed)
.timestamp(timestamp)
.extraData(extraData)
.baseFeePerGas(baseFeePerGas)
.blockHash(blockHash)
.transactions(transactions);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import tech.pegasys.teku.spec.SpecVersion;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBody;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.capella.BeaconBlockBodySchemaCapella;
import tech.pegasys.teku.spec.datastructures.execution.versions.capella.ExecutionPayloadSchemaCapella;

public class BeaconBlockBodyCapella extends BeaconBlockBodyAltair {

Expand Down Expand Up @@ -95,10 +94,6 @@ public BeaconBlockBodySchemaCapella<?> getBeaconBlockBodySchema(final SpecVersio

@Override
public BeaconBlockBody asInternalBeaconBlockBody(final SpecVersion spec) {

final ExecutionPayloadSchemaCapella executionPayloadSchemaCapella =
getBeaconBlockBodySchema(spec).getExecutionPayloadSchema().toVersionCapella().orElseThrow();

final SszListSchema<
tech.pegasys.teku.spec.datastructures.operations.SignedBlsToExecutionChange, ?>
blsToExecutionChangesSchema =
Expand All @@ -108,29 +103,7 @@ public BeaconBlockBody asInternalBeaconBlockBody(final SpecVersion spec) {
spec,
(builder) -> {
builder.executionPayload(
() ->
SafeFuture.completedFuture(
executionPayloadSchemaCapella.create(
executionPayload.parentHash,
executionPayload.feeRecipient,
executionPayload.stateRoot,
executionPayload.receiptsRoot,
executionPayload.logsBloom,
executionPayload.prevRandao,
executionPayload.blockNumber,
executionPayload.gasLimit,
executionPayload.gasUsed,
executionPayload.timestamp,
executionPayload.extraData,
executionPayload.baseFeePerGas,
executionPayload.blockHash,
executionPayload.transactions,
executionPayload.withdrawals.stream()
.map(
withdrawal ->
withdrawal.asInternalWithdrawal(
executionPayloadSchemaCapella.getWithdrawalSchema()))
.collect(toList()))));
() -> SafeFuture.completedFuture(executionPayload.asInternalExecutionPayload(spec)));
builder.blsToExecutionChanges(
() ->
this.blsToExecutionChanges.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import tech.pegasys.teku.api.schema.bellatrix.ExecutionPayloadBellatrix;
import tech.pegasys.teku.infrastructure.bytes.Bytes20;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsCapella;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadBuilder;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadSchema;

public class ExecutionPayloadCapella extends ExecutionPayloadBellatrix implements ExecutionPayload {

Expand Down Expand Up @@ -85,45 +85,18 @@ public ExecutionPayloadCapella(
}

@Override
public Optional<tech.pegasys.teku.spec.datastructures.execution.ExecutionPayload>
asInternalExecutionPayload(final Spec spec, final UInt64 slot) {

final Optional<SchemaDefinitionsCapella> maybeSchema =
spec.atSlot(slot).getSchemaDefinitions().toVersionCapella();

if (maybeSchema.isEmpty()) {
final String message =
String.format("Could not create execution payload at non-bellatrix slot %s", slot);
throw new IllegalArgumentException(message);
}

return maybeSchema.map(
schemaDefinitionsCapella ->
schemaDefinitionsCapella
.getExecutionPayloadSchema()
.toVersionCapella()
.orElseThrow()
.create(
parentHash,
feeRecipient,
stateRoot,
receiptsRoot,
logsBloom,
prevRandao,
blockNumber,
gasLimit,
gasUsed,
timestamp,
extraData,
baseFeePerGas,
blockHash,
transactions,
withdrawals.stream()
.map(
withdrawal ->
withdrawal.asInternalWithdrawal(
schemaDefinitionsCapella.getWithdrawalSchema()))
.collect(toList())));
protected ExecutionPayloadBuilder applyToBuilder(
final ExecutionPayloadSchema<?> executionPayloadSchema,
final ExecutionPayloadBuilder builder) {
return super.applyToBuilder(executionPayloadSchema, builder)
.withdrawals(
() ->
withdrawals.stream()
.map(
withdrawal ->
withdrawal.asInternalWithdrawal(
executionPayloadSchema.getWithdrawalSchemaRequired()))
.collect(toList()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import tech.pegasys.teku.spec.SpecVersion;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBody;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.eip4844.BeaconBlockBodySchemaEip4844;
import tech.pegasys.teku.spec.datastructures.execution.versions.eip4844.ExecutionPayloadSchemaEip4844;
import tech.pegasys.teku.spec.datastructures.type.SszKZGCommitment;

public class BeaconBlockBodyEip4844 extends BeaconBlockBodyAltair {
Expand Down Expand Up @@ -113,10 +112,6 @@ public BeaconBlockBodySchemaEip4844<?> getBeaconBlockBodySchema(final SpecVersio

@Override
public BeaconBlockBody asInternalBeaconBlockBody(final SpecVersion spec) {

final ExecutionPayloadSchemaEip4844 executionPayloadSchemaEip4844 =
getBeaconBlockBodySchema(spec).getExecutionPayloadSchema().toVersionEip4844().orElseThrow();

final SszListSchema<
tech.pegasys.teku.spec.datastructures.operations.SignedBlsToExecutionChange, ?>
blsToExecutionChangesSchema =
Expand All @@ -127,30 +122,7 @@ public BeaconBlockBody asInternalBeaconBlockBody(final SpecVersion spec) {
spec,
(builder) -> {
builder.executionPayload(
() ->
SafeFuture.completedFuture(
executionPayloadSchemaEip4844.create(
executionPayload.parentHash,
executionPayload.feeRecipient,
executionPayload.stateRoot,
executionPayload.receiptsRoot,
executionPayload.logsBloom,
executionPayload.prevRandao,
executionPayload.blockNumber,
executionPayload.gasLimit,
executionPayload.gasUsed,
executionPayload.timestamp,
executionPayload.extraData,
executionPayload.baseFeePerGas,
executionPayload.excessDataGas,
executionPayload.blockHash,
executionPayload.transactions,
executionPayload.withdrawals.stream()
.map(
withdrawal ->
withdrawal.asInternalWithdrawal(
executionPayloadSchemaEip4844.getWithdrawalSchema()))
.collect(toList()))));
() -> SafeFuture.completedFuture(executionPayload.asInternalExecutionPayload(spec)));
builder.blsToExecutionChanges(
() ->
this.blsToExecutionChanges.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

package tech.pegasys.teku.api.schema.eip4844;

import static java.util.stream.Collectors.toList;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.MoreObjects;
Expand All @@ -29,8 +27,8 @@
import tech.pegasys.teku.api.schema.capella.Withdrawal;
import tech.pegasys.teku.infrastructure.bytes.Bytes20;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsEip4844;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadBuilder;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadSchema;

public class ExecutionPayloadEip4844 extends ExecutionPayloadCapella implements ExecutionPayload {

Expand Down Expand Up @@ -81,46 +79,10 @@ public ExecutionPayloadEip4844(
}

@Override
public Optional<tech.pegasys.teku.spec.datastructures.execution.ExecutionPayload>
asInternalExecutionPayload(final Spec spec, final UInt64 slot) {

final Optional<SchemaDefinitionsEip4844> maybeSchema =
spec.atSlot(slot).getSchemaDefinitions().toVersionEip4844();

if (maybeSchema.isEmpty()) {
final String message =
String.format("Could not create execution payload at non-EIP4844 slot %s", slot);
throw new IllegalArgumentException(message);
}

return maybeSchema.map(
schemaDefinitionsEip4844 ->
schemaDefinitionsEip4844
.getExecutionPayloadSchema()
.toVersionEip4844()
.orElseThrow()
.create(
parentHash,
feeRecipient,
stateRoot,
receiptsRoot,
logsBloom,
prevRandao,
blockNumber,
gasLimit,
gasUsed,
timestamp,
extraData,
baseFeePerGas,
excessDataGas,
blockHash,
transactions,
withdrawals.stream()
.map(
withdrawal ->
withdrawal.asInternalWithdrawal(
schemaDefinitionsEip4844.getWithdrawalSchema()))
.collect(toList())));
protected ExecutionPayloadBuilder applyToBuilder(
final ExecutionPayloadSchema<?> executionPayloadSchema,
final ExecutionPayloadBuilder builder) {
return super.applyToBuilder(executionPayloadSchema, builder).excessDataGas(() -> excessDataGas);
}

@Override
Expand Down
Loading

0 comments on commit 2d60d6a

Please sign in to comment.