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 "cryptonote_config.h"
30 #include "misc_log_ex.h"
31 #include "crypto/crypto.h"
32 #include "pruning.h"
33 
34 namespace tools
35 {
36 
38 {
39  CHECK_AND_ASSERT_THROW_MES(log_stripes <= PRUNING_SEED_LOG_STRIPES_MASK, "log_stripes out of range");
40  CHECK_AND_ASSERT_THROW_MES(stripe > 0 && stripe <= (1ul << log_stripes), "stripe out of range");
41  return (log_stripes << PRUNING_SEED_LOG_STRIPES_SHIFT) | ((stripe - 1) << PRUNING_SEED_STRIPE_SHIFT);
42 }
43 
44 bool has_unpruned_block(uint64_t block_height, uint64_t blockchain_height, uint32_t pruning_seed)
45 {
46  const uint32_t stripe = get_pruning_stripe(pruning_seed);
47  if (stripe == 0)
48  return true;
49  const uint32_t log_stripes = get_pruning_log_stripes(pruning_seed);
50  uint32_t block_stripe = get_pruning_stripe(block_height, blockchain_height, log_stripes);
51  return block_stripe == 0 || block_stripe == stripe;
52 }
53 
54 uint32_t get_pruning_stripe(uint64_t block_height, uint64_t blockchain_height, uint32_t log_stripes)
55 {
56  if (block_height + CRYPTONOTE_PRUNING_TIP_BLOCKS >= blockchain_height)
57  return 0;
58  return ((block_height / CRYPTONOTE_PRUNING_STRIPE_SIZE) & (uint64_t)((1ul << log_stripes) - 1)) + 1;
59 }
60 
61 uint32_t get_pruning_seed(uint64_t block_height, uint64_t blockchain_height, uint32_t log_stripes)
62 {
63  const uint32_t stripe = get_pruning_stripe(block_height, blockchain_height, log_stripes);
64  if (stripe == 0)
65  return 0;
66  return make_pruning_seed(stripe, log_stripes);
67 }
68 
69 uint64_t get_next_unpruned_block_height(uint64_t block_height, uint64_t blockchain_height, uint32_t pruning_seed)
70 {
71  CHECK_AND_ASSERT_MES(block_height <= CRYPTONOTE_MAX_BLOCK_NUMBER+1, block_height, "block_height too large");
72  CHECK_AND_ASSERT_MES(blockchain_height <= CRYPTONOTE_MAX_BLOCK_NUMBER+1, block_height, "blockchain_height too large");
73  const uint32_t stripe = get_pruning_stripe(pruning_seed);
74  if (stripe == 0)
75  return block_height;
76  if (block_height + CRYPTONOTE_PRUNING_TIP_BLOCKS >= blockchain_height)
77  return block_height;
78  const uint32_t seed_log_stripes = get_pruning_log_stripes(pruning_seed);
79  const uint64_t log_stripes = seed_log_stripes ? seed_log_stripes : CRYPTONOTE_PRUNING_LOG_STRIPES;
80  const uint64_t mask = (1ul << log_stripes) - 1;
81  const uint32_t block_pruning_stripe = ((block_height / CRYPTONOTE_PRUNING_STRIPE_SIZE) & mask) + 1;
82  if (block_pruning_stripe == stripe)
83  return block_height;
84  const uint64_t cycles = ((block_height / CRYPTONOTE_PRUNING_STRIPE_SIZE) >> log_stripes);
85  const uint64_t cycle_start = cycles + ((stripe > block_pruning_stripe) ? 0 : 1);
86  const uint64_t h = cycle_start * (CRYPTONOTE_PRUNING_STRIPE_SIZE << log_stripes) + (stripe - 1) * CRYPTONOTE_PRUNING_STRIPE_SIZE;
87  if (h + CRYPTONOTE_PRUNING_TIP_BLOCKS > blockchain_height)
88  return blockchain_height < CRYPTONOTE_PRUNING_TIP_BLOCKS ? 0 : blockchain_height - CRYPTONOTE_PRUNING_TIP_BLOCKS;
89  CHECK_AND_ASSERT_MES(h >= block_height, block_height, "h < block_height, unexpected");
90  return h;
91 }
92 
93 uint64_t get_next_pruned_block_height(uint64_t block_height, uint64_t blockchain_height, uint32_t pruning_seed)
94 {
95  const uint32_t stripe = get_pruning_stripe(pruning_seed);
96  if (stripe == 0)
97  return blockchain_height;
98  if (block_height + CRYPTONOTE_PRUNING_TIP_BLOCKS >= blockchain_height)
99  return blockchain_height;
100  const uint32_t seed_log_stripes = get_pruning_log_stripes(pruning_seed);
101  const uint64_t log_stripes = seed_log_stripes ? seed_log_stripes : CRYPTONOTE_PRUNING_LOG_STRIPES;
102  const uint64_t mask = (1ul << log_stripes) - 1;
103  const uint32_t block_pruning_seed = ((block_height / CRYPTONOTE_PRUNING_STRIPE_SIZE) & mask) + 1;
104  if (block_pruning_seed != stripe)
105  return block_height;
106  const uint32_t next_stripe = 1 + (block_pruning_seed & mask);
107  return get_next_unpruned_block_height(block_height, blockchain_height, tools::make_pruning_seed(next_stripe, log_stripes));
108 }
109 
111 {
112  return 1 + crypto::rand<uint8_t>() % (1ul << CRYPTONOTE_PRUNING_LOG_STRIPES);
113 }
114 
115 }
116 
#define CHECK_AND_ASSERT_THROW_MES(expr, message)
Definition: misc_log_ex.h:173
uint32_t get_pruning_seed(uint64_t block_height, uint64_t blockchain_height, uint32_t log_stripes)
Definition: pruning.cpp:61
uint32_t get_random_stripe()
Definition: pruning.cpp:110
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
Definition: misc_log_ex.h:181
#define CRYPTONOTE_MAX_BLOCK_NUMBER
#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 CRYPTONOTE_PRUNING_STRIPE_SIZE
unsigned int uint32_t
Definition: stdint.h:126
Various Tools.
Definition: tools.cpp:31
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
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