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 Jun 14, 2023
1 parent 8d9880a commit aae8900
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 @@ -63,6 +63,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 @@ -194,6 +195,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 @@ -47,6 +47,7 @@ public class EnginePayloadParameter {
private final LogsBloomFilter logsBloom;
private final List<String> transactions;
private final List<WithdrawalParameter> withdrawals;
private final List<Bytes32> versionedHashes;
private final long dataGasUsed;
private final String excessDataGas;

Expand All @@ -68,7 +69,8 @@ public EnginePayloadParameter(
@JsonProperty("transactions") final List<String> transactions,
@JsonProperty("withdrawals") final List<WithdrawalParameter> withdrawals,
@JsonProperty("dataGasUsed") final UnsignedLongParameter dataGasUsed,
@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 @@ -86,6 +88,7 @@ public EnginePayloadParameter(
this.withdrawals = withdrawals;
this.dataGasUsed = dataGasUsed.getValue();
this.excessDataGas = excessDataGas;
this.versionedHashes = versionedHashes;
}

public Hash getBlockHash() {
Expand Down Expand Up @@ -155,4 +158,8 @@ public long getDataGasUsed() {
public String getExcessDataGas() {
return excessDataGas;
}

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

0 comments on commit aae8900

Please sign in to comment.