From 4f703fe8f8cdd7435d70f7f805a9922eeaf56f2d Mon Sep 17 00:00:00 2001 From: sowle Date: Wed, 23 Nov 2022 03:05:59 +0100 Subject: [PATCH 01/11] DIFFICULTY_STARTER was split into DIFFICULTY_POW_STARTER and DIFFICULTY_POS_STARTER --- src/currency_core/currency_basic.h | 5 +++++ src/currency_core/currency_config.h | 4 +++- src/currency_core/currency_format_utils.cpp | 11 ++++++----- src/currency_core/difficulty.cpp | 8 ++++---- src/currency_core/difficulty.h | 4 ++-- tests/core_tests/block_validation.cpp | 4 ++-- tests/core_tests/chaingen.cpp | 4 ++-- tests/core_tests/wallet_tests.cpp | 2 +- 8 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/currency_core/currency_basic.h b/src/currency_core/currency_basic.h index 9ab8a12f..6f226878 100644 --- a/src/currency_core/currency_basic.h +++ b/src/currency_core/currency_basic.h @@ -42,6 +42,7 @@ #include "misc_language.h" #include "block_flags.h" #include "etc_custom_serialization.h" +#include "difficulty.h" namespace currency { @@ -56,6 +57,10 @@ namespace currency const static crypto::hash gdefault_genesis = epee::string_tools::hex_to_pod("CC608F59F8080E2FBFE3C8C80EB6E6A953D47CF2D6AEBD345BADA3A1CAB99852"); const static crypto::hash ffff_hash = epee::string_tools::hex_to_pod("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); + const static wide_difficulty_type global_difficulty_pow_starter = DIFFICULTY_POW_STARTER; + const static wide_difficulty_type global_difficulty_pos_starter = DIFFICULTY_POS_STARTER; + const static uint64_t global_difficulty_pos_target = DIFFICULTY_POS_TARGET; + const static uint64_t global_difficulty_pow_target = DIFFICULTY_POW_TARGET; typedef std::string payment_id_t; diff --git a/src/currency_core/currency_config.h b/src/currency_core/currency_config.h index 5318bc33..e48ca6a3 100644 --- a/src/currency_core/currency_config.h +++ b/src/currency_core/currency_config.h @@ -65,7 +65,7 @@ #define CURRENCY_MINER_TX_MAX_OUTS CURRENCY_TX_MAX_ALLOWED_OUTS #define CURRENCY_TX_OUTS_RND_SPLIT_DIGITS_TO_KEEP 3 -#define DIFFICULTY_STARTER 1 +#define DIFFICULTY_POW_STARTER 1 #define DIFFICULTY_POS_TARGET 120 // seconds #define DIFFICULTY_POW_TARGET 120 // seconds #define DIFFICULTY_TOTAL_TARGET ((DIFFICULTY_POS_TARGET + DIFFICULTY_POW_TARGET) / 4) @@ -110,6 +110,7 @@ #define STRATUM_DEFAULT_PORT 11777 #define P2P_NETWORK_ID_TESTNET_FLAG 0 #define P2P_MAINTAINERS_PUB_KEY "8f138bb73f6d663a3746a542770781a09579a7b84cb4125249e95530824ee607" +#define DIFFICULTY_POS_STARTER 1 #else #define P2P_DEFAULT_PORT (11112 + CURRENCY_FORMATION_VERSION) #define RPC_DEFAULT_PORT 12111 @@ -117,6 +118,7 @@ #define STRARUM_DEFAULT_PORT 51113 #define P2P_NETWORK_ID_TESTNET_FLAG 1 #define P2P_MAINTAINERS_PUB_KEY "aaa2d7aabc8d383fd53a3ae898697b28f236ceade6bafc1eecff413a6a02272a" +#define DIFFICULTY_POS_STARTER 625000000000 #endif #define P2P_NETWORK_ID_VER (CURRENCY_FORMATION_VERSION+0) diff --git a/src/currency_core/currency_format_utils.cpp b/src/currency_core/currency_format_utils.cpp index 2512a41c..9adeb482 100644 --- a/src/currency_core/currency_format_utils.cpp +++ b/src/currency_core/currency_format_utils.cpp @@ -4005,11 +4005,12 @@ namespace currency const difficulties& a_diff, const difficulties& b_diff ) { - static const wide_difficulty_type difficulty_starter = DIFFICULTY_STARTER; - const wide_difficulty_type& a_pos_cumulative_difficulty = a_diff.pos_diff > 0 ? a_diff.pos_diff : difficulty_starter; - const wide_difficulty_type& b_pos_cumulative_difficulty = b_diff.pos_diff > 0 ? b_diff.pos_diff : difficulty_starter; - const wide_difficulty_type& a_pow_cumulative_difficulty = a_diff.pow_diff > 0 ? a_diff.pow_diff : difficulty_starter; - const wide_difficulty_type& b_pow_cumulative_difficulty = b_diff.pow_diff > 0 ? b_diff.pow_diff : difficulty_starter; + static const wide_difficulty_type difficulty_pos_starter = DIFFICULTY_POS_STARTER; + static const wide_difficulty_type difficulty_pow_starter = DIFFICULTY_POW_STARTER; + const wide_difficulty_type& a_pos_cumulative_difficulty = a_diff.pos_diff > 0 ? a_diff.pos_diff : difficulty_pos_starter; + const wide_difficulty_type& b_pos_cumulative_difficulty = b_diff.pos_diff > 0 ? b_diff.pos_diff : difficulty_pos_starter; + const wide_difficulty_type& a_pow_cumulative_difficulty = a_diff.pow_diff > 0 ? a_diff.pow_diff : difficulty_pow_starter; + const wide_difficulty_type& b_pow_cumulative_difficulty = b_diff.pow_diff > 0 ? b_diff.pow_diff : difficulty_pow_starter; boost::multiprecision::uint1024_t basic_sum = boost::multiprecision::uint1024_t(a_pow_cumulative_difficulty) + (boost::multiprecision::uint1024_t(a_pos_cumulative_difficulty)*difficulty_pow_at_split_point) / difficulty_pos_at_split_point; boost::multiprecision::uint1024_t res = diff --git a/src/currency_core/difficulty.cpp b/src/currency_core/difficulty.cpp index 68d82c15..b0bdfac0 100644 --- a/src/currency_core/difficulty.cpp +++ b/src/currency_core/difficulty.cpp @@ -179,7 +179,7 @@ namespace currency { return res.convert_to(); } - wide_difficulty_type next_difficulty_1(vector& timestamps, vector& cumulative_difficulties, size_t target_seconds) + wide_difficulty_type next_difficulty_1(vector& timestamps, vector& cumulative_difficulties, size_t target_seconds, const wide_difficulty_type& difficulty_starter) { // timestamps - first is latest, back - is oldest timestamps @@ -194,7 +194,7 @@ namespace currency { CHECK_AND_ASSERT_MES(length == cumulative_difficulties.size(), 0, "Check \"length == cumulative_difficulties.size()\" failed"); if (length <= 1) { - return DIFFICULTY_STARTER; + return difficulty_starter; } static_assert(DIFFICULTY_WINDOW >= 2, "Window is too small"); @@ -221,7 +221,7 @@ namespace currency { return summ / devider; } - wide_difficulty_type next_difficulty_2(vector& timestamps, vector& cumulative_difficulties, size_t target_seconds) + wide_difficulty_type next_difficulty_2(vector& timestamps, vector& cumulative_difficulties, size_t target_seconds, const wide_difficulty_type& difficulty_starter) { // timestamps - first is latest, back - is oldest timestamps @@ -236,7 +236,7 @@ namespace currency { CHECK_AND_ASSERT_MES(length == cumulative_difficulties.size(), 0, "Check \"length == cumulative_difficulties.size()\" failed"); if (length <= 1) { - return DIFFICULTY_STARTER; + return difficulty_starter; } static_assert(DIFFICULTY_WINDOW >= 2, "Window is too small"); diff --git a/src/currency_core/difficulty.h b/src/currency_core/difficulty.h index ab97f142..731fb26f 100644 --- a/src/currency_core/difficulty.h +++ b/src/currency_core/difficulty.h @@ -19,8 +19,8 @@ namespace currency typedef boost::multiprecision::uint128_t wide_difficulty_type; bool check_hash(const crypto::hash &hash, wide_difficulty_type difficulty); - wide_difficulty_type next_difficulty_1(std::vector& timestamps, std::vector& cumulative_difficulties, size_t target_seconds); - wide_difficulty_type next_difficulty_2(std::vector& timestamps, std::vector& cumulative_difficulties, size_t target_seconds); + wide_difficulty_type next_difficulty_1(std::vector& timestamps, std::vector& cumulative_difficulties, size_t target_seconds, const wide_difficulty_type& difficulty_starter); + wide_difficulty_type next_difficulty_2(std::vector& timestamps, std::vector& cumulative_difficulties, size_t target_seconds, const wide_difficulty_type& difficulty_starter); uint64_t difficulty_to_boundary(wide_difficulty_type difficulty); void difficulty_to_boundary_long(wide_difficulty_type difficulty, crypto::hash& result); } diff --git a/tests/core_tests/block_validation.cpp b/tests/core_tests/block_validation.cpp index c7d55c21..85e9aa1f 100644 --- a/tests/core_tests/block_validation.cpp +++ b/tests/core_tests/block_validation.cpp @@ -21,7 +21,7 @@ namespace for (size_t i = 0; i < new_block_count; ++i) { block blk_next; - wide_difficulty_type diffic = next_difficulty_1(timestamps, cummulative_difficulties, DIFFICULTY_POW_TARGET); + wide_difficulty_type diffic = next_difficulty_1(timestamps, cummulative_difficulties, DIFFICULTY_POW_TARGET, DIFFICULTY_POW_STARTER); if (!generator.construct_block_manually(blk_next, blk_prev, miner_account, test_generator::bf_timestamp | test_generator::bf_diffic, 0, 0, blk_prev.timestamp, crypto::hash(), diffic)) return false; @@ -152,7 +152,7 @@ bool gen_block_invalid_nonce::generate(std::vector& events) co return false; // Create invalid nonce - wide_difficulty_type diffic = next_difficulty_1(timestamps, commulative_difficulties, DIFFICULTY_POW_TARGET); + wide_difficulty_type diffic = next_difficulty_1(timestamps, commulative_difficulties, DIFFICULTY_POW_TARGET, DIFFICULTY_POW_STARTER); CHECK_AND_ASSERT_MES(diffic > 1, false, "diffic > 1 validation failed"); const block& blk_last = boost::get(events.back()); uint64_t timestamp = blk_last.timestamp; diff --git a/tests/core_tests/chaingen.cpp b/tests/core_tests/chaingen.cpp index 0e5270de..18d33a06 100644 --- a/tests/core_tests/chaingen.cpp +++ b/tests/core_tests/chaingen.cpp @@ -766,7 +766,7 @@ bool test_generator::get_params_for_next_pos_block(const crypto::hash& head_id, std::vector timestamps; std::vector commulative_difficulties; if (!blocks.size()) - return DIFFICULTY_STARTER; + return DIFFICULTY_POW_STARTER; for (size_t i = blocks.size() - 1; i != 0; --i) { @@ -775,7 +775,7 @@ bool test_generator::get_params_for_next_pos_block(const crypto::hash& head_id, timestamps.push_back(blocks[i]->b.timestamp); commulative_difficulties.push_back(blocks[i]->cumul_difficulty); } - return next_difficulty_1(timestamps, commulative_difficulties, pow ? DIFFICULTY_POW_TARGET : DIFFICULTY_POS_TARGET); + return next_difficulty_1(timestamps, commulative_difficulties, pow ? global_difficulty_pow_target : global_difficulty_pos_target, pow ? global_difficulty_pow_starter : global_difficulty_pos_starter); } currency::wide_difficulty_type test_generator::get_cumul_difficulty_for_next_block(const crypto::hash& head_id, bool pow) const diff --git a/tests/core_tests/wallet_tests.cpp b/tests/core_tests/wallet_tests.cpp index 3d891568..a1744253 100644 --- a/tests/core_tests/wallet_tests.cpp +++ b/tests/core_tests/wallet_tests.cpp @@ -2769,7 +2769,7 @@ bool premine_wallet_test::generate(std::vector& events) const currency::generate_genesis_block(blk_0_info.b); blk_0_info.already_generated_coins = get_outs_money_amount(blk_0_info.b.miner_tx); blk_0_info.block_size = get_object_blobsize(blk_0_info.b.miner_tx); - blk_0_info.cumul_difficulty = DIFFICULTY_STARTER; + blk_0_info.cumul_difficulty = DIFFICULTY_POW_STARTER; blk_0_info.ks_hash = currency::null_hash; blk_0_info.m_transactions.clear(); events.push_back(blk_0_info.b); From 3ff4a69945e5117e931135d44c9916cf55dcdf82 Mon Sep 17 00:00:00 2001 From: sowle Date: Wed, 23 Nov 2022 04:13:39 +0100 Subject: [PATCH 02/11] DIFFICULTY_STARTER was split into DIFFICULTY_POW_STARTER and DIFFICULTY_POS_STARTER (2) --- src/currency_core/blockchain_storage.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/currency_core/blockchain_storage.cpp b/src/currency_core/blockchain_storage.cpp index 8ac1b3cc..f45dcfd2 100644 --- a/src/currency_core/blockchain_storage.cpp +++ b/src/currency_core/blockchain_storage.cpp @@ -1146,7 +1146,7 @@ wide_difficulty_type blockchain_storage::get_next_diff_conditional(bool pos) con std::vector timestamps; std::vector commulative_difficulties; if (!m_db_blocks.size()) - return DIFFICULTY_STARTER; + return DIFFICULTY_POW_STARTER; //skip genesis timestamp TIME_MEASURE_START_PD(target_calculating_enum_blocks); CRITICAL_REGION_BEGIN(m_targetdata_cache_lock); @@ -1168,11 +1168,11 @@ wide_difficulty_type blockchain_storage::get_next_diff_conditional(bool pos) con TIME_MEASURE_START_PD(target_calculating_calc); if (m_core_runtime_config.is_hardfork_active_for_height(1, m_db_blocks.size())) { - dif = next_difficulty_2(timestamps, commulative_difficulties, pos ? DIFFICULTY_POS_TARGET : DIFFICULTY_POW_TARGET); + dif = next_difficulty_2(timestamps, commulative_difficulties, pos ? global_difficulty_pos_target : global_difficulty_pow_target, pos ? global_difficulty_pos_starter : global_difficulty_pow_starter); } else { - dif = next_difficulty_1(timestamps, commulative_difficulties, pos ? DIFFICULTY_POS_TARGET : DIFFICULTY_POW_TARGET); + dif = next_difficulty_1(timestamps, commulative_difficulties, pos ? global_difficulty_pos_target : global_difficulty_pow_target, pos ? global_difficulty_pos_starter : global_difficulty_pow_starter); } @@ -1187,7 +1187,7 @@ wide_difficulty_type blockchain_storage::get_next_diff_conditional2(bool pos, co std::vector commulative_difficulties; size_t count = 0; if (!m_db_blocks.size()) - return DIFFICULTY_STARTER; + return DIFFICULTY_POW_STARTER; auto cb = [&](const block_extended_info& bei, bool is_main){ if (!bei.height) @@ -1206,9 +1206,9 @@ wide_difficulty_type blockchain_storage::get_next_diff_conditional2(bool pos, co wide_difficulty_type diff = 0; if(m_core_runtime_config.is_hardfork_active_for_height(1, abei.height)) - diff = next_difficulty_2(timestamps, commulative_difficulties, pos ? DIFFICULTY_POS_TARGET : DIFFICULTY_POW_TARGET); + diff = next_difficulty_2(timestamps, commulative_difficulties, pos ? global_difficulty_pos_target : global_difficulty_pow_target, pos ? global_difficulty_pos_starter : global_difficulty_pow_starter); else - diff = next_difficulty_1(timestamps, commulative_difficulties, pos ? DIFFICULTY_POS_TARGET : DIFFICULTY_POW_TARGET); + diff = next_difficulty_1(timestamps, commulative_difficulties, pos ? global_difficulty_pos_target : global_difficulty_pow_target, pos ? global_difficulty_pos_starter : global_difficulty_pow_starter); return diff; } //------------------------------------------------------------------ @@ -1268,7 +1268,7 @@ wide_difficulty_type blockchain_storage::get_next_difficulty_for_alternative_cha commulative_difficulties.push_back(m_db_blocks[i]->cumulative_diff_precise); } - return next_difficulty_1(timestamps, commulative_difficulties, pos ? DIFFICULTY_POS_TARGET:DIFFICULTY_POW_TARGET); + return next_difficulty_1(timestamps, commulative_difficulties, pos ? DIFFICULTY_POS_TARGET:DIFFICULTY_POW_TARGET, pos ? global_difficulty_pos_starter : global_difficulty_pow_starter); } //------------------------------------------------------------------ bool blockchain_storage::prevalidate_miner_transaction(const block& b, uint64_t height, bool pos) const @@ -2821,7 +2821,7 @@ bool blockchain_storage::forecast_difficulty(std::vector Date: Wed, 23 Nov 2022 15:10:06 +0100 Subject: [PATCH 03/11] coretests: zarcanum_basic_tests improved to check post-HF4 transfers via wallet2 with mixins (exposes a bug atm) + overloaded wallet2::transfer() --- src/wallet/wallet2.cpp | 15 +++++++++++++++ src/wallet/wallet2.h | 1 + tests/core_tests/zarcanum_test.cpp | 5 +++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 6122c6be..684b56e8 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -1890,6 +1890,21 @@ void wallet2::transfer(uint64_t amount, const currency::account_public_address& this->transfer(dst, 0, 0, TX_DEFAULT_FEE, extra, attachments, get_current_split_strategy(), tools::tx_dust_policy(DEFAULT_DUST_THRESHOLD), result_tx); } //---------------------------------------------------------------------------------------------------- +void wallet2::transfer(uint64_t amount, size_t fake_outs_count, const currency::account_public_address& acc, uint64_t fee /* = TX_DEFAULT_FEE*/, + const crypto::hash& asset_id /* = currency::null_hash */) +{ + std::vector extra; + std::vector attachments; + transaction result_tx = AUTO_VAL_INIT(result_tx); + + std::vector dst; + dst.resize(1); + dst.back().addr.push_back(acc); + dst.back().amount = amount; + dst.back().asset_id = asset_id; + this->transfer(dst, fake_outs_count, 0, fee, extra, attachments, get_current_split_strategy(), tools::tx_dust_policy(DEFAULT_DUST_THRESHOLD), result_tx); +} +//---------------------------------------------------------------------------------------------------- void wallet2::transfer(uint64_t amount, const currency::account_public_address& acc, const crypto::hash& asset_id) { transaction result_tx = AUTO_VAL_INIT(result_tx); diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 6d1d470a..79b90037 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -571,6 +571,7 @@ namespace tools uint64_t unlocked_balance() const; void transfer(uint64_t amount, const currency::account_public_address& acc, const crypto::hash& asset_id = currency::null_hash); + void transfer(uint64_t amount, size_t fake_outs_count, const currency::account_public_address& acc, uint64_t fee = TX_DEFAULT_FEE, const crypto::hash& asset_id = currency::null_hash); void transfer(uint64_t amount, const currency::account_public_address& acc, currency::transaction& result_tx, const crypto::hash& asset_id = currency::null_hash); void transfer(const std::vector& dsts, diff --git a/tests/core_tests/zarcanum_test.cpp b/tests/core_tests/zarcanum_test.cpp index 2a28a062..099f55c8 100644 --- a/tests/core_tests/zarcanum_test.cpp +++ b/tests/core_tests/zarcanum_test.cpp @@ -131,9 +131,10 @@ bool zarcanum_basic_test::c1(currency::core& c, size_t ev_index, const std::vect CHECK_AND_ASSERT_MES(check_balance_via_wallet(*alice_wlt, "Alice", transfer_amount * batches_to_Alice_count, UINT64_MAX, transfer_amount * batches_to_Alice_count), false, ""); - //create transfer from post-zarcanum inputs to post-zarcanum inputs + //create transfer from post-zarcanum inputs to post-zarcanum inputs with mixins uint64_t transfer_amount2 = AMOUNT_TO_TRANSFER_ZARCANUM_BASIC; - alice_wlt->transfer(transfer_amount2, m_accounts[BOB_ACC_IDX].get_public_address()); + size_t nmix = 10; + alice_wlt->transfer(transfer_amount2, nmix, m_accounts[BOB_ACC_IDX].get_public_address()); LOG_PRINT_MAGENTA("Zarcanum-2-zarcanum transaction sent from Alice to Bob " << print_money_brief(transfer_amount2), LOG_LEVEL_0); From 741aae9b7fc1a7444ec039c8cf29704066d5484c Mon Sep 17 00:00:00 2001 From: sowle Date: Wed, 23 Nov 2022 18:09:49 +0100 Subject: [PATCH 04/11] wallet: prepare_tx_sources() fixed for post-HF4 transfers --- src/wallet/wallet2.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 684b56e8..5794c76c 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -4830,15 +4830,16 @@ bool wallet2::prepare_tx_sources(size_t fake_outputs_count, std::vectorm_ptx_wallet_info->m_tx.vout.size() > it->m_internal_output_index, "m_internal_output_index = " << it->m_internal_output_index << " is greater or equal to outputs count = " << it->m_ptx_wallet_info->m_tx.vout.size()); - req.amounts.push_back(it->amount()); + req.amounts.push_back(it->is_zc() ? 0 : it->m_amount); } bool r = m_core_proxy->call_COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS(req, daemon_resp); From ef38eb97247a335ac6de3ba453079ef84194d784 Mon Sep 17 00:00:00 2001 From: sowle Date: Wed, 23 Nov 2022 18:55:03 +0100 Subject: [PATCH 05/11] p2p: when use-only-priority-nodes is present and none of priority nodes are specified -- don't connect to seed nodes --- src/p2p/net_node.inl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index a63e88ed..f112dbcc 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -32,7 +32,7 @@ namespace nodetool const command_line::arg_descriptor arg_p2p_allow_local_ip ("allow-local-ip", "Allow local ip add to peer list, mostly in debug purposes"); const command_line::arg_descriptor > arg_p2p_add_peer ("add-peer", "Manually add peer to local peerlist"); const command_line::arg_descriptor > arg_p2p_add_priority_node ("add-priority-node", "Specify list of peers to connect to and attempt to keep the connection open"); - const command_line::arg_descriptor arg_p2p_use_only_priority_nodes ("use-only-priority-nodes", "Try to connect only to priority nodes"); + const command_line::arg_descriptor arg_p2p_use_only_priority_nodes ("use-only-priority-nodes", "Connect only to priority nodes"); const command_line::arg_descriptor > arg_p2p_seed_node ("seed-node", "Connect to a node to retrieve peer addresses, and disconnect"); const command_line::arg_descriptor arg_p2p_hide_my_port ("hide-my-port", "Do not announce yourself as peerlist candidate"); const command_line::arg_descriptor arg_p2p_offline_mode ( "offline-mode", "Don't connect to any node and reject any connections"); @@ -846,7 +846,7 @@ namespace nodetool if (m_offline_mode) return true; - if(!m_peerlist.get_white_peers_count() && m_seed_nodes.size() && !m_priority_peers.size()) + if(!m_peerlist.get_white_peers_count() && m_seed_nodes.size() && !m_priority_peers.size() && !m_use_only_priority_peers) { size_t try_count = 0; size_t current_index = crypto::rand()%m_seed_nodes.size(); From 928d937a7b68ac134e67c02c7d2fcfb22e35d7aa Mon Sep 17 00:00:00 2001 From: sowle Date: Wed, 23 Nov 2022 19:28:17 +0100 Subject: [PATCH 06/11] get_amount_from_variant() made noexcept --- src/currency_core/currency_format_utils.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/currency_core/currency_format_utils.h b/src/currency_core/currency_format_utils.h index a5419b20..db3f690a 100644 --- a/src/currency_core/currency_format_utils.h +++ b/src/currency_core/currency_format_utils.h @@ -815,9 +815,16 @@ namespace currency uint64_t operator()(const txin_zc_input&) const { return 0; } uint64_t operator()(const txin_gen& i) const { return 0; } }; - inline uint64_t get_amount_from_variant(const txin_v& v) + inline uint64_t get_amount_from_variant(const txin_v& v) noexcept { - return boost::apply_visitor(input_amount_getter(), v); + try + { + return boost::apply_visitor(input_amount_getter(), v); + } + catch(...) + { + return 0; + } } //--------------------------------------------------------------- struct output_amount_getter : public boost::static_visitor From f2ebc760015b5300aef887de3434480c8df73303 Mon Sep 17 00:00:00 2001 From: sowle Date: Wed, 23 Nov 2022 22:01:55 +0100 Subject: [PATCH 07/11] coretests: zarcanum_in_alt_chain test added (EXPOSES a bug with global outputs during alt block handling) --- tests/core_tests/zarcanum_test.cpp | 113 ++++++++++++++++++++++++++++- tests/core_tests/zarcanum_test.h | 6 ++ 2 files changed, 118 insertions(+), 1 deletion(-) diff --git a/tests/core_tests/zarcanum_test.cpp b/tests/core_tests/zarcanum_test.cpp index 099f55c8..e6d22f72 100644 --- a/tests/core_tests/zarcanum_test.cpp +++ b/tests/core_tests/zarcanum_test.cpp @@ -433,6 +433,9 @@ bool zarcanum_pos_block_math::generate(std::vector& events) co MAKE_NEXT_BLOCK(events, blk_1, blk_0, miner_acc); REWIND_BLOCKS_N_WITH_TIME(events, blk_1r, blk_1, miner_acc, CURRENCY_MINED_MONEY_UNLOCK_WINDOW + 10); + //std::list miner_stake_sources( {miner_acc} ); + //MAKE_NEXT_POS_BLOCK(events, blk_2, blk_1r, miner_acc, miner_stake_sources); + // blocks with an invalid zarcanum sig for(size_t i = 1; ; ++i) { @@ -468,7 +471,6 @@ bool zarcanum_pos_block_math::generate(std::vector& events) co return true; } - //------------------------------------------------------------------------------ zarcanum_txs_with_big_shuffled_decoy_set_shuffled::zarcanum_txs_with_big_shuffled_decoy_set_shuffled() @@ -593,3 +595,112 @@ bool zarcanum_txs_with_big_shuffled_decoy_set_shuffled::generate(std::vector& events) const +{ + bool r = false; + + uint64_t ts = test_core_time::get_time(); + m_accounts.resize(TOTAL_ACCS_COUNT); + account_base& miner_acc = m_accounts[MINER_ACC_IDX]; miner_acc.generate(); miner_acc.set_createtime(ts); + account_base& alice_acc = m_accounts[ALICE_ACC_IDX]; alice_acc.generate(); alice_acc.set_createtime(ts); + account_base& bob_acc = m_accounts[BOB_ACC_IDX]; bob_acc.generate(); bob_acc.set_createtime(ts); + MAKE_GENESIS_BLOCK(events, blk_0, miner_acc, ts); + DO_CALLBACK(events, "configure_core"); // necessary to set m_hardforks + REWIND_BLOCKS_N_WITH_TIME(events, blk_0r, blk_0, miner_acc, CURRENCY_MINED_MONEY_UNLOCK_WINDOW); + + uint64_t alice_amount = COIN * 100; + MAKE_TX(events, tx_0, miner_acc, alice_acc, alice_amount, blk_0r); + MAKE_NEXT_BLOCK_TX1(events, blk_1, blk_0r, miner_acc, tx_0); + + REWIND_BLOCKS_N_WITH_TIME(events, blk_1r, blk_1, miner_acc, CURRENCY_MINED_MONEY_UNLOCK_WINDOW); + + std::list alice_stake_sources({ alice_acc }); + MAKE_NEXT_POS_BLOCK(events, blk_2, blk_1r, alice_acc, alice_stake_sources); + + DO_CALLBACK_PARAMS(events, "check_hardfork_inactive", static_cast(ZANO_HARDFORK_04_ZARCANUM)); + MAKE_NEXT_BLOCK(events, blk_3, blk_2, miner_acc); + DO_CALLBACK_PARAMS(events, "check_hardfork_active", static_cast(ZANO_HARDFORK_04_ZARCANUM)); + + uint64_t bob_amount = COIN * 100; + MAKE_TX(events, tx_1, miner_acc, bob_acc, bob_amount, blk_3); + MAKE_NEXT_BLOCK_TX1(events, blk_4, blk_3, miner_acc, tx_1); + + // HF4 + // | + // 0 10 11 21 22 23 | 24 34 35 36 <- blockchain height + // (0 )..(0r)- (1 )..(1r)- !2 !- (3 )- (4 )..(4r)- (5 ) <- main chain + // tx_0 tx_1 \ tx_2 + // -!5a!- (6a) <- alt chain + + REWIND_BLOCKS_N_WITH_TIME(events, blk_4r, blk_4, miner_acc, CURRENCY_MINED_MONEY_UNLOCK_WINDOW); + + // Bob: move all to miner + MAKE_TX(events, tx_2, bob_acc, miner_acc, bob_amount - TESTS_DEFAULT_FEE, blk_4r); + MAKE_NEXT_BLOCK_TX1(events, blk_5, blk_4r, miner_acc, tx_2); + + // now in the main chain Bob has zero coins + // check it + CREATE_TEST_WALLET(bob_wlt, bob_acc, blk_0); + REFRESH_TEST_WALLET_AT_GEN_TIME(events, bob_wlt, blk_5, 3 * CURRENCY_MINED_MONEY_UNLOCK_WINDOW + 5); + CHECK_TEST_WALLET_BALANCE_AT_GEN_TIME(bob_wlt, 0); + + // TODO: check PoS mining against already spent key image + + std::list bob_stake_sources({ bob_acc }); + MAKE_NEXT_POS_BLOCK(events, blk_5a, blk_4r, bob_acc, bob_stake_sources); + MAKE_NEXT_BLOCK(events, blk_6a, blk_5a, miner_acc); + + DO_CALLBACK_PARAMS(events, "check_top_block", params_top_block(get_block_height(blk_6a), get_block_hash(blk_6a))); + + REWIND_BLOCKS_N_WITH_TIME(events, blk_6ar, blk_6a, miner_acc, CURRENCY_MINED_MONEY_UNLOCK_WINDOW); + + DO_CALLBACK(events, "c1"); + + //MAKE_NEXT_POS_BLOCK(events, blk_7a, blk_5a, miner_acc, miner_stake_sources); + + return true; +} + +bool zarcanum_in_alt_chain::c1(currency::core& c, size_t ev_index, const std::vector& events) +{ + bool r = false; + std::shared_ptr alice_wlt = init_playtime_test_wallet(events, c, ALICE_ACC_IDX); + alice_wlt->refresh(); + std::shared_ptr bob_wlt = init_playtime_test_wallet(events, c, BOB_ACC_IDX); + bob_wlt->refresh(); + + CHECK_AND_FORCE_ASSERT_MES(c.get_pool_transactions_count() == 1, false, "Incorrect txs count in the pool"); + + uint64_t stub = 0; + uint64_t alice_balance_before = alice_wlt->balance(stub); + uint64_t bob_balance_before = bob_wlt->balance(stub); + + uint64_t transfer_amount = COIN; + uint64_t transfer_fee = TESTS_DEFAULT_FEE * 3; + size_t nmix = 38; + bob_wlt->transfer(transfer_amount, nmix, m_accounts[ALICE_ACC_IDX].get_public_address(), transfer_fee); + + CHECK_AND_FORCE_ASSERT_MES(c.get_pool_transactions_count() == 2, false, "Incorrect txs count in the pool"); + + r = mine_next_pow_block_in_playtime(m_accounts[MINER_ACC_IDX].get_public_address(), c); + CHECK_AND_ASSERT_MES(r, false, "mine_next_pow_block_in_playtime failed"); + + CHECK_AND_FORCE_ASSERT_MES(c.get_pool_transactions_count() == 1, false, "Incorrect txs count in the pool"); + + alice_wlt->refresh(); + CHECK_AND_ASSERT_MES(check_balance_via_wallet(*alice_wlt, "Alice", alice_balance_before + transfer_amount ), false, ""); + + bob_wlt->refresh(); + CHECK_AND_ASSERT_MES(check_balance_via_wallet(*bob_wlt, "Bob", bob_balance_before - transfer_amount- transfer_fee), false, ""); + + return true; +} + diff --git a/tests/core_tests/zarcanum_test.h b/tests/core_tests/zarcanum_test.h index 6ecefca1..6b7e575b 100644 --- a/tests/core_tests/zarcanum_test.h +++ b/tests/core_tests/zarcanum_test.h @@ -38,4 +38,10 @@ struct zarcanum_txs_with_big_shuffled_decoy_set_shuffled : public wallet_test bool generate(std::vector& events) const; }; +struct zarcanum_in_alt_chain : public wallet_test +{ + zarcanum_in_alt_chain(); + bool generate(std::vector& events) const; + bool c1(currency::core& c, size_t ev_index, const std::vector& events); +}; From 834ad93094c9f31aa3e8e81d3a6b597560ff9d97 Mon Sep 17 00:00:00 2001 From: sowle Date: Wed, 23 Nov 2022 22:34:33 +0100 Subject: [PATCH 08/11] coretests: zarcanum_in_alt_chain added to the list --- tests/core_tests/chaingen_main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/core_tests/chaingen_main.cpp b/tests/core_tests/chaingen_main.cpp index 64d259db..2f650df5 100644 --- a/tests/core_tests/chaingen_main.cpp +++ b/tests/core_tests/chaingen_main.cpp @@ -1083,6 +1083,7 @@ int main(int argc, char* argv[]) GENERATE_AND_PLAY(zarcanum_gen_time_balance); GENERATE_AND_PLAY(zarcanum_txs_with_big_shuffled_decoy_set_shuffled); GENERATE_AND_PLAY(zarcanum_pos_block_math); + GENERATE_AND_PLAY(zarcanum_in_alt_chain); // GENERATE_AND_PLAY(gen_block_reward); // END OF TESTS */ From 28c6a0db9b73fb782db4eb8d6d5187efbc40d57c Mon Sep 17 00:00:00 2001 From: sowle Date: Thu, 24 Nov 2022 04:33:52 +0100 Subject: [PATCH 09/11] error logging verbosity improved in a few places --- src/currency_core/blockchain_storage.cpp | 8 ++++---- tests/core_tests/chaingen.cpp | 9 ++++++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/currency_core/blockchain_storage.cpp b/src/currency_core/blockchain_storage.cpp index f45dcfd2..10cbf547 100644 --- a/src/currency_core/blockchain_storage.cpp +++ b/src/currency_core/blockchain_storage.cpp @@ -2508,10 +2508,10 @@ bool blockchain_storage::add_out_to_get_random_outs(COMMAND_RPC_GET_RANDOM_OUTPU CRITICAL_REGION_LOCAL(m_read_lock); auto out_ptr = m_db_outputs.get_subitem(amount, g_index); auto tx_ptr = m_db_transactions.find(out_ptr->tx_id); - CHECK_AND_ASSERT_MES(tx_ptr, false, "internal error: transaction with id " << out_ptr->tx_id << ENDL << - ", used in mounts global index for amount=" << amount << ": g_index=" << g_index << "not found in transactions index"); + CHECK_AND_ASSERT_MES(tx_ptr, false, "internal error: transaction " << out_ptr->tx_id << " was not found in transaction DB, amount: " << print_money_brief(amount) << + ", g_index: " << g_index); CHECK_AND_ASSERT_MES(tx_ptr->tx.vout.size() > out_ptr->out_no, false, "internal error: in global outs index, transaction out index=" - << out_ptr->out_no << " more than transaction outputs = " << tx_ptr->tx.vout.size() << ", for tx id = " << out_ptr->tx_id); + << out_ptr->out_no << " is greater than transaction outputs = " << tx_ptr->tx.vout.size() << ", for tx id = " << out_ptr->tx_id); CHECK_AND_ASSERT_MES(amount != 0 || height_upper_limit != 0, false, "height_upper_limit must be nonzero for hidden amounts (amount = 0)"); @@ -2587,7 +2587,7 @@ size_t blockchain_storage::find_end_of_allowed_index(uint64_t amount) const --i; auto out_ptr = m_db_outputs.get_subitem(amount, i); auto tx_ptr = m_db_transactions.find(out_ptr->tx_id); - CHECK_AND_ASSERT_MES(tx_ptr, 0, "internal error: failed to find transaction from outputs index with tx_id=" << out_ptr->tx_id); + CHECK_AND_ASSERT_MES(tx_ptr, 0, "internal error: failed to find transaction from outputs index with tx_id=" << out_ptr->tx_id << ", amount: " << print_money_brief(amount)); if (tx_ptr->m_keeper_block_height + CURRENCY_MINED_MONEY_UNLOCK_WINDOW <= get_current_blockchain_size()) return i+1; } while (i != 0); diff --git a/tests/core_tests/chaingen.cpp b/tests/core_tests/chaingen.cpp index 18d33a06..2196792f 100644 --- a/tests/core_tests/chaingen.cpp +++ b/tests/core_tests/chaingen.cpp @@ -640,7 +640,14 @@ bool test_generator::find_kernel(const std::list& accs, iterations_processed_total += context.iterations_processed; } - LOG_PRINT_RED("PoS mining iteration failed, kernel was not found. Iterations processed across " << wallets.size() << " wallet(s): " << iterations_processed_total, LOG_LEVEL_0); + LOG_PRINT_RED("PoS mining iteration failed, kernel was not found. Iterations processed across " << wallets.size() << " wallet(s): " << iterations_processed_total << ENDL << + "Stake wallet(s) transfers:", LOG_LEVEL_0); + + for (size_t wallet_index = 0, size = wallets.size(); wallet_index < size; ++wallet_index) + { + std::shared_ptr w = wallets[wallet_index].wallet; + LOG_PRINT_L0("wallet #" << wallet_index << " @ block " << w->get_top_block_height() << ENDL << wallets[wallet_index].wallet->dump_trunsfers()); + } return false; } From 4579f172d85df111817721d5cc96f045dabd25c3 Mon Sep 17 00:00:00 2001 From: sowle Date: Thu, 24 Nov 2022 04:34:58 +0100 Subject: [PATCH 10/11] blockchain_storage::pop_transaction_from_global_index() adapted for HF4 --- src/currency_core/blockchain_storage.cpp | 27 +++++++++++++++--------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/currency_core/blockchain_storage.cpp b/src/currency_core/blockchain_storage.cpp index 10cbf547..57fc8ea4 100644 --- a/src/currency_core/blockchain_storage.cpp +++ b/src/currency_core/blockchain_storage.cpp @@ -3476,21 +3476,26 @@ bool blockchain_storage::get_outs(uint64_t amount, std::list bool blockchain_storage::pop_transaction_from_global_index(const transaction& tx, const crypto::hash& tx_id) { CRITICAL_REGION_LOCAL(m_read_lock); - size_t i = tx.vout.size()-1; + + auto do_pop_output = [&](size_t i, uint64_t amount) -> bool { + uint64_t sz = m_db_outputs.get_item_size(amount); + CHECK_AND_ASSERT_MES(sz, false, "transactions outs global index: empty index for amount: " << amount); + auto back_item = m_db_outputs.get_subitem(amount, sz - 1); + CHECK_AND_ASSERT_MES(back_item->tx_id == tx_id, false, "transactions outs global index consistency broken: tx id missmatch"); + CHECK_AND_ASSERT_MES(back_item->out_no == i, false, "transactions outs global index consistency broken: in transaction index missmatch"); + m_db_outputs.pop_back_item(amount); + return true; + }; + + size_t i = tx.vout.size() - 1; BOOST_REVERSE_FOREACH(const auto& otv, tx.vout) { VARIANT_SWITCH_BEGIN(otv); VARIANT_CASE_CONST(tx_out_bare, ot) if (ot.target.type() == typeid(txout_to_key) || ot.target.type() == typeid(txout_htlc)) { - uint64_t sz = m_db_outputs.get_item_size(ot.amount); - CHECK_AND_ASSERT_MES(sz, false, "transactions outs global index: empty index for amount: " << ot.amount); - auto back_item = m_db_outputs.get_subitem(ot.amount, sz - 1); - CHECK_AND_ASSERT_MES(back_item->tx_id == tx_id, false, "transactions outs global index consistency broken: tx id missmatch"); - CHECK_AND_ASSERT_MES(back_item->out_no == i, false, "transactions outs global index consistency broken: in transaction index missmatch"); - m_db_outputs.pop_back_item(ot.amount); - //if (!it->second.size()) - // m_db_outputs.erase(it); + if (!do_pop_output(i, ot.amount)) + return false; } else if (ot.target.type() == typeid(txout_multisig)) { @@ -3500,7 +3505,9 @@ bool blockchain_storage::pop_transaction_from_global_index(const transaction& tx CHECK_AND_ASSERT_MES(res, false, "Internal error: multisig out not found, multisig_out_id " << multisig_out_id << "in multisig outs index"); } VARIANT_CASE_CONST(tx_out_zarcanum, toz) - //@#@ + // TODO: @#@# temporary comment this section and make a test for the corresponding bug + if (!do_pop_output(i, 0)) + return false; VARIANT_CASE_THROW_ON_OTHER(); VARIANT_SWITCH_END(); --i; From b11e66aeffa89cecd96a4073b16a30906ed99c05 Mon Sep 17 00:00:00 2001 From: sowle Date: Thu, 24 Nov 2022 20:39:06 +0100 Subject: [PATCH 11/11] zarcanum testnet pre-alpha has been launched --- src/currency_core/currency_config.h | 9 +++++---- src/p2p/net_node.inl | 14 +++++++++----- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/currency_core/currency_config.h b/src/currency_core/currency_config.h index 64d0d8e5..e4fea06a 100644 --- a/src/currency_core/currency_config.h +++ b/src/currency_core/currency_config.h @@ -252,10 +252,11 @@ #define ZANO_HARDFORK_03_AFTER_HEIGHT 1082577 #define ZANO_HARDFORK_04_AFTER_HEIGHT 999999999 #else -#define ZANO_HARDFORK_01_AFTER_HEIGHT 1440 -#define ZANO_HARDFORK_02_AFTER_HEIGHT 1800 -#define ZANO_HARDFORK_03_AFTER_HEIGHT 1801 -#define ZANO_HARDFORK_04_AFTER_HEIGHT 999999999 +/////// Zarcanum Testnet Pre-Alpha ////////////////////////////// +#define ZANO_HARDFORK_01_AFTER_HEIGHT 1 +#define ZANO_HARDFORK_02_AFTER_HEIGHT 2 +#define ZANO_HARDFORK_03_AFTER_HEIGHT 3 +#define ZANO_HARDFORK_04_AFTER_HEIGHT 4 #endif diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index f112dbcc..04c3bd10 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -321,11 +321,15 @@ namespace nodetool ADD_HARDCODED_SEED_NODE("159.69.76.144", P2P_DEFAULT_PORT); ADD_HARDCODED_SEED_NODE("144.76.183.143", P2P_DEFAULT_PORT); #else - //TODO: - ADD_HARDCODED_SEED_NODE("95.217.43.225", P2P_DEFAULT_PORT); - ADD_HARDCODED_SEED_NODE("94.130.137.230", P2P_DEFAULT_PORT); - ADD_HARDCODED_SEED_NODE("95.217.42.247", P2P_DEFAULT_PORT); - ADD_HARDCODED_SEED_NODE("94.130.160.115", P2P_DEFAULT_PORT); + // TESTNET + //ADD_HARDCODED_SEED_NODE("95.217.43.225", P2P_DEFAULT_PORT); + //ADD_HARDCODED_SEED_NODE("94.130.137.230", P2P_DEFAULT_PORT); + //ADD_HARDCODED_SEED_NODE("95.217.42.247", P2P_DEFAULT_PORT); + //ADD_HARDCODED_SEED_NODE("94.130.160.115", P2P_DEFAULT_PORT); + + // TODO @#@# Here's the only Zarcanum Testnet Pre-Alpha node, update when ready + ADD_HARDCODED_SEED_NODE("95.217.42.247", 22070); + #endif bool res = handle_command_line(vm);