Coverage Report

Created: 2026-06-01 18:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/src/core_io.cpp
Line
Count
Source
1
// Copyright (c) 2009-present The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#include <core_io.h>
6
7
#include <addresstype.h>
8
#include <coins.h>
9
#include <consensus/amount.h>
10
#include <consensus/consensus.h>
11
#include <consensus/validation.h>
12
#include <crypto/hex_base.h>
13
#include <key_io.h>
14
#include <primitives/block.h>
15
#include <primitives/transaction.h>
16
#include <script/descriptor.h>
17
#include <script/interpreter.h>
18
#include <script/script.h>
19
#include <script/signingprovider.h>
20
#include <script/solver.h>
21
#include <serialize.h>
22
#include <streams.h>
23
#include <tinyformat.h>
24
#include <uint256.h>
25
#include <undo.h>
26
#include <univalue.h>
27
#include <util/check.h>
28
#include <util/result.h>
29
#include <util/strencodings.h>
30
#include <util/string.h>
31
#include <util/translation.h>
32
33
#include <algorithm>
34
#include <compare>
35
#include <cstdint>
36
#include <exception>
37
#include <functional>
38
#include <map>
39
#include <memory>
40
#include <optional>
41
#include <span>
42
#include <stdexcept>
43
#include <string>
44
#include <utility>
45
#include <vector>
46
47
using util::SplitString;
48
49
namespace {
50
class OpCodeParser
51
{
52
private:
53
    std::map<std::string, opcodetype> mapOpNames;
54
55
public:
56
    OpCodeParser()
57
0
    {
58
0
        for (unsigned int op = 0; op <= MAX_OPCODE; ++op) {
  Branch (58:35): [True: 0, False: 0]
59
            // Allow OP_RESERVED to get into mapOpNames
60
0
            if (op < OP_NOP && op != OP_RESERVED) {
  Branch (60:17): [True: 0, False: 0]
  Branch (60:32): [True: 0, False: 0]
61
0
                continue;
62
0
            }
63
64
0
            std::string strName = GetOpName(static_cast<opcodetype>(op));
65
0
            if (strName == "OP_UNKNOWN") {
  Branch (65:17): [True: 0, False: 0]
66
0
                continue;
67
0
            }
68
0
            mapOpNames[strName] = static_cast<opcodetype>(op);
69
            // Convenience: OP_ADD and just ADD are both recognized:
70
0
            if (strName.starts_with("OP_")) {
  Branch (70:17): [True: 0, False: 0]
71
0
                mapOpNames[strName.substr(3)] = static_cast<opcodetype>(op);
72
0
            }
73
0
        }
74
0
    }
75
    opcodetype Parse(const std::string& s) const
76
0
    {
77
0
        auto it = mapOpNames.find(s);
78
0
        if (it == mapOpNames.end()) throw std::runtime_error("script parse error: unknown opcode");
  Branch (78:13): [True: 0, False: 0]
79
0
        return it->second;
80
0
    }
81
};
82
83
opcodetype ParseOpCode(const std::string& s)
84
0
{
85
0
    static const OpCodeParser ocp;
86
0
    return ocp.Parse(s);
87
0
}
88
89
} // namespace
90
91
CScript ParseScript(const std::string& s)
92
0
{
93
0
    CScript result;
94
95
0
    std::vector<std::string> words = SplitString(s, " \t\n");
96
97
0
    for (const std::string& w : words) {
  Branch (97:31): [True: 0, False: 0]
98
0
        if (w.empty()) {
  Branch (98:13): [True: 0, False: 0]
99
            // Empty string, ignore. (SplitString doesn't combine multiple separators)
100
0
        } else if (std::all_of(w.begin(), w.end(), ::IsDigit) ||
  Branch (100:20): [True: 0, False: 0]
  Branch (100:20): [True: 0, False: 0]
101
0
                   (w.front() == '-' && w.size() > 1 && std::all_of(w.begin() + 1, w.end(), ::IsDigit)))
  Branch (101:21): [True: 0, False: 0]
  Branch (101:41): [True: 0, False: 0]
  Branch (101:57): [True: 0, False: 0]
102
0
        {
103
            // Number
104
0
            const auto num{ToIntegral<int64_t>(w)};
105
106
            // limit the range of numbers ParseScript accepts in decimal
107
            // since numbers outside -0xFFFFFFFF...0xFFFFFFFF are illegal in scripts
108
0
            if (!num.has_value() || num > int64_t{0xffffffff} || num < -1 * int64_t{0xffffffff}) {
  Branch (108:17): [True: 0, False: 0]
  Branch (108:17): [True: 0, False: 0]
  Branch (108:37): [True: 0, False: 0]
  Branch (108:66): [True: 0, False: 0]
109
0
                throw std::runtime_error("script parse error: decimal numeric value only allowed in the "
110
0
                                         "range -0xFFFFFFFF...0xFFFFFFFF");
111
0
            }
112
113
0
            result << num.value();
114
0
        } else if (w.starts_with("0x") && w.size() > 2 && IsHex(std::string(w.begin() + 2, w.end()))) {
  Branch (114:20): [True: 0, False: 0]
  Branch (114:20): [True: 0, False: 0]
  Branch (114:43): [True: 0, False: 0]
  Branch (114:59): [True: 0, False: 0]
115
            // Raw hex data, inserted NOT pushed onto stack:
116
0
            std::vector<unsigned char> raw = ParseHex(std::string(w.begin() + 2, w.end()));
117
0
            result.insert(result.end(), raw.begin(), raw.end());
118
0
        } else if (w.size() >= 2 && w.front() == '\'' && w.back() == '\'') {
  Branch (118:20): [True: 0, False: 0]
  Branch (118:37): [True: 0, False: 0]
  Branch (118:58): [True: 0, False: 0]
119
            // Single-quoted string, pushed as data. NOTE: this is poor-man's
120
            // parsing, spaces/tabs/newlines in single-quoted strings won't work.
121
0
            std::vector<unsigned char> value(w.begin() + 1, w.end() - 1);
122
0
            result << value;
123
0
        } else {
124
            // opcode, e.g. OP_ADD or ADD:
125
0
            result << ParseOpCode(w);
126
0
        }
127
0
    }
128
129
0
    return result;
130
0
}
131
132
/// Check that all of the input and output scripts of a transaction contain valid opcodes
133
static bool CheckTxScriptsSanity(const CMutableTransaction& tx)
134
0
{
135
    // Check input scripts for non-coinbase txs
136
0
    if (!CTransaction(tx).IsCoinBase()) {
  Branch (136:9): [True: 0, False: 0]
137
0
        for (unsigned int i = 0; i < tx.vin.size(); i++) {
  Branch (137:34): [True: 0, False: 0]
138
0
            if (!tx.vin[i].scriptSig.HasValidOps() || tx.vin[i].scriptSig.size() > MAX_SCRIPT_SIZE) {
  Branch (138:17): [True: 0, False: 0]
  Branch (138:55): [True: 0, False: 0]
139
0
                return false;
140
0
            }
141
0
        }
142
0
    }
143
    // Check output scripts
144
0
    for (unsigned int i = 0; i < tx.vout.size(); i++) {
  Branch (144:30): [True: 0, False: 0]
145
0
        if (!tx.vout[i].scriptPubKey.HasValidOps() || tx.vout[i].scriptPubKey.size() > MAX_SCRIPT_SIZE) {
  Branch (145:13): [True: 0, False: 0]
  Branch (145:55): [True: 0, False: 0]
146
0
            return false;
147
0
        }
148
0
    }
149
150
0
    return true;
151
0
}
152
153
static bool DecodeTx(CMutableTransaction& tx, const std::vector<unsigned char>& tx_data, bool try_no_witness, bool try_witness)
154
0
{
155
    // General strategy:
156
    // - Decode both with extended serialization (which interprets the 0x0001 tag as a marker for
157
    //   the presence of witnesses) and with legacy serialization (which interprets the tag as a
158
    //   0-input 1-output incomplete transaction).
159
    //   - Restricted by try_no_witness (which disables legacy if false) and try_witness (which
160
    //     disables extended if false).
161
    //   - Ignore serializations that do not fully consume the hex string.
162
    // - If neither succeeds, fail.
163
    // - If only one succeeds, return that one.
164
    // - If both decode attempts succeed:
165
    //   - If only one passes the CheckTxScriptsSanity check, return that one.
166
    //   - If neither or both pass CheckTxScriptsSanity, return the extended one.
167
168
0
    CMutableTransaction tx_extended, tx_legacy;
169
0
    bool ok_extended = false, ok_legacy = false;
170
171
    // Try decoding with extended serialization support, and remember if the result successfully
172
    // consumes the entire input.
173
0
    if (try_witness) {
  Branch (173:9): [True: 0, False: 0]
174
0
        SpanReader ssData{tx_data};
175
0
        try {
176
0
            ssData >> TX_WITH_WITNESS(tx_extended);
177
0
            if (ssData.empty()) ok_extended = true;
  Branch (177:17): [True: 0, False: 0]
178
0
        } catch (const std::exception&) {
179
            // Fall through.
180
0
        }
181
0
    }
182
183
    // Optimization: if extended decoding succeeded and the result passes CheckTxScriptsSanity,
184
    // don't bother decoding the other way.
185
0
    if (ok_extended && CheckTxScriptsSanity(tx_extended)) {
  Branch (185:9): [True: 0, False: 0]
  Branch (185:24): [True: 0, False: 0]
186
0
        tx = std::move(tx_extended);
187
0
        return true;
188
0
    }
189
190
    // Try decoding with legacy serialization, and remember if the result successfully consumes the entire input.
191
0
    if (try_no_witness) {
  Branch (191:9): [True: 0, False: 0]
192
0
        SpanReader ssData{tx_data};
193
0
        try {
194
0
            ssData >> TX_NO_WITNESS(tx_legacy);
195
0
            if (ssData.empty()) ok_legacy = true;
  Branch (195:17): [True: 0, False: 0]
196
0
        } catch (const std::exception&) {
197
            // Fall through.
198
0
        }
199
0
    }
200
201
    // If legacy decoding succeeded and passes CheckTxScriptsSanity, that's our answer, as we know
202
    // at this point that extended decoding either failed or doesn't pass the sanity check.
203
0
    if (ok_legacy && CheckTxScriptsSanity(tx_legacy)) {
  Branch (203:9): [True: 0, False: 0]
  Branch (203:22): [True: 0, False: 0]
204
0
        tx = std::move(tx_legacy);
205
0
        return true;
206
0
    }
207
208
    // If extended decoding succeeded, and neither decoding passes sanity, return the extended one.
209
0
    if (ok_extended) {
  Branch (209:9): [True: 0, False: 0]
210
0
        tx = std::move(tx_extended);
211
0
        return true;
212
0
    }
213
214
    // If legacy decoding succeeded and extended didn't, return the legacy one.
215
0
    if (ok_legacy) {
  Branch (215:9): [True: 0, False: 0]
216
0
        tx = std::move(tx_legacy);
217
0
        return true;
218
0
    }
219
220
    // If none succeeded, we failed.
221
0
    return false;
222
0
}
223
224
bool DecodeHexTx(CMutableTransaction& tx, const std::string& hex_tx, bool try_no_witness, bool try_witness)
225
0
{
226
0
    if (!IsHex(hex_tx)) {
  Branch (226:9): [True: 0, False: 0]
227
0
        return false;
228
0
    }
229
230
0
    std::vector<unsigned char> txData(ParseHex(hex_tx));
231
0
    return DecodeTx(tx, txData, try_no_witness, try_witness);
232
0
}
233
234
bool DecodeHexBlockHeader(CBlockHeader& header, const std::string& hex_header)
235
0
{
236
0
    if (!IsHex(hex_header)) return false;
  Branch (236:9): [True: 0, False: 0]
237
238
0
    const std::vector<unsigned char> header_data{ParseHex(hex_header)};
239
0
    try {
240
0
        SpanReader{header_data} >> header;
241
0
    } catch (const std::exception&) {
242
0
        return false;
243
0
    }
244
0
    return true;
245
0
}
246
247
bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
248
0
{
249
0
    if (!IsHex(strHexBlk))
  Branch (249:9): [True: 0, False: 0]
250
0
        return false;
251
252
0
    std::vector<unsigned char> blockData(ParseHex(strHexBlk));
253
0
    try {
254
0
        SpanReader{blockData} >> TX_WITH_WITNESS(block);
255
0
    }
256
0
    catch (const std::exception&) {
257
0
        return false;
258
0
    }
259
260
0
    return true;
261
0
}
262
263
util::Result<int> SighashFromStr(const std::string& sighash)
264
0
{
265
0
    static const std::map<std::string, int> map_sighash_values = {
266
0
        {std::string("DEFAULT"), int(SIGHASH_DEFAULT)},
267
0
        {std::string("ALL"), int(SIGHASH_ALL)},
268
0
        {std::string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY)},
269
0
        {std::string("NONE"), int(SIGHASH_NONE)},
270
0
        {std::string("NONE|ANYONECANPAY"), int(SIGHASH_NONE|SIGHASH_ANYONECANPAY)},
271
0
        {std::string("SINGLE"), int(SIGHASH_SINGLE)},
272
0
        {std::string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY)},
273
0
    };
274
0
    const auto& it = map_sighash_values.find(sighash);
275
0
    if (it != map_sighash_values.end()) {
  Branch (275:9): [True: 0, False: 0]
276
0
        return it->second;
277
0
    } else {
278
0
        return util::Error{Untranslated("'" + sighash + "' is not a valid sighash parameter.")};
279
0
    }
280
0
}
281
282
UniValue ValueFromAmount(const CAmount amount)
283
0
{
284
0
    static_assert(COIN > 1);
285
0
    int64_t quotient = amount / COIN;
286
0
    int64_t remainder = amount % COIN;
287
0
    if (amount < 0) {
  Branch (287:9): [True: 0, False: 0]
288
0
        quotient = -quotient;
289
0
        remainder = -remainder;
290
0
    }
291
0
    return UniValue(UniValue::VNUM,
292
0
            strprintf("%s%d.%08d", amount < 0 ? "-" : "", quotient, remainder));
  Branch (292:36): [True: 0, False: 0]
293
0
}
294
295
std::string FormatScript(const CScript& script)
296
0
{
297
0
    std::string ret;
298
0
    CScript::const_iterator it = script.begin();
299
0
    opcodetype op;
300
0
    while (it != script.end()) {
  Branch (300:12): [True: 0, False: 0]
301
0
        CScript::const_iterator it2 = it;
302
0
        std::vector<unsigned char> vch;
303
0
        if (script.GetOp(it, op, vch)) {
  Branch (303:13): [True: 0, False: 0]
304
0
            if (op == OP_0) {
  Branch (304:17): [True: 0, False: 0]
305
0
                ret += "0 ";
306
0
                continue;
307
0
            } else if ((op >= OP_1 && op <= OP_16) || op == OP_1NEGATE) {
  Branch (307:25): [True: 0, False: 0]
  Branch (307:39): [True: 0, False: 0]
  Branch (307:55): [True: 0, False: 0]
308
0
                ret += strprintf("%i ", op - OP_1NEGATE - 1);
309
0
                continue;
310
0
            } else if (op >= OP_NOP && op <= OP_NOP10) {
  Branch (310:24): [True: 0, False: 0]
  Branch (310:40): [True: 0, False: 0]
311
0
                std::string str(GetOpName(op));
312
0
                if (str.substr(0, 3) == std::string("OP_")) {
  Branch (312:21): [True: 0, False: 0]
313
0
                    ret += str.substr(3, std::string::npos) + " ";
314
0
                    continue;
315
0
                }
316
0
            }
317
0
            if (vch.size() > 0) {
  Branch (317:17): [True: 0, False: 0]
318
0
                ret += strprintf("0x%x 0x%x ", HexStr(std::vector<uint8_t>(it2, it - vch.size())),
319
0
                                               HexStr(std::vector<uint8_t>(it - vch.size(), it)));
320
0
            } else {
321
0
                ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, it)));
322
0
            }
323
0
            continue;
324
0
        }
325
0
        ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, script.end())));
326
0
        break;
327
0
    }
328
0
    return ret.substr(0, ret.empty() ? ret.npos : ret.size() - 1);
  Branch (328:26): [True: 0, False: 0]
329
0
}
330
331
const std::map<unsigned char, std::string> mapSigHashTypes = {
332
    {static_cast<unsigned char>(SIGHASH_ALL), std::string("ALL")},
333
    {static_cast<unsigned char>(SIGHASH_ALL|SIGHASH_ANYONECANPAY), std::string("ALL|ANYONECANPAY")},
334
    {static_cast<unsigned char>(SIGHASH_NONE), std::string("NONE")},
335
    {static_cast<unsigned char>(SIGHASH_NONE|SIGHASH_ANYONECANPAY), std::string("NONE|ANYONECANPAY")},
336
    {static_cast<unsigned char>(SIGHASH_SINGLE), std::string("SINGLE")},
337
    {static_cast<unsigned char>(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY), std::string("SINGLE|ANYONECANPAY")},
338
};
339
340
std::string SighashToStr(unsigned char sighash_type)
341
0
{
342
0
    const auto& it = mapSigHashTypes.find(sighash_type);
343
0
    if (it == mapSigHashTypes.end()) return "";
  Branch (343:9): [True: 0, False: 0]
344
0
    return it->second;
345
0
}
346
347
/**
348
 * Create the assembly string representation of a CScript object.
349
 * @param[in] script    CScript object to convert into the asm string representation.
350
 * @param[in] fAttemptSighashDecode    Whether to attempt to decode sighash types on data within the script that matches the format
351
 *                                     of a signature. Only pass true for scripts you believe could contain signatures. For example,
352
 *                                     pass false, or omit the this argument (defaults to false), for scriptPubKeys.
353
 */
354
std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode)
355
0
{
356
0
    std::string str;
357
0
    opcodetype opcode;
358
0
    std::vector<unsigned char> vch;
359
0
    CScript::const_iterator pc = script.begin();
360
0
    while (pc < script.end()) {
  Branch (360:12): [True: 0, False: 0]
361
0
        if (!str.empty()) {
  Branch (361:13): [True: 0, False: 0]
362
0
            str += " ";
363
0
        }
364
0
        if (!script.GetOp(pc, opcode, vch)) {
  Branch (364:13): [True: 0, False: 0]
365
0
            str += "[error]";
366
0
            return str;
367
0
        }
368
0
        if (0 <= opcode && opcode <= OP_PUSHDATA4) {
  Branch (368:13): [True: 0, False: 0]
  Branch (368:28): [True: 0, False: 0]
369
0
            if (vch.size() <= static_cast<std::vector<unsigned char>::size_type>(4)) {
  Branch (369:17): [True: 0, False: 0]
370
0
                str += strprintf("%d", CScriptNum(vch, false).getint());
371
0
            } else {
372
                // the IsUnspendable check makes sure not to try to decode OP_RETURN data that may match the format of a signature
373
0
                if (fAttemptSighashDecode && !script.IsUnspendable()) {
  Branch (373:21): [True: 0, False: 0]
  Branch (373:46): [True: 0, False: 0]
374
0
                    std::string strSigHashDecode;
375
                    // goal: only attempt to decode a defined sighash type from data that looks like a signature within a scriptSig.
376
                    // this won't decode correctly formatted public keys in Pubkey or Multisig scripts due to
377
                    // the restrictions on the pubkey formats (see IsCompressedOrUncompressedPubKey) being incongruous with the
378
                    // checks in CheckSignatureEncoding.
379
0
                    if (CheckSignatureEncoding(vch, SCRIPT_VERIFY_STRICTENC, nullptr)) {
  Branch (379:25): [True: 0, False: 0]
380
0
                        const unsigned char chSigHashType = vch.back();
381
0
                        const auto it = mapSigHashTypes.find(chSigHashType);
382
0
                        if (it != mapSigHashTypes.end()) {
  Branch (382:29): [True: 0, False: 0]
383
0
                            strSigHashDecode = "[" + it->second + "]";
384
0
                            vch.pop_back(); // remove the sighash type byte. it will be replaced by the decode.
385
0
                        }
386
0
                    }
387
0
                    str += HexStr(vch) + strSigHashDecode;
388
0
                } else {
389
0
                    str += HexStr(vch);
390
0
                }
391
0
            }
392
0
        } else {
393
0
            str += GetOpName(opcode);
394
0
        }
395
0
    }
396
0
    return str;
397
0
}
398
399
std::string EncodeHexTx(const CTransaction& tx)
400
0
{
401
0
    DataStream ssTx;
402
0
    ssTx << TX_WITH_WITNESS(tx);
403
0
    return HexStr(ssTx);
404
0
}
405
406
void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex, bool include_address, const SigningProvider* provider)
407
0
{
408
0
    CTxDestination address;
409
410
0
    out.pushKV("asm", ScriptToAsmStr(script));
411
0
    if (include_address) {
  Branch (411:9): [True: 0, False: 0]
412
0
        out.pushKV("desc", InferDescriptor(script, provider ? *provider : DUMMY_SIGNING_PROVIDER)->ToString());
  Branch (412:52): [True: 0, False: 0]
413
0
    }
414
0
    if (include_hex) {
  Branch (414:9): [True: 0, False: 0]
415
0
        out.pushKV("hex", HexStr(script));
416
0
    }
417
418
0
    std::vector<std::vector<unsigned char>> solns;
419
0
    const TxoutType type{Solver(script, solns)};
420
421
0
    if (include_address && ExtractDestination(script, address) && type != TxoutType::PUBKEY) {
  Branch (421:9): [True: 0, False: 0]
  Branch (421:28): [True: 0, False: 0]
  Branch (421:67): [True: 0, False: 0]
422
0
        out.pushKV("address", EncodeDestination(address));
423
0
    }
424
0
    out.pushKV("type", GetTxnOutputType(type));
425
0
}
426
427
void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry, bool include_hex, const CTxUndo* txundo, TxVerbosity verbosity, std::function<bool(const CTxOut&)> is_change_func)
428
0
{
429
0
    CHECK_NONFATAL(verbosity >= TxVerbosity::SHOW_DETAILS);
430
431
0
    entry.pushKV("txid", tx.GetHash().GetHex());
432
0
    entry.pushKV("hash", tx.GetWitnessHash().GetHex());
433
0
    entry.pushKV("version", tx.version);
434
0
    entry.pushKV("size", tx.ComputeTotalSize());
435
0
    entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR);
436
0
    entry.pushKV("weight", GetTransactionWeight(tx));
437
0
    entry.pushKV("locktime", tx.nLockTime);
438
439
0
    UniValue vin{UniValue::VARR};
440
0
    vin.reserve(tx.vin.size());
441
442
    // If available, use Undo data to calculate the fee. Note that txundo == nullptr
443
    // for coinbase transactions and for transactions where undo data is unavailable.
444
0
    const bool have_undo = txundo != nullptr;
445
0
    CAmount amt_total_in = 0;
446
0
    CAmount amt_total_out = 0;
447
448
0
    for (unsigned int i = 0; i < tx.vin.size(); i++) {
  Branch (448:30): [True: 0, False: 0]
449
0
        const CTxIn& txin = tx.vin[i];
450
0
        UniValue in(UniValue::VOBJ);
451
0
        if (tx.IsCoinBase()) {
  Branch (451:13): [True: 0, False: 0]
452
0
            in.pushKV("coinbase", HexStr(txin.scriptSig));
453
0
        } else {
454
0
            in.pushKV("txid", txin.prevout.hash.GetHex());
455
0
            in.pushKV("vout", txin.prevout.n);
456
0
            UniValue o(UniValue::VOBJ);
457
0
            o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true));
458
0
            o.pushKV("hex", HexStr(txin.scriptSig));
459
0
            in.pushKV("scriptSig", std::move(o));
460
0
        }
461
0
        if (!tx.vin[i].scriptWitness.IsNull()) {
  Branch (461:13): [True: 0, False: 0]
462
0
            UniValue txinwitness(UniValue::VARR);
463
0
            txinwitness.reserve(tx.vin[i].scriptWitness.stack.size());
464
0
            for (const auto& item : tx.vin[i].scriptWitness.stack) {
  Branch (464:35): [True: 0, False: 0]
465
0
                txinwitness.push_back(HexStr(item));
466
0
            }
467
0
            in.pushKV("txinwitness", std::move(txinwitness));
468
0
        }
469
0
        if (have_undo) {
  Branch (469:13): [True: 0, False: 0]
470
0
            const Coin& prev_coin = txundo->vprevout[i];
471
0
            const CTxOut& prev_txout = prev_coin.out;
472
473
0
            amt_total_in += prev_txout.nValue;
474
475
0
            if (verbosity == TxVerbosity::SHOW_DETAILS_AND_PREVOUT) {
  Branch (475:17): [True: 0, False: 0]
476
0
                UniValue o_script_pub_key(UniValue::VOBJ);
477
0
                ScriptToUniv(prev_txout.scriptPubKey, /*out=*/o_script_pub_key, /*include_hex=*/true, /*include_address=*/true);
478
479
0
                UniValue p(UniValue::VOBJ);
480
0
                p.pushKV("generated", prev_coin.IsCoinBase());
481
0
                p.pushKV("height", prev_coin.nHeight);
482
0
                p.pushKV("value", ValueFromAmount(prev_txout.nValue));
483
0
                p.pushKV("scriptPubKey", std::move(o_script_pub_key));
484
0
                in.pushKV("prevout", std::move(p));
485
0
            }
486
0
        }
487
0
        in.pushKV("sequence", txin.nSequence);
488
0
        vin.push_back(std::move(in));
489
0
    }
490
0
    entry.pushKV("vin", std::move(vin));
491
492
0
    UniValue vout(UniValue::VARR);
493
0
    vout.reserve(tx.vout.size());
494
0
    for (unsigned int i = 0; i < tx.vout.size(); i++) {
  Branch (494:30): [True: 0, False: 0]
495
0
        const CTxOut& txout = tx.vout[i];
496
497
0
        UniValue out(UniValue::VOBJ);
498
499
0
        out.pushKV("value", ValueFromAmount(txout.nValue));
500
0
        out.pushKV("n", i);
501
502
0
        UniValue o(UniValue::VOBJ);
503
0
        ScriptToUniv(txout.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);
504
0
        out.pushKV("scriptPubKey", std::move(o));
505
506
0
        if (is_change_func && is_change_func(txout)) {
  Branch (506:13): [True: 0, False: 0]
  Branch (506:31): [True: 0, False: 0]
507
0
            out.pushKV("ischange", true);
508
0
        }
509
510
0
        vout.push_back(std::move(out));
511
512
0
        if (have_undo) {
  Branch (512:13): [True: 0, False: 0]
513
0
            amt_total_out += txout.nValue;
514
0
        }
515
0
    }
516
0
    entry.pushKV("vout", std::move(vout));
517
518
0
    if (have_undo) {
  Branch (518:9): [True: 0, False: 0]
519
0
        const CAmount fee = amt_total_in - amt_total_out;
520
0
        CHECK_NONFATAL(MoneyRange(fee));
521
0
        entry.pushKV("fee", ValueFromAmount(fee));
522
0
    }
523
524
0
    if (!block_hash.IsNull()) {
  Branch (524:9): [True: 0, False: 0]
525
0
        entry.pushKV("blockhash", block_hash.GetHex());
526
0
    }
527
528
0
    if (include_hex) {
  Branch (528:9): [True: 0, False: 0]
529
0
        entry.pushKV("hex", EncodeHexTx(tx)); // The hex-encoded transaction. Used the name "hex" to be consistent with the verbose output of "getrawtransaction".
530
0
    }
531
0
}