From f85498422d9aec79411c739835ab4a5ed27b1688 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Sat, 12 Sep 2015 11:14:00 +0100 Subject: [PATCH] blockchain: use the new hardfork class --- src/cryptonote_core/blockchain.cpp | 26 +++++++++++++++++++++++--- src/cryptonote_core/blockchain.h | 7 +++++-- src/cryptonote_core/cryptonote_basic.h | 4 +--- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index 09f8f8d7f..81612f9a5 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -118,6 +118,11 @@ void Blockchain::serialize(archive_t & ar, const unsigned int version) } } + if (version > 12) + { + 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); } //------------------------------------------------------------------ @@ -706,6 +711,8 @@ bool Blockchain::rollback_blockchain_switching(std::list& 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); + LOG_PRINT_L1("Rollback to height " << rollback_height << " was successful."); if (original_chain.size()) { @@ -803,6 +810,8 @@ bool Blockchain::switch_to_alternative_blockchain(std::listheight(), LOG_LEVEL_0); return true; } @@ -972,12 +981,13 @@ bool Blockchain::create_block_template(block& b, const account_public_address& m uint64_t already_generated_coins; CRITICAL_REGION_BEGIN(m_blockchain_lock); - b.major_version = CURRENT_BLOCK_MAJOR_VERSION; - b.minor_version = CURRENT_BLOCK_MINOR_VERSION; + height = m_db->height(); + + b.major_version = m_hardfork.get_ideal_version(); + b.minor_version = 0; b.prev_id = get_tail_id(); b.timestamp = time(NULL); - height = m_db->height(); diffic = get_difficulty_for_next_block(); CHECK_AND_ASSERT_MES(diffic, false, "difficulty owverhead."); @@ -2245,6 +2255,13 @@ bool Blockchain::handle_block_to_main_chain(const block& bl, const crypto::hash& CRITICAL_REGION_LOCAL(m_blockchain_lock); TIME_MEASURE_START(t1); + // this is a cheap test + 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()); + return false; + } + if(bl.prev_id != get_tail_id()) { LOG_PRINT_L1("Block with id: " << id << std::endl << "has wrong prev_id: " << bl.prev_id << std::endl << "expected: " << get_tail_id()); @@ -2521,6 +2538,9 @@ 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); + 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) { diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h index 7246c1d83..4693d4164 100644 --- a/src/cryptonote_core/blockchain.h +++ b/src/cryptonote_core/blockchain.h @@ -52,6 +52,7 @@ #include "verification_context.h" #include "crypto/hash.h" #include "checkpoints.h" +#include "hardfork.h" #include "blockchain_db/blockchain_db.h" namespace cryptonote @@ -227,6 +228,8 @@ namespace cryptonote std::atomic m_is_blockchain_storing; bool m_enforce_dns_checkpoints; + HardFork m_hardfork; + template 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; bool check_tx_input(const txin_to_key& txin, const crypto::hash& tx_prefix_hash, const std::vector& sig, std::vector &output_keys, uint64_t* pmax_related_block_height); @@ -268,10 +271,10 @@ namespace cryptonote /* */ /************************************************************************/ - #define CURRENT_BLOCKCHAIN_STORAGE_ARCHIVE_VER 12 + #define CURRENT_BLOCKCHAIN_ARCHIVE_VER 13 //------------------------------------------------------------------ } // namespace cryptonote -BOOST_CLASS_VERSION(cryptonote::Blockchain, CURRENT_BLOCKCHAIN_STORAGE_ARCHIVE_VER) +BOOST_CLASS_VERSION(cryptonote::Blockchain, CURRENT_BLOCKCHAIN_ARCHIVE_VER) diff --git a/src/cryptonote_core/cryptonote_basic.h b/src/cryptonote_core/cryptonote_basic.h index 07745bf0d..d3db50068 100644 --- a/src/cryptonote_core/cryptonote_basic.h +++ b/src/cryptonote_core/cryptonote_basic.h @@ -182,7 +182,6 @@ namespace cryptonote FIELD(extra) END_SERIALIZE() - protected: transaction_prefix(){} }; @@ -278,7 +277,7 @@ namespace cryptonote /************************************************************************/ struct block_header { - uint8_t major_version; + uint8_t major_version; // now used as a voting mechanism, rather than how this particular block is built uint8_t minor_version; uint64_t timestamp; crypto::hash prev_id; @@ -286,7 +285,6 @@ namespace cryptonote BEGIN_SERIALIZE() VARINT_FIELD(major_version) - if(major_version > CURRENT_BLOCK_MAJOR_VERSION) return false; VARINT_FIELD(minor_version) VARINT_FIELD(timestamp) FIELD(prev_id)