From 706395e88efdbc2829222f1ecaeaf79aca853a20 Mon Sep 17 00:00:00 2001 From: sowle Date: Mon, 28 Oct 2019 20:45:25 +0300 Subject: [PATCH] coretests: escrow_acceptance_and_balance test added to cover #47 case more carefully --- tests/core_tests/chaingen_main.cpp | 1 + tests/core_tests/escrow_wallet_tests.cpp | 197 +++++++++++++++++++++++ tests/core_tests/escrow_wallet_tests.h | 17 ++ 3 files changed, 215 insertions(+) diff --git a/tests/core_tests/chaingen_main.cpp b/tests/core_tests/chaingen_main.cpp index 230cf874..500bc1a1 100644 --- a/tests/core_tests/chaingen_main.cpp +++ b/tests/core_tests/chaingen_main.cpp @@ -731,6 +731,7 @@ int main(int argc, char* argv[]) GENERATE_AND_PLAY(escrow_cancellation_acceptance_expiration); // GENERATE_AND_PLAY(escrow_proposal_acceptance_in_alt_chain); -- work in progress GENERATE_AND_PLAY(escrow_zero_amounts); + GENERATE_AND_PLAY(escrow_acceptance_and_balance); GENERATE_AND_PLAY(escrow_altchain_meta_test<0>); GENERATE_AND_PLAY(escrow_altchain_meta_test<1>); diff --git a/tests/core_tests/escrow_wallet_tests.cpp b/tests/core_tests/escrow_wallet_tests.cpp index 600339ae..9bbc919f 100644 --- a/tests/core_tests/escrow_wallet_tests.cpp +++ b/tests/core_tests/escrow_wallet_tests.cpp @@ -3095,3 +3095,200 @@ bool escrow_zero_amounts::c1(currency::core& c, size_t ev_index, const std::vect return true; } +//------------------------------------------------------------------------------ + +escrow_acceptance_and_balance::escrow_acceptance_and_balance() + : m_alice_bob_start_amount(0) + , m_alice_bob_start_chunk_amount(0) + , m_alice_fee_proposal(0) + , m_bob_fee_accept(0) + , m_bob_fee_release(0) +{ + REGISTER_CALLBACK_METHOD(escrow_acceptance_and_balance, check_balance_after_proposal_not_confirmed); + REGISTER_CALLBACK_METHOD(escrow_acceptance_and_balance, check_balance_after_proposal_confirmed); + REGISTER_CALLBACK_METHOD(escrow_acceptance_and_balance, check_balance_after_acceptance_not_confirmed); + REGISTER_CALLBACK_METHOD(escrow_acceptance_and_balance, check_balance_after_acceptance_confirmed); +} + +bool escrow_acceptance_and_balance::generate(std::vector& events) const +{ + m_accounts.resize(TOTAL_ACCS_COUNT); + account_base& miner_acc = m_accounts[MINER_ACC_IDX]; miner_acc.generate(); + account_base& alice_acc = m_accounts[ALICE_ACC_IDX]; alice_acc.generate(); + account_base& bob_acc = m_accounts[BOB_ACC_IDX]; bob_acc.generate(); + + MAKE_GENESIS_BLOCK(events, blk_0, miner_acc, test_core_time::get_time()); + REWIND_BLOCKS_N_WITH_TIME(events, blk_0r, blk_0, miner_acc, CURRENCY_MINED_MONEY_UNLOCK_WINDOW); + + m_alice_bob_start_amount = MK_TEST_COINS(200); + uint64_t amount_chunks = 10; + m_alice_bob_start_chunk_amount = m_alice_bob_start_amount / 10; + + transaction tx_0 = AUTO_VAL_INIT(tx_0); + bool r = construct_tx_with_many_outputs(events, blk_0r, miner_acc.get_keys(), alice_acc.get_public_address(), m_alice_bob_start_amount, 10, TESTS_DEFAULT_FEE, tx_0); + CHECK_AND_ASSERT_MES(r, false, "construct_tx_with_many_outputs failed"); + events.push_back(tx_0); + + transaction tx_1 = AUTO_VAL_INIT(tx_1); + r = construct_tx_with_many_outputs(events, blk_0r, miner_acc.get_keys(), bob_acc.get_public_address(), m_alice_bob_start_amount, 10, TESTS_DEFAULT_FEE, tx_1); + CHECK_AND_ASSERT_MES(r, false, "construct_tx_with_many_outputs failed"); + events.push_back(tx_1); + + MAKE_NEXT_BLOCK_TX_LIST(events, blk_1, blk_0r, miner_acc, std::list({tx_0, tx_1})); + + REWIND_BLOCKS_N_WITH_TIME(events, blk_1r, blk_1, miner_acc, WALLET_DEFAULT_TX_SPENDABLE_AGE); + + // prepare contract details + m_cpd = AUTO_VAL_INIT(m_cpd); + m_cpd.amount_a_pledge = MK_TEST_COINS(7); + m_cpd.amount_b_pledge = MK_TEST_COINS(5); + m_cpd.amount_to_pay = MK_TEST_COINS(3); + m_cpd.a_addr = alice_acc.get_public_address(); + m_cpd.b_addr = bob_acc.get_public_address(); + m_alice_fee_proposal = MK_TEST_COINS(4); + m_bob_fee_accept = MK_TEST_COINS(2); + m_bob_fee_release = MK_TEST_COINS(9); // Alice states that Bob should pay this much money for upcoming contract release (which will be sent by Alice) + + std::vector used_sources; + + // escrow proposal + bc_services::proposal_body prop = AUTO_VAL_INIT(prop); + transaction escrow_proposal_tx = AUTO_VAL_INIT(escrow_proposal_tx); + r = build_custom_escrow_proposal(events, blk_1r, alice_acc.get_keys(), m_cpd, 0, 0, 0, blk_1r.timestamp + 36000, 0, m_alice_fee_proposal, m_bob_fee_release, eccf_normal, escrow_proposal_tx, used_sources, &prop); + CHECK_AND_ASSERT_MES(r, false, "build_custom_escrow_proposal failed"); + events.push_back(escrow_proposal_tx); + + DO_CALLBACK(events, "check_balance_after_proposal_not_confirmed"); + + MAKE_NEXT_BLOCK_TX1(events, blk_2, blk_1r, miner_acc, escrow_proposal_tx); + + DO_CALLBACK(events, "check_balance_after_proposal_confirmed"); + + MAKE_NEXT_BLOCK(events, blk_3, blk_2, miner_acc); + + DO_CALLBACK(events, "check_balance_after_proposal_confirmed"); + + // escrow proposal acceptance + transaction escrow_normal_acceptance_tx = prop.tx_template; + uint64_t normal_acceptance_mask = eccf_normal; + r = build_custom_escrow_accept_proposal(events, blk_2, 0, bob_acc.get_keys(), m_cpd, 0, 0, 0, 0, m_bob_fee_accept, m_bob_fee_release, normal_acceptance_mask, prop.tx_onetime_secret_key, escrow_normal_acceptance_tx, used_sources); + CHECK_AND_ASSERT_MES(r, false, "build_custom_escrow_accept_proposal failed"); + + events.push_back(escrow_normal_acceptance_tx); + + DO_CALLBACK(events, "check_balance_after_acceptance_not_confirmed"); + + MAKE_NEXT_BLOCK_TX1(events, blk_4, blk_3, miner_acc, escrow_normal_acceptance_tx); + + DO_CALLBACK(events, "check_balance_after_acceptance_confirmed"); + + MAKE_NEXT_BLOCK(events, blk_5, blk_4, miner_acc); + + DO_CALLBACK(events, "check_balance_after_acceptance_confirmed"); + + return true; +} + +bool escrow_acceptance_and_balance::check_balance_after_proposal_not_confirmed(currency::core& c, size_t ev_index, const std::vector& events) +{ + 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_AND_ASSERT_MES(refresh_wallet_and_check_balance("", "Alice", alice_wlt, + m_alice_bob_start_amount - m_alice_fee_proposal, // total + true, UINT64_MAX, + m_alice_bob_start_amount - 2 * m_alice_bob_start_chunk_amount, // unlocked + 0, // mined + MK_TEST_COINS(0), // awaited in + MK_TEST_COINS(0) // awainted out + ), false, ""); + + CHECK_AND_ASSERT_MES(refresh_wallet_and_check_balance("", "Bob", bob_wlt, + m_alice_bob_start_amount, // total + true, UINT64_MAX, + m_alice_bob_start_amount, // unlocked + 0, // mined + MK_TEST_COINS(0), // awaited in + MK_TEST_COINS(0) // awaited out + ), false, ""); + + return true; +} + +bool escrow_acceptance_and_balance::check_balance_after_proposal_confirmed(currency::core& c, size_t ev_index, const std::vector& events) +{ + 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_AND_ASSERT_MES(refresh_wallet_and_check_balance("", "Alice", alice_wlt, + m_alice_bob_start_amount - m_alice_fee_proposal, // total + true, UINT64_MAX, + m_alice_bob_start_amount - 2 * m_alice_bob_start_chunk_amount, // unlocked + 0, // mined + MK_TEST_COINS(0), // awaited in + MK_TEST_COINS(0) // awaited out + ), false, ""); + + CHECK_AND_ASSERT_MES(refresh_wallet_and_check_balance("", "Bob", bob_wlt, + m_alice_bob_start_amount, // total + true, UINT64_MAX, + m_alice_bob_start_amount, // unlocked + 0, // mined + MK_TEST_COINS(0), // awaited in + MK_TEST_COINS(0) // awaited out + ), false, ""); + + return true; +} + +bool escrow_acceptance_and_balance::check_balance_after_acceptance_not_confirmed(currency::core& c, size_t ev_index, const std::vector& events) +{ + 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_AND_ASSERT_MES(refresh_wallet_and_check_balance("", "Alice", alice_wlt, + m_alice_bob_start_amount - m_alice_fee_proposal - m_cpd.amount_a_pledge - m_cpd.amount_to_pay, // total + true, UINT64_MAX, + m_alice_bob_start_amount - 2 * m_alice_bob_start_chunk_amount, // unlocked + 0, // mined + MK_TEST_COINS(0), // awaited in + m_cpd.amount_a_pledge + m_cpd.amount_to_pay), // awaited out + false, ""); + + CHECK_AND_ASSERT_MES(refresh_wallet_and_check_balance("", "Bob", bob_wlt, + m_alice_bob_start_amount - m_cpd.amount_b_pledge - m_bob_fee_release - m_bob_fee_accept, // total + true, UINT64_MAX, + m_alice_bob_start_amount - 1 * m_alice_bob_start_chunk_amount, // unlocked + 0, // mined + MK_TEST_COINS(0), // awaited in + m_cpd.amount_b_pledge + m_bob_fee_release), // awaited out + false, ""); + + return true; +} + +bool escrow_acceptance_and_balance::check_balance_after_acceptance_confirmed(currency::core& c, size_t ev_index, const std::vector& events) +{ + 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_AND_ASSERT_MES(refresh_wallet_and_check_balance("", "Alice", alice_wlt, + m_alice_bob_start_amount - m_alice_fee_proposal - m_cpd.amount_a_pledge - m_cpd.amount_to_pay, // total + true, UINT64_MAX, + m_alice_bob_start_amount - 2 * m_alice_bob_start_chunk_amount, // unlocked + 0, // mined + MK_TEST_COINS(0), // awaited in + MK_TEST_COINS(0) // awaited out + ), false, ""); + + CHECK_AND_ASSERT_MES(refresh_wallet_and_check_balance("", "Bob", bob_wlt, + m_alice_bob_start_amount - m_cpd.amount_b_pledge - m_bob_fee_release - m_bob_fee_accept, // total + true, UINT64_MAX, + m_alice_bob_start_amount - 1 * m_alice_bob_start_chunk_amount, // unlocked + 0, // mined + MK_TEST_COINS(0), // awaited in + MK_TEST_COINS(0) // awaited out + ), false, ""); + + return true; +} diff --git a/tests/core_tests/escrow_wallet_tests.h b/tests/core_tests/escrow_wallet_tests.h index 33fdb457..332ac5a2 100644 --- a/tests/core_tests/escrow_wallet_tests.h +++ b/tests/core_tests/escrow_wallet_tests.h @@ -141,3 +141,20 @@ struct escrow_zero_amounts : public wallet_test bool generate(std::vector& events) const; bool c1(currency::core& c, size_t ev_index, const std::vector& events); }; + +struct escrow_acceptance_and_balance : public wallet_test +{ + escrow_acceptance_and_balance(); + bool generate(std::vector& events) const; + bool check_balance_after_proposal_not_confirmed(currency::core& c, size_t ev_index, const std::vector& events); + bool check_balance_after_proposal_confirmed(currency::core& c, size_t ev_index, const std::vector& events); + bool check_balance_after_acceptance_not_confirmed(currency::core& c, size_t ev_index, const std::vector& events); + bool check_balance_after_acceptance_confirmed(currency::core& c, size_t ev_index, const std::vector& events); + + mutable uint64_t m_alice_bob_start_amount; + mutable uint64_t m_alice_bob_start_chunk_amount; + mutable uint64_t m_alice_fee_proposal; + mutable uint64_t m_bob_fee_release; + mutable uint64_t m_bob_fee_accept; + mutable bc_services::contract_private_details m_cpd; +};