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

Sync performance improvements #7595

Closed
wants to merge 10 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,19 @@
import java.util.List;
import java.util.stream.IntStream;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;

/** A utility class for body validation tasks. */
public final class BodyValidation {

public static Cache<Hash, Boolean> bodiesValidatedRootsForBlockHashCache =
CacheBuilder.newBuilder().recordStats().maximumSize(10_000).build();
public static Cache<Hash, Boolean> receiptsRootForBlockHashCache =
CacheBuilder.newBuilder().recordStats().maximumSize(10_000).build();

private BodyValidation() {
// Utility Class
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,19 @@ public boolean validateBodyLight(
final BlockHeader header = block.getHeader();
final BlockBody body = block.getBody();

final Bytes32 transactionsRoot = BodyValidation.transactionsRoot(body.getTransactions());
if (!validateTransactionsRoot(header, header.getTransactionsRoot(), transactionsRoot)) {
return false;
if (BodyValidation.bodiesValidatedRootsForBlockHashCache.getIfPresent(header.getHash())
== null) {
final Bytes32 transactionsRoot = BodyValidation.transactionsRoot(body.getTransactions());
if (!validateTransactionsRoot(header, header.getTransactionsRoot(), transactionsRoot)) {
return false;
}
}

final Bytes32 receiptsRoot = BodyValidation.receiptsRoot(receipts);
if (!validateReceiptsRoot(header, header.getReceiptsRoot(), receiptsRoot)) {
return false;
if (BodyValidation.receiptsRootForBlockHashCache.getIfPresent(header.getHash()) == null) {
final Bytes32 receiptsRoot = BodyValidation.receiptsRoot(receipts);
if (!validateReceiptsRoot(header, header.getReceiptsRoot(), receiptsRoot)) {
return false;
}
}

final long gasUsed =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,26 @@ protected Optional<List<Block>> processResponse(
LOG.debug("This message contains unrelated bodies. Peer: {}", peer);
return Optional.empty();
}
headers.forEach(h -> blocks.add(new Block(h, body)));
headers.forEach(
h -> {
blocks.add(new Block(h, body));
// add here calculating async the transaction hash of each transaction
List<Transaction> transactions = body.getTransactions();
for (int i = 0; i < transactions.size(); i++) {
Transaction transaction = transactions.get(i);
ethContext
.getScheduler()
.scheduleTxWorkerTask(
() -> {
Hash txHash = transaction.getHash();
LOG.atTrace()
.setMessage("The hash for the transaction is calculated : {}")
.addArgument(txHash)
.log();
});
}
BodyValidation.bodiesValidatedRootsForBlockHashCache.put(h.getHash(), Boolean.TRUE);
});
// Clear processed headers
headers.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.hyperledger.besu.ethereum.eth.manager.PendingPeerRequest;
import org.hyperledger.besu.ethereum.eth.messages.EthPV63;
import org.hyperledger.besu.ethereum.eth.messages.ReceiptsMessage;
import org.hyperledger.besu.ethereum.mainnet.BodyValidation;
import org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData;
import org.hyperledger.besu.plugin.services.MetricsSystem;

Expand Down Expand Up @@ -119,7 +120,11 @@ protected Optional<Map<BlockHeader, List<TransactionReceipt>>> processResponse(
// Contains receipts that we didn't request, so mustn't be the response we're looking for.
return Optional.empty();
}
blockHeaders.forEach(header -> receiptsByHeader.put(header, receiptsInBlock));
blockHeaders.forEach(
header -> {
receiptsByHeader.put(header, receiptsInBlock);
BodyValidation.receiptsRootForBlockHashCache.put(header.getHash(), Boolean.TRUE);
});
}
return Optional.of(receiptsByHeader);
}
Expand Down
Loading