SideChain: more fine-grained locks

This commit is contained in:
SChernykh
2024-10-29 12:36:39 +01:00
parent 3c4cf098a9
commit 031a1c2eea
2 changed files with 10 additions and 2 deletions
+9 -2
View File
@@ -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_seenWalletsLock);
uv_mutex_init_checked(&m_incomingBlocksLock); uv_mutex_init_checked(&m_incomingBlocksLock);
uv_rwlock_init_checked(&m_curDifficultyLock); uv_rwlock_init_checked(&m_curDifficultyLock);
uv_rwlock_init_checked(&m_watchBlockLock);
m_difficultyData.reserve(m_chainWindowSize); m_difficultyData.reserve(m_chainWindowSize);
@@ -213,6 +214,7 @@ SideChain::~SideChain()
uv_mutex_destroy(&m_seenWalletsLock); uv_mutex_destroy(&m_seenWalletsLock);
uv_mutex_destroy(&m_incomingBlocksLock); uv_mutex_destroy(&m_incomingBlocksLock);
uv_rwlock_destroy(&m_curDifficultyLock); uv_rwlock_destroy(&m_curDifficultyLock);
uv_rwlock_destroy(&m_watchBlockLock);
for (const auto& it : m_blocksById) { for (const auto& it : m_blocksById) {
delete it.second; delete it.second;
@@ -629,7 +631,8 @@ bool SideChain::add_external_block(PoolBlock& block, std::vector<hash>& missing_
missing_blocks.clear(); 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())) { if (!block.m_parent.empty() && (m_blocksById.find(block.m_parent) == m_blocksById.end())) {
missing_blocks.push_back(block.m_parent); missing_blocks.push_back(block.m_parent);
} }
@@ -639,6 +642,10 @@ bool SideChain::add_external_block(PoolBlock& block, std::vector<hash>& missing_
missing_blocks.push_back(h); missing_blocks.push_back(h);
} }
} }
}
{
WriteLock lock(m_watchBlockLock);
if (block.m_merkleRoot == m_watchBlockMerkleRoot) { if (block.m_merkleRoot == m_watchBlockMerkleRoot) {
const Wallet& w = m_pool->params().m_wallet; 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) 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_watchBlock = data;
m_watchBlockMerkleRoot = possible_merkle_root; m_watchBlockMerkleRoot = possible_merkle_root;
} }
+1
View File
@@ -143,6 +143,7 @@ private:
mutable uv_rwlock_t m_curDifficultyLock; mutable uv_rwlock_t m_curDifficultyLock;
difficulty_type m_curDifficulty; difficulty_type m_curDifficulty;
uv_rwlock_t m_watchBlockLock;
ChainMain m_watchBlock; ChainMain m_watchBlock;
hash m_watchBlockMerkleRoot; hash m_watchBlockMerkleRoot;