Skip to content

Commit

Permalink
kinda fed up with this whole approach tbh, putting branch on ice
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Florentine <[email protected]>
  • Loading branch information
jflo committed Sep 20, 2023
1 parent 958972c commit fc5be6b
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ public JsonRpcRequest(
}
}

public JsonRpcRequest( final String version,
final String method,
final JsonRpcRequestId id,
final Object[] params) {
this.version = version;
this.method = method;
this.params = params;
this.id = id;
if (method == null) {
throw new InvalidJsonRpcRequestException("Field 'method' is required");
}
}

@JsonGetter("id")
public Object getId() {
return id == null ? null : id.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,14 @@ public final JsonRpcResponse response(final JsonRpcRequestContext request) {
public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext) {
engineCallListener.executionEngineCalled();
final Object reqId = requestContext.getRequest().getId();
var newPayloadParam = parseVersionedParam(requestContext);
final ValidationResult<RpcErrorType> parameterValidationResult =
validateRequest(requestContext);
validateRequest(newPayloadParam, requestContext);

if (!parameterValidationResult.isValid()) {
return new JsonRpcErrorResponse(reqId, parameterValidationResult);
}
var newPayloadParam = parseVersionedParam(requestContext);

BlockIdentifier blockId =
new BlockIdentifier(
newPayloadParam.getBlockNumber(),
Expand Down Expand Up @@ -256,7 +257,7 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
reqId,
blockId,
null,
getInvalidBlockHashStatus(),
INVALID,
blobValidationResult.getErrorMessage());
}

Expand Down Expand Up @@ -323,7 +324,7 @@ protected EngineStatus getInvalidBlockHashStatus() {
return INVALID;
}

protected abstract ValidationResult<RpcErrorType> validateRequest(
protected abstract <P extends NewPayloadParameterV1> ValidationResult<RpcErrorType> validateRequest( final P newPayloadParam,
final JsonRpcRequestContext requestContext);

public record EngineBlockValidationResult(EngineStatus status, BlockValidationResult validationResult) {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.engine.DepositParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.engine.NewPayloadParameterEIP6110;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.engine.NewPayloadParameterV1;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.engine.NewPayloadParameterV3;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
import org.hyperledger.besu.ethereum.core.BlockHeaderBuilder;
import org.hyperledger.besu.ethereum.core.Deposit;
Expand Down Expand Up @@ -63,15 +64,15 @@ public String getName() {
}

@Override
protected ValidationResult<RpcErrorType> validateRequest(
protected <P extends NewPayloadParameterV1> ValidationResult<RpcErrorType> validateRequest(final P newPayloadParam,
final JsonRpcRequestContext requestContext) {
NewPayloadParameterEIP6110 newPayloadParam =
requestContext.getRequiredParameter(0, NewPayloadParameterEIP6110.class);

ValidationResult<RpcErrorType> v3Validity = super.validateRequest((NewPayloadParameterV3)newPayloadParam, requestContext);

final Optional<List<Deposit>> maybeDeposits =
Optional.ofNullable(newPayloadParam.getDeposits())
.map(ds -> ds.stream().map(DepositParameter::toDeposit).collect(toList()));
Optional.ofNullable(((NewPayloadParameterEIP6110)newPayloadParam).getDeposits())
.map(ds -> ds.stream().map(DepositParameter::toDeposit).collect(toList()));

ValidationResult<RpcErrorType> v3Validity = super.validateRequest(requestContext);
if (!v3Validity.isValid()) {
return v3Validity;
} else if (!getDepositsValidator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected EngineStatus getInvalidBlockHashStatus() {
}

@Override
protected ValidationResult<RpcErrorType> validateRequest(final JsonRpcRequestContext requestContext) {
protected <P extends NewPayloadParameterV1> ValidationResult<RpcErrorType> validateRequest(final P newPayloadParam, final JsonRpcRequestContext requestContext) {
return ValidationResult.valid();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,23 @@ protected <P extends NewPayloadParameterV1> P parseVersionedParam(final JsonRpcR
}

@Override
protected ValidationResult<RpcErrorType> validateRequest(
protected <P extends NewPayloadParameterV1> ValidationResult<RpcErrorType> validateRequest( final P newPayloadParam,
final JsonRpcRequestContext requestContext) {

final NewPayloadParameterV2 newPayloadParam =
requestContext.getRequiredParameter(0, NewPayloadParameterV2.class);

WithdrawalsValidator validator = getWithdrawalsValidator(
protocolSchedule.get(), newPayloadParam.getTimestamp(), newPayloadParam.getBlockNumber());
Optional<List<Withdrawal>> maybeWithdrawals = Optional.empty();
String errorMessage = "Invalid withdrawals";
if(validator instanceof WithdrawalsValidator.AllowedWithdrawals) {
NewPayloadParameterV2 npp = (NewPayloadParameterV2) newPayloadParam;
maybeWithdrawals =
Optional.ofNullable(newPayloadParam.getWithdrawals())
Optional.ofNullable(npp.getWithdrawals())
.map(ws -> ws.stream().map(WithdrawalParameter::toWithdrawal).collect(toList()));


} else {
if(newPayloadParam.getWithdrawals() != null) {
if(newPayloadParam instanceof NewPayloadParameterV2 && ((NewPayloadParameterV2)newPayloadParam).getWithdrawals() != null) {
errorMessage = errorMessage + ", shanghai fork not enabled yet, payload had withdrawals field";
return ValidationResult.invalid(INVALID_PARAMS, errorMessage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ protected <P extends NewPayloadParameterV1> P parseVersionedParam(final JsonRpcR
return (P) request.getRequiredParameter(0, NewPayloadParameterV3.class);
}
@Override
protected ValidationResult<RpcErrorType> validateRequest(
protected <P extends NewPayloadParameterV1> ValidationResult<RpcErrorType> validateRequest(final P newPayloadParam,
final JsonRpcRequestContext requestContext) {
NewPayloadParameterV3 newPayloadParam =
requestContext.getRequiredParameter(0, NewPayloadParameterV3.class);

final Optional<List<String>> maybeVersionedHashParam =
requestContext.getOptionalList(1, String.class);
Expand All @@ -85,20 +83,23 @@ protected ValidationResult<RpcErrorType> validateRequest(
final Optional<Bytes32> maybeParentBeaconBlockRoot =
maybeParentBeaconBlockRootParam.map(Bytes32::fromHexString);
*/
ValidationResult<RpcErrorType> v2Validity = super.validateRequest(requestContext);
ValidationResult<RpcErrorType> v2Validity = super.validateRequest(newPayloadParam, requestContext);
if (!v2Validity.isValid()) {
return v2Validity;
} else if (newPayloadParam.getBlobGasUsed() == null
|| newPayloadParam.getExcessBlobGas() == null) {
return ValidationResult.invalid(RpcErrorType.INVALID_PARAMS, "Missing blob gas fields");
} else if (maybeVersionedHashParam == null) {
return ValidationResult.invalid(
RpcErrorType.INVALID_PARAMS, "Missing versioned hashes field");
} else if (maybeParentBeaconBlockRootParam.isEmpty()) {
return ValidationResult.invalid(
RpcErrorType.INVALID_PARAMS, "Missing parent beacon block root field");
} else {
return ValidationResult.valid();
NewPayloadParameterV3 npp = (NewPayloadParameterV3) newPayloadParam;
if (npp.getBlobGasUsed() == null
|| npp.getExcessBlobGas() == null) {
return ValidationResult.invalid(RpcErrorType.INVALID_PARAMS, "Missing blob gas fields");
} else if (maybeVersionedHashParam == null) {
return ValidationResult.invalid(
RpcErrorType.INVALID_PARAMS, "Missing versioned hashes field");
} else if (maybeParentBeaconBlockRootParam.isEmpty()) {
return ValidationResult.invalid(
RpcErrorType.INVALID_PARAMS, "Missing parent beacon block root field");
} else {
return ValidationResult.valid();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
Expand All @@ -39,6 +40,7 @@
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestId;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.ExecutionEngineJsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
Expand Down Expand Up @@ -93,7 +95,7 @@ AbstractEngineNewPayload create(
}

protected AbstractEngineNewPayload method;
protected Optional<Bytes32> maybeParentBeaconBlockRoot = Optional.empty();
//protected Optional<Bytes32> maybeParentBeaconBlockRoot = Optional.empty();

public AbstractEngineNewPayloadTest() {}

Expand All @@ -114,6 +116,8 @@ public AbstractEngineNewPayloadTest() {}

@Mock protected EngineCallListener engineCallListener;

protected final ObjectMapper mapper = new ObjectMapper();

@BeforeEach
@Override
public void before() {
Expand Down Expand Up @@ -398,7 +402,7 @@ public void shouldReturnValidIfProtocolScheduleIsEmpty() {

protected JsonRpcResponse respondTo(final Object[] params) {
return method.response(
new JsonRpcRequestContext(new JsonRpcRequest("2.0", this.method.getName(), params)));
new JsonRpcRequestContext(new JsonRpcRequest("2.0", this.method.getName(), new JsonRpcRequestId(1), params)));
}

abstract protected String createNewPayloadParam(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.BlobGas;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.BlockProcessingOutputs;
import org.hyperledger.besu.ethereum.BlockProcessingResult;
Expand Down Expand Up @@ -64,7 +65,6 @@ public EngineNewPayloadEIP6110Test() {}
@Override
public void before() {
super.before();
maybeParentBeaconBlockRoot = Optional.of(Bytes32.ZERO);
this.method =
new EngineNewPayloadEIP6110(
vertx,
Expand Down Expand Up @@ -203,8 +203,7 @@ protected BlockHeaderTestFixture createBlockHeaderTestFixture(
.blobGasUsed(100L)
.depositsRoot(maybeDeposits.map(BodyValidation::depositsRoot).orElse(null))
.transactionsRoot(BodyValidation.transactionsRoot(maybeTransactions))
.parentBeaconBlockRoot(
maybeParentBeaconBlockRoot.isPresent() ? maybeParentBeaconBlockRoot : null);
.parentBeaconBlockRoot(Optional.of(Hash.EMPTY_TRIE_HASH));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ protected BlockHeaderTestFixture createBlockHeaderTestFixture(
.timestamp(parentBlockHeader.getTimestamp() + 1)
.extraData(Bytes.fromHexString("0xDEADBEEF"))
.depositsRoot(maybeDeposits.map(BodyValidation::depositsRoot).orElse(null))
.transactionsRoot(BodyValidation.transactionsRoot(maybeTransactions))
.parentBeaconBlockRoot(maybeParentBeaconBlockRoot);
.transactionsRoot(BodyValidation.transactionsRoot(maybeTransactions));

if(maybeWithdrawals.isPresent() && maybeWithdrawals.get().size() > 0) {
testFixture.withdrawalsRoot(maybeWithdrawals.map(BodyValidation::withdrawalsRoot).orElse(null));
}
Expand Down
Loading

0 comments on commit fc5be6b

Please sign in to comment.