hardfork: fix get_earliest_ideal_height_for_version() to support non-existent versions

This commit is contained in:
stoffu 2018-03-20 09:52:16 +09:00
parent c534fe8d19
commit a79fc219b7
No known key found for this signature in database
GPG key ID: 41DAB8343A9EC012
2 changed files with 33 additions and 4 deletions

View file

@ -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

View file

@ -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());
}