Debian Codegraph @

tests/core_tests/wallet_tools.cpp

raw symbols SCIP
1 //
2 // Created by Dusan Klinec on 2019-02-28.
3 //
4
5 #include "wallet_tools.h"
6 #include <random>
7
8 using namespace std;
9 using namespace epee;
10 using namespace crypto;
11 using namespace cryptonote;
12
13 void wallet_accessor_test::set_account(tools::wallet2 * wallet, cryptonote::account_base& account)
14 {
15 wallet->clear();
16 wallet->m_account = account;
17
18 wallet->m_key_device_type = account.get_device().get_type();
19 wallet->m_account_public_address = account.get_keys().m_account_address;
20 wallet->m_watch_only = false;
21 wallet->m_multisig = false;
22 wallet->m_multisig_threshold = 0;
23 wallet->m_multisig_signers.clear();
24 wallet->m_device_name = account.get_device().get_name();
25
26 wallet->m_subaddress_lookahead_major = 5;
27 wallet->m_subaddress_lookahead_minor = 20;
28
29 wallet->setup_new_blockchain(); // generates also subadress register
30 }
31
32 void wallet_accessor_test::process_parsed_blocks(tools::wallet2 * wallet, uint64_t start_height, const std::vector<cryptonote::block_complete_entry> &blocks, const std::vector<tools::wallet2::parsed_block> &parsed_blocks, uint64_t& blocks_added)
33 {
34 wallet->process_parsed_blocks(start_height, blocks, parsed_blocks, blocks_added);
35 }
36
37 void wallet_tools::process_transactions(tools::wallet2 * wallet, const std::vector<test_event_entry>& events, const cryptonote::block& blk_head, block_tracker &bt, const boost::optional<crypto::hash>& blk_tail)
38 {
39 map_hash2tx_t mtx;
40 std::vector<const cryptonote::block*> blockchain;
41 find_block_chain(events, blockchain, mtx, get_block_hash(blk_head));
42
43 if (blk_tail){
44 trim_block_chain(blockchain, blk_tail.get());
45 }
46
47 process_transactions(wallet, blockchain, mtx, bt);
48 }
49
50 void wallet_tools::process_transactions(tools::wallet2 * wallet, const std::vector<const cryptonote::block*>& blockchain, const map_hash2tx_t & mtx, block_tracker &bt)
51 {
52 uint64_t start_height=0, blocks_added=0;
53 std::vector<cryptonote::block_complete_entry> v_bche;
54 std::vector<tools::wallet2::parsed_block> v_parsed_block;
55
56 v_bche.reserve(blockchain.size());
57 v_parsed_block.reserve(blockchain.size());
58
59 size_t idx = 0;
60 for(auto bl : blockchain)
61 {
62 idx += 1;
63 uint64_t height;
64 v_bche.emplace_back();
65 v_parsed_block.emplace_back();
66
67 wallet_tools::gen_block_data(bt, bl, mtx, v_bche.back(), v_parsed_block.back(), idx == 1 ? start_height : height);
68 }
69
70 if (wallet)
71 wallet_accessor_test::process_parsed_blocks(wallet, start_height, v_bche, v_parsed_block, blocks_added);
72 }
73
74 bool wallet_tools::fill_tx_sources(tools::wallet2 * wallet, std::vector<cryptonote::tx_source_entry>& sources, size_t mixin, const boost::optional<size_t>& num_utxo, const boost::optional<uint64_t>& min_amount, block_tracker &bt, std::vector<size_t> &selected, uint64_t cur_height, ssize_t offset, int step, const boost::optional<fnc_accept_tx_source_t>& fnc_accept)
75 {
76 CHECK_AND_ASSERT_THROW_MES(step != 0, "Step is zero");
77 sources.clear();
78
79 auto & transfers = wallet_accessor_test::get_transfers(wallet);
80 std::unordered_set<size_t> selected_idx;
81 std::unordered_set<crypto::key_image> selected_kis;
82 const size_t ntrans = wallet->get_num_transfer_details();
83 size_t roffset = offset >= 0 ? offset : ntrans - offset - 1;
84 size_t iters = 0;
85 uint64_t sum = 0;
86 size_t cur_utxo = 0;
87 bool abort = false;
88 unsigned brk_cond = 0;
89 unsigned brk_thresh = num_utxo && min_amount ? 2 : (num_utxo || min_amount ? 1 : 0);
90
91 #define EVAL_BRK_COND() do { \
92 brk_cond = 0; \
93 if (num_utxo && num_utxo.get() <= cur_utxo) \
94 brk_cond += 1; \
95 if (min_amount && min_amount.get() <= sum) \
96 brk_cond += 1; \
97 } while(0)
98
99 for(ssize_t i = roffset; iters < ntrans && !abort; i += step, ++iters)
100 {
101 EVAL_BRK_COND();
102 if (brk_cond >= brk_thresh)
103 break;
104
105 i = i < 0 ? (i + ntrans) : i % ntrans;
106 auto & td = transfers[i];
107 if (td.m_spent)
108 continue;
109 if (td.m_block_height + CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW > cur_height)
110 continue;
111 if (selected_idx.find((size_t)i) != selected_idx.end()){
112 MERROR("Should not happen (selected_idx not found): " << i);
113 continue;
114 }
115 if (selected_kis.find(td.m_key_image) != selected_kis.end()){
116 MERROR("Should not happen (selected KI): " << i << "ki: " << dump_keys(td.m_key_image.data));
117 continue;
118 }
119
120 try {
121 cryptonote::tx_source_entry src;
122 wallet_tools::gen_tx_src(mixin, cur_height, td, src, bt);
123
124 // Acceptor function
125 if (fnc_accept){
126 tx_source_info_crate_t c_info{.td=&td, .src=&src, .selected_idx=&selected_idx, .selected_kis=&selected_kis,
127 .ntrans=ntrans, .iters=iters, .sum=sum, .cur_utxo=cur_utxo};
128
129 bool take_it = (fnc_accept.get())(c_info, abort);
130 if (!take_it){
131 continue;
132 }
133 }
134
135 MDEBUG("Selected " << i << " from tx: " << dump_keys(td.m_txid.data)
136 << " ki: " << dump_keys(td.m_key_image.data)
137 << " amnt: " << td.amount()
138 << " rct: " << td.is_rct()
139 << " glob: " << td.m_global_output_index);
140
141 sum += td.amount();
142 cur_utxo += 1;
143
144 sources.emplace_back(src);
145 selected.push_back((size_t)i);
146 selected_idx.insert((size_t)i);
147 selected_kis.insert(td.m_key_image);
148
149 } catch(const std::exception &e){
150 MTRACE("Output " << i << ", from: " << dump_keys(td.m_txid.data)
151 << ", amnt: " << td.amount() << ", rct: " << td.is_rct()
152 << ", glob: " << td.m_global_output_index << " is not applicable: " << e.what());
153 }
154 }
155
156 EVAL_BRK_COND();
157 return brk_cond >= brk_thresh;
158 #undef EVAL_BRK_COND
159 }
160
161 void wallet_tools::gen_tx_src(size_t mixin, uint64_t cur_height, const tools::wallet2::transfer_details & td, cryptonote::tx_source_entry & src, block_tracker &bt)
162 {
163 CHECK_AND_ASSERT_THROW_MES(mixin != 0, "mixin is zero");
164 src.amount = td.amount();
165 src.rct = td.is_rct();
166
167 std::vector<tools::wallet2::get_outs_entry> outs;
168 bt.get_fake_outs(mixin, td.is_rct() ? 0 : td.amount(), td.m_global_output_index, cur_height, outs);
169
170 for (size_t n = 0; n < mixin; ++n)
171 {
172 cryptonote::tx_source_entry::output_entry oe;
173 oe.first = std::get<0>(outs[n]);
174 oe.second.dest = rct::pk2rct(std::get<1>(outs[n]));
175 oe.second.mask = std::get<2>(outs[n]);
176 src.outputs.push_back(oe);
177 }
178
179 size_t real_idx = crypto::rand<size_t>() % mixin;
180
181 cryptonote::tx_source_entry::output_entry &real_oe = src.outputs[real_idx];
182 real_oe.first = td.m_global_output_index;
183 real_oe.second.dest = rct::pk2rct(boost::get<txout_to_key>(td.m_tx.vout[td.m_internal_output_index].target).key);
184 real_oe.second.mask = rct::commit(td.amount(), td.m_mask);
185
186 std::sort(src.outputs.begin(), src.outputs.end(), [&](const cryptonote::tx_source_entry::output_entry i0, const cryptonote::tx_source_entry::output_entry i1) {
187 return i0.first < i1.first;
188 });
189
190 for (size_t i = 0; i < src.outputs.size(); ++i){
191 if (src.outputs[i].first == td.m_global_output_index){
192 src.real_output = i;
193 break;
194 }
195 }
196
197 src.mask = td.m_mask;
198 src.real_out_tx_key = get_tx_pub_key_from_extra(td.m_tx, td.m_pk_index);
199 src.real_out_additional_tx_keys = get_additional_tx_pub_keys_from_extra(td.m_tx);
200 src.real_output_in_tx_index = td.m_internal_output_index;
201 src.multisig_kLRki = rct::multisig_kLRki({rct::zero(), rct::zero(), rct::zero(), rct::zero()});
202 }
203
204 void wallet_tools::gen_block_data(block_tracker &bt, const cryptonote::block *bl, const map_hash2tx_t &mtx, cryptonote::block_complete_entry &bche, tools::wallet2::parsed_block &parsed_block, uint64_t &height)
205 {
206 vector<const transaction*> vtx;
207 vtx.push_back(&(bl->miner_tx));
208 height = boost::get<txin_gen>(*bl->miner_tx.vin.begin()).height;
209
210 BOOST_FOREACH(const crypto::hash &h, bl->tx_hashes) {
211 const map_hash2tx_t::const_iterator cit = mtx.find(h);
212 CHECK_AND_ASSERT_THROW_MES(mtx.end() != cit, "block contains an unknown tx hash @ " << height << ", " << h);
213 vtx.push_back(cit->second);
214 }
215
216 bche.block = "NA";
217 bche.txs.resize(bl->tx_hashes.size());
218
219 parsed_block.error = false;
220 parsed_block.hash = get_block_hash(*bl);
221 parsed_block.block = *bl;
222 parsed_block.txes.reserve(bl->tx_hashes.size());
223
224 auto & o_indices = parsed_block.o_indices.indices;
225 o_indices.reserve(bl->tx_hashes.size() + 1);
226
227 size_t cur = 0;
228 BOOST_FOREACH(const transaction *tx, vtx){
229 cur += 1;
230 o_indices.emplace_back();
231 bt.process(bl, tx, cur - 1);
232 bt.global_indices(tx, o_indices.back().indices);
233
234 if (cur > 1) // miner not included
235 parsed_block.txes.push_back(*tx);
236 }
237 }
238
239 void wallet_tools::compute_subaddresses(std::unordered_map<crypto::public_key, cryptonote::subaddress_index> &subaddresses, cryptonote::account_base & creds, size_t account, size_t minors)
240 {
241 auto &hwdev = hw::get_device("default");
242 const std::vector<crypto::public_key> pkeys = hwdev.get_subaddress_spend_public_keys(creds.get_keys(), account, 0, minors);
243
244 for(uint32_t c = 0; c < pkeys.size(); ++c){
245 cryptonote::subaddress_index sidx{(uint32_t)account, c};
246 subaddresses[pkeys[c]] = sidx;
247 }
248 }
249
250 cryptonote::account_public_address get_address(const tools::wallet2* inp)
251 {
252 return (inp)->get_account().get_keys().m_account_address;
253 }
254
255 bool construct_tx_to_key(cryptonote::transaction& tx,
256 tools::wallet2 * sender_wallet, const var_addr_t& to, uint64_t amount,
257 std::vector<cryptonote::tx_source_entry> &sources,
258 uint64_t fee, bool rct, rct::RangeProofType range_proof_type, int bp_version)
259 {
260 vector<tx_destination_entry> destinations;
261 fill_tx_destinations(sender_wallet->get_account(), get_address(to), amount, fee, sources, destinations, rct);
262 return construct_tx_rct(sender_wallet, sources, destinations, get_address(sender_wallet), std::vector<uint8_t>(), tx, rct, range_proof_type, bp_version);
263 }
264
265 bool construct_tx_to_key(cryptonote::transaction& tx,
266 tools::wallet2 * sender_wallet,
267 const std::vector<cryptonote::tx_destination_entry>& destinations,
268 std::vector<cryptonote::tx_source_entry> &sources,
269 uint64_t fee, bool rct, rct::RangeProofType range_proof_type, int bp_version)
270 {
271 vector<tx_destination_entry> all_destinations;
272 fill_tx_destinations(sender_wallet->get_account(), destinations, fee, sources, all_destinations, rct);
273 return construct_tx_rct(sender_wallet, sources, all_destinations, get_address(sender_wallet), std::vector<uint8_t>(), tx, rct, range_proof_type, bp_version);
274 }
275
276 bool construct_tx_rct(tools::wallet2 * sender_wallet, std::vector<cryptonote::tx_source_entry>& sources, const std::vector<cryptonote::tx_destination_entry>& destinations, const boost::optional<cryptonote::account_public_address>& change_addr, std::vector<uint8_t> extra, cryptonote::transaction& tx, bool rct, rct::RangeProofType range_proof_type, int bp_version)
277 {
278 subaddresses_t & subaddresses = wallet_accessor_test::get_subaddresses(sender_wallet);
279 crypto::secret_key tx_key;
280 std::vector<crypto::secret_key> additional_tx_keys;
281 std::vector<tx_destination_entry> destinations_copy = destinations;
282 rct::RCTConfig rct_config = {range_proof_type, bp_version};
283 return construct_tx_and_get_tx_key(sender_wallet->get_account().get_keys(), subaddresses, sources, destinations_copy, change_addr, extra, tx, tx_key, additional_tx_keys, rct, rct_config);
284 }