From d5c0e873ef0aa82dcd16489f1500bd5453e6fd26 Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Wed, 3 Feb 2021 00:13:44 +0100 Subject: [PATCH] htlc: fixes here and there --- src/currency_core/blockchain_storage.cpp | 4 +- src/currency_core/currency_format_utils.cpp | 6 +-- .../currency_format_utils_abstract.h | 2 +- .../currency_format_utils_transactions.h | 23 ++++---- src/wallet/wallet2.cpp | 54 ++++++++----------- src/wallet/wallet2.h | 4 +- 6 files changed, 38 insertions(+), 55 deletions(-) diff --git a/src/currency_core/blockchain_storage.cpp b/src/currency_core/blockchain_storage.cpp index 37e14534..901bdfa0 100644 --- a/src/currency_core/blockchain_storage.cpp +++ b/src/currency_core/blockchain_storage.cpp @@ -4325,11 +4325,11 @@ struct outputs_visitor crypto::public_key pk = null_pkey; if (m_scan_context.htlc_is_expired) { - pk = boost::get(out.target).pkey_after_expiration; + pk = boost::get(out.target).pkey_refund; } else { - pk = boost::get(out.target).pkey_before_expiration; + pk = boost::get(out.target).pkey_redeem; } m_results_collector.push_back(pk); }else diff --git a/src/currency_core/currency_format_utils.cpp b/src/currency_core/currency_format_utils.cpp index 1f5054ef..f6aebcc6 100644 --- a/src/currency_core/currency_format_utils.cpp +++ b/src/currency_core/currency_format_utils.cpp @@ -635,9 +635,9 @@ namespace currency tx_out out; out.amount = de.amount; - if (de.additional_options.type() == typeid(destination_option_htlc_out)) + if (de.htlc_options.htlc_hash != null_hash) { - const destination_option_htlc_out& htlc_dest = boost::get(de.additional_options); + const destination_option_htlc_out& htlc_dest = de.htlc_options; //out htlc CHECK_AND_ASSERT_MES(target_keys.size() == 1, false, "Unexpected htl keys count = " << target_keys.size() << ", expected ==1"); txout_htlc htlc = AUTO_VAL_INIT(htlc); @@ -1250,7 +1250,7 @@ namespace currency if (src_entr.htlc_origin.size()) { //add txin_htlc - txin_htlc in_htlc = AUTO_VAL_INIT(inp_htlc); + txin_htlc in_htlc = AUTO_VAL_INIT(in_htlc); in_htlc.hltc_origin = src_entr.htlc_origin; in_v = in_htlc; txin_htlc& in_v_ref = boost::get(in_v); diff --git a/src/currency_core/currency_format_utils_abstract.h b/src/currency_core/currency_format_utils_abstract.h index 8f562120..451b46e3 100644 --- a/src/currency_core/currency_format_utils_abstract.h +++ b/src/currency_core/currency_format_utils_abstract.h @@ -129,7 +129,7 @@ namespace currency } return found; } - + inline const txin_to_key& get_to_key_input_from_txin_v(const txin_v& in_v) { if (in_v.type() == typeid(txin_to_key)) diff --git a/src/currency_core/currency_format_utils_transactions.h b/src/currency_core/currency_format_utils_transactions.h index 881aae98..337d40d7 100644 --- a/src/currency_core/currency_format_utils_transactions.h +++ b/src/currency_core/currency_format_utils_transactions.h @@ -47,24 +47,19 @@ namespace currency END_SERIALIZE() }; - struct destination_option_void - { - BEGIN_SERIALIZE_OBJECT() - END_SERIALIZE() - }; //if this struct is present, then creating htlc out, expiration -> number of blocks that htlc proposal is active struct destination_option_htlc_out { uint64_t expiration; crypto::hash htlc_hash; + BEGIN_SERIALIZE_OBJECT() - FIELD(transfer) - FIELD(origin) + FIELD(expiration) + FIELD(htlc_hash) END_SERIALIZE() }; - typedef boost::variant destination_option_v; struct tx_destination_entry { @@ -73,13 +68,13 @@ namespace currency size_t minimum_sigs; //if txout_multisig: minimum signatures that are required to spend this output (minimum_sigs <= addr.size()) IF txout_to_key - not used uint64_t amount_to_provide; //amount money that provided by initial creator of tx, used with partially created transactions uint64_t unlock_time; - destination_option_v additional_options; //additional options + destination_option_htlc_out htlc_options; //htlc options - tx_destination_entry() : amount(0), minimum_sigs(0), amount_to_provide(0), unlock_time(0), additional_options(destination_option_void()){} - tx_destination_entry(uint64_t a, const account_public_address& ad) : amount(a), addr(1, ad), minimum_sigs(0), amount_to_provide(0), unlock_time(0), additional_options(destination_option_void()) {} - tx_destination_entry(uint64_t a, const account_public_address& ad, uint64_t ut) : amount(a), addr(1, ad), minimum_sigs(0), amount_to_provide(0), unlock_time(ut), additional_options(destination_option_void()) {} - tx_destination_entry(uint64_t a, const std::list& addr) : amount(a), addr(addr), minimum_sigs(addr.size()), amount_to_provide(0), unlock_time(0), additional_options(destination_option_void()) {} + tx_destination_entry() : amount(0), minimum_sigs(0), amount_to_provide(0), unlock_time(0), htlc_options(destination_option_htlc_out()){} + tx_destination_entry(uint64_t a, const account_public_address& ad) : amount(a), addr(1, ad), minimum_sigs(0), amount_to_provide(0), unlock_time(0), htlc_options(destination_option_htlc_out()) {} + tx_destination_entry(uint64_t a, const account_public_address& ad, uint64_t ut) : amount(a), addr(1, ad), minimum_sigs(0), amount_to_provide(0), unlock_time(ut), htlc_options(destination_option_htlc_out()) {} + tx_destination_entry(uint64_t a, const std::list& addr) : amount(a), addr(addr), minimum_sigs(addr.size()), amount_to_provide(0), unlock_time(0), htlc_options(destination_option_htlc_out()) {} BEGIN_SERIALIZE_OBJECT() FIELD(amount) @@ -87,7 +82,7 @@ namespace currency FIELD(minimum_sigs) FIELD(amount_to_provide) FIELD(unlock_time) - FIELD(additional_options) + FIELD(htlc_options) END_SERIALIZE() }; diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index f355e55c..e05d063d 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -29,6 +29,7 @@ using namespace epee; #include "currency_core/bc_payments_id_service.h" #include "version.h" #include "common/encryption_filter.h" +#include "crypto/bitcoin/sha256_helper.h" using namespace currency; #define MINIMUM_REQUIRED_WALLET_FREE_SPACE_BYTES (100*1024*1024) // 100 MB @@ -4021,10 +4022,10 @@ void wallet2::create_htlc_proposal(uint64_t amount, const currency::account_publ dst.resize(1); dst.back().addr.push_back(addr); dst.back().amount = amount; - destination_option_htlc_out htlc_option = AUTO_VAL_INIT(htlc_option); + destination_option_htlc_out& htlc_option = dst.back().htlc_options; htlc_option.expiration = 740; //about 12 hours htlc_option.htlc_hash = htlc_hash; - dst.back().additional_options = htlc_option; + transaction result_tx = AUTO_VAL_INIT(result_tx); this->transfer(dst, 0, 0, TX_DEFAULT_FEE, extra, attachments, tools::detail::ssi_digit, tools::tx_dust_policy(DEFAULT_DUST_THRESHOLD), result_tx); @@ -4053,39 +4054,16 @@ void wallet2::get_list_of_active_htlc(bool only_redeem_txs, std::list dst; - dst.resize(1); - dst.back().addr.push_back(m_account.get_keys().account_address); - dst.back().amount = 0; - construct_tx_param ctp = get_default_construct_tx_param(); ctp.fee = TX_DEFAULT_FEE; ctp.htlc_tx_id = htlc_tx_id; ctp.htlc_origin = origin; + ctp.dsts.resize(1); + ctp.dsts.back().addr.push_back(m_account.get_keys().account_address); + ctp.dsts.back().amount = 0; - /* - struct destination_option_htlc_in - { - uint64_t transfer; - std::string origin; - BEGIN_SERIALIZE_OBJECT() - FIELD(transfer) - FIELD(origin) - END_SERIALIZE() - }; - transfer(const construct_tx_param& ctp, - currency::transaction &tx, - bool send_to_network, - std::string* p_unsigned_filename_or_tx_blob_str); - - - */ - - - currency::transaction result_tx = AUTO_VAL_INIT(tx); - transaction result_tx = AUTO_VAL_INIT(result_tx); + currency::transaction result_tx = AUTO_VAL_INIT(result_tx); this->transfer(ctp, result_tx, true, nullptr); @@ -4265,6 +4243,7 @@ bool wallet2::prepare_tx_sources(crypto::hash multisig_id, std::vector& sources, uint64_t& found_money) { + typedef currency::tx_source_entry::output_entry tx_output_entry; //lets figure out, if we have active htlc for this htlc auto it = m_active_htlcs_txid.find(htlc_tx_id); if (it == m_active_htlcs_txid.end()) @@ -4301,7 +4280,7 @@ bool wallet2::prepare_tx_sources_htlc(crypto::hash htlc_tx_id, const std::string sources.push_back(AUTO_VAL_INIT(currency::tx_source_entry())); currency::tx_source_entry& src = sources.back(); - currency::tx_output_entry real_oe = AUTO_VAL_INIT(real_oe); + tx_output_entry real_oe = AUTO_VAL_INIT(real_oe); real_oe.first = td.m_global_output_index; // TODO: use ref_by_id when necessary real_oe.second = htlc_out.pkey_redeem; src.outputs.push_back(real_oe); //m_global_output_index should be prefetched @@ -4861,7 +4840,7 @@ void wallet2::prepare_tx_destinations(uint64_t needed_money, } } //---------------------------------------------------------------------------------------------------- -void wallet2::prepare_transaction(const construct_tx_param& ctp, finalize_tx_param& ftp, const currency::transaction& tx_for_mode_separate /* = currency::transaction() */) +void wallet2::prepare_transaction(construct_tx_param& ctp, finalize_tx_param& ftp, const currency::transaction& tx_for_mode_separate /* = currency::transaction() */) { TIME_MEASURE_START_MS(get_needed_money_time); uint64_t needed_money = get_needed_money(ctp.fee, ctp.dsts); @@ -4882,7 +4861,16 @@ void wallet2::prepare_transaction(const construct_tx_param& ctp, finalize_tx_par else if (ctp.htlc_tx_id != currency::null_hash) { //htlc - prepare_tx_sources_htlc(htlc_tx_id, ctp.htlc_origin, sources, found_money); + prepare_tx_sources_htlc(ctp.htlc_tx_id, ctp.htlc_origin, ftp.sources, found_money); + WLT_THROW_IF_FALSE_WITH_CODE(ctp.dsts.size() == 1, + "htlc: unexpected ctp.dsts.size() =" << ctp.dsts.size(), API_RETURN_CODE_INTERNAL_ERROR); + + WLT_THROW_IF_FALSE_WITH_CODE(found_money > ctp.fee, + "htlc: found money less then fee", API_RETURN_CODE_INTERNAL_ERROR); + + //fill amount + ctp.dsts.begin()->amount = found_money - ctp.fee; + } else if (ctp.multisig_id != currency::null_hash) { @@ -5104,7 +5092,7 @@ void wallet2::check_and_throw_if_self_directed_tx_with_payment_id_requested(cons WLT_THROW_IF_FALSE_WALLET_CMN_ERR_EX(!has_payment_id, "sending funds to yourself with payment id is not allowed"); } //---------------------------------------------------------------------------------------------------- -void wallet2::transfer(const construct_tx_param& ctp, +void wallet2::transfer(construct_tx_param& ctp, currency::transaction &tx, bool send_to_network, std::string* p_unsigned_filename_or_tx_blob_str) diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 91b129db..7a15354d 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -576,7 +576,7 @@ namespace tools const std::vector& attachments, currency::transaction& tx); - void transfer(const construct_tx_param& ctp, + void transfer(construct_tx_param& ctp, currency::transaction &tx, bool send_to_network, std::string* p_unsigned_filename_or_tx_blob_str); @@ -820,7 +820,7 @@ namespace tools const std::list& get_expiration_entries() const { return m_money_expirations; }; bool get_tx_key(const crypto::hash &txid, crypto::secret_key &tx_key) const; - void prepare_transaction(const construct_tx_param& ctp, finalize_tx_param& ftp, const currency::transaction& tx_for_mode_separate = currency::transaction()); + void prepare_transaction(construct_tx_param& ctp, finalize_tx_param& ftp, const currency::transaction& tx_for_mode_separate = currency::transaction()); void finalize_transaction(const finalize_tx_param& ftp, currency::transaction& tx, crypto::secret_key& tx_key, bool broadcast_tx, bool store_tx_secret_key = true); std::string get_log_prefix() const { return m_log_prefix; }