From 43b41fec3d0a951d6920a9a98f6dd632ef07b5a8 Mon Sep 17 00:00:00 2001 From: sowle Date: Thu, 19 Oct 2023 14:37:57 +0200 Subject: [PATCH] coretests: check_ionic_swap_tx_outs added, some improvements --- src/currency_core/currency_format_utils.h | 2 + src/wallet/wallet2.cpp | 6 +-- src/wallet/wallet2.h | 6 +-- tests/core_tests/ionic_swap_tests.cpp | 58 +++++++++++++++++++++++ 4 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/currency_core/currency_format_utils.h b/src/currency_core/currency_format_utils.h index aa9e800e..5a2ca910 100644 --- a/src/currency_core/currency_format_utils.h +++ b/src/currency_core/currency_format_utils.h @@ -359,6 +359,8 @@ namespace currency bool derive_public_key_from_target_address(const account_public_address& destination_addr, const crypto::secret_key& tx_sec_key, size_t index, crypto::public_key& out_eph_public_key, crypto::key_derivation& derivation); bool derive_public_key_from_target_address(const account_public_address& destination_addr, const crypto::secret_key& tx_sec_key, size_t index, crypto::public_key& out_eph_public_key); bool derive_key_pair_from_key_pair(const crypto::public_key& src_pub_key, const crypto::secret_key& src_sec_key, crypto::secret_key& derived_sec_key, crypto::public_key& derived_pub_key, const char(&hs_domain)[32], uint64_t index = 0); + uint16_t get_derivation_hint(const crypto::key_derivation& derivation); + tx_derivation_hint make_tx_derivation_hint_from_uint16(uint16_t hint); std::string short_hash_str(const crypto::hash& h); bool is_mixattr_applicable_for_fake_outs_counter(uint8_t mix_attr, uint64_t fake_attr_count); diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index a2283c54..20c36019 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -5427,7 +5427,7 @@ bool wallet2::build_ionic_swap_template(const wallet_public::ionic_swap_proposal return true; } //---------------------------------------------------------------------------------------------------- -bool wallet2::get_ionic_swap_proposal_info(const std::string&raw_proposal, wallet_public::ionic_swap_proposal_info& proposal_info) +bool wallet2::get_ionic_swap_proposal_info(const std::string&raw_proposal, wallet_public::ionic_swap_proposal_info& proposal_info) const { wallet_public::ionic_swap_proposal proposal = AUTO_VAL_INIT(proposal); bool r = t_unserializable_object_from_blob(proposal, raw_proposal); @@ -5435,13 +5435,13 @@ bool wallet2::get_ionic_swap_proposal_info(const std::string&raw_proposal, walle return get_ionic_swap_proposal_info(proposal, proposal_info); } //---------------------------------------------------------------------------------------------------- -bool wallet2::get_ionic_swap_proposal_info(const wallet_public::ionic_swap_proposal& proposal, wallet_public::ionic_swap_proposal_info& proposal_info) +bool wallet2::get_ionic_swap_proposal_info(const wallet_public::ionic_swap_proposal& proposal, wallet_public::ionic_swap_proposal_info& proposal_info) const { wallet_public::ionic_swap_proposal_context ionic_context = AUTO_VAL_INIT(ionic_context); return get_ionic_swap_proposal_info(proposal, proposal_info, ionic_context); } //---------------------------------------------------------------------------------------------------- -bool wallet2::get_ionic_swap_proposal_info(const wallet_public::ionic_swap_proposal& proposal, wallet_public::ionic_swap_proposal_info& proposal_info, wallet_public::ionic_swap_proposal_context& ionic_context) +bool wallet2::get_ionic_swap_proposal_info(const wallet_public::ionic_swap_proposal& proposal, wallet_public::ionic_swap_proposal_info& proposal_info, wallet_public::ionic_swap_proposal_context& ionic_context) const { const transaction& tx = proposal.tx_template; crypto::key_derivation derivation = AUTO_VAL_INIT(derivation); diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index b70ff375..cd32ca48 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -1026,9 +1026,9 @@ namespace tools bool build_ionic_swap_template(const wallet_public::ionic_swap_proposal_info& proposal_detais, const currency::account_public_address& destination_addr, wallet_public::ionic_swap_proposal& proposal, std::vector& selected_transfers_for_template); - bool get_ionic_swap_proposal_info(const std::string&raw_proposal, wallet_public::ionic_swap_proposal_info& proposal_info); - bool get_ionic_swap_proposal_info(const wallet_public::ionic_swap_proposal& proposal, wallet_public::ionic_swap_proposal_info& proposal_info); - bool get_ionic_swap_proposal_info(const wallet_public::ionic_swap_proposal& proposal, wallet_public::ionic_swap_proposal_info& proposal_info, wallet_public::ionic_swap_proposal_context& ionic_context); + bool get_ionic_swap_proposal_info(const std::string&raw_proposal, wallet_public::ionic_swap_proposal_info& proposal_info) const; + bool get_ionic_swap_proposal_info(const wallet_public::ionic_swap_proposal& proposal, wallet_public::ionic_swap_proposal_info& proposal_info) const; + bool get_ionic_swap_proposal_info(const wallet_public::ionic_swap_proposal& proposal, wallet_public::ionic_swap_proposal_info& proposal_info, wallet_public::ionic_swap_proposal_context& ionic_context) const; bool accept_ionic_swap_proposal(const std::string& raw_proposal, currency::transaction& result_tx); bool accept_ionic_swap_proposal(const wallet_public::ionic_swap_proposal& proposal, currency::transaction& result_tx); diff --git a/tests/core_tests/ionic_swap_tests.cpp b/tests/core_tests/ionic_swap_tests.cpp index 690730ce..0ec0602d 100644 --- a/tests/core_tests/ionic_swap_tests.cpp +++ b/tests/core_tests/ionic_swap_tests.cpp @@ -10,6 +10,64 @@ #include "random_helper.h" #include "tx_builder.h" +using namespace currency; + +// Helpers + +bool check_ionic_swap_tx_outs(const std::vector& accounts, const currency::transaction& tx, std::shared_ptr w, const tools::wallet_public::ionic_swap_proposal& proposal) +{ + tools::mode_separate_context msc = AUTO_VAL_INIT(msc); + msc.tx_for_mode_separate = proposal.tx_template; + + tools::wallet_public::ionic_swap_proposal_context ionic_context = AUTO_VAL_INIT(ionic_context); + bool r = w->get_ionic_swap_proposal_info(proposal, msc.proposal_info, ionic_context); + CHECK_AND_ASSERT_MES(r, false, "get_ionic_swap_proposal_info failed"); + + // decode all outputs amounts and asset_ids + std::vector> outs_amounts_and_asset_ids; + outs_amounts_and_asset_ids.resize(tx.vout.size()); + + //ionic_context.one_time_skey + const crypto::public_key& tx_pub_key = get_tx_pub_key_from_extra(tx); + + for(size_t i = 0; i < tx.vout.size(); ++i) + { + VARIANT_SWITCH_BEGIN(tx.vout[i]) + VARIANT_CASE_CONST(currency::tx_out_zarcanum, oz) + for(size_t k = 0; k < accounts.size(); ++k) + { + crypto::key_derivation derivation = AUTO_VAL_INIT(derivation); + bool r = crypto::generate_key_derivation(tx_pub_key, accounts[k].get_keys().view_secret_key, derivation); + CHECK_AND_ASSERT_MES(r, false, "generate_key_derivation failed"); + uint64_t decoded_amount = 0; + crypto::public_key decoded_asset_id{}; + crypto::scalar_t amount_blinding_mask{}; + crypto::scalar_t asset_id_blinding_mask{}; + if (decode_output_amount_and_asset_id(oz, derivation, i, decoded_amount, decoded_asset_id, amount_blinding_mask, asset_id_blinding_mask)) + { + outs_amounts_and_asset_ids[i] = std::make_pair(decoded_amount, decoded_asset_id); + break; + } + } + VARIANT_SWITCH_END() + } + + static_assert(CURRENCY_TX_MIN_ALLOWED_OUTS >= 2); + const size_t expected_number_of_zero_amount_outputs = CURRENCY_TX_MIN_ALLOWED_OUTS - 2; + size_t zero_amount_outputs = 0; + for(auto& oaai : outs_amounts_and_asset_ids) + { + CHECK_AND_ASSERT_MES(oaai.second != null_pkey, false, "Not all outputs were decoded"); + if (oaai.first == 0) + ++zero_amount_outputs; + } + + CHECK_AND_ASSERT_MES(zero_amount_outputs <= expected_number_of_zero_amount_outputs, false, "zero_amount_outputs: " << zero_amount_outputs << ", expected_number_of_zero_amount_outputs: " << expected_number_of_zero_amount_outputs); + + return true; +} + +//---------------------------------------------------------------------------------------------------- ionic_swap_basic_test::ionic_swap_basic_test() {