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 3 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
6 changes: 5 additions & 1 deletion src/cryptonote_core/cryptonote_tx_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ using namespace epee;
#include "ringct/rctSigs.h"
#include "multisig/multisig.h"
#include "int-util.h"
#include <cfenv>

using namespace crypto;

Expand Down Expand Up @@ -981,6 +982,7 @@ namespace cryptonote
{
blobdata bd = get_block_hashing_blob(b);
rx_slow_hash(main_height, seed_height, seed_hash.data, bd.data(), bd.size(), res.data, 0, 1);
std::fesetround(FE_TONEAREST);
}

bool get_block_longhash(const Blockchain *pbc, const block& b, crypto::hash& res, const uint64_t height, const int miners)
Expand All @@ -993,7 +995,8 @@ namespace cryptonote
const_cast<int &>(miners) = 0;
#endif

if (hf_version >= network_version_12_checkpointing) {
if (hf_version >= network_version_12_checkpointing)
{
uint64_t seed_height, main_height;
crypto::hash hash;
if (pbc != NULL)
Expand All @@ -1008,6 +1011,7 @@ namespace cryptonote
main_height = 0;
}
rx_slow_hash(main_height, seed_height, hash.data, bd.data(), bd.size(), res.data, miners, 0);
std::fesetround(FE_TONEAREST);
return true;
}

Expand Down
56 changes: 56 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,6 +15,61 @@ 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;

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