Skip to content

Commit fbddd23

Browse files
MarcoFalkePastaPastaPasta
authored andcommitted
Merge bitcoin#22528: refactor: move GetTransaction to node/transaction.cpp
f685a13 doc: GetTransaction()/getrawtransaction follow-ups to bitcoin#22383 (John Newbery) abc57e1 refactor: move `GetTransaction(...)` to node/transaction.cpp (Sebastian Falbesoner) Pull request description: ~This PR is based on bitcoin#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@f685a13 mjdietzx: crACK f685a13 LarryRuane: Code review, test ACK f685a13 Tree-SHA512: 0e844a6ecb1be04c638b55bc4478c2949549a4fcae01c984eee078de74d176fb19d508fc09360a62ad130677bfa7daf703b67870800e55942838d7313246248c
1 parent 8109f00 commit fbddd23

File tree

6 files changed

+60
-48
lines changed

6 files changed

+60
-48
lines changed

src/node/transaction.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
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 <txmempool.h>
1012
#include <validation.h>
1113
#include <validationinterface.h>
@@ -90,3 +92,38 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
9092

9193
return TransactionError::OK;
9294
}
95+
96+
CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMemPool* const mempool, const uint256& hash, const Consensus::Params& consensusParams, uint256& hashBlock)
97+
{
98+
LOCK(cs_main);
99+
100+
if (mempool && !block_index) {
101+
CTransactionRef ptx = mempool->get(hash);
102+
if (ptx) return ptx;
103+
}
104+
if (g_txindex) {
105+
CTransactionRef tx;
106+
uint256 block_hash;
107+
if (g_txindex->FindTx(hash, block_hash, tx)) {
108+
if (!block_index || block_index->GetBlockHash() == block_hash) {
109+
// Don't return the transaction if the provided block hash doesn't match.
110+
// The case where a transaction appears in multiple blocks (e.g. reorgs or
111+
// BIP30) is handled by the block lookup below.
112+
hashBlock = block_hash;
113+
return tx;
114+
}
115+
}
116+
}
117+
if (block_index) {
118+
CBlock block;
119+
if (ReadBlockFromDisk(block, block_index, consensusParams)) {
120+
for (const auto& tx : block.vtx) {
121+
if (tx->GetHash() == hash) {
122+
hashBlock = block_index->GetBlockHash();
123+
return tx;
124+
}
125+
}
126+
}
127+
}
128+
return nullptr;
129+
}

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& highfee, bool relay, bool wait_callback, bool bypass_limits = false);
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
@@ -116,8 +116,8 @@ static UniValue getrawtransaction(const JSONRPCRequest& request)
116116

117117
"\nBy default, this call only returns a transaction if it is in the mempool. If -txindex is enabled\n"
118118
"and no blockhash argument is passed, it will return the transaction if it is in the mempool or any block.\n"
119-
"If -txindex is not enabled and a blockhash argument is passed, it will return the transaction if\n"
120-
"the specified block is available and the transaction is found in that block.\n"
119+
"If a blockhash argument is passed, it will return the transaction if\n"
120+
"the specified block is available and the transaction is in that block.\n"
121121
"\nHint: Use gettransaction for wallet transactions.\n"
122122

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

src/validation.cpp

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include <flatfile.h>
2121
#include <hash.h>
2222
#include <index/blockfilterindex.h>
23-
#include <index/txindex.h>
2423
#include <logging.h>
2524
#include <logging/timer.h>
2625
#include <node/blockstorage.h>
@@ -1068,37 +1067,6 @@ bool GetAddressUnspent(uint160 addressHash, AddressType type,
10681067
return true;
10691068
}
10701069

1071-
CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMemPool* const mempool, const uint256& hash, const Consensus::Params& consensusParams, uint256& hashBlock)
1072-
{
1073-
LOCK(cs_main);
1074-
1075-
if (mempool && !block_index) {
1076-
CTransactionRef ptx = mempool->get(hash);
1077-
if (ptx) return ptx;
1078-
}
1079-
if (g_txindex) {
1080-
CTransactionRef tx;
1081-
uint256 block_hash;
1082-
if (g_txindex->FindTx(hash, block_hash, tx)) {
1083-
if (!block_index || block_index->GetBlockHash() == block_hash) {
1084-
hashBlock = block_hash;
1085-
return tx;
1086-
}
1087-
}
1088-
}
1089-
if (block_index) {
1090-
CBlock block;
1091-
if (ReadBlockFromDisk(block, block_index, consensusParams)) {
1092-
for (const auto& tx : block.vtx) {
1093-
if (tx->GetHash() == hash) {
1094-
hashBlock = block_index->GetBlockHash();
1095-
return tx;
1096-
}
1097-
}
1098-
}
1099-
}
1100-
return nullptr;
1101-
}
11021070

11031071
double ConvertBitsToDouble(unsigned int nBits)
11041072
{

src/validation.h

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -184,19 +184,7 @@ void UnloadBlockIndex(CTxMemPool* mempool, ChainstateManager& chainman);
184184
void StartScriptCheckWorkerThreads(int threads_num);
185185
/** Stop all of the script checking worker threads */
186186
void StopScriptCheckWorkerThreads();
187-
/**
188-
* Return transaction with a given hash.
189-
* If mempool is provided and block_index is not provided, check it first for the tx.
190-
* If -txindex is available, check it next for the tx.
191-
* Finally, if block_index is provided, check for tx by reading entire block from disk.
192-
*
193-
* @param[in] block_index The block to read from disk, or nullptr
194-
* @param[in] mempool If provided, check mempool for tx
195-
* @param[in] hash The txid
196-
* @param[in] consensusParams The params
197-
* @param[out] hashBlock The block hash, if the tx was found via -txindex or block_index
198-
* @returns The tx if found, otherwise nullptr
199-
*/
187+
200188
CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMemPool* const mempool, const uint256& hash, const Consensus::Params& consensusParams, uint256& hashBlock);
201189

202190
double ConvertBitsToDouble(unsigned int nBits);

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)