diff options
author | stoffu <stoffu@protonmail.ch> | 2018-03-20 09:52:16 +0900 |
---|---|---|
committer | stoffu <stoffu@protonmail.ch> | 2018-05-30 09:49:52 +0900 |
commit | a79fc219b75173d9257ecebb604d1ec24c9f1c2d (patch) | |
tree | 6ddae269b0ebf76546aeefba8f25a2e860fc65a5 | |
parent | Merge pull request #3251 (diff) | |
download | monero-a79fc219b75173d9257ecebb604d1ec24c9f1c2d.tar.xz |
hardfork: fix get_earliest_ideal_height_for_version() to support non-existent versions
-rw-r--r-- | src/cryptonote_basic/hardfork.cpp | 12 | ||||
-rw-r--r-- | 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<uint64_t>::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<uint64_t>::max()); +} + |