From 6b2831e52514e2f639dacc6e5041b4a8a733fb95 Mon Sep 17 00:00:00 2001 From: sowle Date: Fri, 25 Nov 2022 23:01:45 +0100 Subject: [PATCH] wallet: sweep_below adapted for HF4 --- src/simplewallet/simplewallet.cpp | 4 +- src/wallet/wallet2.cpp | 100 ++++++++++++++++++++---------- src/wallet/wallet2.h | 2 +- src/wallet/wallet_rpc_server.cpp | 3 +- 4 files changed, 70 insertions(+), 39 deletions(-) diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 7f4ab1b0..9537390a 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -1822,9 +1822,7 @@ bool simple_wallet::sweep_below(const std::vector &args) uint64_t amount_total = 0, amount_swept = 0; currency::transaction result_tx = AUTO_VAL_INIT(result_tx); std::string filename = "zano_tx_unsigned"; - m_wallet->sweep_below(fake_outs_count, addr, amount, payment_id, fee, outs_total, amount_total, outs_swept, &result_tx, &filename); - if (!get_inputs_money_amount(result_tx, amount_swept)) - LOG_ERROR("get_inputs_money_amount failed, tx: " << obj_to_json_str(result_tx)); + m_wallet->sweep_below(fake_outs_count, addr, amount, payment_id, fee, outs_total, amount_total, outs_swept, amount_swept, &result_tx, &filename); success_msg_writer(false) << outs_swept << " outputs (" << print_money_brief(amount_swept) << " coins) of " << outs_total << " total (" << print_money_brief(amount_total) << ") below the specified limit of " << print_money_brief(amount) << " were successfully swept"; diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 9764eab0..59a48fa1 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -499,7 +499,7 @@ void wallet2::process_new_transaction(const currency::transaction& tx, uint64_t uint64_t max_out_unlock_time = 0; std::vector outs; - uint64_t tx_money_got_in_outs = 0; + uint64_t tx_money_got_in_outs = 0; // TODO: @#@# correctly calculate tx_money_got_in_outs for post-HF4 crypto::public_key tx_pub_key = null_pkey; bool r = parse_and_validate_tx_extra(tx, tx_pub_key); THROW_IF_TRUE_WALLET_EX(!r, error::tx_extra_parse_error, tx); @@ -4981,7 +4981,6 @@ bool wallet2::prepare_tx_sources(size_t fake_outputs_count, std::vectorm_tx.vout[td.m_internal_output_index]); VARIANT_CASE_CONST(tx_out_bare, o) { @@ -5762,8 +5761,10 @@ void wallet2::prepare_tx_destinations(uint64_t needed_money, detail::split_strategy_id_t destination_split_strategy_id, const tx_dust_policy& dust_policy, const std::vector& dsts, - std::vector& final_detinations, const crypto::hash& asset_id) + std::vector& final_destinations, const crypto::hash& asset_id) { + WLT_THROW_IF_FALSE_WALLET_INT_ERR_EX(found_money >= needed_money, "needed_money==" << needed_money << " < found_money==" << found_money); + currency::tx_destination_entry change_dts = AUTO_VAL_INIT(change_dts); if (needed_money < found_money) { @@ -5771,25 +5772,44 @@ void wallet2::prepare_tx_destinations(uint64_t needed_money, change_dts.amount = found_money - needed_money; change_dts.asset_id = asset_id; } - WLT_THROW_IF_FALSE_WALLET_INT_ERR_EX(found_money >= needed_money, "needed_money==" << needed_money << " < found_money==" << found_money); - if (asset_id != currency::null_hash) + + if (is_in_hardfork_zone(ZANO_HARDFORK_04_ZARCANUM)) { - if (change_dts.amount) + // TODO: consider allowing to set them somewhere + size_t min_number_of_outputs = CURRENCY_TX_MIN_ALLOWED_OUTS; + size_t num_digits_to_keep = CURRENCY_TX_OUTS_RND_SPLIT_DIGITS_TO_KEEP; + for(auto& dst : dsts) { - final_detinations.push_back(change_dts); + decompose_amount_randomly(dst.amount, [&](uint64_t a){ final_destinations.emplace_back(a, dst.addr, dst.asset_id); }, min_number_of_outputs, num_digits_to_keep); + } + if (change_dts.amount > 0) + { + if (final_destinations.size() >= min_number_of_outputs - 1) + final_destinations.emplace_back(std::move(change_dts)); // don't split the change if there's enough outs + else + decompose_amount_randomly(change_dts.amount, [&](uint64_t a){ final_destinations.emplace_back(a, change_dts.addr, change_dts.asset_id); }, min_number_of_outputs, num_digits_to_keep); } - return; } - - uint64_t dust = 0; - bool r = detail::apply_split_strategy_by_id(destination_split_strategy_id, dsts, change_dts, dust_policy.dust_threshold, final_detinations, dust, WALLET_MAX_ALLOWED_OUTPUT_AMOUNT); - WLT_THROW_IF_FALSE_WALLET_INT_ERR_EX(r, "invalid split strategy id: " << destination_split_strategy_id); - WLT_THROW_IF_FALSE_WALLET_INT_ERR_EX(dust_policy.dust_threshold >= dust, "invalid dust value: dust = " << dust << ", dust_threshold = " << dust_policy.dust_threshold); - - if (0 != dust && !dust_policy.add_to_fee) + else { - final_detinations.push_back(currency::tx_destination_entry(dust, dust_policy.addr_for_dust)); - final_detinations.back().asset_id = asset_id; + // pre-HF4 + if (asset_id != currency::null_hash) + { + if (change_dts.amount) + { + final_destinations.push_back(change_dts); + } + return; + } + uint64_t dust = 0; + bool r = detail::apply_split_strategy_by_id(destination_split_strategy_id, dsts, change_dts, dust_policy.dust_threshold, final_destinations, dust, WALLET_MAX_ALLOWED_OUTPUT_AMOUNT); + WLT_THROW_IF_FALSE_WALLET_INT_ERR_EX(r, "invalid split strategy id: " << destination_split_strategy_id); + WLT_THROW_IF_FALSE_WALLET_INT_ERR_EX(dust_policy.dust_threshold >= dust, "invalid dust value: dust = " << dust << ", dust_threshold = " << dust_policy.dust_threshold); + + if (0 != dust && !dust_policy.add_to_fee) + { + final_destinations.emplace_back(dust, dust_policy.addr_for_dust, asset_id); + } } } //---------------------------------------------------------------------------------------------------- @@ -6114,7 +6134,7 @@ void wallet2::transfer(construct_tx_param& ctp, } //---------------------------------------------------------------------------------------------------- void wallet2::sweep_below(size_t fake_outs_count, const currency::account_public_address& destination_addr, uint64_t threshold_amount, const currency::payment_id_t& payment_id, - uint64_t fee, size_t& outs_total, uint64_t& amount_total, size_t& outs_swept, currency::transaction* p_result_tx /* = nullptr */, std::string* p_filename_or_unsigned_tx_blob_str /* = nullptr */) + uint64_t fee, size_t& outs_total, uint64_t& amount_total, size_t& outs_swept, uint64_t& amount_swept, currency::transaction* p_result_tx /* = nullptr */, std::string* p_filename_or_unsigned_tx_blob_str /* = nullptr */) { static const size_t estimated_bytes_per_input = 78; const size_t estimated_max_inputs = static_cast(CURRENCY_MAX_TRANSACTION_BLOB_SIZE / (estimated_bytes_per_input * (fake_outs_count + 1.5))); // estimated number of maximum tx inputs under the tx size limit @@ -6124,6 +6144,7 @@ void wallet2::sweep_below(size_t fake_outs_count, const currency::account_public outs_total = 0; amount_total = 0; outs_swept = 0; + amount_swept = 0; std::vector selected_transfers; selected_transfers.reserve(m_transfers.size()); @@ -6158,10 +6179,11 @@ void wallet2::sweep_below(size_t fake_outs_count, const currency::account_public if (fake_outs_count > 0) { COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::request req = AUTO_VAL_INIT(req); + req.height_upper_limit = m_last_pow_block_h; req.use_forced_mix_outs = false; req.decoys_count = fake_outs_count + 1; for (uint64_t i : selected_transfers) - req.amounts.push_back(m_transfers[i].amount()); + req.amounts.push_back(m_transfers[i].is_zc() ? 0 : m_transfers[i].m_amount); r = m_core_proxy->call_COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS(req, rpc_get_random_outs_resp); @@ -6228,16 +6250,14 @@ void wallet2::sweep_below(size_t fake_outs_count, const currency::account_public { if (td.m_global_output_index == daemon_oe.global_amount_index) continue; - tx_output_entry oe = AUTO_VAL_INIT(oe); - oe.out_reference = daemon_oe.global_amount_index; - oe.stealth_address = daemon_oe.stealth_address; - src.outputs.push_back(oe); + src.outputs.emplace_back(daemon_oe.global_amount_index, daemon_oe.stealth_address, daemon_oe.concealing_point, daemon_oe.amount_commitment); if (src.outputs.size() >= fake_outs_count) break; } } // insert real output into src.outputs + // TODO: bad design, we need to get rid of code duplicates below -- sowle auto it_to_insert = std::find_if(src.outputs.begin(), src.outputs.end(), [&](const tx_output_entry& a) { if (a.out_reference.type().hash_code() == typeid(uint64_t).hash_code()) @@ -6245,17 +6265,32 @@ void wallet2::sweep_below(size_t fake_outs_count, const currency::account_public return false; // TODO: implement deterministics real output placement in case there're ref_by_id outs }); tx_output_entry real_oe = AUTO_VAL_INIT(real_oe); - real_oe.out_reference = td.m_global_output_index; - //@#@ - if(td.m_ptx_wallet_info->m_tx.vout[td.m_internal_output_index].type() != typeid(tx_out_bare)) - continue; - const tx_out_bare& out_b = boost::get(td.m_ptx_wallet_info->m_tx.vout[td.m_internal_output_index]); - real_oe.stealth_address = boost::get(out_b.target).key; - auto inserted_it = src.outputs.insert(it_to_insert, real_oe); + txout_ref_v out_reference = td.m_global_output_index; // TODO: use ref_by_id when neccessary + std::vector::iterator interted_it = src.outputs.end(); + VARIANT_SWITCH_BEGIN(td.m_ptx_wallet_info->m_tx.vout[td.m_internal_output_index]); + VARIANT_CASE_CONST(tx_out_bare, o) + { + VARIANT_SWITCH_BEGIN(o.target); + VARIANT_CASE_CONST(txout_to_key, o) + interted_it = src.outputs.emplace(it_to_insert, out_reference, o.key); + VARIANT_CASE_CONST(txout_htlc, htlc) + interted_it = src.outputs.emplace(it_to_insert, out_reference, htlc.pkey_refund); + VARIANT_CASE_OTHER() + { + WLT_THROW_IF_FALSE_WITH_CODE(false, + "Internal error: unexpected type of target: " << o.target.type().name(), + API_RETURN_CODE_INTERNAL_ERROR); + } + VARIANT_SWITCH_END(); + } + VARIANT_CASE_CONST(tx_out_zarcanum, o); + interted_it = src.outputs.emplace(it_to_insert, out_reference, o.stealth_address, o.concealing_point, o.amount_commitment); + WLT_THROW_IF_FALSE_WALLET_INT_ERR_EX(td.m_opt_blinding_mask, "m_opt_blinding_mask is null, transfer index: " << tr_index << ", amount: " << print_money_brief(td.amount())); + src.real_out_amount_blinding_mask = *td.m_opt_blinding_mask; + VARIANT_SWITCH_END(); src.real_out_tx_key = get_tx_pub_key_from_extra(td.m_ptx_wallet_info->m_tx); - src.real_output = inserted_it - src.outputs.begin(); + src.real_output = interted_it - src.outputs.begin(); src.real_output_in_tx_index = td.m_internal_output_index; - //detail::print_source_entry(src); } if (amount_swept <= fee) @@ -6284,7 +6319,6 @@ void wallet2::sweep_below(size_t fake_outs_count, const currency::account_public }; size_t st_index_upper_boundary = std::min(selected_transfers.size(), estimated_max_inputs); - uint64_t amount_swept = 0; try_construct_result_t res = try_construct_tx(st_index_upper_boundary, ftp, amount_swept); WLT_THROW_IF_FALSE_WALLET_CMN_ERR_EX(res != rc_too_few_outputs, st_index_upper_boundary << " biggest unspent outputs have total amount of " << print_money_brief(amount_swept) diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index b3e3ca38..737e8beb 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -714,7 +714,7 @@ namespace tools void submit_transfer_files(const std::string& signed_tx_file, currency::transaction& tx); void sweep_below(size_t fake_outs_count, const currency::account_public_address& destination_addr, uint64_t threshold_amount, const currency::payment_id_t& payment_id, - uint64_t fee, size_t& outs_total, uint64_t& amount_total, size_t& outs_swept, currency::transaction* p_result_tx = nullptr, std::string* p_filename_or_unsigned_tx_blob_str = nullptr); + uint64_t fee, size_t& outs_total, uint64_t& amount_total, size_t& outs_swept, uint64_t& amount_swept, currency::transaction* p_result_tx = nullptr, std::string* p_filename_or_unsigned_tx_blob_str = nullptr); bool get_transfer_address(const std::string& adr_str, currency::account_public_address& addr, std::string& payment_id); inline uint64_t get_blockchain_current_size() const { diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp index e2c953a5..fb804956 100644 --- a/src/wallet/wallet_rpc_server.cpp +++ b/src/wallet/wallet_rpc_server.cpp @@ -620,9 +620,8 @@ namespace tools uint64_t amount_total = 0, amount_swept = 0; std::string unsigned_tx_blob_str; - m_wallet.sweep_below(req.mixin, addr, req.amount, payment_id, req.fee, outs_total, amount_total, outs_swept, &tx, &unsigned_tx_blob_str); + m_wallet.sweep_below(req.mixin, addr, req.amount, payment_id, req.fee, outs_total, amount_total, outs_swept, amount_swept, &tx, &unsigned_tx_blob_str); - get_inputs_money_amount(tx, amount_swept); res.amount_swept = amount_swept; res.amount_total = amount_total; res.outs_swept = outs_swept;