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

fix TransactionLocation in DefaultBlockchain unsafeImportBlock() #7956

Merged
merged 4 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -223,10 +223,11 @@ private void mockBlockchainWithPMTs(
final BlockHeader blockHeader,
final List<PrivateTransactionMetadata> 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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,18 +485,14 @@ public synchronized void unsafeImportBlock(
final List<TransactionReceipt> transactionReceipts,
final Optional<Difficulty> 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();
}

Expand Down Expand Up @@ -563,7 +559,7 @@ private BlockAddedEvent handleNewHead(

updater.putBlockHash(blockWithReceipts.getNumber(), newBlockHash);
updater.setChainHead(newBlockHash);
indexTransactionForBlock(
indexTransactionsForBlock(
updater, newBlockHash, blockWithReceipts.getBlock().getBody().getTransactions());
gasUsedCounter.inc(blockWithReceipts.getHeader().getGasUsed());
numberOfTransactionsCounter.inc(
Expand Down Expand Up @@ -652,7 +648,7 @@ private BlockAddedEvent handleChainReorg(
// 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);
});
Expand Down Expand Up @@ -792,11 +788,11 @@ private void updateCacheForNewCanonicalHead(final Block block, final Difficulty
chainHeadOmmerCount = block.getBody().getOmmers().size();
}

private static void indexTransactionForBlock(
final BlockchainStorage.Updater updater, final Hash hash, final List<Transaction> 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<Transaction> 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);
}
}
Expand Down