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