From 83c5336004b648bcdb3f803a02d1b3d3140b19c8 Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Thu, 16 Mar 2023 19:15:11 +0100 Subject: [PATCH] Core tests for basic ionic swaps scenario: draft for verification code --- src/wallet/wallet2.cpp | 20 ++++++++---- src/wallet/wallet2.h | 3 +- tests/core_tests/ionic_swap_tests.cpp | 46 +++++++++++++++++++++++++-- 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 90d4e988..f6429fde 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -4880,11 +4880,11 @@ bool wallet2::check_htlc_redeemed(const crypto::hash& htlc_tx_id, std::string& o return false; } //---------------------------------------------------------------------------------------------------- -bool wallet2::create_ionic_swap_proposal(const view::ionic_swap_proposal_info& proposal_details, const currency::account_public_address& destination_addr) +bool wallet2::create_ionic_swap_proposal(const view::ionic_swap_proposal_info& proposal_details, const currency::account_public_address& destination_addr, transaction& tx_template) { crypto::secret_key one_time_key = AUTO_VAL_INIT(one_time_key); std::vector selected_transfers_for_template; - transaction tx_template; + build_ionic_swap_template(proposal_details, destination_addr, tx_template, selected_transfers_for_template, one_time_key); const uint32_t mask_to_mark_escrow_template_locked_transfers = WALLET_TRANSFER_DETAIL_FLAG_BLOCKED | WALLET_TRANSFER_DETAIL_FLAG_ESCROW_PROPOSAL_RESERVATION; @@ -5005,15 +5005,23 @@ bool wallet2::get_ionic_swap_proposal_info(const currency::transaction tx, view: proposal.expiration_time = t.v; return true; } + //---------------------------------------------------------------------------------------------------- -bool wallet2::accept_ionic_swap_proposal(std::string&raw_tx_template, currency::transaction& result_tx) +bool wallet2::accept_ionic_swap_proposal(const std::string& raw_tx_template, currency::transaction& result_tx) { - mode_separate_context msc = AUTO_VAL_INIT(msc); - - currency::transaction& tx = msc.tx_for_mode_separate; + currency::transaction tx; bool r = parse_and_validate_tx_from_blob(raw_tx_template, tx); THROW_IF_TRUE_WALLET_EX(!r, error::tx_parse_error, raw_tx_template); + return accept_ionic_swap_proposal(tx, result_tx); +} +//---------------------------------------------------------------------------------------------------- +bool wallet2::accept_ionic_swap_proposal(const currency::transaction& tx_template, currency::transaction& result_tx) +{ + mode_separate_context msc = AUTO_VAL_INIT(msc); + msc.tx_for_mode_separate = tx_template; + + r = get_ionic_swap_proposal_info(tx, msc.proposal); THROW_IF_TRUE_WALLET_EX(!r, error::wallet_internal_error, "Failed to get info from proposal"); diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index ddb69e2e..a53ca4a0 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -930,7 +930,8 @@ namespace tools crypto::secret_key& one_time_key); bool get_ionic_swap_proposal_info(const std::string&raw_tx_template, view::ionic_swap_proposal_info& proposal); bool get_ionic_swap_proposal_info(const currency::transaction tx, view::ionic_swap_proposal_info& proposal); - bool accept_ionic_swap_proposal(std::string&raw_tx_template, currency::transaction& result_tx); + bool accept_ionic_swap_proposal(const std::string&raw_tx_template, currency::transaction& result_tx); + bool accept_ionic_swap_proposal(const currency::transaction& tx_template, currency::transaction& result_tx); private: // -------- t_transport_state_notifier ------------------------------------------------ diff --git a/tests/core_tests/ionic_swap_tests.cpp b/tests/core_tests/ionic_swap_tests.cpp index 509badd8..f2838e52 100644 --- a/tests/core_tests/ionic_swap_tests.cpp +++ b/tests/core_tests/ionic_swap_tests.cpp @@ -105,6 +105,7 @@ bool zarcanum_basic_test::c1(currency::core& c, size_t ev_index, const std::vect CHECK_AND_ASSERT_MES(it_asset->second.total == AMOUNT_ASSETS_TO_TRANSFER_MULTIASSETS_BASIC, false, "Failed to find needed asset in result balances"); CHECK_AND_ASSERT_MES(it_native->second.total == uint64_t(17517226)*COIN, false, "Failed to find needed asset in result balances"); + uint64_t mined_balance = it_native->second.total; balances.clear(); alice_wlt->balance(balances, mined); @@ -116,17 +117,58 @@ bool zarcanum_basic_test::c1(currency::core& c, size_t ev_index, const std::vect CHECK_AND_ASSERT_MES(it_native == balances.end(), false, "Failed to find needed asset in result balances"); CHECK_AND_ASSERT_MES(it_asset->second.total == AMOUNT_ASSETS_TO_TRANSFER_MULTIASSETS_BASIC, false, "Failed to find needed asset in result balances"); + const uint64_t assets_to_exchange = 10 * COIN; + const uint64_t native_tokens_to_exchange = 1 * COIN; + //alice_wlt want to trade with miner_wlt, to exchange 10.0 TCT to 1.0 ZANO view::ionic_swap_proposal_info proposal_details = AUTO_VAL_INIT(proposal_details); proposal_details.expiration_time = alice_wlt->get_core_runtime_config().get_core_time() + 10 * 60; proposal_details.fee = TESTS_DEFAULT_FEE; proposal_details.mixins = 10; - proposal_details.from.push_back(view::asset_funds{ asset_id , 10 * COIN}); - proposal_details.to.push_back(view::asset_funds{ currency::null_pkey , 1 * COIN }); + proposal_details.from.push_back(view::asset_funds{ asset_id , assets_to_exchange }); + proposal_details.to.push_back(view::asset_funds{ currency::null_pkey , native_tokens_to_exchange }); currency::transaction tx_template = AUTO_VAL_INIT(tx_template); alice_wlt->create_ionic_swap_proposal(proposal_details, miner_wlt->get_account().get_public_address(), tx_template); + view::ionic_swap_proposal_info proposal_decoded_info; + miner_wlt->get_ionic_swap_proposal_info(tx_template, proposal_decoded_info); + + //Validate proposal + if (proposal_decoded_info.from != proposal_details.from || proposal_decoded_info.to != proposal_details.to) + { + CHECK_AND_ASSERT_MES(false, false, "proposal actual and proposals decoded mismatch"); + } + + currency::transaction res_tx = AUTO_VAL_INIT(res_tx); + r = miner_wlt->accept_ionic_swap_proposal(tx_template, res_tx); + CHECK_AND_ASSERT_MES(r, false, "Failed to accept ionic proposal"); + + r = mine_next_pow_blocks_in_playtime(miner_wlt->get_account().get_public_address(), c, CURRENCY_MINED_MONEY_UNLOCK_WINDOW); + CHECK_AND_ASSERT_MES(r, false, "mine_next_pow_blocks_in_playtime failed"); + + miner_wlt->refresh(); + alice_wlt->refresh(); + + + balances.clear(); + alice_wlt->balance(balances, mined); + + it_asset = balances.find(asset_id); + it_native = balances.find(currency::null_hash); + + CHECK_AND_ASSERT_MES(it_asset != balances.end(), false, "Failed to find needed asset in result balances"); + CHECK_AND_ASSERT_MES(it_native->second.total == native_tokens_to_exchange, false, "Failed to find needed asset in result balances"); + CHECK_AND_ASSERT_MES(it_asset->second.total == AMOUNT_ASSETS_TO_TRANSFER_MULTIASSETS_BASIC - assets_to_exchange, false, "Failed to find needed asset in result balances"); + + + balances.clear(); + miner_wlt->balance(balances, mined); + + CHECK_AND_ASSERT_MES(it_asset != balances.end(), false, "Failed to find needed asset in result balances"); + CHECK_AND_ASSERT_MES(it_native->second.total == mined_balance - native_tokens_to_exchange, false, "Failed to find needed asset in result balances"); + CHECK_AND_ASSERT_MES(it_asset->second.total == AMOUNT_ASSETS_TO_TRANSFER_MULTIASSETS_BASIC + assets_to_exchange, false, "Failed to find needed asset in result balances"); + return true; }