Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude empty requests from requests hash #8036

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
770ee68
exclude empty requests from requests hash
daniellehrner Dec 16, 2024
6e9f120
Merge branch 'main' into feat/issue-7947/exclude_empty_requests_hash
daniellehrner Dec 17, 2024
d7c3a7f
Exclude empty requests and prepend request type byte in engine_getPay…
siladu Dec 18, 2024
a381b19
Merge branch 'main' into feat/issue-7947/exclude_empty_requests_hash
daniellehrner Dec 18, 2024
ffafb17
Merge branch 'feat/issue-7947/exclude_empty_requests_hash' of github.…
daniellehrner Dec 18, 2024
486812f
Merge branch 'main' into feat/issue-7947/exclude_empty_requests_hash
daniellehrner Dec 18, 2024
9d5b638
Merge branch 'main' into feat/issue-7947/exclude_empty_requests_hash
daniellehrner Jan 8, 2025
d8b62ff
fix test
daniellehrner Jan 8, 2025
857c369
fix prague tests: change request hash, reorder json elements
daniellehrner Jan 9, 2025
9c16f51
fix extracting of request for new payload
daniellehrner Jan 9, 2025
f19574b
Merge branch 'main' into feat/issue-7947/exclude_empty_requests_hash
daniellehrner Jan 9, 2025
1ec2403
Merge branch 'main' into feat/issue-7947/exclude_empty_requests_hash
daniellehrner Jan 13, 2025
61ea63a
fix test by fixing request object creation
daniellehrner Jan 13, 2025
ed3abc7
spotless
daniellehrner Jan 13, 2025
9f6c514
Merge branch 'main' into feat/issue-7947/exclude_empty_requests_hash
daniellehrner Jan 13, 2025
1b8a524
set request type to only one byte, fixed creation of request list in …
daniellehrner Jan 14, 2025
ed5d645
Merge branch 'main' into feat/issue-7947/exclude_empty_requests_hash
daniellehrner Jan 14, 2025
e25ebd2
fix expected json output to exclude empty requests
daniellehrner Jan 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ public void shouldTransferAllEthOfAuthorizerToSponsor() throws IOException {
*/
@Test
public void shouldCheckNonceAfterNonceIncreaseOfSender() throws IOException {
final long GAS_LIMIT = 1000000L;
cluster.verify(authorizer.balanceEquals(Amount.ether(90000)));
final long GAS_LIMIT = 1_000_000L;
cluster.verify(authorizer.balanceEquals(Amount.ether(90_000)));

final CodeDelegation authorization =
org.hyperledger.besu.ethereum.core.CodeDelegation.builder()
Expand All @@ -159,7 +159,7 @@ public void shouldCheckNonceAfterNonceIncreaseOfSender() throws IOException {
.type(TransactionType.DELEGATE_CODE)
.chainId(BigInteger.valueOf(20211))
.nonce(0)
.maxPriorityFeePerGas(Wei.of(1000000000))
.maxPriorityFeePerGas(Wei.of(1_000_000_000))
.maxFeePerGas(Wei.fromHexString("0x02540BE400"))
.gasLimit(GAS_LIMIT)
.to(Address.fromHexStringStrict(authorizer.getAddress()))
Expand Down Expand Up @@ -209,7 +209,7 @@ public void shouldCheckNonceAfterNonceIncreaseOfSender() throws IOException {
.nonce(2)
.maxPriorityFeePerGas(Wei.of(10))
.maxFeePerGas(Wei.of(100))
.gasLimit(21000)
.gasLimit(21_000)
.to(Address.fromHexStringStrict(otherAccount.getAddress()))
.value(Wei.ONE)
.payload(Bytes.EMPTY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ public void buildNewBlock() throws IOException {
executionPayload.toString(), parentBeaconBlockRoot, executionRequests.toString()));
try (final Response newPayloadResponse = newPayloadRequest.execute()) {
assertThat(newPayloadResponse.code()).isEqualTo(200);

final String responseStatus =
mapper.readTree(newPayloadResponse.body().string()).get("result").get("status").asText();
assertThat(responseStatus).isEqualTo("VALID");
}

final Call moveChainAheadRequest = createEngineCall(createForkChoiceRequest(newBlockHash));
Expand Down
10 changes: 2 additions & 8 deletions datatypes/src/main/java/org/hyperledger/besu/datatypes/Hash.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,9 @@ public class Hash extends DelegatingBytes32 {
public static final Hash EMPTY = hash(Bytes.EMPTY);

/**
* Hash of empty requests or "0x6036c41849da9c076ed79654d434017387a88fb833c2856b32e18218b3341c5f"
* Hash of empty requests or "0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
*/
public static final Hash EMPTY_REQUESTS_HASH =
Hash.wrap(
sha256(
Bytes.concatenate(
sha256(Bytes.of(RequestType.DEPOSIT.getSerializedType())),
sha256(Bytes.of(RequestType.WITHDRAWAL.getSerializedType())),
sha256(Bytes.of(RequestType.CONSOLIDATION.getSerializedType())))));
public static final Hash EMPTY_REQUESTS_HASH = Hash.wrap(sha256(Bytes.EMPTY));

/**
* Instantiates a new Hash.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public void shouldGetExpectedValueForEmptyHash() {
public void shouldGetExpectedValueForEmptyRequestsHash() {
assertThat(Hash.EMPTY_REQUESTS_HASH)
.isEqualTo(
Hash.fromHexString(
"0x6036c41849da9c076ed79654d434017387a88fb833c2856b32e18218b3341c5f"));
Hash.fromHexString("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import io.vertx.core.Vertx;
import io.vertx.core.json.Json;
Expand Down Expand Up @@ -596,8 +595,12 @@ private Optional<List<Request>> extractRequests(final Optional<List<String>> may

return maybeRequestsParam.map(
requests ->
IntStream.range(0, requests.size())
.mapToObj(i -> new Request(RequestType.of(i), Bytes.fromHexString(requests.get(i))))
requests.stream()
.map(
s -> {
final Bytes request = Bytes.fromHexString(s);
return new Request(RequestType.of(request.get(0)), request.slice(1));
})
.collect(Collectors.toList()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ public EngineGetPayloadResultV4 payloadTransactionCompleteV4(final PayloadWrappe
rqs ->
rqs.stream()
.sorted(Comparator.comparing(Request::getType))
.map(r -> r.getData().toHexString())
.filter(r -> !r.getData().isEmpty())
.map(Request::getEncodedRequest)
.map(Bytes::toHexString)
.toList());

final BlobsBundleV1 blobsBundleV1 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ header, new BlockBody(List.of(blobTx), emptyList(), Optional.of(emptyList()))),
final List<String> requestsWithoutRequestId =
requests.stream()
.sorted(Comparator.comparing(Request::getType))
.map(r -> r.getData().toHexString())
.map(Request::getEncodedRequest)
.map(Bytes::toHexString)
.toList();
Optional.of(resp)
.map(JsonRpcSuccessResponse.class::cast)
Expand All @@ -172,6 +173,53 @@ header, new BlockBody(List.of(blobTx), emptyList(), Optional.of(emptyList()))),
verify(engineCallListener, times(1)).executionEngineCalled();
}

@Test
public void shouldExcludeEmptyRequestsInRequestsList() {

BlockHeader header =
new BlockHeaderTestFixture().timestamp(pragueHardfork.milestone() + 1).buildHeader();
PayloadIdentifier payloadIdentifier =
PayloadIdentifier.forPayloadParams(
Hash.ZERO,
pragueHardfork.milestone(),
Bytes32.random(),
Address.fromHexString("0x42"),
Optional.empty(),
Optional.empty());

BlockWithReceipts block =
new BlockWithReceipts(
new Block(header, new BlockBody(emptyList(), emptyList(), Optional.of(emptyList()))),
emptyList());
final List<Request> unorderedRequests =
List.of(
new Request(RequestType.CONSOLIDATION, Bytes.of(1)),
new Request(RequestType.DEPOSIT, Bytes.of(1)),
new Request(RequestType.WITHDRAWAL, Bytes.EMPTY));
PayloadWrapper payload =
new PayloadWrapper(payloadIdentifier, block, Optional.of(unorderedRequests));

when(mergeContext.retrievePayloadById(payloadIdentifier)).thenReturn(Optional.of(payload));

final var resp = resp(RpcMethod.ENGINE_GET_PAYLOAD_V4.getMethodName(), payloadIdentifier);
assertThat(resp).isInstanceOf(JsonRpcSuccessResponse.class);

final List<String> expectedRequests =
List.of(
Bytes.concatenate(Bytes.of(RequestType.DEPOSIT.getSerializedType()), Bytes.of(1))
.toHexString(),
Bytes.concatenate(Bytes.of(RequestType.CONSOLIDATION.getSerializedType()), Bytes.of(1))
.toHexString());
Optional.of(resp)
.map(JsonRpcSuccessResponse.class::cast)
.ifPresent(
r -> {
assertThat(r.getResult()).isInstanceOf(EngineGetPayloadResultV4.class);
final EngineGetPayloadResultV4 res = (EngineGetPayloadResultV4) r.getResult();
assertThat(res.getExecutionRequests()).isEqualTo(expectedRequests);
});
}

@Test
public void shouldReturnUnsupportedFork() {
final var resp = resp(RpcMethod.ENGINE_GET_PAYLOAD_V4.getMethodName(), mockPid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ protected JsonRpcResponse resp(final EnginePayloadParameter payload) {
final List<String> requestsWithoutRequestId =
VALID_REQUESTS.stream()
.sorted(Comparator.comparing(Request::getType))
.map(r -> r.getData().toHexString())
.map(
r ->
Bytes.concatenate(Bytes.of(r.getType().getSerializedType()), r.getData())
.toHexString())
.toList();
Object[] params =
maybeParentBeaconBlockRoot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,13 @@ public RequestType getType() {
public Bytes getData() {
return data();
}

/**
* Gets the serialized form of the concatenated type and data.
*
* @return the serialized request as a byte.
*/
public Bytes getEncodedRequest() {
return Bytes.concatenate(Bytes.of(getType().ordinal()), getData());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,18 @@ public static Hash withdrawalsRoot(final List<Withdrawal> withdrawals) {
/**
* Generates the requests hash for a list of requests
*
* @param requests list of request
* @param requests list of request (must be sorted by request type ascending)
* @return the requests hash
*/
public static Hash requestsHash(final List<Request> requests) {
List<Bytes> requestHashes = new ArrayList<>();
IntStream.range(0, requests.size())
.forEach(
i -> {
final Request request = requests.get(i);
final Bytes requestBytes =
Bytes.concatenate(
Bytes.of(request.getType().getSerializedType()), request.getData());
requestHashes.add(sha256(requestBytes));
});
requests.forEach(
request -> {
// empty requests are excluded from the hash
if (!request.getData().isEmpty()) {
requestHashes.add(sha256(request.getEncodedRequest()));
}
});

return Hash.wrap(sha256(Bytes.wrap(requestHashes)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ void genesisFromPrague(final DataStorageConfiguration dataStorageConfiguration)
assertThat(header.getHash())
.isEqualTo(
Hash.fromHexString(
"0x554807b22674e6d335f734485993857bbad7a9543affb0663a10c14d78135ec7"));
"0x5d2d02fce02d1b7ca635ec91a4fe6f7aa36f9b3997ec4304e8c68d8f6f15d266"));
assertThat(header.getGasLimit()).isEqualTo(0x2fefd8);
assertThat(header.getGasUsed()).isZero();
assertThat(header.getNumber()).isZero();
Expand Down Expand Up @@ -330,7 +330,7 @@ void genesisFromPrague(final DataStorageConfiguration dataStorageConfiguration)
assertThat(header.getRequestsHash().get())
.isEqualTo(
Hash.fromHexString(
"0x6036c41849da9c076ed79654d434017387a88fb833c2856b32e18218b3341c5f"));
"0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,12 @@ static T8nResult runTest(
ArrayNode requests = resultObject.putArray("requests");
maybeRequests
.orElseGet(List::of)
.forEach(request -> requests.add(request.getData().toHexString()));
.forEach(
request -> {
if (!request.data().isEmpty()) {
requests.add(request.getEncodedRequest().toHexString());
}
});
}

worldState.persist(blockHeader);
Expand Down
Loading
Loading