From fe98c140ac493f16cb26e24dbc148f29e79aeeb3 Mon Sep 17 00:00:00 2001 From: sowle Date: Fri, 26 Jul 2024 04:49:04 +0200 Subject: [PATCH] wallet: minor refactoring for tx_too_big and other for clearness --- src/simplewallet/simplewallet.cpp | 4 +- src/wallet/wallet2.cpp | 14 ++++--- src/wallet/wallet_errors.h | 67 ++++++++----------------------- 3 files changed, 25 insertions(+), 60 deletions(-) diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 2a92db5d..cebf832e 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -87,9 +87,7 @@ namespace ph = boost::placeholders; } \ catch (const tools::error::tx_too_big& e) \ { \ - currency::transaction tx = e.tx(); \ - fail_msg_writer() << "transaction " << get_transaction_hash(e.tx()) << " is too big. Transaction size: " << \ - get_object_blobsize(e.tx()) << " bytes, transaction size limit: " << e.tx_size_limit() << " bytes. Try to separate this payment into few smaller transfers."; \ + fail_msg_writer() << "transaction is too big. " << e.get_message() << " Try to split this payment into a few smaller transfers."; \ } \ catch (const tools::error::zero_destination&) \ { \ diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 28a754c0..4b58cb37 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -6265,7 +6265,7 @@ bool wallet2::accept_ionic_swap_proposal(const wallet_public::ionic_swap_proposa { if (balances[item.asset_id].unlocked < item.amount) { - WLT_THROW_IF_FALSE_WALLET_EX_MES(false, error::not_enough_money, "", balances[item.asset_id].unlocked, item.amount, 0 /*fee*/, item.asset_id, get_asset_decimal_point(item.asset_id)); + THROW_IF_FALSE_WALLET_EX(false, error::not_enough_money, balances[item.asset_id].unlocked, item.amount, 0 /*fee*/, item.asset_id, get_asset_decimal_point(item.asset_id)); } if (item.asset_id == currency::native_coin_asset_id) { @@ -6280,7 +6280,7 @@ bool wallet2::accept_ionic_swap_proposal(const wallet_public::ionic_swap_proposa additional_fee = m_core_runtime_config.tx_default_fee - msc.proposal_info.fee_paid_by_a; if (balances[currency::native_coin_asset_id].unlocked < additional_fee + native_amount_required) { - WLT_THROW_IF_FALSE_WALLET_EX_MES(false, error::not_enough_money, "", balances[currency::native_coin_asset_id].unlocked, native_amount_required, additional_fee, currency::native_coin_asset_id); + THROW_IF_FALSE_WALLET_EX(false, error::not_enough_money, balances[currency::native_coin_asset_id].unlocked, native_amount_required, additional_fee, currency::native_coin_asset_id); } } @@ -7128,9 +7128,9 @@ bool wallet2::select_indices_for_transfer(assets_selection_context& needed_money WLT_LOG_L1("select_indices_for_transfer: unknown asset id: " << asset_id); auto asset_cache_it = m_found_free_amounts.find(asset_id); - WLT_THROW_IF_FALSE_WALLET_EX_MES(asset_cache_it != m_found_free_amounts.end(), error::not_enough_money, "", item.second.found_amount, item.second.needed_amount, 0, asset_id, asset_info.decimal_point); + THROW_IF_FALSE_WALLET_EX(asset_cache_it != m_found_free_amounts.end(), error::not_enough_money, item.second.found_amount, item.second.needed_amount, (uint64_t)0, asset_id, asset_info.decimal_point); item.second.found_amount = select_indices_for_transfer(selected_indexes, asset_cache_it->second, item.second.needed_amount, fake_outputs_count, asset_id, asset_info.decimal_point); - WLT_THROW_IF_FALSE_WALLET_EX_MES(item.second.found_amount >= item.second.needed_amount, error::not_enough_money, "", item.second.found_amount, item.second.needed_amount, 0, asset_id, asset_info.decimal_point); + THROW_IF_FALSE_WALLET_EX(item.second.found_amount >= item.second.needed_amount, error::not_enough_money, item.second.found_amount, item.second.needed_amount, (uint64_t)0, asset_id, asset_info.decimal_point); } if (m_current_context.pconstruct_tx_param && m_current_context.pconstruct_tx_param->need_at_least_1_zc) { @@ -7661,12 +7661,14 @@ void wallet2::finalize_transaction(currency::finalize_tx_param& ftp, currency::f // broadcasting tx without secret key storing is forbidden to avoid lost key issues WLT_THROW_IF_FALSE_WALLET_INT_ERR_EX(!broadcast_tx || store_tx_secret_key, "finalize_tx is requested to broadcast a tx without storing the key"); + THROW_IF_FALSE_WALLET_EX_MES(ftp.sources.size() <= CURRENCY_TX_MAX_ALLOWED_INPUTS, error::tx_too_big, "Too many inputs: " << ftp.sources.size() << ", maximum allowed is " << CURRENCY_TX_MAX_ALLOWED_INPUTS << "."); + bool r = currency::construct_tx(m_account.get_keys(), ftp, result); //TIME_MEASURE_FINISH_MS(construct_tx_time); THROW_IF_FALSE_WALLET_EX(r, error::tx_not_constructed, ftp.sources, ftp.prepared_destinations, ftp.unlock_time); uint64_t effective_fee = 0; - THROW_IF_FALSE_WALLET_CMN_ERR_EX(!get_tx_fee(result.tx, effective_fee) || effective_fee <= WALLET_TX_MAX_ALLOWED_FEE, "tx fee is WAY too big: " << print_money_brief(effective_fee) << ", max allowed is " << print_money_brief(WALLET_TX_MAX_ALLOWED_FEE)); + THROW_IF_FALSE_WALLET_CMN_ERR_EX(!get_tx_fee(result.tx, effective_fee) || effective_fee <= WALLET_TX_MAX_ALLOWED_FEE, "tx fee is WAY too big: " << print_money_brief(effective_fee) << ", maximum allowed is " << print_money_brief(WALLET_TX_MAX_ALLOWED_FEE) << "."); //TIME_MEASURE_START_MS(sign_ms_input_time); if (ftp.multisig_id != currency::null_hash) @@ -7683,7 +7685,7 @@ void wallet2::finalize_transaction(currency::finalize_tx_param& ftp, currency::f //TIME_MEASURE_FINISH_MS(sign_ms_input_time); size_t tx_blob_size = tx_to_blob(result.tx).size(); - THROW_IF_FALSE_WALLET_EX(tx_blob_size < CURRENCY_MAX_TRANSACTION_BLOB_SIZE, error::tx_too_big, result.tx, m_upper_transaction_size_limit); + THROW_IF_FALSE_WALLET_EX_MES(tx_blob_size < CURRENCY_MAX_TRANSACTION_BLOB_SIZE, error::tx_too_big, "Transaction size: " << tx_blob_size << " bytes, transaction size limit: " << CURRENCY_MAX_TRANSACTION_BLOB_SIZE << " bytes."); if (store_tx_secret_key) m_tx_keys.insert(std::make_pair(get_transaction_hash(result.tx), result.one_time_key)); diff --git a/src/wallet/wallet_errors.h b/src/wallet/wallet_errors.h index 90dbf48c..f8b4e7fe 100644 --- a/src/wallet/wallet_errors.h +++ b/src/wallet/wallet_errors.h @@ -1,4 +1,4 @@ -// Copyright (c) 2014-2018 Zano Project +// Copyright (c) 2014-2024 Zano Project // Copyright (c) 2014-2018 The Louisdor Project // Copyright (c) 2012-2013 The Cryptonote developers // Distributed under the MIT/X11 software license, see the accompanying @@ -350,7 +350,7 @@ namespace tools //---------------------------------------------------------------------------------------------------- struct not_enough_money : public transfer_error { - not_enough_money(std::string&& loc, uint64_t availbable, uint64_t tx_amount, uint64_t fee, const crypto::public_key& asset_id, size_t decimal_point = CURRENCY_DISPLAY_DECIMAL_POINT) + not_enough_money(std::string&& loc, uint64_t availbable, uint64_t tx_amount, uint64_t fee, const crypto::public_key& asset_id, uint8_t decimal_point = CURRENCY_DISPLAY_DECIMAL_POINT) : transfer_error(std::move(loc), "") , m_available(availbable) , m_tx_amount(tx_amount) @@ -381,12 +381,13 @@ namespace tools uint64_t m_tx_amount; uint64_t m_fee; crypto::public_key m_asset_id; - size_t m_decimal_point; + uint8_t m_decimal_point; }; struct no_zc_inputs : public transfer_error { - no_zc_inputs(const std::string& /*v*/): transfer_error(std::string(""), API_RETURN_CODE_MISSING_ZC_INPUTS) + no_zc_inputs(std::string&& loc, const std::string&) + : transfer_error(std::move(loc), API_RETURN_CODE_MISSING_ZC_INPUTS) {} virtual const char* what() const noexcept @@ -566,34 +567,25 @@ namespace tools //---------------------------------------------------------------------------------------------------- struct tx_too_big : public transfer_error { - explicit tx_too_big(std::string&& loc, const currency::transaction& tx, uint64_t tx_size_limit) - : transfer_error(std::move(loc), "transaction is too big") - , m_tx(tx) - , m_tx_size_limit(tx_size_limit) + explicit tx_too_big(std::string&& loc, const std::string& message) + : transfer_error(std::move(loc), API_RETURN_CODE_TX_IS_TOO_BIG) + , m_message(message) { } - const currency::transaction& tx() const { return m_tx; } - uint64_t tx_size_limit() const { return m_tx_size_limit; } + const std::string get_message() const { return m_message; } + // TODO the following overrides need to be redesigned (seems to be necessary for API, consider writing API tests and then refactor this) -- sowle std::string to_string() const { - std::ostringstream ss; - ss << API_RETURN_CODE_TX_IS_TOO_BIG; - //currency::transaction tx = m_tx; - //ss << transfer_error::to_string() << - // ", tx_size_limit = " << m_tx_size_limit << - // ", tx size = " << get_object_blobsize(m_tx) << - // ", tx:\n" << currency::obj_to_json_str(tx); - return ss.str(); + return API_RETURN_CODE_TX_IS_TOO_BIG; } virtual const char* what() const noexcept { return API_RETURN_CODE_TX_IS_TOO_BIG; } private: - currency::transaction m_tx; - uint64_t m_tx_size_limit; + std::string m_message; }; //---------------------------------------------------------------------------------------------------- struct zero_destination : public transfer_error @@ -668,8 +660,6 @@ namespace tools }; //---------------------------------------------------------------------------------------------------- -#if !defined(_MSC_VER) - template void throw_wallet_ex(std::string&& loc, const TArgs&... args) { @@ -677,31 +667,6 @@ namespace tools LOG_PRINT_L0(e.to_string()); throw e; } - -#else - #include - #include - #include - - template - void throw_wallet_ex(std::string&& loc) - { - TException e(std::move(loc)); - LOG_PRINT_L0(e.to_string()); - throw e; - } - -#define GEN_throw_wallet_ex(z, n, data) \ - template \ - void throw_wallet_ex(std::string&& loc, BOOST_PP_ENUM_BINARY_PARAMS(n, const TArg, &arg)) \ - { \ - TException e(std::move(loc), BOOST_PP_ENUM_PARAMS(n, arg)); \ - LOG_PRINT_L0(e.to_string()); \ - throw e; \ - } - - BOOST_PP_REPEAT_FROM_TO(1, 6, GEN_throw_wallet_ex, ~) -#endif } } @@ -739,14 +704,14 @@ if (!(cond)) } -#define THROW_IF_FALSE_WALLET_EX_MES(cond, err_type, mess, ...) \ +#define THROW_IF_FALSE_WALLET_EX_MES(cond, err_type, mes, ...) \ if (!(cond)) \ { \ exception_handler(); \ std::stringstream ss; \ - ss << std::endl << mess; \ - LOG_ERROR(#cond << ". THROW EXCEPTION: " << #err_type); \ - tools::error::throw_wallet_ex(std::string(__FILE__ ":" STRINGIZE(__LINE__)) + ss.str(), ## __VA_ARGS__); \ + ss << mes; \ + LOG_ERROR(#cond << ". THROW EXCEPTION: " << #err_type << " : " << ss.str()); \ + tools::error::throw_wallet_ex(std::string(__FILE__ ":" STRINGIZE(__LINE__)), ss.str(), ## __VA_ARGS__); \ }