From 031a1c2eea718a69ac5c4160c2e334161f941420 Mon Sep 17 00:00:00 2001 From: SChernykh <15806605+SChernykh@users.noreply.github.com> Date: Tue, 29 Oct 2024 12:36:39 +0100 Subject: [PATCH] SideChain: more fine-grained locks --- src/side_chain.cpp | 11 +++++++++-- src/side_chain.h | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/side_chain.cpp b/src/side_chain.cpp index 820912f..041ae20 100644 --- a/src/side_chain.cpp +++ b/src/side_chain.cpp @@ -94,6 +94,7 @@ SideChain::SideChain(p2pool* pool, NetworkType type, const char* pool_name) uv_mutex_init_checked(&m_seenWalletsLock); uv_mutex_init_checked(&m_incomingBlocksLock); uv_rwlock_init_checked(&m_curDifficultyLock); + uv_rwlock_init_checked(&m_watchBlockLock); m_difficultyData.reserve(m_chainWindowSize); @@ -213,6 +214,7 @@ SideChain::~SideChain() uv_mutex_destroy(&m_seenWalletsLock); uv_mutex_destroy(&m_incomingBlocksLock); uv_rwlock_destroy(&m_curDifficultyLock); + uv_rwlock_destroy(&m_watchBlockLock); for (const auto& it : m_blocksById) { delete it.second; @@ -629,7 +631,8 @@ bool SideChain::add_external_block(PoolBlock& block, std::vector& missing_ missing_blocks.clear(); { - WriteLock lock(m_sidechainLock); + ReadLock lock(m_sidechainLock); + if (!block.m_parent.empty() && (m_blocksById.find(block.m_parent) == m_blocksById.end())) { missing_blocks.push_back(block.m_parent); } @@ -639,6 +642,10 @@ bool SideChain::add_external_block(PoolBlock& block, std::vector& missing_ missing_blocks.push_back(h); } } + } + + { + WriteLock lock(m_watchBlockLock); if (block.m_merkleRoot == m_watchBlockMerkleRoot) { const Wallet& w = m_pool->params().m_wallet; @@ -752,7 +759,7 @@ PoolBlock* SideChain::find_block_by_merkle_root(const root_hash& merkle_root) co void SideChain::watch_mainchain_block(const ChainMain& data, const hash& possible_merkle_root) { - WriteLock lock(m_sidechainLock); + WriteLock lock(m_watchBlockLock); m_watchBlock = data; m_watchBlockMerkleRoot = possible_merkle_root; } diff --git a/src/side_chain.h b/src/side_chain.h index 3c5b0bc..7f3bd45 100644 --- a/src/side_chain.h +++ b/src/side_chain.h @@ -143,6 +143,7 @@ private: mutable uv_rwlock_t m_curDifficultyLock; difficulty_type m_curDifficulty; + uv_rwlock_t m_watchBlockLock; ChainMain m_watchBlock; hash m_watchBlockMerkleRoot;