Skip to content

Commit

Permalink
[4844] Swap parameters order and remove unused method (#5668)
Browse files Browse the repository at this point in the history
* Swap parameters order

* Remove method

* Move blob validation to EngineNewPayloadV3 class

Signed-off-by: Gabriel-Trintinalia <gabriel.trintinalia@consensys.net>
  • Loading branch information
Gabriel-Trintinalia authored and jflo committed Jul 5, 2023
1 parent 2bb9972 commit 705c251
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public enum RpcMethod {
ENGINE_GET_PAYLOAD_V1("engine_getPayloadV1"),
ENGINE_GET_PAYLOAD_V2("engine_getPayloadV2"),
ENGINE_GET_PAYLOAD_V3("engine_getPayloadV3"),
ENGINE_EXECUTE_PAYLOAD("engine_executePayloadV1"),
ENGINE_NEW_PAYLOAD_V1("engine_newPayloadV1"),
ENGINE_NEW_PAYLOAD_V2("engine_newPayloadV2"),
ENGINE_NEW_PAYLOAD_V3("engine_newPayloadV3"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.hyperledger.besu.consensus.merge.blockcreation.MergeMiningCoordinator;
import org.hyperledger.besu.datatypes.DataGas;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.VersionedHash;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.BlockProcessingResult;
import org.hyperledger.besu.ethereum.ProtocolContext;
Expand Down Expand Up @@ -67,7 +66,6 @@
import io.vertx.core.Vertx;
import io.vertx.core.json.Json;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -201,17 +199,11 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
return respondWithInvalid(reqId, blockParam, null, getInvalidBlockHashStatus(), errorMessage);
}

var blobTransactions =
transactions.stream().filter(transaction -> transaction.getType().supportsBlob()).toList();

// Validate Blob Transactions
var blobTransactionsInvalidResult =
validateBlobTransactions(blobTransactions, maybeVersionedHashParam);

if (blobTransactionsInvalidResult.isPresent()) {
LOG.debug(blobTransactionsInvalidResult.get());
return respondWithInvalid(
reqId, blockParam, null, INVALID, blobTransactionsInvalidResult.get());
// Validate transactions
var transactionValidationResult =
validateTransactions(blockParam, reqId, transactions, maybeVersionedHashParam);
if (transactionValidationResult.isPresent()) {
return transactionValidationResult.get();
}

// do we already have this payload
Expand Down Expand Up @@ -304,35 +296,6 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
}
}

private Optional<String> validateBlobTransactions(
final List<Transaction> blobTransactions,
final Optional<List<String>> maybeVersionedHashParam) {

List<Bytes32> versionedHashesParam =
maybeVersionedHashParam
.map(strings -> strings.stream().map(Bytes32::fromHexStringStrict).toList())
.orElseGet(ArrayList::new);

final List<Bytes32> transactionVersionedHashes = new ArrayList<>();

for (Transaction transaction : blobTransactions) {
if (transaction.getType().supportsBlob()) {
var versionedHashes = transaction.getVersionedHashes();
if (versionedHashes.isEmpty()) {
return Optional.of("There must be at least one blob");
}
transactionVersionedHashes.addAll(
versionedHashes.get().stream().map(VersionedHash::toBytes).toList());
}
}

// check list contents
if (!versionedHashesParam.equals(transactionVersionedHashes)) {
return Optional.of("Versioned hashes from blob transactions do not match expected values");
}
return Optional.empty();
}

JsonRpcResponse respondWith(
final Object requestId,
final EnginePayloadParameter param,
Expand Down Expand Up @@ -398,6 +361,19 @@ protected EngineStatus getInvalidBlockHashStatus() {
return INVALID;
}

protected Optional<JsonRpcResponse> validateForkSupported(
final Object id, final EnginePayloadParameter payloadParameter) {
return Optional.empty();
}

protected Optional<JsonRpcResponse> validateTransactions(
final EnginePayloadParameter blockParam,
final Object reqId,
final List<Transaction> transactions,
final Optional<List<String>> maybeVersionedHashParam) {
return Optional.empty();
}

private void logImportedBlockInfo(final Block block, final double timeInS) {
final StringBuilder message = new StringBuilder();
message.append("Imported #%,d / %d tx");
Expand All @@ -423,9 +399,4 @@ private void logImportedBlockInfo(final Block block, final double timeInS) {
ethPeers.peerCount()));
LOG.info(String.format(message.toString(), messageArgs.toArray()));
}

Optional<JsonRpcResponse> validateForkSupported(
final Object id, final EnginePayloadParameter payloadParameter) {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,27 @@
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine;

import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.ExecutionEngineJsonRpcMethod.EngineStatus.INVALID;
import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError.INVALID_PARAMS;
import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError.UNSUPPORTED_FORK;

import org.hyperledger.besu.consensus.merge.blockcreation.MergeMiningCoordinator;
import org.hyperledger.besu.datatypes.VersionedHash;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.EnginePayloadParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.eth.manager.EthPeers;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import io.vertx.core.Vertx;
import org.apache.tuweni.bytes.Bytes32;

public class EngineNewPayloadV3 extends AbstractEngineNewPayload {

Expand Down Expand Up @@ -69,4 +75,56 @@ protected Optional<JsonRpcResponse> validateForkSupported(
}
return Optional.empty();
}

@Override
protected Optional<JsonRpcResponse> validateTransactions(
final EnginePayloadParameter blockParam,
final Object reqId,
final List<Transaction> transactions,
final Optional<List<String>> maybeVersionedHashParam) {

var blobTransactions =
transactions.stream().filter(transaction -> transaction.getType().supportsBlob()).toList();

return validateBlobTransactions(blockParam, reqId, blobTransactions, maybeVersionedHashParam);
}

private Optional<JsonRpcResponse> validateBlobTransactions(
final EnginePayloadParameter blockParam,
final Object reqId,
final List<Transaction> blobTransactions,
final Optional<List<String>> maybeVersionedHashParam) {

List<Bytes32> versionedHashesParam =
maybeVersionedHashParam
.map(strings -> strings.stream().map(Bytes32::fromHexStringStrict).toList())
.orElseGet(ArrayList::new);

final List<Bytes32> transactionVersionedHashes = new ArrayList<>();

for (Transaction transaction : blobTransactions) {
if (transaction.getType().supportsBlob()) {
var versionedHashes = transaction.getVersionedHashes();
if (versionedHashes.isEmpty()) {
return Optional.of(
respondWithInvalid(
reqId, blockParam, null, INVALID, "There must be at least one blob"));
}
transactionVersionedHashes.addAll(
versionedHashes.get().stream().map(VersionedHash::toBytes).toList());
}
}

// check list contents
if (!versionedHashesParam.equals(transactionVersionedHashes)) {
return Optional.of(
respondWithInvalid(
reqId,
blockParam,
null,
INVALID,
"Versioned hashes from blob transactions do not match expected values"));
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public EnginePayloadParameter(
@JsonProperty("transactions") final List<String> transactions,
@JsonProperty("withdrawals") final List<WithdrawalParameter> withdrawals,
@JsonProperty("dataGasUsed") final UnsignedLongParameter dataGasUsed,
@JsonProperty("deposits") final List<DepositParameter> deposits,
@JsonProperty("excessDataGas") final String excessDataGas,
@JsonProperty("versionedHashes") final List<Bytes32> versionedHashes) {
@JsonProperty("versionedHashes") final List<Bytes32> versionedHashes,
@JsonProperty("deposits") final List<DepositParameter> deposits) {
this.blockHash = blockHash;
this.parentHash = parentHash;
this.feeRecipient = feeRecipient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,9 +596,9 @@ protected EnginePayloadParameter mockPayload(final BlockHeader header, final Lis
txs,
null,
header.getDataGasUsed().map(UnsignedLongParameter::new).orElse(null),
null,
header.getExcessDataGas().map(DataGas::toHexString).orElse(null),
null);
new ArrayList<>(),
null); // deposits
}

private EnginePayloadParameter mockPayload(
Expand All @@ -623,9 +623,9 @@ private EnginePayloadParameter mockPayload(
txs,
withdrawals,
header.getDataGasUsed().map(UnsignedLongParameter::new).orElse(null),
deposits,
header.getExcessDataGas().map(DataGas::toHexString).orElse(null),
List.of(DEFAULT_VERSIONED_HASH.toBytes()));
List.of(DEFAULT_VERSIONED_HASH.toBytes()),
deposits);
}

@NotNull
Expand Down

0 comments on commit 705c251

Please sign in to comment.