diff options
author | Riccardo Spagni <ric@spagni.net> | 2019-04-01 17:34:02 +0200 |
---|---|---|
committer | Riccardo Spagni <ric@spagni.net> | 2019-04-01 17:34:02 +0200 |
commit | 29e4e70f3069ab79944b5128c79823d2e6a52309 (patch) | |
tree | 431cd0e235f55b201ce2bf7851d2d95573f7ff7a /src/cryptonote_core/blockchain.cpp | |
parent | Merge pull request #5331 (diff) | |
parent | blockchain: incremental long term block weight cache (diff) | |
download | monero-29e4e70f3069ab79944b5128c79823d2e6a52309.tar.xz |
Merge pull request #5352
ae6885f6 blockchain: incremental long term block weight cache (moneromooo-monero)
9b687c78 blockchain: simple cache for the long term block weights (moneromooo-monero)
Diffstat (limited to 'src/cryptonote_core/blockchain.cpp')
-rw-r--r-- | src/cryptonote_core/blockchain.cpp | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index 263227148..83d3044f8 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -178,6 +178,7 @@ Blockchain::Blockchain(tx_memory_pool& tx_pool) : m_enforce_dns_checkpoints(false), m_max_prepare_blocks_threads(4), m_db_sync_on_blocks(true), m_db_sync_threshold(1), m_db_sync_mode(db_async), m_db_default_sync(false), m_fast_sync(true), m_show_time_stats(false), m_sync_counter(0), m_bytes_to_sync(0), m_cancel(false), m_long_term_block_weights_window(CRYPTONOTE_LONG_TERM_BLOCK_WEIGHT_WINDOW_SIZE), m_long_term_effective_median_block_weight(0), + m_long_term_block_weights_cache_tip_hash(crypto::null_hash), m_difficulty_for_next_block_top_hash(crypto::null_hash), m_difficulty_for_next_block(1), m_btc_valid(false) @@ -1281,7 +1282,50 @@ void Blockchain::get_long_term_block_weights(std::vector<uint64_t>& weights, uin LOG_PRINT_L3("Blockchain::" << __func__); CRITICAL_REGION_LOCAL(m_blockchain_lock); + PERF_TIMER(get_long_term_block_weights); + + if (count == 0) + return; + + bool cached = false; + uint64_t blockchain_height = m_db->height(); + uint64_t tip_height = start_height + count - 1; + crypto::hash tip_hash = crypto::null_hash; + if (tip_height < blockchain_height && count == m_long_term_block_weights_cache.size()) + { + tip_hash = m_db->get_block_hash_from_height(tip_height); + cached = tip_hash == m_long_term_block_weights_cache_tip_hash; + } + + if (cached) + { + MTRACE("requesting " << count << " from " << start_height << ", cached"); + weights = m_long_term_block_weights_cache; + return; + } + + // in the vast majority of uncached cases, most is still cached, + // as we just move the window one block up: + if (tip_height > 0 && count == m_long_term_block_weights_cache.size() && tip_height < blockchain_height) + { + crypto::hash old_tip_hash = m_db->get_block_hash_from_height(tip_height - 1); + if (old_tip_hash == m_long_term_block_weights_cache_tip_hash) + { + weights = m_long_term_block_weights_cache; + for (size_t i = 1; i < weights.size(); ++i) + weights[i - 1] = weights[i]; + MTRACE("requesting " << count << " from " << start_height << ", incremental"); + weights.back() = m_db->get_block_long_term_weight(tip_height); + m_long_term_block_weights_cache = weights; + m_long_term_block_weights_cache_tip_hash = tip_hash; + return; + } + } + + MTRACE("requesting " << count << " from " << start_height << ", uncached"); weights = m_db->get_long_term_block_weights(start_height, count); + m_long_term_block_weights_cache = weights; + m_long_term_block_weights_cache_tip_hash = tip_hash; } //------------------------------------------------------------------ uint64_t Blockchain::get_current_cumulative_block_weight_limit() const |