New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rpc: Add level 3 verbosity to getblock RPC call (#21245 modified) #22918
Changes from 5 commits
3cc9534
51dbc16
4330af6
459104b
8edf620
5c34507
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -163,7 +163,7 @@ void ScriptPubKeyToUniv(const CScript& scriptPubKey, UniValue& out, bool include | |
out.pushKV("type", GetTxnOutputType(type)); | ||
} | ||
|
||
void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, bool include_hex, int serialize_flags, const CTxUndo* txundo) | ||
void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, bool include_hex, int serialize_flags, const CTxUndo* txundo, TxVerbosity verbosity) | ||
{ | ||
entry.pushKV("txid", tx.GetHash().GetHex()); | ||
entry.pushKV("hash", tx.GetWitnessHash().GetHex()); | ||
|
@@ -204,8 +204,27 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, | |
in.pushKV("txinwitness", txinwitness); | ||
} | ||
if (calculate_fee) { | ||
const CTxOut& prev_txout = txundo->vprevout[i].out; | ||
const Coin& prev_coin = txundo->vprevout[i]; | ||
const CTxOut& prev_txout = prev_coin.out; | ||
|
||
amt_total_in += prev_txout.nValue; | ||
switch (verbosity) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit, we only care about one value, what's wrong with if (verbosity == TxVerbosity::SHOW_DETAILS_AND_PREVOUT) { If you do this, then declare There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you. I would like to address that a next rebase or in a follow-up PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have actually proposed the same in the original PR: #21245 (comment) |
||
case TxVerbosity::SHOW_TXID: | ||
case TxVerbosity::SHOW_DETAILS: | ||
break; | ||
|
||
case TxVerbosity::SHOW_DETAILS_AND_PREVOUT: | ||
UniValue o_script_pub_key(UniValue::VOBJ); | ||
ScriptPubKeyToUniv(prev_txout.scriptPubKey, o_script_pub_key, /* includeHex */ true); | ||
|
||
UniValue p(UniValue::VOBJ); | ||
p.pushKV("generated", bool(prev_coin.fCoinBase)); | ||
p.pushKV("height", uint64_t(prev_coin.nHeight)); | ||
p.pushKV("value", ValueFromAmount(prev_txout.nValue)); | ||
p.pushKV("scriptPubKey", o_script_pub_key); | ||
in.pushKV("prevout", p); | ||
break; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per } // no default case, so the compiler can warn about missing cases
assert(false);
} (usually, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you. I would like to address that a next rebase or in a follow-up PR. |
||
} | ||
in.pushKV("sequence", (int64_t)txin.nSequence); | ||
vin.push_back(in); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -200,30 +200,37 @@ UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex | |
return result; | ||
} | ||
|
||
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails) | ||
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, TxVerbosity verbosity) | ||
{ | ||
UniValue result = blockheaderToJSON(tip, blockindex); | ||
|
||
result.pushKV("strippedsize", (int)::GetSerializeSize(block, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS)); | ||
result.pushKV("size", (int)::GetSerializeSize(block, PROTOCOL_VERSION)); | ||
result.pushKV("weight", (int)::GetBlockWeight(block)); | ||
UniValue txs(UniValue::VARR); | ||
if (txDetails) { | ||
CBlockUndo blockUndo; | ||
const bool have_undo = !IsBlockPruned(blockindex) && UndoReadFromDisk(blockUndo, blockindex); | ||
for (size_t i = 0; i < block.vtx.size(); ++i) { | ||
const CTransactionRef& tx = block.vtx.at(i); | ||
// coinbase transaction (i == 0) doesn't have undo data | ||
const CTxUndo* txundo = (have_undo && i) ? &blockUndo.vtxundo.at(i - 1) : nullptr; | ||
UniValue objTx(UniValue::VOBJ); | ||
TxToUniv(*tx, uint256(), objTx, true, RPCSerializationFlags(), txundo); | ||
txs.push_back(objTx); | ||
} | ||
} else { | ||
for (const CTransactionRef& tx : block.vtx) { | ||
txs.push_back(tx->GetHash().GetHex()); | ||
} | ||
|
||
switch (verbosity) { | ||
case TxVerbosity::SHOW_TXID: | ||
for (const CTransactionRef& tx : block.vtx) { | ||
txs.push_back(tx->GetHash().GetHex()); | ||
} | ||
break; | ||
|
||
case TxVerbosity::SHOW_DETAILS: | ||
case TxVerbosity::SHOW_DETAILS_AND_PREVOUT: | ||
CBlockUndo blockUndo; | ||
const bool have_undo = !IsBlockPruned(blockindex) && UndoReadFromDisk(blockUndo, blockindex); | ||
|
||
for (size_t i = 0; i < block.vtx.size(); ++i) { | ||
const CTransactionRef& tx = block.vtx.at(i); | ||
// coinbase transaction (i.e. i == 0) doesn't have undo data | ||
const CTxUndo* txundo = (have_undo && i > 0) ? &blockUndo.vtxundo.at(i - 1) : nullptr; | ||
UniValue objTx(UniValue::VOBJ); | ||
TxToUniv(*tx, uint256(), objTx, true, RPCSerializationFlags(), txundo, verbosity); | ||
txs.push_back(objTx); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit, add |
||
} | ||
|
||
result.pushKV("tx", txs); | ||
|
||
return result; | ||
|
@@ -931,7 +938,8 @@ static RPCHelpMan getblock() | |
return RPCHelpMan{"getblock", | ||
"\nIf verbosity is 0, returns a string that is serialized, hex-encoded data for block 'hash'.\n" | ||
"If verbosity is 1, returns an Object with information about block <hash>.\n" | ||
"If verbosity is 2, returns an Object with information about block <hash> and information about each transaction. \n", | ||
"If verbosity is 2, returns an Object with information about block <hash> and information about each transaction.\n" | ||
"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", | ||
{ | ||
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The block hash"}, | ||
{"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"}, | ||
|
@@ -1018,7 +1026,16 @@ static RPCHelpMan getblock() | |
return strHex; | ||
} | ||
|
||
return blockToJSON(block, tip, pblockindex, verbosity >= 2); | ||
TxVerbosity tx_verbosity; | ||
if (verbosity == 1) { | ||
tx_verbosity = TxVerbosity::SHOW_TXID; | ||
} else if (verbosity == 2) { | ||
tx_verbosity = TxVerbosity::SHOW_DETAILS; | ||
} else { | ||
tx_verbosity = TxVerbosity::SHOW_DETAILS_AND_PREVOUT; | ||
} | ||
|
||
return blockToJSON(block, tip, pblockindex, tx_verbosity); | ||
}, | ||
}; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
verbose
->verbosity
andinputs
->inputs'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you. I will fix in a next rebase or in a follow-up PR.