Skip to content

Commit a03f804

Browse files
author
Jim Posen
committed
[index] Move disk IO logic from GetTransaction to TxIndex::FindTx.
1 parent e0a3b80 commit a03f804

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

src/index/txindex.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,30 @@ bool TxIndex::BlockUntilSyncedToCurrentChain()
254254
return true;
255255
}
256256

257-
bool TxIndex::FindTx(const uint256& txid, CDiskTxPos& pos) const
257+
bool TxIndex::FindTx(const uint256& tx_hash, uint256& block_hash, CTransactionRef& tx) const
258258
{
259-
return m_db->ReadTxPos(txid, pos);
259+
CDiskTxPos postx;
260+
if (!m_db->ReadTxPos(tx_hash, postx)) {
261+
return false;
262+
}
263+
264+
CAutoFile file(OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION);
265+
if (file.IsNull()) {
266+
return error("%s: OpenBlockFile failed", __func__);
267+
}
268+
CBlockHeader header;
269+
try {
270+
file >> header;
271+
fseek(file.Get(), postx.nTxOffset, SEEK_CUR);
272+
file >> tx;
273+
} catch (const std::exception& e) {
274+
return error("%s: Deserialize or I/O error - %s", __func__, e.what());
275+
}
276+
if (tx->GetHash() != tx_hash) {
277+
return error("%s: txid mismatch", __func__);
278+
}
279+
block_hash = header.GetHash();
280+
return true;
260281
}
261282

262283
void TxIndex::Interrupt()

src/index/txindex.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define BITCOIN_INDEX_TXINDEX_H
77

88
#include <primitives/block.h>
9+
#include <primitives/transaction.h>
910
#include <threadinterrupt.h>
1011
#include <txdb.h>
1112
#include <uint256.h>
@@ -69,8 +70,13 @@ class TxIndex final : public CValidationInterface
6970
/// up from far behind, this method does not block and immediately returns false.
7071
bool BlockUntilSyncedToCurrentChain();
7172

72-
/// Look up the on-disk location of a transaction by hash.
73-
bool FindTx(const uint256& txid, CDiskTxPos& pos) const;
73+
/// Look up a transaction by hash.
74+
///
75+
/// @param[in] tx_hash The hash of the transaction to be returned.
76+
/// @param[out] block_hash The hash of the block the transaction is found in.
77+
/// @param[out] tx The transaction itself.
78+
/// @return true if transaction is found, false otherwise
79+
bool FindTx(const uint256& tx_hash, uint256& block_hash, CTransactionRef& tx) const;
7480

7581
void Interrupt();
7682

src/validation.cpp

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,27 +1029,7 @@ bool GetTransaction(const uint256& hash, CTransactionRef& txOut, const Consensus
10291029
}
10301030

10311031
if (g_txindex) {
1032-
CDiskTxPos postx;
1033-
if (g_txindex->FindTx(hash, postx)) {
1034-
CAutoFile file(OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION);
1035-
if (file.IsNull())
1036-
return error("%s: OpenBlockFile failed", __func__);
1037-
CBlockHeader header;
1038-
try {
1039-
file >> header;
1040-
fseek(file.Get(), postx.nTxOffset, SEEK_CUR);
1041-
file >> txOut;
1042-
} catch (const std::exception& e) {
1043-
return error("%s: Deserialize or I/O error - %s", __func__, e.what());
1044-
}
1045-
hashBlock = header.GetHash();
1046-
if (txOut->GetHash() != hash)
1047-
return error("%s: txid mismatch", __func__);
1048-
return true;
1049-
}
1050-
1051-
// transaction not found in index, nothing more can be done
1052-
return false;
1032+
return g_txindex->FindTx(hash, hashBlock, txOut);
10531033
}
10541034

10551035
if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it

0 commit comments

Comments
 (0)