Skip to content

Commit a2e1590

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#24673: refactor: followup of remove -deprecatedrpc=addresses flag
9563a64 refactor: add stdd:: includes to core_write (fanquake) 8b9efeb refactor: use named args when ScriptToUniv or TxToUniv are invoked (Michael Dietz) 22f25a6 refactor: prefer snake case, TxToUniv arg hashBlock renamed block_hash (Michael Dietz) 828a094 refactor: merge ScriptPubKeyToUniv & ScriptToUniv into one function (Michael Dietz) Pull request description: I've cherry-picked some of the commits out of #22924, and made minor changes (like fixing named args). ACKs for top commit: MarcoFalke: re-ACK 9563a64 🕓 Tree-SHA512: 4f0e5b45c14cbf68b9e389bbe1211c125d95cbd3da5205b1cff6a4c44f15b15039ba2a5b25cd7e2580d9169404f1b7ff620d8a7e01f6112e3cb153ecfaef8916
2 parents d2b4355 + 9563a64 commit a2e1590

File tree

9 files changed

+40
-41
lines changed

9 files changed

+40
-41
lines changed

src/bitcoin-tx.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ static void MutateTx(CMutableTransaction& tx, const std::string& command,
750750
static void OutputTxJSON(const CTransaction& tx)
751751
{
752752
UniValue entry(UniValue::VOBJ);
753-
TxToUniv(tx, uint256(), entry);
753+
TxToUniv(tx, /*block_hash=*/uint256(), entry);
754754

755755
std::string jsonOutput = entry.write(4);
756756
tfm::format(std::cout, "%s\n", jsonOutput);

src/core_io.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ UniValue ValueFromAmount(const CAmount amount);
5353
std::string FormatScript(const CScript& script);
5454
std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags = 0);
5555
std::string SighashToStr(unsigned char sighash_type);
56-
void ScriptPubKeyToUniv(const CScript& scriptPubKey, UniValue& out, bool include_hex, bool include_address = true);
57-
void ScriptToUniv(const CScript& script, UniValue& out);
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);
56+
void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex = true, bool include_address = false);
57+
void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry, bool include_hex = true, int serialize_flags = 0, const CTxUndo* txundo = nullptr, TxVerbosity verbosity = TxVerbosity::SHOW_DETAILS);
5958

6059
#endif // BITCOIN_CORE_IO_H

src/core_write.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#include <util/strencodings.h>
2020
#include <util/system.h>
2121

22+
#include <map>
23+
#include <string>
24+
#include <vector>
25+
2226
UniValue ValueFromAmount(const CAmount amount)
2327
{
2428
static_assert(COIN > 1);
@@ -143,31 +147,28 @@ std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags)
143147
return HexStr(ssTx);
144148
}
145149

146-
void ScriptToUniv(const CScript& script, UniValue& out)
147-
{
148-
ScriptPubKeyToUniv(script, out, /* include_hex */ true, /* include_address */ false);
149-
}
150-
151-
void ScriptPubKeyToUniv(const CScript& scriptPubKey, UniValue& out, bool include_hex, bool include_address)
150+
void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex, bool include_address)
152151
{
153152
CTxDestination address;
154153

155-
out.pushKV("asm", ScriptToAsmStr(scriptPubKey));
154+
out.pushKV("asm", ScriptToAsmStr(script));
156155
if (include_address) {
157-
out.pushKV("desc", InferDescriptor(scriptPubKey, DUMMY_SIGNING_PROVIDER)->ToString());
156+
out.pushKV("desc", InferDescriptor(script, DUMMY_SIGNING_PROVIDER)->ToString());
157+
}
158+
if (include_hex) {
159+
out.pushKV("hex", HexStr(script));
158160
}
159-
if (include_hex) out.pushKV("hex", HexStr(scriptPubKey));
160161

161162
std::vector<std::vector<unsigned char>> solns;
162-
const TxoutType type{Solver(scriptPubKey, solns)};
163+
const TxoutType type{Solver(script, solns)};
163164

164-
if (include_address && ExtractDestination(scriptPubKey, address) && type != TxoutType::PUBKEY) {
165+
if (include_address && ExtractDestination(script, address) && type != TxoutType::PUBKEY) {
165166
out.pushKV("address", EncodeDestination(address));
166167
}
167168
out.pushKV("type", GetTxnOutputType(type));
168169
}
169170

170-
void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, bool include_hex, int serialize_flags, const CTxUndo* txundo, TxVerbosity verbosity)
171+
void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry, bool include_hex, int serialize_flags, const CTxUndo* txundo, TxVerbosity verbosity)
171172
{
172173
entry.pushKV("txid", tx.GetHash().GetHex());
173174
entry.pushKV("hash", tx.GetWitnessHash().GetHex());
@@ -215,7 +216,7 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry,
215216

216217
if (verbosity == TxVerbosity::SHOW_DETAILS_AND_PREVOUT) {
217218
UniValue o_script_pub_key(UniValue::VOBJ);
218-
ScriptPubKeyToUniv(prev_txout.scriptPubKey, o_script_pub_key, /*include_hex=*/ true);
219+
ScriptToUniv(prev_txout.scriptPubKey, /*out=*/o_script_pub_key, /*include_hex=*/true, /*include_address=*/true);
219220

220221
UniValue p(UniValue::VOBJ);
221222
p.pushKV("generated", bool(prev_coin.fCoinBase));
@@ -240,7 +241,7 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry,
240241
out.pushKV("n", (int64_t)i);
241242

242243
UniValue o(UniValue::VOBJ);
243-
ScriptPubKeyToUniv(txout.scriptPubKey, o, true);
244+
ScriptToUniv(txout.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);
244245
out.pushKV("scriptPubKey", o);
245246
vout.push_back(out);
246247

@@ -256,8 +257,9 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry,
256257
entry.pushKV("fee", ValueFromAmount(fee));
257258
}
258259

259-
if (!hashBlock.IsNull())
260-
entry.pushKV("blockhash", hashBlock.GetHex());
260+
if (!block_hash.IsNull()) {
261+
entry.pushKV("blockhash", block_hash.GetHex());
262+
}
261263

262264
if (include_hex) {
263265
entry.pushKV("hex", EncodeHexTx(tx, serialize_flags)); // The hex-encoded transaction. Used the name "hex" to be consistent with the verbose output of "getrawtransaction".

src/rest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ static bool rest_tx(const std::any& context, HTTPRequest* req, const std::string
670670

671671
case RetFormat::JSON: {
672672
UniValue objTx(UniValue::VOBJ);
673-
TxToUniv(*tx, hashBlock, objTx);
673+
TxToUniv(*tx, /*block_hash=*/hashBlock, /*entry=*/ objTx);
674674
std::string strJSON = objTx.write() + "\n";
675675
req->WriteHeader("Content-Type", "application/json");
676676
req->WriteReply(HTTP_OK, strJSON);
@@ -855,7 +855,7 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
855855

856856
// include the script in a json output
857857
UniValue o(UniValue::VOBJ);
858-
ScriptPubKeyToUniv(coin.out.scriptPubKey, o, true);
858+
ScriptToUniv(coin.out.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);
859859
utxo.pushKV("scriptPubKey", o);
860860
utxos.push_back(utxo);
861861
}

src/rpc/blockchain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIn
187187
// coinbase transaction (i.e. i == 0) doesn't have undo data
188188
const CTxUndo* txundo = (have_undo && i > 0) ? &blockUndo.vtxundo.at(i - 1) : nullptr;
189189
UniValue objTx(UniValue::VOBJ);
190-
TxToUniv(*tx, uint256(), objTx, true, RPCSerializationFlags(), txundo, verbosity);
190+
TxToUniv(*tx, /*block_hash=*/uint256(), /*entry=*/objTx, /*include_hex=*/true, RPCSerializationFlags(), txundo, verbosity);
191191
txs.push_back(objTx);
192192
}
193193
break;
@@ -1026,7 +1026,7 @@ static RPCHelpMan gettxout()
10261026
}
10271027
ret.pushKV("value", ValueFromAmount(coin.out.nValue));
10281028
UniValue o(UniValue::VOBJ);
1029-
ScriptPubKeyToUniv(coin.out.scriptPubKey, o, true);
1029+
ScriptToUniv(coin.out.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);
10301030
ret.pushKV("scriptPubKey", o);
10311031
ret.pushKV("coinbase", (bool)coin.fCoinBase);
10321032

src/rpc/rawtransaction.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue&
5858
// Blockchain contextual information (confirmations and blocktime) is not
5959
// available to code in bitcoin-common, so we query them here and push the
6060
// data into the returned UniValue.
61-
TxToUniv(tx, uint256(), entry, true, RPCSerializationFlags());
61+
TxToUniv(tx, /*block_hash=*/uint256(), entry, /*include_hex=*/true, RPCSerializationFlags());
6262

6363
if (!hashBlock.IsNull()) {
6464
LOCK(cs_main);
@@ -383,7 +383,7 @@ static RPCHelpMan decoderawtransaction()
383383
}
384384

385385
UniValue result(UniValue::VOBJ);
386-
TxToUniv(CTransaction(std::move(mtx)), uint256(), result, false);
386+
TxToUniv(CTransaction(std::move(mtx)), /*block_hash=*/uint256(), /*entry=*/result, /*include_hex=*/false);
387387

388388
return result;
389389
},
@@ -435,7 +435,7 @@ static RPCHelpMan decodescript()
435435
} else {
436436
// Empty scripts are valid
437437
}
438-
ScriptPubKeyToUniv(script, r, /* include_hex */ false);
438+
ScriptToUniv(script, /*out=*/r, /*include_hex=*/false, /*include_address=*/true);
439439

440440
std::vector<std::vector<unsigned char>> solutions_data;
441441
const TxoutType which_type{Solver(script, solutions_data)};
@@ -512,7 +512,7 @@ static RPCHelpMan decodescript()
512512
// Scripts that are not fit for P2WPKH are encoded as P2WSH.
513513
segwitScr = GetScriptForDestination(WitnessV0ScriptHash(script));
514514
}
515-
ScriptPubKeyToUniv(segwitScr, sr, /* include_hex */ true);
515+
ScriptToUniv(segwitScr, /*out=*/sr, /*include_hex=*/true, /*include_address=*/true);
516516
sr.pushKV("p2sh-segwit", EncodeDestination(ScriptHash(segwitScr)));
517517
r.pushKV("segwit", sr);
518518
}
@@ -900,7 +900,7 @@ static RPCHelpMan decodepsbt()
900900

901901
// Add the decoded tx
902902
UniValue tx_univ(UniValue::VOBJ);
903-
TxToUniv(CTransaction(*psbtx.tx), uint256(), tx_univ, false);
903+
TxToUniv(CTransaction(*psbtx.tx), /*block_hash=*/uint256(), /*entry=*/tx_univ, /*include_hex=*/false);
904904
result.pushKV("tx", tx_univ);
905905

906906
// Add the global xpubs
@@ -956,7 +956,7 @@ static RPCHelpMan decodepsbt()
956956
txout = input.witness_utxo;
957957

958958
UniValue o(UniValue::VOBJ);
959-
ScriptPubKeyToUniv(txout.scriptPubKey, o, /* include_hex */ true);
959+
ScriptToUniv(txout.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);
960960

961961
UniValue out(UniValue::VOBJ);
962962
out.pushKV("amount", ValueFromAmount(txout.nValue));
@@ -970,7 +970,7 @@ static RPCHelpMan decodepsbt()
970970
txout = input.non_witness_utxo->vout[psbtx.tx->vin[i].prevout.n];
971971

972972
UniValue non_wit(UniValue::VOBJ);
973-
TxToUniv(*input.non_witness_utxo, uint256(), non_wit, false);
973+
TxToUniv(*input.non_witness_utxo, /*block_hash=*/uint256(), /*entry=*/non_wit, /*include_hex=*/false);
974974
in.pushKV("non_witness_utxo", non_wit);
975975

976976
have_a_utxo = true;
@@ -1003,12 +1003,12 @@ static RPCHelpMan decodepsbt()
10031003
// Redeem script and witness script
10041004
if (!input.redeem_script.empty()) {
10051005
UniValue r(UniValue::VOBJ);
1006-
ScriptToUniv(input.redeem_script, r);
1006+
ScriptToUniv(input.redeem_script, /*out=*/r);
10071007
in.pushKV("redeem_script", r);
10081008
}
10091009
if (!input.witness_script.empty()) {
10101010
UniValue r(UniValue::VOBJ);
1011-
ScriptToUniv(input.witness_script, r);
1011+
ScriptToUniv(input.witness_script, /*out=*/r);
10121012
in.pushKV("witness_script", r);
10131013
}
10141014

@@ -1113,12 +1113,12 @@ static RPCHelpMan decodepsbt()
11131113
// Redeem script and witness script
11141114
if (!output.redeem_script.empty()) {
11151115
UniValue r(UniValue::VOBJ);
1116-
ScriptToUniv(output.redeem_script, r);
1116+
ScriptToUniv(output.redeem_script, /*out=*/r);
11171117
out.pushKV("redeem_script", r);
11181118
}
11191119
if (!output.witness_script.empty()) {
11201120
UniValue r(UniValue::VOBJ);
1121-
ScriptToUniv(output.witness_script, r);
1121+
ScriptToUniv(output.witness_script, /*out=*/r);
11221122
out.pushKV("witness_script", r);
11231123
}
11241124

src/test/fuzz/script_format.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,5 @@ FUZZ_TARGET_INIT(script_format, initialize_script_format)
2929
(void)ScriptToAsmStr(script, /*fAttemptSighashDecode=*/fuzzed_data_provider.ConsumeBool());
3030

3131
UniValue o1(UniValue::VOBJ);
32-
ScriptPubKeyToUniv(script, o1, /*include_hex=*/fuzzed_data_provider.ConsumeBool());
33-
UniValue o3(UniValue::VOBJ);
34-
ScriptToUniv(script, o3);
32+
ScriptToUniv(script, /*out=*/o1, /*include_hex=*/fuzzed_data_provider.ConsumeBool(), /*include_address=*/fuzzed_data_provider.ConsumeBool());
3533
}

src/test/fuzz/transaction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,6 @@ FUZZ_TARGET_INIT(transaction, initialize_transaction)
102102
(void)IsWitnessStandard(tx, coins_view_cache);
103103

104104
UniValue u(UniValue::VOBJ);
105-
TxToUniv(tx, /*hashBlock=*/uint256::ZERO, u);
106-
TxToUniv(tx, /*hashBlock=*/uint256::ONE, u);
105+
TxToUniv(tx, /*block_hash=*/uint256::ZERO, /*entry=*/u);
106+
TxToUniv(tx, /*block_hash=*/uint256::ONE, /*entry=*/u);
107107
}

src/wallet/rpc/transactions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ RPCHelpMan gettransaction()
800800

801801
if (verbose) {
802802
UniValue decoded(UniValue::VOBJ);
803-
TxToUniv(*wtx.tx, uint256(), decoded, false);
803+
TxToUniv(*wtx.tx, /*block_hash=*/uint256(), /*entry=*/decoded, /*include_hex=*/false);
804804
entry.pushKV("decoded", decoded);
805805
}
806806

0 commit comments

Comments
 (0)