From f24cbc5245246384e604beb097207edd94fa6f61 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Wed, 6 Jun 2018 10:37:26 +0100 Subject: blockchain: fix deadlock with the difficulty cache --- src/cryptonote_core/blockchain.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index 54d8fac31..52befc474 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -807,16 +807,18 @@ difficulty_type Blockchain::get_difficulty_for_next_block() { LOG_PRINT_L3("Blockchain::" << __func__); - CRITICAL_REGION_LOCAL(m_difficulty_lock); - // we can call this without the blockchain lock, it might just give us - // something a bit out of date, but that's fine since anything which - // requires the blockchain lock will have acquired it in the first place, - // and it will be unlocked only when called from the getinfo RPC crypto::hash top_hash = get_tail_id(); - if (top_hash == m_difficulty_for_next_block_top_hash) - return m_difficulty_for_next_block; + { + CRITICAL_REGION_LOCAL(m_difficulty_lock); + // we can call this without the blockchain lock, it might just give us + // something a bit out of date, but that's fine since anything which + // requires the blockchain lock will have acquired it in the first place, + // and it will be unlocked only when called from the getinfo RPC + if (top_hash == m_difficulty_for_next_block_top_hash) + return m_difficulty_for_next_block; + } - CRITICAL_REGION_LOCAL1(m_blockchain_lock); + CRITICAL_REGION_LOCAL(m_blockchain_lock); std::vector timestamps; std::vector difficulties; auto height = m_db->height(); @@ -860,6 +862,8 @@ difficulty_type Blockchain::get_difficulty_for_next_block() } size_t target = get_difficulty_target(); difficulty_type diff = next_difficulty(timestamps, difficulties, target); + + CRITICAL_REGION_LOCAL1(m_difficulty_lock); m_difficulty_for_next_block_top_hash = top_hash; m_difficulty_for_next_block = diff; return diff; -- cgit v1.2.3