From 06ca6e035da08c259065415d6c8ef9c918d5e51f Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Sun, 14 Feb 2021 00:28:39 +0100 Subject: [PATCH] implemented taking origin from redeemed tx --- src/currency_core/currency_format_utils.h | 27 +++++++++++----- src/wallet/wallet2.cpp | 23 ++++++++++++-- src/wallet/wallet2.h | 14 ++++++++- tests/core_tests/atomic_tests.cpp | 38 +++++++++++++++++++++-- 4 files changed, 89 insertions(+), 13 deletions(-) diff --git a/src/currency_core/currency_format_utils.h b/src/currency_core/currency_format_utils.h index 00240e6c..872df0fa 100644 --- a/src/currency_core/currency_format_utils.h +++ b/src/currency_core/currency_format_utils.h @@ -501,16 +501,29 @@ namespace currency return true; } //--------------------------------------------------------------- + template + extra_t& get_or_add_field_to_variant_vector(container_type& container) + { + for (auto& ev : container) + { + if (ev.type() == typeid(exadd_type_ttra_t)) + return boost::get(ev); + } + container.push_back(add_type_t()); + return boost::get(container.back()); + } + //--------------------------------------------------------------- template extra_t& get_or_add_field_to_extra(std::vector& extra) { - for (auto& ev : extra) - { - if (ev.type() == typeid(extra_t)) - return boost::get(ev); - } - extra.push_back(extra_t()); - return boost::get(extra.back()); +// for (auto& ev : extra) +// { +// if (ev.type() == typeid(extra_t)) +// return boost::get(ev); +// } +// extra.push_back(extra_t()); +// return boost::get(extra.back()); + return get_or_add_field_to_variant_vector(extra); } //--------------------------------------------------------------- template diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 2d78bfe4..5cf45e0e 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -397,7 +397,9 @@ void wallet2::process_new_transaction(const currency::transaction& tx, uint64_t 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; + m_transfers[it->second].m_spent_height = height; + transfer_details_extra_option_htlc_info& tdeohi = get_or_add_field_to_variant_vector(); + tdeohi.origin = in_htlc.hltc_origin; } } i++; @@ -4061,9 +4063,26 @@ void wallet2::redeem_htlc(const crypto::hash& htlc_tx_id, std::string origin) currency::transaction result_tx = AUTO_VAL_INIT(result_tx); this->transfer(ctp, result_tx, true, nullptr); +} +//---------------------------------------------------------------------------------------------------- +bool wallet2::check_htlc_redeemed(const crypto::hash& htlc_tx_id, std::string& origin) +{ + auto it = m_active_htlcs_txid.find(htlc_tx_id); + WLT_THROW_IF_FALSE_WITH_CODE(it != m_active_htlcs_txid.end(), + "htlc not found with tx_id = " << htlc_tx_id, API_RETURN_CODE_NOT_FOUND); - + transfer_details_extra_option_htlc_info htlc_options = AUTO_VAL_INIT(htlc_options); + if (!currency::get_type_in_variant_container(m_transfers[it->second].varian_options, htlc_options)) + { + return false; + } + if (htlc_options.origin.size()) + { + origin = htlc_options.origin; + return true; + } + return false; } //---------------------------------------------------------------------------------------------------- bool wallet2::prepare_tx_sources_for_packing(uint64_t items_to_pack, size_t fake_outputs_count, std::vector& sources, std::vector& selected_indicies, uint64_t& found_money) diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index f70f16ba..92313c20 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -397,10 +397,18 @@ namespace tools }; + struct transfer_details_extra_option_htlc_info + { + std::string origin; //this field filled only if htlc had been redeemed + } + + typedef boost::variant transfer_details_extra_options_v; + struct transfer_details : public transfer_details_base { uint64_t m_global_output_index; crypto::key_image m_key_image; //TODO: key_image stored twice :( + transfer_details_extra_options_v varian_options; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(m_global_output_index) @@ -852,6 +860,7 @@ namespace tools currency::transaction &tx, std::string &origin); void get_list_of_active_htlc(bool only_redeem_txs, std::list& htlcs); void redeem_htlc(const crypto::hash& htlc_tx_id, std::string origin); + bool check_htlc_redeemed(const crypto::hash& htlc_tx_id, std::string& origin); private: @@ -1044,7 +1053,7 @@ private: BOOST_CLASS_VERSION(tools::wallet2, WALLET_FILE_SERIALIZATION_VERSION) BOOST_CLASS_VERSION(tools::wallet_public::wallet_transfer_info, 9) -BOOST_CLASS_VERSION(tools::wallet2::transfer_details, 2) +BOOST_CLASS_VERSION(tools::wallet2::transfer_details, 3) namespace boost @@ -1074,6 +1083,9 @@ namespace boost a & x.m_global_output_index; a & x.m_key_image; a & static_cast(x); + if (ver < 3) + return; + a & x.varian_options; } template diff --git a/tests/core_tests/atomic_tests.cpp b/tests/core_tests/atomic_tests.cpp index fea9167c..c9eb5395 100644 --- a/tests/core_tests/atomic_tests.cpp +++ b/tests/core_tests/atomic_tests.cpp @@ -173,20 +173,52 @@ bool atomic_simple_test::c1(currency::core& c, size_t ev_index, const std::vecto CHECK_AND_ASSERT_MES(htlcs_alice_b.size() == 1, false, "htlcs_alice_b.size() == 1 failed"); std::list htlcs_bob_b; - bob_a_wlt_instance->get_list_of_active_htlc(htlcs_bob_b, false); + bob_b_wlt_instance->get_list_of_active_htlc(htlcs_bob_b, false); CHECK_AND_ASSERT_MES(htlcs_bob_b.size() == 1, false, "htlcs_bob_b.size() == 1 failed"); const wallet_public::htlc_entry_info& hei_bob_b = *htlcs_bob_b.begin(); - CHECK_AND_ASSERT_MES(hei_bob_b.is_redeem == true, false, "hei_bob.is_redeem == true failed"); + CHECK_AND_ASSERT_MES(hei_bob_b.is_redeem == true, false, "hei_bob_b.is_redeem == true failed"); const wallet_public::htlc_entry_info& hei_alice_b = *htlcs_alice_b.begin(); - CHECK_AND_ASSERT_MES(hei_alice_b.is_redeem == false, false, "hei_alice.is_redeem == false failed"); + CHECK_AND_ASSERT_MES(hei_alice_b.is_redeem == false, false, "hei_alice_b.is_redeem == false failed"); CHECK_AND_ASSERT_MES(hei_alice_b.amount == hei_bob_b.amount && hei_alice_b.sha256_hash == hei_bob_b.sha256_hash && hei_alice_b.tx_id == hei_bob_b.tx_id, false, "hei_alice !=hei_bob "); + //now alice redeem her contract in blockchain B + alice_b_wlt_instance->redeem_htlc(hei_alice_b.tx_id, alice_origin); + + r = mine_next_pow_blocks_in_playtime(m_mining_accunt.get_public_address(), c, CURRENCY_MINED_MONEY_UNLOCK_WINDOW); + CHECK_AND_ASSERT_MES(r, false, "mine_next_pow_blocks_in_playtime failed"); + alice_a_wlt_instance->refresh(); + alice_b_wlt_instance->refresh(); + bob_a_wlt_instance->refresh(); + bob_b_wlt_instance->refresh(); + + //htlcs_alice_b.clear(); + //alice_b_wlt_instance->get_list_of_active_htlc(htlcs_alice_b, false); + //CHECK_AND_ASSERT_MES(htlcs_alice_b.size() == 0, false, "htlcs_alice_b.size() == 1 failed"); + + //htlcs_bob_b.clear(); + //bob_b_wlt_instance->get_list_of_active_htlc(htlcs_bob_b, false); + //CHECK_AND_ASSERT_MES(htlcs_bob_b.size() == 0, false, "htlcs_bob_b.size() == 1 failed"); + std::string bob_detected_origin; + r = bob_b_wlt_instance->check_htlc_redeemed(hei_bob_b.tx_id, bob_detected_origin); + CHECK_AND_ASSERT_MES(r, false, "bob_a_wlt_instance->check_htlc_redeemed(hei_bob_b.tx_id, bob_detected_origin); returned false"); + + CHECK_AND_ASSERT_MES(bob_detected_origin == alice_origin, false, "bob_detected_origin == alice_origin failed"); + bob_b_wlt_instance->redeem_htlc(hei_bob.tx_id, bob_detected_origin); + + + r = mine_next_pow_blocks_in_playtime(m_mining_accunt.get_public_address(), c, CURRENCY_MINED_MONEY_UNLOCK_WINDOW); + CHECK_AND_ASSERT_MES(r, false, "mine_next_pow_blocks_in_playtime failed"); + alice_a_wlt_instance->refresh(); + alice_b_wlt_instance->refresh(); + bob_a_wlt_instance->refresh(); + bob_b_wlt_instance->refresh(); + return r; }