From 00976d5f6585dfa01a67df7d76a99d2dcc6892b6 Mon Sep 17 00:00:00 2001 From: sowle Date: Fri, 8 Jan 2021 02:24:43 +0300 Subject: [PATCH] coretests: chain_switching_when_out_spent_in_alt_chain_ref_id test added --- tests/core_tests/chain_switch_1.cpp | 80 ++++++++++++++++++++++++++++- tests/core_tests/chain_switch_1.h | 5 ++ tests/core_tests/chaingen_main.cpp | 1 + 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/tests/core_tests/chain_switch_1.cpp b/tests/core_tests/chain_switch_1.cpp index 72732b18..de59da4a 100644 --- a/tests/core_tests/chain_switch_1.cpp +++ b/tests/core_tests/chain_switch_1.cpp @@ -642,7 +642,7 @@ bool chain_switching_when_out_spent_in_alt_chain_mixin::generate(std::vector& events) const +{ + // Test idea: + bool r = false; + GENERATE_ACCOUNT(miner_acc); + GENERATE_ACCOUNT(alice_acc); + GENERATE_ACCOUNT(bob_acc); + MAKE_GENESIS_BLOCK(events, blk_0, miner_acc, test_core_time::get_time()); + MAKE_NEXT_BLOCK(events, blk_1, blk_0, miner_acc); + REWIND_BLOCKS_N(events, blk_1r, blk_1, miner_acc, CURRENCY_MINED_MONEY_UNLOCK_WINDOW); + + // 0 1 11 12 13 14 + // (0 )- (1 )-...(1r)- (2 )- (3 )- + // \ + // \ + // \- (2a)- (3a)- (4a)- + // tx_0 <- tx_1 // tx_1 spends an output from tx_0 + + // send batch of 10 x 5 test coins to Alice for easier tx_0 construction + transaction tx_0; + /*r = construct_tx_with_many_outputs(events, blk_1r, miner_acc.get_keys(), alice_acc.get_public_address(), MK_TEST_COINS(50), 10, + TESTS_DEFAULT_FEE, tx_0);*/ + size_t nmix = 0; + std::vector destinations; + for (size_t i = 0; i < 10; ++i) + destinations.push_back(tx_destination_entry(MK_TEST_COINS(5), alice_acc.get_public_address())); + r = construct_tx_to_key(events, tx_0, blk_1r, miner_acc, destinations, TESTS_DEFAULT_FEE, nmix, 0, empty_extra, empty_attachment, true, true, true); + CHECK_AND_ASSERT_MES(r, false, "construct_tx_to_key failed"); + + // make sure tx_0 really use ref_by_id + size_t refs_count = 0, gindex_count = 0; + count_ref_by_id_and_gindex_refs_for_tx_inputs(tx_0, refs_count, gindex_count); + CHECK_AND_ASSERT_MES(refs_count == 1 && gindex_count == 0, false, "incorrect input references: " << refs_count << ", " << gindex_count); + + events.push_back(tx_0); + + MAKE_NEXT_BLOCK(events, blk_2, blk_1r, miner_acc); + MAKE_NEXT_BLOCK(events, blk_3, blk_2, miner_acc); + + MAKE_NEXT_BLOCK_TX1(events, blk_2a, blk_1r, miner_acc, tx_0); + + // make sure Alice received exactly 50 test coins + CREATE_TEST_WALLET(alice_wlt, alice_acc, blk_0); + REFRESH_TEST_WALLET_AT_GEN_TIME(events, alice_wlt, blk_2a, CURRENCY_MINED_MONEY_UNLOCK_WINDOW + 2); + CHECK_TEST_WALLET_BALANCE_AT_GEN_TIME(alice_wlt, MK_TEST_COINS(50)); + + // Alice spends her 5 test coins received by tx_0 + transaction tx_1; + destinations.clear(); + destinations.push_back(tx_destination_entry(MK_TEST_COINS(4), bob_acc.get_public_address())); + nmix = 3; + r = construct_tx_to_key(events, tx_1, blk_2a, alice_acc, destinations, TESTS_DEFAULT_FEE, nmix, 0, empty_extra, empty_attachment, true, true, true); + CHECK_AND_ASSERT_MES(r, false, "construct_tx_to_key failed"); + + //MAKE_TX_FEE_MIX(events, tx_1, alice_acc, bob_acc, MK_TEST_COINS(4), TESTS_DEFAULT_FEE, 3, blk_2a); + //events.pop_back(); // pop back tx_1 as it won't go into the tx pool normally because of alt chain + + // simulate handling a block with that tx: handle tx like going with the block... + events.push_back(event_visitor_settings(event_visitor_settings::set_txs_kept_by_block, true)); + events.push_back(tx_1); + events.push_back(event_visitor_settings(event_visitor_settings::set_txs_kept_by_block, false)); + + MAKE_NEXT_BLOCK_TX1(events, blk_3a, blk_2a, miner_acc, tx_1); + MAKE_NEXT_BLOCK(events, blk_4a, blk_3a, miner_acc); + + // make sure Alice has correct balance + REFRESH_TEST_WALLET_AT_GEN_TIME(events, alice_wlt, blk_4a, 2); + CHECK_TEST_WALLET_BALANCE_AT_GEN_TIME(alice_wlt, MK_TEST_COINS(45)); + + + return true; } diff --git a/tests/core_tests/chain_switch_1.h b/tests/core_tests/chain_switch_1.h index 36375f72..f19e99b9 100644 --- a/tests/core_tests/chain_switch_1.h +++ b/tests/core_tests/chain_switch_1.h @@ -77,3 +77,8 @@ struct chain_switching_when_out_spent_in_alt_chain_mixin : public test_chain_uni { bool generate(std::vector& events) const; }; + +struct chain_switching_when_out_spent_in_alt_chain_ref_id : public test_chain_unit_enchanced +{ + bool generate(std::vector& events) const; +}; diff --git a/tests/core_tests/chaingen_main.cpp b/tests/core_tests/chaingen_main.cpp index e9a1f1ab..c61c8986 100644 --- a/tests/core_tests/chaingen_main.cpp +++ b/tests/core_tests/chaingen_main.cpp @@ -900,6 +900,7 @@ int main(int argc, char* argv[]) GENERATE_AND_PLAY(alt_blocks_validation_and_same_new_amount_in_two_txs); GENERATE_AND_PLAY(alt_blocks_with_the_same_txs); GENERATE_AND_PLAY(chain_switching_when_out_spent_in_alt_chain_mixin); + // GENERATE_AND_PLAY(chain_switching_when_out_spent_in_alt_chain_ref_id); // miscellaneous tests