37#undef ELECTRONEUM_DEFAULT_LOG_CATEGORY
38#define ELECTRONEUM_DEFAULT_LOG_CATEGORY "hardfork"
60 forked_time(forked_time),
61 update_time(update_time),
62 window_size(window_size),
63 default_threshold_percent(default_threshold_percent),
64 original_version(original_version),
65 original_version_till_height(original_version_till_height),
69 throw "window_size needs to be strictly positive";
70 if (default_threshold_percent > 100)
71 throw "default_threshold_percent needs to be between 0 and 100";
81 if (!heights.empty()) {
82 if (version <= heights.back().version)
84 if (height <= heights.back().height)
86 if (time <= heights.back().time)
91 heights.push_back(
Params(version, height, threshold, time));
97 return add_fork(version, height, default_threshold_percent, time);
100uint8_t HardFork::get_effective_version(
uint8_t voting_version)
const
102 if (!heights.empty()) {
103 uint8_t max_version = heights.back().version;
104 if (voting_version > max_version)
105 voting_version = max_version;
107 return voting_version;
110bool HardFork::do_check(
uint8_t block_version,
uint8_t voting_version)
const
112 return block_version == heights[current_fork_index].version
113 && voting_version >= heights[current_fork_index].version;
119 return do_check(::get_block_version(
block), ::get_block_vote(
block));
124 int fork_index = get_voted_fork_index(height);
125 return block_version == heights[fork_index].version
126 && voting_version >= heights[fork_index].version;
132 return do_check_for_height(::get_block_version(
block), ::get_block_vote(
block), height);
139 if (!do_check(block_version, voting_version))
144 voting_version = get_effective_version(voting_version);
146 while (versions.size() >= window_size) {
147 const uint8_t old_version = versions.front();
148 assert(last_versions[old_version] >= 1);
149 last_versions[old_version]--;
150 versions.pop_front();
153 last_versions[voting_version]++;
154 versions.push_back(voting_version);
156 uint8_t voted = get_voted_fork_index(height + 1);
157 if (voted > current_fork_index) {
158 current_fork_index = voted;
166 return add(::get_block_version(
block), ::get_block_vote(
block), height);
175 heights.push_back(
Params(original_version, 0, 0, 0));
178 for (
size_t n = 0; n < 256; ++n)
179 last_versions[n] = 0;
180 current_fork_index = 0;
184 if (height > window_size)
185 height -= window_size - 1;
189 rescan_from_chain_height(height);
195 if (height <= original_version_till_height)
196 return original_version;
199 return ::get_block_version(
block);
205 if (height >= db.height())
208 bool stop_batch = db.batch_start();
212 for (
size_t n = 0; n < 256; ++n)
213 last_versions[n] = 0;
214 const uint64_t rescan_height = height >= (window_size - 1) ? height - (window_size -1) : 0;
215 const uint8_t start_version = height == 0 ? original_version : db.get_hard_fork_version(height);
216 while (current_fork_index > 0 && heights[current_fork_index].version > start_version) {
217 --current_fork_index;
219 for (
uint64_t h = rescan_height; h <= height; ++h) {
221 const uint8_t v = get_effective_version(get_block_vote(b));
223 versions.push_back(v);
226 uint8_t voted = get_voted_fork_index(height + 1);
227 if (voted > current_fork_index) {
228 current_fork_index = voted;
231 const uint64_t bc_height = db.height();
232 for (
uint64_t h = height + 1; h < bc_height; ++h) {
233 add(db.get_block_from_height(h), h);
249bool HardFork::rescan_from_block_height(
uint64_t height)
253 if (height >= db.
height())
258 for (
size_t n = 0; n < 256; ++n)
259 last_versions[n] = 0;
262 const uint8_t v = get_effective_version(get_block_vote(b));
264 versions.push_back(v);
268 current_fork_index = 0;
269 while (current_fork_index + 1 < heights.size() && heights[current_fork_index].version != lastv)
270 ++current_fork_index;
273 if (voted > current_fork_index) {
274 current_fork_index = voted;
280bool HardFork::rescan_from_chain_height(
uint64_t height)
284 return rescan_from_block_height(height - 1);
293 const uint64_t new_chain_height = db.height();
294 const uint64_t old_chain_height = new_chain_height + nblocks;
296 for (
uint64_t height = old_chain_height - 1; height >= new_chain_height; --height)
298 version = versions.back();
299 last_versions[version]--;
301 version = db.get_hard_fork_version(height);
302 versions.push_front(version);
303 last_versions[version]++;
307 for (current_fork_index = heights.size() - 1; current_fork_index > 0; --current_fork_index)
308 if (new_chain_height >= heights[current_fork_index].height)
312int HardFork::get_voted_fork_index(
uint64_t height)
const
316 for (
int n = heights.size() - 1; n >= 0; --n) {
317 uint8_t v = heights[n].version;
318 accumulated_votes += last_versions[v];
319 uint32_t threshold = (window_size * heights[n].threshold + 99) / 100;
320 if (height >= heights[n].height && accumulated_votes >= threshold) {
324 return current_fork_index;
332 if (heights.size() <= 1)
335 time_t t_last_fork = heights.back().time;
336 if (t >= t_last_fork + forked_time)
338 if (t >= t_last_fork + update_time)
351 if (height > db.height()) {
355 if (height == db.height()) {
358 return db.get_hard_fork_version(height);
364 return heights[current_fork_index].version;
370 return heights.back().version;
376 for (
unsigned int n = heights.size() - 1; n > 0; --n) {
377 if (height >= heights[n].height) {
378 return heights[n].version;
381 return original_version;
386 uint64_t height = std::numeric_limits<uint64_t>::max();
387 for (
auto i = heights.rbegin(); i != heights.rend(); ++i) {
388 if (i->version >= version) {
401 for (
auto i = heights.rbegin(); i != heights.rend(); ++i) {
402 if (height >= i->height) {
403 return (i == heights.rbegin() ? i : (i - 1))->version;
406 return original_version;
413 const uint8_t current_version = heights[current_fork_index].version;
414 const bool enabled = current_version >= version;
415 window = versions.size();
417 for (
size_t n = version; n < 256; ++n)
418 votes += last_versions[n];
419 threshold = (window * heights[current_fork_index].threshold + 99) / 100;
422 voting = heights.back().version;
The BlockchainDB backing store interface declaration/contract.
virtual uint8_t get_hard_fork_version(uint64_t height) const =0
checks which hardfork version a height is on
virtual block get_block_from_height(const uint64_t &height) const
fetch a block by height
virtual uint64_t height() const =0
fetch the current blockchain height
virtual void set_hard_fork_version(uint64_t height, uint8_t version)=0
sets which hardfork version a height is on
uint8_t get_ideal_version() const
returns the latest "ideal" version
bool add(const cryptonote::block &block, uint64_t height)
add a new block
void on_block_popped(uint64_t new_chain_height)
called when one or more blocks are popped from the blockchain
void init()
initialize the object
uint8_t get_current_version() const
returns the current version
bool reorganize_from_block_height(uint64_t height)
called when the blockchain is reorganized
bool get_voting_info(uint8_t version, uint32_t &window, uint32_t &votes, uint32_t &threshold, uint64_t &earliest_height, uint8_t &voting) const
returns information about current voting state
uint8_t get_next_version() const
returns the next version
bool add_fork(uint8_t version, uint64_t height, uint8_t threshold, time_t time)
add a new hardfork height
uint64_t get_earliest_ideal_height_for_version(uint8_t version) const
returns the earliest block a given version may activate
bool reorganize_from_chain_height(uint64_t height)
bool check(const cryptonote::block &block) const
check whether a new block would be accepted
bool check_for_height(const cryptonote::block &block, uint64_t height) const
same as check, but for a particular height, rather than the top
HardFork(cryptonote::BlockchainDB &db, uint8_t original_version=1, uint64_t original_version_till_height=DEFAULT_ORIGINAL_VERSION_TILL_HEIGHT, time_t forked_time=DEFAULT_FORKED_TIME, time_t update_time=DEFAULT_UPDATE_TIME, uint64_t window_size=DEFAULT_WINDOW_SIZE, uint8_t default_threshold_percent=DEFAULT_THRESHOLD_PERCENT)
creates a new HardFork object
State get_state(time_t t) const
returns current state at the given time
uint8_t get(uint64_t height) const
returns the hard fork version for the given block height
#define CHECK_AND_ASSERT_THROW_MES(expr, message)
Holds cryptonote related classes and helpers.
unsigned __int64 uint64_t
Params(uint8_t version, uint64_t height, uint8_t threshold, time_t time)
#define CRITICAL_REGION_LOCAL(x)