Skip to content

Commit abc57e1

Browse files
committed
refactor: move GetTransaction(...) to node/transaction.cpp
can be reviewed with --color-moved
1 parent 78f4c8b commit abc57e1

File tree

5 files changed

+56
-48
lines changed

5 files changed

+56
-48
lines changed

src/node/transaction.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#include <consensus/validation.h>
7+
#include <index/txindex.h>
78
#include <net.h>
89
#include <net_processing.h>
10+
#include <node/blockstorage.h>
911
#include <node/context.h>
12+
#include <txmempool.h>
1013
#include <validation.h>
1114
#include <validationinterface.h>
1215
#include <node/transaction.h>
@@ -104,3 +107,35 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
104107

105108
return TransactionError::OK;
106109
}
110+
111+
CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMemPool* const mempool, const uint256& hash, const Consensus::Params& consensusParams, uint256& hashBlock)
112+
{
113+
LOCK(cs_main);
114+
115+
if (mempool && !block_index) {
116+
CTransactionRef ptx = mempool->get(hash);
117+
if (ptx) return ptx;
118+
}
119+
if (g_txindex) {
120+
CTransactionRef tx;
121+
uint256 block_hash;
122+
if (g_txindex->FindTx(hash, block_hash, tx)) {
123+
if (!block_index || block_index->GetBlockHash() == block_hash) {
124+
hashBlock = block_hash;
125+
return tx;
126+
}
127+
}
128+
}
129+
if (block_index) {
130+
CBlock block;
131+
if (ReadBlockFromDisk(block, block_index, consensusParams)) {
132+
for (const auto& tx : block.vtx) {
133+
if (tx->GetHash() == hash) {
134+
hashBlock = block_index->GetBlockHash();
135+
return tx;
136+
}
137+
}
138+
}
139+
}
140+
return nullptr;
141+
}

src/node/transaction.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
#include <primitives/transaction.h>
1111
#include <util/error.h>
1212

13+
class CBlockIndex;
14+
class CTxMemPool;
1315
struct NodeContext;
16+
namespace Consensus {
17+
struct Params;
18+
}
1419

1520
/** Maximum fee rate for sendrawtransaction and testmempoolaccept RPC calls.
1621
* Also used by the GUI when broadcasting a completed PSBT.
@@ -38,4 +43,19 @@ static const CFeeRate DEFAULT_MAX_RAW_TX_FEE_RATE{COIN / 10};
3843
*/
3944
[[nodiscard]] TransactionError BroadcastTransaction(NodeContext& node, CTransactionRef tx, std::string& err_string, const CAmount& max_tx_fee, bool relay, bool wait_callback);
4045

46+
/**
47+
* Return transaction with a given hash.
48+
* If mempool is provided and block_index is not provided, check it first for the tx.
49+
* If -txindex is available, check it next for the tx.
50+
* Finally, if block_index is provided, check for tx by reading entire block from disk.
51+
*
52+
* @param[in] block_index The block to read from disk, or nullptr
53+
* @param[in] mempool If provided, check mempool for tx
54+
* @param[in] hash The txid
55+
* @param[in] consensusParams The params
56+
* @param[out] hashBlock The block hash, if the tx was found via -txindex or block_index
57+
* @returns The tx if found, otherwise nullptr
58+
*/
59+
CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMemPool* const mempool, const uint256& hash, const Consensus::Params& consensusParams, uint256& hashBlock);
60+
4161
#endif // BITCOIN_NODE_TRANSACTION_H

src/validation.cpp

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <flatfile.h>
1919
#include <hash.h>
2020
#include <index/blockfilterindex.h>
21-
#include <index/txindex.h>
2221
#include <logging.h>
2322
#include <logging/timer.h>
2423
#include <node/blockstorage.h>
@@ -1155,38 +1154,6 @@ PackageMempoolAcceptResult ProcessNewPackage(CChainState& active_chainstate, CTx
11551154
return result;
11561155
}
11571156

1158-
CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMemPool* const mempool, const uint256& hash, const Consensus::Params& consensusParams, uint256& hashBlock)
1159-
{
1160-
LOCK(cs_main);
1161-
1162-
if (mempool && !block_index) {
1163-
CTransactionRef ptx = mempool->get(hash);
1164-
if (ptx) return ptx;
1165-
}
1166-
if (g_txindex) {
1167-
CTransactionRef tx;
1168-
uint256 block_hash;
1169-
if (g_txindex->FindTx(hash, block_hash, tx)) {
1170-
if (!block_index || block_index->GetBlockHash() == block_hash) {
1171-
hashBlock = block_hash;
1172-
return tx;
1173-
}
1174-
}
1175-
}
1176-
if (block_index) {
1177-
CBlock block;
1178-
if (ReadBlockFromDisk(block, block_index, consensusParams)) {
1179-
for (const auto& tx : block.vtx) {
1180-
if (tx->GetHash() == hash) {
1181-
hashBlock = block_index->GetBlockHash();
1182-
return tx;
1183-
}
1184-
}
1185-
}
1186-
}
1187-
return nullptr;
1188-
}
1189-
11901157
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
11911158
{
11921159
int halvings = nHeight / consensusParams.nSubsidyHalvingInterval;

src/validation.h

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,20 +141,7 @@ void UnloadBlockIndex(CTxMemPool* mempool, ChainstateManager& chainman);
141141
void StartScriptCheckWorkerThreads(int threads_num);
142142
/** Stop all of the script checking worker threads */
143143
void StopScriptCheckWorkerThreads();
144-
/**
145-
* Return transaction with a given hash.
146-
* If mempool is provided and block_index is not provided, check it first for the tx.
147-
* If -txindex is available, check it next for the tx.
148-
* Finally, if block_index is provided, check for tx by reading entire block from disk.
149-
*
150-
* @param[in] block_index The block to read from disk, or nullptr
151-
* @param[in] mempool If provided, check mempool for tx
152-
* @param[in] hash The txid
153-
* @param[in] consensusParams The params
154-
* @param[out] hashBlock The block hash, if the tx was found via -txindex or block_index
155-
* @returns The tx if found, otherwise nullptr
156-
*/
157-
CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMemPool* const mempool, const uint256& hash, const Consensus::Params& consensusParams, uint256& hashBlock);
144+
158145
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams);
159146

160147
bool AbortNode(BlockValidationState& state, const std::string& strMessage, const bilingual_str& userMessage = bilingual_str{});

test/lint/lint-circular-dependencies.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export LC_ALL=C
1010

1111
EXPECTED_CIRCULAR_DEPENDENCIES=(
1212
"chainparamsbase -> util/system -> chainparamsbase"
13-
"index/txindex -> validation -> index/txindex"
1413
"node/blockstorage -> validation -> node/blockstorage"
1514
"index/blockfilterindex -> node/blockstorage -> validation -> index/blockfilterindex"
1615
"index/base -> validation -> index/blockfilterindex -> index/base"

0 commit comments

Comments
 (0)