bitcoin core ipc fuzz coverage

Coverage Report

Created: 2026-08-26 16:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/home/enochazariah/dev/bitcoin-invariants/src/wallet/transaction.h
Line
Count
Source
1
// Copyright (c) 2021-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
#ifndef BITCOIN_WALLET_TRANSACTION_H
6
#define BITCOIN_WALLET_TRANSACTION_H
7
8
#include <attributes.h>
9
#include <consensus/amount.h>
10
#include <primitives/transaction.h>
11
#include <tinyformat.h>
12
#include <uint256.h>
13
#include <util/check.h>
14
#include <util/overloaded.h>
15
#include <util/strencodings.h>
16
#include <util/string.h>
17
#include <wallet/types.h>
18
19
#include <bitset>
20
#include <cstdint>
21
#include <map>
22
#include <utility>
23
#include <variant>
24
#include <vector>
25
26
namespace interfaces {
27
class Chain;
28
} // namespace interfaces
29
30
namespace wallet {
31
//! State of transaction confirmed in a block.
32
struct TxStateConfirmed {
33
    uint256 confirmed_block_hash;
34
    int confirmed_block_height;
35
    int position_in_block;
36
37
0
    explicit TxStateConfirmed(const uint256& block_hash, int height, int index) : confirmed_block_hash(block_hash), confirmed_block_height(height), position_in_block(index) {}
38
0
    std::string toString() const { return strprintf("Confirmed (block=%s, height=%i, index=%i)", confirmed_block_hash.ToString(), confirmed_block_height, position_in_block); }
Line
Count
Source
1172
0
#define strprintf tfm::format
39
};
40
41
//! State of transaction added to mempool.
42
struct TxStateInMempool {
43
0
    std::string toString() const { return strprintf("InMempool"); }
Line
Count
Source
1172
0
#define strprintf tfm::format
44
};
45
46
//! State of rejected transaction that conflicts with a confirmed block.
47
struct TxStateBlockConflicted {
48
    uint256 conflicting_block_hash;
49
    int conflicting_block_height;
50
51
0
    explicit TxStateBlockConflicted(const uint256& block_hash, int height) : conflicting_block_hash(block_hash), conflicting_block_height(height) {}
52
0
    std::string toString() const { return strprintf("BlockConflicted (block=%s, height=%i)", conflicting_block_hash.ToString(), conflicting_block_height); }
Line
Count
Source
1172
0
#define strprintf tfm::format
53
};
54
55
//! State of transaction not confirmed or conflicting with a known block and
56
//! not in the mempool. May conflict with the mempool, or with an unknown block,
57
//! or be abandoned, never broadcast, or rejected from the mempool for another
58
//! reason.
59
struct TxStateInactive {
60
    bool abandoned;
61
62
0
    explicit TxStateInactive(bool abandoned = false) : abandoned(abandoned) {}
63
0
    std::string toString() const { return strprintf("Inactive (abandoned=%i)", abandoned); }
Line
Count
Source
1172
0
#define strprintf tfm::format
64
};
65
66
//! State of transaction loaded in an unrecognized state with unexpected hash or
67
//! index values. Treated as inactive (with serialized hash and index values
68
//! preserved) by default, but may enter another state if transaction is added
69
//! to the mempool, or confirmed, or abandoned, or found conflicting.
70
struct TxStateUnrecognized {
71
    uint256 block_hash;
72
    int index;
73
74
0
    TxStateUnrecognized(const uint256& block_hash, int index) : block_hash(block_hash), index(index) {}
75
0
    std::string toString() const { return strprintf("Unrecognized (block=%s, index=%i)", block_hash.ToString(), index); }
Line
Count
Source
1172
0
#define strprintf tfm::format
76
};
77
78
//! All possible CWalletTx states
79
using TxState = std::variant<TxStateConfirmed, TxStateInMempool, TxStateBlockConflicted, TxStateInactive, TxStateUnrecognized>;
80
81
//! Subset of states transaction sync logic is implemented to handle.
82
using SyncTxState = std::variant<TxStateConfirmed, TxStateInMempool, TxStateInactive>;
83
84
//! Try to interpret deserialized TxStateUnrecognized data as a recognized state.
85
static inline TxState TxStateInterpretSerialized(TxStateUnrecognized data)
86
0
{
87
0
    if (data.block_hash == uint256::ZERO) {
88
0
        if (data.index == 0) return TxStateInactive{};
89
0
    } else if (data.block_hash == uint256::ONE) {
90
0
        if (data.index == -1) return TxStateInactive{/*abandoned=*/true};
91
0
    } else if (data.index >= 0) {
92
0
        return TxStateConfirmed{data.block_hash, /*height=*/-1, data.index};
93
0
    } else if (data.index == -1) {
94
0
        return TxStateBlockConflicted{data.block_hash, /*height=*/-1};
95
0
    }
96
0
    return data;
97
0
}
Unexecuted instantiation: fees.cpp:wallet::TxStateInterpretSerialized(wallet::TxStateUnrecognized)
Unexecuted instantiation: scriptpubkeyman.cpp:wallet::TxStateInterpretSerialized(wallet::TxStateUnrecognized)
Unexecuted instantiation: spend.cpp:wallet::TxStateInterpretSerialized(wallet::TxStateUnrecognized)
Unexecuted instantiation: dump.cpp:wallet::TxStateInterpretSerialized(wallet::TxStateUnrecognized)
Unexecuted instantiation: transaction.cpp:wallet::TxStateInterpretSerialized(wallet::TxStateUnrecognized)
Unexecuted instantiation: wallet.cpp:wallet::TxStateInterpretSerialized(wallet::TxStateUnrecognized)
Unexecuted instantiation: walletdb.cpp:wallet::TxStateInterpretSerialized(wallet::TxStateUnrecognized)
Unexecuted instantiation: interfaces.cpp:wallet::TxStateInterpretSerialized(wallet::TxStateUnrecognized)
Unexecuted instantiation: load.cpp:wallet::TxStateInterpretSerialized(wallet::TxStateUnrecognized)
Unexecuted instantiation: receive.cpp:wallet::TxStateInterpretSerialized(wallet::TxStateUnrecognized)
Unexecuted instantiation: export.cpp:wallet::TxStateInterpretSerialized(wallet::TxStateUnrecognized)
Unexecuted instantiation: feebumper.cpp:wallet::TxStateInterpretSerialized(wallet::TxStateUnrecognized)
Unexecuted instantiation: addresses.cpp:wallet::TxStateInterpretSerialized(wallet::TxStateUnrecognized)
Unexecuted instantiation: backup.cpp:wallet::TxStateInterpretSerialized(wallet::TxStateUnrecognized)
Unexecuted instantiation: coins.cpp:wallet::TxStateInterpretSerialized(wallet::TxStateUnrecognized)
Unexecuted instantiation: encrypt.cpp:wallet::TxStateInterpretSerialized(wallet::TxStateUnrecognized)
Unexecuted instantiation: signmessage.cpp:wallet::TxStateInterpretSerialized(wallet::TxStateUnrecognized)
Unexecuted instantiation: transactions.cpp:wallet::TxStateInterpretSerialized(wallet::TxStateUnrecognized)
Unexecuted instantiation: util.cpp:wallet::TxStateInterpretSerialized(wallet::TxStateUnrecognized)
Unexecuted instantiation: init.cpp:wallet::TxStateInterpretSerialized(wallet::TxStateUnrecognized)
98
99
//! Get TxState serialized block hash. Inverse of TxStateInterpretSerialized.
100
static inline uint256 TxStateSerializedBlockHash(const TxState& state)
101
0
{
102
0
    return std::visit(util::Overloaded{
103
0
        [](const TxStateInactive& inactive) { return inactive.abandoned ? uint256::ONE : uint256::ZERO; },
Unexecuted instantiation: wallet.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(wallet::TxStateInactive const&)::operator()(wallet::TxStateInactive const&) const
Unexecuted instantiation: walletdb.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(wallet::TxStateInactive const&)::operator()(wallet::TxStateInactive const&) const
104
0
        [](const TxStateInMempool& in_mempool) { return uint256::ZERO; },
Unexecuted instantiation: wallet.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(wallet::TxStateInMempool const&)::operator()(wallet::TxStateInMempool const&) const
Unexecuted instantiation: walletdb.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(wallet::TxStateInMempool const&)::operator()(wallet::TxStateInMempool const&) const
105
0
        [](const TxStateConfirmed& confirmed) { return confirmed.confirmed_block_hash; },
Unexecuted instantiation: wallet.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(wallet::TxStateConfirmed const&)::operator()(wallet::TxStateConfirmed const&) const
Unexecuted instantiation: walletdb.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(wallet::TxStateConfirmed const&)::operator()(wallet::TxStateConfirmed const&) const
106
0
        [](const TxStateBlockConflicted& conflicted) { return conflicted.conflicting_block_hash; },
Unexecuted instantiation: wallet.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(wallet::TxStateBlockConflicted const&)::operator()(wallet::TxStateBlockConflicted const&) const
Unexecuted instantiation: walletdb.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(wallet::TxStateBlockConflicted const&)::operator()(wallet::TxStateBlockConflicted const&) const
107
0
        [](const TxStateUnrecognized& unrecognized) { return unrecognized.block_hash; }
Unexecuted instantiation: wallet.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(wallet::TxStateUnrecognized const&)::operator()(wallet::TxStateUnrecognized const&) const
Unexecuted instantiation: walletdb.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(wallet::TxStateUnrecognized const&)::operator()(wallet::TxStateUnrecognized const&) const
108
0
    }, state);
109
0
}
Unexecuted instantiation: fees.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: scriptpubkeyman.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: spend.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: dump.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: transaction.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: wallet.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: walletdb.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: interfaces.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: load.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: receive.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: export.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: feebumper.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: addresses.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: backup.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: coins.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: encrypt.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: signmessage.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: transactions.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: util.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: init.cpp:wallet::TxStateSerializedBlockHash(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
110
111
//! Get TxState serialized block index. Inverse of TxStateInterpretSerialized.
112
static inline int TxStateSerializedIndex(const TxState& state)
113
0
{
114
0
    return std::visit(util::Overloaded{
115
0
        [](const TxStateInactive& inactive) { return inactive.abandoned ? -1 : 0; },
Unexecuted instantiation: wallet.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(wallet::TxStateInactive const&)::operator()(wallet::TxStateInactive const&) const
Unexecuted instantiation: walletdb.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(wallet::TxStateInactive const&)::operator()(wallet::TxStateInactive const&) const
116
0
        [](const TxStateInMempool& in_mempool) { return 0; },
Unexecuted instantiation: wallet.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(wallet::TxStateInMempool const&)::operator()(wallet::TxStateInMempool const&) const
Unexecuted instantiation: walletdb.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(wallet::TxStateInMempool const&)::operator()(wallet::TxStateInMempool const&) const
117
0
        [](const TxStateConfirmed& confirmed) { return confirmed.position_in_block; },
Unexecuted instantiation: wallet.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(wallet::TxStateConfirmed const&)::operator()(wallet::TxStateConfirmed const&) const
Unexecuted instantiation: walletdb.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(wallet::TxStateConfirmed const&)::operator()(wallet::TxStateConfirmed const&) const
118
0
        [](const TxStateBlockConflicted& conflicted) { return -1; },
Unexecuted instantiation: wallet.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(wallet::TxStateBlockConflicted const&)::operator()(wallet::TxStateBlockConflicted const&) const
Unexecuted instantiation: walletdb.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(wallet::TxStateBlockConflicted const&)::operator()(wallet::TxStateBlockConflicted const&) const
119
0
        [](const TxStateUnrecognized& unrecognized) { return unrecognized.index; }
Unexecuted instantiation: wallet.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(wallet::TxStateUnrecognized const&)::operator()(wallet::TxStateUnrecognized const&) const
Unexecuted instantiation: walletdb.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(wallet::TxStateUnrecognized const&)::operator()(wallet::TxStateUnrecognized const&) const
120
0
    }, state);
121
0
}
Unexecuted instantiation: fees.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: scriptpubkeyman.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: spend.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: dump.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: transaction.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: wallet.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: walletdb.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: interfaces.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: load.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: receive.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: export.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: feebumper.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: addresses.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: backup.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: coins.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: encrypt.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: signmessage.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: transactions.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: util.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
Unexecuted instantiation: init.cpp:wallet::TxStateSerializedIndex(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)
122
123
//! Return TxState or SyncTxState as a string for logging or debugging.
124
template<typename T>
125
std::string TxStateString(const T& state)
126
0
{
127
0
    return std::visit([](const auto& s) { return s.toString(); }, state);
Unexecuted instantiation: auto std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> wallet::TxStateString<std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized>>(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::operator()<wallet::TxStateConfirmed>(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&) const
Unexecuted instantiation: auto std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> wallet::TxStateString<std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized>>(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::operator()<wallet::TxStateInMempool>(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&) const
Unexecuted instantiation: auto std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> wallet::TxStateString<std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized>>(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::operator()<wallet::TxStateBlockConflicted>(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&) const
Unexecuted instantiation: auto std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> wallet::TxStateString<std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized>>(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::operator()<wallet::TxStateInactive>(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&) const
Unexecuted instantiation: auto std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> wallet::TxStateString<std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized>>(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::'lambda'(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&)::operator()<wallet::TxStateUnrecognized>(std::variant<wallet::TxStateConfirmed, wallet::TxStateInMempool, wallet::TxStateBlockConflicted, wallet::TxStateInactive, wallet::TxStateUnrecognized> const&) const
128
0
}
129
130
/**
131
 * Cachable amount subdivided into avoid reuse and all balances
132
 */
133
struct CachableAmount
134
{
135
    std::optional<CAmount> m_avoid_reuse_value;
136
    std::optional<CAmount> m_all_value;
137
    inline void Reset()
138
0
    {
139
0
        m_avoid_reuse_value.reset();
140
0
        m_all_value.reset();
141
0
    }
142
    void Set(bool avoid_reuse, CAmount value)
143
0
    {
144
0
        if (avoid_reuse) {
145
0
            m_avoid_reuse_value = value;
146
0
        } else {
147
0
            m_all_value = value;
148
0
        }
149
0
    }
150
    CAmount Get(bool avoid_reuse)
151
0
    {
152
0
        if (avoid_reuse) {
153
0
            Assert(m_avoid_reuse_value.has_value());
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
154
0
            return m_avoid_reuse_value.value();
155
0
        }
156
0
        Assert(m_all_value.has_value());
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
157
0
        return m_all_value.value();
158
0
    }
159
    bool IsCached(bool avoid_reuse)
160
0
    {
161
0
        if (avoid_reuse) return m_avoid_reuse_value.has_value();
162
0
        return m_all_value.has_value();
163
0
    }
164
};
165
166
167
/** Legacy class used for deserializing vtxPrev for backwards compatibility.
168
 * vtxPrev was removed in commit 93a18a3650292afbb441a47d1fa1b94aeb0164e3,
169
 * but old wallet.dat files may still contain vtxPrev vectors of CMerkleTxs.
170
 * These need to get deserialized for field alignment when deserializing
171
 * a CWalletTx, but the deserialized values are discarded.**/
172
class CMerkleTx
173
{
174
public:
175
    template<typename Stream>
176
    void Unserialize(Stream& s)
177
0
    {
178
0
        CTransactionRef tx;
179
0
        uint256 hashBlock;
180
0
        std::vector<uint256> vMerkleBranch;
181
0
        int nIndex;
182
183
0
        s >> TX_WITH_WITNESS(tx) >> hashBlock >> vMerkleBranch >> nIndex;
184
0
    }
185
};
186
187
/**
188
 * A transaction with a bunch of additional info that only the owner cares about.
189
 * It includes any unrecorded transactions needed to link it back to the block chain.
190
 */
191
class CWalletTx
192
{
193
public:
194
    // "from" and "message" are obsolete fields that could be set in
195
    // the UI prior to 2011 (removed in commit 4d9b223)
196
    // These fields are kept to avoid losing metadata.
197
    std::optional<std::string> m_from;
198
    std::optional<std::string> m_message;
199
    // Comment strings provided by the user
200
    std::optional<std::string> m_comment;
201
    std::optional<std::string> m_comment_to;
202
    std::optional<Txid> m_replaces_txid;
203
    std::optional<Txid> m_replaced_by_txid;
204
    // BIP 21 URI Messages
205
    std::vector<std::string> m_messages;
206
    // BIP 70 Payment Request (deprecated, field kept to preserve metadata from old wallets)
207
    std::vector<std::string> m_payment_requests;
208
    unsigned int nTimeReceived; //!< time received by this node
209
    /**
210
     * Stable timestamp that never changes, and reflects the order a transaction
211
     * was added to the wallet. Timestamp is based on the block time for a
212
     * transaction added as part of a block, or else the time when the
213
     * transaction was received if it wasn't part of a block, with the timestamp
214
     * adjusted in both cases so timestamp order matches the order transactions
215
     * were added to the wallet. More details can be found in
216
     * CWallet::ComputeTimeSmart().
217
     */
218
    unsigned int nTimeSmart;
219
    // Cached value for whether the transaction spends any inputs known to the wallet
220
    mutable std::optional<bool> m_cached_from_me{std::nullopt};
221
    int64_t nOrderPos; //!< position in ordered transaction list
222
    std::multimap<int64_t, CWalletTx*>::const_iterator m_it_wtxOrdered;
223
224
    // memory only
225
    enum AmountType { DEBIT, CREDIT, AMOUNTTYPE_ENUM_ELEMENTS };
226
    mutable CachableAmount m_amounts[AMOUNTTYPE_ENUM_ELEMENTS];
227
    /**
228
     * This flag is true if all m_amounts caches are empty. This is particularly
229
     * useful in places where MarkDirty is conditionally called and the
230
     * condition can be expensive and thus can be skipped if the flag is true.
231
     * See MarkDestinationsDirty.
232
     */
233
    mutable bool m_is_cache_empty{true};
234
    mutable bool fChangeCached;
235
    mutable CAmount nChangeCached;
236
237
0
    CWalletTx(CTransactionRef tx, const TxState& state) : tx(std::move(tx)), m_state(state)
238
0
    {
239
0
        Init();
240
0
    }
241
242
    void Init()
243
0
    {
244
0
        nTimeReceived = 0;
245
0
        nTimeSmart = 0;
246
0
        fChangeCached = false;
247
0
        nChangeCached = 0;
248
0
        nOrderPos = -1;
249
0
    }
250
251
    CTransactionRef tx;
252
    TxState m_state;
253
254
    // Set of mempool transactions that conflict
255
    // directly with the transaction, or that conflict
256
    // with an ancestor transaction. This set will be
257
    // empty if state is InMempool or Confirmed, but
258
    // can be nonempty if state is Inactive or
259
    // BlockConflicted.
260
    std::set<Txid> mempool_conflicts;
261
262
    // Track v3 mempool tx that spends from this tx
263
    // so that we don't try to create another unconfirmed child
264
    std::optional<Txid> truc_child_in_mempool;
265
266
    template<typename Stream>
267
    void Serialize(Stream& s) const
268
0
    {
269
0
        std::map<std::string, std::string> string_values;
270
0
        if (m_from) string_values["from"] = *m_from;
271
0
        if (m_message) string_values["message"] = *m_message;
272
0
        if (m_comment) string_values["comment"] = *m_comment;
273
0
        if (m_comment_to) string_values["to"] = *m_comment_to;
274
0
        if (m_replaces_txid) string_values["replaces_txid"] = m_replaces_txid->ToString();
275
0
        if (m_replaced_by_txid) string_values["replaced_by_txid"] = m_replaced_by_txid->ToString();
276
0
        string_values["fromaccount"] = "";
277
0
        if (nOrderPos != -1) string_values["n"] = util::ToString(nOrderPos);
278
0
        if (nTimeSmart) string_values["timesmart"] = strprintf("%u", nTimeSmart);
Line
Count
Source
1172
0
#define strprintf tfm::format
279
280
0
        std::vector<std::pair<std::string, std::string>> msgs_reqs;
281
0
        msgs_reqs.reserve(m_messages.size() + m_payment_requests.size());
282
0
        for (const std::string& msg : m_messages) {
283
0
            msgs_reqs.emplace_back("Message", msg);
284
0
        }
285
0
        for (const std::string& req : m_payment_requests) {
286
0
            msgs_reqs.emplace_back("PaymentRequest", req);
287
0
        }
288
289
0
        std::vector<uint8_t> dummy_vector1; // Used to be vMerkleBranch
290
0
        std::vector<uint8_t> dummy_vector2; // Used to be vtxPrev
291
0
        bool dummy_bool = false; // Used to be fFromMe, and fSpent
292
0
        uint32_t dummy_int = 0; // Used to be fTimeReceivedIsTxTime
293
0
        uint256 serializedHash = TxStateSerializedBlockHash(m_state);
294
0
        int serializedIndex = TxStateSerializedIndex(m_state);
295
0
        s << TX_WITH_WITNESS(tx) << serializedHash << dummy_vector1 << serializedIndex << dummy_vector2 << string_values << msgs_reqs << dummy_int << nTimeReceived << dummy_bool << dummy_bool;
296
0
    }
297
298
    template<typename Stream>
299
    void Unserialize(Stream& s)
300
0
    {
301
0
        Init();
302
303
0
        std::vector<uint256> dummy_vector1; // Used to be vMerkleBranch
304
0
        std::vector<CMerkleTx> dummy_vector2; // Used to be vtxPrev
305
0
        bool dummy_bool; // Used to be fFromMe, and fSpent
306
0
        uint32_t dummy_int; // Used to be fTimeReceivedIsTxTime
307
0
        uint256 serialized_block_hash;
308
0
        int serializedIndex;
309
0
        std::map<std::string, std::string> string_values;
310
0
        std::vector<std::pair<std::string, std::string>> msgs_reqs;
311
0
        s >> TX_WITH_WITNESS(tx) >> serialized_block_hash >> dummy_vector1 >> serializedIndex >> dummy_vector2 >> string_values >> msgs_reqs >> dummy_int >> nTimeReceived >> dummy_bool >> dummy_bool;
312
313
0
        m_state = TxStateInterpretSerialized({serialized_block_hash, serializedIndex});
314
315
0
        string_values.erase("fromaccount");
316
0
        string_values.erase("spent");
317
0
        for (const auto& [key, value] : string_values) {
318
0
            if (key == "n") nOrderPos = LocaleIndependentAtoi<int64_t>(value);
319
0
            else if (key == "timesmart") nTimeSmart = LocaleIndependentAtoi<int64_t>(value);
320
0
            else if (key == "from") m_from = value;
321
0
            else if (key == "message") m_message = value;
322
0
            else if (key == "comment") m_comment = value;
323
0
            else if (key == "to") m_comment_to = value;
324
0
            else if (key == "replaces_txid") m_replaces_txid = Txid::FromHex(value);
325
0
            else if (key == "replaced_by_txid") m_replaced_by_txid = Txid::FromHex(value);
326
0
            else {
327
0
                throw std::runtime_error("Unexpected value in CWalletTx strings value map");
328
0
            }
329
0
        }
330
331
0
        for (const auto& [type, data] : msgs_reqs) {
332
0
            if (type == "Message") m_messages.emplace_back(data);
333
0
            else if (type == "PaymentRequest") m_payment_requests.emplace_back(data);
334
0
            else {
335
0
                throw std::runtime_error("Unknown type in CWalletTx messages and requests vector");
336
0
            }
337
0
        }
338
0
    }
339
340
    void SetTx(CTransactionRef arg)
341
0
    {
342
0
        tx = std::move(arg);
343
0
    }
344
345
    //! make sure balances are recalculated
346
    void MarkDirty()
347
0
    {
348
0
        m_amounts[DEBIT].Reset();
349
0
        m_amounts[CREDIT].Reset();
350
0
        fChangeCached = false;
351
0
        m_is_cache_empty = true;
352
0
        m_cached_from_me = std::nullopt;
353
0
    }
354
355
    /** True if only scriptSigs are different */
356
    bool IsEquivalentTo(const CWalletTx& tx) const;
357
358
    bool InMempool() const;
359
360
    int64_t GetTxTime() const;
361
362
0
    template<typename T> const T* state() const { return std::get_if<T>(&m_state); }
Unexecuted instantiation: wallet::TxStateInactive const* wallet::CWalletTx::state<wallet::TxStateInactive>() const
Unexecuted instantiation: wallet::TxStateBlockConflicted const* wallet::CWalletTx::state<wallet::TxStateBlockConflicted>() const
Unexecuted instantiation: wallet::TxStateConfirmed const* wallet::CWalletTx::state<wallet::TxStateConfirmed>() const
Unexecuted instantiation: wallet::TxStateInMempool const* wallet::CWalletTx::state<wallet::TxStateInMempool>() const
363
0
    template<typename T> T* state() { return std::get_if<T>(&m_state); }
Unexecuted instantiation: wallet::TxStateConfirmed* wallet::CWalletTx::state<wallet::TxStateConfirmed>()
Unexecuted instantiation: wallet::TxStateBlockConflicted* wallet::CWalletTx::state<wallet::TxStateBlockConflicted>()
Unexecuted instantiation: wallet::TxStateInMempool* wallet::CWalletTx::state<wallet::TxStateInMempool>()
364
365
    //! Update transaction state when attaching to a chain, filling in heights
366
    //! of conflicted and confirmed blocks
367
    void updateState(interfaces::Chain& chain);
368
369
0
    bool isAbandoned() const { return state<TxStateInactive>() && state<TxStateInactive>()->abandoned; }
370
0
    bool isMempoolConflicted() const { return !mempool_conflicts.empty(); }
371
0
    bool isBlockConflicted() const { return state<TxStateBlockConflicted>(); }
372
0
    bool isInactive() const { return state<TxStateInactive>(); }
373
0
    bool isUnconfirmed() const { return !isAbandoned() && !isBlockConflicted() && !isMempoolConflicted() && !isConfirmed(); }
374
0
    bool isConfirmed() const { return state<TxStateConfirmed>(); }
375
0
    const Txid& GetHash() const LIFETIMEBOUND { return tx->GetHash(); }
376
0
    const Wtxid& GetWitnessHash() const LIFETIMEBOUND { return tx->GetWitnessHash(); }
377
0
    bool IsCoinBase() const { return tx->IsCoinBase(); }
378
379
private:
380
    // Disable copying of CWalletTx objects to prevent bugs where instances get
381
    // copied in and out of the mapWallet map, and fields are updated in the
382
    // wrong copy.
383
    CWalletTx(const CWalletTx&) = default;
384
0
    CWalletTx& operator=(const CWalletTx&) = default;
385
public:
386
    // Instead have an explicit copy function
387
    void CopyFrom(const CWalletTx&);
388
};
389
390
struct WalletTxOrderComparator {
391
    bool operator()(const CWalletTx* a, const CWalletTx* b) const
392
0
    {
393
0
        return a->nOrderPos < b->nOrderPos;
394
0
    }
395
};
396
397
class WalletTXO
398
{
399
private:
400
    const CWalletTx& m_wtx;
401
    const CTxOut& m_output;
402
403
public:
404
    WalletTXO(const CWalletTx& wtx, const CTxOut& output)
405
0
    : m_wtx(wtx),
406
0
    m_output(output)
407
0
    {
408
0
        Assume(std::ranges::find(wtx.tx->vout, output) != wtx.tx->vout.end());
Line
Count
Source
128
0
#define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val)
409
0
    }
410
411
0
    const CWalletTx& GetWalletTx() const { return m_wtx; }
412
413
0
    const CTxOut& GetTxOut() const { return m_output; }
414
};
415
} // namespace wallet
416
417
#endif // BITCOIN_WALLET_TRANSACTION_H