Skip to content

Commit

Permalink
v2 tests passing
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Florentine <[email protected]>
  • Loading branch information
jflo committed Sep 18, 2023
1 parent 4a88094 commit dbe7c9b
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
"Failed to parse block body: "+rlpE.getMessage());
}
Hash txRoot = BodyValidation.transactionsRoot(allegedBody.getTransactions());
BlockHeader allegedHeader = composeNewHeader(requestContext, txRoot).buildBlockHeader();
BlockHeader allegedHeader = composeNewHeader(requestContext, newPayloadParam, txRoot).buildBlockHeader();

Block allegedBlock = new Block(allegedHeader, allegedBody);

Expand All @@ -207,20 +207,8 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
return respondWith(reqId, blockId, null, SYNCING);
}

/*
if (allegedHeader.getExtraData() == null) {
return respondWithInvalid(
reqId,
allegedHeader,
mergeCoordinator.getLatestValidAncestor(allegedHeader.getParentHash()).orElse(null),
INVALID,
"Field extraData must not be null");
}
*/

// ensure the block hash matches the blockParam hash
// this must be done before any other check
// this must be done before other block validity checks
if (!allegedHeader.getHash().equals(blockId.blockHash())) {
String errorMessage =
String.format(
Expand Down Expand Up @@ -341,8 +329,8 @@ protected abstract ValidationResult<RpcErrorType> validateRequest(
public record EngineBlockValidationResult(EngineStatus status, BlockValidationResult validationResult) {};
protected abstract <P extends NewPayloadParameterV1> EngineBlockValidationResult validateBlock(P payload);

protected abstract BlockHeaderBuilder composeNewHeader(
final JsonRpcRequestContext requestContext, final Hash txroot);
protected abstract <P extends NewPayloadParameterV1> BlockHeaderBuilder composeNewHeader(
final JsonRpcRequestContext requestContext, final P newPayloadParam, final Hash txroot);

protected abstract ValidationResult<RpcErrorType> validateBlobs(
final Block newBlock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
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.response.RpcErrorType;
import org.hyperledger.besu.ethereum.core.BlockHeaderBuilder;
import org.hyperledger.besu.ethereum.core.Deposit;
Expand Down Expand Up @@ -105,15 +106,14 @@ protected ValidationResult<RpcErrorType> validateForkSupported(
}

@Override
protected BlockHeaderBuilder composeNewHeader(
final JsonRpcRequestContext context, final Hash txRoot) {
final NewPayloadParameterEIP6110 blockParam =
context.getRequiredParameter(0, NewPayloadParameterEIP6110.class);
protected <P extends NewPayloadParameterV1> BlockHeaderBuilder composeNewHeader(
final JsonRpcRequestContext context, final P newPayloadParam, final Hash txRoot) {


final Optional<List<Deposit>> maybeDeposits =
Optional.ofNullable(blockParam.getDeposits())
Optional.ofNullable(((NewPayloadParameterEIP6110)newPayloadParam).getDeposits())
.map(ds -> ds.stream().map(DepositParameter::toDeposit).collect(toList()));
final BlockHeaderBuilder builder = super.composeNewHeader(context, txRoot);
final BlockHeaderBuilder builder = super.composeNewHeader(context, newPayloadParam, txRoot);
builder.depositsRoot(maybeDeposits.map(BodyValidation::depositsRoot).orElse(null));
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,27 +92,26 @@ protected <P extends NewPayloadParameterV1> EngineBlockValidationResult validate

@Override
@SuppressWarnings("signedness:argument")
protected BlockHeaderBuilder composeNewHeader(
final JsonRpcRequestContext requestContext, final Hash txRoot) {
NewPayloadParameterV1 blockParam =
requestContext.getRequiredParameter(0, NewPayloadParameterV1.class);
protected <P extends NewPayloadParameterV1> BlockHeaderBuilder composeNewHeader(
final JsonRpcRequestContext requestContext, final P newPayloadParam, final Hash txRoot) {

final BlockHeaderBuilder builder = new BlockHeaderBuilder();
builder
.parentHash(blockParam.getParentHash())
.parentHash(newPayloadParam.getParentHash())
.ommersHash(OMMERS_HASH_CONSTANT)
.coinbase(blockParam.getFeeRecipient())
.stateRoot(blockParam.getStateRoot())
.coinbase(newPayloadParam.getFeeRecipient())
.stateRoot(newPayloadParam.getStateRoot())
.transactionsRoot(txRoot)
.receiptsRoot(blockParam.getReceiptsRoot())
.logsBloom(blockParam.getLogsBloom())
.receiptsRoot(newPayloadParam.getReceiptsRoot())
.logsBloom(newPayloadParam.getLogsBloom())
.difficulty(Difficulty.ZERO)
.number(blockParam.getBlockNumber())
.gasLimit(blockParam.getGasLimit())
.gasUsed(blockParam.getGasUsed())
.timestamp(blockParam.getTimestamp())
.extraData(Bytes.fromHexString(blockParam.getExtraData()))
.baseFee(blockParam.getBaseFeePerGas())
.prevRandao(blockParam.getPrevRandao())
.number(newPayloadParam.getBlockNumber())
.gasLimit(newPayloadParam.getGasLimit())
.gasUsed(newPayloadParam.getGasUsed())
.timestamp(newPayloadParam.getTimestamp())
.extraData(Bytes.fromHexString(newPayloadParam.getExtraData()))
.baseFee(newPayloadParam.getBaseFeePerGas())
.prevRandao(newPayloadParam.getPrevRandao())
.nonce(0)
.blockHeaderFunctions(headerFunctions);
return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,16 @@ public EngineNewPayloadV2(

@Override
@SuppressWarnings("unchecked")
protected <P extends NewPayloadParameterV1> P parseVersionedParam(final JsonRpcRequestContext request) {
return (P) request.getRequiredParameter(0, NewPayloadParameterV2.class);
protected <P extends NewPayloadParameterV1> P parseVersionedParam(final JsonRpcRequestContext requestContext) {
final NewPayloadParameterV1 newPayloadParam =
requestContext.getRequiredParameter(0, NewPayloadParameterV1.class);
WithdrawalsValidator validator = getWithdrawalsValidator(
protocolSchedule.get(), newPayloadParam.getTimestamp(), newPayloadParam.getBlockNumber());
if(validator instanceof WithdrawalsValidator.AllowedWithdrawals) {
return (P) requestContext.getRequiredParameter(0, NewPayloadParameterV2.class);
} else {
return (P) newPayloadParam;
}
}

@Override
Expand Down Expand Up @@ -100,16 +108,17 @@ protected ValidationResult<RpcErrorType> validateRequest(
}

@Override
protected BlockHeaderBuilder composeNewHeader(
final JsonRpcRequestContext requestContext, final Hash txRoot) {
BlockHeaderBuilder builder = super.composeNewHeader(requestContext, txRoot);
NewPayloadParameterV2 blockParam =
requestContext.getRequiredParameter(0, NewPayloadParameterV2.class);
final Optional<List<Withdrawal>> maybeWithdrawals =
Optional.ofNullable(blockParam.getWithdrawals())
.map(ws -> ws.stream().map(WithdrawalParameter::toWithdrawal).collect(toList()));
protected <P extends NewPayloadParameterV1> BlockHeaderBuilder composeNewHeader(
final JsonRpcRequestContext requestContext, final P newPayloadParam, final Hash txRoot) {
BlockHeaderBuilder builder = super.composeNewHeader(requestContext, newPayloadParam, txRoot);
if(newPayloadParam instanceof NewPayloadParameterV2) {
final Optional<List<Withdrawal>> maybeWithdrawals =
Optional.ofNullable(((NewPayloadParameterV2)newPayloadParam).getWithdrawals())
.map(ws -> ws.stream().map(WithdrawalParameter::toWithdrawal).collect(toList()));

builder.withdrawalsRoot(maybeWithdrawals.map(BodyValidation::withdrawalsRoot).orElse(BodyValidation.withdrawalsRoot(Collections.emptyList())));
}

builder.withdrawalsRoot(maybeWithdrawals.map(BodyValidation::withdrawalsRoot).orElse(BodyValidation.withdrawalsRoot(Collections.emptyList())));
return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,14 @@ private Optional<List<VersionedHash>> extractVersionedHashes(

@Override
@SuppressWarnings("signedness:argument")
protected BlockHeaderBuilder composeNewHeader(
final JsonRpcRequestContext context, final Hash txRoot) {
final NewPayloadParameterV3 blockParam =
context.getRequiredParameter(0, NewPayloadParameterV3.class);
protected <P extends NewPayloadParameterV1> BlockHeaderBuilder composeNewHeader(
final JsonRpcRequestContext context, final P newPayloadParam, final Hash txRoot) {
String parentBeaconBlockRootParam = context.getRequiredParameter(2, String.class);

final BlockHeaderBuilder builder = super.composeNewHeader(context, txRoot);
final BlockHeaderBuilder builder = super.composeNewHeader(context, newPayloadParam, txRoot);
builder
.blobGasUsed(blockParam.getBlobGasUsed())
.excessBlobGas(BlobGas.fromHexString(blockParam.getExcessBlobGas()))
.blobGasUsed(((NewPayloadParameterV3)newPayloadParam).getBlobGasUsed())
.excessBlobGas(BlobGas.fromHexString(((NewPayloadParameterV3)newPayloadParam).getExcessBlobGas()))
.parentBeaconBlockRoot(Bytes32.fromHexString(parentBeaconBlockRootParam));
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ public NewPayloadV2Serializer() {
protected void serializeToOpenObject(final NewPayloadParameterV2 value, final JsonGenerator gen, final SerializerProvider provider) throws IOException {
v1Serializer.serializeToOpenObject(value, gen, provider);

gen.writeArrayFieldStart("withdrawals");

if(value.getWithdrawals() != null) {
gen.writeArrayFieldStart("withdrawals");
for (WithdrawalParameter withdrawal : value.getWithdrawals()) {
gen.writeObject(withdrawal);
}
gen.writeEndArray();
}
gen.writeEndArray();

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ public void shouldReturnValidIfWithdrawalsIsNotNull_WhenWithdrawalsAllowed() thr
}

@Test
public void shouldReturnValidIfWithdrawalsIsNull_WhenWithdrawalsProhibited() throws JsonProcessingException {
final List<WithdrawalParameter> withdrawals = null;
public void shouldReturnValidIfWithdrawalsIsNull_WhenWithdrawalsProhibited() {
when(protocolSpec.getWithdrawalsValidator())
.thenReturn(new WithdrawalsValidator.ProhibitedWithdrawals());
BlockHeader mockHeader =
Expand All @@ -120,7 +119,7 @@ public void shouldReturnValidIfWithdrawalsIsNull_WhenWithdrawalsProhibited() thr
.thenReturn(Optional.of(mock(BlockHeader.class)));
var resp =
respondTo(
new Object[] {createNewPayloadParam(mockHeader, Collections.emptyList(), withdrawals)});
new Object[] {createNewPayloadParam(mockHeader, Collections.emptyList())});

assertValidResponse(mockHeader, resp);
}
Expand Down Expand Up @@ -207,15 +206,18 @@ protected BlockHeaderTestFixture createBlockHeaderTestFixture(
final Optional<List<Deposit>> maybeDeposits) {
BlockHeader parentBlockHeader =
new BlockHeaderTestFixture().baseFeePerGas(Wei.ONE).buildHeader();
return new BlockHeaderTestFixture()
BlockHeaderTestFixture testFixture = new BlockHeaderTestFixture()
.baseFeePerGas(Wei.ONE)
.parentHash(parentBlockHeader.getParentHash())
.number(parentBlockHeader.getNumber() + 1)
.timestamp(parentBlockHeader.getTimestamp() + 1)
.extraData(Bytes.fromHexString("0xDEADBEEF"))
.withdrawalsRoot(maybeWithdrawals.map(BodyValidation::withdrawalsRoot).orElse(null))
.depositsRoot(maybeDeposits.map(BodyValidation::depositsRoot).orElse(null))
.transactionsRoot(BodyValidation.transactionsRoot(maybeTransactions))
.parentBeaconBlockRoot(maybeParentBeaconBlockRoot);
if(maybeWithdrawals.isPresent() && maybeWithdrawals.get().size() > 0) {
testFixture.withdrawalsRoot(maybeWithdrawals.map(BodyValidation::withdrawalsRoot).orElse(null));
}
return testFixture;
}
}

0 comments on commit dbe7c9b

Please sign in to comment.