Skip to content

Commit

Permalink
feat:add optimism attribute fields
Browse files Browse the repository at this point in the history
Signed-off-by: Chen Kai <[email protected]>
  • Loading branch information
GrapeBaBa committed Dec 1, 2023
1 parent 7c649af commit 5a30cf3
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,18 @@ default boolean isChainPruningEnabled() {
* @return the boolean
*/
boolean isPostMergeAtGenesis();

/**
* Is configured for optimism.
*
* @return the boolean
*/
boolean isOptimism();

/**
* Sets is configured for optimism.
*
* @param isOptimism the boolean
*/
default void setOptimism(final boolean isOptimism) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public class PostMergeContext implements MergeContext {
// https://github.com/hyperledger/besu/pull/4703 is merged.
private boolean isChainPruningEnabled = false;

private boolean isOptimism;

/** Instantiates a new Post merge context. */
@VisibleForTesting
PostMergeContext() {
Expand Down Expand Up @@ -335,6 +337,16 @@ public boolean isPostMergeAtGenesis() {
return this.isPostMergeAtGenesis;
}

@Override
public void setOptimism(final boolean isOptimism) {
this.isOptimism = isOptimism;
}

@Override
public boolean isOptimism() {
return this.isOptimism;
}

/**
* Sets whether it is post merge at genesis
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,14 @@ public boolean isCheckpointPostMergeSync() {
public boolean isPostMergeAtGenesis() {
return postMergeContext.isPostMergeAtGenesis();
}

@Override
public boolean isOptimism() {
return postMergeContext.isOptimism();
}

@Override
public void setOptimism(final boolean isOptimism) {
postMergeContext.setOptimism(isOptimism);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public String getName() {
@Override
protected Optional<JsonRpcErrorResponse> isPayloadAttributesValid(
final Object requestId, final EnginePayloadAttributesParameter payloadAttributes) {
if (this.mergeContext.get().isOptimism()) {
return Optional.empty();
}

if (payloadAttributes.getTimestamp() >= cancunTimestamp) {
if (payloadAttributes.getParentBeaconBlockRoot() == null
|| payloadAttributes.getParentBeaconBlockRoot().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected ValidationResult<RpcErrorType> validateParameter(
} else if (fcuParameter.getFinalizedBlockHash() == null) {
return ValidationResult.invalid(RpcErrorType.INVALID_PARAMS, "Missing finalized block hash");
}
if (maybePayloadAttributes.isPresent()) {
if (maybePayloadAttributes.isPresent() && (!this.mergeContext.get().isOptimism())) {
if (maybePayloadAttributes.get().getParentBeaconBlockRoot() == null) {
return ValidationResult.invalid(
RpcErrorType.INVALID_PARAMS, "Missing parent beacon block root hash");
Expand All @@ -73,6 +73,10 @@ protected ValidationResult<RpcErrorType> validateParameter(

@Override
protected ValidationResult<RpcErrorType> validateForkSupported(final long blockTimestamp) {
if (this.mergeContext.get().isOptimism()) {
return ValidationResult.valid();
}

if (protocolSchedule.isPresent()) {
if (cancun.isPresent() && blockTimestamp >= cancun.get().milestone()) {
return ValidationResult.valid();
Expand All @@ -90,6 +94,10 @@ protected ValidationResult<RpcErrorType> validateForkSupported(final long blockT
@Override
protected Optional<JsonRpcErrorResponse> isPayloadAttributesValid(
final Object requestId, final EnginePayloadAttributesParameter payloadAttributes) {
if (this.mergeContext.get().isOptimism()) {
return Optional.empty();
}

if (payloadAttributes.getParentBeaconBlockRoot() == null) {
LOG.error(
"Parent beacon block root hash not present in payload attributes after cancun hardfork");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.hyperledger.besu.datatypes.Address;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -32,19 +33,30 @@ public class EnginePayloadAttributesParameter {
final List<WithdrawalParameter> withdrawals;
private final Bytes32 parentBeaconBlockRoot;

// optimism payload attributes
private final Boolean noTxPool;
private final String[] transactions;
private final Long gasLimit;

@JsonCreator
public EnginePayloadAttributesParameter(
@JsonProperty("timestamp") final String timestamp,
@JsonProperty("prevRandao") final String prevRandao,
@JsonProperty("suggestedFeeRecipient") final String suggestedFeeRecipient,
@JsonProperty("withdrawals") final List<WithdrawalParameter> withdrawals,
@JsonProperty("parentBeaconBlockRoot") final String parentBeaconBlockRoot) {
@JsonProperty("parentBeaconBlockRoot") final String parentBeaconBlockRoot,
@JsonProperty("noTxPool") final Boolean noTxPool,
@JsonProperty("transactions") final String[] transactions,
@JsonProperty("gasLimit") final Long gasLimit) {
this.timestamp = Long.decode(timestamp);
this.prevRandao = Bytes32.fromHexString(prevRandao);
this.suggestedFeeRecipient = Address.fromHexString(suggestedFeeRecipient);
this.withdrawals = withdrawals;
this.parentBeaconBlockRoot =
parentBeaconBlockRoot == null ? null : Bytes32.fromHexString(parentBeaconBlockRoot);
this.noTxPool = noTxPool;
this.transactions = transactions;
this.gasLimit = gasLimit;
}

public Long getTimestamp() {
Expand All @@ -67,6 +79,18 @@ public List<WithdrawalParameter> getWithdrawals() {
return withdrawals;
}

public Boolean isNoTxPool() {
return noTxPool;
}

public String[] getTransactions() {
return transactions;
}

public Long getGasLimit() {
return gasLimit;
}

public String serialize() {
final JsonObject json =
new JsonObject()
Expand All @@ -81,6 +105,15 @@ public String serialize() {
if (parentBeaconBlockRoot != null) {
json.put("parentBeaconBlockRoot", parentBeaconBlockRoot.toHexString());
}
if (noTxPool != null) {
json.put("noTxPool", noTxPool);
}
if (transactions != null) {
json.put("transactions", Arrays.stream(transactions).toList());
}
if (gasLimit != null) {
json.put("gasLimit", gasLimit);
}
return json.encode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ public void shouldReturnValidWithoutFinalizedWithPayload() {
Bytes32.fromHexStringLenient("0xDEADBEEF").toHexString(),
Address.ECREC.toString(),
null,
null,
null,
null,
null);
var mockPayloadId =
PayloadIdentifier.forPayloadParams(
Expand Down Expand Up @@ -430,6 +433,9 @@ public void shouldIgnoreUpdateToOldHeadAndNotPreparePayload() {
Bytes32.fromHexStringLenient("0xDEADBEEF").toHexString(),
Address.ECREC.toString(),
null,
null,
null,
null,
null);

var resp =
Expand Down Expand Up @@ -467,6 +473,9 @@ public void shouldReturnInvalidIfPayloadTimestampNotGreaterThanHead() {
Bytes32.fromHexStringLenient("0xDEADBEEF").toHexString(),
Address.ECREC.toString(),
emptyList(),
null,
null,
null,
null);

var resp =
Expand All @@ -492,6 +501,9 @@ public void shouldReturnInvalidIfWithdrawalsIsNotNull_WhenWithdrawalsProhibited(
Bytes32.fromHexStringLenient("0xDEADBEEF").toHexString(),
Address.ECREC.toString(),
emptyList(),
null,
null,
null,
null);

var resp =
Expand All @@ -517,6 +529,9 @@ public void shouldReturnValidIfWithdrawalsIsNull_WhenWithdrawalsProhibited() {
Bytes32.fromHexStringLenient("0xDEADBEEF").toHexString(),
Address.ECREC.toString(),
null,
null,
null,
null,
null);

var mockPayloadId =
Expand Down Expand Up @@ -561,6 +576,9 @@ public void shouldReturnInvalidIfWithdrawalsIsNull_WhenWithdrawalsAllowed() {
Bytes32.fromHexStringLenient("0xDEADBEEF").toHexString(),
Address.ECREC.toString(),
null,
null,
null,
null,
null);

var resp =
Expand Down Expand Up @@ -597,6 +615,9 @@ public void shouldReturnValidIfWithdrawalsIsNotNull_WhenWithdrawalsAllowed() {
Bytes32.fromHexStringLenient("0xDEADBEEF").toHexString(),
Address.ECREC.toString(),
withdrawalParameters,
null,
null,
null,
null);

final Optional<List<Withdrawal>> withdrawals =
Expand Down Expand Up @@ -646,6 +667,9 @@ public void shouldReturnValidIfProtocolScheduleIsEmpty() {
Bytes32.fromHexStringLenient("0xDEADBEEF").toHexString(),
Address.ECREC.toString(),
null,
null,
null,
null,
null);

var mockPayloadId =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,20 @@ public void serialize_WithdrawalsPresent() {

private EnginePayloadAttributesParameter parameterWithdrawalsOmitted() {
return new EnginePayloadAttributesParameter(
TIMESTAMP, PREV_RANDAO, SUGGESTED_FEE_RECIPIENT_ADDRESS, null, null);
TIMESTAMP, PREV_RANDAO, SUGGESTED_FEE_RECIPIENT_ADDRESS, null, null, null, null, null);
}

private EnginePayloadAttributesParameter parameterWithdrawalsPresent() {
final List<WithdrawalParameter> withdrawals = List.of(WITHDRAWAL_PARAM_1, WITHDRAWAL_PARAM_2);
return new EnginePayloadAttributesParameter(
TIMESTAMP, PREV_RANDAO, SUGGESTED_FEE_RECIPIENT_ADDRESS, withdrawals, null);
TIMESTAMP,
PREV_RANDAO,
SUGGESTED_FEE_RECIPIENT_ADDRESS,
withdrawals,
null,
null,
null,
null);
}

// TODO: add a parent beacon block root test here
Expand Down

0 comments on commit 5a30cf3

Please sign in to comment.