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