| 1 | // Copyright (c) 2018-2022, The Monero Project |
| 2 | |
| 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 "gtest/gtest.h" |
| 31 | |
| 32 | #include "misc_log_ex.h" |
| 33 | #include "cryptonote_config.h" |
| 34 | #include "common/pruning.h" |
| 35 | |
| 36 | #define ASSERT_EX(x) do { bool ex = false; try { x; } catch(...) { ex = true; } ASSERT_TRUE(ex); } while(0) |
| 37 | |
| 38 | TEST(pruning, parts) |
| 39 | { |
| 40 | ASSERT_EQ(tools::get_pruning_stripe(tools::make_pruning_seed(3, 2)), 3); |
| 41 | ASSERT_EQ(tools::get_pruning_stripe(tools::make_pruning_seed(1, 2)), 1); |
| 42 | ASSERT_EQ(tools::get_pruning_stripe(tools::make_pruning_seed(7, 7)), 7); |
| 43 | |
| 44 | ASSERT_EQ(tools::get_pruning_log_stripes(tools::make_pruning_seed(1, 2)), 2); |
| 45 | ASSERT_EQ(tools::get_pruning_log_stripes(tools::make_pruning_seed(1, 0)), 0); |
| 46 | ASSERT_EQ(tools::get_pruning_log_stripes(tools::make_pruning_seed(1, 7)), 7); |
| 47 | ASSERT_EQ(tools::get_pruning_log_stripes(tools::make_pruning_seed(7, 7)), 7); |
| 48 | |
| 49 | for (uint32_t log_stripes = 1; log_stripes <= tools::PRUNING_SEED_LOG_STRIPES_MASK; ++log_stripes) |
| 50 | { |
| 51 | for (uint32_t stripe = 1; stripe <= (1 << log_stripes); ++stripe) |
| 52 | { |
| 53 | ASSERT_EQ(tools::get_pruning_log_stripes(tools::make_pruning_seed(stripe, log_stripes)), log_stripes); |
| 54 | ASSERT_EQ(tools::get_pruning_stripe(tools::make_pruning_seed(stripe, log_stripes)), stripe); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | TEST(pruning, out_of_range) |
| 60 | { |
| 61 | ASSERT_EX(tools::make_pruning_seed(5, 2)); |
| 62 | ASSERT_EX(tools::make_pruning_seed(0, 2)); |
| 63 | } |
| 64 | |
| 65 | TEST(pruning, fits) |
| 66 | { |
| 67 | const uint32_t log_stripes = 3; |
| 68 | const uint32_t num_stripes = 1 << log_stripes; |
| 69 | for (uint32_t stripe = 1; stripe <= num_stripes; ++stripe) |
| 70 | { |
| 71 | const uint32_t seed = tools::make_pruning_seed(stripe, log_stripes); |
| 72 | ASSERT_NE(seed, 0); |
| 73 | ASSERT_EQ(tools::get_pruning_log_stripes(seed), log_stripes); |
| 74 | ASSERT_EQ(tools::get_pruning_stripe(seed), stripe); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | TEST(pruning, tip) |
| 79 | { |
| 80 | static constexpr uint64_t H = CRYPTONOTE_PRUNING_TIP_BLOCKS + 1000; |
| 81 | static_assert(H >= CRYPTONOTE_PRUNING_TIP_BLOCKS, "H must be >= CRYPTONOTE_PRUNING_TIP_BLOCKS"); |
| 82 | for (uint64_t h = H - CRYPTONOTE_PRUNING_TIP_BLOCKS; h < H; ++h) |
| 83 | { |
| 84 | uint32_t pruning_seed = tools::get_pruning_seed(h, H, CRYPTONOTE_PRUNING_LOG_STRIPES); |
| 85 | ASSERT_EQ(pruning_seed, 0); |
| 86 | for (pruning_seed = 0; pruning_seed <= (1 << CRYPTONOTE_PRUNING_LOG_STRIPES); ++pruning_seed) |
| 87 | ASSERT_TRUE(tools::has_unpruned_block(h, H, pruning_seed)); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | TEST(pruning, seed) |
| 92 | { |
| 93 | const uint64_t SS = CRYPTONOTE_PRUNING_STRIPE_SIZE; |
| 94 | const uint64_t NS = 1 << CRYPTONOTE_PRUNING_LOG_STRIPES; |
| 95 | const uint64_t TB = NS * SS; |
| 96 | |
| 97 | for (uint64_t cycle = 0; cycle < 10; ++cycle) |
| 98 | { |
| 99 | const uint64_t O = TB * cycle; |
| 100 | ASSERT_EQ(tools::get_pruning_stripe(O + 0, 10000000, CRYPTONOTE_PRUNING_LOG_STRIPES), 1); |
| 101 | ASSERT_EQ(tools::get_pruning_stripe(O + 1, 10000000, CRYPTONOTE_PRUNING_LOG_STRIPES), 1); |
| 102 | ASSERT_EQ(tools::get_pruning_stripe(O + SS-1, 10000000, CRYPTONOTE_PRUNING_LOG_STRIPES), 1); |
| 103 | ASSERT_EQ(tools::get_pruning_stripe(O + SS, 10000000, CRYPTONOTE_PRUNING_LOG_STRIPES), 2); |
| 104 | ASSERT_EQ(tools::get_pruning_stripe(O + SS*2-1, 10000000, CRYPTONOTE_PRUNING_LOG_STRIPES), 2); |
| 105 | ASSERT_EQ(tools::get_pruning_stripe(O + SS*2, 10000000, CRYPTONOTE_PRUNING_LOG_STRIPES), 3); |
| 106 | ASSERT_EQ(tools::get_pruning_stripe(O + SS*NS-1, 10000000, CRYPTONOTE_PRUNING_LOG_STRIPES), NS); |
| 107 | ASSERT_EQ(tools::get_pruning_stripe(O + SS*NS, 10000000, CRYPTONOTE_PRUNING_LOG_STRIPES), 1); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | TEST(pruning, match) |
| 112 | { |
| 113 | static constexpr uint64_t H = CRYPTONOTE_PRUNING_TIP_BLOCKS + 1000; |
| 114 | static_assert(H >= CRYPTONOTE_PRUNING_TIP_BLOCKS, "H must be >= CRYPTONOTE_PRUNING_TIP_BLOCKS"); |
| 115 | for (uint64_t h = 0; h < H - CRYPTONOTE_PRUNING_TIP_BLOCKS; ++h) |
| 116 | { |
| 117 | uint32_t pruning_seed = tools::get_pruning_seed(h, H, CRYPTONOTE_PRUNING_LOG_STRIPES); |
| 118 | uint32_t pruning_stripe = tools::get_pruning_stripe(pruning_seed); |
| 119 | ASSERT_TRUE(pruning_stripe > 0 && pruning_stripe <= (1 << CRYPTONOTE_PRUNING_LOG_STRIPES)); |
| 120 | for (uint32_t other_pruning_stripe = 1; other_pruning_stripe <= (1 << CRYPTONOTE_PRUNING_LOG_STRIPES); ++other_pruning_stripe) |
| 121 | { |
| 122 | uint32_t other_pruning_seed = tools::make_pruning_seed(other_pruning_stripe, CRYPTONOTE_PRUNING_LOG_STRIPES); |
| 123 | ASSERT_TRUE(tools::has_unpruned_block(h, H, other_pruning_seed) == (other_pruning_seed == pruning_seed)); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | TEST(pruning, stripe_size) |
| 129 | { |
| 130 | static constexpr uint64_t H = CRYPTONOTE_PRUNING_TIP_BLOCKS + CRYPTONOTE_PRUNING_STRIPE_SIZE * (1 << CRYPTONOTE_PRUNING_LOG_STRIPES) + 1000; |
| 131 | static_assert(H >= CRYPTONOTE_PRUNING_TIP_BLOCKS + CRYPTONOTE_PRUNING_STRIPE_SIZE * (1 << CRYPTONOTE_PRUNING_LOG_STRIPES), "H must be >= that stuff in front"); |
| 132 | for (uint32_t pruning_stripe = 1; pruning_stripe <= (1 << CRYPTONOTE_PRUNING_LOG_STRIPES); ++pruning_stripe) |
| 133 | { |
| 134 | uint32_t pruning_seed = tools::make_pruning_seed(pruning_stripe, CRYPTONOTE_PRUNING_LOG_STRIPES); |
| 135 | unsigned int current_run = 0, best_run = 0; |
| 136 | for (uint64_t h = 0; h < H - CRYPTONOTE_PRUNING_TIP_BLOCKS; ++h) |
| 137 | { |
| 138 | if (tools::has_unpruned_block(h, H, pruning_seed)) |
| 139 | { |
| 140 | ++current_run; |
| 141 | } |
| 142 | else if (current_run) |
| 143 | { |
| 144 | ASSERT_EQ(current_run, CRYPTONOTE_PRUNING_STRIPE_SIZE); |
| 145 | best_run = std::max(best_run, current_run); |
| 146 | current_run = 0; |
| 147 | } |
| 148 | } |
| 149 | ASSERT_EQ(best_run, CRYPTONOTE_PRUNING_STRIPE_SIZE); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | TEST(pruning, next_unpruned) |
| 154 | { |
| 155 | static_assert((1 << CRYPTONOTE_PRUNING_LOG_STRIPES) >= 4, "CRYPTONOTE_PRUNING_LOG_STRIPES too low"); |
| 156 | |
| 157 | const uint64_t SS = CRYPTONOTE_PRUNING_STRIPE_SIZE; |
| 158 | const uint64_t NS = 1 << CRYPTONOTE_PRUNING_LOG_STRIPES; |
| 159 | const uint64_t TB = NS * SS; |
| 160 | |
| 161 | for (uint64_t h = 0; h < 100; ++h) |
| 162 | ASSERT_EQ(tools::get_next_unpruned_block_height(h, 10000000, 0), h); |
| 163 | |
| 164 | const uint32_t seed1 = tools::make_pruning_seed(1, CRYPTONOTE_PRUNING_LOG_STRIPES); |
| 165 | const uint32_t seed2 = tools::make_pruning_seed(2, CRYPTONOTE_PRUNING_LOG_STRIPES); |
| 166 | const uint32_t seed3 = tools::make_pruning_seed(3, CRYPTONOTE_PRUNING_LOG_STRIPES); |
| 167 | const uint32_t seed4 = tools::make_pruning_seed(4, CRYPTONOTE_PRUNING_LOG_STRIPES); |
| 168 | const uint32_t seedNS = tools::make_pruning_seed(NS, CRYPTONOTE_PRUNING_LOG_STRIPES); |
| 169 | |
| 170 | ASSERT_EQ(tools::get_next_unpruned_block_height(0, 10000000, seed1), 0); |
| 171 | ASSERT_EQ(tools::get_next_unpruned_block_height(1, 10000000, seed1), 1); |
| 172 | ASSERT_EQ(tools::get_next_unpruned_block_height(SS-1, 10000000, seed1), SS-1); |
| 173 | ASSERT_EQ(tools::get_next_unpruned_block_height(SS, 10000000, seed1), TB); |
| 174 | ASSERT_EQ(tools::get_next_unpruned_block_height(TB, 10000000, seed1), TB); |
| 175 | ASSERT_EQ(tools::get_next_unpruned_block_height(TB-1, 10000000, seed1), TB); |
| 176 | |
| 177 | ASSERT_EQ(tools::get_next_unpruned_block_height(0, 10000000, seed2), SS); |
| 178 | ASSERT_EQ(tools::get_next_unpruned_block_height(1, 10000000, seed2), SS); |
| 179 | ASSERT_EQ(tools::get_next_unpruned_block_height(SS-1, 10000000, seed2), SS); |
| 180 | ASSERT_EQ(tools::get_next_unpruned_block_height(SS, 10000000, seed2), SS); |
| 181 | ASSERT_EQ(tools::get_next_unpruned_block_height(2*SS-1, 10000000, seed2), 2*SS-1); |
| 182 | ASSERT_EQ(tools::get_next_unpruned_block_height(2*SS, 10000000, seed2), TB+SS); |
| 183 | ASSERT_EQ(tools::get_next_unpruned_block_height(TB+2*SS,10000000, seed2), TB*2+SS); |
| 184 | |
| 185 | ASSERT_EQ(tools::get_next_unpruned_block_height(0, 10000000, seed3), SS*2); |
| 186 | ASSERT_EQ(tools::get_next_unpruned_block_height(SS, 10000000, seed3), SS*2); |
| 187 | ASSERT_EQ(tools::get_next_unpruned_block_height(2*SS, 10000000, seed3), SS*2); |
| 188 | ASSERT_EQ(tools::get_next_unpruned_block_height(3*SS-1, 10000000, seed3), SS*3-1); |
| 189 | ASSERT_EQ(tools::get_next_unpruned_block_height(3*SS, 10000000, seed3), TB+SS*2); |
| 190 | ASSERT_EQ(tools::get_next_unpruned_block_height(TB+3*SS,10000000, seed3), TB*2+SS*2); |
| 191 | |
| 192 | ASSERT_EQ(tools::get_next_unpruned_block_height(SS, 10000000, seed4), 3*SS); |
| 193 | ASSERT_EQ(tools::get_next_unpruned_block_height(4*SS-1, 10000000, seed4), 4*SS-1); |
| 194 | ASSERT_EQ(tools::get_next_unpruned_block_height(4*SS, 10000000, seed4), TB+3*SS); |
| 195 | ASSERT_EQ(tools::get_next_unpruned_block_height(TB+4*SS,10000000, seed4), TB*2+3*SS); |
| 196 | |
| 197 | ASSERT_EQ(tools::get_next_unpruned_block_height(SS, 10000000, seedNS), (NS-1)*SS); |
| 198 | ASSERT_EQ(tools::get_next_unpruned_block_height(NS*SS-1,10000000, seedNS), NS*SS-1); |
| 199 | ASSERT_EQ(tools::get_next_unpruned_block_height(NS*SS, 10000000, seedNS), TB+(NS-1)*SS); |
| 200 | ASSERT_EQ(tools::get_next_unpruned_block_height(TB+NS*SS, 10000000, seedNS), TB*2+(NS-1)*SS); |
| 201 | } |
| 202 | |
| 203 | TEST(pruning, next_pruned) |
| 204 | { |
| 205 | static_assert((1 << CRYPTONOTE_PRUNING_LOG_STRIPES) >= 4, "CRYPTONOTE_PRUNING_LOG_STRIPES too low"); |
| 206 | |
| 207 | const uint64_t SS = CRYPTONOTE_PRUNING_STRIPE_SIZE; |
| 208 | const uint64_t NS = 1 << CRYPTONOTE_PRUNING_LOG_STRIPES; |
| 209 | const uint64_t TB = NS * SS; |
| 210 | |
| 211 | const uint32_t seed1 = tools::make_pruning_seed(1, CRYPTONOTE_PRUNING_LOG_STRIPES); |
| 212 | const uint32_t seed2 = tools::make_pruning_seed(2, CRYPTONOTE_PRUNING_LOG_STRIPES); |
| 213 | const uint32_t seedNS_1 = tools::make_pruning_seed(NS-1, CRYPTONOTE_PRUNING_LOG_STRIPES); |
| 214 | const uint32_t seedNS = tools::make_pruning_seed(NS, CRYPTONOTE_PRUNING_LOG_STRIPES); |
| 215 | |
| 216 | for (uint64_t h = 0; h < 100; ++h) |
| 217 | ASSERT_EQ(tools::get_next_pruned_block_height(h, 10000000, 0), 10000000); |
| 218 | for (uint64_t h = 10000000 - 1 - CRYPTONOTE_PRUNING_TIP_BLOCKS; h < 10000000; ++h) |
| 219 | ASSERT_EQ(tools::get_next_pruned_block_height(h, 10000000, 0), 10000000); |
| 220 | |
| 221 | ASSERT_EQ(tools::get_next_pruned_block_height(1, 10000000, seed1), SS); |
| 222 | ASSERT_EQ(tools::get_next_pruned_block_height(SS-1, 10000000, seed1), SS); |
| 223 | ASSERT_EQ(tools::get_next_pruned_block_height(SS, 10000000, seed1), SS); |
| 224 | ASSERT_EQ(tools::get_next_pruned_block_height(TB-1, 10000000, seed1), TB-1); |
| 225 | ASSERT_EQ(tools::get_next_pruned_block_height(TB, 10000000, seed1), TB+SS); |
| 226 | |
| 227 | ASSERT_EQ(tools::get_next_pruned_block_height(1, 10000000, seed2), 1); |
| 228 | ASSERT_EQ(tools::get_next_pruned_block_height(SS-1, 10000000, seed2), SS-1); |
| 229 | ASSERT_EQ(tools::get_next_pruned_block_height(SS, 10000000, seed2), 2*SS); |
| 230 | ASSERT_EQ(tools::get_next_pruned_block_height(TB-1, 10000000, seed2), TB-1); |
| 231 | |
| 232 | ASSERT_EQ(tools::get_next_pruned_block_height(1, 10000000, seedNS_1), 1); |
| 233 | ASSERT_EQ(tools::get_next_pruned_block_height(SS-1, 10000000, seedNS_1), SS-1); |
| 234 | ASSERT_EQ(tools::get_next_pruned_block_height(SS, 10000000, seedNS_1), SS); |
| 235 | ASSERT_EQ(tools::get_next_pruned_block_height(TB-1, 10000000, seedNS_1), TB-1); |
| 236 | |
| 237 | ASSERT_EQ(tools::get_next_pruned_block_height(1, 10000000, seedNS), 1); |
| 238 | ASSERT_EQ(tools::get_next_pruned_block_height(SS-1, 10000000, seedNS), SS-1); |
| 239 | ASSERT_EQ(tools::get_next_pruned_block_height(SS, 10000000, seedNS), SS); |
| 240 | ASSERT_EQ(tools::get_next_pruned_block_height(TB-1, 10000000, seedNS), TB); |
| 241 | } |