Repository navigation
Expand file tree
/
Copy pathservice_node_rules.cpp
More file actions
162 lines (135 loc) · 5.05 KB
/
Copy pathservice_node_rules.cpp
File metadata and controls
162 lines (135 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "cryptonote_config.h"
#include "common/worktips.h"
#include "int-util.h"
#include <vector>
#include <boost/lexical_cast.hpp>
#include "service_node_rules.h"
namespace service_nodes {
uint64_t get_staking_requirement(cryptonote::network_type m_nettype, uint64_t height, int hf_version)
{
if (m_nettype == cryptonote::TESTNET || m_nettype == cryptonote::FAKECHAIN)
return COIN * 100;
uint64_t hardfork_height = m_nettype == cryptonote::MAINNET ? 100 : 100 /* stagenet */;
if (height < hardfork_height) height = hardfork_height;
uint64_t height_adjusted = height - hardfork_height;
uint64_t base = 0, variable = 0;
if (height > 100)
{
base = 200000 * COIN;
variable = (250007.0 * COIN) / worktips::exp2(height_adjusted/6480000.0);
}
else {
if (hf_version >= cryptonote::network_version_11_infinite_staking)
{
base = 150000 * COIN;
variable = (250007.0 * COIN) / worktips::exp2(height_adjusted/129600.0);
}
else
{
base = 100000 * COIN;
variable = (350000.0 * COIN) / worktips::exp2(height_adjusted/129600.0);
}
}
uint64_t result = base + variable;
return result;
}
uint64_t portions_to_amount(uint64_t portions, uint64_t staking_requirement)
{
uint64_t hi, lo, resulthi, resultlo;
lo = mul128(staking_requirement, portions, &hi);
div128_64(hi, lo, STAKING_PORTIONS, &resulthi, &resultlo);
return resultlo;
}
bool check_service_node_portions(uint8_t hf_version, const std::vector<uint64_t>& portions)
{
if (portions.size() > MAX_NUMBER_OF_CONTRIBUTORS) return false;
uint64_t reserved = 0;
for (auto i = 0u; i < portions.size(); ++i)
{
const uint64_t min_portions = get_min_node_contribution(hf_version, STAKING_PORTIONS, reserved, i);
if (portions[i] < min_portions) return false;
reserved += portions[i];
}
return reserved <= STAKING_PORTIONS;
}
crypto::hash generate_request_stake_unlock_hash(uint32_t nonce)
{
crypto::hash result = {};
char const *nonce_ptr = (char *)&nonce;
char *hash_ptr = result.data;
static_assert(sizeof(result) % sizeof(nonce) == 0, "The nonce should be evenly divisible into the hash");
for (size_t i = 0; i < sizeof(result) / sizeof(nonce); ++i)
{
memcpy(hash_ptr, nonce_ptr, sizeof(nonce));
hash_ptr += sizeof(nonce);
}
assert(hash_ptr == (char *)result.data + sizeof(result));
return result;
}
uint64_t get_locked_key_image_unlock_height(cryptonote::network_type nettype, uint64_t node_register_height, uint64_t curr_height)
{
uint64_t blocks_to_lock = staking_num_lock_blocks(nettype);
uint64_t result = curr_height + (blocks_to_lock / 2);
return result;
}
static uint64_t get_min_node_contribution_pre_v11(uint64_t staking_requirement, uint64_t total_reserved)
{
return std::min(staking_requirement - total_reserved, staking_requirement / MAX_NUMBER_OF_CONTRIBUTORS);
}
uint64_t get_min_node_contribution(uint8_t version, uint64_t staking_requirement, uint64_t total_reserved, size_t num_contributions)
{
if (version < cryptonote::network_version_11_infinite_staking)
return get_min_node_contribution_pre_v11(staking_requirement, total_reserved);
const uint64_t needed = staking_requirement - total_reserved;
const size_t max_num_of_contributions = MAX_NUMBER_OF_CONTRIBUTORS * MAX_KEY_IMAGES_PER_CONTRIBUTOR;
assert(max_num_of_contributions > num_contributions);
if (max_num_of_contributions <= num_contributions) return UINT64_MAX;
const size_t num_contributions_remaining_avail = max_num_of_contributions - num_contributions;
return needed / num_contributions_remaining_avail;
}
uint64_t get_min_node_contribution_in_portions(uint8_t version, uint64_t staking_requirement, uint64_t total_reserved, size_t num_contributions)
{
uint64_t atomic_amount = get_min_node_contribution(version, staking_requirement, total_reserved, num_contributions);
uint64_t result = (atomic_amount == UINT64_MAX) ? UINT64_MAX : (get_portions_to_make_amount(staking_requirement, atomic_amount));
return result;
}
uint64_t get_portions_to_make_amount(uint64_t staking_requirement, uint64_t amount)
{
uint64_t lo, hi, resulthi, resultlo;
lo = mul128(amount, STAKING_PORTIONS, &hi);
if (lo > UINT64_MAX - (staking_requirement - 1))
hi++;
lo += staking_requirement-1;
div128_64(hi, lo, staking_requirement, &resulthi, &resultlo);
return resultlo;
}
static bool get_portions_from_percent(double cur_percent, uint64_t& portions) {
if(cur_percent < 0.0 || cur_percent > 100.0) return false;
// Fix for truncation issue when operator cut = 100 for a pool Service Node.
if (cur_percent == 100.0)
{
portions = STAKING_PORTIONS;
}
else
{
portions = (cur_percent / 100.0) * STAKING_PORTIONS;
}
return true;
}
bool get_portions_from_percent_str(std::string cut_str, uint64_t& portions) {
if(!cut_str.empty() && cut_str.back() == '%')
{
cut_str.pop_back();
}
double cut_percent;
try
{
cut_percent = boost::lexical_cast<double>(cut_str);
}
catch(...)
{
return false;
}
return get_portions_from_percent(cut_percent, portions);
}
} // namespace service_nodes