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)
100 uint8_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;
110 bool 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);
157 if (voted > current_fork_index) {
158 current_fork_index = voted;
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;
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);
212 for (
size_t n = 0; n < 256; ++n)
213 last_versions[n] = 0;
216 while (current_fork_index > 0 && heights[current_fork_index].
version > start_version) {
217 --current_fork_index;
221 const uint8_t v = get_effective_version(get_block_vote(b));
223 versions.push_back(v);
227 if (voted > current_fork_index) {
228 current_fork_index = voted;
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;
284 return rescan_from_block_height(
height - 1);
294 const uint64_t old_chain_height = new_chain_height + nblocks;
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)
316 for (
int n = heights.size() - 1; n >= 0; --n) {
317 uint8_t v = heights[n].version;
318 accumulated_votes += last_versions[v];
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)
364 return heights[current_fork_index].version;
370 return heights.back().version;
376 for (
unsigned int n = heights.size() - 1; n > 0; --n) {
378 return heights[n].version;
381 return original_version;
387 for (
auto i = heights.rbegin(); i != heights.rend(); ++i) {
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;
#define CHECK_AND_ASSERT_THROW_MES(expr, message)
virtual void set_hard_fork_version(uint64_t height, uint8_t version)=0
sets which hardfork version a height is on
bool reorganize_from_chain_height(uint64_t height)
uint8_t get_current_version() const
returns the current version
uint8_t get_next_version() const
returns the next version
bool check(const cryptonote::block &block) const
check whether a new block would be accepted
void init()
initialize the object
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
void on_block_popped(uint64_t new_chain_height)
called when one or more blocks are popped from the blockchain
Holds cryptonote related classes and helpers.
virtual block get_block_from_height(const uint64_t &height) const
fetch a block by height
uint8_t get(uint64_t height) const
returns the hard fork version for the given block height
bool add(const cryptonote::block &block, uint64_t height)
add a new block
unsigned __int64 uint64_t
#define CRITICAL_REGION_LOCAL(x)
bool check_for_height(const cryptonote::block &block, uint64_t height) const
same as check, but for a particular height, rather than the top
virtual uint64_t height() const =0
fetch the current blockchain height
virtual void batch_stop()=0
ends a batch transaction
version
Supported socks variants.
uint8_t get_ideal_version() const
returns the latest "ideal" version
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
The BlockchainDB backing store interface declaration/contract.
uint64_t get_earliest_ideal_height_for_version(uint8_t version) const
returns the earliest block a given version may activate
bool add_fork(uint8_t version, uint64_t height, uint8_t threshold, time_t time)
add a new hardfork height
bool reorganize_from_block_height(uint64_t height)
called when the blockchain is reorganized
virtual uint8_t get_hard_fork_version(uint64_t height) const =0
checks which hardfork version a height is on
virtual bool batch_start(uint64_t batch_num_blocks=0, uint64_t batch_bytes=0)=0
tells the BlockchainDB to start a new "batch" of blocks