Skip to content

Commit 04da9f4

Browse files
committed
[RPC] Update getrawtransaction interface
1 parent a9b71a0 commit 04da9f4

12 files changed

+35
-28
lines changed

doc/release-notes.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ in the Low-level Changes section below.
246246

247247
- See the [Mining](#mining) section for changes to `getblocktemplate`.
248248

249+
- The `getrawtransaction` RPC no longer checks the unspent UTXO set for
250+
a transaction. The remaining behaviors are as follows: 1. If a
251+
blockhash is provided, check the corresponding block. 2. If no
252+
blockhash is provided, check the mempool. 3. If no blockhash is
253+
provided but txindex is enabled, also check txindex.
254+
249255
Graphical User Interface (GUI)
250256
------------------------------
251257

src/rest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ static bool rest_tx(HTTPRequest* req, const std::string& strURIPart)
352352

353353
CTransactionRef tx;
354354
uint256 hashBlock = uint256();
355-
if (!GetTransaction(hash, tx, Params().GetConsensus(), hashBlock, true))
355+
if (!GetTransaction(hash, tx, Params().GetConsensus(), hashBlock))
356356
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
357357

358358
switch (rf) {

src/rpc/blockchain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1918,7 +1918,7 @@ static UniValue getblockstats(const JSONRPCRequest& request)
19181918
for (const CTxIn& in : tx->vin) {
19191919
CTransactionRef tx_in;
19201920
uint256 hashBlock;
1921-
if (!GetTransaction(in.prevout.hash, tx_in, Params().GetConsensus(), hashBlock, false)) {
1921+
if (!GetTransaction(in.prevout.hash, tx_in, Params().GetConsensus(), hashBlock)) {
19221922
throw JSONRPCError(RPC_INTERNAL_ERROR, std::string("Unexpected internal error (tx index seems corrupt)"));
19231923
}
19241924

src/rpc/rawtransaction.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,11 @@ static UniValue getrawtransaction(const JSONRPCRequest& request)
6767
if (request.fHelp || request.params.size() < 1 || request.params.size() > 3)
6868
throw std::runtime_error(
6969
RPCHelpMan{"getrawtransaction",
70-
"\nNOTE: By default this function only works for mempool transactions. If the -txindex option is\n"
71-
"enabled, it also works for blockchain transactions. If the block which contains the transaction\n"
72-
"is known, its hash can be provided even for nodes without -txindex. Note that if a blockhash is\n"
73-
"provided, only that block will be searched and if the transaction is in the mempool or other\n"
74-
"blocks, or if this node does not have the given block available, the transaction will not be found.\n"
75-
"DEPRECATED: for now, it also works for transactions with unspent outputs.\n"
70+
"\nBy default this function only works for mempool transactions. When called with a blockhash\n"
71+
"argument, getrawtransaction will return the transaction if the specified block is available and\n"
72+
"the transaction is found in that block. When called without a blockhash argument, getrawtransaction\n"
73+
"will return the transaction if it is in the mempool, or if -txindex is enabled and the transaction\n"
74+
"is in a block in the blockchain.\n"
7675

7776
"\nReturn the raw transaction data.\n"
7877
"\nIf verbose is 'true', returns an Object with information about 'txid'.\n"
@@ -175,7 +174,7 @@ static UniValue getrawtransaction(const JSONRPCRequest& request)
175174

176175
CTransactionRef tx;
177176
uint256 hash_block;
178-
if (!GetTransaction(hash, tx, Params().GetConsensus(), hash_block, true, blockindex)) {
177+
if (!GetTransaction(hash, tx, Params().GetConsensus(), hash_block, blockindex)) {
179178
std::string errmsg;
180179
if (blockindex) {
181180
if (!(blockindex->nStatus & BLOCK_HAVE_DATA)) {
@@ -270,7 +269,7 @@ static UniValue gettxoutproof(const JSONRPCRequest& request)
270269
if (pblockindex == nullptr)
271270
{
272271
CTransactionRef tx;
273-
if (!GetTransaction(oneTxid, tx, Params().GetConsensus(), hashBlock, false) || hashBlock.IsNull())
272+
if (!GetTransaction(oneTxid, tx, Params().GetConsensus(), hashBlock) || hashBlock.IsNull())
274273
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not yet in block");
275274
pblockindex = LookupBlockIndex(hashBlock);
276275
if (!pblockindex) {

src/validation.cpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -994,13 +994,11 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
994994
* Return transaction in txOut, and if it was found inside a block, its hash is placed in hashBlock.
995995
* If blockIndex is provided, the transaction is fetched from the corresponding block.
996996
*/
997-
bool GetTransaction(const uint256& hash, CTransactionRef& txOut, const Consensus::Params& consensusParams, uint256& hashBlock, bool fAllowSlow, CBlockIndex* blockIndex)
997+
bool GetTransaction(const uint256& hash, CTransactionRef& txOut, const Consensus::Params& consensusParams, uint256& hashBlock, const CBlockIndex* const block_index)
998998
{
999-
CBlockIndex* pindexSlow = blockIndex;
1000-
1001999
LOCK(cs_main);
10021000

1003-
if (!blockIndex) {
1001+
if (!block_index) {
10041002
CTransactionRef ptx = mempool.get(hash);
10051003
if (ptx) {
10061004
txOut = ptx;
@@ -1010,20 +1008,13 @@ bool GetTransaction(const uint256& hash, CTransactionRef& txOut, const Consensus
10101008
if (g_txindex) {
10111009
return g_txindex->FindTx(hash, hashBlock, txOut);
10121010
}
1013-
1014-
if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it
1015-
const Coin& coin = AccessByTxid(*pcoinsTip, hash);
1016-
if (!coin.IsSpent()) pindexSlow = chainActive[coin.nHeight];
1017-
}
1018-
}
1019-
1020-
if (pindexSlow) {
1011+
} else {
10211012
CBlock block;
1022-
if (ReadBlockFromDisk(block, pindexSlow, consensusParams)) {
1013+
if (ReadBlockFromDisk(block, block_index, consensusParams)) {
10231014
for (const auto& tx : block.vtx) {
10241015
if (tx->GetHash() == hash) {
10251016
txOut = tx;
1026-
hashBlock = pindexSlow->GetBlockHash();
1017+
hashBlock = block_index->GetBlockHash();
10271018
return true;
10281019
}
10291020
}

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ void ThreadScriptCheck();
269269
/** Check whether we are doing an initial block download (synchronizing from disk or network) */
270270
bool IsInitialBlockDownload();
271271
/** Retrieve a transaction (from memory pool, or from disk, if possible) */
272-
bool GetTransaction(const uint256& hash, CTransactionRef& tx, const Consensus::Params& params, uint256& hashBlock, bool fAllowSlow = false, CBlockIndex* blockIndex = nullptr);
272+
bool GetTransaction(const uint256& hash, CTransactionRef& tx, const Consensus::Params& params, uint256& hashBlock, const CBlockIndex* const blockIndex = nullptr);
273273
/**
274274
* Find the best known block, and make it the tip of the block chain
275275
*

test/functional/feature_segwit.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,26 @@ def set_test_params(self):
4343
self.setup_clean_chain = True
4444
self.num_nodes = 3
4545
# This test tests SegWit both pre and post-activation, so use the normal BIP9 activation.
46+
# TODO: remove -txindex. Currently required for getrawtransaction call.
4647
self.extra_args = [
4748
[
4849
"-rpcserialversion=0",
4950
"-vbparams=segwit:0:999999999999",
5051
"-addresstype=legacy",
52+
"-txindex"
5153
],
5254
[
5355
"-blockversion=4",
5456
"-rpcserialversion=1",
5557
"-vbparams=segwit:0:999999999999",
5658
"-addresstype=legacy",
59+
"-txindex"
5760
],
5861
[
5962
"-blockversion=536870915",
6063
"-vbparams=segwit:0:999999999999",
6164
"-addresstype=legacy",
65+
"-txindex"
6266
],
6367
]
6468

test/functional/interface_rest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ class RESTTest (BitcoinTestFramework):
4141
def set_test_params(self):
4242
self.setup_clean_chain = True
4343
self.num_nodes = 2
44-
self.extra_args = [["-rest"], []]
44+
# TODO: remove -txindex. Currently required for getrawtransaction call.
45+
self.extra_args = [["-rest", "-txindex"], []]
4546

4647
def skip_test_if_missing_module(self):
4748
self.skip_if_no_wallet()

test/functional/rpc_psbt.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class PSBTTest(BitcoinTestFramework):
1919
def set_test_params(self):
2020
self.setup_clean_chain = False
2121
self.num_nodes = 3
22+
# TODO: remove -txindex. Currently required for getrawtransaction call.
23+
self.extra_args = [[], ["-txindex"], ["-txindex"]]
2224

2325
def skip_test_if_missing_module(self):
2426
self.skip_if_no_wallet()

test/functional/rpc_rawtransaction.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ class RawTransactionsTest(BitcoinTestFramework):
4242
def set_test_params(self):
4343
self.setup_clean_chain = True
4444
self.num_nodes = 3
45-
self.extra_args = [["-addresstype=legacy"], ["-addresstype=legacy"], ["-addresstype=legacy"]]
45+
# TODO: remove -txindex. Currently required for getrawtransaction call.
46+
self.extra_args = [["-addresstype=legacy", "-txindex"], ["-addresstype=legacy", "-txindex"], ["-addresstype=legacy", "-txindex"]]
4647

4748
def skip_test_if_missing_module(self):
4849
self.skip_if_no_wallet()

0 commit comments

Comments
 (0)