| 1 | // Copyright (c) 2019-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 | #define IN_UNIT_TESTS |
| 30 | |
| 31 | #include "gtest/gtest.h" |
| 32 | #include "cryptonote_core/blockchain.h" |
| 33 | #include "cryptonote_core/tx_pool.h" |
| 34 | #include "cryptonote_core/cryptonote_core.h" |
| 35 | #include "blockchain_db/testdb.h" |
| 36 | |
| 37 | #define TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW 5000 |
| 38 | |
| 39 | namespace |
| 40 | { |
| 41 | |
| 42 | class TestDB: public cryptonote::BaseTestDB |
| 43 | { |
| 44 | private: |
| 45 | struct block_t |
| 46 | { |
| 47 | size_t weight; |
| 48 | uint64_t long_term_weight; |
| 49 | }; |
| 50 | |
| 51 | public: |
| 52 | TestDB() { m_open = true; } |
| 53 | |
| 54 | virtual void add_block( const cryptonote::block& blk |
| 55 | , size_t block_weight |
| 56 | , uint64_t long_term_block_weight |
| 57 | , const cryptonote::difficulty_type& cumulative_difficulty |
| 58 | , const uint64_t& coins_generated |
| 59 | , uint64_t num_rct_outs |
| 60 | , const crypto::hash& blk_hash |
| 61 | ) override { |
| 62 | blocks.push_back({block_weight, long_term_block_weight}); |
| 63 | } |
| 64 | virtual uint64_t height() const override { return blocks.size(); } |
| 65 | virtual size_t get_block_weight(const uint64_t &h) const override { return blocks[h].weight; } |
| 66 | virtual uint64_t get_block_long_term_weight(const uint64_t &h) const override { return blocks[h].long_term_weight; } |
| 67 | virtual std::vector<uint64_t> get_block_weights(uint64_t start_height, size_t count) const override { |
| 68 | std::vector<uint64_t> ret; |
| 69 | ret.reserve(count); |
| 70 | while (count-- && start_height < blocks.size()) ret.push_back(blocks[start_height++].weight); |
| 71 | return ret; |
| 72 | } |
| 73 | virtual std::vector<uint64_t> get_long_term_block_weights(uint64_t start_height, size_t count) const override { |
| 74 | std::vector<uint64_t> ret; |
| 75 | ret.reserve(count); |
| 76 | while (count-- && start_height < blocks.size()) ret.push_back(blocks[start_height++].long_term_weight); |
| 77 | return ret; |
| 78 | } |
| 79 | virtual crypto::hash get_block_hash_from_height(const uint64_t &height) const override { |
| 80 | crypto::hash hash = crypto::null_hash; |
| 81 | *(uint64_t*)&hash = height; |
| 82 | return hash; |
| 83 | } |
| 84 | virtual crypto::hash top_block_hash(uint64_t *block_height = NULL) const override { |
| 85 | uint64_t h = height(); |
| 86 | crypto::hash top = crypto::null_hash; |
| 87 | if (h) |
| 88 | *(uint64_t*)&top = h - 1; |
| 89 | if (block_height) |
| 90 | *block_height = h - 1; |
| 91 | return top; |
| 92 | } |
| 93 | virtual void pop_block(cryptonote::block &blk, std::vector<cryptonote::transaction> &txs) override { blocks.pop_back(); } |
| 94 | |
| 95 | private: |
| 96 | std::vector<block_t> blocks; |
| 97 | }; |
| 98 | |
| 99 | static uint32_t lcg_seed = 0; |
| 100 | |
| 101 | static uint32_t lcg() |
| 102 | { |
| 103 | lcg_seed = (lcg_seed * 0x100000001b3 + 0xcbf29ce484222325) & 0xffffffff; |
| 104 | return lcg_seed; |
| 105 | } |
| 106 | |
| 107 | } |
| 108 | |
| 109 | struct BlockchainAndPool |
| 110 | { |
| 111 | cryptonote::tx_memory_pool txpool; |
| 112 | cryptonote::Blockchain bc; |
| 113 | BlockchainAndPool(): txpool(bc), bc(txpool) {} |
| 114 | }; |
| 115 | |
| 116 | #define PREFIX_WINDOW(hf_version,window) \ |
| 117 | BlockchainAndPool bap; \ |
| 118 | cryptonote::Blockchain *bc = &bap.bc; \ |
| 119 | struct get_test_options { \ |
| 120 | const std::pair<uint8_t, uint64_t> hard_forks[3]; \ |
| 121 | const cryptonote::test_options test_options = { \ |
| 122 | hard_forks, \ |
| 123 | window, \ |
| 124 | }; \ |
| 125 | get_test_options(): hard_forks{std::make_pair(1, (uint64_t)0), std::make_pair((uint8_t)hf_version, (uint64_t)1), std::make_pair((uint8_t)0, (uint64_t)0)} {} \ |
| 126 | } opts; \ |
| 127 | bool r = bc->init(new TestDB(), cryptonote::FAKECHAIN, true, &opts.test_options, 0, NULL); \ |
| 128 | ASSERT_TRUE(r) |
| 129 | |
| 130 | #define PREFIX(hf_version) PREFIX_WINDOW(hf_version, TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW) |
| 131 | |
| 132 | TEST(long_term_block_weight, empty_short) |
| 133 | { |
| 134 | PREFIX(9); |
| 135 | |
| 136 | ASSERT_TRUE(bc->update_next_cumulative_weight_limit()); |
| 137 | |
| 138 | ASSERT_EQ(bc->get_current_cumulative_block_weight_median(), CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V5); |
| 139 | ASSERT_EQ(bc->get_current_cumulative_block_weight_limit(), CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V5 * 2); |
| 140 | } |
| 141 | |
| 142 | TEST(long_term_block_weight, identical_before_fork) |
| 143 | { |
| 144 | PREFIX(9); |
| 145 | |
| 146 | for (uint64_t h = 1; h < 10 * TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW; ++h) |
| 147 | { |
| 148 | size_t w = h < CRYPTONOTE_REWARD_BLOCKS_WINDOW ? CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V5 : bc->get_current_cumulative_block_weight_limit(); |
| 149 | uint64_t ltw = bc->get_next_long_term_block_weight(w); |
| 150 | bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {}); |
| 151 | ASSERT_TRUE(bc->update_next_cumulative_weight_limit()); |
| 152 | } |
| 153 | for (uint64_t h = 0; h < 10 * TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW; ++h) |
| 154 | { |
| 155 | ASSERT_EQ(bc->get_db().get_block_long_term_weight(h), bc->get_db().get_block_weight(h)); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | TEST(long_term_block_weight, identical_after_fork_before_long_term_window) |
| 160 | { |
| 161 | PREFIX(10); |
| 162 | |
| 163 | for (uint64_t h = 1; h <= TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW; ++h) |
| 164 | { |
| 165 | size_t w = h < TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW ? CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V5 : bc->get_current_cumulative_block_weight_limit(); |
| 166 | uint64_t ltw = bc->get_next_long_term_block_weight(w); |
| 167 | bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {}); |
| 168 | ASSERT_TRUE(bc->update_next_cumulative_weight_limit()); |
| 169 | } |
| 170 | for (uint64_t h = 0; h < TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW; ++h) |
| 171 | { |
| 172 | ASSERT_EQ(bc->get_db().get_block_long_term_weight(h), bc->get_db().get_block_weight(h)); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | TEST(long_term_block_weight, ceiling_at_30000000) |
| 177 | { |
| 178 | PREFIX(10); |
| 179 | |
| 180 | for (uint64_t h = 0; h < TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW + TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW / 2 - 1; ++h) |
| 181 | { |
| 182 | size_t w = h < TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW ? CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V5 : bc->get_current_cumulative_block_weight_limit(); |
| 183 | uint64_t ltw = bc->get_next_long_term_block_weight(w); |
| 184 | bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {}); |
| 185 | ASSERT_TRUE(bc->update_next_cumulative_weight_limit()); |
| 186 | } |
| 187 | ASSERT_EQ(bc->get_current_cumulative_block_weight_median(), 15000000); |
| 188 | ASSERT_EQ(bc->get_current_cumulative_block_weight_limit(), 30000000); |
| 189 | } |
| 190 | |
| 191 | TEST(long_term_block_weight, multi_pop) |
| 192 | { |
| 193 | PREFIX(10); |
| 194 | |
| 195 | for (uint64_t h = 1; h <= TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW + 20; ++h) |
| 196 | { |
| 197 | size_t w = h < TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW ? CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V5 : bc->get_current_cumulative_block_weight_limit(); |
| 198 | uint64_t ltw = bc->get_next_long_term_block_weight(w); |
| 199 | bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {}); |
| 200 | ASSERT_TRUE(bc->update_next_cumulative_weight_limit()); |
| 201 | } |
| 202 | |
| 203 | const uint64_t effective_median = bc->get_current_cumulative_block_weight_median(); |
| 204 | const uint64_t effective_limit = bc->get_current_cumulative_block_weight_limit(); |
| 205 | |
| 206 | const uint64_t num_pop = 4; |
| 207 | for (uint64_t h = 0; h < num_pop; ++h) |
| 208 | { |
| 209 | size_t w = bc->get_current_cumulative_block_weight_limit(); |
| 210 | uint64_t ltw = bc->get_next_long_term_block_weight(w); |
| 211 | bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {}); |
| 212 | ASSERT_TRUE(bc->update_next_cumulative_weight_limit()); |
| 213 | } |
| 214 | |
| 215 | cryptonote::block b; |
| 216 | std::vector<cryptonote::transaction> txs; |
| 217 | for (uint64_t h = 0; h < num_pop; ++h) |
| 218 | bc->get_db().pop_block(b, txs); |
| 219 | ASSERT_TRUE(bc->update_next_cumulative_weight_limit()); |
| 220 | |
| 221 | ASSERT_EQ(effective_median, bc->get_current_cumulative_block_weight_median()); |
| 222 | ASSERT_EQ(effective_limit, bc->get_current_cumulative_block_weight_limit()); |
| 223 | } |
| 224 | |
| 225 | TEST(long_term_block_weight, multiple_updates) |
| 226 | { |
| 227 | PREFIX(10); |
| 228 | |
| 229 | for (uint64_t h = 1; h <= 3 * TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW; ++h) |
| 230 | { |
| 231 | size_t w = h < TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW ? CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V5 : bc->get_current_cumulative_block_weight_limit(); |
| 232 | uint64_t ltw = bc->get_next_long_term_block_weight(w); |
| 233 | bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {}); |
| 234 | ASSERT_TRUE(bc->update_next_cumulative_weight_limit()); |
| 235 | const uint64_t effective_median = bc->get_current_cumulative_block_weight_median(); |
| 236 | const uint64_t effective_limit = bc->get_current_cumulative_block_weight_limit(); |
| 237 | ASSERT_TRUE(bc->update_next_cumulative_weight_limit()); |
| 238 | ASSERT_EQ(effective_median, bc->get_current_cumulative_block_weight_median()); |
| 239 | ASSERT_EQ(effective_limit, bc->get_current_cumulative_block_weight_limit()); |
| 240 | ASSERT_TRUE(bc->update_next_cumulative_weight_limit()); |
| 241 | ASSERT_EQ(effective_median, bc->get_current_cumulative_block_weight_median()); |
| 242 | ASSERT_EQ(effective_limit, bc->get_current_cumulative_block_weight_limit()); |
| 243 | ASSERT_TRUE(bc->update_next_cumulative_weight_limit()); |
| 244 | ASSERT_EQ(effective_median, bc->get_current_cumulative_block_weight_median()); |
| 245 | ASSERT_EQ(effective_limit, bc->get_current_cumulative_block_weight_limit()); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | TEST(long_term_block_weight, pop_invariant_max) |
| 250 | { |
| 251 | PREFIX(10); |
| 252 | |
| 253 | for (uint64_t h = 1; h < TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW - 10; ++h) |
| 254 | { |
| 255 | size_t w = bc->get_db().height() < TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW ? CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V5 : bc->get_current_cumulative_block_weight_limit(); |
| 256 | uint64_t ltw = bc->get_next_long_term_block_weight(w); |
| 257 | bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {}); |
| 258 | ASSERT_TRUE(bc->update_next_cumulative_weight_limit()); |
| 259 | } |
| 260 | |
| 261 | for (int n = 0; n < 1000; ++n) |
| 262 | { |
| 263 | // pop some blocks, then add some more |
| 264 | int remove = 1 + (n * 17) % 8; |
| 265 | int add = (n * 23) % 12; |
| 266 | |
| 267 | // save long term block weights we're about to remove |
| 268 | uint64_t old_ltbw[16], h0 = bc->get_db().height() - remove - 1; |
| 269 | for (int i = -2; i < remove; ++i) |
| 270 | { |
| 271 | old_ltbw[i + 2] = bc->get_db().get_block_long_term_weight(h0 + i); |
| 272 | } |
| 273 | |
| 274 | for (int i = 0; i < remove; ++i) |
| 275 | { |
| 276 | cryptonote::block b; |
| 277 | std::vector<cryptonote::transaction> txs; |
| 278 | bc->get_db().pop_block(b, txs); |
| 279 | ASSERT_TRUE(bc->update_next_cumulative_weight_limit()); |
| 280 | } |
| 281 | for (int i = 0; i < add; ++i) |
| 282 | { |
| 283 | size_t w = bc->get_db().height() < TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW ? CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V5 : bc->get_current_cumulative_block_weight_limit(); |
| 284 | uint64_t ltw = bc->get_next_long_term_block_weight(w); |
| 285 | bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, bc->get_db().height(), bc->get_db().height(), {}); |
| 286 | ASSERT_TRUE(bc->update_next_cumulative_weight_limit()); |
| 287 | } |
| 288 | |
| 289 | // check the new values are the same as the old ones |
| 290 | for (int i = -2; i < std::min(add, remove); ++i) |
| 291 | { |
| 292 | ASSERT_EQ(bc->get_db().get_block_long_term_weight(h0 + i), old_ltbw[i + 2]); |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | TEST(long_term_block_weight, pop_invariant_random) |
| 298 | { |
| 299 | PREFIX(10); |
| 300 | |
| 301 | for (uint64_t h = 1; h < 2 * TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW - 10; ++h) |
| 302 | { |
| 303 | lcg_seed = bc->get_db().height(); |
| 304 | uint32_t r = lcg(); |
| 305 | size_t w = bc->get_db().height() < TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW ? CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V5 : (r % bc->get_current_cumulative_block_weight_limit()); |
| 306 | uint64_t ltw = bc->get_next_long_term_block_weight(w); |
| 307 | bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {}); |
| 308 | ASSERT_TRUE(bc->update_next_cumulative_weight_limit()); |
| 309 | } |
| 310 | |
| 311 | for (int n = 0; n < 1000; ++n) |
| 312 | { |
| 313 | // pop some blocks, then add some more |
| 314 | int remove = 1 + (n * 17) % 8; |
| 315 | int add = (n * 23) % 123; |
| 316 | |
| 317 | // save long term block weights we're about to remove |
| 318 | uint64_t old_ltbw[16], h0 = bc->get_db().height() - remove - 1; |
| 319 | for (int i = -2; i < remove; ++i) |
| 320 | { |
| 321 | old_ltbw[i + 2] = bc->get_db().get_block_long_term_weight(h0 + i); |
| 322 | } |
| 323 | |
| 324 | for (int i = 0; i < remove; ++i) |
| 325 | { |
| 326 | cryptonote::block b; |
| 327 | std::vector<cryptonote::transaction> txs; |
| 328 | bc->get_db().pop_block(b, txs); |
| 329 | ASSERT_TRUE(bc->update_next_cumulative_weight_limit()); |
| 330 | const uint64_t effective_median = bc->get_current_cumulative_block_weight_median(); |
| 331 | const uint64_t effective_limit = bc->get_current_cumulative_block_weight_limit(); |
| 332 | ASSERT_TRUE(bc->update_next_cumulative_weight_limit()); |
| 333 | ASSERT_EQ(effective_median, bc->get_current_cumulative_block_weight_median()); |
| 334 | ASSERT_EQ(effective_limit, bc->get_current_cumulative_block_weight_limit()); |
| 335 | } |
| 336 | for (int i = 0; i < add; ++i) |
| 337 | { |
| 338 | lcg_seed = bc->get_db().height(); |
| 339 | uint32_t r = lcg(); |
| 340 | size_t w = bc->get_db().height() < TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW ? CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V5 : (r % bc->get_current_cumulative_block_weight_limit()); |
| 341 | uint64_t ltw = bc->get_next_long_term_block_weight(w); |
| 342 | bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, bc->get_db().height(), bc->get_db().height(), {}); |
| 343 | ASSERT_TRUE(bc->update_next_cumulative_weight_limit()); |
| 344 | const uint64_t effective_median = bc->get_current_cumulative_block_weight_median(); |
| 345 | const uint64_t effective_limit = bc->get_current_cumulative_block_weight_limit(); |
| 346 | ASSERT_TRUE(bc->update_next_cumulative_weight_limit()); |
| 347 | ASSERT_EQ(effective_median, bc->get_current_cumulative_block_weight_median()); |
| 348 | ASSERT_EQ(effective_limit, bc->get_current_cumulative_block_weight_limit()); |
| 349 | } |
| 350 | |
| 351 | // check the new values are the same as the old ones |
| 352 | for (int i = -2; i < std::min(add, remove); ++i) |
| 353 | { |
| 354 | ASSERT_EQ(bc->get_db().get_block_long_term_weight(h0 + i), old_ltbw[i + 2]); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | TEST(long_term_block_weight, long_growth_spike_and_drop) |
| 360 | { |
| 361 | PREFIX(10); |
| 362 | |
| 363 | uint64_t long_term_effective_median_block_weight; |
| 364 | |
| 365 | // constant init |
| 366 | for (uint64_t h = 0; h < TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW; ++h) |
| 367 | { |
| 368 | size_t w = CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V5; |
| 369 | uint64_t ltw = bc->get_next_long_term_block_weight(w); |
| 370 | bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {}); |
| 371 | ASSERT_TRUE(bc->update_next_cumulative_weight_limit(&long_term_effective_median_block_weight)); |
| 372 | } |
| 373 | ASSERT_EQ(long_term_effective_median_block_weight, 300000); |
| 374 | |
| 375 | // slow 10% yearly for a year (scaled down by 100000 / TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW) -> 8% change |
| 376 | for (uint64_t h = 0; h < 365 * 720 * TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW / 100000; ++h) |
| 377 | { |
| 378 | //size_t w = bc->get_current_cumulative_block_weight_median() * rate; |
| 379 | float t = h / float(365 * 720 * TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW / 100000); |
| 380 | size_t w = 300000 + t * 30000; |
| 381 | uint64_t ltw = bc->get_next_long_term_block_weight(w); |
| 382 | bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {}); |
| 383 | ASSERT_TRUE(bc->update_next_cumulative_weight_limit(&long_term_effective_median_block_weight)); |
| 384 | } |
| 385 | ASSERT_GT(long_term_effective_median_block_weight, 300000 * 1.07); |
| 386 | ASSERT_LT(long_term_effective_median_block_weight, 300000 * 1.09); |
| 387 | |
| 388 | // spike over three weeks - does not move much |
| 389 | for (uint64_t h = 0; h < 21 * 720 * TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW / 100000; ++h) |
| 390 | { |
| 391 | size_t w = bc->get_current_cumulative_block_weight_limit(); |
| 392 | uint64_t ltw = bc->get_next_long_term_block_weight(w); |
| 393 | bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {}); |
| 394 | ASSERT_TRUE(bc->update_next_cumulative_weight_limit(&long_term_effective_median_block_weight)); |
| 395 | } |
| 396 | ASSERT_GT(long_term_effective_median_block_weight, 300000 * 1.07); |
| 397 | ASSERT_LT(long_term_effective_median_block_weight, 300000 * 1.09); |
| 398 | |
| 399 | // drop - does not move much |
| 400 | for (uint64_t h = 0; h < 21 * 720 * TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW / 100000; ++h) |
| 401 | { |
| 402 | size_t w = bc->get_current_cumulative_block_weight_median() * .25; |
| 403 | uint64_t ltw = bc->get_next_long_term_block_weight(w); |
| 404 | bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {}); |
| 405 | ASSERT_TRUE(bc->update_next_cumulative_weight_limit(&long_term_effective_median_block_weight)); |
| 406 | } |
| 407 | ASSERT_GT(long_term_effective_median_block_weight, 300000 * 1.07); |
| 408 | ASSERT_LT(long_term_effective_median_block_weight, 300000 * 1.09); |
| 409 | } |
| 410 | |
| 411 | TEST(long_term_block_weight, cache_matches_true_value) |
| 412 | { |
| 413 | PREFIX(16); |
| 414 | |
| 415 | // Add big blocks to increase the block weight limit |
| 416 | for (uint64_t h = 0; h <= 2000; ++h) |
| 417 | { |
| 418 | size_t w = bc->get_current_cumulative_block_weight_limit(); |
| 419 | uint64_t ltw = bc->get_next_long_term_block_weight(w); |
| 420 | bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {}); |
| 421 | bc->update_next_cumulative_weight_limit(); |
| 422 | } |
| 423 | |
| 424 | ASSERT_GT(bc->get_current_cumulative_block_weight_limit() * 10/17 , 300000); |
| 425 | |
| 426 | // Add small blocks to the top of the chain |
| 427 | for (uint64_t h = 2000; h <= 5001; ++h) |
| 428 | { |
| 429 | size_t w = (bc->get_current_cumulative_block_weight_median() * 10/17) - 1000; |
| 430 | uint64_t ltw = bc->get_next_long_term_block_weight(w); |
| 431 | bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {}); |
| 432 | bc->update_next_cumulative_weight_limit(); |
| 433 | } |
| 434 | |
| 435 | // get the weight limit |
| 436 | uint64_t weight_limit = bc->get_current_cumulative_block_weight_limit(); |
| 437 | // refresh the cache |
| 438 | bc->m_long_term_block_weights_cache_rolling_median.clear(); |
| 439 | bc->get_long_term_block_weight_median(bc->get_db().height() - TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW, TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW); |
| 440 | bc->update_next_cumulative_weight_limit(); |
| 441 | |
| 442 | // make sure the weight limit is the same |
| 443 | ASSERT_EQ(weight_limit, bc->get_current_cumulative_block_weight_limit()); |
| 444 | } |