Debian Codegraph @

src/cryptonote_basic/hardfork.h

raw symbols SCIP
1 // Copyright (c) 2014-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 #pragma once
30
31 #include "syncobj.h"
32 #include "hardforks/hardforks.h"
33 #include "cryptonote_basic/cryptonote_basic.h"
34
35 namespace cryptonote
36 {
37 class BlockchainDB;
38
39 class HardFork
40 {
41 public:
42 typedef enum {
43 LikelyForked,
44 UpdateNeeded,
45 Ready,
46 } State;
47
48 static const uint64_t DEFAULT_ORIGINAL_VERSION_TILL_HEIGHT = 0; // <= actual height
49 static const time_t DEFAULT_FORKED_TIME = 31557600; // a year in seconds
50 static const time_t DEFAULT_UPDATE_TIME = 31557600 / 2;
51 static const uint64_t DEFAULT_WINDOW_SIZE = 10080; // supermajority window check length - a week
52 static const uint8_t DEFAULT_THRESHOLD_PERCENT = 80;
53
54 /**
55 * @brief creates a new HardFork object
56 *
57 * @param original_version the block version for blocks 0 through to the first fork
58 * @param forked_time the time in seconds before thinking we're forked
59 * @param update_time the time in seconds before thinking we need to update
60 * @param window_size the size of the window in blocks to consider for version voting
61 * @param default_threshold_percent the size of the majority in percents
62 */
63 HardFork(cryptonote::BlockchainDB &db, uint8_t original_version = 1, uint64_t original_version_till_height = DEFAULT_ORIGINAL_VERSION_TILL_HEIGHT, time_t forked_time = DEFAULT_FORKED_TIME, time_t update_time = DEFAULT_UPDATE_TIME, uint64_t window_size = DEFAULT_WINDOW_SIZE, uint8_t default_threshold_percent = DEFAULT_THRESHOLD_PERCENT);
64
65 /**
66 * @brief add a new hardfork height
67 *
68 * returns true if no error, false otherwise
69 *
70 * @param version the major block version for the fork
71 * @param height The height the hardfork takes effect
72 * @param threshold The threshold of votes needed for this fork (0-100)
73 * @param time Approximate time of the hardfork (seconds since epoch)
74 */
75 bool add_fork(uint8_t version, uint64_t height, uint8_t threshold, time_t time);
76
77 /**
78 * @brief add a new hardfork height
79 *
80 * returns true if no error, false otherwise
81 *
82 * @param version the major block version for the fork
83 * @param height The height the hardfork takes effect
84 * @param time Approximate time of the hardfork (seconds since epoch)
85 */
86 bool add_fork(uint8_t version, uint64_t height, time_t time);
87
88 /**
89 * @brief initialize the object
90 *
91 * Must be done after adding all the required hardforks via add above
92 */
93 void init();
94
95 /**
96 * @brief check whether a new block would be accepted
97 *
98 * returns true if the block is accepted, false otherwise
99 *
100 * @param block the new block
101 *
102 * This check is made by add. It is exposed publicly to allow
103 * the caller to inexpensively check whether a block would be
104 * accepted or rejected by its version number. Indeed, if this
105 * check could only be done as part of add, the caller would
106 * either have to add the block to the blockchain first, then
107 * call add, then have to pop the block from the blockchain if
108 * its version did not satisfy the hard fork requirements, or
109 * call add first, then, if the hard fork requirements are met,
110 * add the block to the blockchain, upon which a failure (the
111 * block being invalid, double spending, etc) would cause the
112 * hardfork object to reorganize.
113 */
114 bool check(const cryptonote::block &block) const;
115
116 /**
117 * @brief same as check, but for a particular height, rather than the top
118 *
119 * NOTE: this does not play well with voting, and relies on voting to be
120 * disabled (that is, forks happen on the scheduled date, whether or not
121 * enough blocks have voted for the fork).
122 *
123 * returns true if no error, false otherwise
124 *
125 * @param block the new block
126 * @param height which height to check for
127 */
128 bool check_for_height(const cryptonote::block &block, uint64_t height) const;
129
130 /**
131 * @brief add a new block
132 *
133 * returns true if no error, false otherwise
134 *
135 * @param block the new block
136 */
137 bool add(const cryptonote::block &block, uint64_t height);
138
139 /**
140 * @brief called when the blockchain is reorganized
141 *
142 * This will rescan the blockchain to determine which hard forks
143 * have been triggered
144 *
145 * returns true if no error, false otherwise
146 *
147 * @param blockchain the blockchain
148 * @param height of the last block kept from the previous blockchain
149 */
150 bool reorganize_from_block_height(uint64_t height);
151 bool reorganize_from_chain_height(uint64_t height);
152
153 /**
154 * @brief called when one or more blocks are popped from the blockchain
155 *
156 * The current fork will be updated by looking up the db,
157 * which is much cheaper than recomputing everything
158 *
159 * @param new_chain_height the height of the chain after popping
160 */
161 void on_block_popped(uint64_t new_chain_height);
162
163 /**
164 * @brief returns current state at the given time
165 *
166 * Based on the approximate time of the last known hard fork,
167 * estimate whether we need to update, or if we're way behind
168 *
169 * @param t the time to consider
170 */
171 State get_state(time_t t) const;
172 State get_state() const;
173
174 /**
175 * @brief returns the hard fork version for the given block height
176 *
177 * @param height height of the block to check
178 */
179 uint8_t get(uint64_t height) const;
180
181 /**
182 * @brief returns the latest "ideal" version
183 *
184 * This is the latest version that's been scheduled
185 */
186 uint8_t get_ideal_version() const;
187
188 /**
189 * @brief returns the "ideal" version for a given height
190 *
191 * @param height height of the block to check
192 */
193 uint8_t get_ideal_version(uint64_t height) const;
194
195 /**
196 * @brief returns the next version
197 *
198 * This is the version which will we fork to next
199 */
200 uint8_t get_next_version() const;
201
202 /**
203 * @brief returns the current version
204 *
205 * This is the latest version that's past its trigger date and had enough votes
206 * at one point in the past.
207 */
208 uint8_t get_current_version() const;
209
210 /**
211 * @brief returns the earliest block a given version may activate
212 */
213 uint64_t get_earliest_ideal_height_for_version(uint8_t version) const;
214
215 /**
216 * @brief returns information about current voting state
217 *
218 * returns true if the given version is enabled (ie, the current version
219 * is at least the passed version), false otherwise
220 *
221 * @param version the version to check voting for
222 * @param window the number of blocks considered in voting
223 * @param votes number of votes for next version
224 * @param threshold number of votes needed to switch to next version
225 * @param earliest_height earliest height at which the version can take effect
226 */
227 bool get_voting_info(uint8_t version, uint32_t &window, uint32_t &votes, uint32_t &threshold, uint64_t &earliest_height, uint8_t &voting) const;
228
229 /**
230 * @brief returns the size of the voting window in blocks
231 */
232 uint64_t get_window_size() const { return window_size; }
233
234 /**
235 * @brief returns info for all known hard forks
236 */
237 const std::vector<hardfork_t>& get_hardforks() const { return heights; }
238
239 private:
240
241 uint8_t get_block_version(uint64_t height) const;
242 bool do_check(uint8_t block_version, uint8_t voting_version) const;
243 bool do_check_for_height(uint8_t block_version, uint8_t voting_version, uint64_t height) const;
244 int get_voted_fork_index(uint64_t height) const;
245 uint8_t get_effective_version(uint8_t voting_version) const;
246 bool add(uint8_t block_version, uint8_t voting_version, uint64_t height);
247
248 bool rescan_from_block_height(uint64_t height);
249 bool rescan_from_chain_height(uint64_t height);
250
251 private:
252
253 BlockchainDB &db;
254
255 time_t forked_time;
256 time_t update_time;
257 uint64_t window_size;
258 uint8_t default_threshold_percent;
259
260 uint8_t original_version;
261 uint64_t original_version_till_height;
262
263 std::vector<hardfork_t> heights;
264
265 std::deque<uint8_t> versions; /* rolling window of the last N blocks' versions */
266 unsigned int last_versions[256]; /* count of the block versions in the last N blocks */
267 uint32_t current_fork_index;
268
269 mutable epee::critical_section lock;
270 };
271
272 } // namespace cryptonote
273