Debian Codegraph @

monero/src/wallet/wallet_errors.h

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 #pragma once
32
33 #include <stdexcept>
34 #include <system_error>
35 #include <string>
36 #include <vector>
37
38 #include "cryptonote_basic/cryptonote_format_utils.h"
39 #include "cryptonote_core/cryptonote_tx_utils.h"
40 #include "rpc/core_rpc_server_commands_defs.h"
41 #include "include_base_utils.h"
42
43
44 namespace tools
45 {
46 namespace error
47 {
48 // std::exception
49 // std::runtime_error
50 // wallet_runtime_error *
51 // wallet_internal_error
52 // unexpected_txin_type
53 // wallet_not_initialized
54 // multisig_export_needed
55 // multisig_import_needed
56 // password_needed
57 // std::logic_error
58 // wallet_logic_error *
59 // file_exists
60 // file_not_found
61 // file_read_error
62 // file_save_error
63 // invalid_password
64 // invalid_priority
65 // invalid_multisig_seed
66 // invalid_spend_key
67 // refresh_error *
68 // acc_outs_lookup_error
69 // block_parse_error
70 // get_blocks_error
71 // get_hashes_error
72 // get_out_indexes_error
73 // tx_parse_error
74 // get_tx_pool_error
75 // out_of_hashchain_bounds_error
76 // signature_check_failed
77 // transfer_error *
78 // get_outs_general_error
79 // not_enough_unlocked_money
80 // not_enough_money
81 // tx_not_possible
82 // not_enough_outs_to_mix
83 // tx_not_constructed
84 // tx_rejected
85 // tx_sum_overflow
86 // tx_too_big
87 // zero_amount
88 // zero_destination
89 // subtract_fee_from_bad_index
90 // nonzero_unlock_time
91 // wallet_rpc_error *
92 // daemon_busy
93 // no_connection_to_daemon
94 // is_key_image_spent_error
95 // get_histogram_error
96 // get_output_distribution
97 // payment_required
98 // wallet_files_doesnt_correspond
99 // scan_tx_error *
100 // wont_reprocess_recent_txs_via_untrusted_daemon
101 // background_sync_error *
102 // background_wallet_already_open
103 // background_custom_password_same_as_wallet_password
104 //
105 // * - class with protected ctor
106
107 //----------------------------------------------------------------------------------------------------
108 template<typename Base>
109 struct wallet_error_base : public Base
110 {
111 const std::string& location() const { return m_loc; }
112
113 std::string to_string() const
114 {
115 std::ostringstream ss;
116 ss << Base::what();
117 return ss.str();
118 }
119
120 protected:
121 wallet_error_base(std::string&& loc, const std::string& message)
122 : Base(message)
123 , m_loc(loc)
124 {
125 }
126
127 private:
128 std::string m_loc;
129 };
130 //----------------------------------------------------------------------------------------------------
131 const char* const failed_rpc_request_messages[] = {
132 "failed to get blocks",
133 "failed to get hashes",
134 "failed to get out indices",
135 "failed to get random outs"
136 };
137 enum failed_rpc_request_message_indices
138 {
139 get_blocks_error_message_index,
140 get_hashes_error_message_index,
141 get_out_indices_error_message_index,
142 get_outs_error_message_index
143 };
144
145 template<typename Base, int msg_index>
146 struct failed_rpc_request : public Base
147 {
148 explicit failed_rpc_request(std::string&& loc, const std::string& status)
149 : Base(std::move(loc), failed_rpc_request_messages[msg_index])
150 , m_status(status)
151 {
152 }
153
154 const std::string& status() const { return m_status; }
155
156 std::string to_string() const
157 {
158 std::ostringstream ss;
159 ss << Base::to_string() << ", status = " << status();
160 return ss.str();
161 }
162
163 private:
164 std::string m_status;
165 };
166 //----------------------------------------------------------------------------------------------------
167 typedef wallet_error_base<std::logic_error> wallet_logic_error;
168 typedef wallet_error_base<std::runtime_error> wallet_runtime_error;
169 //----------------------------------------------------------------------------------------------------
170 struct wallet_internal_error : public wallet_runtime_error
171 {
172 explicit wallet_internal_error(std::string&& loc, const std::string& message)
173 : wallet_runtime_error(std::move(loc), message)
174 {
175 }
176 };
177 //----------------------------------------------------------------------------------------------------
178 struct unexpected_txin_type : public wallet_internal_error
179 {
180 explicit unexpected_txin_type(std::string&& loc, const cryptonote::transaction& tx)
181 : wallet_internal_error(std::move(loc), "one of tx inputs has unexpected type")
182 , m_tx(tx)
183 {
184 }
185
186 const cryptonote::transaction& tx() const { return m_tx; }
187
188 std::string to_string() const
189 {
190 std::ostringstream ss;
191 cryptonote::transaction tx = m_tx;
192 ss << wallet_internal_error::to_string() << ", tx:\n" << cryptonote::obj_to_json_str(tx);
193 return ss.str();
194 }
195
196 private:
197 cryptonote::transaction m_tx;
198 };
199 //----------------------------------------------------------------------------------------------------
200 struct wallet_not_initialized : public wallet_internal_error
201 {
202 explicit wallet_not_initialized(std::string&& loc)
203 : wallet_internal_error(std::move(loc), "wallet is not initialized")
204 {
205 }
206 };
207 //----------------------------------------------------------------------------------------------------
208 struct multisig_export_needed : public wallet_runtime_error
209 {
210 explicit multisig_export_needed(std::string&& loc)
211 : wallet_runtime_error(std::move(loc), "This signature was made with stale data: export fresh multisig data, which other participants must then use")
212 {
213 }
214 };
215 //----------------------------------------------------------------------------------------------------
216 struct multisig_import_needed : public wallet_runtime_error
217 {
218 explicit multisig_import_needed(std::string&& loc)
219 : wallet_runtime_error(std::move(loc), "Not enough multisig data was found to sign: import multisig data from more other participants")
220 {
221 }
222 };
223 //----------------------------------------------------------------------------------------------------
224 struct password_needed : public wallet_runtime_error
225 {
226 explicit password_needed(std::string&& loc, const std::string &msg = "Password needed")
227 : wallet_runtime_error(std::move(loc), msg)
228 {
229 }
230 };
231 //----------------------------------------------------------------------------------------------------
232 struct password_entry_failed : public wallet_runtime_error
233 {
234 explicit password_entry_failed(std::string&& loc, const std::string &msg = "Password entry failed")
235 : wallet_runtime_error(std::move(loc), msg)
236 {
237 }
238 };
239 //----------------------------------------------------------------------------------------------------
240 const char* const file_error_messages[] = {
241 "file already exists",
242 "file not found",
243 "failed to read file",
244 "failed to save file"
245 };
246 enum file_error_message_indices
247 {
248 file_exists_message_index,
249 file_not_found_message_index,
250 file_read_error_message_index,
251 file_save_error_message_index
252 };
253
254 template<int msg_index>
255 struct file_error_base : public wallet_logic_error
256 {
257 explicit file_error_base(std::string&& loc, const std::string& file)
258 : wallet_logic_error(std::move(loc), std::string(file_error_messages[msg_index]) + " \"" + file + '\"')
259 , m_file(file)
260 {
261 }
262
263 explicit file_error_base(std::string&& loc, const std::string& file, const std::error_code &e)
264 : wallet_logic_error(std::move(loc), std::string(file_error_messages[msg_index]) + " \"" + file + "\": " + e.message())
265 , m_file(file)
266 {
267 }
268
269 const std::string& file() const { return m_file; }
270
271 std::string to_string() const { return wallet_logic_error::to_string(); }
272
273 private:
274 std::string m_file;
275 };
276 //----------------------------------------------------------------------------------------------------
277 typedef file_error_base<file_exists_message_index> file_exists;
278 typedef file_error_base<file_not_found_message_index> file_not_found;
279 typedef file_error_base<file_read_error_message_index> file_read_error;
280 typedef file_error_base<file_save_error_message_index> file_save_error;
281 //----------------------------------------------------------------------------------------------------
282 struct invalid_password : public wallet_logic_error
283 {
284 explicit invalid_password(std::string&& loc)
285 : wallet_logic_error(std::move(loc), "invalid password")
286 {
287 }
288
289 std::string to_string() const { return wallet_logic_error::to_string(); }
290 };
291 struct invalid_priority : public wallet_logic_error
292 {
293 explicit invalid_priority(std::string&& loc)
294 : wallet_logic_error(std::move(loc), "invalid priority")
295 {
296 }
297
298 std::string to_string() const { return wallet_logic_error::to_string(); }
299 };
300
301 struct invalid_multisig_seed : public wallet_logic_error
302 {
303 explicit invalid_multisig_seed(std::string&& loc)
304 : wallet_logic_error(std::move(loc), "invalid multisig seed")
305 {
306 }
307
308 std::string to_string() const { return wallet_logic_error::to_string(); }
309 };
310
311 struct invalid_spend_key : public wallet_logic_error
312 {
313 explicit invalid_spend_key(std::string&& loc)
314 : wallet_logic_error(std::move(loc), "invalid spend key")
315 {
316 }
317
318 std::string to_string() const { return wallet_logic_error::to_string(); }
319 };
320
321 //----------------------------------------------------------------------------------------------------
322 struct invalid_pregenerated_random : public wallet_logic_error
323 {
324 explicit invalid_pregenerated_random (std::string&& loc)
325 : wallet_logic_error(std::move(loc), "invalid pregenerated random for wallet creation/recovery")
326 {
327 }
328
329 std::string to_string() const { return wallet_logic_error::to_string(); }
330 };
331 //----------------------------------------------------------------------------------------------------
332 struct refresh_error : public wallet_logic_error
333 {
334 protected:
335 explicit refresh_error(std::string&& loc, const std::string& message)
336 : wallet_logic_error(std::move(loc), message)
337 {
338 }
339 };
340 //----------------------------------------------------------------------------------------------------
341 struct index_outofbound : public wallet_logic_error
342 {
343 explicit index_outofbound(std::string&& loc, const std::string& message)
344 : wallet_logic_error(std::move(loc), message)
345 {
346 }
347 };
348 struct account_index_outofbound : public index_outofbound
349 {
350 explicit account_index_outofbound(std::string&& loc)
351 : index_outofbound(std::move(loc), "account index is out of bound")
352 {
353 }
354 };
355 struct address_index_outofbound: public index_outofbound
356 {
357 explicit address_index_outofbound(std::string&& loc)
358 : index_outofbound(std::move(loc), "address index is out of bound")
359 {
360 }
361 };
362 //----------------------------------------------------------------------------------------------------
363 struct acc_outs_lookup_error : public refresh_error
364 {
365 explicit acc_outs_lookup_error(std::string&& loc, const cryptonote::transaction& tx,
366 const crypto::public_key& tx_pub_key, const cryptonote::account_keys& acc_keys)
367 : refresh_error(std::move(loc), "account outs lookup error")
368 , m_tx(tx)
369 , m_tx_pub_key(tx_pub_key)
370 , m_acc_keys(acc_keys)
371 {
372 }
373
374 const cryptonote::transaction& tx() const { return m_tx; }
375 const crypto::public_key& tx_pub_key() const { return m_tx_pub_key; }
376 const cryptonote::account_keys& acc_keys() const { return m_acc_keys; }
377
378 std::string to_string() const
379 {
380 std::ostringstream ss;
381 cryptonote::transaction tx = m_tx;
382 ss << refresh_error::to_string() << ", tx: " << cryptonote::obj_to_json_str(tx);
383 return ss.str();
384 }
385
386 private:
387 const cryptonote::transaction m_tx;
388 const crypto::public_key m_tx_pub_key;
389 const cryptonote::account_keys m_acc_keys;
390 };
391 //----------------------------------------------------------------------------------------------------
392 struct block_parse_error : public refresh_error
393 {
394 explicit block_parse_error(std::string&& loc, const cryptonote::blobdata& block_data)
395 : refresh_error(std::move(loc), "block parse error")
396 , m_block_blob(block_data)
397 {
398 }
399
400 const cryptonote::blobdata& block_blob() const { return m_block_blob; }
401
402 std::string to_string() const { return refresh_error::to_string(); }
403
404 private:
405 cryptonote::blobdata m_block_blob;
406 };
407 //----------------------------------------------------------------------------------------------------
408 typedef failed_rpc_request<refresh_error, get_blocks_error_message_index> get_blocks_error;
409 //----------------------------------------------------------------------------------------------------
410 typedef failed_rpc_request<refresh_error, get_hashes_error_message_index> get_hashes_error;
411 //----------------------------------------------------------------------------------------------------
412 typedef failed_rpc_request<refresh_error, get_out_indices_error_message_index> get_out_indices_error;
413 //----------------------------------------------------------------------------------------------------
414 struct tx_parse_error : public refresh_error
415 {
416 explicit tx_parse_error(std::string&& loc, const cryptonote::blobdata& tx_blob)
417 : refresh_error(std::move(loc), "transaction parse error")
418 , m_tx_blob(tx_blob)
419 {
420 }
421
422 const cryptonote::blobdata& tx_blob() const { return m_tx_blob; }
423
424 std::string to_string() const { return refresh_error::to_string(); }
425
426 private:
427 cryptonote::blobdata m_tx_blob;
428 };
429 //----------------------------------------------------------------------------------------------------
430 struct get_tx_pool_error : public refresh_error
431 {
432 explicit get_tx_pool_error(std::string&& loc)
433 : refresh_error(std::move(loc), "error getting transaction pool")
434 {
435 }
436
437 std::string to_string() const { return refresh_error::to_string(); }
438 };
439 //----------------------------------------------------------------------------------------------------
440 struct out_of_hashchain_bounds_error : public refresh_error
441 {
442 explicit out_of_hashchain_bounds_error(std::string&& loc)
443 : refresh_error(std::move(loc), "Index out of bounds of of hashchain")
444 {
445 }
446
447 std::string to_string() const { return refresh_error::to_string(); }
448 };
449 //----------------------------------------------------------------------------------------------------
450 struct reorg_depth_error : public refresh_error
451 {
452 explicit reorg_depth_error(std::string&& loc, const std::string& message)
453 : refresh_error(std::move(loc), message)
454 {
455 }
456
457 std::string to_string() const { return refresh_error::to_string(); }
458 };
459 //----------------------------------------------------------------------------------------------------
460 struct signature_check_failed : public wallet_logic_error
461 {
462 explicit signature_check_failed(std::string&& loc, const std::string& message)
463 : wallet_logic_error(std::move(loc), "Signature check failed " + message)
464 {
465 }
466 };
467 //----------------------------------------------------------------------------------------------------
468 struct transfer_error : public wallet_logic_error
469 {
470 protected:
471 explicit transfer_error(std::string&& loc, const std::string& message)
472 : wallet_logic_error(std::move(loc), message)
473 {
474 }
475 };
476 //----------------------------------------------------------------------------------------------------
477 typedef failed_rpc_request<transfer_error, get_outs_error_message_index> get_outs_error;
478 //----------------------------------------------------------------------------------------------------
479 struct not_enough_unlocked_money : public transfer_error
480 {
481 explicit not_enough_unlocked_money(std::string&& loc, uint64_t available, uint64_t tx_amount, uint64_t fee)
482 : transfer_error(std::move(loc), "not enough unlocked money")
483 , m_available(available)
484 , m_tx_amount(tx_amount)
485 , m_fee(fee)
486 {
487 }
488
489 uint64_t available() const { return m_available; }
490 uint64_t tx_amount() const { return m_tx_amount; }
491 uint64_t fee() const { return m_fee; }
492
493 std::string to_string() const
494 {
495 std::ostringstream ss;
496 ss << transfer_error::to_string() <<
497 ", available = " << cryptonote::print_money(m_available) <<
498 ", tx_amount = " << cryptonote::print_money(m_tx_amount);
499 return ss.str();
500 }
501
502 private:
503 uint64_t m_available;
504 uint64_t m_tx_amount;
505 uint64_t m_fee;
506 };
507 //----------------------------------------------------------------------------------------------------
508 struct not_enough_money : public transfer_error
509 {
510 explicit not_enough_money(std::string&& loc, uint64_t available, uint64_t tx_amount, uint64_t fee)
511 : transfer_error(std::move(loc), "not enough money")
512 , m_available(available)
513 , m_tx_amount(tx_amount)
514 , m_fee(fee)
515 {
516 }
517
518 uint64_t available() const { return m_available; }
519 uint64_t tx_amount() const { return m_tx_amount; }
520 uint64_t fee() const { return m_fee; }
521
522 std::string to_string() const
523 {
524 std::ostringstream ss;
525 ss << transfer_error::to_string() <<
526 ", available = " << cryptonote::print_money(m_available) <<
527 ", tx_amount = " << cryptonote::print_money(m_tx_amount);
528 return ss.str();
529 }
530
531 private:
532 uint64_t m_available;
533 uint64_t m_tx_amount;
534 uint64_t m_fee;
535 };
536 //----------------------------------------------------------------------------------------------------
537 struct tx_not_possible : public transfer_error
538 {
539 explicit tx_not_possible(std::string&& loc, uint64_t available, uint64_t tx_amount, uint64_t fee)
540 : transfer_error(std::move(loc), "tx not possible")
541 , m_available(available)
542 , m_tx_amount(tx_amount)
543 , m_fee(fee)
544 {
545 }
546
547 uint64_t available() const { return m_available; }
548 uint64_t tx_amount() const { return m_tx_amount; }
549 uint64_t fee() const { return m_fee; }
550
551 std::string to_string() const
552 {
553 std::ostringstream ss;
554 ss << transfer_error::to_string() <<
555 ", available = " << cryptonote::print_money(m_available) <<
556 ", tx_amount = " << cryptonote::print_money(m_tx_amount) <<
557 ", fee = " << cryptonote::print_money(m_fee);
558 return ss.str();
559 }
560
561 private:
562 uint64_t m_available;
563 uint64_t m_tx_amount;
564 uint64_t m_fee;
565 };
566 //----------------------------------------------------------------------------------------------------
567 struct not_enough_outs_to_mix : public transfer_error
568 {
569 typedef std::unordered_map<uint64_t, uint64_t> scanty_outs_t;
570
571 explicit not_enough_outs_to_mix(std::string&& loc, const scanty_outs_t& scanty_outs, size_t mixin_count)
572 : transfer_error(std::move(loc), "not enough outputs to use")
573 , m_scanty_outs(scanty_outs)
574 , m_mixin_count(mixin_count)
575 {
576 }
577
578 const scanty_outs_t& scanty_outs() const { return m_scanty_outs; }
579 size_t mixin_count() const { return m_mixin_count; }
580
581 std::string to_string() const
582 {
583 std::ostringstream ss;
584 ss << transfer_error::to_string() << ", ring size = " << (m_mixin_count + 1) << ", scanty_outs:";
585 for (const auto& out: m_scanty_outs)
586 {
587 ss << '\n' << cryptonote::print_money(out.first) << " - " << out.second;
588 }
589 return ss.str();
590 }
591
592 private:
593 scanty_outs_t m_scanty_outs;
594 size_t m_mixin_count;
595 };
596 //----------------------------------------------------------------------------------------------------
597 struct tx_not_constructed : public transfer_error
598 {
599 typedef std::vector<cryptonote::tx_source_entry> sources_t;
600 typedef std::vector<cryptonote::tx_destination_entry> destinations_t;
601
602 explicit tx_not_constructed(
603 std::string && loc
604 , sources_t const & sources
605 , destinations_t const & destinations
606 , cryptonote::network_type nettype
607 )
608 : transfer_error(std::move(loc), "transaction was not constructed")
609 , m_sources(sources)
610 , m_destinations(destinations)
611 , m_nettype(nettype)
612 {
613 }
614
615 const sources_t& sources() const { return m_sources; }
616 const destinations_t& destinations() const { return m_destinations; }
617
618 std::string to_string() const
619 {
620 std::ostringstream ss;
621 ss << transfer_error::to_string();
622 ss << "\nSources:";
623 for (size_t i = 0; i < m_sources.size(); ++i)
624 {
625 const cryptonote::tx_source_entry& src = m_sources[i];
626 ss << "\n source " << i << ":";
627 ss << "\n amount: " << cryptonote::print_money(src.amount);
628 // It's not good, if logs will contain such much data
629 //ss << "\n real_output: " << src.real_output;
630 //ss << "\n real_output_in_tx_index: " << src.real_output_in_tx_index;
631 //ss << "\n real_out_tx_key: " << epee::string_tools::pod_to_hex(src.real_out_tx_key);
632 //ss << "\n outputs:";
633 //for (size_t j = 0; j < src.outputs.size(); ++j)
634 //{
635 // const cryptonote::tx_source_entry::output_entry& out = src.outputs[j];
636 // ss << "\n " << j << ": " << out.first << ", " << epee::string_tools::pod_to_hex(out.second);
637 //}
638 }
639
640 ss << "\nDestinations:";
641 for (size_t i = 0; i < m_destinations.size(); ++i)
642 {
643 const cryptonote::tx_destination_entry& dst = m_destinations[i];
644 ss << "\n " << i << ": " << cryptonote::get_account_address_as_str(m_nettype, dst.is_subaddress, dst.addr) << " " <<
645 cryptonote::print_money(dst.amount);
646 }
647
648 return ss.str();
649 }
650
651 private:
652 sources_t m_sources;
653 destinations_t m_destinations;
654 cryptonote::network_type m_nettype;
655 };
656 //----------------------------------------------------------------------------------------------------
657 struct tx_rejected : public transfer_error
658 {
659 explicit tx_rejected(std::string&& loc, const cryptonote::transaction& tx, const std::string& status, const std::string& reason)
660 : transfer_error(std::move(loc), "transaction was rejected by daemon")
661 , m_tx(tx)
662 , m_status(status)
663 , m_reason(reason)
664 {
665 }
666
667 const cryptonote::transaction& tx() const { return m_tx; }
668 const std::string& status() const { return m_status; }
669 const std::string& reason() const { return m_reason; }
670
671 std::string to_string() const
672 {
673 std::ostringstream ss;
674 ss << transfer_error::to_string() << ", status = " << m_status << ", tx:\n";
675 cryptonote::transaction tx = m_tx;
676 ss << cryptonote::obj_to_json_str(tx);
677 if (!m_reason.empty())
678 {
679 ss << " (" << m_reason << ")";
680 }
681 return ss.str();
682 }
683
684 private:
685 cryptonote::transaction m_tx;
686 std::string m_status;
687 std::string m_reason;
688 };
689 //----------------------------------------------------------------------------------------------------
690 struct tx_sum_overflow : public transfer_error
691 {
692 explicit tx_sum_overflow(
693 std::string && loc
694 , const std::vector<cryptonote::tx_destination_entry>& destinations
695 , uint64_t fee
696 , cryptonote::network_type nettype
697 )
698 : transfer_error(std::move(loc), "transaction sum + fee exceeds " + cryptonote::print_money(std::numeric_limits<uint64_t>::max()))
699 , m_destinations(destinations)
700 , m_fee(fee)
701 , m_nettype(nettype)
702 {
703 }
704
705 const std::vector<cryptonote::tx_destination_entry>& destinations() const { return m_destinations; }
706 uint64_t fee() const { return m_fee; }
707
708 std::string to_string() const
709 {
710 std::ostringstream ss;
711 ss << transfer_error::to_string() <<
712 ", fee = " << cryptonote::print_money(m_fee) <<
713 ", destinations:";
714 for (const auto& dst : m_destinations)
715 {
716 ss << '\n' << cryptonote::print_money(dst.amount) << " -> " << cryptonote::get_account_address_as_str(m_nettype, dst.is_subaddress, dst.addr);
717 }
718 return ss.str();
719 }
720
721 private:
722 std::vector<cryptonote::tx_destination_entry> m_destinations;
723 uint64_t m_fee;
724 cryptonote::network_type m_nettype;
725 };
726 //----------------------------------------------------------------------------------------------------
727 struct tx_too_big : public transfer_error
728 {
729 explicit tx_too_big(std::string&& loc, const cryptonote::transaction& tx, uint64_t tx_weight_limit)
730 : transfer_error(std::move(loc), "transaction is too big")
731 , m_tx(tx)
732 , m_tx_valid(true)
733 , m_tx_weight(cryptonote::get_transaction_weight(tx))
734 , m_tx_weight_limit(tx_weight_limit)
735 {
736 }
737
738 explicit tx_too_big(std::string&& loc, uint64_t tx_weight, uint64_t tx_weight_limit)
739 : transfer_error(std::move(loc), "transaction would be too big")
740 , m_tx_valid(false)
741 , m_tx_weight(tx_weight)
742 , m_tx_weight_limit(tx_weight_limit)
743 {
744 }
745
746 bool tx_valid() const { return m_tx_valid; }
747 const cryptonote::transaction& tx() const { return m_tx; }
748 uint64_t tx_weight() const { return m_tx_weight; }
749 uint64_t tx_weight_limit() const { return m_tx_weight_limit; }
750
751 std::string to_string() const
752 {
753 std::ostringstream ss;
754 ss << transfer_error::to_string() <<
755 ", tx_weight_limit = " << m_tx_weight_limit <<
756 ", tx weight = " << m_tx_weight;
757 if (m_tx_valid)
758 {
759 cryptonote::transaction tx = m_tx;
760 ss << ", tx:\n" << cryptonote::obj_to_json_str(tx);
761 }
762 return ss.str();
763 }
764
765 private:
766 cryptonote::transaction m_tx;
767 bool m_tx_valid;
768 uint64_t m_tx_weight;
769 uint64_t m_tx_weight_limit;
770 };
771 //----------------------------------------------------------------------------------------------------
772 struct zero_amount: public transfer_error
773 {
774 explicit zero_amount(std::string&& loc)
775 : transfer_error(std::move(loc), "destination amount is zero")
776 {
777 }
778 };
779 //----------------------------------------------------------------------------------------------------
780 struct zero_destination : public transfer_error
781 {
782 explicit zero_destination(std::string&& loc)
783 : transfer_error(std::move(loc), "transaction has no destination")
784 {
785 }
786 };
787 //----------------------------------------------------------------------------------------------------
788 struct subtract_fee_from_bad_index : public transfer_error
789 {
790 explicit subtract_fee_from_bad_index(std::string&& loc, long bad_index)
791 : transfer_error(std::move(loc),
792 "subtractfeefrom: bad index: " + std::to_string(bad_index) + " (indexes are 0-based)")
793 {
794 }
795 };
796 //----------------------------------------------------------------------------------------------------
797 struct nonzero_unlock_time : public transfer_error
798 {
799 explicit nonzero_unlock_time(std::string&& loc)
800 : transfer_error(std::move(loc), "transaction cannot have non-zero unlock time")
801 {
802 }
803 };
804 //----------------------------------------------------------------------------------------------------
805 struct wallet_rpc_error : public wallet_logic_error
806 {
807 const std::string& request() const { return m_request; }
808
809 std::string to_string() const
810 {
811 std::ostringstream ss;
812 ss << "RPC Error. " << wallet_logic_error::to_string() << ": " << m_request;
813 return ss.str();
814 }
815
816 protected:
817 explicit wallet_rpc_error(std::string&& loc, const std::string& message, const std::string& request)
818 : wallet_logic_error(std::move(loc), message)
819 , m_request(request)
820 {
821 }
822
823 private:
824 std::string m_request;
825 };
826 //----------------------------------------------------------------------------------------------------
827 struct wallet_generic_rpc_error : public wallet_rpc_error
828 {
829 explicit wallet_generic_rpc_error(std::string&& loc, const std::string& request, const std::string& status)
830 : wallet_rpc_error(std::move(loc), std::string("error in ") + request + " RPC: " + status, request),
831 m_status(status)
832 {
833 }
834 const std::string& status() const { return m_status; }
835 private:
836 const std::string m_status;
837 };
838 //----------------------------------------------------------------------------------------------------
839 struct wallet_coded_rpc_error : public wallet_rpc_error
840 {
841 explicit wallet_coded_rpc_error(std::string&& loc, const std::string& request, int code, const std::string& status)
842 : wallet_rpc_error(std::move(loc), std::string("error ") + std::to_string(code) + (" in ") + request + " RPC: " + status, request),
843 m_code(code), m_status(status)
844 {
845 }
846 int code() const { return m_code; }
847 const std::string& status() const { return m_status; }
848 private:
849 int m_code;
850 const std::string m_status;
851 };
852 //----------------------------------------------------------------------------------------------------
853 struct daemon_busy : public wallet_rpc_error
854 {
855 explicit daemon_busy(std::string&& loc, const std::string& request)
856 : wallet_rpc_error(std::move(loc), "daemon is busy", request)
857 {
858 }
859 };
860 //----------------------------------------------------------------------------------------------------
861 struct no_connection_to_daemon : public wallet_rpc_error
862 {
863 explicit no_connection_to_daemon(std::string&& loc, const std::string& request)
864 : wallet_rpc_error(std::move(loc), "no connection to daemon", request)
865 {
866 }
867 };
868 //----------------------------------------------------------------------------------------------------
869 struct is_key_image_spent_error : public wallet_rpc_error
870 {
871 explicit is_key_image_spent_error(std::string&& loc, const std::string& request)
872 : wallet_rpc_error(std::move(loc), "error from is_key_image_spent call", request)
873 {
874 }
875 };
876 //----------------------------------------------------------------------------------------------------
877 struct get_histogram_error : public wallet_rpc_error
878 {
879 explicit get_histogram_error(std::string&& loc, const std::string& request)
880 : wallet_rpc_error(std::move(loc), "failed to get output histogram", request)
881 {
882 }
883 };
884 //----------------------------------------------------------------------------------------------------
885 struct get_output_distribution : public wallet_rpc_error
886 {
887 explicit get_output_distribution(std::string&& loc, const std::string& request)
888 : wallet_rpc_error(std::move(loc), "failed to get output distribution", request)
889 {
890 }
891 };
892 //----------------------------------------------------------------------------------------------------
893 struct payment_required: public wallet_rpc_error
894 {
895 explicit payment_required(std::string&& loc, const std::string& request)
896 : wallet_rpc_error(std::move(loc), "payment required", request)
897 {
898 }
899 };
900 //----------------------------------------------------------------------------------------------------
901 struct wallet_files_doesnt_correspond : public wallet_logic_error
902 {
903 explicit wallet_files_doesnt_correspond(std::string&& loc, const std::string& keys_file, const std::string& wallet_file)
904 : wallet_logic_error(std::move(loc), "file " + wallet_file + " does not correspond to " + keys_file)
905 {
906 }
907
908 const std::string& keys_file() const { return m_keys_file; }
909 const std::string& wallet_file() const { return m_wallet_file; }
910
911 std::string to_string() const { return wallet_logic_error::to_string(); }
912
913 private:
914 std::string m_keys_file;
915 std::string m_wallet_file;
916 };
917 //----------------------------------------------------------------------------------------------------
918 struct mms_error : public wallet_logic_error
919 {
920 protected:
921 explicit mms_error(std::string&& loc, const std::string& message)
922 : wallet_logic_error(std::move(loc), message)
923 {
924 }
925 };
926 //----------------------------------------------------------------------------------------------------
927 struct no_connection_to_bitmessage : public mms_error
928 {
929 explicit no_connection_to_bitmessage(std::string&& loc, const std::string& address)
930 : mms_error(std::move(loc), "no connection to PyBitmessage at address " + address)
931 {
932 }
933 };
934 //----------------------------------------------------------------------------------------------------
935 struct bitmessage_api_error : public mms_error
936 {
937 explicit bitmessage_api_error(std::string&& loc, const std::string& error_string)
938 : mms_error(std::move(loc), "PyBitmessage returned " + error_string)
939 {
940 }
941 };
942 //----------------------------------------------------------------------------------------------------
943 struct scan_tx_error : public wallet_logic_error
944 {
945 protected:
946 explicit scan_tx_error(std::string&& loc, const std::string& message)
947 : wallet_logic_error(std::move(loc), message)
948 {
949 }
950 };
951 //----------------------------------------------------------------------------------------------------
952 struct wont_reprocess_recent_txs_via_untrusted_daemon : public scan_tx_error
953 {
954 explicit wont_reprocess_recent_txs_via_untrusted_daemon(std::string&& loc)
955 : scan_tx_error(std::move(loc), "The wallet has already seen 1 or more recent transactions than the scanned tx")
956 {
957 }
958 };
959 //----------------------------------------------------------------------------------------------------
960 struct background_sync_error : public wallet_logic_error
961 {
962 protected:
963 explicit background_sync_error(std::string&& loc, const std::string& message)
964 : wallet_logic_error(std::move(loc), message)
965 {
966 }
967 };
968 //----------------------------------------------------------------------------------------------------
969 struct background_wallet_already_open : public background_sync_error
970 {
971 explicit background_wallet_already_open(std::string&& loc, const std::string& background_wallet_file)
972 : background_sync_error(std::move(loc), "background wallet " + background_wallet_file + " is already opened by another wallet program")
973 {
974 }
975 };
976 //----------------------------------------------------------------------------------------------------
977 struct background_custom_password_same_as_wallet_password : public background_sync_error
978 {
979 explicit background_custom_password_same_as_wallet_password(std::string&& loc)
980 : background_sync_error(std::move(loc), "custom background password must be different than wallet password")
981 {
982 }
983 };
984 //----------------------------------------------------------------------------------------------------
985
986 #if !defined(_MSC_VER)
987
988 template<typename TException, typename... TArgs>
989 void throw_wallet_ex(std::string&& loc, const TArgs&... args)
990 {
991 TException e(std::move(loc), args...);
992 LOG_PRINT_L0(e.to_string());
993 throw e;
994 }
995
996 #else
997 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
998 #include <boost/preprocessor/repetition/enum_params.hpp>
999 #include <boost/preprocessor/repetition/repeat_from_to.hpp>
1000
1001 template<typename TException>
1002 void throw_wallet_ex(std::string&& loc)
1003 {
1004 TException e(std::move(loc));
1005 LOG_PRINT_L0(e.to_string());
1006 throw e;
1007 }
1008
1009 #define GEN_throw_wallet_ex(z, n, data) \
1010 template<typename TException, BOOST_PP_ENUM_PARAMS(n, typename TArg)> \
1011 void throw_wallet_ex(std::string&& loc, BOOST_PP_ENUM_BINARY_PARAMS(n, const TArg, &arg)) \
1012 { \
1013 TException e(std::move(loc), BOOST_PP_ENUM_PARAMS(n, arg)); \
1014 LOG_PRINT_L0(e.to_string()); \
1015 throw e; \
1016 }
1017
1018 BOOST_PP_REPEAT_FROM_TO(1, 6, GEN_throw_wallet_ex, ~)
1019 #endif
1020 }
1021 }
1022
1023 #define STRINGIZE_DETAIL(x) #x
1024 #define STRINGIZE(x) STRINGIZE_DETAIL(x)
1025
1026 #define THROW_WALLET_EXCEPTION(err_type, ...) \
1027 do { \
1028 LOG_ERROR("THROW EXCEPTION: " << #err_type); \
1029 tools::error::throw_wallet_ex<err_type>(std::string(__FILE__ ":" STRINGIZE(__LINE__)), ## __VA_ARGS__); \
1030 } while(0)
1031
1032 #define THROW_WALLET_EXCEPTION_IF(cond, err_type, ...) \
1033 if (cond) \
1034 { \
1035 LOG_ERROR(#cond << ". THROW EXCEPTION: " << #err_type); \
1036 tools::error::throw_wallet_ex<err_type>(std::string(__FILE__ ":" STRINGIZE(__LINE__)), ## __VA_ARGS__); \
1037 }