Skip to content

Commit ad3086c

Browse files
MarcoFalkePastaPastaPasta
authored andcommitted
Merge bitcoin#22383: rpc: Prefer to use txindex if available for GetTransaction
78f4c8b prefer to use txindex if available for GetTransaction (Jameson Lopp) Pull request description: Fixes bitcoin#22382 Motivation: prevent excessive disk reads if txindex is enabled. Worth noting that this could be argued to be less of a bug and more of an issue of undefined behavior. If a user calls GetTransaction with the wrong block hash, what should happen? ACKs for top commit: jonatack: ACK 78f4c8b theStack: Code review ACK 78f4c8b LarryRuane: tACK 78f4c8b luke-jr: utACK 78f4c8b jnewbery: utACK 78f4c8b rajarshimaitra: Code review ACK bitcoin@78f4c8b lsilva01: Code Review ACK and Tested ACK bitcoin@78f4c8b on Ubuntu 20.04 Tree-SHA512: af7db5b98cb2ae4897b28476b2fa243bf7e6f850750d9347062fe8013c5720986d1a3c808f80098e5289bd84b085de03c81a44e584dc28982f721c223651bfe0
1 parent 7eb5033 commit ad3086c

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

src/rpc/rawtransaction.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ static UniValue getrawtransaction(const JSONRPCRequest& request)
113113
RPCHelpMan{
114114
"getrawtransaction",
115115
"\nReturn the raw transaction data.\n"
116-
"\nBy default this function only works for mempool transactions. When called with a blockhash\n"
117-
"argument, getrawtransaction will return the transaction if the specified block is available and\n"
118-
"the transaction is found in that block. When called without a blockhash argument, getrawtransaction\n"
119-
"will return the transaction if it is in the mempool, or if -txindex is enabled and the transaction\n"
120-
"is in a block in the blockchain.\n"
116+
117+
"\nBy default, this call only returns a transaction if it is in the mempool. If -txindex is enabled\n"
118+
"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"
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: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,20 @@ CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMe
10721072
{
10731073
LOCK(cs_main);
10741074

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+
}
10751089
if (block_index) {
10761090
CBlock block;
10771091
if (ReadBlockFromDisk(block, block_index, consensusParams)) {
@@ -1082,15 +1096,6 @@ CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMe
10821096
}
10831097
}
10841098
}
1085-
return nullptr;
1086-
}
1087-
if (mempool) {
1088-
CTransactionRef ptx = mempool->get(hash);
1089-
if (ptx) return ptx;
1090-
}
1091-
if (g_txindex) {
1092-
CTransactionRef tx;
1093-
if (g_txindex->FindTx(hash, hashBlock, tx)) return tx;
10941099
}
10951100
return nullptr;
10961101
}

src/validation.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,16 @@ void StartScriptCheckWorkerThreads(int threads_num);
185185
/** Stop all of the script checking worker threads */
186186
void StopScriptCheckWorkerThreads();
187187
/**
188-
* Return transaction from the block at block_index.
189-
* If block_index is not provided, fall back to mempool.
190-
* If mempool is not provided or the tx couldn't be found in mempool, fall back to g_txindex.
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.
191192
*
192193
* @param[in] block_index The block to read from disk, or nullptr
193-
* @param[in] mempool If block_index is not provided, look in the mempool, if provided
194+
* @param[in] mempool If provided, check mempool for tx
194195
* @param[in] hash The txid
195196
* @param[in] consensusParams The params
196-
* @param[out] hashBlock The hash of block_index, if the tx was found via block_index
197+
* @param[out] hashBlock The block hash, if the tx was found via -txindex or block_index
197198
* @returns The tx if found, otherwise nullptr
198199
*/
199200
CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMemPool* const mempool, const uint256& hash, const Consensus::Params& consensusParams, uint256& hashBlock);

0 commit comments

Comments
 (0)