1
0
Fork 0
forked from lthn/blockchain

implemented taking origin from redeemed tx

This commit is contained in:
cryptozoidberg 2021-02-14 00:28:39 +01:00
parent 82d0e335e6
commit 06ca6e035d
No known key found for this signature in database
GPG key ID: 22DEB97A54C6FDEC
4 changed files with 89 additions and 13 deletions

View file

@ -501,16 +501,29 @@ namespace currency
return true;
}
//---------------------------------------------------------------
template<class add_type_t, class container_type>
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<add_type_t>(ev);
}
container.push_back(add_type_t());
return boost::get<add_type_t>(container.back());
}
//---------------------------------------------------------------
template<class extra_t>
extra_t& get_or_add_field_to_extra(std::vector<extra_v>& extra)
{
for (auto& ev : extra)
{
if (ev.type() == typeid(extra_t))
return boost::get<extra_t>(ev);
}
extra.push_back(extra_t());
return boost::get<extra_t>(extra.back());
// for (auto& ev : extra)
// {
// if (ev.type() == typeid(extra_t))
// return boost::get<extra_t>(ev);
// }
// extra.push_back(extra_t());
// return boost::get<extra_t>(extra.back());
return get_or_add_field_to_variant_vector<extra_t>(extra);
}
//---------------------------------------------------------------
template<class variant_t, class variant_type_t>

View file

@ -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<transfer_details_extra_option_htlc_info>();
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<currency::tx_source_entry>& sources, std::vector<uint64_t>& selected_indicies, uint64_t& found_money)

View file

@ -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_option_htlc_info> 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<wallet_public::htlc_entry_info>& 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<tools::wallet2::transfer_details_base&>(x);
if (ver < 3)
return;
a & x.varian_options;
}
template <class Archive>

View file

@ -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<wallet_public::htlc_entry_info> 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;
}