mirror of
https://github.com/SChernykh/p2pool.git
synced 2025-01-03 09:19:24 +00:00
P2PServer: delete old cached blocks after initial sync
Saves ~20 MB of memory
This commit is contained in:
parent
00281fb006
commit
2a3cd13b19
7 changed files with 48 additions and 9 deletions
|
@ -241,21 +241,36 @@ private:
|
||||||
std::unordered_map<std::array<uint8_t, HASH_SIZE * 2 + sizeof(size_t)>, hash> public_keys;
|
std::unordered_map<std::array<uint8_t, HASH_SIZE * 2 + sizeof(size_t)>, hash> public_keys;
|
||||||
};
|
};
|
||||||
|
|
||||||
static Cache cache;
|
static Cache* cache = nullptr;
|
||||||
|
|
||||||
bool generate_key_derivation(const hash& key1, const hash& key2, hash& derivation)
|
bool generate_key_derivation(const hash& key1, const hash& key2, hash& derivation)
|
||||||
{
|
{
|
||||||
return cache.get_derivation(key1, key2, derivation);
|
return cache->get_derivation(key1, key2, derivation);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool derive_public_key(const hash& derivation, size_t output_index, const hash& base, hash& derived_key)
|
bool derive_public_key(const hash& derivation, size_t output_index, const hash& base, hash& derived_key)
|
||||||
{
|
{
|
||||||
return cache.get_public_key(derivation, output_index, base, derived_key);
|
return cache->get_public_key(derivation, output_index, base, derived_key);
|
||||||
|
}
|
||||||
|
|
||||||
|
void init_crypto_cache()
|
||||||
|
{
|
||||||
|
if (!cache) {
|
||||||
|
cache = new Cache();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void destroy_crypto_cache()
|
||||||
|
{
|
||||||
|
if (cache) {
|
||||||
|
delete cache;
|
||||||
|
cache = nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear_crypto_cache()
|
void clear_crypto_cache()
|
||||||
{
|
{
|
||||||
cache.clear();
|
cache->clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace p2pool
|
} // namespace p2pool
|
||||||
|
|
|
@ -23,6 +23,9 @@ void generate_keys(hash& pub, hash& sec);
|
||||||
bool check_keys(const hash& pub, const hash& sec);
|
bool check_keys(const hash& pub, const hash& sec);
|
||||||
bool generate_key_derivation(const hash& key1, const hash& key2, hash& derivation);
|
bool generate_key_derivation(const hash& key1, const hash& key2, hash& derivation);
|
||||||
bool derive_public_key(const hash& derivation, size_t output_index, const hash& base, hash& derived_key);
|
bool derive_public_key(const hash& derivation, size_t output_index, const hash& base, hash& derived_key);
|
||||||
|
|
||||||
|
void init_crypto_cache();
|
||||||
|
void destroy_crypto_cache();
|
||||||
void clear_crypto_cache();
|
void clear_crypto_cache();
|
||||||
|
|
||||||
} // namespace p2pool
|
} // namespace p2pool
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
#include "crypto.h"
|
||||||
#include "p2pool.h"
|
#include "p2pool.h"
|
||||||
#include "stratum_server.h"
|
#include "stratum_server.h"
|
||||||
#include "p2p_server.h"
|
#include "p2p_server.h"
|
||||||
|
@ -68,10 +69,14 @@ int main(int argc, char* argv[])
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
memory_tracking_start();
|
memory_tracking_start();
|
||||||
|
|
||||||
|
p2pool::init_crypto_cache();
|
||||||
{
|
{
|
||||||
p2pool::p2pool pool(argc, argv);
|
p2pool::p2pool pool(argc, argv);
|
||||||
result = pool.run();
|
result = pool.run();
|
||||||
}
|
}
|
||||||
|
p2pool::destroy_crypto_cache();
|
||||||
|
|
||||||
memory_tracking_stop();
|
memory_tracking_stop();
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
|
@ -102,14 +102,11 @@ P2PServer::~P2PServer()
|
||||||
uv_mutex_destroy(&m_peerListLock);
|
uv_mutex_destroy(&m_peerListLock);
|
||||||
uv_mutex_destroy(&m_broadcastLock);
|
uv_mutex_destroy(&m_broadcastLock);
|
||||||
uv_mutex_destroy(&m_missingBlockRequestsLock);
|
uv_mutex_destroy(&m_missingBlockRequestsLock);
|
||||||
|
|
||||||
|
clear_cached_blocks();
|
||||||
uv_rwlock_destroy(&m_cachedBlocksLock);
|
uv_rwlock_destroy(&m_cachedBlocksLock);
|
||||||
|
|
||||||
delete m_block;
|
delete m_block;
|
||||||
|
|
||||||
for (auto it : m_cachedBlocks) {
|
|
||||||
delete it.second;
|
|
||||||
}
|
|
||||||
|
|
||||||
delete m_cache;
|
delete m_cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,6 +121,16 @@ void P2PServer::add_cached_block(const 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()
|
||||||
|
{
|
||||||
|
WriteLock lock(m_cachedBlocksLock);
|
||||||
|
|
||||||
|
for (auto it : m_cachedBlocks) {
|
||||||
|
delete it.second;
|
||||||
|
}
|
||||||
|
m_cachedBlocks.clear();
|
||||||
|
}
|
||||||
|
|
||||||
void P2PServer::store_in_cache(const PoolBlock& block)
|
void P2PServer::store_in_cache(const PoolBlock& block)
|
||||||
{
|
{
|
||||||
if (m_cache && block.m_verified && !block.m_invalid) {
|
if (m_cache && block.m_verified && !block.m_invalid) {
|
||||||
|
|
|
@ -49,6 +49,7 @@ public:
|
||||||
~P2PServer();
|
~P2PServer();
|
||||||
|
|
||||||
void add_cached_block(const PoolBlock& block);
|
void add_cached_block(const PoolBlock& block);
|
||||||
|
void clear_cached_blocks();
|
||||||
void store_in_cache(const PoolBlock& block);
|
void store_in_cache(const PoolBlock& block);
|
||||||
|
|
||||||
void connect_to_peers(const std::string& peer_list);
|
void connect_to_peers(const std::string& peer_list);
|
||||||
|
|
|
@ -1552,6 +1552,10 @@ void SideChain::prune_old_blocks()
|
||||||
|
|
||||||
if (num_blocks_pruned) {
|
if (num_blocks_pruned) {
|
||||||
LOGINFO(4, "pruned " << num_blocks_pruned << " old blocks at heights <= " << h);
|
LOGINFO(4, "pruned " << num_blocks_pruned << " old blocks at heights <= " << h);
|
||||||
|
|
||||||
|
// If side-chain started pruning blocks it means the initial sync is complete
|
||||||
|
// It's now safe to delete cached blocks
|
||||||
|
m_pool->p2p_server()->clear_cached_blocks();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,8 @@ namespace p2pool {
|
||||||
|
|
||||||
TEST(crypto, derivation)
|
TEST(crypto, derivation)
|
||||||
{
|
{
|
||||||
|
init_crypto_cache();
|
||||||
|
|
||||||
// Run the tests twice to check how crypto cache works
|
// Run the tests twice to check how crypto cache works
|
||||||
for (int i = 0; i < 2; ++i) {
|
for (int i = 0; i < 2; ++i) {
|
||||||
std::ifstream f("crypto_tests.txt");
|
std::ifstream f("crypto_tests.txt");
|
||||||
|
@ -61,6 +63,8 @@ TEST(crypto, derivation)
|
||||||
}
|
}
|
||||||
} while (!f.eof());
|
} while (!f.eof());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
destroy_crypto_cache();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue