Merge bitcoin/bitcoin#34524: refactor: [rpc] Remove confusing and brittle integral casts (take 2)

fa6801366d76a34f51a7d60be7d3710ed8e722db refactor: [rpc] Remove confusing and brittle integral casts (take 2) (MarcoFalke)

Pull request description:

  Second take for cases which I seem to have missed in commit 94ddc2dced5736612e358a3b80f2bc718fbd8161.

  When constructing an UniValue from integral values, historically (long ago), in some cases casts where needed. With the current UniValue constructor, only very few are actually needed.

  Keeping the unused casts around is:

  * confusing, because code readers do not understand why they are needed
  * brittle, because some may copy them into new places, where they will lead to hard-to-find logic bugs, such as the ones fixed in pull https://github.com/bitcoin/bitcoin/pull/34112

  So fix all issues by removing them, except for a few cases, where casting was required:
  * `static_cast<bool>(fCoinBase)`, because `bool{fCoinBase}` only works on modern compilers.

  This hardening refactor does not fix any bugs and does not change any behavior.

ACKs for top commit:
  Sjors:
    ACK fa6801366d76a34f51a7d60be7d3710ed8e722db
  sedited:
    ACK fa6801366d76a34f51a7d60be7d3710ed8e722db

Tree-SHA512: 77f03f496ea2a42987720cb4a36eb4e7a0d5b512ed7f029e41dd54c04fb4c1680f7cc13514acbc9f1bae991be4de3cf31c5a0d27c016a030f5749d132603f71e
This commit is contained in:
merge-script 2026-02-09 13:32:28 +00:00
commit 44afed4cd9
No known key found for this signature in database
GPG Key ID: 2EEB9F5CC09526C1
10 changed files with 24 additions and 24 deletions

View File

@ -437,7 +437,7 @@ void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry
entry.pushKV("size", tx.ComputeTotalSize());
entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR);
entry.pushKV("weight", GetTransactionWeight(tx));
entry.pushKV("locktime", (int64_t)tx.nLockTime);
entry.pushKV("locktime", tx.nLockTime);
UniValue vin{UniValue::VARR};
vin.reserve(tx.vin.size());
@ -455,7 +455,7 @@ void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry
in.pushKV("coinbase", HexStr(txin.scriptSig));
} else {
in.pushKV("txid", txin.prevout.hash.GetHex());
in.pushKV("vout", (int64_t)txin.prevout.n);
in.pushKV("vout", txin.prevout.n);
UniValue o(UniValue::VOBJ);
o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true));
o.pushKV("hex", HexStr(txin.scriptSig));
@ -480,14 +480,14 @@ void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry
ScriptToUniv(prev_txout.scriptPubKey, /*out=*/o_script_pub_key, /*include_hex=*/true, /*include_address=*/true);
UniValue p(UniValue::VOBJ);
p.pushKV("generated", bool(prev_coin.fCoinBase));
p.pushKV("height", uint64_t(prev_coin.nHeight));
p.pushKV("generated", static_cast<bool>(prev_coin.fCoinBase));
p.pushKV("height", prev_coin.nHeight);
p.pushKV("value", ValueFromAmount(prev_txout.nValue));
p.pushKV("scriptPubKey", std::move(o_script_pub_key));
in.pushKV("prevout", std::move(p));
}
}
in.pushKV("sequence", (int64_t)txin.nSequence);
in.pushKV("sequence", txin.nSequence);
vin.push_back(std::move(in));
}
entry.pushKV("vin", std::move(vin));
@ -500,7 +500,7 @@ void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry
UniValue out(UniValue::VOBJ);
out.pushKV("value", ValueFromAmount(txout.nValue));
out.pushKV("n", (int64_t)i);
out.pushKV("n", i);
UniValue o(UniValue::VOBJ);
ScriptToUniv(txout.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);

View File

@ -1064,7 +1064,7 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
UniValue utxos(UniValue::VARR);
for (const CCoin& coin : outs) {
UniValue utxo(UniValue::VOBJ);
utxo.pushKV("height", (int32_t)coin.nHeight);
utxo.pushKV("height", coin.nHeight);
utxo.pushKV("value", ValueFromAmount(coin.out.nValue));
// include the script in a json output

View File

@ -1096,7 +1096,7 @@ static RPCHelpMan gettxoutsetinfo()
CHECK_NONFATAL(stats.total_amount.has_value());
ret.pushKV("total_amount", ValueFromAmount(stats.total_amount.value()));
if (!stats.index_used) {
ret.pushKV("transactions", static_cast<int64_t>(stats.nTransactions));
ret.pushKV("transactions", stats.nTransactions);
ret.pushKV("disk_size", stats.nDiskSize);
} else {
CCoinsStats prev_stats{};

View File

@ -983,7 +983,7 @@ static RPCHelpMan getblocktemplate()
result.pushKV("version", block.nVersion);
result.pushKV("rules", std::move(aRules));
result.pushKV("vbavailable", std::move(vbavailable));
result.pushKV("vbrequired", int(0));
result.pushKV("vbrequired", 0);
result.pushKV("previousblockhash", block.hashPrevBlock.GetHex());
result.pushKV("transactions", std::move(transactions));

View File

@ -235,7 +235,7 @@ static RPCHelpMan getpeerinfo()
}
obj.pushKV("network", GetNetworkName(stats.m_network));
if (stats.m_mapped_as != 0) {
obj.pushKV("mapped_as", uint64_t(stats.m_mapped_as));
obj.pushKV("mapped_as", stats.m_mapped_as);
}
ServiceFlags services{statestats.their_services};
obj.pushKV("services", strprintf("%016x", services));
@ -958,7 +958,7 @@ static RPCHelpMan getnodeaddresses()
for (const CAddress& addr : vAddr) {
UniValue obj(UniValue::VOBJ);
obj.pushKV("time", int64_t{TicksSinceEpoch<std::chrono::seconds>(addr.nTime)});
obj.pushKV("time", TicksSinceEpoch<std::chrono::seconds>(addr.nTime));
obj.pushKV("services", static_cast<std::underlying_type_t<decltype(addr.nServices)>>(addr.nServices));
obj.pushKV("address", addr.ToStringAddr());
obj.pushKV("port", addr.GetPort());
@ -1127,7 +1127,7 @@ UniValue AddrmanEntryToJSON(const AddrInfo& info, const CConnman& connman)
}
ret.pushKV("port", info.GetPort());
ret.pushKV("services", static_cast<std::underlying_type_t<decltype(info.nServices)>>(info.nServices));
ret.pushKV("time", int64_t{TicksSinceEpoch<std::chrono::seconds>(info.nTime)});
ret.pushKV("time", TicksSinceEpoch<std::chrono::seconds>(info.nTime));
ret.pushKV("network", GetNetworkName(info.GetNetClass()));
ret.pushKV("source", info.source.ToStringAddr());
ret.pushKV("source_network", GetNetworkName(info.source.GetNetClass()));

View File

@ -113,12 +113,12 @@ static UniValue RPCLockedMemoryInfo()
{
LockedPool::Stats stats = LockedPoolManager::Instance().stats();
UniValue obj(UniValue::VOBJ);
obj.pushKV("used", uint64_t(stats.used));
obj.pushKV("free", uint64_t(stats.free));
obj.pushKV("total", uint64_t(stats.total));
obj.pushKV("locked", uint64_t(stats.locked));
obj.pushKV("chunks_used", uint64_t(stats.chunks_used));
obj.pushKV("chunks_free", uint64_t(stats.chunks_free));
obj.pushKV("used", stats.used);
obj.pushKV("free", stats.free);
obj.pushKV("total", stats.total);
obj.pushKV("locked", stats.locked);
obj.pushKV("chunks_used", stats.chunks_used);
obj.pushKV("chunks_free", stats.chunks_free);
return obj;
}

View File

@ -1092,7 +1092,7 @@ static RPCHelpMan decodepsbt()
result.pushKV("global_xpubs", std::move(global_xpubs));
// PSBT version
result.pushKV("psbt_version", static_cast<uint64_t>(psbtx.GetVersion()));
result.pushKV("psbt_version", psbtx.GetVersion());
// Proprietary
UniValue proprietary(UniValue::VARR);

View File

@ -219,7 +219,7 @@ static RPCHelpMan getrpcinfo()
for (const RPCCommandExecutionInfo& info : g_rpc_server_info.active_commands) {
UniValue entry(UniValue::VOBJ);
entry.pushKV("method", info.method);
entry.pushKV("duration", int64_t{Ticks<std::chrono::microseconds>(SteadyClock::now() - info.start)});
entry.pushKV("duration", Ticks<std::chrono::microseconds>(SteadyClock::now() - info.start));
active_commands.push_back(std::move(entry));
}

View File

@ -667,9 +667,9 @@ RPCHelpMan listunspent()
CAmount ancestor_fees;
pwallet->chain().getTransactionAncestry(out.outpoint.hash, ancestor_count, descendant_count, &ancestor_size, &ancestor_fees);
if (ancestor_count) {
entry.pushKV("ancestorcount", uint64_t(ancestor_count));
entry.pushKV("ancestorsize", uint64_t(ancestor_size));
entry.pushKV("ancestorfees", uint64_t(ancestor_fees));
entry.pushKV("ancestorcount", ancestor_count);
entry.pushKV("ancestorsize", ancestor_size);
entry.pushKV("ancestorfees", ancestor_fees);
}
}
entry.pushKV("spendable", true); // Any coins we list are always spendable

View File

@ -47,7 +47,7 @@ static void WalletTxToJSON(const CWallet& wallet, const CWalletTx& wtx, UniValue
mempool_conflicts.push_back(mempool_conflict.GetHex());
entry.pushKV("mempoolconflicts", std::move(mempool_conflicts));
entry.pushKV("time", wtx.GetTxTime());
entry.pushKV("timereceived", int64_t{wtx.nTimeReceived});
entry.pushKV("timereceived", wtx.nTimeReceived);
// Add opt-in RBF status
std::string rbfStatus = "no";