Debian Codegraph @

src/common/pruning.cpp

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