Skip to content

Commit 78f4c8b

Browse files
committed
prefer to use txindex if available for GetTransaction
Fixes #22382
1 parent 2749613 commit 78f4c8b

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

src/rpc/rawtransaction.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,10 @@ static RPCHelpMan getrawtransaction()
7474
"getrawtransaction",
7575
"\nReturn the raw transaction data.\n"
7676

77-
"\nBy default this function only works for mempool transactions. When called with a blockhash\n"
78-
"argument, getrawtransaction will return the transaction if the specified block is available and\n"
79-
"the transaction is found in that block. When called without a blockhash argument, getrawtransaction\n"
80-
"will return the transaction if it is in the mempool, or if -txindex is enabled and the transaction\n"
81-
"is in a block in the blockchain.\n"
82-
77+
"\nBy default, this call only returns a transaction if it is in the mempool. If -txindex is enabled\n"
78+
"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"
8381
"\nHint: Use gettransaction for wallet transactions.\n"
8482

8583
"\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
@@ -1159,6 +1159,20 @@ CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMe
11591159
{
11601160
LOCK(cs_main);
11611161

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+
}
11621176
if (block_index) {
11631177
CBlock block;
11641178
if (ReadBlockFromDisk(block, block_index, consensusParams)) {
@@ -1169,15 +1183,6 @@ CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMe
11691183
}
11701184
}
11711185
}
1172-
return nullptr;
1173-
}
1174-
if (mempool) {
1175-
CTransactionRef ptx = mempool->get(hash);
1176-
if (ptx) return ptx;
1177-
}
1178-
if (g_txindex) {
1179-
CTransactionRef tx;
1180-
if (g_txindex->FindTx(hash, hashBlock, tx)) return tx;
11811186
}
11821187
return nullptr;
11831188
}

src/validation.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,16 @@ void StartScriptCheckWorkerThreads(int threads_num);
142142
/** Stop all of the script checking worker threads */
143143
void StopScriptCheckWorkerThreads();
144144
/**
145-
* Return transaction from the block at block_index.
146-
* If block_index is not provided, fall back to mempool.
147-
* If mempool is not provided or the tx couldn't be found in mempool, fall back to g_txindex.
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.
148149
*
149150
* @param[in] block_index The block to read from disk, or nullptr
150-
* @param[in] mempool If block_index is not provided, look in the mempool, if provided
151+
* @param[in] mempool If provided, check mempool for tx
151152
* @param[in] hash The txid
152153
* @param[in] consensusParams The params
153-
* @param[out] hashBlock The hash of block_index, if the tx was found via block_index
154+
* @param[out] hashBlock The block hash, if the tx was found via -txindex or block_index
154155
* @returns The tx if found, otherwise nullptr
155156
*/
156157
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)