5#ifndef BITCOIN_WALLET_TRANSACTION_H
6#define BITCOIN_WALLET_TRANSACTION_H
81using TxState = std::variant<TxStateConfirmed, TxStateInMempool, TxStateBlockConflicted, TxStateInactive, TxStateUnrecognized>;
84using SyncTxState = std::variant<TxStateConfirmed, TxStateInMempool, TxStateInactive>;
93 }
else if (
data.index >= 0) {
95 }
else if (
data.index == -1) {
107 [](
const TxStateConfirmed& confirmed) {
return confirmed.confirmed_block_hash; },
117 [](
const TxStateInactive& inactive) {
return inactive.abandoned ? -1 : 0; },
119 [](
const TxStateConfirmed& confirmed) {
return confirmed.position_in_block; },
129 return std::visit([](
const auto&
s) {
return s.toString(); }, state);
177 template<
typename Stream>
182 std::vector<uint256> vMerkleBranch;
243 m_txs.emplace(tx->GetWitnessHash(), std::move(tx));
247 template <
typename Stream>
252 for (
const auto& [wtxid, tx] : variants) {
253 if (tx->GetHash() != canonical_txid)
throw std::runtime_error(
"variant txid does not match wallet txid");
256 m_txs.insert(variants.begin(), variants.end());
274 template<
typename Stream>
277 std::map<std::string, std::string> string_values;
284 string_values[
"fromaccount"] =
"";
288 std::vector<std::pair<std::string, std::string>> msgs_reqs;
291 msgs_reqs.emplace_back(
"Message",
msg);
294 msgs_reqs.emplace_back(
"PaymentRequest", req);
297 std::vector<uint8_t> dummy_vector1;
298 std::vector<uint8_t> dummy_vector2;
299 bool dummy_bool =
false;
300 uint32_t dummy_int = 0;
303 s <<
TX_WITH_WITNESS(
GetTx()) << serializedHash << dummy_vector1 << serializedIndex << dummy_vector2 << string_values << msgs_reqs << dummy_int <<
nTimeReceived << dummy_bool << dummy_bool;
306 template<
typename Stream>
311 std::vector<uint256> dummy_vector1;
312 std::vector<CMerkleTx> dummy_vector2;
317 std::map<std::string, std::string> string_values;
318 std::vector<std::pair<std::string, std::string>> msgs_reqs;
320 s >>
TX_WITH_WITNESS(canonical_tx) >> serialized_block_hash >> dummy_vector1 >> serializedIndex >> dummy_vector2 >> string_values >> msgs_reqs >> dummy_int >>
nTimeReceived >> dummy_bool >> dummy_bool;
326 string_values.erase(
"fromaccount");
327 string_values.erase(
"spent");
328 for (
const auto& [key, value] : string_values) {
329 if (key ==
"n")
nOrderPos = LocaleIndependentAtoi<int64_t>(value);
330 else if (key ==
"timesmart")
nTimeSmart = LocaleIndependentAtoi<int64_t>(value);
331 else if (key ==
"from")
m_from = value;
332 else if (key ==
"message")
m_message = value;
333 else if (key ==
"comment")
m_comment = value;
338 throw std::runtime_error(
"Unexpected value in CWalletTx strings value map");
342 for (
const auto& [type,
data] : msgs_reqs) {
346 throw std::runtime_error(
"Unknown type in CWalletTx messages and requests vector");
377 template<
typename T>
const T*
state()
const {
return std::get_if<T>(&
m_state); }
384 bool isAbandoned()
const {
return state<TxStateInactive>() && state<TxStateInactive>()->abandoned; }
394 const std::map<Wtxid, CTransactionRef>&
GetTxs()
const {
return m_txs; }
423 std::map<Wtxid, CTransactionRef>
m_txs;
448 Assume(std::ranges::find(wtx.
GetTx()->vout, output) != wtx.
GetTx()->vout.end());
int64_t CAmount
Amount in satoshis (Can be negative)
#define Assert(val)
Identity function.
#define Assume(val)
Assume is the identity function.
An output of a transaction.
std::string ToString() const
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
static std::optional< transaction_identifier > FromHex(std::string_view hex)
static const uint256 ZERO
Legacy class used for deserializing vtxPrev for backwards compatibility.
void Unserialize(Stream &s)
A transaction with a bunch of additional info that only the owner cares about.
bool Update(CTransactionRef tx, const TxState &new_state, WalletBatch &batch, bool metadata_changed)
bool isBlockConflicted() const
CWalletTx(CWalletTx &&)=default
std::vector< std::string > m_messages
const Txid & GetHash() const LIFETIMEBOUND
std::set< Txid > mempool_conflicts
std::optional< Txid > m_replaces_txid
std::map< Wtxid, CTransactionRef > m_txs
void updateState(interfaces::Chain &chain)
Update transaction state when attaching to a chain, filling in heights of conflicted and confirmed bl...
@ AMOUNTTYPE_ENUM_ELEMENTS
std::optional< std::string > m_comment_to
void Serialize(Stream &s) const
const std::map< Wtxid, CTransactionRef > & GetTxs() const
std::optional< std::string > m_message
int64_t nOrderPos
position in ordered transaction list
bool isUnconfirmed() const
std::optional< bool > m_cached_from_me
std::optional< std::string > m_comment
unsigned int nTimeReceived
time received by this node
std::optional< std::string > m_from
std::optional< Txid > m_replaced_by_txid
void Unserialize(Stream &s)
bool isMempoolConflicted() const
CWalletTx & operator=(const CWalletTx &)=delete
CWalletTx(deserialize_type, Stream &s, const std::map< Wtxid, CTransactionRef > &variants)
CachableAmount m_amounts[AMOUNTTYPE_ENUM_ELEMENTS]
std::optional< Txid > truc_child_in_mempool
CWalletTx(const CWalletTx &)=delete
std::vector< std::string > m_payment_requests
int64_t GetTxTime() const
CTransactionRef GetTx() const
CWalletTx(CTransactionRef tx, const TxState &state)
bool IsMalleation(const CWalletTx &tx) const
True if tx is a malleation of this, i.e.
void RecomputeCanonical()
Set m_canonical_wtxid to the best variant under the unconfirmed rule (witnessed preferred,...
bool m_is_cache_empty
This flag is true if all m_amounts caches are empty.
std::multimap< int64_t, CWalletTx * >::const_iterator m_it_wtxOrdered
unsigned int nTimeSmart
Stable timestamp that never changes, and reflects the order a transaction was added to the wallet.
const Wtxid & GetWitnessHash() const LIFETIMEBOUND
void MarkDirty()
make sure balances are recalculated
Access to the wallet database.
WalletTXO(const CWalletTx &wtx, const CTxOut &output)
const CWalletTx & GetWalletTx() const
const CTxOut & GetTxOut() const
static const PrecomputedData data
Precomputed COutPoint and CCoins values.
#define T(expected, seed, data)
std::string ToString(const T &t)
Locale-independent version of std::to_string.
std::variant< TxStateConfirmed, TxStateInMempool, TxStateInactive > SyncTxState
Subset of states transaction sync logic is implemented to handle.
std::variant< TxStateConfirmed, TxStateInMempool, TxStateBlockConflicted, TxStateInactive, TxStateUnrecognized > TxState
All possible CWalletTx states.
static int TxStateSerializedIndex(const TxState &state)
Get TxState serialized block index. Inverse of TxStateInterpretSerialized.
static TxState TxStateInterpretSerialized(TxStateUnrecognized data)
Try to interpret deserialized TxStateUnrecognized data as a recognized state.
static uint256 TxStateSerializedBlockHash(const TxState &state)
Get TxState serialized block hash. Inverse of TxStateInterpretSerialized.
std::string TxStateString(const T &state)
Return TxState or SyncTxState as a string for logging or debugging.
constexpr TransactionSerParams TX_WITH_WITNESS
std::shared_ptr< const CTransaction > CTransactionRef
Dummy data type to identify deserializing constructors.
Overloaded helper for std::visit.
Cachable amount subdivided into avoid reuse and all balances.
std::optional< CAmount > m_avoid_reuse_value
std::optional< CAmount > m_all_value
bool IsCached(bool avoid_reuse)
CAmount Get(bool avoid_reuse)
void Set(bool avoid_reuse, CAmount value)
State of rejected transaction that conflicts with a confirmed block.
std::string toString() const
int conflicting_block_height
uint256 conflicting_block_hash
TxStateBlockConflicted(const uint256 &block_hash, int height)
State of transaction confirmed in a block.
int confirmed_block_height
std::string toString() const
TxStateConfirmed(const uint256 &block_hash, int height, int index)
uint256 confirmed_block_hash
State of transaction added to mempool.
std::string toString() const
State of transaction not confirmed or conflicting with a known block and not in the mempool.
std::string toString() const
TxStateInactive(bool abandoned=false)
State of transaction loaded in an unrecognized state with unexpected hash or index values.
TxStateUnrecognized(const uint256 &block_hash, int index)
std::string toString() const
bool operator()(const CWalletTx *a, const CWalletTx *b) const
is a home for public enum and struct type definitions that are used by internally by wallet code,...