Skip to content

Commit 4b1fb50

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#22528: refactor: move GetTransaction to node/transaction.cpp
f685a13 doc: GetTransaction()/getrawtransaction follow-ups to #22383 (John Newbery) abc57e1 refactor: move `GetTransaction(...)` to node/transaction.cpp (Sebastian Falbesoner) Pull request description: ~This PR is based on #22383, which should be reviewed first~ (merged by now). In [yesterday's PR review club session to PR 22383](https://bitcoincore.reviews/22383), the idea of moving the function `GetTransaction(...)` from src/validation.cpp to src/node/transaction.cpp came up. With this, the circular dependency "index/txindex -> validation -> index/txindex" is removed (see change in `lint-circular-dependencies.sh`). Thanks to jnewbery for suggesting and to sipa for providing historical background. Relevant IRC log: ``` 17:52 <jnewbery> Was anyone surprised that GetTransaction() is in validation.cpp? It seems to me that node/transaction.cpp would be a more appropriate place for it. 17:53 <raj_> jnewbery, +1 17:53 <stickies-v> agreed! 17:54 <glozow> jnewbery ya 17:54 <jnewbery> seems weird that validation would call into txindex. I wonder if we remove this function, then validation would no longer need to #include txindex 17:54 <sipa> GetTransaction predates node/transaction.cpp, and even the generic index framework itself :) 17:55 <sipa> (before 0.8, validation itself used the txindex) 17:55 <jnewbery> (and GetTransaction() seems like a natural sibling to BroadcastTransaction(), which is already in node/transaction.cpp) 17:55 <jnewbery> sipa: right, this is not meant as a criticism of course. Just wondering if we can organize things a bit more rationally now that we have better separation between things. 17:55 <sipa> jnewbery: sure, just providing background 17:56 <sipa> seems very reasonable to move it elsewhere now ``` The commit should be trivial to review with `--color-moved`. ACKs for top commit: jnewbery: Code review ACK f685a13 rajarshimaitra: tACK bitcoin/bitcoin@f685a13 mjdietzx: crACK f685a13 LarryRuane: Code review, test ACK f685a13 Tree-SHA512: 0e844a6ecb1be04c638b55bc4478c2949549a4fcae01c984eee078de74d176fb19d508fc09360a62ad130677bfa7daf703b67870800e55942838d7313246248c
2 parents 67b9416 + f685a13 commit 4b1fb50

File tree

6 files changed

+61
-50
lines changed

6 files changed

+61
-50
lines changed

src/node/transaction.cpp

Lines changed: 38 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>
@@ -119,3 +122,38 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
119122

120123
return TransactionError::OK;
121124
}
125+
126+
CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMemPool* const mempool, const uint256& hash, const Consensus::Params& consensusParams, uint256& hashBlock)
127+
{
128+
LOCK(cs_main);
129+
130+
if (mempool && !block_index) {
131+
CTransactionRef ptx = mempool->get(hash);
132+
if (ptx) return ptx;
133+
}
134+
if (g_txindex) {
135+
CTransactionRef tx;
136+
uint256 block_hash;
137+
if (g_txindex->FindTx(hash, block_hash, tx)) {
138+
if (!block_index || block_index->GetBlockHash() == block_hash) {
139+
// Don't return the transaction if the provided block hash doesn't match.
140+
// The case where a transaction appears in multiple blocks (e.g. reorgs or
141+
// BIP30) is handled by the block lookup below.
142+
hashBlock = block_hash;
143+
return tx;
144+
}
145+
}
146+
}
147+
if (block_index) {
148+
CBlock block;
149+
if (ReadBlockFromDisk(block, block_index, consensusParams)) {
150+
for (const auto& tx : block.vtx) {
151+
if (tx->GetHash() == hash) {
152+
hashBlock = block_index->GetBlockHash();
153+
return tx;
154+
}
155+
}
156+
}
157+
}
158+
return nullptr;
159+
}

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/rpc/rawtransaction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ static RPCHelpMan getrawtransaction()
7676

7777
"\nBy default, this call only returns a transaction if it is in the mempool. If -txindex is enabled\n"
7878
"and no blockhash argument is passed, it will return the transaction if it is in the mempool or any block.\n"
79-
"If -txindex is not enabled and a blockhash argument is passed, it will return the transaction if\n"
80-
"the specified block is available and the transaction is found in that block.\n"
79+
"If a blockhash argument is passed, it will return the transaction if\n"
80+
"the specified block is available and the transaction is in that block.\n"
8181
"\nHint: Use gettransaction for wallet transactions.\n"
8282

8383
"\nIf verbose is 'true', returns an Object with information about 'txid'.\n"

src/validation.cpp

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include <flatfile.h>
2020
#include <hash.h>
2121
#include <index/blockfilterindex.h>
22-
#include <index/txindex.h>
2322
#include <logging.h>
2423
#include <logging/timer.h>
2524
#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
@@ -140,20 +140,7 @@ void UnloadBlockIndex(CTxMemPool* mempool, ChainstateManager& chainman);
140140
void StartScriptCheckWorkerThreads(int threads_num);
141141
/** Stop all of the script checking worker threads */
142142
void StopScriptCheckWorkerThreads();
143-
/**
144-
* Return transaction with a given hash.
145-
* If mempool is provided and block_index is not provided, check it first for the tx.
146-
* If -txindex is available, check it next for the tx.
147-
* Finally, if block_index is provided, check for tx by reading entire block from disk.
148-
*
149-
* @param[in] block_index The block to read from disk, or nullptr
150-
* @param[in] mempool If provided, check mempool for tx
151-
* @param[in] hash The txid
152-
* @param[in] consensusParams The params
153-
* @param[out] hashBlock The block hash, if the tx was found via -txindex or block_index
154-
* @returns The tx if found, otherwise nullptr
155-
*/
156-
CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMemPool* const mempool, const uint256& hash, const Consensus::Params& consensusParams, uint256& hashBlock);
143+
157144
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams);
158145

159146
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)