From a79fc219b75173d9257ecebb604d1ec24c9f1c2d Mon Sep 17 00:00:00 2001 From: stoffu Date: Tue, 20 Mar 2018 09:52:16 +0900 Subject: [PATCH] hardfork: fix get_earliest_ideal_height_for_version() to support non-existent versions --- src/cryptonote_basic/hardfork.cpp | 12 ++++++++---- tests/unit_tests/hardfork.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/cryptonote_basic/hardfork.cpp b/src/cryptonote_basic/hardfork.cpp index 95f1ecab9..aa6746304 100644 --- a/src/cryptonote_basic/hardfork.cpp +++ b/src/cryptonote_basic/hardfork.cpp @@ -379,11 +379,15 @@ uint8_t HardFork::get_ideal_version(uint64_t height) const uint64_t HardFork::get_earliest_ideal_height_for_version(uint8_t version) const { - for (unsigned int n = heights.size() - 1; n > 0; --n) { - if (heights[n].version <= version) - return heights[n].height; + uint64_t height = std::numeric_limits::max(); + for (auto i = heights.rbegin(); i != heights.rend(); ++i) { + if (i->version >= version) { + height = i->height; + } else { + break; + } } - return 0; + return height; } uint8_t HardFork::get_next_version() const diff --git a/tests/unit_tests/hardfork.cpp b/tests/unit_tests/hardfork.cpp index 7c27b9c5d..372cee513 100644 --- a/tests/unit_tests/hardfork.cpp +++ b/tests/unit_tests/hardfork.cpp @@ -554,3 +554,28 @@ TEST(get, higher) ASSERT_EQ(hf.get_ideal_version(7), 3); } +TEST(get, earliest_ideal_height) +{ + TestDB db; + HardFork hf(db, 1, 0, 1, 1, 4, 50); + + // v h t + ASSERT_TRUE(hf.add_fork(1, 0, 0)); + ASSERT_TRUE(hf.add_fork(2, 2, 1)); + ASSERT_TRUE(hf.add_fork(5, 5, 2)); + ASSERT_TRUE(hf.add_fork(6, 10, 3)); + ASSERT_TRUE(hf.add_fork(9, 15, 4)); + hf.init(); + + ASSERT_EQ(hf.get_earliest_ideal_height_for_version(1), 0); + ASSERT_EQ(hf.get_earliest_ideal_height_for_version(2), 2); + ASSERT_EQ(hf.get_earliest_ideal_height_for_version(3), 5); + ASSERT_EQ(hf.get_earliest_ideal_height_for_version(4), 5); + ASSERT_EQ(hf.get_earliest_ideal_height_for_version(5), 5); + ASSERT_EQ(hf.get_earliest_ideal_height_for_version(6), 10); + ASSERT_EQ(hf.get_earliest_ideal_height_for_version(7), 15); + ASSERT_EQ(hf.get_earliest_ideal_height_for_version(8), 15); + ASSERT_EQ(hf.get_earliest_ideal_height_for_version(9), 15); + ASSERT_EQ(hf.get_earliest_ideal_height_for_version(10), std::numeric_limits::max()); +} +