From 0383fb78d42d4e4c0a56672ca9bb68c84ef6493c Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Sat, 31 Aug 2019 22:53:04 +0200 Subject: [PATCH] fixed misprint in the fork choise rule --- src/currency_core/currency_format_utils.cpp | 2 +- tests/unit_tests/fork_choice_rule.cpp | 76 +++++++++++++++++++++ tests/unit_tests/lmdb_tests.cpp | 2 +- 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 tests/unit_tests/fork_choice_rule.cpp diff --git a/src/currency_core/currency_format_utils.cpp b/src/currency_core/currency_format_utils.cpp index 76537043..27b908c4 100644 --- a/src/currency_core/currency_format_utils.cpp +++ b/src/currency_core/currency_format_utils.cpp @@ -2715,7 +2715,7 @@ namespace currency 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 = - (basic_sum * a_pow_cumulative_difficulty * a_pos_cumulative_difficulty) / (boost::multiprecision::uint1024_t(a_pow_cumulative_difficulty)*a_pos_cumulative_difficulty); + (basic_sum * a_pow_cumulative_difficulty * a_pos_cumulative_difficulty) / (boost::multiprecision::uint1024_t(b_pow_cumulative_difficulty)*b_pos_cumulative_difficulty); if (res > boost::math::tools::max_value()) { diff --git a/tests/unit_tests/fork_choice_rule.cpp b/tests/unit_tests/fork_choice_rule.cpp new file mode 100644 index 00000000..c3fcf84b --- /dev/null +++ b/tests/unit_tests/fork_choice_rule.cpp @@ -0,0 +1,76 @@ +// Copyright (c) 2012-2013 The Cryptonote developers +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include "gtest/gtest.h" + +#include "include_base_utils.h" +#include "currency_core/currency_format_utils.h" + +bool if_alt_chain_stronger(const currency::wide_difficulty_type& pos, const currency::wide_difficulty_type& pow) +{ + currency::difficulties main_cumul_diff; + main_cumul_diff.pos_diff = 400000; + main_cumul_diff.pow_diff = 4000; + currency::difficulties alt_cumul_diff; + alt_cumul_diff.pow_diff = pow; + alt_cumul_diff.pos_diff = pos; + static currency::wide_difficulty_type difficulty_pos_at_split_point = 400000; + static currency::wide_difficulty_type difficulty_pow_at_split_point = 4000; + currency::wide_difficulty_type main = currency::get_a_to_b_relative_cumulative_difficulty(difficulty_pos_at_split_point, difficulty_pow_at_split_point, main_cumul_diff, alt_cumul_diff); + currency::wide_difficulty_type alt = currency::get_a_to_b_relative_cumulative_difficulty(difficulty_pos_at_split_point, difficulty_pow_at_split_point, alt_cumul_diff, main_cumul_diff); + if (alt > main) + return true; + return false; +} + +TEST(fork_choice_rule_test, fork_choice_rule_test_1) +{ +// std::stringstream ss; +// for (uint64_t pos = 100000; pos < 1000001; pos += 10000) +// { +// for (uint64_t pow = 100; pow < 18000; pow += 100) +// { +// bool r = if_alt_chain_stronger(pos, pow); +// if(r) +// ss << pos << "\t" << pow << std::endl; +// //ss << pos << "\t" << pow << "\t" << (r ? "1" : "0") << std::endl; +// +// +// } +// } +// bool r = epee::file_io_utils::save_string_to_file("stat.txt", ss.str()); + bool res = false; + res = if_alt_chain_stronger(1000000, 1000); + ASSERT_FALSE(res); + res = if_alt_chain_stronger(1000000, 1500); + ASSERT_TRUE(res); + res = if_alt_chain_stronger(800000, 1700); + ASSERT_FALSE(res); + res = if_alt_chain_stronger(800000, 2000); + ASSERT_TRUE(res); + res = if_alt_chain_stronger(600000, 2200); + ASSERT_FALSE(res); + res = if_alt_chain_stronger(600000, 2800); + ASSERT_TRUE(res); + res = if_alt_chain_stronger(400000, 3999); + ASSERT_FALSE(res); + res = if_alt_chain_stronger(400000, 4001); + ASSERT_TRUE(res); + res = if_alt_chain_stronger(200000, 7000); + ASSERT_FALSE(res); + res = if_alt_chain_stronger(200000, 7700); + ASSERT_TRUE(res); + res = if_alt_chain_stronger(200000, 7000); + ASSERT_FALSE(res); + res = if_alt_chain_stronger(200000, 7700); + ASSERT_TRUE(res); + res = if_alt_chain_stronger(100000, 10000); + ASSERT_FALSE(res); + res = if_alt_chain_stronger(200000, 14000); + ASSERT_TRUE(res); + + + + +} diff --git a/tests/unit_tests/lmdb_tests.cpp b/tests/unit_tests/lmdb_tests.cpp index 10dc1229..a155df5f 100644 --- a/tests/unit_tests/lmdb_tests.cpp +++ b/tests/unit_tests/lmdb_tests.cpp @@ -54,7 +54,7 @@ namespace lmdb_test if (key % 1024 == 0) { ASSERT_TRUE(lmdb_ptr->commit_transaction()); - ASSERT_TRUE(lmdb_ptr->resize_if_needed()); + //ASSERT_TRUE(lmdb_ptr->resize_if_needed()); ASSERT_TRUE(lmdb_ptr->begin_transaction()); std::cout << total_data / 1024 / 1024 << " MB written to DB" << ENDL; }