diff options
author | moneromooo-monero <moneromooo-monero@users.noreply.github.com> | 2015-09-20 18:41:38 +0100 |
---|---|---|
committer | moneromooo-monero <moneromooo-monero@users.noreply.github.com> | 2015-09-20 18:42:52 +0100 |
commit | 5b11a89a76cf44c0fb98d98b05eb1c9e4ddce0c4 (patch) | |
tree | 8f6b2d879f7e8b5c224a4592373bd37afc2a9957 | |
parent | hardfork: remove the "parts are copyright cryptonote" notices (diff) | |
download | monero-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.
-rw-r--r-- | src/blockchain_db/berkeleydb/db_bdb.cpp | 75 | ||||
-rw-r--r-- | src/blockchain_db/berkeleydb/db_bdb.h | 10 | ||||
-rw-r--r-- | src/blockchain_db/blockchain_db.h | 6 | ||||
-rw-r--r-- | src/blockchain_db/lmdb/db_lmdb.cpp | 124 | ||||
-rw-r--r-- | src/blockchain_db/lmdb/db_lmdb.h | 9 | ||||
-rw-r--r-- | src/cryptonote_core/blockchain.cpp | 41 | ||||
-rw-r--r-- | src/cryptonote_core/blockchain.h | 6 | ||||
-rw-r--r-- | src/cryptonote_core/hardfork.cpp | 109 | ||||
-rw-r--r-- | src/cryptonote_core/hardfork.h | 41 | ||||
-rw-r--r-- | tests/unit_tests/hardfork.cpp | 342 |
10 files changed, 521 insertions, 242 deletions
diff --git a/src/blockchain_db/berkeleydb/db_bdb.cpp b/src/blockchain_db/berkeleydb/db_bdb.cpp index a990d7aaf..2a9e9a7f8 100644 --- a/src/blockchain_db/berkeleydb/db_bdb.cpp +++ b/src/blockchain_db/berkeleydb/db_bdb.cpp @@ -118,6 +118,9 @@ const char* const BDB_OUTPUT_KEYS = "output_keys"; const char* const BDB_SPENT_KEYS = "spent_keys"; +const char* const BDB_HF_STARTING_HEIGHTS = "hf_starting_heights"; +const char* const BDB_HF_VERSIONS = "hf_versions"; + const unsigned int MB = 1024 * 1024; // ND: FIXME: db keeps running out of locks when doing full syncs. Possible bug??? Set it to 5K for now. const unsigned int DB_MAX_LOCKS = 5000; @@ -667,6 +670,9 @@ void BlockchainBDB::open(const std::string& filename, const int db_flags) m_spent_keys = new Db(m_env, 0); + m_hf_starting_heights = new Db(m_env, 0); + m_hf_versions = new Db(m_env, 0); + // Tell DB about Dbs that need duplicate support // Note: no need to tell about sorting, // as the default is insertion order, which we want @@ -684,6 +690,9 @@ void BlockchainBDB::open(const std::string& filename, const int db_flags) m_output_indices->set_re_len(sizeof(uint64_t)); m_output_keys->set_re_len(sizeof(output_data_t)); + m_hf_starting_heights->set_re_len(sizeof(uint64_t)); + m_hf_versions->set_re_len(sizeof(uint8_t)); + //TODO: Find out if we need to do Db::set_flags(DB_RENUMBER) // for the RECNO databases. We shouldn't as we're only // inserting/removing from the end, but we'll see. @@ -713,6 +722,9 @@ void BlockchainBDB::open(const std::string& filename, const int db_flags) m_spent_keys->open(txn, BDB_SPENT_KEYS, NULL, DB_HASH, DB_CREATE, 0); + m_hf_starting_heights->open(txn, BDB_HF_STARTING_HEIGHTS, NULL, DB_RECNO, DB_CREATE, 0); + m_hf_versions->open(txn, BDB_HF_VERSIONS, NULL, DB_RECNO, DB_CREATE, 0); + txn.commit(); DB_BTREE_STAT* stats; @@ -785,6 +797,9 @@ void BlockchainBDB::sync() m_output_keys->sync(0); m_spent_keys->sync(0); + + m_hf_starting_heights->sync(0); + m_hf_versions->sync(0); } catch (const std::exception& e) { @@ -859,6 +874,12 @@ std::vector<std::string> BlockchainBDB::get_filenames() const m_spent_keys->get_dbname(pfname, pdbname); filenames.push_back(fname); + m_hf_starting_heights->get_dbname(pfname, pdbname); + filenames.push_back(fname); + + m_hf_versions->get_dbname(pfname, pdbname); + filenames.push_back(fname); + std::vector<std::string> full_paths; for (auto& filename : filenames) @@ -1839,6 +1860,60 @@ void BlockchainBDB::get_output_tx_and_index(const uint64_t& amount, const std::v LOG_PRINT_L3("db3: " << db3); } +void BlockchainBDB::set_hard_fork_starting_height(uint8_t version, uint64_t height) +{ + LOG_PRINT_L3("BlockchainBDB::" << __func__); + check_open(); + + Dbt_copy<uint8_t> val_key(version); + Dbt_copy<uint64_t> val(height); + if (m_hf_starting_heights->put(DB_DEFAULT_TX, &val_key, &val, 0)) + throw1(DB_ERROR("Error adding hard fork starting height to db transaction.")); +} + +uint64_t BlockchainBDB::get_hard_fork_starting_height(uint8_t version) const +{ + LOG_PRINT_L3("BlockchainBDB::" << __func__); + check_open(); + + Dbt_copy<uint8_t> key(version); + Dbt_copy<uint64_t> result; + + auto get_result = m_hf_starting_heights->get(DB_DEFAULT_TX, &key, &result, 0); + if (get_result == DB_NOTFOUND) + return std::numeric_limits<uint64_t>::max(); + else if (get_result) + throw0(DB_ERROR("Error attempting to retrieve hard fork starting height from the db")); + + return result; +} + +void BlockchainBDB::set_hard_fork_version(uint64_t height, uint8_t version) +{ + LOG_PRINT_L3("BlockchainBDB::" << __func__); + check_open(); + + Dbt_copy<uint64_t> val_key(height); + Dbt_copy<uint8_t> val(version); + if (m_hf_versions->put(DB_DEFAULT_TX, &val_key, &val, 0)) + throw1(DB_ERROR("Error adding hard fork version to db transaction.")); +} + +uint8_t BlockchainBDB::get_hard_fork_version(uint64_t height) const +{ + LOG_PRINT_L3("BlockchainBDB::" << __func__); + check_open(); + + Dbt_copy<uint64_t> key(height); + Dbt_copy<uint8_t> result; + + auto get_result = m_hf_versions->get(DB_DEFAULT_TX, &key, &result, 0); + if (get_result == DB_NOTFOUND || get_result) + throw0(DB_ERROR("Error attempting to retrieve hard fork version from the db")); + + return result; +} + void BlockchainBDB::checkpoint_worker() const { LOG_PRINT_L0("Entering BDB checkpoint thread.") diff --git a/src/blockchain_db/berkeleydb/db_bdb.h b/src/blockchain_db/berkeleydb/db_bdb.h index f92bbef68..54edcf0ad 100644 --- a/src/blockchain_db/berkeleydb/db_bdb.h +++ b/src/blockchain_db/berkeleydb/db_bdb.h @@ -374,6 +374,13 @@ private: virtual void remove_spent_key(const crypto::key_image& k_image); void get_output_global_indices(const uint64_t& amount, const std::vector<uint64_t> &offsets, std::vector<uint64_t> &global_indices); + + // Hard fork related storage + virtual void set_hard_fork_starting_height(uint8_t version, uint64_t height); + virtual uint64_t get_hard_fork_starting_height(uint8_t version) const; + virtual void set_hard_fork_version(uint64_t height, uint8_t version); + virtual uint8_t get_hard_fork_version(uint64_t height) const; + /** * @brief convert a tx output to a blob for storage * @@ -430,6 +437,9 @@ private: Db* m_spent_keys; + Db* m_hf_starting_heights; + Db* m_hf_versions; + uint64_t m_height; uint64_t m_num_outputs; std::string m_folder; diff --git a/src/blockchain_db/blockchain_db.h b/src/blockchain_db/blockchain_db.h index 25a34fc09..24bf4024d 100644 --- a/src/blockchain_db/blockchain_db.h +++ b/src/blockchain_db/blockchain_db.h @@ -493,6 +493,12 @@ public: // returns true if key image <img> is present in spent key images storage virtual bool has_key_image(const crypto::key_image& img) const = 0; + // Hard fork related storage + virtual void set_hard_fork_starting_height(uint8_t version, uint64_t height) = 0; + virtual uint64_t get_hard_fork_starting_height(uint8_t version) const = 0; + virtual void set_hard_fork_version(uint64_t height, uint8_t version) = 0; + virtual uint8_t get_hard_fork_version(uint64_t height) const = 0; + void set_auto_remove_logs(bool auto_remove) { m_auto_remove_logs = auto_remove; } bool m_open; diff --git a/src/blockchain_db/lmdb/db_lmdb.cpp b/src/blockchain_db/lmdb/db_lmdb.cpp index 1583a0c06..2695d83f8 100644 --- a/src/blockchain_db/lmdb/db_lmdb.cpp +++ b/src/blockchain_db/lmdb/db_lmdb.cpp @@ -131,6 +131,15 @@ auto compare_uint64 = [](const MDB_val *a, const MDB_val *b) else return 1; }; +auto compare_uint8 = [](const MDB_val *a, const MDB_val *b) +{ + const uint8_t va = *(const uint8_t*)a->mv_data; + const uint8_t vb = *(const uint8_t*)b->mv_data; + if (va < vb) return -1; + else if (va == vb) return 0; + else return 1; +}; + int compare_hash32(const MDB_val *a, const MDB_val *b) { uint32_t *va = (uint32_t*) a->mv_data; @@ -166,6 +175,9 @@ const char* const LMDB_OUTPUTS = "outputs"; const char* const LMDB_OUTPUT_GINDICES = "output_gindices"; const char* const LMDB_SPENT_KEYS = "spent_keys"; +const char* const LMDB_HF_STARTING_HEIGHTS = "hf_starting_heights"; +const char* const LMDB_HF_VERSIONS = "hf_versions"; + inline void lmdb_db_open(MDB_txn* txn, const char* name, int flags, MDB_dbi& dbi, const std::string& error_string) { if (mdb_dbi_open(txn, name, flags, &dbi)) @@ -1022,6 +1034,9 @@ void BlockchainLMDB::open(const std::string& filename, const int mdb_flags) lmdb_db_open(txn, LMDB_SPENT_KEYS, MDB_CREATE, m_spent_keys, "Failed to open db handle for m_spent_keys"); + lmdb_db_open(txn, LMDB_HF_STARTING_HEIGHTS, MDB_CREATE, m_hf_starting_heights, "Failed to open db handle for m_hf_starting_heights"); + lmdb_db_open(txn, LMDB_HF_VERSIONS, MDB_CREATE, m_hf_versions, "Failed to open db handle for m_hf_versions"); + mdb_set_dupsort(txn, m_output_amounts, compare_uint64); mdb_set_dupsort(txn, m_tx_outputs, compare_uint64); mdb_set_compare(txn, m_spent_keys, compare_hash32); @@ -1029,6 +1044,8 @@ void BlockchainLMDB::open(const std::string& filename, const int mdb_flags) mdb_set_compare(txn, m_txs, compare_hash32); mdb_set_compare(txn, m_tx_unlocks, compare_hash32); mdb_set_compare(txn, m_tx_heights, compare_hash32); + mdb_set_compare(txn, m_hf_starting_heights, compare_uint8); + mdb_set_compare(txn, m_hf_versions, compare_uint64); // get and keep current height MDB_stat db_stats; @@ -2347,4 +2364,111 @@ void BlockchainLMDB::get_output_tx_and_index(const uint64_t& amount, const std:: LOG_PRINT_L3("db3: " << db3); } +void BlockchainLMDB::set_hard_fork_starting_height(uint8_t version, uint64_t height) +{ + LOG_PRINT_L3("BlockchainLMDB::" << __func__); + check_open(); + + mdb_txn_safe txn; + mdb_txn_safe* txn_ptr = &txn; + if (m_batch_active) + txn_ptr = m_write_txn; + else + { + if (mdb_txn_begin(m_env, NULL, 0, txn)) + throw0(DB_ERROR("Failed to create a transaction for the db")); + txn_ptr = &txn; + } + + MDB_val_copy<uint8_t> val_key(version); + MDB_val_copy<uint64_t> val_value(height); + if (auto result = mdb_put(*txn_ptr, m_hf_starting_heights, &val_key, &val_value, 0)) + throw1(DB_ERROR(std::string("Error adding hard fork starting height to db transaction: ").append(mdb_strerror(result)).c_str())); + + if (!m_batch_active) + txn.commit(); +} + +uint64_t BlockchainLMDB::get_hard_fork_starting_height(uint8_t version) const +{ + LOG_PRINT_L3("BlockchainLMDB::" << __func__); + check_open(); + + mdb_txn_safe txn; + mdb_txn_safe* txn_ptr = &txn; + if (m_batch_active) + txn_ptr = m_write_txn; + else + { + if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR("Failed to create a transaction for the db")); + txn_ptr = &txn; + } + + MDB_val_copy<uint8_t> val_key(version); + MDB_val val_ret; + auto result = mdb_get(*txn_ptr, m_hf_starting_heights, &val_key, &val_ret); + if (result == MDB_NOTFOUND) + return std::numeric_limits<uint64_t>::max(); + if (result) + throw0(DB_ERROR("Error attempting to retrieve a hard fork starting height from the db")); + + if (!m_batch_active) + txn.commit(); + return *(const uint64_t*)val_ret.mv_data; +} + +void BlockchainLMDB::set_hard_fork_version(uint64_t height, uint8_t version) +{ + LOG_PRINT_L3("BlockchainLMDB::" << __func__); + check_open(); + +//LOG_PRINT_L1("BlockchainLMDB::set_hard_fork_version: batch " << m_batch_active << ", height " << height << ", version " << (int)version); + mdb_txn_safe txn; + mdb_txn_safe* txn_ptr = &txn; + if (m_batch_active) + txn_ptr = m_write_txn; + else + { + if (mdb_txn_begin(m_env, NULL, 0, txn)) + throw0(DB_ERROR("Failed to create a transaction for the db")); + txn_ptr = &txn; + } + + MDB_val_copy<uint64_t> val_key(height); + MDB_val_copy<uint8_t> val_value(version); + if (auto result = mdb_put(*txn_ptr, m_hf_versions, &val_key, &val_value, 0)) + throw1(DB_ERROR(std::string("Error adding hard fork version to db transaction: ").append(mdb_strerror(result)).c_str())); + + if (!m_batch_active) + txn.commit(); +} + +uint8_t BlockchainLMDB::get_hard_fork_version(uint64_t height) const +{ + LOG_PRINT_L3("BlockchainLMDB::" << __func__); + check_open(); + + mdb_txn_safe txn; + mdb_txn_safe* txn_ptr = &txn; + if (m_batch_active) + txn_ptr = m_write_txn; + else + { + if (mdb_txn_begin(m_env, NULL, MDB_RDONLY, txn)) + throw0(DB_ERROR("Failed to create a transaction for the db")); + txn_ptr = &txn; + } + + MDB_val_copy<uint64_t> val_key(height); + MDB_val val_ret; + auto result = mdb_get(*txn_ptr, m_hf_versions, &val_key, &val_ret); + if (result == MDB_NOTFOUND || result) + throw0(DB_ERROR("Error attempting to retrieve a hard fork version from the db")); + + if (!m_batch_active) + txn.commit(); + return *(const uint8_t*)val_ret.mv_data; +} + } // namespace cryptonote diff --git a/src/blockchain_db/lmdb/db_lmdb.h b/src/blockchain_db/lmdb/db_lmdb.h index b4c197803..380954295 100644 --- a/src/blockchain_db/lmdb/db_lmdb.h +++ b/src/blockchain_db/lmdb/db_lmdb.h @@ -238,6 +238,12 @@ private: virtual void remove_spent_key(const crypto::key_image& k_image); + // Hard fork + virtual void set_hard_fork_starting_height(uint8_t version, uint64_t height); + virtual uint64_t get_hard_fork_starting_height(uint8_t version) const; + virtual void set_hard_fork_version(uint64_t height, uint8_t version); + virtual uint8_t get_hard_fork_version(uint64_t height) const; + /** * @brief convert a tx output to a blob for storage * @@ -292,6 +298,9 @@ private: MDB_dbi m_spent_keys; + MDB_dbi m_hf_starting_heights; + MDB_dbi m_hf_versions; + uint64_t m_height; uint64_t m_num_outputs; std::string m_folder; 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) diff --git a/tests/unit_tests/hardfork.cpp b/tests/unit_tests/hardfork.cpp index 6b3c332a8..ad00ed60f 100644 --- a/tests/unit_tests/hardfork.cpp +++ b/tests/unit_tests/hardfork.cpp @@ -39,6 +39,97 @@ using namespace cryptonote; #define BLOCKS_PER_YEAR 525960 #define SECONDS_PER_YEAR 31557600 + +class TestDB: public BlockchainDB { +public: + virtual void open(const std::string& filename, const int db_flags = 0) { + for (size_t n = 0; n < 256; ++n) + starting_height[n] = std::numeric_limits<uint64_t>::max(); + } + virtual void close() {} + virtual void sync() {} + virtual void reset() {} + virtual std::vector<std::string> get_filenames() const { return std::vector<std::string>(); } + virtual std::string get_db_name() const { return std::string(); } + virtual bool lock() { return true; } + virtual void unlock() { } + virtual void batch_start(uint64_t batch_num_blocks=0) {} + virtual void batch_stop() {} + virtual void set_batch_transactions(bool) {} + virtual bool block_exists(const crypto::hash& h) const { return false; } + virtual block get_block(const crypto::hash& h) const { return block(); } + virtual uint64_t get_block_height(const crypto::hash& h) const { return 0; } + virtual block_header get_block_header(const crypto::hash& h) const { return block_header(); } + virtual uint64_t get_block_timestamp(const uint64_t& height) const { return 0; } + virtual uint64_t get_top_block_timestamp() const { return 0; } + virtual size_t get_block_size(const uint64_t& height) const { return 128; } + virtual difficulty_type get_block_cumulative_difficulty(const uint64_t& height) const { return 10; } + virtual difficulty_type get_block_difficulty(const uint64_t& height) const { return 0; } + virtual uint64_t get_block_already_generated_coins(const uint64_t& height) const { return 10000000000; } + virtual crypto::hash get_block_hash_from_height(const uint64_t& height) const { return crypto::hash(); } + virtual std::vector<block> get_blocks_range(const uint64_t& h1, const uint64_t& h2) const { return std::vector<block>(); } + virtual std::vector<crypto::hash> get_hashes_range(const uint64_t& h1, const uint64_t& h2) const { return std::vector<crypto::hash>(); } + virtual crypto::hash top_block_hash() const { return crypto::hash(); } + virtual block get_top_block() const { return block(); } + virtual uint64_t height() const { return blocks.size(); } + virtual bool tx_exists(const crypto::hash& h) const { return false; } + virtual uint64_t get_tx_unlock_time(const crypto::hash& h) const { return 0; } + virtual transaction get_tx(const crypto::hash& h) const { return transaction(); } + virtual uint64_t get_tx_count() const { return 0; } + virtual std::vector<transaction> get_tx_list(const std::vector<crypto::hash>& hlist) const { return std::vector<transaction>(); } + virtual uint64_t get_tx_block_height(const crypto::hash& h) const { return 0; } + virtual uint64_t get_num_outputs(const uint64_t& amount) const { return 1; } + virtual output_data_t get_output_key(const uint64_t& amount, const uint64_t& index) { return output_data_t(); } + virtual output_data_t get_output_key(const uint64_t& global_index) const { return output_data_t(); } + virtual tx_out get_output(const crypto::hash& h, const uint64_t& index) const { return tx_out(); } + virtual tx_out_index get_output_tx_and_index_from_global(const uint64_t& index) const { return tx_out_index(); } + virtual tx_out_index get_output_tx_and_index(const uint64_t& amount, const uint64_t& index) { return tx_out_index(); } + virtual void get_output_tx_and_index(const uint64_t& amount, const std::vector<uint64_t> &offsets, std::vector<tx_out_index> &indices) {} + virtual void get_output_key(const uint64_t &amount, const std::vector<uint64_t> &offsets, std::vector<output_data_t> &outputs) {} + virtual bool can_thread_bulk_indices() const { return false; } + virtual std::vector<uint64_t> get_tx_output_indices(const crypto::hash& h) const { return std::vector<uint64_t>(); } + virtual std::vector<uint64_t> get_tx_amount_output_indices(const crypto::hash& h) const { return std::vector<uint64_t>(); } + virtual bool has_key_image(const crypto::key_image& img) const { return false; } + virtual void remove_block() { blocks.pop_back(); } + virtual void add_transaction_data(const crypto::hash& blk_hash, const transaction& tx, const crypto::hash& tx_hash) {} + virtual void remove_transaction_data(const crypto::hash& tx_hash, const transaction& tx) {} + virtual void add_output(const crypto::hash& tx_hash, const tx_out& tx_output, const uint64_t& local_index, const uint64_t unlock_time) {} + virtual void remove_output(const tx_out& tx_output) {} + virtual void add_spent_key(const crypto::key_image& k_image) {} + virtual void remove_spent_key(const crypto::key_image& k_image) {} + + virtual void add_block( const block& blk + , const size_t& block_size + , const difficulty_type& cumulative_difficulty + , const uint64_t& coins_generated + , const crypto::hash& blk_hash + ) { + blocks.push_back(blk); + } + virtual block get_block_from_height(const uint64_t& height) const { + return blocks[height]; + } + virtual void set_hard_fork_starting_height(uint8_t version, uint64_t height) { + starting_height[version] = height; + } + virtual uint64_t get_hard_fork_starting_height(uint8_t version) const { + return starting_height[version]; + } + virtual void set_hard_fork_version(uint64_t height, uint8_t version) { + printf("set_hard_fork_version(%lu, %u)\n", (unsigned long)height, version); + if (versions.size() <= height) versions.resize(height+1); versions[height] = version; + } + virtual uint8_t get_hard_fork_version(uint64_t height) const { + printf("get_hard_fork_version(%lu)\n", (unsigned long)height); + return versions[height]; + } + +private: + std::vector<block> blocks; + uint64_t starting_height[256]; + std::deque<uint8_t> versions; +}; + static cryptonote::block mkblock(uint8_t version) { cryptonote::block b; @@ -48,19 +139,27 @@ static cryptonote::block mkblock(uint8_t version) TEST(empty_hardforks, Success) { - HardFork hf; + TestDB db; + HardFork hf(db); + ASSERT_TRUE(hf.add(1, 0, 0)); + hf.init(); ASSERT_TRUE(hf.get_state(time(NULL)) == HardFork::Ready); ASSERT_TRUE(hf.get_state(time(NULL) + 3600*24*400) == HardFork::Ready); + for (uint64_t h = 0; h <= 10; ++h) { + db.add_block(mkblock(1), 0, 0, 0, crypto::hash()); + ASSERT_TRUE(hf.add(db.get_block_from_height(h), h)); + } ASSERT_EQ(hf.get(0), 1); ASSERT_EQ(hf.get(1), 1); - ASSERT_EQ(hf.get(100000000), 1); + ASSERT_EQ(hf.get(10), 1); } TEST(ordering, Success) { - HardFork hf; + TestDB db; + HardFork hf(db); ASSERT_TRUE(hf.add(2, 2, 1)); ASSERT_FALSE(hf.add(3, 3, 1)); @@ -73,8 +172,10 @@ TEST(ordering, Success) TEST(states, Success) { - HardFork hf; + TestDB db; + HardFork hf(db); + ASSERT_TRUE(hf.add(1, 0, 0)); ASSERT_TRUE(hf.add(2, BLOCKS_PER_YEAR, SECONDS_PER_YEAR)); ASSERT_TRUE(hf.get_state(0) == HardFork::Ready); @@ -94,15 +195,20 @@ TEST(states, Success) TEST(steps_asap, Success) { - HardFork hf(1,1,1,1); + TestDB db; + HardFork hf(db, 1,1,1,1); // v h t + ASSERT_TRUE(hf.add(1, 0, 0)); ASSERT_TRUE(hf.add(4, 2, 1)); ASSERT_TRUE(hf.add(7, 4, 2)); ASSERT_TRUE(hf.add(9, 6, 3)); + hf.init(); - for (uint64_t h = 0; h < 10; ++h) - hf.add(mkblock(10), h); + for (uint64_t h = 0; h < 10; ++h) { + db.add_block(mkblock(10), 0, 0, 0, crypto::hash()); + ASSERT_TRUE(hf.add(db.get_block_from_height(h), h)); + } ASSERT_EQ(hf.get(0), 1); ASSERT_EQ(hf.get(1), 1); @@ -114,195 +220,142 @@ TEST(steps_asap, Success) ASSERT_EQ(hf.get(7), 9); ASSERT_EQ(hf.get(8), 9); ASSERT_EQ(hf.get(9), 9); - ASSERT_EQ(hf.get(100000), 9); } TEST(steps_1, Success) { - HardFork hf(1,1,1,1); + TestDB db; + HardFork hf(db, 1,1,1,1); - // v h t + ASSERT_TRUE(hf.add(1, 0, 0)); for (int n = 1 ; n < 10; ++n) ASSERT_TRUE(hf.add(n+1, n, n)); + hf.init(); + + for (uint64_t h = 0 ; h < 10; ++h) { + db.add_block(mkblock(h+1), 0, 0, 0, crypto::hash()); + ASSERT_TRUE(hf.add(db.get_block_from_height(h), h)); + } for (uint64_t h = 0; h < 10; ++h) { - hf.add(mkblock(h+1), h); ASSERT_EQ(hf.get(h), h+1); } } -class TestDB: public BlockchainDB { -public: - virtual void open(const std::string& filename, const int db_flags = 0) {} - virtual void close() {} - virtual void sync() {} - virtual void reset() {} - virtual std::vector<std::string> get_filenames() const { return std::vector<std::string>(); } - virtual std::string get_db_name() const { return std::string(); } - virtual bool lock() { return true; } - virtual void unlock() { } - virtual void batch_start(uint64_t batch_num_blocks=0) {} - virtual void batch_stop() {} - virtual void set_batch_transactions(bool) {} - virtual bool block_exists(const crypto::hash& h) const { return false; } - virtual block get_block(const crypto::hash& h) const { return block(); } - virtual uint64_t get_block_height(const crypto::hash& h) const { return 0; } - virtual block_header get_block_header(const crypto::hash& h) const { return block_header(); } - virtual uint64_t get_block_timestamp(const uint64_t& height) const { return 0; } - virtual uint64_t get_top_block_timestamp() const { return 0; } - virtual size_t get_block_size(const uint64_t& height) const { return 128; } - virtual difficulty_type get_block_cumulative_difficulty(const uint64_t& height) const { return 10; } - virtual difficulty_type get_block_difficulty(const uint64_t& height) const { return 0; } - virtual uint64_t get_block_already_generated_coins(const uint64_t& height) const { return 10000000000; } - virtual crypto::hash get_block_hash_from_height(const uint64_t& height) const { return crypto::hash(); } - virtual std::vector<block> get_blocks_range(const uint64_t& h1, const uint64_t& h2) const { return std::vector<block>(); } - virtual std::vector<crypto::hash> get_hashes_range(const uint64_t& h1, const uint64_t& h2) const { return std::vector<crypto::hash>(); } - virtual crypto::hash top_block_hash() const { return crypto::hash(); } - virtual block get_top_block() const { return block(); } - virtual uint64_t height() const { return blocks.size(); } - virtual bool tx_exists(const crypto::hash& h) const { return false; } - virtual uint64_t get_tx_unlock_time(const crypto::hash& h) const { return 0; } - virtual transaction get_tx(const crypto::hash& h) const { return transaction(); } - virtual uint64_t get_tx_count() const { return 0; } - virtual std::vector<transaction> get_tx_list(const std::vector<crypto::hash>& hlist) const { return std::vector<transaction>(); } - virtual uint64_t get_tx_block_height(const crypto::hash& h) const { return 0; } - virtual uint64_t get_num_outputs(const uint64_t& amount) const { return 1; } - virtual output_data_t get_output_key(const uint64_t& amount, const uint64_t& index) { return output_data_t(); } - virtual output_data_t get_output_key(const uint64_t& global_index) const { return output_data_t(); } - virtual tx_out get_output(const crypto::hash& h, const uint64_t& index) const { return tx_out(); } - virtual tx_out_index get_output_tx_and_index_from_global(const uint64_t& index) const { return tx_out_index(); } - virtual tx_out_index get_output_tx_and_index(const uint64_t& amount, const uint64_t& index) { return tx_out_index(); } - virtual void get_output_tx_and_index(const uint64_t& amount, const std::vector<uint64_t> &offsets, std::vector<tx_out_index> &indices) {} - virtual void get_output_key(const uint64_t &amount, const std::vector<uint64_t> &offsets, std::vector<output_data_t> &outputs) {} - virtual bool can_thread_bulk_indices() const { return false; } - virtual std::vector<uint64_t> get_tx_output_indices(const crypto::hash& h) const { return std::vector<uint64_t>(); } - virtual std::vector<uint64_t> get_tx_amount_output_indices(const crypto::hash& h) const { return std::vector<uint64_t>(); } - virtual bool has_key_image(const crypto::key_image& img) const { return false; } - virtual void remove_block() { blocks.pop_back(); } - virtual void add_transaction_data(const crypto::hash& blk_hash, const transaction& tx, const crypto::hash& tx_hash) {} - virtual void remove_transaction_data(const crypto::hash& tx_hash, const transaction& tx) {} - virtual void add_output(const crypto::hash& tx_hash, const tx_out& tx_output, const uint64_t& local_index, const uint64_t unlock_time) {} - virtual void remove_output(const tx_out& tx_output) {} - virtual void add_spent_key(const crypto::key_image& k_image) {} - virtual void remove_spent_key(const crypto::key_image& k_image) {} - - virtual void add_block( const block& blk - , const size_t& block_size - , const difficulty_type& cumulative_difficulty - , const uint64_t& coins_generated - , const crypto::hash& blk_hash - ) { - blocks.push_back(blk); - } - virtual block get_block_from_height(const uint64_t& height) const { - return blocks[height]; - } -private: - std::vector<block> blocks; -}; - TEST(reorganize, Same) { for (int history = 1; history <= 12; ++history) { - for (uint64_t checkpoint_period = 1; checkpoint_period <= 16; checkpoint_period++) { - HardFork hf(1, 1, 1, history, 100, checkpoint_period); - TestDB db; - - // v h t - ASSERT_TRUE(hf.add(4, 2, 1)); - ASSERT_TRUE(hf.add(7, 4, 2)); - ASSERT_TRUE(hf.add(9, 6, 3)); - - // index 0 1 2 3 4 5 6 7 8 9 - static const uint8_t block_versions[] = { 1, 1, 4, 4, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }; - for (uint64_t h = 0; h < 20; ++h) { - db.add_block(mkblock(block_versions[h]), 0, 0, 0, crypto::hash()); - hf.add(db.get_block_from_height(h), h); - } - - for (uint64_t rh = 0; rh < 20; ++rh) { - hf.reorganize_from_block_height(&db, rh); - for (int hh = 0; hh < 20; ++hh) { - uint8_t version = hh >= (history-1) ? block_versions[hh - (history-1)] : 1; - ASSERT_EQ(hf.get(hh), version); - } - ASSERT_EQ(hf.get(100000), 9); - } - } - } -} - -TEST(reorganize, Changed) -{ - int history = 4; - for (uint64_t checkpoint_period = 1; checkpoint_period <= 16; checkpoint_period++) { - HardFork hf(1, 1, 1, 4, 100, checkpoint_period); TestDB db; + HardFork hf(db, 1, 1, 1, history, 100); // v h t + ASSERT_TRUE(hf.add(1, 0, 0)); ASSERT_TRUE(hf.add(4, 2, 1)); ASSERT_TRUE(hf.add(7, 4, 2)); ASSERT_TRUE(hf.add(9, 6, 3)); + hf.init(); // index 0 1 2 3 4 5 6 7 8 9 static const uint8_t block_versions[] = { 1, 1, 4, 4, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }; for (uint64_t h = 0; h < 20; ++h) { db.add_block(mkblock(block_versions[h]), 0, 0, 0, crypto::hash()); - hf.add(db.get_block_from_height(h), h); + ASSERT_TRUE(hf.add(db.get_block_from_height(h), h)); } for (uint64_t rh = 0; rh < 20; ++rh) { - hf.reorganize_from_block_height(&db, rh); + hf.reorganize_from_block_height(rh); for (int hh = 0; hh < 20; ++hh) { uint8_t version = hh >= (history-1) ? block_versions[hh - (history-1)] : 1; ASSERT_EQ(hf.get(hh), version); } - ASSERT_EQ(hf.get(100000), 9); } + } +} - // delay a bit for 9, and go back to 1 to check it stays at 9 - static const uint8_t block_versions_new[] = { 1, 1, 4, 4, 7, 7, 4, 7, 7, 7, 9, 9, 9, 9, 9, 1, 1, 1, 1, 1 }; - static const uint8_t expected_versions_new[] = { 1, 1, 1, 1, 1, 4, 4, 4, 4, 4, 7, 7, 7, 9, 9, 9, 9, 9, 9, 9 }; - for (uint64_t h = 3; h < 20; ++h) { - db.remove_block(); - } - ASSERT_EQ(db.height(), 3); - hf.reorganize_from_block_height(&db, 2); - for (uint64_t h = 3; h < 20; ++h) { - db.add_block(mkblock(block_versions_new[h]), 0, 0, 0, crypto::hash()); - hf.add(db.get_block_from_height(h), h); - } - for (int hh = 0; hh < 20; ++hh) { - ASSERT_EQ(hf.get(hh), expected_versions_new[hh]); +TEST(reorganize, Changed) +{ + int history = 4; + TestDB db; + HardFork hf(db, 1, 1, 1, 4, 100); + + // v h t + ASSERT_TRUE(hf.add(1, 0, 0)); + ASSERT_TRUE(hf.add(4, 2, 1)); + ASSERT_TRUE(hf.add(7, 4, 2)); + ASSERT_TRUE(hf.add(9, 6, 3)); + hf.init(); + + // index 0 1 2 3 4 5 6 7 8 9 + static const uint8_t block_versions[] = { 1, 1, 4, 4, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }; + for (uint64_t h = 0; h < 16; ++h) { + db.add_block(mkblock(block_versions[h]), 0, 0, 0, crypto::hash()); + ASSERT_TRUE (hf.add(db.get_block_from_height(h), h)); + } + + for (uint64_t rh = 0; rh < 16; ++rh) { + hf.reorganize_from_block_height(rh); + for (int hh = 0; hh < 16; ++hh) { + uint8_t version = hh >= (history-1) ? block_versions[hh - (history-1)] : 1; + ASSERT_EQ(hf.get(hh), version); } - ASSERT_EQ(hf.get(100000), 9); + } + + // delay a bit for 9, and go back to 1 to check it stays at 9 + static const uint8_t block_versions_new[] = { 1, 1, 4, 4, 7, 7, 4, 7, 7, 7, 9, 9, 9, 9, 9, 1 }; + static const uint8_t expected_versions_new[] = { 1, 1, 1, 1, 1, 4, 4, 4, 4, 4, 7, 7, 7, 9, 9, 9 }; + for (uint64_t h = 3; h < 16; ++h) { + db.remove_block(); + } + ASSERT_EQ(db.height(), 3); + hf.reorganize_from_block_height(2); + for (uint64_t h = 3; h < 16; ++h) { + db.add_block(mkblock(block_versions_new[h]), 0, 0, 0, crypto::hash()); + bool ret = hf.add(db.get_block_from_height(h), h); + ASSERT_EQ (ret, h < 15); + } + db.remove_block(); // last block added to the blockchain, but not hf + ASSERT_EQ(db.height(), 15); + for (int hh = 0; hh < 15; ++hh) { + ASSERT_EQ(hf.get(hh), expected_versions_new[hh]); } } TEST(voting, threshold) { for (int threshold = 87; threshold <= 88; ++threshold) { - HardFork hf(1, 1, 1, 8, threshold, 10); TestDB db; + HardFork hf(db, 1, 1, 1, 8, threshold); // v h t + ASSERT_TRUE(hf.add(1, 0, 0)); ASSERT_TRUE(hf.add(2, 2, 1)); + hf.init(); - for (uint64_t h = 0; h < 10; ++h) { + for (uint64_t h = 0; h <= 8; ++h) { uint8_t v = 1 + !!(h % 8); - hf.add(mkblock(v), h); - uint8_t expected = threshold == 88 ? 1 : h < 7 ? 1 : 2; - ASSERT_EQ(hf.get(h), expected); + db.add_block(mkblock(v), 0, 0, 0, crypto::hash()); + bool ret = hf.add(db.get_block_from_height(h), h); + if (h >= 8 && threshold == 87) { + ASSERT_FALSE(ret); + } + else { + ASSERT_TRUE(ret); + uint8_t expected = threshold == 88 ? 1 : h < 7 ? 1 : 2; + ASSERT_EQ(hf.get(h), expected); + } } } } TEST(new_blocks, denied) { - HardFork hf(1, 1, 1, 4, 50, 10); + TestDB db; + HardFork hf(db, 1, 1, 1, 4, 50); // v h t + ASSERT_TRUE(hf.add(1, 0, 0)); ASSERT_TRUE(hf.add(2, 2, 1)); + hf.init(); ASSERT_FALSE(hf.add(mkblock(0), 0)); ASSERT_TRUE(hf.add(mkblock(1), 0)); @@ -322,10 +375,13 @@ TEST(new_blocks, denied) TEST(new_version, early) { - HardFork hf(1, 1, 1, 4, 50, 10); + TestDB db; + HardFork hf(db, 1, 1, 1, 4, 50); // v h t + ASSERT_TRUE(hf.add(1, 0, 0)); ASSERT_TRUE(hf.add(2, 4, 1)); + hf.init(); ASSERT_FALSE(hf.add(mkblock(0), 0)); ASSERT_TRUE(hf.add(mkblock(2), 0)); @@ -342,12 +398,14 @@ TEST(new_version, early) TEST(reorganize, changed) { - HardFork hf(1, 1, 1, 4, 50, 10); TestDB db; + HardFork hf(db, 1, 1, 1, 4, 50); // v h t + ASSERT_TRUE(hf.add(1, 0, 0)); ASSERT_TRUE(hf.add(2, 2, 1)); ASSERT_TRUE(hf.add(3, 5, 2)); + hf.init(); #define ADD(v, h, a) \ do { \ @@ -376,10 +434,10 @@ TEST(reorganize, changed) // pop a few blocks and check current version goes back down db.remove_block(); - hf.reorganize_from_block_height(&db, 8); + hf.reorganize_from_block_height(8); ASSERT_EQ(hf.get_current_version(), 3); db.remove_block(); - hf.reorganize_from_block_height(&db, 7); + hf.reorganize_from_block_height(7); ASSERT_EQ(hf.get_current_version(), 2); db.remove_block(); ASSERT_EQ(hf.get_current_version(), 2); |