From cccb0e209f276c85513297cb942ef1f4468d4720 Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Sun, 31 Jan 2021 19:42:02 +0100 Subject: [PATCH] multiple fixes over wallet --- src/currency_core/currency_format_utils.cpp | 8 +++-- src/currency_core/currency_format_utils.h | 3 +- src/wallet/wallet2.cpp | 33 ++++++++++----------- src/wallet/wallet2.h | 26 +++++++++------- 4 files changed, 38 insertions(+), 32 deletions(-) diff --git a/src/currency_core/currency_format_utils.cpp b/src/currency_core/currency_format_utils.cpp index 7a81b008..17b6749a 100644 --- a/src/currency_core/currency_format_utils.cpp +++ b/src/currency_core/currency_format_utils.cpp @@ -29,6 +29,7 @@ using namespace epee; #include "genesis.h" #include "genesis_acc.h" #include "common/mnemonic-encoding.h" +#include "crypto/bitcoin/sha256_helper.h" namespace currency { @@ -590,16 +591,16 @@ namespace currency // return true; // } //--------------------------------------------------------------- - std::string generate_origin_for_htlc(cons crypto::public_key& redeem, cons crypto::public_key& refund, const account_keys& acc_keys) + std::string generate_origin_for_htlc(const crypto::public_key& redeem, const crypto::public_key& refund, const account_keys& acc_keys) { std::string blob; string_tools::apped_pod_to_strbuff(blob, redeem); string_tools::apped_pod_to_strbuff(blob, refund); string_tools::apped_pod_to_strbuff(blob, acc_keys.spend_secret_key); - crypto::hash origin_hs = cn_fast_hash(blob.data(), blob.size()); + crypto::hash origin_hs = crypto::cn_fast_hash(blob.data(), blob.size()); std::string origin_blob; string_tools::apped_pod_to_strbuff(origin_blob, origin_hs); - return origin_hs; + return origin_blob; } //--------------------------------------------------------------- bool construct_tx_out(const tx_destination_entry& de, const crypto::secret_key& tx_sec_key, size_t output_index, transaction& tx, std::set& deriv_cache, uint8_t tx_outs_attr, const account_keys& self) @@ -645,6 +646,7 @@ namespace currency htlc.pkey_redeem = *target_keys.begin(); //generate refund key crypto::key_derivation derivation = AUTO_VAL_INIT(derivation); + crypto::public_key out_eph_public_key = AUTO_VAL_INIT(out_eph_public_key); bool r = derive_public_key_from_target_address(self.account_address, tx_sec_key, output_index, out_eph_public_key, derivation); CHECK_AND_ASSERT_MES(r, false, "failed to derive_public_key_from_target_address"); htlc.pkey_refund = out_eph_public_key; diff --git a/src/currency_core/currency_format_utils.h b/src/currency_core/currency_format_utils.h index aa90549b..0210d3e4 100644 --- a/src/currency_core/currency_format_utils.h +++ b/src/currency_core/currency_format_utils.h @@ -215,6 +215,7 @@ namespace currency bool is_out_to_acc(const account_keys& acc, const txout_to_key& out_key, const crypto::key_derivation& derivation, size_t output_index); bool is_out_to_acc(const account_keys& acc, const txout_multisig& out_multisig, const crypto::key_derivation& derivation, size_t output_index); bool lookup_acc_outs(const account_keys& acc, const transaction& tx, const crypto::public_key& tx_pub_key, std::vector& outs, uint64_t& money_transfered, crypto::key_derivation& derivation); + bool lookup_acc_outs(const account_keys& acc, const transaction& tx, const crypto::public_key& tx_pub_key, std::vector& outs, uint64_t& money_transfered, crypto::key_derivation& derivation, std::list& htlc_info_list); bool lookup_acc_outs(const account_keys& acc, const transaction& tx, std::vector& outs, uint64_t& money_transfered, crypto::key_derivation& derivation); bool get_tx_fee(const transaction& tx, uint64_t & fee); uint64_t get_tx_fee(const transaction& tx); @@ -298,7 +299,7 @@ namespace currency void append_per_block_increments_for_tx(const transaction& tx, std::unordered_map& gindices); std::string get_word_from_timstamp(uint64_t timestamp, bool use_password); uint64_t get_timstamp_from_word(std::string word, bool& password_used); - std::string generate_origin_for_htlc(cons crypto::public_key& redeem, cons crypto::public_key& refund, const account_keys& acc_keys); + std::string generate_origin_for_htlc(const crypto::public_key& redeem, const crypto::public_key& refund, const account_keys& acc_keys); template typename std::conditional::value, const std::vector, std::vector >::type& get_txin_etc_options(t_txin_v& in) { diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index e19452c6..85dba890 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -389,12 +389,12 @@ void wallet2::process_new_transaction(const currency::transaction& tx, uint64_t continue; } - auto it = m_active_htlcs.find(std::make_pair(in_htlc.amount, boost::get<>(in_htlc.key_offsets[0]))); + auto it = m_active_htlcs.find(std::make_pair(in_htlc.amount, boost::get(in_htlc.key_offsets[0]))); if (it != m_active_htlcs.end()) { transfer_details& td = m_transfers[it->second]; - WLT_THROW_IF_FALSE_WALLET_INT_ERR_EX(td->m_ptx_wallet_info->m_tx.vout.size() > td.m_internal_output_index, "Internal error: wrong index in m_transfers"); - WLT_THROW_IF_FALSE_WALLET_INT_ERR_EX(td->m_ptx_wallet_info->m_tx.vout[td.m_internal_output_index].target.type() > typeid(), "Internal error: wrong index in m_transfers"); + WLT_THROW_IF_FALSE_WALLET_INT_ERR_EX(td.m_ptx_wallet_info->m_tx.vout.size() > td.m_internal_output_index, "Internal error: wrong index in m_transfers"); + WLT_THROW_IF_FALSE_WALLET_INT_ERR_EX(td.m_ptx_wallet_info->m_tx.vout[td.m_internal_output_index].target.type() == typeid(txout_htlc), "Internal error: wrong index in m_transfers"); //input spend active htlc m_transfers[it->second].m_spent_height = height; } @@ -460,7 +460,7 @@ void wallet2::process_new_transaction(const currency::transaction& tx, uint64_t } else if (tx.vout[o].target.type() == typeid(txout_htlc)) { - THROW_IF_FALSE_WALLET_EX(htlc_info_list.size() > 0, "Found txout_htlc out but htlc_info_list is empty"); + THROW_IF_TRUE_WALLET_INT_ERR_EX(htlc_info_list.size() > 0, "Found txout_htlc out but htlc_info_list is empty"); if (htlc_info_list.front().hltc_our_out_is_before_expiration) { out_key = boost::get(tx.vout[o].target).pkey_redeem; @@ -473,7 +473,7 @@ void wallet2::process_new_transaction(const currency::transaction& tx, uint64_t } else { - THROW_IF_FALSE_WALLET_EX(false, "Unexpected out type im wallet: " << tx.vout[o].target.type().name()); + THROW_IF_TRUE_WALLET_INT_ERR_EX(false, "Unexpected out type im wallet: " << tx.vout[o].target.type().name()); } //const currency::txout_to_key& otk = boost::get(tx.vout[o].target); @@ -569,6 +569,7 @@ void wallet2::process_new_transaction(const currency::transaction& tx, uint64_t } } uint64_t amount = tx.vout[o].amount; + size_t transfer_index = m_transfers.size() - 1; if (tx.vout[o].target.type() == typeid(txout_htlc)) { const txout_htlc& hltc = boost::get(tx.vout[o].target); @@ -577,9 +578,9 @@ void wallet2::process_new_transaction(const currency::transaction& tx, uint64_t //create entry for htlc input htlc_expiration_trigger het = AUTO_VAL_INIT(het); het.is_wallet_owns_redeem = (out_key == hltc.pkey_redeem) ? true:false; - het.transfer_index = m_transfers.size() - 1; + het.transfer_index = transfer_index; uint64_t expired_if_more_then = td.m_ptx_wallet_info->m_block_height + hltc.expiration; - m_htlcs[expired_if_more_then] = het; + m_htlcs.insert(std::make_pair(expired_if_more_then, het)); if (het.is_wallet_owns_redeem) { @@ -591,7 +592,6 @@ void wallet2::process_new_transaction(const currency::transaction& tx, uint64_t m_active_htlcs[amount_gindex_pair] = transfer_index; m_active_htlcs_txid[get_transaction_hash(tx)] = transfer_index; } - size_t transfer_index = m_transfers.size()-1; if (td.m_key_image != currency::null_ki) m_key_images[td.m_key_image] = transfer_index; @@ -1338,9 +1338,8 @@ void wallet2::process_htlc_triggers_on_block_added(uint64_t height) } else { - const transfer_details& td = m_transfers[it->second]; - m_active_htlcs.erase(it); - auto it_tx = m_active_htlcs_txid.find(td.tx_hash()); + m_active_htlcs.erase(it_active_htlc); + auto it_tx = m_active_htlcs_txid.find(tr.tx_hash()); if (it_tx == m_active_htlcs_txid.end()) { LOG_ERROR("Erasing active htlc(;), but it seems to be already erased"); @@ -1360,7 +1359,7 @@ void wallet2::process_new_blockchain_entry(const currency::block& b, const curre !(height == m_minimum_height || get_blockchain_current_size() <= 1), error::wallet_internal_error, "current_index=" + std::to_string(height) + ", get_blockchain_current_height()=" + std::to_string(get_blockchain_current_size())); - process_htlc_triggers_on_block_added(height) + process_htlc_triggers_on_block_added(height); //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 @@ -2217,7 +2216,7 @@ void wallet2::detach_blockchain(uint64_t including_height) WLT_LOG_BLUE("Transfer [" << i << "] spent height: " << tr.m_spent_height << " -> 0, reason: detaching blockchain", LOG_LEVEL_1); tr.m_spent_height = 0; //check if it's hltc contract - if (tr.m_ptx_wallet_info->m_tx.vout[tr.m_internal_output_index].target == typeid(txout_htlc) && tr.m_flags & WALLET_TRANSFER_DETAIL_FLAG_HTLC_REDEEM) + if (tr.m_ptx_wallet_info->m_tx.vout[tr.m_internal_output_index].target.type() == typeid(txout_htlc) && tr.m_flags & WALLET_TRANSFER_DETAIL_FLAG_HTLC_REDEEM) { //only if htlc was spent as a redeem, then we put htlc back as active const txout_htlc& htlc = boost::get(tr.m_ptx_wallet_info->m_tx.vout[tr.m_internal_output_index].target); @@ -4013,7 +4012,7 @@ void wallet2::send_escrow_proposal(const bc_services::contract_private_details& print_tx_sent_message(tx, "(from multisig)", fee); } //---------------------------------------------------------------------------------------------------- -void wallet2::create_htlc_proposal(uint64_t amount, account_public_address& addr, uint64_t lock_blocks_count, currency::transaction &tx) +void wallet2::create_htlc_proposal(uint64_t amount, const currency::account_public_address& addr, uint64_t lock_blocks_count, currency::transaction &tx) { std::vector extra; std::vector attachments; @@ -4029,17 +4028,17 @@ void wallet2::create_htlc_proposal(uint64_t amount, account_public_address& addr this->transfer(dst, 0, 0, TX_DEFAULT_FEE, extra, attachments, tools::detail::ssi_digit, tools::tx_dust_policy(DEFAULT_DUST_THRESHOLD), result_tx); } //---------------------------------------------------------------------------------------------------- -void wallet2::get_list_of_active_htlc(bool only_redeem_txs, std::list& htlcs) +void wallet2::get_list_of_active_htlc(bool only_redeem_txs, std::list& htlcs) { for (auto htlc_entry : m_active_htlcs_txid) { + const transfer_details& td = m_transfers[htlc_entry.second]; if (only_redeem_txs && !(td.m_flags&WALLET_TRANSFER_DETAIL_FLAG_HTLC_REDEEM)) { continue; } - htlc_entry_info entry = AUTO_VAL_INIT(entry); + wallet_public::htlc_entry_info entry = AUTO_VAL_INIT(entry); entry.tx_id = htlc_entry.first; - const transfer_details& td = m_transfers[htlc_entry.second]; entry.amount = td.m_ptx_wallet_info->m_tx.vout[td.m_internal_output_index].amount; WLT_THROW_IF_FALSE_WALLET_INT_ERR_EX(td.m_ptx_wallet_info->m_tx.vout[td.m_internal_output_index].target.type() == typeid(txout_htlc), "[get_list_of_active_htlc]Internal error: unexpected type of out"); diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 1629953e..e5d58067 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -398,7 +398,12 @@ namespace tools END_KV_SERIALIZE_MAP() }; - + //used in wallet + struct htlc_expiration_trigger + { + bool is_wallet_owns_redeem; //specify if this HTLC belong to this wallet by pkey_redeem or by pkey_refund + uint64_t transfer_index; + }; struct payment_details @@ -818,9 +823,9 @@ namespace tools uint64_t get_sync_progress(); uint64_t get_wallet_file_size()const; void set_use_deffered_global_outputs(bool use); - void create_htlc_proposal(uint64_t amount, account_public_address& addr, uint64_t lock_blocks_count, - account_public_address& addr, currency::transaction &tx); - void get_list_of_active_htlc(bool only_redeem_txs, std::list& htlcs); + void create_htlc_proposal(uint64_t amount, const currency::account_public_address& addr, uint64_t lock_blocks_count, + currency::transaction &tx); + void get_list_of_active_htlc(bool only_redeem_txs, std::list& htlcs); void redeem_htlc(const crypto::hash& htlc_tx_id, const std::string& origin); private: @@ -983,12 +988,6 @@ private: std::unordered_set m_unconfirmed_multisig_transfers; std::unordered_map m_tx_keys; - //used in wallet - struct htlc_expiration_trigger - { - bool is_wallet_owns_redeem; //specify if this HTLC belong to this wallet by pkey_redeem or by pkey_refund - uint64_t transfer_index; - }; std::multimap m_htlcs; //map [expired_if_more_then] -> height of expiration amount_gindex_to_transfer_id_container m_active_htlcs; // map [amount; gindex] -> transfer index std::unordered_map m_active_htlcs_txid; // map [txid] -> transfer index, limitation: 1 transactiom -> 1 htlc @@ -1051,7 +1050,12 @@ namespace boost a & static_cast(x); } - + template + inline void serialize(Archive &a, tools::wallet2::htlc_expiration_trigger &x, const boost::serialization::version_type ver) + { + a & x.is_wallet_owns_redeem; + a & x.transfer_index; + } template inline void serialize(Archive& a, tools::wallet2::payment_details& x, const boost::serialization::version_type ver)