From 21718b01b962d980d9eb786c5994a3ad1217e21a Mon Sep 17 00:00:00 2001 From: sowle Date: Fri, 6 Oct 2023 15:31:36 +0200 Subject: [PATCH] coretests: ionic_swap_basic_test clean-up and improvements --- tests/core_tests/ionic_swap_tests.cpp | 118 ++++++++++---------------- tests/core_tests/ionic_swap_tests.h | 4 - 2 files changed, 44 insertions(+), 78 deletions(-) diff --git a/tests/core_tests/ionic_swap_tests.cpp b/tests/core_tests/ionic_swap_tests.cpp index 68c7d855..f6f1d1d0 100644 --- a/tests/core_tests/ionic_swap_tests.cpp +++ b/tests/core_tests/ionic_swap_tests.cpp @@ -10,11 +10,6 @@ #include "random_helper.h" #include "tx_builder.h" -#define AMOUNT_TO_TRANSFER_MULTIASSETS_BASIC (TESTS_DEFAULT_FEE) - -#define AMOUNT_ASSETS_TO_TRANSFER_MULTIASSETS_BASIC 500000000000000000 - - ionic_swap_basic_test::ionic_swap_basic_test() { @@ -29,12 +24,7 @@ ionic_swap_basic_test::ionic_swap_basic_test() bool ionic_swap_basic_test::generate(std::vector& events) const { - // NOTE: This test is made deterministic to be able to correctly set up checkpoint. - random_state_test_restorer::reset_random(); // random generator's state was previously stored, will be restore on dtor (see also m_random_state_test_restorer) - uint64_t ts = 1450000000; - test_core_time::adjust(ts); - - ts = test_core_time::get_time(); + uint64_t ts = test_core_time::get_time(); m_accounts.resize(TOTAL_ACCS_COUNT); currency::account_base& miner_acc = m_accounts[MINER_ACC_IDX]; miner_acc.generate(); miner_acc.set_createtime(ts); currency::account_base& alice_acc = m_accounts[ALICE_ACC_IDX]; alice_acc.generate(); alice_acc.set_createtime(ts); @@ -54,43 +44,34 @@ bool ionic_swap_basic_test::c1(currency::core& c, size_t ev_index, const std::ve { bool r = false; std::shared_ptr miner_wlt = init_playtime_test_wallet(events, c, MINER_ACC_IDX); - miner_wlt->get_account().set_createtime(0); - - //std::shared_ptr alice_wlt = init_playtime_test_wallet(events, c, ALICE_ACC_IDX); - //std::shared_ptr bob_wlt = init_playtime_test_wallet(events, c, BOB_ACC_IDX); + std::shared_ptr alice_wlt = init_playtime_test_wallet(events, c, ALICE_ACC_IDX); + std::shared_ptr bob_wlt = init_playtime_test_wallet(events, c, BOB_ACC_IDX); // check passing over the hardfork CHECK_AND_ASSERT_MES(c.get_blockchain_storage().is_hardfork_active(ZANO_HARDFORK_04_ZARCANUM), false, "ZANO_HARDFORK_04_ZARCANUM is active"); - - currency::account_base alice_acc; - alice_acc.generate(); - std::shared_ptr alice_wlt = init_playtime_test_wallet(events, c, alice_acc); - alice_wlt->get_account().set_createtime(0); - - - currency::account_base bob_acc; - bob_acc.generate(); - std::shared_ptr bob_wlt = init_playtime_test_wallet(events, c, bob_acc); - bob_wlt->get_account().set_createtime(0); - - miner_wlt->refresh(); currency::asset_descriptor_base adb = AUTO_VAL_INIT(adb); - adb.total_max_supply = 1000000000000000000; //1M coins + adb.total_max_supply = 1000000 * COIN; //1M coins adb.full_name = "Test coins"; adb.ticker = "TCT"; adb.decimal_point = 12; - std::vector destinations(2); - destinations[0].addr.push_back(bob_wlt->get_account().get_public_address()); - destinations[0].amount = AMOUNT_ASSETS_TO_TRANSFER_MULTIASSETS_BASIC; + uint64_t alice_amount = adb.total_max_supply / 2; + uint64_t bob_amount = adb.total_max_supply / 2; + + std::vector destinations(4); + destinations[0].addr.push_back(m_accounts[ALICE_ACC_IDX].get_public_address()); + destinations[0].amount = alice_amount; destinations[0].asset_id = currency::null_pkey; - destinations[1].addr.push_back(alice_wlt->get_account().get_public_address()); - destinations[1].amount = AMOUNT_ASSETS_TO_TRANSFER_MULTIASSETS_BASIC; + destinations[1].addr.push_back(m_accounts[BOB_ACC_IDX].get_public_address()); + destinations[1].amount = bob_amount; destinations[1].asset_id = currency::null_pkey; + destinations[2] = currency::tx_destination_entry(COIN, m_accounts[ALICE_ACC_IDX].get_public_address()); + destinations[3] = currency::tx_destination_entry(COIN, m_accounts[BOB_ACC_IDX].get_public_address()); + LOG_PRINT_MAGENTA("destinations[0].asset_id:" << destinations[0].asset_id, LOG_LEVEL_0); LOG_PRINT_MAGENTA("destinations[1].asset_id:" << destinations[1].asset_id, LOG_LEVEL_0); LOG_PRINT_MAGENTA("currency::null_pkey:" << currency::null_pkey, LOG_LEVEL_0); @@ -100,11 +81,11 @@ bool ionic_swap_basic_test::c1(currency::core& c, size_t ev_index, const std::ve miner_wlt->deploy_new_asset(adb, destinations, tx, asset_id); LOG_PRINT_L0("Deployed new asset: " << asset_id << ", tx_id: " << currency::get_transaction_hash(tx)); - currency::transaction res_tx = AUTO_VAL_INIT(res_tx); - miner_wlt->transfer(COIN, alice_wlt->get_account().get_public_address(), res_tx); - miner_wlt->transfer(COIN, bob_wlt->get_account().get_public_address(), res_tx); + //currency::transaction res_tx = AUTO_VAL_INIT(res_tx); + //miner_wlt->transfer(COIN, alice_wlt->get_account().get_public_address(), res_tx); + //miner_wlt->transfer(COIN, bob_wlt->get_account().get_public_address(), res_tx); - r = mine_next_pow_blocks_in_playtime(miner_wlt->get_account().get_public_address(), c, CURRENCY_MINED_MONEY_UNLOCK_WINDOW); + r = mine_next_pow_blocks_in_playtime(m_accounts[MINER_ACC_IDX].get_public_address(), c, CURRENCY_MINED_MONEY_UNLOCK_WINDOW); CHECK_AND_ASSERT_MES(r, false, "mine_next_pow_blocks_in_playtime failed"); @@ -119,11 +100,9 @@ bool ionic_swap_basic_test::c1(currency::core& c, size_t ev_index, const std::ve CHECK_AND_ASSERT_MES(it_asset != balances.end() && 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"); + CHECK_AND_ASSERT_MES(it_asset->second.total == bob_amount, false, "Failed to find needed asset in result balances"); CHECK_AND_ASSERT_MES(it_native->second.total == 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); @@ -132,21 +111,21 @@ bool ionic_swap_basic_test::c1(currency::core& c, size_t ev_index, const std::ve CHECK_AND_ASSERT_MES(it_asset != balances.end() && it_native != balances.end(), false, "Failed to find needed asset in result balances"); CHECK_AND_ASSERT_MES(it_native->second.total == COIN, 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"); + CHECK_AND_ASSERT_MES(it_asset->second.total == alice_amount, false, "Failed to find needed asset in result balances"); const uint64_t assets_to_exchange = 10 * COIN; - const uint64_t native_tokens_to_exchange = COIN/2; + const uint64_t native_coins_to_exchange = COIN/2; { - //alice_wlt want to trade with bob_wlt, to exchange 10.0 TCT to 1.0 ZANO + // Alice wants to trade with Bob, to exchange 10.0 TCT to 0.5 ZANO view::ionic_swap_proposal_info proposal_details = AUTO_VAL_INIT(proposal_details); proposal_details.fee_paid_by_a = TESTS_DEFAULT_FEE; proposal_details.mixins = 10; proposal_details.to_bob.push_back(view::asset_funds{ asset_id , assets_to_exchange }); - proposal_details.to_alice.push_back(view::asset_funds{ currency::native_coin_asset_id , native_tokens_to_exchange }); + proposal_details.to_alice.push_back(view::asset_funds{ currency::native_coin_asset_id , native_coins_to_exchange }); tools::wallet_public::ionic_swap_proposal proposal = AUTO_VAL_INIT(proposal); - alice_wlt->create_ionic_swap_proposal(proposal_details, bob_wlt->get_account().get_public_address(), proposal); + alice_wlt->create_ionic_swap_proposal(proposal_details, m_accounts[BOB_ACC_IDX].get_public_address(), proposal); view::ionic_swap_proposal_info proposal_decoded_info = AUTO_VAL_INIT(proposal_decoded_info); bob_wlt->get_ionic_swap_proposal_info(proposal, proposal_decoded_info); @@ -160,50 +139,47 @@ bool ionic_swap_basic_test::c1(currency::core& c, size_t ev_index, const std::ve { CHECK_AND_ASSERT_MES(false, false, "proposal actual and proposals decoded mismatch"); } + currency::transaction res_tx2 = AUTO_VAL_INIT(res_tx2); r = bob_wlt->accept_ionic_swap_proposal(proposal, res_tx2); 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); + r = mine_next_pow_blocks_in_playtime(m_accounts[MINER_ACC_IDX].get_public_address(), c, CURRENCY_MINED_MONEY_UNLOCK_WINDOW); CHECK_AND_ASSERT_MES(r, false, "mine_next_pow_blocks_in_playtime failed"); - bob_wlt->refresh(); + alice_wlt->refresh(); - - balances.clear(); alice_wlt->balance(balances, mined); - it_asset = balances.find(asset_id); it_native = balances.find(currency::native_coin_asset_id); 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 + COIN - TESTS_DEFAULT_FEE, 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"); + CHECK_AND_ASSERT_MES(it_native->second.total == native_coins_to_exchange + COIN - TESTS_DEFAULT_FEE, false, "Failed to find needed asset in result balances"); + CHECK_AND_ASSERT_MES(it_asset->second.total == alice_amount - assets_to_exchange, false, "Failed to find needed asset in result balances"); + bob_wlt->refresh(); balances.clear(); bob_wlt->balance(balances, mined); it_asset = balances.find(asset_id); it_native = balances.find(currency::native_coin_asset_id); - 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 == COIN - 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"); + CHECK_AND_ASSERT_MES(it_native->second.total == COIN - native_coins_to_exchange, false, "Failed to find needed asset in result balances"); + CHECK_AND_ASSERT_MES(it_asset->second.total == bob_amount + assets_to_exchange, false, "Failed to find needed asset in result balances"); { - //now alice_wlt want to trade with bob_wlt, to send 1.0 ZANO and get 10.0 TCT in exchange + //now Alice want to trade with Bob, to send 0.5 ZANO and get 10.0 TCT in exchange view::ionic_swap_proposal_info proposal_details = AUTO_VAL_INIT(proposal_details); proposal_details.fee_paid_by_a = TESTS_DEFAULT_FEE; proposal_details.mixins = 10; - proposal_details.to_bob.push_back(view::asset_funds{ currency::native_coin_asset_id , native_tokens_to_exchange }); + proposal_details.to_bob.push_back(view::asset_funds{ currency::native_coin_asset_id , native_coins_to_exchange }); proposal_details.to_alice.push_back(view::asset_funds{ asset_id , assets_to_exchange }); tools::wallet_public::ionic_swap_proposal proposal = AUTO_VAL_INIT(proposal); - alice_wlt->create_ionic_swap_proposal(proposal_details, bob_wlt->get_account().get_public_address(), proposal); + alice_wlt->create_ionic_swap_proposal(proposal_details, m_accounts[BOB_ACC_IDX].get_public_address(), proposal); view::ionic_swap_proposal_info proposal_decoded_info = AUTO_VAL_INIT(proposal_decoded_info); bob_wlt->get_ionic_swap_proposal_info(proposal, proposal_decoded_info); @@ -217,42 +193,36 @@ bool ionic_swap_basic_test::c1(currency::core& c, size_t ev_index, const std::ve { CHECK_AND_ASSERT_MES(false, false, "proposal actual and proposals decoded mismatch"); } + currency::transaction res_tx2 = AUTO_VAL_INIT(res_tx2); r = bob_wlt->accept_ionic_swap_proposal(proposal, res_tx2); 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); + r = mine_next_pow_blocks_in_playtime(m_accounts[MINER_ACC_IDX].get_public_address(), c, CURRENCY_MINED_MONEY_UNLOCK_WINDOW); CHECK_AND_ASSERT_MES(r, false, "mine_next_pow_blocks_in_playtime failed"); - bob_wlt->refresh(); + alice_wlt->refresh(); - - balances.clear(); alice_wlt->balance(balances, mined); - it_asset = balances.find(asset_id); it_native = balances.find(currency::native_coin_asset_id); 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 + COIN - TESTS_DEFAULT_FEE * 2 - 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 + assets_to_exchange, false, "Failed to find needed asset in result balances"); + CHECK_AND_ASSERT_MES(it_native->second.total == native_coins_to_exchange + COIN - TESTS_DEFAULT_FEE * 2 - native_coins_to_exchange, false, "Failed to find needed asset in result balances"); + CHECK_AND_ASSERT_MES(it_asset->second.total == alice_amount, false, "Failed to find needed asset in result balances"); + bob_wlt->refresh(); balances.clear(); bob_wlt->balance(balances, mined); it_asset = balances.find(asset_id); it_native = balances.find(currency::native_coin_asset_id); - 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 == COIN - native_tokens_to_exchange + 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 - assets_to_exchange, false, "Failed to find needed asset in result balances"); - - - + CHECK_AND_ASSERT_MES(it_native->second.total == COIN - native_coins_to_exchange + native_coins_to_exchange, false, "Failed to find needed asset in result balances"); + CHECK_AND_ASSERT_MES(it_asset->second.total == bob_amount, false, "Failed to find needed asset in result balances"); diff --git a/tests/core_tests/ionic_swap_tests.h b/tests/core_tests/ionic_swap_tests.h index d5a9978f..49041649 100644 --- a/tests/core_tests/ionic_swap_tests.h +++ b/tests/core_tests/ionic_swap_tests.h @@ -4,15 +4,11 @@ #pragma once #include "chaingen.h" #include "wallet_tests_basic.h" -#include "random_helper.h" struct ionic_swap_basic_test : public wallet_test { ionic_swap_basic_test(); bool generate(std::vector& events) const; bool c1(currency::core& c, size_t ev_index, const std::vector& events); - -private: - random_state_test_restorer m_random_state_test_restorer; };