Merge bitcoin/bitcoin#24673: refactor: followup of remove -deprecatedrpc=addresses flag

9563a645c2 refactor: add stdd:: includes to core_write (fanquake)
8b9efebb0a refactor: use named args when ScriptToUniv or TxToUniv are invoked (Michael Dietz)
22f25a6116 refactor: prefer snake case, TxToUniv arg hashBlock renamed block_hash (Michael Dietz)
828a094ecf 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 9563a645c2 🕓

Tree-SHA512: 4f0e5b45c14cbf68b9e389bbe1211c125d95cbd3da5205b1cff6a4c44f15b15039ba2a5b25cd7e2580d9169404f1b7ff620d8a7e01f6112e3cb153ecfaef8916
This commit is contained in:
MarcoFalke 2022-03-31 08:31:10 +02:00
commit a2e1590f67
No known key found for this signature in database
GPG Key ID: CE2B75697E69A548
9 changed files with 40 additions and 41 deletions

View File

@ -750,7 +750,7 @@ static void MutateTx(CMutableTransaction& tx, const std::string& command,
static void OutputTxJSON(const CTransaction& tx) static void OutputTxJSON(const CTransaction& tx)
{ {
UniValue entry(UniValue::VOBJ); UniValue entry(UniValue::VOBJ);
TxToUniv(tx, uint256(), entry); TxToUniv(tx, /*block_hash=*/uint256(), entry);
std::string jsonOutput = entry.write(4); std::string jsonOutput = entry.write(4);
tfm::format(std::cout, "%s\n", jsonOutput); tfm::format(std::cout, "%s\n", jsonOutput);

View File

@ -53,8 +53,7 @@ UniValue ValueFromAmount(const CAmount amount);
std::string FormatScript(const CScript& script); std::string FormatScript(const CScript& script);
std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags = 0); std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags = 0);
std::string SighashToStr(unsigned char sighash_type); std::string SighashToStr(unsigned char sighash_type);
void ScriptPubKeyToUniv(const CScript& scriptPubKey, UniValue& out, bool include_hex, bool include_address = true); void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex = true, bool include_address = false);
void ScriptToUniv(const CScript& script, UniValue& out); 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);
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);
#endif // BITCOIN_CORE_IO_H #endif // BITCOIN_CORE_IO_H

View File

@ -19,6 +19,10 @@
#include <util/strencodings.h> #include <util/strencodings.h>
#include <util/system.h> #include <util/system.h>
#include <map>
#include <string>
#include <vector>
UniValue ValueFromAmount(const CAmount amount) UniValue ValueFromAmount(const CAmount amount)
{ {
static_assert(COIN > 1); static_assert(COIN > 1);
@ -143,31 +147,28 @@ std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags)
return HexStr(ssTx); return HexStr(ssTx);
} }
void ScriptToUniv(const CScript& script, UniValue& out) void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex, bool include_address)
{
ScriptPubKeyToUniv(script, out, /* include_hex */ true, /* include_address */ false);
}
void ScriptPubKeyToUniv(const CScript& scriptPubKey, UniValue& out, bool include_hex, bool include_address)
{ {
CTxDestination address; CTxDestination address;
out.pushKV("asm", ScriptToAsmStr(scriptPubKey)); out.pushKV("asm", ScriptToAsmStr(script));
if (include_address) { if (include_address) {
out.pushKV("desc", InferDescriptor(scriptPubKey, DUMMY_SIGNING_PROVIDER)->ToString()); out.pushKV("desc", InferDescriptor(script, DUMMY_SIGNING_PROVIDER)->ToString());
}
if (include_hex) {
out.pushKV("hex", HexStr(script));
} }
if (include_hex) out.pushKV("hex", HexStr(scriptPubKey));
std::vector<std::vector<unsigned char>> solns; std::vector<std::vector<unsigned char>> solns;
const TxoutType type{Solver(scriptPubKey, solns)}; const TxoutType type{Solver(script, solns)};
if (include_address && ExtractDestination(scriptPubKey, address) && type != TxoutType::PUBKEY) { if (include_address && ExtractDestination(script, address) && type != TxoutType::PUBKEY) {
out.pushKV("address", EncodeDestination(address)); out.pushKV("address", EncodeDestination(address));
} }
out.pushKV("type", GetTxnOutputType(type)); out.pushKV("type", GetTxnOutputType(type));
} }
void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, bool include_hex, int serialize_flags, const CTxUndo* txundo, TxVerbosity verbosity) void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry, bool include_hex, int serialize_flags, const CTxUndo* txundo, TxVerbosity verbosity)
{ {
entry.pushKV("txid", tx.GetHash().GetHex()); entry.pushKV("txid", tx.GetHash().GetHex());
entry.pushKV("hash", tx.GetWitnessHash().GetHex()); entry.pushKV("hash", tx.GetWitnessHash().GetHex());
@ -215,7 +216,7 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry,
if (verbosity == TxVerbosity::SHOW_DETAILS_AND_PREVOUT) { if (verbosity == TxVerbosity::SHOW_DETAILS_AND_PREVOUT) {
UniValue o_script_pub_key(UniValue::VOBJ); UniValue o_script_pub_key(UniValue::VOBJ);
ScriptPubKeyToUniv(prev_txout.scriptPubKey, o_script_pub_key, /*include_hex=*/ true); ScriptToUniv(prev_txout.scriptPubKey, /*out=*/o_script_pub_key, /*include_hex=*/true, /*include_address=*/true);
UniValue p(UniValue::VOBJ); UniValue p(UniValue::VOBJ);
p.pushKV("generated", bool(prev_coin.fCoinBase)); p.pushKV("generated", bool(prev_coin.fCoinBase));
@ -240,7 +241,7 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry,
out.pushKV("n", (int64_t)i); out.pushKV("n", (int64_t)i);
UniValue o(UniValue::VOBJ); UniValue o(UniValue::VOBJ);
ScriptPubKeyToUniv(txout.scriptPubKey, o, true); ScriptToUniv(txout.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);
out.pushKV("scriptPubKey", o); out.pushKV("scriptPubKey", o);
vout.push_back(out); vout.push_back(out);
@ -256,8 +257,9 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry,
entry.pushKV("fee", ValueFromAmount(fee)); entry.pushKV("fee", ValueFromAmount(fee));
} }
if (!hashBlock.IsNull()) if (!block_hash.IsNull()) {
entry.pushKV("blockhash", hashBlock.GetHex()); entry.pushKV("blockhash", block_hash.GetHex());
}
if (include_hex) { if (include_hex) {
entry.pushKV("hex", EncodeHexTx(tx, serialize_flags)); // The hex-encoded transaction. Used the name "hex" to be consistent with the verbose output of "getrawtransaction". entry.pushKV("hex", EncodeHexTx(tx, serialize_flags)); // The hex-encoded transaction. Used the name "hex" to be consistent with the verbose output of "getrawtransaction".

View File

@ -670,7 +670,7 @@ static bool rest_tx(const std::any& context, HTTPRequest* req, const std::string
case RetFormat::JSON: { case RetFormat::JSON: {
UniValue objTx(UniValue::VOBJ); UniValue objTx(UniValue::VOBJ);
TxToUniv(*tx, hashBlock, objTx); TxToUniv(*tx, /*block_hash=*/hashBlock, /*entry=*/ objTx);
std::string strJSON = objTx.write() + "\n"; std::string strJSON = objTx.write() + "\n";
req->WriteHeader("Content-Type", "application/json"); req->WriteHeader("Content-Type", "application/json");
req->WriteReply(HTTP_OK, strJSON); req->WriteReply(HTTP_OK, strJSON);
@ -855,7 +855,7 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
// include the script in a json output // include the script in a json output
UniValue o(UniValue::VOBJ); UniValue o(UniValue::VOBJ);
ScriptPubKeyToUniv(coin.out.scriptPubKey, o, true); ScriptToUniv(coin.out.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);
utxo.pushKV("scriptPubKey", o); utxo.pushKV("scriptPubKey", o);
utxos.push_back(utxo); utxos.push_back(utxo);
} }

View File

@ -187,7 +187,7 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIn
// coinbase transaction (i.e. i == 0) doesn't have undo data // coinbase transaction (i.e. i == 0) doesn't have undo data
const CTxUndo* txundo = (have_undo && i > 0) ? &blockUndo.vtxundo.at(i - 1) : nullptr; const CTxUndo* txundo = (have_undo && i > 0) ? &blockUndo.vtxundo.at(i - 1) : nullptr;
UniValue objTx(UniValue::VOBJ); UniValue objTx(UniValue::VOBJ);
TxToUniv(*tx, uint256(), objTx, true, RPCSerializationFlags(), txundo, verbosity); TxToUniv(*tx, /*block_hash=*/uint256(), /*entry=*/objTx, /*include_hex=*/true, RPCSerializationFlags(), txundo, verbosity);
txs.push_back(objTx); txs.push_back(objTx);
} }
break; break;
@ -1026,7 +1026,7 @@ static RPCHelpMan gettxout()
} }
ret.pushKV("value", ValueFromAmount(coin.out.nValue)); ret.pushKV("value", ValueFromAmount(coin.out.nValue));
UniValue o(UniValue::VOBJ); UniValue o(UniValue::VOBJ);
ScriptPubKeyToUniv(coin.out.scriptPubKey, o, true); ScriptToUniv(coin.out.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);
ret.pushKV("scriptPubKey", o); ret.pushKV("scriptPubKey", o);
ret.pushKV("coinbase", (bool)coin.fCoinBase); ret.pushKV("coinbase", (bool)coin.fCoinBase);

View File

@ -58,7 +58,7 @@ static void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue&
// Blockchain contextual information (confirmations and blocktime) is not // Blockchain contextual information (confirmations and blocktime) is not
// available to code in bitcoin-common, so we query them here and push the // available to code in bitcoin-common, so we query them here and push the
// data into the returned UniValue. // data into the returned UniValue.
TxToUniv(tx, uint256(), entry, true, RPCSerializationFlags()); TxToUniv(tx, /*block_hash=*/uint256(), entry, /*include_hex=*/true, RPCSerializationFlags());
if (!hashBlock.IsNull()) { if (!hashBlock.IsNull()) {
LOCK(cs_main); LOCK(cs_main);
@ -383,7 +383,7 @@ static RPCHelpMan decoderawtransaction()
} }
UniValue result(UniValue::VOBJ); UniValue result(UniValue::VOBJ);
TxToUniv(CTransaction(std::move(mtx)), uint256(), result, false); TxToUniv(CTransaction(std::move(mtx)), /*block_hash=*/uint256(), /*entry=*/result, /*include_hex=*/false);
return result; return result;
}, },
@ -435,7 +435,7 @@ static RPCHelpMan decodescript()
} else { } else {
// Empty scripts are valid // Empty scripts are valid
} }
ScriptPubKeyToUniv(script, r, /* include_hex */ false); ScriptToUniv(script, /*out=*/r, /*include_hex=*/false, /*include_address=*/true);
std::vector<std::vector<unsigned char>> solutions_data; std::vector<std::vector<unsigned char>> solutions_data;
const TxoutType which_type{Solver(script, solutions_data)}; const TxoutType which_type{Solver(script, solutions_data)};
@ -512,7 +512,7 @@ static RPCHelpMan decodescript()
// Scripts that are not fit for P2WPKH are encoded as P2WSH. // Scripts that are not fit for P2WPKH are encoded as P2WSH.
segwitScr = GetScriptForDestination(WitnessV0ScriptHash(script)); segwitScr = GetScriptForDestination(WitnessV0ScriptHash(script));
} }
ScriptPubKeyToUniv(segwitScr, sr, /* include_hex */ true); ScriptToUniv(segwitScr, /*out=*/sr, /*include_hex=*/true, /*include_address=*/true);
sr.pushKV("p2sh-segwit", EncodeDestination(ScriptHash(segwitScr))); sr.pushKV("p2sh-segwit", EncodeDestination(ScriptHash(segwitScr)));
r.pushKV("segwit", sr); r.pushKV("segwit", sr);
} }
@ -900,7 +900,7 @@ static RPCHelpMan decodepsbt()
// Add the decoded tx // Add the decoded tx
UniValue tx_univ(UniValue::VOBJ); UniValue tx_univ(UniValue::VOBJ);
TxToUniv(CTransaction(*psbtx.tx), uint256(), tx_univ, false); TxToUniv(CTransaction(*psbtx.tx), /*block_hash=*/uint256(), /*entry=*/tx_univ, /*include_hex=*/false);
result.pushKV("tx", tx_univ); result.pushKV("tx", tx_univ);
// Add the global xpubs // Add the global xpubs
@ -956,7 +956,7 @@ static RPCHelpMan decodepsbt()
txout = input.witness_utxo; txout = input.witness_utxo;
UniValue o(UniValue::VOBJ); UniValue o(UniValue::VOBJ);
ScriptPubKeyToUniv(txout.scriptPubKey, o, /* include_hex */ true); ScriptToUniv(txout.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);
UniValue out(UniValue::VOBJ); UniValue out(UniValue::VOBJ);
out.pushKV("amount", ValueFromAmount(txout.nValue)); out.pushKV("amount", ValueFromAmount(txout.nValue));
@ -970,7 +970,7 @@ static RPCHelpMan decodepsbt()
txout = input.non_witness_utxo->vout[psbtx.tx->vin[i].prevout.n]; txout = input.non_witness_utxo->vout[psbtx.tx->vin[i].prevout.n];
UniValue non_wit(UniValue::VOBJ); UniValue non_wit(UniValue::VOBJ);
TxToUniv(*input.non_witness_utxo, uint256(), non_wit, false); TxToUniv(*input.non_witness_utxo, /*block_hash=*/uint256(), /*entry=*/non_wit, /*include_hex=*/false);
in.pushKV("non_witness_utxo", non_wit); in.pushKV("non_witness_utxo", non_wit);
have_a_utxo = true; have_a_utxo = true;
@ -1003,12 +1003,12 @@ static RPCHelpMan decodepsbt()
// Redeem script and witness script // Redeem script and witness script
if (!input.redeem_script.empty()) { if (!input.redeem_script.empty()) {
UniValue r(UniValue::VOBJ); UniValue r(UniValue::VOBJ);
ScriptToUniv(input.redeem_script, r); ScriptToUniv(input.redeem_script, /*out=*/r);
in.pushKV("redeem_script", r); in.pushKV("redeem_script", r);
} }
if (!input.witness_script.empty()) { if (!input.witness_script.empty()) {
UniValue r(UniValue::VOBJ); UniValue r(UniValue::VOBJ);
ScriptToUniv(input.witness_script, r); ScriptToUniv(input.witness_script, /*out=*/r);
in.pushKV("witness_script", r); in.pushKV("witness_script", r);
} }
@ -1113,12 +1113,12 @@ static RPCHelpMan decodepsbt()
// Redeem script and witness script // Redeem script and witness script
if (!output.redeem_script.empty()) { if (!output.redeem_script.empty()) {
UniValue r(UniValue::VOBJ); UniValue r(UniValue::VOBJ);
ScriptToUniv(output.redeem_script, r); ScriptToUniv(output.redeem_script, /*out=*/r);
out.pushKV("redeem_script", r); out.pushKV("redeem_script", r);
} }
if (!output.witness_script.empty()) { if (!output.witness_script.empty()) {
UniValue r(UniValue::VOBJ); UniValue r(UniValue::VOBJ);
ScriptToUniv(output.witness_script, r); ScriptToUniv(output.witness_script, /*out=*/r);
out.pushKV("witness_script", r); out.pushKV("witness_script", r);
} }

View File

@ -29,7 +29,5 @@ FUZZ_TARGET_INIT(script_format, initialize_script_format)
(void)ScriptToAsmStr(script, /*fAttemptSighashDecode=*/fuzzed_data_provider.ConsumeBool()); (void)ScriptToAsmStr(script, /*fAttemptSighashDecode=*/fuzzed_data_provider.ConsumeBool());
UniValue o1(UniValue::VOBJ); UniValue o1(UniValue::VOBJ);
ScriptPubKeyToUniv(script, o1, /*include_hex=*/fuzzed_data_provider.ConsumeBool()); ScriptToUniv(script, /*out=*/o1, /*include_hex=*/fuzzed_data_provider.ConsumeBool(), /*include_address=*/fuzzed_data_provider.ConsumeBool());
UniValue o3(UniValue::VOBJ);
ScriptToUniv(script, o3);
} }

View File

@ -102,6 +102,6 @@ FUZZ_TARGET_INIT(transaction, initialize_transaction)
(void)IsWitnessStandard(tx, coins_view_cache); (void)IsWitnessStandard(tx, coins_view_cache);
UniValue u(UniValue::VOBJ); UniValue u(UniValue::VOBJ);
TxToUniv(tx, /*hashBlock=*/uint256::ZERO, u); TxToUniv(tx, /*block_hash=*/uint256::ZERO, /*entry=*/u);
TxToUniv(tx, /*hashBlock=*/uint256::ONE, u); TxToUniv(tx, /*block_hash=*/uint256::ONE, /*entry=*/u);
} }

View File

@ -800,7 +800,7 @@ RPCHelpMan gettransaction()
if (verbose) { if (verbose) {
UniValue decoded(UniValue::VOBJ); UniValue decoded(UniValue::VOBJ);
TxToUniv(*wtx.tx, uint256(), decoded, false); TxToUniv(*wtx.tx, /*block_hash=*/uint256(), /*entry=*/decoded, /*include_hex=*/false);
entry.pushKV("decoded", decoded); entry.pushKV("decoded", decoded);
} }