mirror of
https://github.com/SChernykh/p2pool.git
synced 2024-08-19 21:18:46 -06:00
Fixed memory leak after loading block cache
This commit is contained in:
parent
8b27faad6d
commit
028a5d0c88
@ -47,6 +47,7 @@ P2PServer::P2PServer(p2pool* pool)
|
|||||||
, m_cache(pool->params().m_blockCache ? new BlockCache() : nullptr)
|
, m_cache(pool->params().m_blockCache ? new BlockCache() : nullptr)
|
||||||
, m_cacheLoaded(false)
|
, m_cacheLoaded(false)
|
||||||
, m_initialPeerList(pool->params().m_p2pPeerList)
|
, m_initialPeerList(pool->params().m_p2pPeerList)
|
||||||
|
, m_cachedBlocks(nullptr)
|
||||||
, m_rng(RandomDeviceSeed::instance)
|
, m_rng(RandomDeviceSeed::instance)
|
||||||
, m_block(new PoolBlock())
|
, m_block(new PoolBlock())
|
||||||
, m_timer{}
|
, m_timer{}
|
||||||
@ -128,18 +129,34 @@ void P2PServer::add_cached_block(const PoolBlock& block)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!m_cachedBlocks) {
|
||||||
|
m_cachedBlocks = new unordered_map<hash, PoolBlock*>();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_cachedBlocks->find(block.m_sidechainId) == m_cachedBlocks->end()) {
|
||||||
PoolBlock* new_block = new PoolBlock(block);
|
PoolBlock* new_block = new PoolBlock(block);
|
||||||
m_cachedBlocks.insert({ new_block->m_sidechainId, new_block });
|
m_cachedBlocks->insert({ new_block->m_sidechainId, new_block });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void P2PServer::clear_cached_blocks()
|
void P2PServer::clear_cached_blocks()
|
||||||
{
|
{
|
||||||
|
if (!m_cachedBlocks) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
WriteLock lock(m_cachedBlocksLock);
|
WriteLock lock(m_cachedBlocksLock);
|
||||||
|
|
||||||
for (auto it : m_cachedBlocks) {
|
if (!m_cachedBlocks) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto it : *m_cachedBlocks) {
|
||||||
delete it.second;
|
delete it.second;
|
||||||
}
|
}
|
||||||
m_cachedBlocks.clear();
|
|
||||||
|
delete m_cachedBlocks;
|
||||||
|
m_cachedBlocks = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void P2PServer::store_in_cache(const PoolBlock& block)
|
void P2PServer::store_in_cache(const PoolBlock& block)
|
||||||
@ -934,12 +951,14 @@ void P2PServer::download_missing_blocks()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ReadLock lock2(m_cachedBlocksLock);
|
||||||
|
|
||||||
// Try to download each block from a random client
|
// Try to download each block from a random client
|
||||||
for (const hash& id : missing_blocks) {
|
for (const hash& id : missing_blocks) {
|
||||||
P2PClient* client = clients[get_random64() % clients.size()];
|
P2PClient* client = clients[get_random64() % clients.size()];
|
||||||
|
|
||||||
{
|
{
|
||||||
MutexLock lock2(m_missingBlockRequestsLock);
|
MutexLock lock3(m_missingBlockRequestsLock);
|
||||||
|
|
||||||
const uint64_t truncated_block_id = *reinterpret_cast<const uint64_t*>(id.h);
|
const uint64_t truncated_block_id = *reinterpret_cast<const uint64_t*>(id.h);
|
||||||
if (!m_missingBlockRequests.insert({ client->m_peerId, truncated_block_id }).second) {
|
if (!m_missingBlockRequests.insert({ client->m_peerId, truncated_block_id }).second) {
|
||||||
@ -949,6 +968,15 @@ void P2PServer::download_missing_blocks()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (m_cachedBlocks) {
|
||||||
|
auto it = m_cachedBlocks->find(id);
|
||||||
|
if (it != m_cachedBlocks->end()) {
|
||||||
|
LOGINFO(5, "using cached block for id = " << id);
|
||||||
|
client->handle_incoming_block_async(it->second);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const bool result = send(client,
|
const bool result = send(client,
|
||||||
[&id](void* buf)
|
[&id](void* buf)
|
||||||
{
|
{
|
||||||
@ -1944,12 +1972,14 @@ void P2PServer::P2PClient::post_handle_incoming_block(const uint32_t reset_count
|
|||||||
ReadLock lock(server->m_cachedBlocksLock);
|
ReadLock lock(server->m_cachedBlocksLock);
|
||||||
|
|
||||||
for (const hash& id : missing_blocks) {
|
for (const hash& id : missing_blocks) {
|
||||||
auto it = server->m_cachedBlocks.find(id);
|
if (server->m_cachedBlocks) {
|
||||||
if (it != server->m_cachedBlocks.end()) {
|
auto it = server->m_cachedBlocks->find(id);
|
||||||
|
if (it != server->m_cachedBlocks->end()) {
|
||||||
LOGINFO(5, "using cached block for id = " << id);
|
LOGINFO(5, "using cached block for id = " << id);
|
||||||
handle_incoming_block_async(it->second);
|
handle_incoming_block_async(it->second);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const bool result = m_owner->send(this,
|
const bool result = m_owner->send(this,
|
||||||
[&id](void* buf)
|
[&id](void* buf)
|
||||||
|
@ -149,7 +149,7 @@ private:
|
|||||||
uint32_t m_maxIncomingPeers;
|
uint32_t m_maxIncomingPeers;
|
||||||
|
|
||||||
uv_rwlock_t m_cachedBlocksLock;
|
uv_rwlock_t m_cachedBlocksLock;
|
||||||
unordered_map<hash, PoolBlock*> m_cachedBlocks;
|
unordered_map<hash, PoolBlock*>* m_cachedBlocks;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static void on_timer(uv_timer_t* timer) { reinterpret_cast<P2PServer*>(timer->data)->on_timer(); }
|
static void on_timer(uv_timer_t* timer) { reinterpret_cast<P2PServer*>(timer->data)->on_timer(); }
|
||||||
|
Loading…
Reference in New Issue
Block a user