Electroneum
hardfork.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2019, The Electroneum Project
2 // Copyrights(c) 2014-2017, The Monero Project
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 #pragma once
31 
32 #include "syncobj.h"
34 
35 namespace cryptonote
36 {
37  class BlockchainDB;
38 
39  class HardFork
40  {
41  public:
42  typedef enum {
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 
64 
75  bool add_fork(uint8_t version, uint64_t height, uint8_t threshold, time_t time);
76 
87  bool add_fork(uint8_t version, uint64_t height, time_t time);
88 
94  void init();
95 
115  bool check(const cryptonote::block &block) const;
116 
129  bool check_for_height(const cryptonote::block &block, uint64_t height) const;
130 
138  bool add(const cryptonote::block &block, uint64_t height);
139 
151  bool reorganize_from_block_height(uint64_t height);
152  bool reorganize_from_chain_height(uint64_t height);
153 
162  State get_state(time_t t) const;
163  State get_state() const;
164 
170  uint8_t get(uint64_t height) const;
171 
177  uint8_t get_ideal_version() const;
178 
184  uint8_t get_ideal_version(uint64_t height) const;
185 
191  uint8_t get_next_version() const;
192 
199  uint8_t get_current_version() const;
200 
204  uint64_t get_earliest_ideal_height_for_version(uint8_t version) const;
205 
218  bool get_voting_info(uint8_t version, uint32_t &window, uint32_t &votes, uint32_t &threshold, uint64_t &earliest_height, uint8_t &voting) const;
219 
223  uint64_t get_window_size() const { return window_size; }
224 
225  private:
226 
227  uint8_t get_block_version(uint64_t height) const;
228  bool do_check(uint8_t block_version, uint8_t voting_version) const;
229  bool do_check_for_height(uint8_t block_version, uint8_t voting_version, uint64_t height) const;
230  int get_voted_fork_index(uint64_t height) const;
231  uint8_t get_effective_version(uint8_t voting_version) const;
232  bool add(uint8_t block_version, uint8_t voting_version, uint64_t height);
233 
234  bool rescan_from_block_height(uint64_t height);
235  bool rescan_from_chain_height(uint64_t height);
236 
237  private:
238 
240 
241  time_t forked_time;
242  time_t update_time;
243  uint64_t window_size;
245 
248 
249  struct Params {
250  uint8_t version;
251  uint8_t threshold;
252  uint64_t height;
253  time_t time;
254  Params(uint8_t version, uint64_t height, uint8_t threshold, time_t time): version(version), threshold(threshold), height(height), time(time) {}
255  };
256  std::vector<Params> heights;
257 
258  std::deque<uint8_t> versions; /* rolling window of the last N blocks' versions */
259  unsigned int last_versions[256]; /* count of the block versions in the last N blocks */
261 
262  mutable epee::critical_section lock;
263  };
264 
265 } // namespace cryptonote
266 
Params(uint8_t version, uint64_t height, uint8_t threshold, time_t time)
Definition: hardfork.h:254
int get_voted_fork_index(uint64_t height) const
Definition: hardfork.cpp:309
uint32_t current_fork_index
Definition: hardfork.h:260
Definition: cryptonote_basic.h:382
bool add(const cryptonote::block &block, uint64_t height)
add a new block
Definition: hardfork.cpp:163
State get_state() const
Definition: hardfork.cpp:340
uint64_t height
Definition: blockchain.cpp:87
static const uint64_t DEFAULT_ORIGINAL_VERSION_TILL_HEIGHT
Definition: hardfork.h:48
static const uint64_t DEFAULT_WINDOW_SIZE
Definition: hardfork.h:51
uint64_t get_window_size() const
returns the size of the voting window in blocks
Definition: hardfork.h:223
Definition: hardfork.h:39
uint64_t get_earliest_ideal_height_for_version(uint8_t version) const
returns the earliest block a given version may activate
Definition: hardfork.cpp:381
epee::critical_section lock
Definition: hardfork.h:262
uint8_t get_effective_version(uint8_t voting_version) const
Definition: hardfork.cpp:99
time_t time
Definition: hardfork.h:253
void init()
initialize the object
Definition: hardfork.cpp:168
BlockchainDB & db
Definition: hardfork.h:239
bool rescan_from_block_height(uint64_t height)
Definition: hardfork.cpp:267
Holds cryptonote related classes and helpers.
Definition: db_bdb.cpp:225
bool reorganize_from_chain_height(uint64_t height)
Definition: hardfork.cpp:260
uint8_t get_next_version() const
returns the next version
Definition: hardfork.cpp:390
time_t time
Definition: blockchain.cpp:89
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:402
Definition: hardfork.h:44
std::vector< Params > heights
Definition: hardfork.h:256
time_t forked_time
Definition: hardfork.h:241
bool rescan_from_chain_height(uint64_t height)
Definition: hardfork.cpp:302
uint64_t original_version_till_height
Definition: hardfork.h:247
bool check(const cryptonote::block &block) const
check whether a new block would be accepted
Definition: hardfork.cpp:115
bool reorganize_from_block_height(uint64_t height)
called when the blockchain is reorganized
Definition: hardfork.cpp:219
uint8_t get_block_version(uint64_t height) const
Definition: hardfork.cpp:210
uint64_t height
Definition: hardfork.h:252
Definition: hardfork.h:43
Definition: hardfork.h:45
uint8_t version
Definition: blockchain.cpp:86
bool add_fork(uint8_t version, uint64_t height, uint8_t threshold, time_t time)
add a new hardfork height
Definition: hardfork.cpp:73
bool do_check_for_height(uint8_t block_version, uint8_t voting_version, uint64_t height) const
Definition: hardfork.cpp:121
bool do_check(uint8_t block_version, uint8_t voting_version) const
Definition: hardfork.cpp:109
uint64_t window_size
Definition: hardfork.h:243
The BlockchainDB backing store interface declaration/contract.
Definition: blockchain_db.h:342
static const uint8_t DEFAULT_THRESHOLD_PERCENT
Definition: hardfork.h:52
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:58
State
Definition: hardfork.h:42
unsigned int last_versions[256]
Definition: hardfork.h:259
static const time_t DEFAULT_UPDATE_TIME
Definition: hardfork.h:50
time_t update_time
Definition: hardfork.h:242
uint8_t threshold
Definition: hardfork.h:251
uint8_t get_current_version() const
returns the current version
Definition: hardfork.cpp:358
uint8_t original_version
Definition: hardfork.h:246
uint8_t get_ideal_version() const
returns the latest "ideal" version
Definition: hardfork.cpp:364
Definition: hardfork.h:249
uint8_t version
Definition: hardfork.h:250
std::deque< uint8_t > versions
Definition: hardfork.h:258
uint8_t default_threshold_percent
Definition: hardfork.h:244
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:128
static const time_t DEFAULT_FORKED_TIME
Definition: hardfork.h:49
uint8_t threshold
Definition: blockchain.cpp:88