From 745932738e35935d23dc74ab95275cb3406b705d Mon Sep 17 00:00:00 2001 From: sowle Date: Mon, 7 Apr 2025 04:47:55 +0300 Subject: [PATCH] coretests: get_tx_version_and_harfork_id_from_events refactoring --- src/currency_core/currency_format_utils.cpp | 4 ++-- tests/core_tests/chaingen.cpp | 13 +++++++++++++ tests/core_tests/chaingen.h | 8 ++++++++ tests/core_tests/emission_test.cpp | 3 +-- tests/core_tests/escrow_wallet_tests.cpp | 5 ++--- tests/core_tests/get_random_outs.cpp | 3 +-- tests/core_tests/integer_overflow.cpp | 9 +++------ tests/core_tests/misc_tests.cpp | 14 ++++++-------- tests/core_tests/multiassets_test.cpp | 8 ++------ tests/core_tests/pos_basic_tests.cpp | 3 +-- tests/core_tests/tx_validation.cpp | 16 ++++++---------- tests/core_tests/zarcanum_test.cpp | 9 ++++----- 12 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/currency_core/currency_format_utils.cpp b/src/currency_core/currency_format_utils.cpp index cd010206..c8ad89d7 100644 --- a/src/currency_core/currency_format_utils.cpp +++ b/src/currency_core/currency_format_utils.cpp @@ -1445,8 +1445,8 @@ namespace currency uint64_t tx_version, size_t tx_hardfork_id, uint64_t unlock_time, - uint8_t tx_outs_attr, - bool shuffle) + uint8_t tx_outs_attr /* = CURRENCY_TO_KEY_OUT_RELAXED */, + bool shuffle /* = true */) { crypto::secret_key one_time_secret_key{}; return construct_tx(sender_account_keys, sources, destinations, std::vector(), attachments, tx, tx_version, tx_hardfork_id, one_time_secret_key, unlock_time, tx_outs_attr, shuffle); diff --git a/tests/core_tests/chaingen.cpp b/tests/core_tests/chaingen.cpp index 145ec001..f0406702 100644 --- a/tests/core_tests/chaingen.cpp +++ b/tests/core_tests/chaingen.cpp @@ -2013,6 +2013,19 @@ bool construct_tx(const currency::account_keys& sender_account_keys, ); } +bool construct_tx(const currency::account_keys& sender_account_keys, + const std::vector& sources, + const std::vector& destinations, + const std::vector &events_to_get_hardfork_and_version, + const test_chain_unit_base* p_test_instance, + currency::transaction& result_tx) +{ + size_t tx_hardfork_id{}; + uint64_t tx_version = p_test_instance->get_tx_version_and_harfork_id_from_events(events_to_get_hardfork_and_version, tx_hardfork_id); + return construct_tx(sender_account_keys, sources, destinations, empty_attachment, result_tx, tx_version, tx_hardfork_id, 0); +} + + uint64_t get_balance(const currency::account_keys& addr, const std::vector& blockchain, const map_hash2tx_t& mtx, bool dbg_log) { uint64_t res = 0; diff --git a/tests/core_tests/chaingen.h b/tests/core_tests/chaingen.h index ae86cf52..e4286057 100644 --- a/tests/core_tests/chaingen.h +++ b/tests/core_tests/chaingen.h @@ -727,6 +727,14 @@ bool construct_tx(const currency::account_keys& sender_account_keys, currency::transaction& tx, uint64_t tx_version); +bool construct_tx(const currency::account_keys& sender_account_keys, + const std::vector& sources, + const std::vector& destinations, + const std::vector &events_to_get_hardfork_and_version, + const test_chain_unit_base* p_test_instance, + currency::transaction& result_tx); + + void get_confirmed_txs(const std::vector& blockchain, const map_hash2tx_t& mtx, map_hash2tx_t& confirmed_txs); bool find_block_chain(const std::vector& events, std::vector& blockchain, map_hash2tx_t& mtx, const crypto::hash& head); diff --git a/tests/core_tests/emission_test.cpp b/tests/core_tests/emission_test.cpp index eb8ccb97..a683a61c 100644 --- a/tests/core_tests/emission_test.cpp +++ b/tests/core_tests/emission_test.cpp @@ -182,8 +182,7 @@ bool pos_emission_test::generate(std::vector &events) destinations.push_back(tx_destination_entry(pos_entry_amount, alice_acc.get_public_address())); transaction tx_1{}; - size_t tx_hardfork_id{}; - r = construct_tx(preminer_acc.get_keys(), sources, destinations, empty_attachment, tx_1, get_tx_version_and_harfork_id_from_events(events, tx_hardfork_id), tx_hardfork_id, 0); + r = construct_tx(preminer_acc.get_keys(), sources, destinations, events, this, tx_1); CHECK_AND_ASSERT_MES(r, false, "construct_tx failed"); events.push_back(tx_1); MAKE_NEXT_BLOCK_TX1(events, blk_1, blk_0r, miner_acc, tx_1); diff --git a/tests/core_tests/escrow_wallet_tests.cpp b/tests/core_tests/escrow_wallet_tests.cpp index 6c805d29..b5c71e29 100644 --- a/tests/core_tests/escrow_wallet_tests.cpp +++ b/tests/core_tests/escrow_wallet_tests.cpp @@ -1497,8 +1497,7 @@ bool escrow_custom_test::generate(std::vector& events) const } // no change back to the miner - it will become a fee transaction tx_1{}; - size_t tx_hardfork_id{}; - r = construct_tx(miner_acc.get_keys(), sources, destinations, empty_attachment, tx_1, get_tx_version_and_harfork_id_from_events(events, tx_hardfork_id), tx_hardfork_id, 0); + r = construct_tx(miner_acc.get_keys(), sources, destinations, events, this, tx_1); CHECK_AND_ASSERT_MES(r, false, "construct_tx failed"); events.push_back(tx_1); @@ -1953,7 +1952,7 @@ bool escrow_incorrect_cancel_proposal::generate(std::vector& e // no change back to the miner - it will become a fee transaction tx_1 = AUTO_VAL_INIT(tx_1); size_t tx_hardfork_id{}; - r = construct_tx(miner_acc.get_keys(), sources, destinations, empty_attachment, tx_1, get_tx_version_and_harfork_id_from_events(events, tx_hardfork_id), tx_hardfork_id, 0); + r = construct_tx(miner_acc.get_keys(), sources, destinations, events, this, tx_1); CHECK_AND_ASSERT_MES(r, false, "construct_tx failed"); events.push_back(tx_1); diff --git a/tests/core_tests/get_random_outs.cpp b/tests/core_tests/get_random_outs.cpp index 65063b45..0b616057 100644 --- a/tests/core_tests/get_random_outs.cpp +++ b/tests/core_tests/get_random_outs.cpp @@ -99,8 +99,7 @@ bool random_outs_and_burnt_coins::generate(std::vector& events CHECK_AND_ASSERT_MES(r, false, "fill_tx_sources failed"); transaction tx_0{}; - size_t tx_hardfork_id{}; - r = construct_tx(miner_acc.get_keys(), sources, destinations, empty_attachment, tx_0, get_tx_version_and_harfork_id_from_events(events, tx_hardfork_id), tx_hardfork_id, 0); + r = construct_tx(miner_acc.get_keys(), sources, destinations, events, this, tx_0); CHECK_AND_ASSERT_MES(r, false, "construct_tx failed"); uint64_t burned_tx_amount_total = get_burned_amount(tx_0); diff --git a/tests/core_tests/integer_overflow.cpp b/tests/core_tests/integer_overflow.cpp index 33361093..eddb4078 100644 --- a/tests/core_tests/integer_overflow.cpp +++ b/tests/core_tests/integer_overflow.cpp @@ -158,9 +158,7 @@ bool gen_uint_overflow_2::generate(std::vector& events) const destinations.push_back(tx_destination_entry(sources.front().amount - TX_MAX_TRANSFER_AMOUNT - TX_MAX_TRANSFER_AMOUNT + 1 - TESTS_DEFAULT_FEE, bob_addr)); currency::transaction tx_1{}; - std::vector attachments; - size_t tx_hardfork_id{}; - if (!construct_tx(miner_account.get_keys(), sources, destinations, attachments, tx_1, get_tx_version_and_harfork_id_from_events(events, tx_hardfork_id), tx_hardfork_id, 0)) + if (!construct_tx(miner_account.get_keys(), sources, destinations, events, this, tx_1)) return false; events.push_back(tx_1); @@ -185,9 +183,8 @@ bool gen_uint_overflow_2::generate(std::vector& events) const destinations.push_back(de); destinations.push_back(de); - currency::transaction tx_2; - std::vector attachments2; - if (!construct_tx(bob_account.get_keys(), sources, destinations, attachments2, tx_2, get_tx_version_and_harfork_id_from_events(events, tx_hardfork_id), tx_hardfork_id, 0)) + currency::transaction tx_2{}; + if (!construct_tx(bob_account.get_keys(), sources, destinations, events, this, tx_2)) return false; events.push_back(tx_2); diff --git a/tests/core_tests/misc_tests.cpp b/tests/core_tests/misc_tests.cpp index c7b738ed..8dccba81 100644 --- a/tests/core_tests/misc_tests.cpp +++ b/tests/core_tests/misc_tests.cpp @@ -237,9 +237,8 @@ bool block_template_vs_invalid_txs_from_pool::generate(std::vector sources; r = fill_tx_sources(sources, events, blk_0r, miner_acc.get_keys(), de.amount + TESTS_DEFAULT_FEE, 0); CHECK_AND_ASSERT_MES(r, false, "fill_tx_sources failed"); - transaction tx_1m; - size_t tx_hardfork_id{}; - r = construct_tx(miner_acc.get_keys(), sources, std::vector({ de }), empty_attachment, tx_1m, get_tx_version_and_harfork_id_from_events(events, tx_hardfork_id), tx_hardfork_id, 0); + transaction tx_1m{}; + r = construct_tx(miner_acc.get_keys(), sources, std::vector({ de }), events, this, tx_1m); CHECK_AND_ASSERT_MES(r, false, "construct_tx failed"); events.push_back(tx_1m); @@ -272,7 +271,7 @@ bool block_template_vs_invalid_txs_from_pool::generate(std::vector({ se }), std::vector({ tx_destination_entry(se.amount - TESTS_DEFAULT_FEE, miner_acc.get_public_address()) }), - empty_attachment, tx_2m, get_tx_version_and_harfork_id_from_events(events, tx_hardfork_id), tx_hardfork_id, 0); + events, this, tx_2m); CHECK_AND_ASSERT_MES(r, false, "construct_tx failed"); bool input_fully_signed = false; r = sign_multisig_input_in_tx(tx_2m, 0, bob_acc.get_keys(), tx_1m, &input_fully_signed); @@ -281,7 +280,7 @@ bool block_template_vs_invalid_txs_from_pool::generate(std::vector({ se }), std::vector({ tx_destination_entry(se.amount - TESTS_DEFAULT_FEE, miner_acc.get_public_address()) }), - empty_attachment, tx_2ma, get_tx_version_and_harfork_id_from_events(events, tx_hardfork_id), tx_hardfork_id, 0); + events, this, tx_2ma); CHECK_AND_ASSERT_MES(r, false, "construct_tx failed"); input_fully_signed = false; r = sign_multisig_input_in_tx(tx_2ma, 0, bob_acc.get_keys(), tx_1m, &input_fully_signed); @@ -438,8 +437,7 @@ bool test_blockchain_vs_spent_multisig_outs::generate(std::vector destinations; r = fill_tx_sources_and_destinations(events, blk_0r, miner_acc, alice_acc, m_alice_initial_balance, TESTS_DEFAULT_FEE, 0, sources, destinations, true /* spends */, false /* unlock time */); CHECK_AND_ASSERT_MES(r, false, "fill_tx_sources_and_destinations failed"); - size_t tx_hardfork_id{}; - uint64_t tx_version = get_tx_version_and_harfork_id_from_events(events, tx_hardfork_id); - r = construct_tx(miner_acc.get_keys(), sources, destinations, empty_attachment, tx_0, tx_version, tx_hardfork_id, 0); + r = construct_tx(miner_acc.get_keys(), sources, destinations, events, this, tx_0); CHECK_AND_ASSERT_MES(r, false, "construct_tx failed"); ADD_CUSTOM_EVENT(events, tx_0); @@ -706,9 +704,7 @@ bool asset_depoyment_and_few_zc_utxos::generate(std::vector& e m_alice_initial_balance = TESTS_DEFAULT_FEE * 100; r = fill_tx_sources(sources, events, blk_0r, miner_acc.get_keys(), m_alice_initial_balance + TESTS_DEFAULT_FEE, 0); CHECK_AND_ASSERT_MES(r, false, "fill_tx_sources failed"); - size_t tx_hardfork_id{}; - uint64_t tx_version = get_tx_version_and_harfork_id_from_events(events, tx_hardfork_id); - r = construct_tx(miner_acc.get_keys(), sources, destinations, empty_attachment, tx_0, tx_version, tx_hardfork_id, 0); + r = construct_tx(miner_acc.get_keys(), sources, destinations, events, this, tx_0); CHECK_AND_ASSERT_MES(r, false, "construct_tx failed"); ADD_CUSTOM_EVENT(events, tx_0); diff --git a/tests/core_tests/pos_basic_tests.cpp b/tests/core_tests/pos_basic_tests.cpp index c07a2850..1f18db8a 100644 --- a/tests/core_tests/pos_basic_tests.cpp +++ b/tests/core_tests/pos_basic_tests.cpp @@ -174,8 +174,7 @@ bool pos_mining_with_decoys::generate(std::vector& events) con destinations.emplace_back(COIN, carol_acc.get_public_address()); transaction tx_0{}; - size_t tx_hardfork_id{}; - r = construct_tx(miner_acc.get_keys(), sources, destinations, empty_attachment, tx_0, get_tx_version_and_harfork_id_from_events(events, tx_hardfork_id), tx_hardfork_id, 0); + r = construct_tx(miner_acc.get_keys(), sources, destinations, events, this, tx_0); CHECK_AND_ASSERT_MES(r, false, "construct_tx failed"); events.push_back(tx_0); MAKE_NEXT_BLOCK_TX1(events, blk_1, blk_0r, miner_acc, tx_0); diff --git a/tests/core_tests/tx_validation.cpp b/tests/core_tests/tx_validation.cpp index a0527f4a..654113a0 100644 --- a/tests/core_tests/tx_validation.cpp +++ b/tests/core_tests/tx_validation.cpp @@ -1240,8 +1240,7 @@ bool tx_expiration_time::generate(std::vector& events) const r = fill_tx_sources_and_destinations(events, blk_2, alice_acc.get_keys(), bob_acc.get_public_address(), MK_TEST_COINS(1), TESTS_DEFAULT_FEE, 0, sources, destinations); CHECK_AND_ASSERT_MES(r, false, "fill_tx_sources_and_destinations failed"); transaction tx_2{}; - size_t tx_hardfork_id{}; - r = construct_tx(alice_acc.get_keys(), sources, destinations, empty_attachment, tx_2, get_tx_version_and_harfork_id_from_events(events, tx_hardfork_id), tx_hardfork_id, 0); + r = construct_tx(alice_acc.get_keys(), sources, destinations, events, this, tx_2); CHECK_AND_ASSERT_MES(r, false, "construct_tx failed"); set_tx_expiration_time(tx_2, ts_median + TX_EXPIRATION_MEDIAN_SHIFT + 1); // one second greather than minimum allowed r = resign_tx(alice_acc.get_keys(), sources, tx_2); @@ -1258,8 +1257,8 @@ bool tx_expiration_time::generate(std::vector& events) const destinations.clear(); r = fill_tx_sources_and_destinations(events, blk_3, alice_acc.get_keys(), bob_acc.get_public_address(), MK_TEST_COINS(1), TESTS_DEFAULT_FEE, 0, sources, destinations); CHECK_AND_ASSERT_MES(r, false, "fill_tx_sources_and_destinations failed"); - transaction tx_3 = AUTO_VAL_INIT(tx_3); - r = construct_tx(alice_acc.get_keys(), sources, destinations, empty_attachment, tx_3, get_tx_version_and_harfork_id_from_events(events, tx_hardfork_id), tx_hardfork_id, 0); + transaction tx_3{}; + r = construct_tx(alice_acc.get_keys(), sources, destinations, events, this, tx_3); CHECK_AND_ASSERT_MES(r, false, "construct_tx failed"); set_tx_expiration_time(tx_3, ts_median + TX_EXPIRATION_MEDIAN_SHIFT + 0); // exact expiration time, should not pass (see core condition above) r = resign_tx(alice_acc.get_keys(), sources, tx_3); @@ -1324,8 +1323,7 @@ bool tx_expiration_time_and_block_template::generate(std::vector& events) r = fill_tx_sources_and_destinations(events, blk_0r, m_miner_acc.get_keys(), bob_acc.get_public_address(), MK_TEST_COINS(1), TESTS_DEFAULT_FEE, 0, sources, destinations); CHECK_AND_ASSERT_MES(r, false, "fill_tx_sources_and_destinations failed"); transaction tx_0{}; - size_t tx_hardfork_id{}; - r = construct_tx(m_miner_acc.get_keys(), sources, destinations, empty_attachment, tx_0, get_tx_version_and_harfork_id_from_events(events, tx_hardfork_id), tx_hardfork_id, 0); + r = construct_tx(m_miner_acc.get_keys(), sources, destinations, events, this, tx_0); CHECK_AND_ASSERT_MES(r, false, "construct_tx failed"); LOG_PRINT_YELLOW("tx_0 = " << get_transaction_hash(tx_0), LOG_LEVEL_0); // do not push tx_0 into events yet diff --git a/tests/core_tests/zarcanum_test.cpp b/tests/core_tests/zarcanum_test.cpp index 9fc76438..abe83524 100644 --- a/tests/core_tests/zarcanum_test.cpp +++ b/tests/core_tests/zarcanum_test.cpp @@ -520,8 +520,7 @@ bool zarcanum_txs_with_big_shuffled_decoy_set_shuffled::generate(std::vector