aboutsummaryrefslogtreecommitdiff
path: root/src/cryptonote_core
diff options
context:
space:
mode:
authormoneromooo-monero <moneromooo-monero@users.noreply.github.com>2015-09-20 18:41:38 +0100
committermoneromooo-monero <moneromooo-monero@users.noreply.github.com>2015-09-20 18:42:52 +0100
commit5b11a89a76cf44c0fb98d98b05eb1c9e4ddce0c4 (patch)
tree8f6b2d879f7e8b5c224a4592373bd37afc2a9957 /src/cryptonote_core
parenthardfork: remove the "parts are copyright cryptonote" notices (diff)
downloadmonero-5b11a89a76cf44c0fb98d98b05eb1c9e4ddce0c4.tar.xz
hardfork: most state now saved to the DB
There will be a delay on first load of an existing blockchain as it gets reparsed for this state data.
Diffstat (limited to 'src/cryptonote_core')
-rw-r--r--src/cryptonote_core/blockchain.cpp41
-rw-r--r--src/cryptonote_core/blockchain.h6
-rw-r--r--src/cryptonote_core/hardfork.cpp109
-rw-r--r--src/cryptonote_core/hardfork.h41
4 files changed, 97 insertions, 100 deletions
diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp
index 2ac50aefc..65d07dcf7 100644
--- a/src/cryptonote_core/blockchain.cpp
+++ b/src/cryptonote_core/blockchain.cpp
@@ -69,10 +69,22 @@ extern "C" void slow_hash_free_state();
DISABLE_VS_WARNINGS(4267)
+static const struct {
+ uint8_t version;
+ uint64_t height;
+ time_t time;
+} hard_forks[] = {
+ // version 1 from the start of the blockchain
+ { 1, 1, 1341378000 },
+
+ // version 2 can start from block 1009827, setup on the 20th of september
+ { 2, 1009827, 1442763710 },
+};
+
//------------------------------------------------------------------
Blockchain::Blockchain(tx_memory_pool& tx_pool) :
m_db(), m_tx_pool(tx_pool), m_timestamps_and_difficulties_height(0), m_current_block_cumul_sz_limit(0), m_is_in_checkpoint_zone(false),
-m_is_blockchain_storing(false), m_enforce_dns_checkpoints(false), m_hardfork(), m_max_prepare_blocks_threads(4), m_db_blocks_per_sync(1), m_db_sync_mode(db_async), m_fast_sync(true)
+m_is_blockchain_storing(false), m_enforce_dns_checkpoints(false), m_max_prepare_blocks_threads(4), m_db_blocks_per_sync(1), m_db_sync_mode(db_async), m_fast_sync(true)
{
LOG_PRINT_L3("Blockchain::" << __func__);
}
@@ -120,7 +132,7 @@ void Blockchain::serialize(archive_t & ar, const unsigned int version)
if (version > 12)
{
- ar & m_hardfork;
+ ar & *m_hardfork;
}
LOG_PRINT_L3("Blockchain storage:" << std::endl << "m_blocks: " << m_db->height() << std::endl << "m_blocks_index: " << m_blocks_index.size() << std::endl << "m_transactions: " << m_transactions.size() << std::endl << "dummy_key_images_container: " << dummy_key_images_container.size() << std::endl << "m_alternative_chains: " << m_alternative_chains.size() << std::endl << "m_outputs: " << m_outputs.size() << std::endl << "m_invalid_blocks: " << m_invalid_blocks.size() << std::endl << "m_current_block_cumul_sz_limit: " << m_current_block_cumul_sz_limit);
@@ -275,6 +287,11 @@ bool Blockchain::init(BlockchainDB* db, const bool testnet)
m_db = db;
+ m_hardfork = new HardFork(*db);
+ for (size_t n = 0; n < sizeof(hard_forks) / sizeof(hard_forks[0]); ++n)
+ m_hardfork->add(hard_forks[n].version, hard_forks[n].height, hard_forks[n].time);
+ m_hardfork->init();
+
// if the blockchain is new, add the genesis block
// this feels kinda kludgy to do it this way, but can be looked at later.
// TODO: add function to create and store genesis block,
@@ -355,9 +372,6 @@ bool Blockchain::init(BlockchainDB* db, const bool testnet)
}
#endif
- // reinitialize hard fork versions, since they're not saved in the DB
- m_hardfork.reorganize_from_chain_height (m_db, 1);
-
LOG_PRINT_GREEN("Blockchain initialized. last block: " << m_db->height() - 1 << ", " << epee::misc_utils::get_time_interval_string(timestamp_diff) << " time ago, current difficulty: " << get_difficulty_for_next_block(), LOG_LEVEL_0);
return true;
@@ -424,6 +438,7 @@ bool Blockchain::deinit()
LOG_PRINT_L0("There was an issue closing/storing the blockchain, shutting down now to prevent issues!");
}
+ delete m_hardfork;
delete m_db;
return true;
}
@@ -714,7 +729,7 @@ bool Blockchain::rollback_blockchain_switching(std::list<block>& original_chain,
CHECK_AND_ASSERT_MES(r && bvc.m_added_to_main_chain, false, "PANIC! failed to add (again) block while chain switching during the rollback!");
}
- m_hardfork.reorganize_from_chain_height(m_db, rollback_height);
+ m_hardfork->reorganize_from_chain_height(rollback_height);
LOG_PRINT_L1("Rollback to height " << rollback_height << " was successful.");
if (original_chain.size())
@@ -813,7 +828,7 @@ bool Blockchain::switch_to_alternative_blockchain(std::list<blocks_ext_by_hash::
m_alternative_chains.erase(ch_ent);
}
- m_hardfork.reorganize_from_chain_height(m_db, split_height);
+ m_hardfork->reorganize_from_chain_height(split_height);
LOG_PRINT_GREEN("REORGANIZE SUCCESS! on height: " << split_height << ", new blockchain size: " << m_db->height(), LOG_LEVEL_0);
return true;
@@ -986,7 +1001,7 @@ bool Blockchain::create_block_template(block& b, const account_public_address& m
CRITICAL_REGION_BEGIN(m_blockchain_lock);
height = m_db->height();
- b.major_version = m_hardfork.get_ideal_version();
+ b.major_version = m_hardfork->get_ideal_version();
b.minor_version = 0;
b.prev_id = get_tail_id();
b.timestamp = time(NULL);
@@ -2259,9 +2274,9 @@ bool Blockchain::handle_block_to_main_chain(const block& bl, const crypto::hash&
TIME_MEASURE_START(t1);
// this is a cheap test
- if (!m_hardfork.check(bl))
+ if (!m_hardfork->check(bl))
{
- LOG_PRINT_L1("Block with id: " << id << std::endl << "has old version: " << bl.major_version << std::endl << "current: " << m_hardfork.get_current_version());
+ LOG_PRINT_L1("Block with id: " << id << std::endl << "has old version: " << bl.major_version << std::endl << "current: " << m_hardfork->get_current_version());
return false;
}
@@ -2542,7 +2557,7 @@ bool Blockchain::handle_block_to_main_chain(const block& bl, const crypto::hash&
update_next_cumulative_size_limit();
// this will not fail since check succeeded above
- m_hardfork.add(bl, new_height - 1);
+ m_hardfork->add(bl, new_height - 1);
LOG_PRINT_L1("+++++ BLOCK SUCCESSFULLY ADDED" << std::endl << "id:\t" << id << std::endl << "PoW:\t" << proof_of_work << std::endl << "HEIGHT " << new_height << ", difficulty:\t" << current_diffic << std::endl << "block reward: " << print_money(fee_summary + base_reward) << "(" << print_money(base_reward) << " + " << print_money(fee_summary) << "), coinbase_blob_size: " << coinbase_blob_size << ", cumulative size: " << cumulative_block_size << ", " << block_processing_time << "(" << target_calculating_time << "/" << longhash_calculating_time << ")ms");
if(m_show_time_stats)
@@ -3080,10 +3095,10 @@ void Blockchain::set_user_options(uint64_t maxthreads, uint64_t blocks_per_sync,
HardFork::State Blockchain::get_hard_fork_state() const
{
- return m_hardfork.get_state();
+ return m_hardfork->get_state();
}
bool Blockchain::get_hard_fork_voting_info(uint8_t version, uint32_t &window, uint32_t &votes, uint32_t &threshold, uint8_t &voting) const
{
- return m_hardfork.get_voting_info(version, window, votes, threshold, voting);
+ return m_hardfork->get_voting_info(version, window, votes, threshold, voting);
}
diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h
index e549ea2d0..a248682fc 100644
--- a/src/cryptonote_core/blockchain.h
+++ b/src/cryptonote_core/blockchain.h
@@ -160,8 +160,8 @@ namespace cryptonote
void set_show_time_stats(bool stats) { m_show_time_stats = stats; }
HardFork::State get_hard_fork_state() const;
- uint8_t get_current_hard_fork_version() const { return m_hardfork.get_current_version(); }
- uint8_t get_ideal_hard_fork_version() const { return m_hardfork.get_ideal_version(); }
+ uint8_t get_current_hard_fork_version() const { return m_hardfork->get_current_version(); }
+ uint8_t get_ideal_hard_fork_version() const { return m_hardfork->get_ideal_version(); }
bool get_hard_fork_voting_info(uint8_t version, uint32_t &window, uint32_t &votes, uint32_t &threshold, uint8_t &voting) const;
BlockchainDB& get_db()
@@ -233,7 +233,7 @@ namespace cryptonote
std::atomic<bool> m_is_blockchain_storing;
bool m_enforce_dns_checkpoints;
- HardFork m_hardfork;
+ HardFork *m_hardfork;
template<class visitor_t>
inline bool scan_outputkeys_for_indexes(const txin_to_key& tx_in_to_key, visitor_t &vis, const crypto::hash &tx_prefix_hash, uint64_t* pmax_related_block_height = NULL) const;
diff --git a/src/cryptonote_core/hardfork.cpp b/src/cryptonote_core/hardfork.cpp
index 3003e74c8..d2e95b58e 100644
--- a/src/cryptonote_core/hardfork.cpp
+++ b/src/cryptonote_core/hardfork.cpp
@@ -35,15 +35,14 @@
using namespace cryptonote;
-HardFork::HardFork(uint8_t original_version, time_t forked_time, time_t update_time, uint64_t max_history, int threshold_percent, uint64_t checkpoint_period):
+HardFork::HardFork(cryptonote::BlockchainDB &db, uint8_t original_version, time_t forked_time, time_t update_time, uint64_t window_size, int threshold_percent):
+ db(db),
original_version(original_version),
forked_time(forked_time),
update_time(update_time),
- max_history(max_history),
- threshold_percent(threshold_percent),
- checkpoint_period(checkpoint_period)
+ window_size(window_size),
+ threshold_percent(threshold_percent)
{
- init();
}
bool HardFork::add(uint8_t version, uint64_t height, time_t time)
@@ -96,7 +95,7 @@ bool HardFork::add(const cryptonote::block &block, uint64_t height)
const uint8_t version = get_effective_version(block);
- while (versions.size() >= max_history) {
+ while (versions.size() >= window_size) {
const uint8_t old_version = versions.front();
last_versions[old_version]--;
assert(last_versions[old_version] >= 0);
@@ -109,13 +108,12 @@ bool HardFork::add(const cryptonote::block &block, uint64_t height)
uint8_t voted = get_voted_fork_index(height);
if (voted > current_fork_index) {
for (int v = heights[current_fork_index].version + 1; v <= heights[voted].version; ++v) {
- starting[v] = height;
+ db.set_hard_fork_starting_height(v, height);
}
current_fork_index = voted;
}
- if (height % checkpoint_period == 0)
- checkpoints.push_back(std::make_pair(height, current_fork_index));
+ db.set_hard_fork_version(height, heights[current_fork_index].version);
return true;
}
@@ -126,59 +124,66 @@ void HardFork::init()
versions.clear();
for (size_t n = 0; n < 256; ++n)
last_versions[n] = 0;
- for (size_t n = 0; n < 256; ++n)
- starting[n] = std::numeric_limits<uint64_t>::max();
- add(original_version, 0, 0);
- for (size_t n = 0; n <= original_version; ++n)
- starting[n] = 0;
- checkpoints.clear();
current_fork_index = 0;
- vote_threshold = (uint32_t)ceilf(max_history * threshold_percent / 100.0f);
+ vote_threshold = (uint32_t)ceilf(window_size * threshold_percent / 100.0f);
+
+ // restore state from DB
+ uint64_t height = db.height();
+ if (height > window_size)
+ height -= window_size;
+ else
+ height = 1;
+
+ bool populate = db.get_hard_fork_starting_height(original_version) == std::numeric_limits<uint64_t>::max();
+ if (populate) {
+ LOG_PRINT_L0("The DB has no hard fork info, reparsing from start");
+ height = 1;
+ }
+ LOG_PRINT_L1("reorganizing from " << height);
+ reorganize_from_chain_height(height);
+ if (populate) {
+ // reorg will not touch the genesis block, use this as a flag for populating done
+ db.set_hard_fork_version(0, original_version);
+ db.set_hard_fork_starting_height(original_version, 0);
+ }
+ LOG_PRINT_L1("reorganization done");
}
-bool HardFork::reorganize_from_block_height(const cryptonote::BlockchainDB *db, uint64_t height)
+bool HardFork::reorganize_from_block_height(uint64_t height)
{
CRITICAL_REGION_LOCAL(lock);
- if (!db || height >= db->height())
+ if (height >= db.height())
return false;
- while (!checkpoints.empty() && checkpoints.back().first > height)
- checkpoints.pop_back();
+
versions.clear();
- int v;
- for (v = 255; v >= 0; --v) {
- if (starting[v] <= height)
- break;
- if (starting[v] != std::numeric_limits<uint64_t>::max()) {
- starting[v] = std::numeric_limits<uint64_t>::max();
- }
- }
- for (current_fork_index = 0; current_fork_index < heights.size(); ++current_fork_index) {
- if (heights[current_fork_index].version == v)
- break;
- }
for (size_t n = 0; n < 256; ++n)
last_versions[n] = 0;
- const uint64_t rescan_height = height >= (max_history - 1) ? height - (max_history - 1) : 0;
+ const uint64_t rescan_height = height >= (window_size - 1) ? height - (window_size - 1) : 0;
+ const uint8_t start_version = height == 0 ? original_version : db.get_hard_fork_version(height);
+ while (heights[current_fork_index].version > start_version) {
+ db.set_hard_fork_starting_height(heights[current_fork_index].version, std::numeric_limits<uint64_t>::max());
+ --current_fork_index;
+ }
for (uint64_t h = rescan_height; h <= height; ++h) {
- cryptonote::block b = db->get_block_from_height(h);
+ cryptonote::block b = db.get_block_from_height(h);
const uint8_t v = get_effective_version(b);
last_versions[v]++;
versions.push_back(v);
}
- const uint64_t bc_height = db->height();
+ const uint64_t bc_height = db.height();
for (uint64_t h = height + 1; h < bc_height; ++h) {
- add(db->get_block_from_height(h), h);
+ add(db.get_block_from_height(h), h);
}
return true;
}
-bool HardFork::reorganize_from_chain_height(const cryptonote::BlockchainDB *db, uint64_t height)
+bool HardFork::reorganize_from_chain_height(uint64_t height)
{
if (height == 0)
return false;
- return reorganize_from_block_height(db, height - 1);
+ return reorganize_from_block_height(height - 1);
}
int HardFork::get_voted_fork_index(uint64_t height) const
@@ -219,18 +224,17 @@ HardFork::State HardFork::get_state() const
uint8_t HardFork::get(uint64_t height) const
{
CRITICAL_REGION_LOCAL(lock);
- for (size_t n = 1; n < 256; ++n) {
- if (starting[n] > height)
- return n - 1;
+ if (height > db.height()) {
+ assert(false);
+ return 255;
}
- assert(false);
- return 255;
+ return db.get_hard_fork_version(height);
}
uint64_t HardFork::get_start_height(uint8_t version) const
{
CRITICAL_REGION_LOCAL(lock);
- return starting[version];
+ return db.get_hard_fork_starting_height(version);
}
uint8_t HardFork::get_current_version() const
@@ -261,20 +265,3 @@ bool HardFork::get_voting_info(uint8_t version, uint32_t &window, uint32_t &vote
return enabled;
}
-template<class archive_t>
-void HardFork::serialize(archive_t & ar, const unsigned int version)
-{
- CRITICAL_REGION_LOCAL(lock);
- ar & forked_time;
- ar & update_time;
- ar & max_history;
- ar & threshold_percent;
- ar & original_version;
- ar & heights;
- ar & last_versions;
- ar & starting;
- ar & current_fork_index;
- ar & vote_threshold;
- ar & checkpoint_period;
- ar & checkpoints;
-}
diff --git a/src/cryptonote_core/hardfork.h b/src/cryptonote_core/hardfork.h
index 3bcf46cff..bdac87f2c 100644
--- a/src/cryptonote_core/hardfork.h
+++ b/src/cryptonote_core/hardfork.h
@@ -28,8 +28,6 @@
#pragma once
-#include <boost/serialization/serialization.hpp>
-#include <boost/serialization/version.hpp>
#include "syncobj.h"
#include "cryptonote_core/cryptonote_basic.h"
@@ -48,9 +46,8 @@ namespace cryptonote
static const time_t DEFAULT_FORKED_TIME = 31557600; // a year in seconds
static const time_t DEFAULT_UPDATE_TIME = 31557600 / 2;
- static const uint64_t DEFAULT_MAX_HISTORY = 50; // supermajority window check length
+ static const uint64_t DEFAULT_WINDOW_SIZE = 50; // supermajority window check length
static const int DEFAULT_THRESHOLD_PERCENT = 80;
- static const uint64_t DEFAULT_CHECKPOINT_PERIOD = 1024; // mark a checkpoint every that many blocks
/**
* @brief creates a new HardFork object
@@ -58,10 +55,10 @@ namespace cryptonote
* @param original_version the block version for blocks 0 through to the first fork
* @param forked_time the time in seconds before thinking we're forked
* @param update_time the time in seconds before thinking we need to update
- * @param max_history the size of the window in blocks to consider for version voting
+ * @param window_size the size of the window in blocks to consider for version voting
* @param threshold_percent the size of the majority in percents
*/
- HardFork(uint8_t original_version = 1, time_t forked_time = DEFAULT_FORKED_TIME, time_t update_time = DEFAULT_UPDATE_TIME, uint64_t max_history = DEFAULT_MAX_HISTORY, int threshold_percent = DEFAULT_THRESHOLD_PERCENT, uint64_t checkpoint_period = DEFAULT_CHECKPOINT_PERIOD);
+ HardFork(cryptonote::BlockchainDB &db, uint8_t original_version = 1, time_t forked_time = DEFAULT_FORKED_TIME, time_t update_time = DEFAULT_UPDATE_TIME, uint64_t window_size = DEFAULT_WINDOW_SIZE, int threshold_percent = DEFAULT_THRESHOLD_PERCENT);
/**
* @brief add a new hardfork height
@@ -75,6 +72,13 @@ namespace cryptonote
bool add(uint8_t version, uint64_t height, time_t time);
/**
+ * @brief initialize the object
+ *
+ * Must be done after adding all the required hardforks via add above
+ */
+ void init();
+
+ /**
* @brief check whether a new block would be accepted
*
* returns true if the block is accepted, false otherwise
@@ -91,9 +95,7 @@ namespace cryptonote
* call add first, then, if the hard fork requirements are met,
* add the block to the blockchain, upon which a failure (the
* block being invalid, double spending, etc) would cause the
- * hardfork object to rescan the blockchain versions past the
- * last checkpoint, potentially causing a large number of DB
- * operations.
+ * hardfork object to reorganize.
*/
bool check(const cryptonote::block &block) const;
@@ -117,8 +119,8 @@ namespace cryptonote
* @param blockchain the blockchain
* @param height of the last block kept from the previous blockchain
*/
- bool reorganize_from_block_height(const cryptonote::BlockchainDB *db, uint64_t height);
- bool reorganize_from_chain_height(const cryptonote::BlockchainDB *db, uint64_t height);
+ bool reorganize_from_block_height(uint64_t height);
+ bool reorganize_from_chain_height(uint64_t height);
/**
* @brief returns current state at the given time
@@ -176,23 +178,21 @@ namespace cryptonote
/**
* @brief returns the size of the voting window in blocks
*/
- uint64_t get_window_size() const { return max_history; }
-
- template<class archive_t>
- void serialize(archive_t & ar, const unsigned int version);
+ uint64_t get_window_size() const { return window_size; }
private:
- void init();
bool do_check(const cryptonote::block &block) const;
int get_voted_fork_index(uint64_t height) const;
uint8_t get_effective_version(const cryptonote::block &block) const;
private:
+ BlockchainDB &db;
+
time_t forked_time;
time_t update_time;
- uint64_t max_history;
+ uint64_t window_size;
int threshold_percent;
uint8_t original_version;
@@ -206,16 +206,11 @@ namespace cryptonote
std::deque<uint8_t> versions; /* rolling window of the last N blocks' versions */
unsigned int last_versions[256]; /* count of the block versions in the last N blocks */
- uint64_t starting[256]; /* block height at which each fork starts */
- unsigned int current_fork_index;
+ uint32_t current_fork_index;
uint32_t vote_threshold;
- uint64_t checkpoint_period;
- std::vector<std::pair<uint64_t, int>> checkpoints;
-
mutable epee::critical_section lock;
};
} // namespace cryptonote
-BOOST_CLASS_VERSION(cryptonote::HardFork, 1)