Skip to content

Commit 51dbc16

Browse files
fyquahluke-jr0xB10C
authored andcommitted
rpc: Add level 3 verbosity to getblock RPC call.
Display the prevout in transaction inputs when calling getblock level 3 verbosity. Co-authored-by: Luke Dashjr <[email protected]> Co-authored-by: 0xB10C <[email protected]>
1 parent 3cc9534 commit 51dbc16

File tree

6 files changed

+37
-12
lines changed

6 files changed

+37
-12
lines changed

src/bench/rpc_blockchain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static void BlockToJsonVerbose(benchmark::Bench& bench)
4040
{
4141
TestBlockAndIndex data;
4242
bench.run([&] {
43-
auto univalue = blockToJSON(data.block, &data.blockindex, &data.blockindex, TxVerbosity::SHOW_DETAILS);
43+
auto univalue = blockToJSON(data.block, &data.blockindex, &data.blockindex, TxVerbosity::SHOW_DETAILS_AND_PREVOUT);
4444
ankerl::nanobench::doNotOptimizeAway(univalue);
4545
});
4646
}
@@ -50,7 +50,7 @@ BENCHMARK(BlockToJsonVerbose);
5050
static void BlockToJsonVerboseWrite(benchmark::Bench& bench)
5151
{
5252
TestBlockAndIndex data;
53-
auto univalue = blockToJSON(data.block, &data.blockindex, &data.blockindex, TxVerbosity::SHOW_DETAILS);
53+
auto univalue = blockToJSON(data.block, &data.blockindex, &data.blockindex, TxVerbosity::SHOW_DETAILS_AND_PREVOUT);
5454
bench.run([&] {
5555
auto str = univalue.write();
5656
ankerl::nanobench::doNotOptimizeAway(str);

src/core_io.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class CTxUndo;
2525
*/
2626
enum class TxVerbosity {
2727
SHOW_TXID, //!< Only TXID for each block's transaction
28-
SHOW_DETAILS //!< Include TXID, inputs, outputs, and other common block's transaction information
28+
SHOW_DETAILS, //!< Include TXID, inputs, outputs, and other common block's transaction information
29+
SHOW_DETAILS_AND_PREVOUT //!< The same as previous option with information about prevouts if available
2930
};
3031

3132
// core_read.cpp
@@ -54,6 +55,6 @@ std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags = 0);
5455
std::string SighashToStr(unsigned char sighash_type);
5556
void ScriptPubKeyToUniv(const CScript& scriptPubKey, UniValue& out, bool include_hex, bool include_address = true);
5657
void ScriptToUniv(const CScript& script, UniValue& out);
57-
void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, bool include_hex = true, int serialize_flags = 0, const CTxUndo* txundo = nullptr);
58+
void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, bool include_hex = true, int serialize_flags = 0, const CTxUndo* txundo = nullptr, TxVerbosity verbosity = TxVerbosity::SHOW_DETAILS);
5859

5960
#endif // BITCOIN_CORE_IO_H

src/core_write.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ void ScriptPubKeyToUniv(const CScript& scriptPubKey, UniValue& out, bool include
163163
out.pushKV("type", GetTxnOutputType(type));
164164
}
165165

166-
void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, bool include_hex, int serialize_flags, const CTxUndo* txundo)
166+
void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, bool include_hex, int serialize_flags, const CTxUndo* txundo, TxVerbosity verbosity)
167167
{
168168
entry.pushKV("txid", tx.GetHash().GetHex());
169169
entry.pushKV("hash", tx.GetWitnessHash().GetHex());
@@ -204,8 +204,27 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry,
204204
in.pushKV("txinwitness", txinwitness);
205205
}
206206
if (calculate_fee) {
207-
const CTxOut& prev_txout = txundo->vprevout[i].out;
207+
const Coin& prev_coin = txundo->vprevout[i];
208+
const CTxOut& prev_txout = prev_coin.out;
209+
208210
amt_total_in += prev_txout.nValue;
211+
switch (verbosity) {
212+
case TxVerbosity::SHOW_TXID:
213+
case TxVerbosity::SHOW_DETAILS:
214+
break;
215+
216+
case TxVerbosity::SHOW_DETAILS_AND_PREVOUT:
217+
UniValue o_script_pub_key(UniValue::VOBJ);
218+
ScriptPubKeyToUniv(prev_txout.scriptPubKey, o_script_pub_key, /* includeHex */ true);
219+
220+
UniValue p(UniValue::VOBJ);
221+
p.pushKV("generated", bool(prev_coin.fCoinBase));
222+
p.pushKV("height", uint64_t(prev_coin.nHeight));
223+
p.pushKV("value", ValueFromAmount(prev_txout.nValue));
224+
p.pushKV("scriptPubKey", o_script_pub_key);
225+
in.pushKV("prevout", p);
226+
break;
227+
}
209228
}
210229
in.pushKV("sequence", (int64_t)txin.nSequence);
211230
vin.push_back(in);

src/rest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ static bool rest_block(const std::any& context,
327327

328328
static bool rest_block_extended(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
329329
{
330-
return rest_block(context, req, strURIPart, TxVerbosity::SHOW_DETAILS);
330+
return rest_block(context, req, strURIPart, TxVerbosity::SHOW_DETAILS_AND_PREVOUT);
331331
}
332332

333333
static bool rest_block_notxdetails(const std::any& context, HTTPRequest* req, const std::string& strURIPart)

src/rpc/blockchain.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,16 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIn
217217
break;
218218

219219
case TxVerbosity::SHOW_DETAILS:
220+
case TxVerbosity::SHOW_DETAILS_AND_PREVOUT:
220221
CBlockUndo blockUndo;
221222
const bool have_undo = !IsBlockPruned(blockindex) && UndoReadFromDisk(blockUndo, blockindex);
222223

223224
for (size_t i = 0; i < block.vtx.size(); ++i) {
224225
const CTransactionRef& tx = block.vtx.at(i);
225226
// coinbase transaction (i.e. i == 0) doesn't have undo data
226-
const CTxUndo* txundo = (have_undo && i) ? &blockUndo.vtxundo.at(i - 1) : nullptr;
227+
const CTxUndo* txundo = (have_undo && i > 0) ? &blockUndo.vtxundo.at(i - 1) : nullptr;
227228
UniValue objTx(UniValue::VOBJ);
228-
TxToUniv(*tx, uint256(), objTx, true, RPCSerializationFlags(), txundo);
229+
TxToUniv(*tx, uint256(), objTx, true, RPCSerializationFlags(), txundo, verbosity);
229230
txs.push_back(objTx);
230231
}
231232
}
@@ -937,7 +938,8 @@ static RPCHelpMan getblock()
937938
return RPCHelpMan{"getblock",
938939
"\nIf verbosity is 0, returns a string that is serialized, hex-encoded data for block 'hash'.\n"
939940
"If verbosity is 1, returns an Object with information about block <hash>.\n"
940-
"If verbosity is 2, returns an Object with information about block <hash> and information about each transaction.\n",
941+
"If verbosity is 2, returns an Object with information about block <hash> and information about each transaction.\n"
942+
"If verbosity is 3, returns an Object with information about block <hash> and information about each transaction, including prevout information for inputs (only for unpruned blocks in the current best chain).\n",
941943
{
942944
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The block hash"},
943945
{"verbosity|verbose", RPCArg::Type::NUM, RPCArg::Default{1}, "0 for hex-encoded data, 1 for a json object, and 2 for json object with transaction data"},
@@ -1027,8 +1029,10 @@ static RPCHelpMan getblock()
10271029
TxVerbosity tx_verbosity;
10281030
if (verbosity == 1) {
10291031
tx_verbosity = TxVerbosity::SHOW_TXID;
1030-
} else {
1032+
} else if (verbosity == 2) {
10311033
tx_verbosity = TxVerbosity::SHOW_DETAILS;
1034+
} else {
1035+
tx_verbosity = TxVerbosity::SHOW_DETAILS_AND_PREVOUT;
10321036
}
10331037

10341038
return blockToJSON(block, tip, pblockindex, tx_verbosity);

src/rpc/blockchain.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define BITCOIN_RPC_BLOCKCHAIN_H
77

88
#include <consensus/amount.h>
9+
#include <core_io.h>
910
#include <streams.h>
1011
#include <sync.h>
1112

@@ -38,7 +39,7 @@ double GetDifficulty(const CBlockIndex* blockindex);
3839
void RPCNotifyBlockChange(const CBlockIndex*);
3940

4041
/** Block description to JSON */
41-
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, TxVerbosity verbosity = TxVerbosity::SHOW_TXID) LOCKS_EXCLUDED(cs_main);
42+
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, TxVerbosity verbosity) LOCKS_EXCLUDED(cs_main);
4243

4344
/** Mempool information to JSON */
4445
UniValue MempoolInfoToJSON(const CTxMemPool& pool);

0 commit comments

Comments
 (0)