diff options
author | Jim Posen <jimpo@coinbase.com> | 2018-03-30 00:39:08 -0700 |
---|---|---|
committer | Jim Posen <jimpo@coinbase.com> | 2018-04-25 11:25:15 -0700 |
commit | a03f804f2aa0261ed3a47103dfe989ebd9302480 (patch) | |
tree | 0bb7e5c43cc73c6bf0a439e2f7c55632a029a547 /src | |
parent | e0a3b80033be388b7b8ecce8bd4273867e4bb699 (diff) |
[index] Move disk IO logic from GetTransaction to TxIndex::FindTx.
Diffstat (limited to 'src')
-rw-r--r-- | src/index/txindex.cpp | 25 | ||||
-rw-r--r-- | src/index/txindex.h | 10 | ||||
-rw-r--r-- | src/validation.cpp | 22 |
3 files changed, 32 insertions, 25 deletions
diff --git a/src/index/txindex.cpp b/src/index/txindex.cpp index 7992d85338..2a661f0330 100644 --- a/src/index/txindex.cpp +++ b/src/index/txindex.cpp @@ -254,9 +254,30 @@ bool TxIndex::BlockUntilSyncedToCurrentChain() return true; } -bool TxIndex::FindTx(const uint256& txid, CDiskTxPos& pos) const +bool TxIndex::FindTx(const uint256& tx_hash, uint256& block_hash, CTransactionRef& tx) const { - return m_db->ReadTxPos(txid, pos); + CDiskTxPos postx; + if (!m_db->ReadTxPos(tx_hash, postx)) { + return false; + } + + CAutoFile file(OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION); + if (file.IsNull()) { + return error("%s: OpenBlockFile failed", __func__); + } + CBlockHeader header; + try { + file >> header; + fseek(file.Get(), postx.nTxOffset, SEEK_CUR); + file >> tx; + } catch (const std::exception& e) { + return error("%s: Deserialize or I/O error - %s", __func__, e.what()); + } + if (tx->GetHash() != tx_hash) { + return error("%s: txid mismatch", __func__); + } + block_hash = header.GetHash(); + return true; } void TxIndex::Interrupt() diff --git a/src/index/txindex.h b/src/index/txindex.h index 41199f0b3f..ac746de05b 100644 --- a/src/index/txindex.h +++ b/src/index/txindex.h @@ -6,6 +6,7 @@ #define BITCOIN_INDEX_TXINDEX_H #include <primitives/block.h> +#include <primitives/transaction.h> #include <threadinterrupt.h> #include <txdb.h> #include <uint256.h> @@ -69,8 +70,13 @@ public: /// up from far behind, this method does not block and immediately returns false. bool BlockUntilSyncedToCurrentChain(); - /// Look up the on-disk location of a transaction by hash. - bool FindTx(const uint256& txid, CDiskTxPos& pos) const; + /// Look up a transaction by hash. + /// + /// @param[in] tx_hash The hash of the transaction to be returned. + /// @param[out] block_hash The hash of the block the transaction is found in. + /// @param[out] tx The transaction itself. + /// @return true if transaction is found, false otherwise + bool FindTx(const uint256& tx_hash, uint256& block_hash, CTransactionRef& tx) const; void Interrupt(); diff --git a/src/validation.cpp b/src/validation.cpp index 5ea81bfc98..14257d78f9 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -1029,27 +1029,7 @@ bool GetTransaction(const uint256& hash, CTransactionRef& txOut, const Consensus } if (g_txindex) { - CDiskTxPos postx; - if (g_txindex->FindTx(hash, postx)) { - CAutoFile file(OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION); - if (file.IsNull()) - return error("%s: OpenBlockFile failed", __func__); - CBlockHeader header; - try { - file >> header; - fseek(file.Get(), postx.nTxOffset, SEEK_CUR); - file >> txOut; - } catch (const std::exception& e) { - return error("%s: Deserialize or I/O error - %s", __func__, e.what()); - } - hashBlock = header.GetHash(); - if (txOut->GetHash() != hash) - return error("%s: txid mismatch", __func__); - return true; - } - - // transaction not found in index, nothing more can be done - return false; + return g_txindex->FindTx(hash, hashBlock, txOut); } if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it |