Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn staking requirement into integer math variant #877

Merged
merged 4 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cryptonote_basic/cryptonote_basic_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ namespace cryptonote {

uint64_t block_reward_unpenalized_formula_v8(uint64_t height)
{
std::fesetround(FE_TONEAREST);
uint64_t result = 28000000000.0 + 100000000000.0 / loki::exp2(height / (720.0 * 90.0)); // halve every 90 days.
return result;
}
Expand Down
57 changes: 57 additions & 0 deletions src/cryptonote_core/service_node_rules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "int-util.h"
#include <vector>
#include <boost/lexical_cast.hpp>
#include <cfenv>

#include "service_node_rules.h"

Expand All @@ -14,11 +15,67 @@ uint64_t get_staking_requirement(cryptonote::network_type m_nettype, uint64_t he
if (m_nettype == cryptonote::TESTNET || m_nettype == cryptonote::FAKECHAIN)
return COIN * 100;

if (hf_version >= cryptonote::network_version_13_enforce_checkpoints)
{
constexpr int64_t heights[] = {
385824,
429024,
472224,
515424,
558624,
601824,
645024,
688224,
731424,
774624,
817824,
861024,
1000000,
};

constexpr int64_t lsr[] = {
20458380815527,
19332319724305,
18438564443912,
17729190407764,
17166159862153,
16719282221956,
16364595203882,
16083079931076,
15859641110978,
15682297601941,
15541539965538,
15429820555489,
15000000000000,
};

assert(height >= heights[0]);
constexpr uint64_t LAST_HEIGHT = heights[loki::array_count(heights) - 1];
constexpr uint64_t LAST_REQUIREMENT = lsr [loki::array_count(lsr) - 1];
if (height >= LAST_HEIGHT)
return LAST_REQUIREMENT;

size_t i = 0;
for (size_t index = 1; index < loki::array_count(heights); index++)
{
if (heights[index] > static_cast<int64_t>(height))
{
i = (index - 1);
break;
}
}

int64_t H = height;
int64_t result = lsr[i] + (H - heights[i]) * ((lsr[i + 1] - lsr[i]) / (heights[i + 1] - heights[i]));
return static_cast<uint64_t>(result);
}

uint64_t hardfork_height = m_nettype == cryptonote::MAINNET ? 101250 : 96210 /* stagenet */;
if (height < hardfork_height) height = hardfork_height;

uint64_t height_adjusted = height - hardfork_height;
uint64_t base = 0, variable = 0;
std::fesetround(FE_TONEAREST);
if (hf_version >= cryptonote::network_version_11_infinite_staking)
{
base = 15000 * COIN;
Expand Down
39 changes: 28 additions & 11 deletions tests/unit_tests/service_nodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ TEST(service_nodes, staking_requirement)
ASSERT_LT(mainnet_delta, atomic_epsilon);
}

// NOTE: Staking Requirement Algorithm Switch 2
// Sliftly after the boundary when the scheme switches over to a smooth emissions curve to 15k
{
uint64_t height = 235987;
Expand All @@ -98,24 +99,40 @@ TEST(service_nodes, staking_requirement)
ASSERT_LT(mainnet_delta, atomic_epsilon);
}

// Check requirements are decreasing after switching over to new requirements curve
// Check staking requirement on height whose value is different with different floating point rounding modes, we expect FE_TONEAREST.
{
uint64_t height = 706050;
uint64_t height = 373200;
int64_t mainnet_requirement = (int64_t)service_nodes::get_staking_requirement(cryptonote::MAINNET, height, cryptonote::network_version_11_infinite_staking);

int64_t mainnet_expected = (int64_t)((15984 * COIN) + 588930000);
int64_t mainnet_delta = std::abs(mainnet_requirement - mainnet_expected);
ASSERT_LT(mainnet_delta, atomic_epsilon);
int64_t mainnet_expected = (int64_t)((20839 * COIN) + 644149350);
ASSERT_EQ(mainnet_requirement, mainnet_expected);
}

// Check approaching 15k requirement
// NOTE: Staking Requirement Algorithm Switch: Integer Math Variant ^____^
{
uint64_t height = 3643650;
int64_t mainnet_requirement = (int64_t)service_nodes::get_staking_requirement(cryptonote::MAINNET, height, cryptonote::network_version_11_infinite_staking);
uint64_t height = 450000;
uint64_t mainnet_requirement = service_nodes::get_staking_requirement(cryptonote::MAINNET, height, cryptonote::network_version_13_enforce_checkpoints);

int64_t mainnet_expected = (int64_t)((15000 * COIN) + 150000);
int64_t mainnet_delta = std::abs(mainnet_requirement - mainnet_expected);
ASSERT_LT(mainnet_delta, atomic_epsilon);
uint64_t mainnet_expected = (18898 * COIN) + 351896001;
ASSERT_EQ(mainnet_requirement, mainnet_expected);
}

// Just before 15k boundary
{
uint64_t height = 999999;
uint64_t mainnet_requirement = service_nodes::get_staking_requirement(cryptonote::MAINNET, height, cryptonote::network_version_13_enforce_checkpoints);

uint64_t mainnet_expected = (15000 * COIN) + 3122689;
ASSERT_EQ(mainnet_requirement, mainnet_expected);
}

// 15k requirement boundary
{
uint64_t height = 1000000;
uint64_t mainnet_requirement = service_nodes::get_staking_requirement(cryptonote::MAINNET, height, cryptonote::network_version_13_enforce_checkpoints);

uint64_t mainnet_expected = 15000 * COIN;
ASSERT_EQ(mainnet_requirement, mainnet_expected);
}
}

Expand Down