From 8a033b96765d85b89c1ce9dd543481c9fd681e97 Mon Sep 17 00:00:00 2001 From: Stefan Pingel <16143240+pinges@users.noreply.github.com> Date: Fri, 29 Nov 2024 16:10:24 +1000 Subject: [PATCH] fix TransactionLocation in DefaultBlockchain unsafeImportBlock() (#7956) * fix TransactionLocation in DefaultBlockchain unsafeImportBlock() and make some readability improvements Signed-off-by: stefan.pingel@consensys.net --- .../api/query/PrivacyQueriesTest.java | 7 ++-- .../ethereum/chain/DefaultBlockchain.java | 32 ++++++++----------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/query/PrivacyQueriesTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/query/PrivacyQueriesTest.java index 2379037150..8c31f7894d 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/query/PrivacyQueriesTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/query/PrivacyQueriesTest.java @@ -223,10 +223,11 @@ public class PrivacyQueriesTest { final BlockHeader blockHeader, final List transactionMetadataList) { - for (int i = 0; i < transactionMetadataList.size(); i++) { - final PrivateTransactionMetadata privateTransactionMetadata = transactionMetadataList.get(i); + for (int index = 0; index < transactionMetadataList.size(); index++) { + final PrivateTransactionMetadata privateTransactionMetadata = + transactionMetadataList.get(index); final Hash pmtHash = privateTransactionMetadata.getPrivateMarkerTransactionHash(); - final TransactionLocation pmtLocation = new TransactionLocation(blockHeader.getHash(), i); + final TransactionLocation pmtLocation = new TransactionLocation(blockHeader.getHash(), index); when(blockchainQueries.transactionLocationByHash(pmtHash)) .thenReturn(Optional.of(pmtLocation)); } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/chain/DefaultBlockchain.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/chain/DefaultBlockchain.java index 8a354638f0..5b1fa1effb 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/chain/DefaultBlockchain.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/chain/DefaultBlockchain.java @@ -485,18 +485,14 @@ public class DefaultBlockchain implements MutableBlockchain { final List transactionReceipts, final Optional maybeTotalDifficulty) { final BlockchainStorage.Updater updater = blockchainStorage.updater(); - final Hash hash = block.getHash(); - updater.putBlockHeader(hash, block.getHeader()); - updater.putBlockHash(block.getHeader().getNumber(), hash); - updater.putBlockBody(hash, block.getBody()); - final int nbTrx = block.getBody().getTransactions().size(); - for (int i = 0; i < nbTrx; i++) { - final Hash transactionHash = block.getBody().getTransactions().get(i).getHash(); - updater.putTransactionLocation(transactionHash, new TransactionLocation(transactionHash, i)); - } - updater.putTransactionReceipts(hash, transactionReceipts); + final Hash blockHash = block.getHash(); + updater.putBlockHeader(blockHash, block.getHeader()); + updater.putBlockHash(block.getHeader().getNumber(), blockHash); + updater.putBlockBody(blockHash, block.getBody()); + indexTransactionsForBlock(updater, blockHash, block.getBody().getTransactions()); + updater.putTransactionReceipts(blockHash, transactionReceipts); maybeTotalDifficulty.ifPresent( - totalDifficulty -> updater.putTotalDifficulty(hash, totalDifficulty)); + totalDifficulty -> updater.putTotalDifficulty(blockHash, totalDifficulty)); updater.commit(); } @@ -563,7 +559,7 @@ public class DefaultBlockchain implements MutableBlockchain { updater.putBlockHash(blockWithReceipts.getNumber(), newBlockHash); updater.setChainHead(newBlockHash); - indexTransactionForBlock( + indexTransactionsForBlock( updater, newBlockHash, blockWithReceipts.getBlock().getBody().getTransactions()); gasUsedCounter.inc(blockWithReceipts.getHeader().getGasUsed()); numberOfTransactionsCounter.inc( @@ -652,7 +648,7 @@ public class DefaultBlockchain implements MutableBlockchain { // Update indexed transactions newTransactions.forEach( (blockHash, transactionsInBlock) -> { - indexTransactionForBlock(updater, blockHash, transactionsInBlock); + indexTransactionsForBlock(updater, blockHash, transactionsInBlock); // Don't remove transactions that are being re-indexed. removedTransactions.removeAll(transactionsInBlock); }); @@ -792,11 +788,11 @@ public class DefaultBlockchain implements MutableBlockchain { chainHeadOmmerCount = block.getBody().getOmmers().size(); } - private static void indexTransactionForBlock( - final BlockchainStorage.Updater updater, final Hash hash, final List txs) { - for (int i = 0; i < txs.size(); i++) { - final Hash txHash = txs.get(i).getHash(); - final TransactionLocation loc = new TransactionLocation(hash, i); + private static void indexTransactionsForBlock( + final BlockchainStorage.Updater updater, final Hash blockHash, final List txs) { + for (int index = 0; index < txs.size(); index++) { + final Hash txHash = txs.get(index).getHash(); + final TransactionLocation loc = new TransactionLocation(blockHash, index); updater.putTransactionLocation(txHash, loc); } }