From 4bcaa41c8e6882fd8d240970ce1306b25acc50f0 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Mon, 27 Sep 2021 13:28:23 +0200 Subject: [PATCH] Clear crypto derivation/key cache when a block is found --- src/crypto.cpp | 13 +++++++++++++ src/crypto.h | 1 + src/p2pool.cpp | 2 ++ src/util.h | 4 ++-- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/crypto.cpp b/src/crypto.cpp index 87f9f15..8397152 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -227,6 +227,14 @@ public: return true; } + void clear() + { + MutexLock lock(m); + + derivations.clear(); + public_keys.clear(); + } + private: uv_mutex_t m; std::unordered_map, hash> derivations; @@ -245,4 +253,9 @@ bool derive_public_key(const hash& derivation, size_t output_index, const hash& return cache.get_public_key(derivation, output_index, base, derived_key); } +void clear_crypto_cache() +{ + cache.clear(); +} + } // namespace p2pool diff --git a/src/crypto.h b/src/crypto.h index 00bef64..6ac7574 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -23,5 +23,6 @@ void generate_keys(hash& pub, hash& sec); bool check_keys(const hash& pub, const hash& sec); 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); +void clear_crypto_cache(); } // namespace p2pool diff --git a/src/p2pool.cpp b/src/p2pool.cpp index 47d9cef..b1b0246 100644 --- a/src/p2pool.cpp +++ b/src/p2pool.cpp @@ -1055,6 +1055,8 @@ void p2pool::api_update_stats_mod() void p2pool::api_update_block_found(const ChainMain* data) { + clear_crypto_cache(); + if (!m_api) { return; } diff --git a/src/util.h b/src/util.h index bc55422..ae436b9 100644 --- a/src/util.h +++ b/src/util.h @@ -126,7 +126,7 @@ namespace std { template<> struct hash { - FORCEINLINE size_t operator()(const p2pool::hash& value) const + FORCEINLINE size_t operator()(const p2pool::hash& value) const noexcept { uint64_t result = 0xcbf29ce484222325ull; for (size_t i = 0; i < p2pool::HASH_SIZE; ++i) { @@ -139,7 +139,7 @@ struct hash template struct hash> { - FORCEINLINE size_t operator()(const std::array& value) const + FORCEINLINE size_t operator()(const std::array& value) const noexcept { uint64_t result = 0xcbf29ce484222325ull; for (size_t i = 0; i < N; ++i) {