Monero
hardfork.h
Go to the documentation of this file.
1 // Copyright (c) 2014-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 #pragma once
30 
31 #include "syncobj.h"
33 
34 namespace cryptonote
35 {
36  class BlockchainDB;
37 
38  class HardFork
39  {
40  public:
41  typedef enum {
45  } State;
46 
47  static const uint64_t DEFAULT_ORIGINAL_VERSION_TILL_HEIGHT = 0; // <= actual height
48  static const time_t DEFAULT_FORKED_TIME = 31557600; // a year in seconds
49  static const time_t DEFAULT_UPDATE_TIME = 31557600 / 2;
50  static const uint64_t DEFAULT_WINDOW_SIZE = 10080; // supermajority window check length - a week
51  static const uint8_t DEFAULT_THRESHOLD_PERCENT = 80;
52 
63 
74  bool add_fork(uint8_t version, uint64_t height, uint8_t threshold, time_t time);
75 
85  bool add_fork(uint8_t version, uint64_t height, time_t time);
86 
92  void init();
93 
113  bool check(const cryptonote::block &block) const;
114 
127  bool check_for_height(const cryptonote::block &block, uint64_t height) const;
128 
136  bool add(const cryptonote::block &block, uint64_t height);
137 
149  bool reorganize_from_block_height(uint64_t height);
150  bool reorganize_from_chain_height(uint64_t height);
151 
160  State get_state(time_t t) const;
161  State get_state() const;
162 
168  uint8_t get(uint64_t height) const;
169 
175  uint8_t get_ideal_version() const;
176 
182  uint8_t get_ideal_version(uint64_t height) const;
183 
189  uint8_t get_next_version() const;
190 
197  uint8_t get_current_version() const;
198 
202  uint64_t get_earliest_ideal_height_for_version(uint8_t version) const;
203 
216  bool get_voting_info(uint8_t version, uint32_t &window, uint32_t &votes, uint32_t &threshold, uint64_t &earliest_height, uint8_t &voting) const;
217 
221  uint64_t get_window_size() const { return window_size; }
222 
223  struct Params {
224  uint8_t version;
225  uint8_t threshold;
226  uint64_t height;
227  time_t time;
228  Params(uint8_t version, uint64_t height, uint8_t threshold, time_t time): version(version), threshold(threshold), height(height), time(time) {}
229  };
230 
231  private:
232 
233  uint8_t get_block_version(uint64_t height) const;
234  bool do_check(uint8_t block_version, uint8_t voting_version) const;
235  bool do_check_for_height(uint8_t block_version, uint8_t voting_version, uint64_t height) const;
236  int get_voted_fork_index(uint64_t height) const;
237  uint8_t get_effective_version(uint8_t voting_version) const;
238  bool add(uint8_t block_version, uint8_t voting_version, uint64_t height);
239 
240  bool rescan_from_block_height(uint64_t height);
241  bool rescan_from_chain_height(uint64_t height);
242 
243  private:
244 
246 
247  time_t forked_time;
248  time_t update_time;
249  uint64_t window_size;
251 
254 
255  std::vector<Params> heights;
256 
257  std::deque<uint8_t> versions; /* rolling window of the last N blocks' versions */
258  unsigned int last_versions[256]; /* count of the block versions in the last N blocks */
260 
261  mutable epee::critical_section lock;
262  };
263 
264 } // namespace cryptonote
265 
Params(uint8_t version, uint64_t height, uint8_t threshold, time_t time)
Definition: hardfork.h:228
int get_voted_fork_index(uint64_t height) const
Definition: hardfork.cpp:308
uint32_t current_fork_index
Definition: hardfork.h:259
Definition: cryptonote_basic.h:366
bool add(const cryptonote::block &block, uint64_t height)
add a new block
Definition: hardfork.cpp:162
State get_state() const
Definition: hardfork.cpp:339
uint64_t height
Definition: blockchain.cpp:88
static const uint64_t DEFAULT_ORIGINAL_VERSION_TILL_HEIGHT
Definition: hardfork.h:47
static const uint64_t DEFAULT_WINDOW_SIZE
Definition: hardfork.h:50
uint64_t get_window_size() const
returns the size of the voting window in blocks
Definition: hardfork.h:221
Definition: hardfork.h:38
uint64_t get_earliest_ideal_height_for_version(uint8_t version) const
returns the earliest block a given version may activate
Definition: hardfork.cpp:380
epee::critical_section lock
Definition: hardfork.h:261
uint8_t get_effective_version(uint8_t voting_version) const
Definition: hardfork.cpp:98
time_t time
Definition: hardfork.h:227
void init()
initialize the object
Definition: hardfork.cpp:167
BlockchainDB & db
Definition: hardfork.h:245
bool rescan_from_block_height(uint64_t height)
Definition: hardfork.cpp:266
Holds cryptonote related classes and helpers.
Definition: db_bdb.cpp:224
bool reorganize_from_chain_height(uint64_t height)
Definition: hardfork.cpp:259
uint8_t get_next_version() const
returns the next version
Definition: hardfork.cpp:393
time_t time
Definition: blockchain.cpp:90
bool get_voting_info(uint8_t version, uint32_t &window, uint32_t &votes, uint32_t &threshold, uint64_t &earliest_height, uint8_t &voting) const
returns information about current voting state
Definition: hardfork.cpp:405
Definition: hardfork.h:43
std::vector< Params > heights
Definition: hardfork.h:255
time_t forked_time
Definition: hardfork.h:247
bool rescan_from_chain_height(uint64_t height)
Definition: hardfork.cpp:301
uint64_t original_version_till_height
Definition: hardfork.h:253
bool check(const cryptonote::block &block) const
check whether a new block would be accepted
Definition: hardfork.cpp:114
bool reorganize_from_block_height(uint64_t height)
called when the blockchain is reorganized
Definition: hardfork.cpp:218
uint8_t get_block_version(uint64_t height) const
Definition: hardfork.cpp:209
uint64_t height
Definition: hardfork.h:226
Definition: hardfork.h:42
Definition: hardfork.h:44
uint8_t version
Definition: blockchain.cpp:87
bool add_fork(uint8_t version, uint64_t height, uint8_t threshold, time_t time)
add a new hardfork height
Definition: hardfork.cpp:72
bool do_check_for_height(uint8_t block_version, uint8_t voting_version, uint64_t height) const
Definition: hardfork.cpp:120
bool do_check(uint8_t block_version, uint8_t voting_version) const
Definition: hardfork.cpp:108
uint64_t window_size
Definition: hardfork.h:249
The BlockchainDB backing store interface declaration/contract.
Definition: blockchain_db.h:343
static const uint8_t DEFAULT_THRESHOLD_PERCENT
Definition: hardfork.h:51
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)
creates a new HardFork object
Definition: hardfork.cpp:57
State
Definition: hardfork.h:41
unsigned int last_versions[256]
Definition: hardfork.h:258
static const time_t DEFAULT_UPDATE_TIME
Definition: hardfork.h:49
time_t update_time
Definition: hardfork.h:248
uint8_t threshold
Definition: hardfork.h:225
uint8_t get_current_version() const
returns the current version
Definition: hardfork.cpp:357
uint8_t original_version
Definition: hardfork.h:252
uint8_t get_ideal_version() const
returns the latest "ideal" version
Definition: hardfork.cpp:363
Definition: hardfork.h:223
uint8_t version
Definition: hardfork.h:224
std::deque< uint8_t > versions
Definition: hardfork.h:257
uint8_t default_threshold_percent
Definition: hardfork.h:250
bool check_for_height(const cryptonote::block &block, uint64_t height) const
same as check, but for a particular height, rather than the top
Definition: hardfork.cpp:127
static const time_t DEFAULT_FORKED_TIME
Definition: hardfork.h:48
uint8_t threshold
Definition: blockchain.cpp:89