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