Skip to content

Commit 3cc9534

Browse files
fyquahluke-jr0xB10C
authored andcommitted
rpc: Replace boolean argument for tx details with enum class.
Co-authored-by: Luke Dashjr <[email protected]> Co-authored-by: 0xB10C <[email protected]>
1 parent 816e15e commit 3cc9534

File tree

5 files changed

+46
-25
lines changed

5 files changed

+46
-25
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, /*verbose*/ true);
43+
auto univalue = blockToJSON(data.block, &data.blockindex, &data.blockindex, TxVerbosity::SHOW_DETAILS);
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, /*verbose*/ true);
53+
auto univalue = blockToJSON(data.block, &data.blockindex, &data.blockindex, TxVerbosity::SHOW_DETAILS);
5454
bench.run([&] {
5555
auto str = univalue.write();
5656
ankerl::nanobench::doNotOptimizeAway(str);

src/core_io.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ class uint256;
2020
class UniValue;
2121
class CTxUndo;
2222

23+
/**
24+
* Verbose level for block's transaction
25+
*/
26+
enum class TxVerbosity {
27+
SHOW_TXID, //!< Only TXID for each block's transaction
28+
SHOW_DETAILS //!< Include TXID, inputs, outputs, and other common block's transaction information
29+
};
30+
2331
// core_read.cpp
2432
CScript ParseScript(const std::string& s);
2533
std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode = false);

src/rest.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ static bool rest_headers(const std::any& context,
260260
static bool rest_block(const std::any& context,
261261
HTTPRequest* req,
262262
const std::string& strURIPart,
263-
bool showTxDetails)
263+
TxVerbosity tx_verbosity)
264264
{
265265
if (!CheckWarmup(req))
266266
return false;
@@ -312,7 +312,7 @@ static bool rest_block(const std::any& context,
312312
}
313313

314314
case RetFormat::JSON: {
315-
UniValue objBlock = blockToJSON(block, tip, pblockindex, showTxDetails);
315+
UniValue objBlock = blockToJSON(block, tip, pblockindex, tx_verbosity);
316316
std::string strJSON = objBlock.write() + "\n";
317317
req->WriteHeader("Content-Type", "application/json");
318318
req->WriteReply(HTTP_OK, strJSON);
@@ -327,12 +327,12 @@ 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, true);
330+
return rest_block(context, req, strURIPart, TxVerbosity::SHOW_DETAILS);
331331
}
332332

333333
static bool rest_block_notxdetails(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
334334
{
335-
return rest_block(context, req, strURIPart, false);
335+
return rest_block(context, req, strURIPart, TxVerbosity::SHOW_TXID);
336336
}
337337

338338
// A bit of a hack - dependency on a function defined in rpc/blockchain.cpp

src/rpc/blockchain.cpp

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -200,30 +200,36 @@ UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex
200200
return result;
201201
}
202202

203-
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails)
203+
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, TxVerbosity verbosity)
204204
{
205205
UniValue result = blockheaderToJSON(tip, blockindex);
206206

207207
result.pushKV("strippedsize", (int)::GetSerializeSize(block, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS));
208208
result.pushKV("size", (int)::GetSerializeSize(block, PROTOCOL_VERSION));
209209
result.pushKV("weight", (int)::GetBlockWeight(block));
210210
UniValue txs(UniValue::VARR);
211-
if (txDetails) {
212-
CBlockUndo blockUndo;
213-
const bool have_undo = !IsBlockPruned(blockindex) && UndoReadFromDisk(blockUndo, blockindex);
214-
for (size_t i = 0; i < block.vtx.size(); ++i) {
215-
const CTransactionRef& tx = block.vtx.at(i);
216-
// coinbase transaction (i == 0) doesn't have undo data
217-
const CTxUndo* txundo = (have_undo && i) ? &blockUndo.vtxundo.at(i - 1) : nullptr;
218-
UniValue objTx(UniValue::VOBJ);
219-
TxToUniv(*tx, uint256(), objTx, true, RPCSerializationFlags(), txundo);
220-
txs.push_back(objTx);
221-
}
222-
} else {
223-
for (const CTransactionRef& tx : block.vtx) {
224-
txs.push_back(tx->GetHash().GetHex());
225-
}
211+
212+
switch (verbosity) {
213+
case TxVerbosity::SHOW_TXID:
214+
for (const CTransactionRef& tx : block.vtx) {
215+
txs.push_back(tx->GetHash().GetHex());
216+
}
217+
break;
218+
219+
case TxVerbosity::SHOW_DETAILS:
220+
CBlockUndo blockUndo;
221+
const bool have_undo = !IsBlockPruned(blockindex) && UndoReadFromDisk(blockUndo, blockindex);
222+
223+
for (size_t i = 0; i < block.vtx.size(); ++i) {
224+
const CTransactionRef& tx = block.vtx.at(i);
225+
// 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+
UniValue objTx(UniValue::VOBJ);
228+
TxToUniv(*tx, uint256(), objTx, true, RPCSerializationFlags(), txundo);
229+
txs.push_back(objTx);
230+
}
226231
}
232+
227233
result.pushKV("tx", txs);
228234

229235
return result;
@@ -931,7 +937,7 @@ static RPCHelpMan getblock()
931937
return RPCHelpMan{"getblock",
932938
"\nIf verbosity is 0, returns a string that is serialized, hex-encoded data for block 'hash'.\n"
933939
"If verbosity is 1, returns an Object with information about block <hash>.\n"
934-
"If verbosity is 2, returns an Object with information about block <hash> and information about each transaction. \n",
940+
"If verbosity is 2, returns an Object with information about block <hash> and information about each transaction.\n",
935941
{
936942
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The block hash"},
937943
{"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 +1024,14 @@ static RPCHelpMan getblock()
10181024
return strHex;
10191025
}
10201026

1021-
return blockToJSON(block, tip, pblockindex, verbosity >= 2);
1027+
TxVerbosity tx_verbosity;
1028+
if (verbosity == 1) {
1029+
tx_verbosity = TxVerbosity::SHOW_TXID;
1030+
} else {
1031+
tx_verbosity = TxVerbosity::SHOW_DETAILS;
1032+
}
1033+
1034+
return blockToJSON(block, tip, pblockindex, tx_verbosity);
10221035
},
10231036
};
10241037
}

src/rpc/blockchain.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ double GetDifficulty(const CBlockIndex* blockindex);
3838
void RPCNotifyBlockChange(const CBlockIndex*);
3939

4040
/** Block description to JSON */
41-
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails = false) LOCKS_EXCLUDED(cs_main);
41+
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, TxVerbosity verbosity = TxVerbosity::SHOW_TXID) LOCKS_EXCLUDED(cs_main);
4242

4343
/** Mempool information to JSON */
4444
UniValue MempoolInfoToJSON(const CTxMemPool& pool);

0 commit comments

Comments
 (0)