From 038d151e09f47074d9de87688969e3c1a5108e5a Mon Sep 17 00:00:00 2001 From: Ameziane H Date: Tue, 10 Sep 2024 11:17:48 +0200 Subject: [PATCH 1/7] Cache transactionsRoot and receiptsRoot validation when getting data from other peers Signed-off-by: Ameziane H --- .../besu/ethereum/mainnet/BodyValidation.java | 6 ++++++ .../mainnet/MainnetBlockBodyValidator.java | 16 ++++++++++------ .../eth/manager/task/GetBodiesFromPeerTask.java | 5 ++++- .../manager/task/GetReceiptsFromPeerTask.java | 6 +++++- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BodyValidation.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BodyValidation.java index 15e8c3c804f..4c9f1600130 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BodyValidation.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BodyValidation.java @@ -16,6 +16,8 @@ import static org.hyperledger.besu.crypto.Hash.keccak256; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.ethereum.core.BlockHeader; import org.hyperledger.besu.ethereum.core.Request; @@ -40,6 +42,10 @@ /** A utility class for body validation tasks. */ public final class BodyValidation { + public static Cache bodiesValidatedRootsCache = CacheBuilder.newBuilder().recordStats().maximumSize(2_000).build(); + public static Cache receiptsRootCache = CacheBuilder.newBuilder().recordStats().maximumSize(2_000).build(); + + private BodyValidation() { // Utility Class } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidator.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidator.java index 3202440dd89..45df63cd9ca 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidator.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidator.java @@ -81,14 +81,18 @@ 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 (Boolean.FALSE.equals(BodyValidation.bodiesValidatedRootsCache.getIfPresent(header))) { + 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 (Boolean.FALSE.equals(BodyValidation.receiptsRootCache.getIfPresent(header))) { + final Bytes32 receiptsRoot = BodyValidation.receiptsRoot(receipts); + if (!validateReceiptsRoot(header, header.getReceiptsRoot(), receiptsRoot)) { + return false; + } } final long gasUsed = diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetBodiesFromPeerTask.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetBodiesFromPeerTask.java index 56a85b040b0..22c978d0603 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetBodiesFromPeerTask.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetBodiesFromPeerTask.java @@ -133,7 +133,10 @@ protected Optional> 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)); + BodyValidation.bodiesValidatedRootsCache.put(h, Boolean.TRUE); + }); // Clear processed headers headers.clear(); } diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetReceiptsFromPeerTask.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetReceiptsFromPeerTask.java index 5b52078b23e..1b00762f931 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetReceiptsFromPeerTask.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetReceiptsFromPeerTask.java @@ -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; @@ -119,7 +120,10 @@ protected Optional>> 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.receiptsRootCache.put(header, Boolean.TRUE); + }); } return Optional.of(receiptsByHeader); } From 5e51bf18ca79e50b0fbe23fda0ceb0477512c809 Mon Sep 17 00:00:00 2001 From: Ameziane H Date: Tue, 10 Sep 2024 15:12:08 +0200 Subject: [PATCH 2/7] spotless Signed-off-by: Ameziane H --- .../besu/ethereum/mainnet/BodyValidation.java | 11 ++++++----- .../eth/manager/task/GetBodiesFromPeerTask.java | 9 +++++---- .../eth/manager/task/GetReceiptsFromPeerTask.java | 9 +++++---- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BodyValidation.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BodyValidation.java index 4c9f1600130..e9b75d5b289 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BodyValidation.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BodyValidation.java @@ -16,8 +16,6 @@ import static org.hyperledger.besu.crypto.Hash.keccak256; -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.ethereum.core.BlockHeader; import org.hyperledger.besu.ethereum.core.Request; @@ -36,15 +34,18 @@ 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 bodiesValidatedRootsCache = CacheBuilder.newBuilder().recordStats().maximumSize(2_000).build(); - public static Cache receiptsRootCache = CacheBuilder.newBuilder().recordStats().maximumSize(2_000).build(); - + public static Cache bodiesValidatedRootsCache = + CacheBuilder.newBuilder().recordStats().maximumSize(2_000).build(); + public static Cache receiptsRootCache = + CacheBuilder.newBuilder().recordStats().maximumSize(2_000).build(); private BodyValidation() { // Utility Class diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetBodiesFromPeerTask.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetBodiesFromPeerTask.java index 22c978d0603..beffe91844c 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetBodiesFromPeerTask.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetBodiesFromPeerTask.java @@ -133,10 +133,11 @@ protected Optional> processResponse( LOG.debug("This message contains unrelated bodies. Peer: {}", peer); return Optional.empty(); } - headers.forEach(h -> { - blocks.add(new Block(h, body)); - BodyValidation.bodiesValidatedRootsCache.put(h, Boolean.TRUE); - }); + headers.forEach( + h -> { + blocks.add(new Block(h, body)); + BodyValidation.bodiesValidatedRootsCache.put(h, Boolean.TRUE); + }); // Clear processed headers headers.clear(); } diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetReceiptsFromPeerTask.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetReceiptsFromPeerTask.java index 1b00762f931..86be6b8f1ee 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetReceiptsFromPeerTask.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetReceiptsFromPeerTask.java @@ -120,10 +120,11 @@ protected Optional>> 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); - BodyValidation.receiptsRootCache.put(header, Boolean.TRUE); - }); + blockHeaders.forEach( + header -> { + receiptsByHeader.put(header, receiptsInBlock); + BodyValidation.receiptsRootCache.put(header, Boolean.TRUE); + }); } return Optional.of(receiptsByHeader); } From e7ccc94aa179ebb1621ebc542e4a300a5f260912 Mon Sep 17 00:00:00 2001 From: Ameziane H Date: Wed, 11 Sep 2024 11:27:59 +0200 Subject: [PATCH 3/7] Fix reference tests issue Signed-off-by: Ameziane H --- .../ethereum/mainnet/MainnetBlockBodyValidator.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidator.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidator.java index 45df63cd9ca..299c55d9236 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidator.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidator.java @@ -81,18 +81,18 @@ public boolean validateBodyLight( final BlockHeader header = block.getHeader(); final BlockBody body = block.getBody(); - if (Boolean.FALSE.equals(BodyValidation.bodiesValidatedRootsCache.getIfPresent(header))) { + if (BodyValidation.bodiesValidatedRootsCache.getIfPresent(header) == null) { final Bytes32 transactionsRoot = BodyValidation.transactionsRoot(body.getTransactions()); if (!validateTransactionsRoot(header, header.getTransactionsRoot(), transactionsRoot)) { return false; } } - if (Boolean.FALSE.equals(BodyValidation.receiptsRootCache.getIfPresent(header))) { + if (BodyValidation.receiptsRootCache.getIfPresent(header) == null) { final Bytes32 receiptsRoot = BodyValidation.receiptsRoot(receipts); - if (!validateReceiptsRoot(header, header.getReceiptsRoot(), receiptsRoot)) { - return false; - } + if (!validateReceiptsRoot(header, header.getReceiptsRoot(), receiptsRoot)) { + return false; + } } final long gasUsed = From 35da69a7a013ed1fe2da572c325efb4d4c57f305 Mon Sep 17 00:00:00 2001 From: Ameziane H Date: Wed, 11 Sep 2024 11:40:28 +0200 Subject: [PATCH 4/7] spotless. Signed-off-by: Ameziane H --- .../besu/ethereum/mainnet/MainnetBlockBodyValidator.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidator.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidator.java index 299c55d9236..c59e93a81fe 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidator.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidator.java @@ -90,9 +90,9 @@ public boolean validateBodyLight( if (BodyValidation.receiptsRootCache.getIfPresent(header) == null) { final Bytes32 receiptsRoot = BodyValidation.receiptsRoot(receipts); - if (!validateReceiptsRoot(header, header.getReceiptsRoot(), receiptsRoot)) { - return false; - } + if (!validateReceiptsRoot(header, header.getReceiptsRoot(), receiptsRoot)) { + return false; + } } final long gasUsed = From 56069a529fe534980bad760e705640f7662ba753 Mon Sep 17 00:00:00 2001 From: Ameziane H Date: Wed, 11 Sep 2024 16:08:46 +0200 Subject: [PATCH 5/7] Calculate asynchronously the hashes of transactions during "downloadBodies" step Signed-off-by: Ameziane H --- .../eth/manager/task/GetBodiesFromPeerTask.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetBodiesFromPeerTask.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetBodiesFromPeerTask.java index beffe91844c..6ee86ca7e16 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetBodiesFromPeerTask.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetBodiesFromPeerTask.java @@ -136,6 +136,21 @@ protected Optional> processResponse( headers.forEach( h -> { blocks.add(new Block(h, body)); + // add here calculating async the transaction hash of each transaction + List 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.bodiesValidatedRootsCache.put(h, Boolean.TRUE); }); // Clear processed headers From 8650c944cdac172a19e03f08af9d5d53051505a2 Mon Sep 17 00:00:00 2001 From: Ameziane H Date: Thu, 12 Sep 2024 11:27:34 +0200 Subject: [PATCH 6/7] Reduce caches memory footprint Signed-off-by: Ameziane H --- .../hyperledger/besu/ethereum/mainnet/BodyValidation.java | 8 ++++---- .../besu/ethereum/mainnet/MainnetBlockBodyValidator.java | 4 ++-- .../ethereum/eth/manager/task/GetBodiesFromPeerTask.java | 2 +- .../eth/manager/task/GetReceiptsFromPeerTask.java | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BodyValidation.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BodyValidation.java index e9b75d5b289..75dae49319d 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BodyValidation.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BodyValidation.java @@ -42,10 +42,10 @@ /** A utility class for body validation tasks. */ public final class BodyValidation { - public static Cache bodiesValidatedRootsCache = - CacheBuilder.newBuilder().recordStats().maximumSize(2_000).build(); - public static Cache receiptsRootCache = - CacheBuilder.newBuilder().recordStats().maximumSize(2_000).build(); + public static Cache bodiesValidatedRootsForBlockHashCache = + CacheBuilder.newBuilder().recordStats().maximumSize(10_000).build(); + public static Cache receiptsRootForBlockHashCache = + CacheBuilder.newBuilder().recordStats().maximumSize(10_000).build(); private BodyValidation() { // Utility Class diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidator.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidator.java index c59e93a81fe..fb11ab46101 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidator.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidator.java @@ -81,14 +81,14 @@ public boolean validateBodyLight( final BlockHeader header = block.getHeader(); final BlockBody body = block.getBody(); - if (BodyValidation.bodiesValidatedRootsCache.getIfPresent(header) == null) { + if (BodyValidation.bodiesValidatedRootsForBlockHashCache.getIfPresent(header) == null) { final Bytes32 transactionsRoot = BodyValidation.transactionsRoot(body.getTransactions()); if (!validateTransactionsRoot(header, header.getTransactionsRoot(), transactionsRoot)) { return false; } } - if (BodyValidation.receiptsRootCache.getIfPresent(header) == null) { + if (BodyValidation.receiptsRootForBlockHashCache.getIfPresent(header) == null) { final Bytes32 receiptsRoot = BodyValidation.receiptsRoot(receipts); if (!validateReceiptsRoot(header, header.getReceiptsRoot(), receiptsRoot)) { return false; diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetBodiesFromPeerTask.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetBodiesFromPeerTask.java index 6ee86ca7e16..2733c197946 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetBodiesFromPeerTask.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetBodiesFromPeerTask.java @@ -151,7 +151,7 @@ protected Optional> processResponse( .log(); }); } - BodyValidation.bodiesValidatedRootsCache.put(h, Boolean.TRUE); + BodyValidation.bodiesValidatedRootsForBlockHashCache.put(h.getHash(), Boolean.TRUE); }); // Clear processed headers headers.clear(); diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetReceiptsFromPeerTask.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetReceiptsFromPeerTask.java index 86be6b8f1ee..ffb6b449cfe 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetReceiptsFromPeerTask.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/GetReceiptsFromPeerTask.java @@ -123,7 +123,7 @@ protected Optional>> processResponse( blockHeaders.forEach( header -> { receiptsByHeader.put(header, receiptsInBlock); - BodyValidation.receiptsRootCache.put(header, Boolean.TRUE); + BodyValidation.receiptsRootForBlockHashCache.put(header.getHash(), Boolean.TRUE); }); } return Optional.of(receiptsByHeader); From 486f1ef2577130d27cb96ce1494ddecc0e566928 Mon Sep 17 00:00:00 2001 From: Ameziane H Date: Thu, 12 Sep 2024 11:38:26 +0200 Subject: [PATCH 7/7] Fix the type issue Signed-off-by: Ameziane H --- .../besu/ethereum/mainnet/MainnetBlockBodyValidator.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidator.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidator.java index fb11ab46101..2036599ed2c 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidator.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockBodyValidator.java @@ -81,14 +81,15 @@ public boolean validateBodyLight( final BlockHeader header = block.getHeader(); final BlockBody body = block.getBody(); - if (BodyValidation.bodiesValidatedRootsForBlockHashCache.getIfPresent(header) == null) { + if (BodyValidation.bodiesValidatedRootsForBlockHashCache.getIfPresent(header.getHash()) + == null) { final Bytes32 transactionsRoot = BodyValidation.transactionsRoot(body.getTransactions()); if (!validateTransactionsRoot(header, header.getTransactionsRoot(), transactionsRoot)) { return false; } } - if (BodyValidation.receiptsRootForBlockHashCache.getIfPresent(header) == null) { + if (BodyValidation.receiptsRootForBlockHashCache.getIfPresent(header.getHash()) == null) { final Bytes32 receiptsRoot = BodyValidation.receiptsRoot(receipts); if (!validateReceiptsRoot(header, header.getReceiptsRoot(), receiptsRoot)) { return false;