Debian Codegraph @

src/wallet/api/transaction_history.cpp

raw symbols SCIP
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30
31
32 #include "transaction_history.h"
33 #include "transaction_info.h"
34 #include "wallet.h"
35
36 #include "crypto/hash.h"
37 #include "wallet/wallet2.h"
38
39
40 #include <string>
41 #include <list>
42
43 using namespace epee;
44
45 namespace Monero {
46
47 TransactionHistory::~TransactionHistory() {}
48
49
50 TransactionHistoryImpl::TransactionHistoryImpl(WalletImpl *wallet)
51 : m_wallet(wallet)
52 {
53
54 }
55
56 TransactionHistoryImpl::~TransactionHistoryImpl()
57 {
58 for (auto t : m_history)
59 delete t;
60 }
61
62 int TransactionHistoryImpl::count() const
63 {
64 boost::shared_lock<boost::shared_mutex> lock(m_historyMutex);
65 int result = m_history.size();
66 return result;
67 }
68
69 TransactionInfo *TransactionHistoryImpl::transaction(int index) const
70 {
71 boost::shared_lock<boost::shared_mutex> lock(m_historyMutex);
72 // sanity check
73 if (index < 0)
74 return nullptr;
75 unsigned index_ = static_cast<unsigned>(index);
76 return index_ < m_history.size() ? m_history[index_] : nullptr;
77 }
78
79 TransactionInfo *TransactionHistoryImpl::transaction(const std::string &id) const
80 {
81 boost::shared_lock<boost::shared_mutex> lock(m_historyMutex);
82 auto itr = std::find_if(m_history.begin(), m_history.end(),
83 [&](const TransactionInfo * ti) {
84 return ti->hash() == id;
85 });
86 return itr != m_history.end() ? *itr : nullptr;
87 }
88
89 std::vector<TransactionInfo *> TransactionHistoryImpl::getAll() const
90 {
91 boost::shared_lock<boost::shared_mutex> lock(m_historyMutex);
92 return m_history;
93 }
94
95 void TransactionHistoryImpl::setTxNote(const std::string &txid, const std::string &note)
96 {
97 cryptonote::blobdata txid_data;
98 if(!epee::string_tools::parse_hexstr_to_binbuff(txid, txid_data) || txid_data.size() != sizeof(crypto::hash))
99 return;
100 const crypto::hash htxid = *reinterpret_cast<const crypto::hash*>(txid_data.data());
101
102 m_wallet->m_wallet->set_tx_note(htxid, note);
103 refresh();
104 }
105
106 void TransactionHistoryImpl::refresh()
107 {
108 // multithreaded access:
109 // boost::lock_guard<boost::mutex> guarg(m_historyMutex);
110 // for "write" access, locking exclusively
111 boost::unique_lock<boost::shared_mutex> lock(m_historyMutex);
112
113 // TODO: configurable values;
114 uint64_t min_height = 0;
115 uint64_t max_height = (uint64_t)-1;
116 uint64_t wallet_height = m_wallet->blockChainHeight();
117
118 // delete old transactions;
119 for (auto t : m_history)
120 delete t;
121 m_history.clear();
122
123 // transactions are stored in wallet2:
124 // - confirmed_transfer_details - out transfers
125 // - unconfirmed_transfer_details - pending out transfers
126 // - payment_details - input transfers
127
128 // payments are "input transactions";
129 // one input transaction contains only one transfer. e.g. <transaction_id> - <100XMR>
130
131 std::list<std::pair<crypto::hash, tools::wallet2::payment_details>> in_payments;
132 m_wallet->m_wallet->get_payments(in_payments, min_height, max_height);
133 for (std::list<std::pair<crypto::hash, tools::wallet2::payment_details>>::const_iterator i = in_payments.begin(); i != in_payments.end(); ++i) {
134 const tools::wallet2::payment_details &pd = i->second;
135 std::string payment_id = string_tools::pod_to_hex(i->first);
136 if (payment_id.substr(16).find_first_not_of('0') == std::string::npos)
137 payment_id = payment_id.substr(0,16);
138 TransactionInfoImpl * ti = new TransactionInfoImpl();
139 ti->m_paymentid = payment_id;
140 ti->m_coinbase = pd.m_coinbase;
141 ti->m_amount = pd.m_amount;
142 ti->m_fee = pd.m_fee;
143 ti->m_direction = TransactionInfo::Direction_In;
144 ti->m_hash = string_tools::pod_to_hex(pd.m_tx_hash);
145 ti->m_blockheight = pd.m_block_height;
146 ti->m_description = m_wallet->m_wallet->get_tx_note(pd.m_tx_hash);
147 ti->m_subaddrIndex = { pd.m_subaddr_index.minor };
148 ti->m_subaddrAccount = pd.m_subaddr_index.major;
149 ti->m_label = m_wallet->m_wallet->get_subaddress_label(pd.m_subaddr_index);
150 ti->m_timestamp = pd.m_timestamp;
151 ti->m_confirmations = (wallet_height > pd.m_block_height) ? wallet_height - pd.m_block_height : 0;
152 ti->m_unlock_time = pd.m_unlock_time;
153 m_history.push_back(ti);
154
155 }
156
157 // confirmed output transactions
158 // one output transaction may contain more than one money transfer, e.g.
159 // <transaction_id>:
160 // transfer1: 100XMR to <address_1>
161 // transfer2: 50XMR to <address_2>
162 // fee: fee charged per transaction
163 //
164
165 std::list<std::pair<crypto::hash, tools::wallet2::confirmed_transfer_details>> out_payments;
166 m_wallet->m_wallet->get_payments_out(out_payments, min_height, max_height);
167
168 for (std::list<std::pair<crypto::hash, tools::wallet2::confirmed_transfer_details>>::const_iterator i = out_payments.begin();
169 i != out_payments.end(); ++i) {
170
171 const crypto::hash &hash = i->first;
172 const tools::wallet2::confirmed_transfer_details &pd = i->second;
173
174 uint64_t change = pd.m_change == (uint64_t)-1 ? 0 : pd.m_change; // change may not be known
175 uint64_t fee = pd.m_amount_in - pd.m_amount_out;
176
177
178 std::string payment_id = string_tools::pod_to_hex(i->second.m_payment_id);
179 if (payment_id.substr(16).find_first_not_of('0') == std::string::npos)
180 payment_id = payment_id.substr(0,16);
181
182
183 TransactionInfoImpl * ti = new TransactionInfoImpl();
184 ti->m_paymentid = payment_id;
185 ti->m_amount = pd.m_amount_in - change - fee;
186 ti->m_fee = fee;
187 ti->m_direction = TransactionInfo::Direction_Out;
188 ti->m_hash = string_tools::pod_to_hex(hash);
189 ti->m_blockheight = pd.m_block_height;
190 ti->m_description = m_wallet->m_wallet->get_tx_note(hash);
191 ti->m_subaddrIndex = pd.m_subaddr_indices;
192 ti->m_subaddrAccount = pd.m_subaddr_account;
193 ti->m_label = pd.m_subaddr_indices.size() == 1 ? m_wallet->m_wallet->get_subaddress_label({pd.m_subaddr_account, *pd.m_subaddr_indices.begin()}) : "";
194 ti->m_timestamp = pd.m_timestamp;
195 ti->m_confirmations = (wallet_height > pd.m_block_height) ? wallet_height - pd.m_block_height : 0;
196
197 // single output transaction might contain multiple transfers
198 for (const auto &d: pd.m_dests) {
199 ti->m_transfers.push_back({d.amount, d.address(m_wallet->m_wallet->nettype(), pd.m_payment_id)});
200 }
201
202 m_history.push_back(ti);
203 }
204
205 // unconfirmed output transactions
206 std::list<std::pair<crypto::hash, tools::wallet2::unconfirmed_transfer_details>> upayments_out;
207 m_wallet->m_wallet->get_unconfirmed_payments_out(upayments_out);
208 for (std::list<std::pair<crypto::hash, tools::wallet2::unconfirmed_transfer_details>>::const_iterator i = upayments_out.begin(); i != upayments_out.end(); ++i) {
209 const tools::wallet2::unconfirmed_transfer_details &pd = i->second;
210 const crypto::hash &hash = i->first;
211 uint64_t amount = pd.m_amount_in;
212 uint64_t fee = amount - pd.m_amount_out;
213 std::string payment_id = string_tools::pod_to_hex(i->second.m_payment_id);
214 if (payment_id.substr(16).find_first_not_of('0') == std::string::npos)
215 payment_id = payment_id.substr(0,16);
216 bool is_failed = pd.m_state == tools::wallet2::unconfirmed_transfer_details::failed;
217
218 TransactionInfoImpl * ti = new TransactionInfoImpl();
219 ti->m_paymentid = payment_id;
220 ti->m_amount = amount - pd.m_change - fee;
221 ti->m_fee = fee;
222 ti->m_direction = TransactionInfo::Direction_Out;
223 ti->m_failed = is_failed;
224 ti->m_pending = true;
225 ti->m_hash = string_tools::pod_to_hex(hash);
226 ti->m_description = m_wallet->m_wallet->get_tx_note(hash);
227 ti->m_subaddrIndex = pd.m_subaddr_indices;
228 ti->m_subaddrAccount = pd.m_subaddr_account;
229 ti->m_label = pd.m_subaddr_indices.size() == 1 ? m_wallet->m_wallet->get_subaddress_label({pd.m_subaddr_account, *pd.m_subaddr_indices.begin()}) : "";
230 ti->m_timestamp = pd.m_timestamp;
231 ti->m_confirmations = 0;
232 for (const auto &d : pd.m_dests)
233 {
234 ti->m_transfers.push_back({d.amount, d.address(m_wallet->m_wallet->nettype(), pd.m_payment_id)});
235 }
236 m_history.push_back(ti);
237 }
238
239
240 // unconfirmed payments (tx pool)
241 std::list<std::pair<crypto::hash, tools::wallet2::pool_payment_details>> upayments;
242 m_wallet->m_wallet->get_unconfirmed_payments(upayments);
243 for (std::list<std::pair<crypto::hash, tools::wallet2::pool_payment_details>>::const_iterator i = upayments.begin(); i != upayments.end(); ++i) {
244 const tools::wallet2::payment_details &pd = i->second.m_pd;
245 std::string payment_id = string_tools::pod_to_hex(i->first);
246 if (payment_id.substr(16).find_first_not_of('0') == std::string::npos)
247 payment_id = payment_id.substr(0,16);
248 TransactionInfoImpl * ti = new TransactionInfoImpl();
249 ti->m_paymentid = payment_id;
250 ti->m_amount = pd.m_amount;
251 ti->m_direction = TransactionInfo::Direction_In;
252 ti->m_hash = string_tools::pod_to_hex(pd.m_tx_hash);
253 ti->m_blockheight = pd.m_block_height;
254 ti->m_description = m_wallet->m_wallet->get_tx_note(pd.m_tx_hash);
255 ti->m_pending = true;
256 ti->m_subaddrIndex = { pd.m_subaddr_index.minor };
257 ti->m_subaddrAccount = pd.m_subaddr_index.major;
258 ti->m_label = m_wallet->m_wallet->get_subaddress_label(pd.m_subaddr_index);
259 ti->m_timestamp = pd.m_timestamp;
260 ti->m_confirmations = 0;
261 m_history.push_back(ti);
262
263 LOG_PRINT_L1(__FUNCTION__ << ": Unconfirmed payment found " << pd.m_amount);
264 }
265
266 }
267
268 } // namespace