fix TransactionLocation in DefaultBlockchain unsafeImportBlock() (#7956)

* fix TransactionLocation in DefaultBlockchain unsafeImportBlock() and make some readability improvements

Signed-off-by: stefan.pingel@consensys.net <stefan.pingel@consensys.net>
pull/7964/head
Stefan Pingel 6 days ago committed by GitHub
parent 63496dbc6c
commit 8a033b9676
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 7
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/query/PrivacyQueriesTest.java
  2. 32
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/chain/DefaultBlockchain.java

@ -223,10 +223,11 @@ public class PrivacyQueriesTest {
final BlockHeader blockHeader, final BlockHeader blockHeader,
final List<PrivateTransactionMetadata> transactionMetadataList) { final List<PrivateTransactionMetadata> transactionMetadataList) {
for (int i = 0; i < transactionMetadataList.size(); i++) { for (int index = 0; index < transactionMetadataList.size(); index++) {
final PrivateTransactionMetadata privateTransactionMetadata = transactionMetadataList.get(i); final PrivateTransactionMetadata privateTransactionMetadata =
transactionMetadataList.get(index);
final Hash pmtHash = privateTransactionMetadata.getPrivateMarkerTransactionHash(); 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)) when(blockchainQueries.transactionLocationByHash(pmtHash))
.thenReturn(Optional.of(pmtLocation)); .thenReturn(Optional.of(pmtLocation));
} }

@ -485,18 +485,14 @@ public class DefaultBlockchain implements MutableBlockchain {
final List<TransactionReceipt> transactionReceipts, final List<TransactionReceipt> transactionReceipts,
final Optional<Difficulty> maybeTotalDifficulty) { final Optional<Difficulty> maybeTotalDifficulty) {
final BlockchainStorage.Updater updater = blockchainStorage.updater(); final BlockchainStorage.Updater updater = blockchainStorage.updater();
final Hash hash = block.getHash(); final Hash blockHash = block.getHash();
updater.putBlockHeader(hash, block.getHeader()); updater.putBlockHeader(blockHash, block.getHeader());
updater.putBlockHash(block.getHeader().getNumber(), hash); updater.putBlockHash(block.getHeader().getNumber(), blockHash);
updater.putBlockBody(hash, block.getBody()); updater.putBlockBody(blockHash, block.getBody());
final int nbTrx = block.getBody().getTransactions().size(); indexTransactionsForBlock(updater, blockHash, block.getBody().getTransactions());
for (int i = 0; i < nbTrx; i++) { updater.putTransactionReceipts(blockHash, transactionReceipts);
final Hash transactionHash = block.getBody().getTransactions().get(i).getHash();
updater.putTransactionLocation(transactionHash, new TransactionLocation(transactionHash, i));
}
updater.putTransactionReceipts(hash, transactionReceipts);
maybeTotalDifficulty.ifPresent( maybeTotalDifficulty.ifPresent(
totalDifficulty -> updater.putTotalDifficulty(hash, totalDifficulty)); totalDifficulty -> updater.putTotalDifficulty(blockHash, totalDifficulty));
updater.commit(); updater.commit();
} }
@ -563,7 +559,7 @@ public class DefaultBlockchain implements MutableBlockchain {
updater.putBlockHash(blockWithReceipts.getNumber(), newBlockHash); updater.putBlockHash(blockWithReceipts.getNumber(), newBlockHash);
updater.setChainHead(newBlockHash); updater.setChainHead(newBlockHash);
indexTransactionForBlock( indexTransactionsForBlock(
updater, newBlockHash, blockWithReceipts.getBlock().getBody().getTransactions()); updater, newBlockHash, blockWithReceipts.getBlock().getBody().getTransactions());
gasUsedCounter.inc(blockWithReceipts.getHeader().getGasUsed()); gasUsedCounter.inc(blockWithReceipts.getHeader().getGasUsed());
numberOfTransactionsCounter.inc( numberOfTransactionsCounter.inc(
@ -652,7 +648,7 @@ public class DefaultBlockchain implements MutableBlockchain {
// Update indexed transactions // Update indexed transactions
newTransactions.forEach( newTransactions.forEach(
(blockHash, transactionsInBlock) -> { (blockHash, transactionsInBlock) -> {
indexTransactionForBlock(updater, blockHash, transactionsInBlock); indexTransactionsForBlock(updater, blockHash, transactionsInBlock);
// Don't remove transactions that are being re-indexed. // Don't remove transactions that are being re-indexed.
removedTransactions.removeAll(transactionsInBlock); removedTransactions.removeAll(transactionsInBlock);
}); });
@ -792,11 +788,11 @@ public class DefaultBlockchain implements MutableBlockchain {
chainHeadOmmerCount = block.getBody().getOmmers().size(); chainHeadOmmerCount = block.getBody().getOmmers().size();
} }
private static void indexTransactionForBlock( private static void indexTransactionsForBlock(
final BlockchainStorage.Updater updater, final Hash hash, final List<Transaction> txs) { final BlockchainStorage.Updater updater, final Hash blockHash, final List<Transaction> txs) {
for (int i = 0; i < txs.size(); i++) { for (int index = 0; index < txs.size(); index++) {
final Hash txHash = txs.get(i).getHash(); final Hash txHash = txs.get(index).getHash();
final TransactionLocation loc = new TransactionLocation(hash, i); final TransactionLocation loc = new TransactionLocation(blockHash, index);
updater.putTransactionLocation(txHash, loc); updater.putTransactionLocation(txHash, loc);
} }
} }

Loading…
Cancel
Save