From eab8a51bb7a30813d4cd8f570486fbee034acdb8 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Mon, 22 May 2023 15:30:57 +0200 Subject: [PATCH] SideChain: tweaked `m_seenBlocks` cleanup logic --- src/side_chain.cpp | 28 +++++++++++++++++++++++++++- src/side_chain.h | 2 ++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/side_chain.cpp b/src/side_chain.cpp index 57275e6..e831e49 100644 --- a/src/side_chain.cpp +++ b/src/side_chain.cpp @@ -490,6 +490,28 @@ void SideChain::unsee_block(const PoolBlock& block) m_seenBlocks.erase(block.get_full_id()); } +size_t SideChain::cleanup_seen_blocks() +{ + size_t n = 0; + + MutexLock lock(m_seenBlocksLock); + + // Forget seen blocks that weren't added for any reason + hash h; + for (auto i = m_seenBlocks.begin(); i != m_seenBlocks.end();) { + memcpy(h.h, i->data(), HASH_SIZE); + if (m_blocksById.count(h) == 0) { + i = m_seenBlocks.erase(i); + ++n; + } + else { + ++i; + } + } + + return n; +} + bool SideChain::add_external_block(PoolBlock& block, std::vector& missing_blocks) { if (block.m_difficulty < m_minDifficulty) { @@ -2003,7 +2025,6 @@ void SideChain::prune_old_blocks() auto it2 = m_blocksById.find(block->m_sidechainId); if (it2 != m_blocksById.end()) { m_blocksById.erase(it2); - unsee_block(*block); delete block; ++num_blocks_pruned; } @@ -2035,6 +2056,11 @@ void SideChain::prune_old_blocks() // Pre-calc workers are not needed anymore finish_precalc(); + const size_t n = cleanup_seen_blocks(); + if (n > 0) { + LOGINFO(5, "pruned " << n << " seen blocks"); + } + #ifdef DEV_TEST_SYNC if (m_pool && m_precalcFinished.load() && (cur_time >= m_synchronizedTime + 120)) { LOGINFO(0, log::LightGreen() << "[DEV] Synchronization finished successfully, stopping P2Pool now"); diff --git a/src/side_chain.h b/src/side_chain.h index 7728dbb..c9d0551 100644 --- a/src/side_chain.h +++ b/src/side_chain.h @@ -48,6 +48,8 @@ public: bool block_seen(const PoolBlock& block); void unsee_block(const PoolBlock& block); + size_t cleanup_seen_blocks(); + bool add_external_block(PoolBlock& block, std::vector& missing_blocks); bool add_block(const PoolBlock& block); void get_missing_blocks(std::vector& missing_blocks) const;