Skip to content

Commit f866971

Browse files
committed
rpc: Return fee and prevout(s) to getrawtransaction
* Add optional fee response in BTC to getrawtransaction * Add optional prevout(s) response to getrawtransaction showing utxos being spent * Add getrawtransaction_verbosity functional test to validate fields
1 parent bfce05c commit f866971

File tree

3 files changed

+164
-30
lines changed

3 files changed

+164
-30
lines changed

src/rpc/client.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
102102
{ "getchaintxstats", 0, "nblocks" },
103103
{ "gettransaction", 1, "include_watchonly" },
104104
{ "gettransaction", 2, "verbose" },
105+
{ "getrawtransaction", 1, "verbosity" },
105106
{ "getrawtransaction", 1, "verbose" },
106107
{ "createrawtransaction", 0, "inputs" },
107108
{ "createrawtransaction", 1, "outputs" },

src/rpc/rawtransaction.cpp

Lines changed: 88 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <script/signingprovider.h>
3333
#include <script/standard.h>
3434
#include <uint256.h>
35+
#include <undo.h>
3536
#include <util/bip32.h>
3637
#include <util/check.h>
3738
#include <util/strencodings.h>
@@ -50,15 +51,17 @@ using node::FindCoins;
5051
using node::GetTransaction;
5152
using node::NodeContext;
5253
using node::PSBTAnalysis;
54+
using node::ReadBlockFromDisk;
55+
using node::UndoReadFromDisk;
5356

54-
static void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry, Chainstate& active_chainstate)
57+
static void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry, Chainstate& active_chainstate, const CTxUndo* txundo = nullptr, TxVerbosity verbosity = TxVerbosity::SHOW_TXID)
5558
{
5659
// Call into TxToUniv() in bitcoin-common to decode the transaction hex.
5760
//
5861
// Blockchain contextual information (confirmations and blocktime) is not
5962
// available to code in bitcoin-common, so we query them here and push the
6063
// data into the returned UniValue.
61-
TxToUniv(tx, /*block_hash=*/uint256(), entry, /*include_hex=*/true, RPCSerializationFlags());
64+
TxToUniv(tx, /*block_hash=*/uint256(), entry, /*include_hex=*/true, RPCSerializationFlags(), txundo, verbosity);
6265

6366
if (!hashBlock.IsNull()) {
6467
LOCK(cs_main);
@@ -166,26 +169,27 @@ static RPCHelpMan getrawtransaction()
166169
{
167170
return RPCHelpMan{
168171
"getrawtransaction",
169-
"Return the raw transaction data.\n"
170172

171-
"\nBy default, this call only returns a transaction if it is in the mempool. If -txindex is enabled\n"
173+
"By default, this call only returns a transaction if it is in the mempool. If -txindex is enabled\n"
172174
"and no blockhash argument is passed, it will return the transaction if it is in the mempool or any block.\n"
173175
"If a blockhash argument is passed, it will return the transaction if\n"
174-
"the specified block is available and the transaction is in that block.\n"
175-
"\nHint: Use gettransaction for wallet transactions.\n"
176+
"the specified block is available and the transaction is in that block.\n\n"
177+
"Hint: Use gettransaction for wallet transactions.\n\n"
176178

177-
"\nIf verbose is 'true', returns an Object with information about 'txid'.\n"
178-
"If verbose is 'false' or omitted, returns a string that is serialized, hex-encoded data for 'txid'.",
179+
"If verbosity is 0 or omitted, returns the serialized transaction as a hex-encoded string.\n"
180+
"If verbosity is 1, returns a JSON Object with information about transaction.\n"
181+
"If verbosity is 2, returns a JSON Object with information about transaction, including fee and prevout information.",
179182
{
180183
{"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id"},
181-
{"verbose", RPCArg::Type::BOOL, RPCArg::Default{false}, "If false, return a string, otherwise return a json object"},
184+
{"verbosity|verbose", RPCArg::Type::NUM, RPCArg::Default{0}, "0 for hex-encoded data, 1 for a JSON object, and 2 for JSON object with fee and prevout"},
182185
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED_NAMED_ARG, "The block in which to look for the transaction"},
183186
},
184187
{
185-
RPCResult{"if verbose is not set or set to false",
186-
RPCResult::Type::STR, "data", "The serialized, hex-encoded data for 'txid'"
188+
RPCResult{"if verbosity is not set or set to 0",
189+
RPCResult::Type::STR, "data", "The serialized transaction as a hex-encoded string for 'txid'"
187190
},
188-
RPCResult{"if verbose is set to true",
191+
RPCResult{"if verbosity is set to 1",
192+
// When updating this documentation, update `decoderawtransaction` in the same way.
189193
RPCResult::Type::OBJ, "", "",
190194
Cat<std::vector<RPCResult>>(
191195
{
@@ -198,20 +202,47 @@ static RPCHelpMan getrawtransaction()
198202
},
199203
DecodeTxDoc(/*txid_field_doc=*/"The transaction id (same as provided)")),
200204
},
205+
RPCResult{"for verbosity = 2",
206+
RPCResult::Type::OBJ, "", "",
207+
{
208+
{RPCResult::Type::ELISION, "", "Same output as verbosity = 1"},
209+
{RPCResult::Type::NUM, "fee", /* optional */ true, "transaction fee in " + CURRENCY_UNIT + ", omitted if block undo data is not available"},
210+
{RPCResult::Type::ARR, "vin", "",
211+
{
212+
{RPCResult::Type::OBJ, "", /* optional */ true, "utxo being spent, omitted if block undo data is not available",
213+
{
214+
{RPCResult::Type::ELISION, "", "Same output as verbosity = 1"},
215+
{RPCResult::Type::OBJ, "prevout", "Only if undo information is available)",
216+
{
217+
{RPCResult::Type::BOOL, "generated", "Coinbase or not"},
218+
{RPCResult::Type::NUM, "height", "The height of the prevout"},
219+
{RPCResult::Type::STR_AMOUNT, "value", "The value in " + CURRENCY_UNIT},
220+
{RPCResult::Type::OBJ, "scriptPubKey", "",
221+
{
222+
{RPCResult::Type::STR, "asm", "Disassembly of the public key script"},
223+
{RPCResult::Type::STR, "desc", "Inferred descriptor for the output"},
224+
{RPCResult::Type::STR_HEX, "hex", "The raw public key script bytes, hex-encoded"},
225+
{RPCResult::Type::STR, "address", /*optional=*/true, "The Bitcoin address (only if a well-defined address exists)"},
226+
{RPCResult::Type::STR, "type", "The type (one of: " + GetAllOutputTypes() + ")"},
227+
}},
228+
}},
229+
}},
230+
}},
231+
}},
201232
},
202233
RPCExamples{
203234
HelpExampleCli("getrawtransaction", "\"mytxid\"")
204-
+ HelpExampleCli("getrawtransaction", "\"mytxid\" true")
205-
+ HelpExampleRpc("getrawtransaction", "\"mytxid\", true")
206-
+ HelpExampleCli("getrawtransaction", "\"mytxid\" false \"myblockhash\"")
207-
+ HelpExampleCli("getrawtransaction", "\"mytxid\" true \"myblockhash\"")
235+
+ HelpExampleCli("getrawtransaction", "\"mytxid\" 1")
236+
+ HelpExampleRpc("getrawtransaction", "\"mytxid\", 1")
237+
+ HelpExampleCli("getrawtransaction", "\"mytxid\" 0 \"myblockhash\"")
238+
+ HelpExampleCli("getrawtransaction", "\"mytxid\" 1 \"myblockhash\"")
239+
+ HelpExampleCli("getrawtransaction", "\"mytxid\" 2 \"myblockhash\"")
208240
},
209241
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
210242
{
211243
const NodeContext& node = EnsureAnyNodeContext(request.context);
212244
ChainstateManager& chainman = EnsureChainman(node);
213245

214-
bool in_active_chain = true;
215246
uint256 hash = ParseHashV(request.params[0], "parameter 1");
216247
const CBlockIndex* blockindex = nullptr;
217248

@@ -220,10 +251,14 @@ static RPCHelpMan getrawtransaction()
220251
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "The genesis block coinbase is not considered an ordinary transaction and cannot be retrieved");
221252
}
222253

223-
// Accept either a bool (true) or a num (>=1) to indicate verbose output.
224-
bool fVerbose = false;
254+
// Accept either a bool (true) or a num (>=0) to indicate verbosity.
255+
int verbosity{0};
225256
if (!request.params[1].isNull()) {
226-
fVerbose = request.params[1].isNum() ? (request.params[1].getInt<int>() != 0) : request.params[1].get_bool();
257+
if (request.params[1].isBool()) {
258+
verbosity = request.params[1].get_bool();
259+
} else {
260+
verbosity = request.params[1].getInt<int>();
261+
}
227262
}
228263

229264
if (!request.params[2].isNull()) {
@@ -234,7 +269,6 @@ static RPCHelpMan getrawtransaction()
234269
if (!blockindex) {
235270
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block hash not found");
236271
}
237-
in_active_chain = chainman.ActiveChain().Contains(blockindex);
238272
}
239273

240274
bool f_txindex_ready = false;
@@ -262,13 +296,43 @@ static RPCHelpMan getrawtransaction()
262296
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, errmsg + ". Use gettransaction for wallet transactions.");
263297
}
264298

265-
if (!fVerbose) {
299+
if (verbosity <= 0) {
266300
return EncodeHexTx(*tx, RPCSerializationFlags());
267301
}
268302

269303
UniValue result(UniValue::VOBJ);
270-
if (blockindex) result.pushKV("in_active_chain", in_active_chain);
271-
TxToJSON(*tx, hash_block, result, chainman.ActiveChainstate());
304+
if (blockindex) {
305+
LOCK(cs_main);
306+
result.pushKV("in_active_chain", chainman.ActiveChain().Contains(blockindex));
307+
}
308+
// If request is verbosity >= 1 but no blockhash was given, then look up the blockindex
309+
if (request.params[2].isNull()) {
310+
LOCK(cs_main);
311+
blockindex = chainman.m_blockman.LookupBlockIndex(hash_block);
312+
}
313+
if (verbosity == 1) {
314+
TxToJSON(*tx, hash_block, result, chainman.ActiveChainstate());
315+
return result;
316+
}
317+
318+
CBlockUndo blockUndo;
319+
CBlock block;
320+
const bool is_block_pruned{WITH_LOCK(cs_main, return chainman.m_blockman.IsBlockPruned(blockindex))};
321+
322+
if (tx->IsCoinBase() ||
323+
!blockindex || is_block_pruned ||
324+
!(UndoReadFromDisk(blockUndo, blockindex) && ReadBlockFromDisk(block, blockindex, Params().GetConsensus()))) {
325+
TxToJSON(*tx, hash_block, result, chainman.ActiveChainstate());
326+
return result;
327+
}
328+
329+
CTxUndo* undoTX {nullptr};
330+
auto it = std::find_if(block.vtx.begin(), block.vtx.end(), [tx](CTransactionRef t){ return *t == *tx; });
331+
if (it != block.vtx.end()) {
332+
// -1 as blockundo does not have coinbase tx
333+
undoTX = &blockUndo.vtxundo.at(it - block.vtx.begin() - 1);
334+
}
335+
TxToJSON(*tx, hash_block, result, chainman.ActiveChainstate(), undoTX, TxVerbosity::SHOW_DETAILS_AND_PREVOUT);
272336
return result;
273337
},
274338
};

test/functional/rpc_rawtransaction.py

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from collections import OrderedDict
1616
from decimal import Decimal
17+
from itertools import product
1718

1819
from test_framework.blocktools import COINBASE_MATURITY
1920
from test_framework.messages import (
@@ -80,6 +81,7 @@ def run_test(self):
8081
self.generate(self.nodes[0], COINBASE_MATURITY + 1)
8182

8283
self.getrawtransaction_tests()
84+
self.getrawtransaction_verbosity_tests()
8385
self.createrawtransaction_tests()
8486
self.sendrawtransaction_tests()
8587
self.sendrawtransaction_testmempoolaccept_tests()
@@ -114,6 +116,7 @@ def getrawtransaction_tests(self):
114116
# 4. valid parameters - supply txid and 1 for verbose.
115117
# We only check the "hex" field of the output so we don't need to update this test every time the output format changes.
116118
assert_equal(self.nodes[n].getrawtransaction(txId, 1)["hex"], tx['hex'])
119+
assert_equal(self.nodes[n].getrawtransaction(txId, 2)["hex"], tx['hex'])
117120

118121
# 5. valid parameters - supply txid and True for non-verbose
119122
assert_equal(self.nodes[n].getrawtransaction(txId, True)["hex"], tx['hex'])
@@ -124,13 +127,14 @@ def getrawtransaction_tests(self):
124127

125128
# 6. invalid parameters - supply txid and invalid boolean values (strings) for verbose
126129
for value in ["True", "False"]:
127-
assert_raises_rpc_error(-3, "not of expected type bool", self.nodes[n].getrawtransaction, txid=txId, verbose=value)
130+
assert_raises_rpc_error(-3, "not of expected type number", self.nodes[n].getrawtransaction, txid=txId, verbose=value)
131+
assert_raises_rpc_error(-3, "not of expected type number", self.nodes[n].getrawtransaction, txid=txId, verbosity=value)
128132

129133
# 7. invalid parameters - supply txid and empty array
130-
assert_raises_rpc_error(-3, "not of expected type bool", self.nodes[n].getrawtransaction, txId, [])
134+
assert_raises_rpc_error(-3, "not of expected type number", self.nodes[n].getrawtransaction, txId, [])
131135

132136
# 8. invalid parameters - supply txid and empty dict
133-
assert_raises_rpc_error(-3, "not of expected type bool", self.nodes[n].getrawtransaction, txId, {})
137+
assert_raises_rpc_error(-3, "not of expected type number", self.nodes[n].getrawtransaction, txId, {})
134138

135139
# Make a tx by sending, then generate 2 blocks; block1 has the tx in it
136140
tx = self.wallet.send_self_transfer(from_node=self.nodes[2])['txid']
@@ -143,9 +147,10 @@ def getrawtransaction_tests(self):
143147
assert_equal(gottx['in_active_chain'], True)
144148
if n == 0:
145149
self.log.info("Test getrawtransaction with -txindex, without blockhash: 'in_active_chain' should be absent")
146-
gottx = self.nodes[n].getrawtransaction(txid=tx, verbose=True)
147-
assert_equal(gottx['txid'], tx)
148-
assert 'in_active_chain' not in gottx
150+
for v in [1,2]:
151+
gottx = self.nodes[n].getrawtransaction(txid=tx, verbosity=v)
152+
assert_equal(gottx['txid'], tx)
153+
assert 'in_active_chain' not in gottx
149154
else:
150155
self.log.info("Test getrawtransaction without -txindex, without blockhash: expect the call to raise")
151156
assert_raises_rpc_error(-5, err_msg, self.nodes[n].getrawtransaction, txid=tx, verbose=True)
@@ -170,6 +175,70 @@ def getrawtransaction_tests(self):
170175
block = self.nodes[0].getblock(self.nodes[0].getblockhash(0))
171176
assert_raises_rpc_error(-5, "The genesis block coinbase is not considered an ordinary transaction", self.nodes[0].getrawtransaction, block['merkleroot'])
172177

178+
def getrawtransaction_verbosity_tests(self):
179+
tx = self.wallet.send_self_transfer(from_node=self.nodes[1])['txid']
180+
[block1] = self.generate(self.nodes[1], 1)
181+
fields = [
182+
'blockhash',
183+
'blocktime',
184+
'confirmations',
185+
'hash',
186+
'hex',
187+
'in_active_chain',
188+
'locktime',
189+
'size',
190+
'time',
191+
'txid',
192+
'vin',
193+
'vout',
194+
'vsize',
195+
'weight',
196+
]
197+
prevout_fields = [
198+
'generated',
199+
'height',
200+
'value',
201+
'scriptPubKey',
202+
]
203+
script_pub_key_fields = [
204+
'address',
205+
'asm',
206+
'hex',
207+
'type',
208+
]
209+
# node 0 & 2 with verbosity 1 & 2
210+
for n, v in product([0, 2], [1, 2]):
211+
self.log.info(f"Test getrawtransaction_verbosity {v} {'with' if n == 0 else 'without'} -txindex, with blockhash")
212+
gottx = self.nodes[n].getrawtransaction(txid=tx, verbosity=v, blockhash=block1)
213+
missing_fields = set(fields).difference(gottx.keys())
214+
if missing_fields:
215+
raise AssertionError(f"fields {', '.join(missing_fields)} are not in transaction")
216+
217+
assert(len(gottx['vin']) > 0)
218+
if v == 1:
219+
assert('fee' not in gottx)
220+
assert('prevout' not in gottx['vin'][0])
221+
if v == 2:
222+
assert(isinstance(gottx['fee'], Decimal))
223+
assert('prevout' in gottx['vin'][0])
224+
prevout = gottx['vin'][0]['prevout']
225+
script_pub_key = prevout['scriptPubKey']
226+
227+
missing_fields = set(prevout_fields).difference(prevout.keys())
228+
if missing_fields:
229+
raise AssertionError(f"fields {', '.join(missing_fields)} are not in transaction")
230+
231+
missing_fields = set(script_pub_key_fields).difference(script_pub_key.keys())
232+
if missing_fields:
233+
raise AssertionError(f"fields {', '.join(missing_fields)} are not in transaction")
234+
235+
# check verbosity 2 without blockhash but with txindex
236+
assert('fee' in self.nodes[0].getrawtransaction(txid=tx, verbosity=2))
237+
# check that coinbase has no fee or does not throw any errors for verbosity 2
238+
coin_base = self.nodes[1].getblock(block1)['tx'][0]
239+
gottx = self.nodes[1].getrawtransaction(txid=coin_base, verbosity=2, blockhash=block1)
240+
assert('fee' not in gottx)
241+
173242
def createrawtransaction_tests(self):
174243
self.log.info("Test createrawtransaction")
175244
# Test `createrawtransaction` required parameters

0 commit comments

Comments
 (0)