Electroneum
Loading...
Searching...
No Matches
hardfork.cpp
Go to the documentation of this file.
1// Copyrights(c) 2017-2021, The Electroneum Project
2// Copyrights(c) 2014-2019, The Monero Project
3//
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without modification, are
7// permitted provided that the following conditions are met:
8//
9// 1. Redistributions of source code must retain the above copyright notice, this list of
10// conditions and the following disclaimer.
11//
12// 2. Redistributions in binary form must reproduce the above copyright notice, this list
13// of conditions and the following disclaimer in the documentation and/or other
14// materials provided with the distribution.
15//
16// 3. Neither the name of the copyright holder nor the names of its contributors may be
17// used to endorse or promote products derived from this software without specific
18// prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30#include <algorithm>
31#include <cstdio>
32
35#include "hardfork.h"
36
37#undef ELECTRONEUM_DEFAULT_LOG_CATEGORY
38#define ELECTRONEUM_DEFAULT_LOG_CATEGORY "hardfork"
39
40using namespace cryptonote;
41
42static uint8_t get_block_vote(const cryptonote::block &b)
43{
44 // Pre-hardfork blocks have a minor version hardcoded to 0.
45 // For the purposes of voting, we consider 0 to refer to
46 // version number 1, which is what all blocks from the genesis
47 // block are. It makes things simpler.
48 if (b.minor_version == 0)
49 return 1;
50 return b.minor_version;
51}
52
53static uint8_t get_block_version(const cryptonote::block &b)
54{
55 return b.major_version;
56}
57
58HardFork::HardFork(cryptonote::BlockchainDB &db, uint8_t original_version, uint64_t original_version_till_height, time_t forked_time, time_t update_time, uint64_t window_size, uint8_t default_threshold_percent):
59 db(db),
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),
66 current_fork_index(0)
67{
68 if (window_size == 0)
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";
72}
73
75{
77
78 // add in order
79 if (version == 0)
80 return false;
81 if (!heights.empty()) {
82 if (version <= heights.back().version)
83 return false;
84 if (height <= heights.back().height)
85 return false;
86 if (time <= heights.back().time)
87 return false;
88 }
89 if (threshold > 100)
90 return false;
91 heights.push_back(Params(version, height, threshold, time));
92 return true;
93}
94
96{
97 return add_fork(version, height, default_threshold_percent, time);
98}
99
100uint8_t HardFork::get_effective_version(uint8_t voting_version) const
101{
102 if (!heights.empty()) {
103 uint8_t max_version = heights.back().version;
104 if (voting_version > max_version)
105 voting_version = max_version;
106 }
107 return voting_version;
108}
109
110bool HardFork::do_check(uint8_t block_version, uint8_t voting_version) const
111{
112 return block_version == heights[current_fork_index].version
113 && voting_version >= heights[current_fork_index].version;
114}
115
117{
119 return do_check(::get_block_version(block), ::get_block_vote(block));
120}
121
122bool HardFork::do_check_for_height(uint8_t block_version, uint8_t voting_version, uint64_t height) const
123{
124 int fork_index = get_voted_fork_index(height);
125 return block_version == heights[fork_index].version
126 && voting_version >= heights[fork_index].version;
127}
128
130{
132 return do_check_for_height(::get_block_version(block), ::get_block_vote(block), height);
133}
134
135bool HardFork::add(uint8_t block_version, uint8_t voting_version, uint64_t height)
136{
138
139 if (!do_check(block_version, voting_version))
140 return false;
141
142 db.set_hard_fork_version(height, heights[current_fork_index].version);
143
144 voting_version = get_effective_version(voting_version);
145
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();
151 }
152
153 last_versions[voting_version]++;
154 versions.push_back(voting_version);
155
156 uint8_t voted = get_voted_fork_index(height + 1);
157 if (voted > current_fork_index) {
158 current_fork_index = voted;
159 }
160
161 return true;
162}
163
165{
166 return add(::get_block_version(block), ::get_block_vote(block), height);
167}
168
170{
172
173 // add a placeholder for the default version, to avoid special cases
174 if (heights.empty())
175 heights.push_back(Params(original_version, 0, 0, 0));
176
177 versions.clear();
178 for (size_t n = 0; n < 256; ++n)
179 last_versions[n] = 0;
180 current_fork_index = 0;
181
182 // restore state from DB
183 uint64_t height = db.height();
184 if (height > window_size)
185 height -= window_size - 1;
186 else
187 height = 1;
188
189 rescan_from_chain_height(height);
190 MDEBUG("init done");
191}
192
193uint8_t HardFork::get_block_version(uint64_t height) const
194{
195 if (height <= original_version_till_height)
196 return original_version;
197
199 return ::get_block_version(block);
200}
201
203{
205 if (height >= db.height())
206 return false;
207
208 bool stop_batch = db.batch_start();
209
210 versions.clear();
211
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;
218 }
219 for (uint64_t h = rescan_height; h <= height; ++h) {
220 cryptonote::block b = db.get_block_from_height(h);
221 const uint8_t v = get_effective_version(get_block_vote(b));
222 last_versions[v]++;
223 versions.push_back(v);
224 }
225
226 uint8_t voted = get_voted_fork_index(height + 1);
227 if (voted > current_fork_index) {
228 current_fork_index = voted;
229 }
230
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);
234 }
235
236 if (stop_batch)
237 db.batch_stop();
238
239 return true;
240}
241
243{
244 if (height == 0)
245 return false;
247}
248
249bool HardFork::rescan_from_block_height(uint64_t height)
250{
252 db_rtxn_guard rtxn_guard(&db);
253 if (height >= db.height())
254 return false;
255
256 versions.clear();
257
258 for (size_t n = 0; n < 256; ++n)
259 last_versions[n] = 0;
260 for (uint64_t h = height; h < db.height(); ++h) {
262 const uint8_t v = get_effective_version(get_block_vote(b));
263 last_versions[v]++;
264 versions.push_back(v);
265 }
266
267 uint8_t lastv = db.get_hard_fork_version(db.height() - 1);
268 current_fork_index = 0;
269 while (current_fork_index + 1 < heights.size() && heights[current_fork_index].version != lastv)
270 ++current_fork_index;
271
272 uint8_t voted = get_voted_fork_index(db.height());
273 if (voted > current_fork_index) {
274 current_fork_index = voted;
275 }
276
277 return true;
278}
279
280bool HardFork::rescan_from_chain_height(uint64_t height)
281{
282 if (height == 0)
283 return false;
284 return rescan_from_block_height(height - 1);
285}
286
288{
289 CHECK_AND_ASSERT_THROW_MES(nblocks > 0, "nblocks must be greater than 0");
290
292
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)
297 {
298 version = versions.back();
299 last_versions[version]--;
300 versions.pop_back();
301 version = db.get_hard_fork_version(height);
302 versions.push_front(version);
303 last_versions[version]++;
304 }
305
306 // does not take voting into account
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)
309 break;
310}
311
312int HardFork::get_voted_fork_index(uint64_t height) const
313{
315 uint32_t accumulated_votes = 0;
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) {
321 return n;
322 }
323 }
324 return current_fork_index;
325}
326
328{
330
331 // no hard forks setup yet
332 if (heights.size() <= 1)
333 return Ready;
334
335 time_t t_last_fork = heights.back().time;
336 if (t >= t_last_fork + forked_time)
337 return LikelyForked;
338 if (t >= t_last_fork + update_time)
339 return UpdateNeeded;
340 return Ready;
341}
342
344{
345 return get_state(time(NULL));
346}
347
349{
351 if (height > db.height()) {
352 assert(false);
353 return 255;
354 }
355 if (height == db.height()) {
356 return get_current_version();
357 }
358 return db.get_hard_fork_version(height);
359}
360
362{
364 return heights[current_fork_index].version;
365}
366
368{
370 return heights.back().version;
371}
372
374{
376 for (unsigned int n = heights.size() - 1; n > 0; --n) {
377 if (height >= heights[n].height) {
378 return heights[n].version;
379 }
380 }
381 return original_version;
382}
383
385{
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) {
389 height = i->height;
390 } else {
391 break;
392 }
393 }
394 return height;
395}
396
398{
400 uint64_t height = db.height();
401 for (auto i = heights.rbegin(); i != heights.rend(); ++i) {
402 if (height >= i->height) {
403 return (i == heights.rbegin() ? i : (i - 1))->version;
404 }
405 }
406 return original_version;
407}
408
409bool HardFork::get_voting_info(uint8_t version, uint32_t &window, uint32_t &votes, uint32_t &threshold, uint64_t &earliest_height, uint8_t &voting) const
410{
412
413 const uint8_t current_version = heights[current_fork_index].version;
414 const bool enabled = current_version >= version;
415 window = versions.size();
416 votes = 0;
417 for (size_t n = version; n < 256; ++n)
418 votes += last_versions[n];
419 threshold = (window * heights[current_fork_index].threshold + 99) / 100;
420 //assert((votes >= threshold) == enabled);
422 voting = heights.back().version;
423 return enabled;
424}
425
uint64_t height
uint8_t version
time_t time
uint8_t threshold
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
Definition hardfork.cpp:367
bool add(const cryptonote::block &block, uint64_t height)
add a new block
Definition hardfork.cpp:164
void on_block_popped(uint64_t new_chain_height)
called when one or more blocks are popped from the blockchain
Definition hardfork.cpp:287
void init()
initialize the object
Definition hardfork.cpp:169
uint8_t get_current_version() const
returns the current version
Definition hardfork.cpp:361
bool reorganize_from_block_height(uint64_t height)
called when the blockchain is reorganized
Definition hardfork.cpp:202
State get_state() const
Definition hardfork.cpp:343
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
Definition hardfork.cpp:409
uint8_t get_next_version() const
returns the next version
Definition hardfork.cpp:397
bool add_fork(uint8_t version, uint64_t height, uint8_t threshold, time_t time)
add a new hardfork height
Definition hardfork.cpp:74
uint64_t get_earliest_ideal_height_for_version(uint8_t version) const
returns the earliest block a given version may activate
Definition hardfork.cpp:384
bool reorganize_from_chain_height(uint64_t height)
Definition hardfork.cpp:242
bool check(const cryptonote::block &block) const
check whether a new block would be accepted
Definition hardfork.cpp:116
bool check_for_height(const cryptonote::block &block, uint64_t height) const
same as check, but for a particular height, rather than the top
Definition hardfork.cpp:129
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
Definition hardfork.cpp:58
uint8_t get(uint64_t height) const
returns the hard fork version for the given block height
Definition hardfork.cpp:348
#define MDEBUG(x)
Definition misc_log_ex.h:76
#define CHECK_AND_ASSERT_THROW_MES(expr, message)
Holds cryptonote related classes and helpers.
Definition ban.cpp:40
unsigned int uint32_t
Definition stdint.h:126
unsigned char uint8_t
Definition stdint.h:124
unsigned __int64 uint64_t
Definition stdint.h:136
#define CRITICAL_REGION_LOCAL(x)
Definition syncobj.h:228