diff options
Diffstat (limited to 'src/wallet')
-rw-r--r-- | src/wallet/wallet2.cpp | 113 | ||||
-rw-r--r-- | src/wallet/wallet2.h | 186 |
2 files changed, 206 insertions, 93 deletions
diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index aa4e2f518..7b898755a 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -32,14 +32,16 @@ bool wallet2::init(const std::string& daemon_address, uint64_t upper_transaction return true; } //---------------------------------------------------------------------------------------------------- -bool wallet2::process_new_transaction(cryptonote::transaction& tx, uint64_t height) +bool wallet2::process_new_transaction(cryptonote::transaction& tx, uint64_t height, fail_details& fd) { std::vector<size_t> outs; uint64_t tx_money_got_in_outs = 0; crypto::public_key tx_pub_key = null_pkey; bool r = parse_and_validate_tx_extra(tx, tx_pub_key); + fd.reason = fail_details::error_to_parse_tx_extra; CHECK_AND_ASSERT_MES(r && tx_pub_key != null_pkey, false, "process_new_transaction failed."); r = lookup_acc_outs(m_account.get_keys(), tx, tx_pub_key, outs, tx_money_got_in_outs); + fd.reason = fail_details::error_invalid_tx; CHECK_AND_ASSERT_MES(r, false, "call lookup_acc_outs failed"); if(outs.size() && tx_money_got_in_outs) { @@ -49,14 +51,24 @@ bool wallet2::process_new_transaction(cryptonote::transaction& tx, uint64_t heig cryptonote::COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES::response res = AUTO_VAL_INIT(res); req.txid = get_transaction_hash(tx); bool r = net_utils::invoke_http_bin_remote_command2(m_daemon_address + "/get_o_indexes.bin", req, res, m_http_client, WALLET_RCP_CONNECTION_TIMEOUT); - CHECK_AND_ASSERT_MES(r, false, "failed to get_o_indexes.bin"); - if(res.status != CORE_RPC_STATUS_OK) - return false;// in case of split while lookup_acc_outs, transaction could be lost (especially if it is coinbase tx) + if (!r) fd.reason = fail_details::error_not_connected; + else if (CORE_RPC_STATUS_BUSY == res.status) fd.reason = fail_details::error_daemon_is_busy; + else if (CORE_RPC_STATUS_OK != res.status) fd.reason = fail_details::error_internal_error; + else fd.reason = fail_details::error_ok; + if (fail_details::error_ok != fd.reason) + { + // in case of split while lookup_acc_outs, transaction could be lost (especially if it is coinbase tx) + LOG_PRINT_L0("failed to invoke get_o_indexes.bin: " << interpret_rpc_response(r, res.status)); + return false; + } + + fd.reason = fail_details::error_internal_error; CHECK_AND_ASSERT_MES(res.o_indexes.size() == tx.vout.size(), false, "internal error: transactions outputs size=" << tx.vout.size() - << " not match with COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES response size=" << res.o_indexes.size()); + << " not match with COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES response size=" << res.o_indexes.size()); BOOST_FOREACH(size_t o, outs) { + fd.reason = fail_details::error_invalid_tx; CHECK_AND_ASSERT_MES(o < tx.vout.size(), false, "wrong out in transaction: internal index=" << o << ", total_outs" << tx.vout.size()); m_transfers.push_back(boost::value_initialized<transfer_details>()); transfer_details& td = m_transfers.back(); @@ -67,10 +79,12 @@ bool wallet2::process_new_transaction(cryptonote::transaction& tx, uint64_t heig td.m_spent = false; cryptonote::keypair in_ephemeral; cryptonote::generate_key_image_helper(m_account.get_keys(), tx_pub_key, o, in_ephemeral, td.m_key_image); + fd.reason = fail_details::error_internal_error; CHECK_AND_ASSERT_MES(in_ephemeral.pub == boost::get<cryptonote::txout_to_key>(tx.vout[o].target).key, - false, "internal error: at key_image generating ephemeral public key not matched with output_key"); + false, "internal error: at key_image generating ephemeral public key not matched with output_key"); m_key_images[td.m_key_image] = m_transfers.size()-1; - LOG_PRINT_L1("Received money: " << print_money(td.amount()) << ", with tx: " << get_transaction_hash(tx)); + LOG_PRINT_COLOR("Received money: " << print_money(td.amount()) << ", with tx: " << get_transaction_hash(tx), + LOG_LEVEL_0, epee::log_space::console_color_green); } } // check all outputs for spending (compare key images) @@ -81,22 +95,24 @@ bool wallet2::process_new_transaction(cryptonote::transaction& tx, uint64_t heig auto it = m_key_images.find(boost::get<cryptonote::txin_to_key>(in).k_image); if(it != m_key_images.end()) { - LOG_PRINT_L1("Spent money: " << print_money(boost::get<cryptonote::txin_to_key>(in).amount) << ", with tx: " << get_transaction_hash(tx)); + LOG_PRINT_COLOR("Spent money: " << print_money(boost::get<cryptonote::txin_to_key>(in).amount) << ", with tx: " << get_transaction_hash(tx), + LOG_LEVEL_0, epee::log_space::console_color_magenta); m_transfers[it->second].m_spent = true; } } return true; } //---------------------------------------------------------------------------------------------------- -bool wallet2::process_new_blockchain_entry(cryptonote::block& b, cryptonote::block_complete_entry& bche, crypto::hash& bl_id, uint64_t height) +bool wallet2::process_new_blockchain_entry(cryptonote::block& b, cryptonote::block_complete_entry& bche, crypto::hash& bl_id, uint64_t height, fail_details& fd) { //handle transactions from new block + fd.reason = fail_details::error_internal_error; CHECK_AND_ASSERT_MES(height == m_blockchain.size(), false, "internal error: current_index=" << height << ", m_blockchain.size()=" << m_blockchain.size()); //optimization: seeking only for blocks that are not older then the wallet creation time plus 1 day. 1 day is for possible user incorrect time setup if(b.timestamp + 60*60*24 > m_account.get_createtime()) { TIME_MEASURE_START(miner_tx_handle_time); - bool r = process_new_transaction(b.miner_tx, height); + bool r = process_new_transaction(b.miner_tx, height, fd); TIME_MEASURE_FINISH(miner_tx_handle_time); CHECK_AND_NO_ASSERT_MES(r, false, "failed to process transaction"); @@ -105,8 +121,9 @@ bool wallet2::process_new_blockchain_entry(cryptonote::block& b, cryptonote::blo { cryptonote::transaction tx; r = parse_and_validate_tx_from_blob(txblob, tx); + fd.reason = fail_details::error_to_parse_tx; CHECK_AND_ASSERT_MES(r, false, "failed to parse and validate transaction from blob"); - r = process_new_transaction(tx, height); + r = process_new_transaction(tx, height, fd); CHECK_AND_ASSERT_MES(r, false, "failed to process transaction"); } TIME_MEASURE_FINISH(txs_handle_time); @@ -149,18 +166,26 @@ bool wallet2::get_short_chain_history(std::list<crypto::hash>& ids) return true; } //---------------------------------------------------------------------------------------------------- -bool wallet2::pull_blocks(size_t& blocks_added) +bool wallet2::pull_blocks(size_t& blocks_added, fail_details& fd) { blocks_added = 0; cryptonote::COMMAND_RPC_GET_BLOCKS_FAST::request req = AUTO_VAL_INIT(req); cryptonote::COMMAND_RPC_GET_BLOCKS_FAST::response res = AUTO_VAL_INIT(res); get_short_chain_history(req.block_ids); bool r = net_utils::invoke_http_bin_remote_command2(m_daemon_address + "/getblocks.bin", req, res, m_http_client, WALLET_RCP_CONNECTION_TIMEOUT); - CHECK_AND_ASSERT_MES(r, false, "failed to get blocks"); - CHECK_AND_ASSERT_MES(res.status == CORE_RPC_STATUS_OK, false, "failed to get blocks"); + if (!r) fd.reason = fail_details::error_not_connected; + else if (CORE_RPC_STATUS_BUSY == res.status) fd.reason = fail_details::error_daemon_is_busy; + else if (CORE_RPC_STATUS_OK != res.status) fd.reason = fail_details::error_internal_error; + else fd.reason = fail_details::error_ok; + if (fail_details::error_ok != fd.reason) + { + LOG_PRINT_L0("failed to get blocks: " << interpret_rpc_response(r, res.status)); + return false; + } //find split position, if split happened + fd.reason = fail_details::error_internal_error; CHECK_AND_ASSERT_MES(res.start_height < m_blockchain.size(), false, "wrong daemon response: m_start_height=" << res.start_height << " not less than local blockchain size=" << m_blockchain.size()); @@ -169,11 +194,12 @@ bool wallet2::pull_blocks(size_t& blocks_added) { cryptonote::block bl; r = cryptonote::parse_and_validate_block_from_blob(bl_entry.block, bl); + fd.reason = fail_details::error_to_parse_block; CHECK_AND_ASSERT_MES(r, false, "failed to parse/validate block"); crypto::hash bl_id = get_block_hash(bl); if(current_index >= m_blockchain.size()) { - r = process_new_blockchain_entry(bl, bl_entry, bl_id, current_index); + r = process_new_blockchain_entry(bl, bl_entry, bl_id, current_index, fd); if(!r) return false; ++blocks_added; }else @@ -181,12 +207,12 @@ bool wallet2::pull_blocks(size_t& blocks_added) if(bl_id != m_blockchain[current_index]) { //split detected here !!! + fd.reason = fail_details::error_internal_error; CHECK_AND_ASSERT_MES(current_index != res.start_height, false, "wrong daemon response: first block in response " << string_tools::pod_to_hex(bl_id) << "\nnot match with local block id " << string_tools::pod_to_hex(m_blockchain[current_index])); detach_blockchain(current_index); - r = process_new_blockchain_entry(bl, bl_entry, bl_id, current_index); + r = process_new_blockchain_entry(bl, bl_entry, bl_id, current_index, fd); if(!r) return false; - } } ++current_index; @@ -194,19 +220,19 @@ bool wallet2::pull_blocks(size_t& blocks_added) return true; } //---------------------------------------------------------------------------------------------------- -bool wallet2::refresh() +bool wallet2::refresh(fail_details& fd) { size_t blocks_fetched = 0; - return refresh(blocks_fetched); + return refresh(blocks_fetched, fd); } //---------------------------------------------------------------------------------------------------- -bool wallet2::refresh(size_t & blocks_fetched) +bool wallet2::refresh(size_t & blocks_fetched, fail_details& fd) { bool received_money = false; - return refresh(blocks_fetched, received_money); + return refresh(blocks_fetched, received_money, fd); } //---------------------------------------------------------------------------------------------------- -bool wallet2::refresh(size_t & blocks_fetched, bool& received_money) +bool wallet2::refresh(size_t & blocks_fetched, bool& received_money, fail_details& fd) { received_money = false; blocks_fetched = 0; @@ -214,19 +240,19 @@ bool wallet2::refresh(size_t & blocks_fetched, bool& received_money) size_t try_count = 0; crypto::hash last_tx_hash_id = m_transfers.size() ? get_transaction_hash(m_transfers.back().m_tx) : null_hash; - while(true) + while(m_run.load(std::memory_order_relaxed)) { - bool res = pull_blocks(added_blocks); + bool res = pull_blocks(added_blocks, fd); if(!res) { if(try_count < 3) { - LOG_PRINT_L0("Another try pull_blocks(try_count=" << try_count << ")..."); + LOG_PRINT_L1("Another try pull_blocks (try_count=" << try_count << ")..."); ++try_count; continue; }else { - LOG_PRINT_L0("pull_blocks failed, try_count=" << try_count); + LOG_PRINT_L1("pull_blocks failed, try_count=" << try_count); return false; } } @@ -238,7 +264,7 @@ bool wallet2::refresh(size_t & blocks_fetched, bool& received_money) if(last_tx_hash_id != (m_transfers.size() ? get_transaction_hash(m_transfers.back().m_tx) : null_hash)) received_money = true; - LOG_PRINT_L2( "Refresh done, blocks received: " << blocks_fetched << ", balance: " << print_money(balance()) << ", unlocked: " << print_money(unlocked_balance())); + LOG_PRINT_L1("Refresh done, blocks received: " << blocks_fetched << ", balance: " << print_money(balance()) << ", unlocked: " << print_money(unlocked_balance())); return true; } //---------------------------------------------------------------------------------------------------- @@ -439,25 +465,6 @@ bool wallet2::store() return r; } //---------------------------------------------------------------------------------------------------- -void wallet2::show_incoming_transfers() -{ - uint64_t amount = 0; - if(!m_transfers.size()) - { - LOG_PRINT_L0("No incoming transfers"); - return; - } - BOOST_FOREACH(transfer_details& td, m_transfers) - { - LOG_PRINT_L0("transfer: " << print_money(td.amount()) - << ", spent: " << td.m_spent - << ", global_index: " << td.m_global_output_index - << ", tx_id: " << get_transaction_hash(td.m_tx)); - if(!td.m_spent) - amount += td.amount(); - } -} -//---------------------------------------------------------------------------------------------------- uint64_t wallet2::unlocked_balance() { uint64_t amount = 0; @@ -535,7 +542,7 @@ namespace } } //---------------------------------------------------------------------------------------------------- -uint64_t wallet2::select_transfers(uint64_t needed_money, uint64_t dust, std::list<transfer_container::iterator>& selected_transfers) +uint64_t wallet2::select_transfers(uint64_t needed_money, bool add_dust, uint64_t dust, std::list<transfer_container::iterator>& selected_transfers) { std::vector<size_t> unused_transfers_indices; std::vector<size_t> unused_dust_indices; @@ -551,19 +558,19 @@ uint64_t wallet2::select_transfers(uint64_t needed_money, uint64_t dust, std::li } } - bool at_least_one_dust_selected = unused_dust_indices.empty(); + bool select_one_dust = add_dust && !unused_dust_indices.empty(); uint64_t found_money = 0; while (found_money < needed_money && (!unused_transfers_indices.empty() || !unused_dust_indices.empty())) { size_t idx; - if (at_least_one_dust_selected) + if (select_one_dust) { - idx = !unused_transfers_indices.empty() ? pop_random_value(unused_transfers_indices) : pop_random_value(unused_dust_indices); + idx = pop_random_value(unused_dust_indices); + select_one_dust = false; } else { - idx = pop_random_value(unused_dust_indices); - at_least_one_dust_selected = true; + idx = !unused_transfers_indices.empty() ? pop_random_value(unused_transfers_indices) : pop_random_value(unused_dust_indices); } transfer_container::iterator it = m_transfers.begin() + idx; @@ -580,7 +587,7 @@ bool wallet2::transfer(const std::vector<cryptonote::tx_destination_entry>& dsts return transfer(dsts, fake_outputs_count, unlock_time, fee, detail::digit_split_strategy, tx_dust_policy(fee), tx); } //---------------------------------------------------------------------------------------------------- -bool wallet2::transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee, cryptonote::transaction& tx, transafer_fail_details& tfd) +bool wallet2::transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee, cryptonote::transaction& tx, fail_details& tfd) { return transfer(dsts, fake_outputs_count, unlock_time, fee, detail::digit_split_strategy, tx_dust_policy(fee), tx, tfd); } diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 27799e2da..f26f8022e 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -25,11 +25,33 @@ namespace tools { + inline std::string interpret_rpc_response(bool ok, const std::string& status) + { + std::string err; + if (ok) + { + if (status == CORE_RPC_STATUS_BUSY) + { + err = "daemon is busy. Please try later"; + } + else if (status != CORE_RPC_STATUS_OK) + { + err = status; + } + } + else + { + err = "possible lost connection to daemon"; + } + return err; + } + + class wallet2 { - wallet2(const wallet2&){}; + wallet2(const wallet2&) : m_run(true) {}; public: - wallet2(){}; + wallet2() : m_run(true) {}; struct transfer_details { uint64_t m_block_height; @@ -68,20 +90,46 @@ namespace tools END_SERIALIZE() }; - struct transafer_fail_details + struct fail_details { enum fail_reason { error_ok = 0, error_not_connected, + error_daemon_is_busy, error_rejected_by_daemon, - error_too_big_transaction, - error_not_enough_money, + error_too_big_transaction, + error_not_enough_money, + error_too_big_mixin, + error_to_parse_block, + error_to_parse_tx, + error_to_parse_tx_extra, + error_invalid_tx, error_internal_error }; fail_reason reason; uint64_t tx_blob_size; uint64_t max_expected_tx_blob_size; + + std::string what() const + { + switch (reason) + { + case error_ok: return "OK"; + case error_not_connected: return "not connected"; + case error_daemon_is_busy: return "daemon is busy. Please try later"; + case error_rejected_by_daemon: return "rejected by daemon"; + case error_too_big_transaction: return "transaction size is too big"; + case error_not_enough_money: return "not enough money"; + case error_too_big_mixin: return "not enough outputs for specified mixin_count"; + case error_to_parse_block: return "failed to parse/validate block"; + case error_to_parse_tx: return "failed to parse/validate tx"; + case error_to_parse_tx_extra: return "failed to parse/validate tx extra"; + case error_invalid_tx: return "wrong tx"; + case error_internal_error: return "internal error"; + default: return "unknown error"; + } + } }; bool generate(const std::string& wallet, const std::string& password); @@ -91,23 +139,26 @@ namespace tools bool init(const std::string& daemon_address = "http://localhost:8080", uint64_t upper_transaction_size_limit = CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE*2 - CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE); - bool refresh(); - bool refresh(size_t & blocks_fetched); - bool refresh(size_t & blocks_fetched, bool& received_money); + bool refresh(fail_details& fd); + bool refresh(size_t & blocks_fetched, fail_details& fd); + bool refresh(size_t & blocks_fetched, bool& received_money, fail_details& fd); bool deinit(); + void stop() { m_run.store(false, std::memory_order_relaxed); } + uint64_t balance(); uint64_t unlocked_balance(); - void show_incoming_transfers(); + template<typename T> + bool enum_incoming_transfers(const T& handler) const; template<typename T> bool transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee, T destination_split_strategy, const tx_dust_policy& dust_policy); template<typename T> - bool transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee, T destination_split_strategy, const tx_dust_policy& dust_policy, cryptonote::transaction &tx, transafer_fail_details& tfd); + bool transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee, T destination_split_strategy, const tx_dust_policy& dust_policy, cryptonote::transaction &tx, fail_details& tfd); template<typename T> bool transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee, T destination_split_strategy, const tx_dust_policy& dust_policy, cryptonote::transaction &tx); bool transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee); bool transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee, cryptonote::transaction& tx); - bool transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee, cryptonote::transaction& tx, transafer_fail_details& tfd); + bool transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee, cryptonote::transaction& tx, fail_details& tfd); bool check_connection(); bool get_transfers(wallet2::transfer_container& incoming_transfers); uint64_t get_blockchain_current_height() const { return m_local_bc_height; } @@ -125,15 +176,15 @@ namespace tools private: bool store_keys(const std::string& keys_file_name, const std::string& password); bool load_keys(const std::string& keys_file_name, const std::string& password); - bool process_new_transaction(cryptonote::transaction& tx, uint64_t height); - bool process_new_blockchain_entry(cryptonote::block& b, cryptonote::block_complete_entry& bche, crypto::hash& bl_id, uint64_t height); + bool process_new_transaction(cryptonote::transaction& tx, uint64_t height, fail_details& fd); + bool process_new_blockchain_entry(cryptonote::block& b, cryptonote::block_complete_entry& bche, crypto::hash& bl_id, uint64_t height, fail_details& fd); bool detach_blockchain(uint64_t height); bool get_short_chain_history(std::list<crypto::hash>& ids); bool is_tx_spendtime_unlocked(uint64_t unlock_time) const; bool is_transfer_unlocked(const transfer_details& td) const; bool clear(); - bool pull_blocks(size_t& blocks_added); - uint64_t select_transfers(uint64_t needed_money, uint64_t dust, std::list<transfer_container::iterator>& selected_transfers); + bool pull_blocks(size_t& blocks_added, fail_details& fd); + uint64_t select_transfers(uint64_t needed_money, bool add_dust, uint64_t dust, std::list<transfer_container::iterator>& selected_transfers); bool prepare_file_names(const std::string& file_path); cryptonote::account_base m_account; @@ -148,6 +199,8 @@ namespace tools std::unordered_map<crypto::key_image, size_t> m_key_images; cryptonote::account_public_address m_account_public_address; uint64_t m_upper_transaction_size_limit; //TODO: auto-calc this value or request from daemon, now use some fixed value + + std::atomic<bool> m_run; }; } BOOST_CLASS_VERSION(tools::wallet2, 5) @@ -229,12 +282,29 @@ namespace tools { std::string indexes; std::for_each(src.outputs.begin(), src.outputs.end(), [&](const cryptonote::tx_source_entry::output_entry& s_e) { indexes += boost::to_string(s_e.first) + " "; }); - std::cout << "amount=" << cryptonote::print_money(src.amount) << ", real_output=" <<src.real_output << ", real_output_in_tx_index=" << src.real_output_in_tx_index << ", indexes: " << indexes << ENDL; + LOG_PRINT_L0("amount=" << cryptonote::print_money(src.amount) << ", real_output=" <<src.real_output << ", real_output_in_tx_index=" << src.real_output_in_tx_index << ", indexes: " << indexes); } //---------------------------------------------------------------------------------------------------- } //---------------------------------------------------------------------------------------------------- template<typename T> + bool wallet2::enum_incoming_transfers(const T& handler) const + { + if(!m_transfers.empty()) + { + BOOST_FOREACH(const transfer_details& td, m_transfers) + { + handler(td.m_tx, td.m_global_output_index, td.amount(), td.m_spent); + } + return true; + } + else + { + return false; + } + } + //---------------------------------------------------------------------------------------------------- + template<typename T> bool wallet2::transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee, T destination_split_strategy, const tx_dust_policy& dust_policy) { @@ -246,13 +316,13 @@ namespace tools bool wallet2::transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, uint64_t unlock_time, uint64_t fee, T destination_split_strategy, const tx_dust_policy& dust_policy, cryptonote::transaction &tx) { - transafer_fail_details stub = AUTO_VAL_INIT(stub); + fail_details stub = AUTO_VAL_INIT(stub); return transfer(dsts, fake_outputs_count, unlock_time, fee, destination_split_strategy, dust_policy, tx, stub); } template<typename T> bool wallet2::transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count, - uint64_t unlock_time, uint64_t fee, T destination_split_strategy, const tx_dust_policy& dust_policy, cryptonote::transaction &tx, transafer_fail_details& tfd) + uint64_t unlock_time, uint64_t fee, T destination_split_strategy, const tx_dust_policy& dust_policy, cryptonote::transaction &tx, fail_details& tfd) { using namespace cryptonote; @@ -264,12 +334,13 @@ namespace tools } std::list<transfer_container::iterator> selected_transfers; - uint64_t found_money = select_transfers(needed_money, dust_policy.dust_threshold, selected_transfers); + uint64_t found_money = select_transfers(needed_money, 0 == fake_outputs_count, dust_policy.dust_threshold, selected_transfers); if(found_money < needed_money) { - LOG_ERROR("not enough money, available only " << print_money(found_money) << ", expected " << print_money(needed_money) ); - tfd.reason = transafer_fail_details::error_not_enough_money; + LOG_ERROR("not enough money, available only " << print_money(found_money) << ", transaction amount " << + print_money(needed_money) << " = " << print_money(needed_money - fee) << " + " << print_money(fee) << " (fee)"); + tfd.reason = fail_details::error_not_enough_money; return false; } //typedef COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount outs_for_amount; @@ -279,7 +350,6 @@ namespace tools COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::response daemon_resp = AUTO_VAL_INIT(daemon_resp); if(fake_outputs_count) { - COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::request req = AUTO_VAL_INIT(req); req.outs_count = fake_outputs_count + 1;// add one to make possible (if need) to skip real output key BOOST_FOREACH(transfer_container::iterator it, selected_transfers) @@ -288,15 +358,37 @@ namespace tools << it->m_internal_output_index << " more than " << it->m_tx.vout.size()); req.amounts.push_back(it->amount()); } + bool r = net_utils::invoke_http_bin_remote_command2(m_daemon_address + "/getrandom_outs.bin", req, daemon_resp, m_http_client, 200000); - tfd.reason = transafer_fail_details::error_not_connected; - CHECK_AND_ASSERT_MES(r, false, "failed to get getrandom_outs"); - tfd.reason = transafer_fail_details::error_internal_error; - CHECK_AND_ASSERT_MES(daemon_resp.status == CORE_RPC_STATUS_OK, false, "failed to getrandom_outs.bin"); - CHECK_AND_ASSERT_MES(daemon_resp.outs.size() == selected_transfers.size(), false, "internal error: daemon returned wrong response for getrandom_outs, wrong amounts count = " + if (!r) tfd.reason = fail_details::error_not_connected; + else if (CORE_RPC_STATUS_BUSY == daemon_resp.status) tfd.reason = fail_details::error_daemon_is_busy; + else if (CORE_RPC_STATUS_OK != daemon_resp.status) tfd.reason = fail_details::error_internal_error; + else tfd.reason = fail_details::error_ok; + if (fail_details::error_ok != tfd.reason) + { + LOG_PRINT_L0("failed to invoke getrandom_outs.bin: " << interpret_rpc_response(r, daemon_resp.status)); + return false; + } + + tfd.reason = fail_details::error_internal_error; + CHECK_AND_ASSERT_MES(daemon_resp.outs.size() == selected_transfers.size(), false, + "internal error: daemon returned wrong response for getrandom_outs.bin, wrong amounts count = " << daemon_resp.outs.size() << ", expected " << selected_transfers.size()); + + tfd.reason = fail_details::error_ok; + BOOST_FOREACH(COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount& amount_outs, daemon_resp.outs) + { + if (amount_outs.outs.size() != fake_outputs_count) + { + tfd.reason = fail_details::error_too_big_mixin; + LOG_PRINT_L0("not enough outputs to mix output " << print_money(amount_outs.amount) << ", requested " << + fake_outputs_count << ", found " << amount_outs.outs.size()); + } + } + if (fail_details::error_ok != tfd.reason) + return false; } - tfd.reason = transafer_fail_details::error_ok; + tfd.reason = fail_details::error_ok; //prepare inputs size_t i = 0; @@ -356,34 +448,48 @@ namespace tools { splitted_dsts.push_back(cryptonote::tx_destination_entry(dust, dust_policy.addr_for_dust)); } - - tfd.reason = transafer_fail_details::error_internal_error; + + tfd.reason = fail_details::error_internal_error; bool r = cryptonote::construct_tx(m_account.get_keys(), sources, splitted_dsts, tx, unlock_time); CHECK_AND_ASSERT_MES(r, false, "Transaction construction failed"); //check transaction size if(get_object_blobsize(tx) >= m_upper_transaction_size_limit) { - LOG_PRINT_RED("Transaction size is too big: " << get_object_blobsize(tx) << ", expected size < " << m_upper_transaction_size_limit, LOG_LEVEL_2); - tfd.reason = transafer_fail_details::error_too_big_transaction; + LOG_PRINT_L0("Transaction size is too big: " << get_object_blobsize(tx) << ", expected size < " << m_upper_transaction_size_limit); + tfd.reason = fail_details::error_too_big_transaction; tfd.tx_blob_size = get_object_blobsize(tx); tfd.max_expected_tx_blob_size = m_upper_transaction_size_limit; return false; } - COMMAND_RPC_SEND_RAW_TX::request req; req.tx_as_hex = epee::string_tools::buff_to_hex_nodelimer(tx_to_blob(tx)); COMMAND_RPC_SEND_RAW_TX::response daemon_send_resp; - tfd.reason = transafer_fail_details::error_not_connected; r = net_utils::invoke_http_json_remote_command2(m_daemon_address + "/sendrawtransaction", req, daemon_send_resp, m_http_client, 200000); - CHECK_AND_ASSERT_MES(r, false, "failed to send transaction"); - if(daemon_send_resp.status != CORE_RPC_STATUS_OK) + if (!r) + { + tfd.reason = fail_details::error_not_connected; + LOG_PRINT_L0("failed to send transaction: " << interpret_rpc_response(r, daemon_send_resp.status)); + return false; + } + else if (CORE_RPC_STATUS_BUSY == daemon_send_resp.status) { - tfd.reason = transafer_fail_details::error_rejected_by_daemon; - LOG_ERROR("daemon failed to accept generated transaction, id: " << get_transaction_hash(tx) ); + tfd.reason = fail_details::error_daemon_is_busy; + LOG_PRINT_L0("failed to send transaction: " << interpret_rpc_response(r, daemon_send_resp.status)); return false; } + else if (CORE_RPC_STATUS_OK != daemon_send_resp.status) + { + tfd.reason = fail_details::error_rejected_by_daemon; + LOG_ERROR("daemon failed to accept generated transaction, id: " << get_transaction_hash(tx)); + return false; + } + else + { + tfd.reason = fail_details::error_ok; + } + std::string key_images; std::for_each(tx.vin.begin(), tx.vin.end(), [&](const txin_v& s_e) -> bool { @@ -397,12 +503,12 @@ namespace tools it->m_spent = true; LOG_PRINT_L0("Transaction successfully sent. <" << get_transaction_hash(tx) << ">" << ENDL - << "Commission: " << print_money(fee+dust) << "(dust: " << print_money(dust) << ")" << ENDL + << "Commission: " << print_money(fee+dust) << " (dust: " << print_money(dust) << ")" << ENDL << "Balance: " << print_money(balance()) << ENDL << "Unlocked: " << print_money(unlocked_balance()) << ENDL << "Please, wait for confirmation for your balance to be unlocked."); - tfd.reason = transafer_fail_details::error_ok; + tfd.reason = fail_details::error_ok; return true; } } |