1 // Copyright (c) 2011-2018 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_QT_TRANSACTIONRECORD_H
6 #define BITCOIN_QT_TRANSACTIONRECORD_H
7 
8 #include <amount.h>
9 #include <uint256.h>
10 
11 #include <QList>
12 #include <QString>
13 
14 namespace interfaces {
15 class Node;
16 class Wallet;
17 struct WalletTx;
18 struct WalletTxStatus;
19 }
20 
21 /** UI model for transaction status. The transaction status is the part of a transaction that will change over time.
22  */
23 class TransactionStatus
24 {
25 public:
TransactionStatus()26     TransactionStatus() : countsForBalance(false), sortKey(""),
27                           matures_in(0), status(Unconfirmed), depth(0), open_for(0)
28     { }
29 
30     enum Status {
31         Confirmed,          /**< Have 6 or more confirmations (normal tx) or fully mature (mined tx) **/
32         /// Normal (sent/received) transactions
33         OpenUntilDate,      /**< Transaction not yet final, waiting for date */
34         OpenUntilBlock,     /**< Transaction not yet final, waiting for block */
35         Unconfirmed,        /**< Not yet mined into a block **/
36         Confirming,         /**< Confirmed, but waiting for the recommended number of confirmations **/
37         Conflicted,         /**< Conflicts with other transaction or mempool **/
38         Abandoned,          /**< Abandoned from the wallet **/
39         /// Generated (mined) transactions
40         Immature,           /**< Mined but waiting for maturity */
41         NotAccepted         /**< Mined but not accepted */
42     };
43 
44     /// Transaction counts towards available balance
45     bool countsForBalance;
46     /// Sorting key based on status
47     std::string sortKey;
48 
49     /** @name Generated (mined) transactions
50        @{*/
51     int matures_in;
52     /**@}*/
53 
54     /** @name Reported status
55        @{*/
56     Status status;
57     qint64 depth;
58     qint64 open_for; /**< Timestamp if status==OpenUntilDate, otherwise number
59                       of additional blocks that need to be mined before
60                       finalization */
61     /**@}*/
62 
63     /** Current block hash (to know whether cached status is still valid) */
64     uint256 m_cur_block_hash{};
65 
66     bool needsUpdate;
67 };
68 
69 /** UI model for a transaction. A core transaction can be represented by multiple UI transactions if it has
70     multiple outputs.
71  */
72 class TransactionRecord
73 {
74 public:
75     enum Type
76     {
77         Other,
78         Generated,
79         SendToAddress,
80         SendToOther,
81         RecvWithAddress,
82         RecvFromOther,
83         SendToSelf
84     };
85 
86     /** Number of confirmation recommended for accepting a transaction */
87     static const int RecommendedNumConfirmations = 6;
88 
TransactionRecord()89     TransactionRecord():
90             hash(), time(0), type(Other), address(""), debit(0), credit(0), idx(0)
91     {
92     }
93 
TransactionRecord(uint256 _hash,qint64 _time)94     TransactionRecord(uint256 _hash, qint64 _time):
95             hash(_hash), time(_time), type(Other), address(""), debit(0),
96             credit(0), idx(0)
97     {
98     }
99 
TransactionRecord(uint256 _hash,qint64 _time,Type _type,const std::string & _address,const CAmount & _debit,const CAmount & _credit)100     TransactionRecord(uint256 _hash, qint64 _time,
101                 Type _type, const std::string &_address,
102                 const CAmount& _debit, const CAmount& _credit):
103             hash(_hash), time(_time), type(_type), address(_address), debit(_debit), credit(_credit),
104             idx(0)
105     {
106     }
107 
108     /** Decompose CWallet transaction to model transaction records.
109      */
110     static bool showTransaction();
111     static QList<TransactionRecord> decomposeTransaction(const interfaces::WalletTx& wtx);
112 
113     /** @name Immutable transaction attributes
114       @{*/
115     uint256 hash;
116     qint64 time;
117     Type type;
118     std::string address;
119     CAmount debit;
120     CAmount credit;
121     /**@}*/
122 
123     /** Subtransaction index, for sort key */
124     int idx;
125 
126     /** Status: can change with block chain update */
127     TransactionStatus status;
128 
129     /** Whether the transaction was sent/received with a watch-only address */
130     bool involvesWatchAddress;
131 
132     /** Return the unique identifier for this transaction (part) */
133     QString getTxHash() const;
134 
135     /** Return the output index of the subtransaction  */
136     int getOutputIndex() const;
137 
138     /** Update status from core wallet tx.
139      */
140     void updateStatus(const interfaces::WalletTxStatus& wtx, const uint256& block_hash, int numBlocks, int64_t block_time);
141 
142     /** Return whether a status update is needed.
143      */
144     bool statusUpdateNeeded(const uint256& block_hash) const;
145 };
146 
147 #endif // BITCOIN_QT_TRANSACTIONRECORD_H
148