diff --git a/src/block_cache.cpp b/src/block_cache.cpp index 2aaa87d..2cac247 100644 --- a/src/block_cache.cpp +++ b/src/block_cache.cpp @@ -154,6 +154,7 @@ struct BlockCache::Impl : public nocopy_nomove BlockCache::BlockCache() : m_impl(new Impl()) , m_flushRunning(0) + , m_storeIndex(0) { } @@ -171,7 +172,7 @@ void BlockCache::store(const PoolBlock& block) return; } - uint8_t* data = m_impl->m_data + (static_cast(block.m_sidechainHeight % NUM_BLOCKS) * BLOCK_SIZE); + uint8_t* data = m_impl->m_data + (static_cast((m_storeIndex++) % NUM_BLOCKS) * BLOCK_SIZE); *reinterpret_cast(data) = static_cast(n1 + n2); memcpy(data + sizeof(uint32_t), block.m_mainChainData.data(), n1); diff --git a/src/block_cache.h b/src/block_cache.h index d9fd27b..ab22b32 100644 --- a/src/block_cache.h +++ b/src/block_cache.h @@ -37,6 +37,7 @@ private: struct Impl; Impl* m_impl; std::atomic m_flushRunning; + std::atomic m_storeIndex; }; } // namespace p2pool diff --git a/src/side_chain.cpp b/src/side_chain.cpp index 199121f..286cb5e 100644 --- a/src/side_chain.cpp +++ b/src/side_chain.cpp @@ -514,11 +514,6 @@ void SideChain::add_block(const PoolBlock& block) ", verified = " << (block.m_verified ? 1 : 0) ); - // Save it for faster syncing on the next p2pool start - if (p2pServer()) { - p2pServer()->store_in_cache(block); - } - PoolBlock* new_block = new PoolBlock(block); MutexLock lock(m_sidechainLock); @@ -541,6 +536,11 @@ void SideChain::add_block(const PoolBlock& block) if (new_block->m_verified) { if (!new_block->m_invalid) { update_chain_tip(new_block); + + // Save it for faster syncing on the next p2pool start + if (P2PServer* server = p2pServer()) { + server->store_in_cache(*new_block); + } } } else { @@ -1013,17 +1013,19 @@ void SideChain::verify_loop(PoolBlock* block) ", height " << block->m_sidechainHeight); } + P2PServer* server = p2pServer(); + // If it came through a broadcast, send it to our peers if (block->m_wantBroadcast && !block->m_broadcasted) { block->m_broadcasted = true; - if (p2pServer() && (block->m_depth < UNCLE_BLOCK_DEPTH)) { - p2pServer()->broadcast(*block); + if (server && (block->m_depth < UNCLE_BLOCK_DEPTH)) { + server->broadcast(*block); } } // Save it for faster syncing on the next p2pool start - if (p2pServer()) { - p2pServer()->store_in_cache(*block); + if (server) { + server->store_in_cache(*block); } // Try to verify blocks on top of this one