Merge pull request #6650

0fd6cce blockchain: fix timestamp/difficulty cache getting out of sync (moneromooo-monero)
This commit is contained in:
luigi1111 2020-07-08 20:38:03 -05:00
commit 9f93a1b632
No known key found for this signature in database
GPG Key ID: F4ACA0183641E010
2 changed files with 17 additions and 1 deletions

View File

@ -86,7 +86,7 @@ DISABLE_VS_WARNINGS(4267)
//------------------------------------------------------------------ //------------------------------------------------------------------
Blockchain::Blockchain(tx_memory_pool& tx_pool) : Blockchain::Blockchain(tx_memory_pool& tx_pool) :
m_db(), m_tx_pool(tx_pool), m_hardfork(NULL), m_timestamps_and_difficulties_height(0), m_current_block_cumul_weight_limit(0), m_current_block_cumul_weight_median(0), m_db(), m_tx_pool(tx_pool), m_hardfork(NULL), m_timestamps_and_difficulties_height(0), m_reset_timestamps_and_difficulties_height(true), m_current_block_cumul_weight_limit(0), m_current_block_cumul_weight_median(0),
m_enforce_dns_checkpoints(false), m_max_prepare_blocks_threads(4), m_db_sync_on_blocks(true), m_db_sync_threshold(1), m_db_sync_mode(db_async), m_db_default_sync(false), m_fast_sync(true), m_show_time_stats(false), m_sync_counter(0), m_bytes_to_sync(0), m_cancel(false), m_enforce_dns_checkpoints(false), m_max_prepare_blocks_threads(4), m_db_sync_on_blocks(true), m_db_sync_threshold(1), m_db_sync_mode(db_async), m_db_default_sync(false), m_fast_sync(true), m_show_time_stats(false), m_sync_counter(0), m_bytes_to_sync(0), m_cancel(false),
m_long_term_block_weights_window(CRYPTONOTE_LONG_TERM_BLOCK_WEIGHT_WINDOW_SIZE), m_long_term_block_weights_window(CRYPTONOTE_LONG_TERM_BLOCK_WEIGHT_WINDOW_SIZE),
m_long_term_effective_median_block_weight(0), m_long_term_effective_median_block_weight(0),
@ -427,6 +427,7 @@ bool Blockchain::init(BlockchainDB* db, const network_type nettype, bool offline
if (num_popped_blocks > 0) if (num_popped_blocks > 0)
{ {
m_timestamps_and_difficulties_height = 0; m_timestamps_and_difficulties_height = 0;
m_reset_timestamps_and_difficulties_height = true;
m_hardfork->reorganize_from_chain_height(get_current_blockchain_height()); m_hardfork->reorganize_from_chain_height(get_current_blockchain_height());
uint64_t top_block_height; uint64_t top_block_height;
crypto::hash top_block_hash = get_tail_id(top_block_height); crypto::hash top_block_hash = get_tail_id(top_block_height);
@ -567,6 +568,7 @@ block Blockchain::pop_block_from_blockchain()
CRITICAL_REGION_LOCAL(m_blockchain_lock); CRITICAL_REGION_LOCAL(m_blockchain_lock);
m_timestamps_and_difficulties_height = 0; m_timestamps_and_difficulties_height = 0;
m_reset_timestamps_and_difficulties_height = true;
block popped_block; block popped_block;
std::vector<transaction> popped_txs; std::vector<transaction> popped_txs;
@ -644,6 +646,7 @@ bool Blockchain::reset_and_set_genesis_block(const block& b)
LOG_PRINT_L3("Blockchain::" << __func__); LOG_PRINT_L3("Blockchain::" << __func__);
CRITICAL_REGION_LOCAL(m_blockchain_lock); CRITICAL_REGION_LOCAL(m_blockchain_lock);
m_timestamps_and_difficulties_height = 0; m_timestamps_and_difficulties_height = 0;
m_reset_timestamps_and_difficulties_height = true;
invalidate_block_template_cache(); invalidate_block_template_cache();
m_db->reset(); m_db->reset();
m_db->drop_alt_blocks(); m_db->drop_alt_blocks();
@ -858,6 +861,8 @@ start:
// pop the oldest one from the list. This only requires 1x read per height instead // pop the oldest one from the list. This only requires 1x read per height instead
// of doing 735 (DIFFICULTY_BLOCKS_COUNT). // of doing 735 (DIFFICULTY_BLOCKS_COUNT).
bool check = false; bool check = false;
if (m_reset_timestamps_and_difficulties_height)
m_timestamps_and_difficulties_height = 0;
if (m_timestamps_and_difficulties_height != 0 && ((height - m_timestamps_and_difficulties_height) == 1) && m_timestamps.size() >= DIFFICULTY_BLOCKS_COUNT) if (m_timestamps_and_difficulties_height != 0 && ((height - m_timestamps_and_difficulties_height) == 1) && m_timestamps.size() >= DIFFICULTY_BLOCKS_COUNT)
{ {
uint64_t index = height - 1; uint64_t index = height - 1;
@ -981,6 +986,7 @@ bool Blockchain::rollback_blockchain_switching(std::list<block>& original_chain,
} }
m_timestamps_and_difficulties_height = 0; m_timestamps_and_difficulties_height = 0;
m_reset_timestamps_and_difficulties_height = true;
// remove blocks from blockchain until we get back to where we should be. // remove blocks from blockchain until we get back to where we should be.
while (m_db->height() != rollback_height) while (m_db->height() != rollback_height)
@ -1017,6 +1023,7 @@ bool Blockchain::switch_to_alternative_blockchain(std::list<block_extended_info>
CRITICAL_REGION_LOCAL(m_blockchain_lock); CRITICAL_REGION_LOCAL(m_blockchain_lock);
m_timestamps_and_difficulties_height = 0; m_timestamps_and_difficulties_height = 0;
m_reset_timestamps_and_difficulties_height = true;
// if empty alt chain passed (not sure how that could happen), return false // if empty alt chain passed (not sure how that could happen), return false
CHECK_AND_ASSERT_MES(alt_chain.size(), false, "switch_to_alternative_blockchain: empty chain passed"); CHECK_AND_ASSERT_MES(alt_chain.size(), false, "switch_to_alternative_blockchain: empty chain passed");
@ -1706,6 +1713,7 @@ bool Blockchain::handle_alternative_block(const block& b, const crypto::hash& id
LOG_PRINT_L3("Blockchain::" << __func__); LOG_PRINT_L3("Blockchain::" << __func__);
CRITICAL_REGION_LOCAL(m_blockchain_lock); CRITICAL_REGION_LOCAL(m_blockchain_lock);
m_timestamps_and_difficulties_height = 0; m_timestamps_and_difficulties_height = 0;
m_reset_timestamps_and_difficulties_height = true;
uint64_t block_height = get_block_height(b); uint64_t block_height = get_block_height(b);
if(0 == block_height) if(0 == block_height)
{ {
@ -4385,7 +4393,14 @@ bool Blockchain::cleanup_handle_incoming_blocks(bool force_sync)
try try
{ {
if (m_batch_success) if (m_batch_success)
{
m_db->batch_stop(); m_db->batch_stop();
if (m_reset_timestamps_and_difficulties_height)
{
m_timestamps_and_difficulties_height = 0;
m_reset_timestamps_and_difficulties_height = false;
}
}
else else
m_db->batch_abort(); m_db->batch_abort();
success = true; success = true;

View File

@ -1067,6 +1067,7 @@ namespace cryptonote
std::vector<uint64_t> m_timestamps; std::vector<uint64_t> m_timestamps;
std::vector<difficulty_type> m_difficulties; std::vector<difficulty_type> m_difficulties;
uint64_t m_timestamps_and_difficulties_height; uint64_t m_timestamps_and_difficulties_height;
bool m_reset_timestamps_and_difficulties_height;
uint64_t m_long_term_block_weights_window; uint64_t m_long_term_block_weights_window;
uint64_t m_long_term_effective_median_block_weight; uint64_t m_long_term_effective_median_block_weight;
mutable crypto::hash m_long_term_block_weights_cache_tip_hash; mutable crypto::hash m_long_term_block_weights_cache_tip_hash;