Skip to content

Commit

Permalink
validate versioned hashes (#5546)
Browse files Browse the repository at this point in the history
* wip - added versioned hashes checking

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>

* optional handling, updated test mocks

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>

---------

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>
Signed-off-by: Justin Florentine <justin+github@florentine.us>
Co-authored-by: Justin Florentine <justin+github@florentine.us>
  • Loading branch information
macfarla and jflo committed Jul 5, 2023
1 parent 0176210 commit c82b65a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
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 @@ -206,6 +207,50 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
"Block already present in bad block manager.");
}

final List<Bytes32> transactionVersionedHashes = new ArrayList<>();
// get versioned hashes, in order, from all blob tx
transactions.stream()
.filter(tx -> tx.getBlobCount() > 0)
.forEachOrdered(
tx ->
transactionVersionedHashes.addAll(
tx.getVersionedHashes().get().stream()
.map(vh -> vh.toBytes())
.collect(toList())));
// and compare with expected versioned hashes param
final Optional<List<Bytes32>> maybeVersionedHashes =
Optional.ofNullable(blockParam.getVersionedHashes());
// check if one is empty
if (maybeVersionedHashes.isPresent() && transactionVersionedHashes.isEmpty()) {
return respondWithInvalid(
reqId,
blockParam,
null,
INVALID,
"Versioned hashes from blob transactions (empty) do not match expected values");
}
if (maybeVersionedHashes.isEmpty() && !transactionVersionedHashes.isEmpty()) {
return respondWithInvalid(
reqId,
blockParam,
null,
INVALID,
"Versioned hashes from blob transactions do not match expected values (empty)");
}
if (maybeVersionedHashes.isEmpty() && transactionVersionedHashes.isEmpty()) {
LOG.trace("Versioned hashes from blob tx (empty) matches expected values (empty)");
} else {
// otherwise, check list contents
if (!maybeVersionedHashes.get().equals(transactionVersionedHashes)) {
return respondWithInvalid(
reqId,
blockParam,
null,
INVALID,
"Versioned hashes from blob transactions do not match expected values (empty)");
}
}

final Optional<BlockHeader> maybeParentHeader =
protocolContext.getBlockchain().getBlockHeader(blockParam.getParentHash());
if (maybeParentHeader.isPresent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class EnginePayloadParameter {
private final List<String> transactions;
private final List<WithdrawalParameter> withdrawals;
private final List<DepositParameter> deposits;
private final List<Bytes32> versionedHashes;
private final long dataGasUsed;
private final String excessDataGas;

Expand All @@ -70,7 +71,8 @@ public EnginePayloadParameter(
@JsonProperty("withdrawals") final List<WithdrawalParameter> withdrawals,
@JsonProperty("dataGasUsed") final UnsignedLongParameter dataGasUsed,
@JsonProperty("deposits") final List<DepositParameter> deposits,
@JsonProperty("excessDataGas") final String excessDataGas) {
@JsonProperty("excessDataGas") final String excessDataGas,
@JsonProperty("versionedHashes") final List<Bytes32> versionedHashes) {
this.blockHash = blockHash;
this.parentHash = parentHash;
this.feeRecipient = feeRecipient;
Expand All @@ -89,6 +91,7 @@ public EnginePayloadParameter(
this.deposits = deposits;
this.dataGasUsed = dataGasUsed.getValue();
this.excessDataGas = excessDataGas;
this.versionedHashes = versionedHashes;
}

public Hash getBlockHash() {
Expand Down Expand Up @@ -162,4 +165,8 @@ public String getExcessDataGas() {
public List<DepositParameter> getDeposits() {
return deposits;
}

public List<Bytes32> getVersionedHashes() {
return versionedHashes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ private EnginePayloadParameter mockPayload(
header.getPrevRandao().map(Bytes32::toHexString).orElse("0x0"),
txs,
withdrawals,
null,
new UnsignedLongParameter(header.getDataGasUsed()),
deposits,
header.getExcessDataGas().map(DataGas::toHexString).orElse(null));
Expand Down

0 comments on commit c82b65a

Please sign in to comment.