diff --git a/src/currency_core/blockchain_storage.cpp b/src/currency_core/blockchain_storage.cpp index 696e7422..e7632e63 100644 --- a/src/currency_core/blockchain_storage.cpp +++ b/src/currency_core/blockchain_storage.cpp @@ -70,7 +70,6 @@ using namespace currency; #endif #define BLOCK_POS_STRICT_SEQUENCE_LIMIT 20 -#define BLOCKCHAIN_FIRST_BLOCK_TIMESTAMP 1557342384 DISABLE_VS_WARNINGS(4267) @@ -111,7 +110,8 @@ blockchain_storage::blockchain_storage(tx_memory_pool& tx_pool) :m_db(nullptr, m m_is_reorganize_in_process(false), m_deinit_is_done(false), m_cached_next_pow_difficulty(0), - m_cached_next_pos_difficulty(0) + m_cached_next_pos_difficulty(0), + m_blockchain_launch_timestamp(0) { @@ -2941,16 +2941,31 @@ bool blockchain_storage::find_blockchain_supplement(const std::list 2) + { + m_blockchain_launch_timestamp = m_db_blocks[1]->bl.timestamp; + } + return m_blockchain_launch_timestamp; +} +//------------------------------------------------------------------ bool blockchain_storage::get_est_height_from_date(uint64_t date, uint64_t& res_h)const { CRITICAL_REGION_LOCAL(m_read_lock); #define GET_EST_HEIGHT_FROM_DATE_THRESHOLD 1440 - if (date < BLOCKCHAIN_FIRST_BLOCK_TIMESTAMP) - return false; + if (date < get_blockchain_launch_timestamp()) + { + res_h = 0; + return true; + } - uint64_t calculated_estimated_height = (date - BLOCKCHAIN_FIRST_BLOCK_TIMESTAMP) / DIFFICULTY_TOTAL_TARGET; + uint64_t calculated_estimated_height = (date - get_blockchain_launch_timestamp()) / DIFFICULTY_TOTAL_TARGET; if (date > m_db_blocks[m_db_blocks.size() - 1]->bl.timestamp) { @@ -2965,7 +2980,7 @@ bool blockchain_storage::get_est_height_from_date(uint64_t date, uint64_t& res_h else { //likely impossible, but just in case - res_h = 1; + res_h = 0; } } @@ -2987,7 +3002,13 @@ bool blockchain_storage::get_est_height_from_date(uint64_t date, uint64_t& res_h if (ts > high_boundary) { //we moved too much forward - calculated_estimated_height -= (ts - aim) / DIFFICULTY_TOTAL_TARGET; + uint64_t offset = (ts - aim) / DIFFICULTY_TOTAL_TARGET; + if (offset > calculated_estimated_height) + { + res_h = 0; + break; + } + calculated_estimated_height -= offset; } else if (ts < low_boundary) { diff --git a/src/currency_core/blockchain_storage.h b/src/currency_core/blockchain_storage.h index 02f03839..7802a8bc 100644 --- a/src/currency_core/blockchain_storage.h +++ b/src/currency_core/blockchain_storage.h @@ -535,7 +535,7 @@ namespace currency mutable uint64_t m_current_fee_median_effective_index; bool m_is_reorganize_in_process; mutable std::atomic m_deinit_is_done; - + mutable uint64_t m_blockchain_launch_timestamp; bool init_tx_fee_median(); bool update_tx_fee_median(); @@ -616,6 +616,7 @@ namespace currency void pop_block_from_per_block_increments(uint64_t height_); void calculate_local_gindex_lookup_table_for_height(uint64_t split_height, std::map& increments) const; void do_erase_altblock(alt_chain_container::iterator it); + uint64_t get_blockchain_launch_timestamp()const; diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index dbf724a0..5e89ae22 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -1156,7 +1156,7 @@ void wallet2::pull_blocks(size_t& blocks_added, std::atomic& stop) currency::COMMAND_RPC_GET_BLOCKS_DIRECT::response res = AUTO_VAL_INIT(res); req.minimum_height = get_wallet_minimum_height(); - get_short_chain_history(req.block_ids); + m_chain.get_short_chain_history(req.block_ids); bool r = m_core_proxy->call_COMMAND_RPC_GET_BLOCKS_DIRECT(req, res); if (!r) throw error::no_connection_to_daemon(LOCATION_STR, "getblocks.bin"); @@ -1179,7 +1179,7 @@ void wallet2::pull_blocks(size_t& blocks_added, std::atomic& stop) reset_all(); m_chain.set_genesis(new_genesis_id); WLT_LOG_MAGENTA("New genesis set for wallet: " << new_genesis_id, LOG_LEVEL_0); - get_short_chain_history(req.block_ids); + m_chain.get_short_chain_history(req.block_ids); //req.block_ids.push_back(new_genesis_id); bool r = m_core_proxy->call_COMMAND_RPC_GET_BLOCKS_DIRECT(req, res); THROW_IF_TRUE_WALLET_EX(!r, error::no_connection_to_daemon, "getblocks.bin"); @@ -1203,7 +1203,7 @@ void wallet2::handle_pulled_blocks(size_t& blocks_added, std::atomic& stop currency::COMMAND_RPC_GET_BLOCKS_DIRECT::response& res) { size_t current_index = res.start_height; - + bool been_matched_block = false; if (res.start_height == 0 && get_blockchain_current_size() == 1 && !res.blocks.empty()) { const currency::block& genesis = res.blocks.front().block_ptr->bl; @@ -1211,10 +1211,10 @@ void wallet2::handle_pulled_blocks(size_t& blocks_added, std::atomic& stop process_genesis_if_needed(genesis); res.blocks.pop_front(); ++current_index; + been_matched_block = true; } uint64_t last_matched_index = 0; - bool been_matched_block = false; for(const auto& bl_entry: res.blocks) { if (stop) @@ -1249,7 +1249,7 @@ void wallet2::handle_pulled_blocks(size_t& blocks_added, std::atomic& stop bool block_found = false; bool block_matched = false; bool full_reset_needed = false; - check_if_block_matched(height, bl_id, block_found, block_matched, full_reset_needed); + m_chain.check_if_block_matched(height, bl_id, block_found, block_matched, full_reset_needed); if (block_found && block_matched) { //block matched in that number diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 6aaab37e..a9e214f3 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -900,8 +900,8 @@ private: void check_and_throw_if_self_directed_tx_with_payment_id_requested(const construct_tx_param& ctp); void push_new_block_id(const crypto::hash& id, uint64_t height); bool lookup_item_around(uint64_t i, std::pair& result); - void get_short_chain_history(std::list& ids); - void check_if_block_matched(uint64_t i, const crypto::hash& id, bool& block_found, bool& block_matched, bool& full_reset_needed); + //void get_short_chain_history(std::list& ids); + //void check_if_block_matched(uint64_t i, const crypto::hash& id, bool& block_found, bool& block_matched, bool& full_reset_needed); uint64_t detach_from_block_ids(uint64_t height); uint64_t get_wallet_minimum_height(); diff --git a/src/wallet/wallet_chain_shortener.cpp b/src/wallet/wallet_chain_shortener.cpp index e8778b66..3d3bba28 100644 --- a/src/wallet/wallet_chain_shortener.cpp +++ b/src/wallet/wallet_chain_shortener.cpp @@ -12,7 +12,7 @@ #define WALLET_EVERY_100_BLOCKS_SIZE 144 #define WALLET_EVERY_1000_BLOCKS_SIZE 144 -void exception_handler(){} +static void exception_handler(){} wallet_chain_shortener::wallet_chain_shortener(): m_genesis(currency::gdefault_genesis)