Skip to content

Commit 2656cdf

Browse files
committed
Merge pull request #9014
69de381 add a test for the long term weight cache (Boog900) 810f6a6 Fix: long term block weight cache The long term block weight cache was doing a wrong calculation when adding a new block to the cache. (Boog900)
2 parents 1c12d30 + 69de381 commit 2656cdf

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

src/cryptonote_core/blockchain.cpp

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4616,40 +4616,9 @@ bool Blockchain::update_next_cumulative_weight_limit(uint64_t *long_term_effecti
46164616
}
46174617
else
46184618
{
4619-
const uint64_t block_weight = m_db->get_block_weight(db_height - 1);
4619+
const uint64_t nblocks = std::min<uint64_t>(m_long_term_block_weights_window, db_height);
4620+
const uint64_t long_term_median = get_long_term_block_weight_median(db_height - nblocks, nblocks);
46204621

4621-
uint64_t long_term_median;
4622-
if (db_height == 1)
4623-
{
4624-
long_term_median = CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V5;
4625-
}
4626-
else
4627-
{
4628-
uint64_t nblocks = std::min<uint64_t>(m_long_term_block_weights_window, db_height);
4629-
if (nblocks == db_height)
4630-
--nblocks;
4631-
long_term_median = get_long_term_block_weight_median(db_height - nblocks - 1, nblocks);
4632-
}
4633-
4634-
m_long_term_effective_median_block_weight = std::max<uint64_t>(CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V5, long_term_median);
4635-
4636-
uint64_t short_term_constraint = m_long_term_effective_median_block_weight;
4637-
if (hf_version >= HF_VERSION_2021_SCALING)
4638-
short_term_constraint += m_long_term_effective_median_block_weight * 7 / 10;
4639-
else
4640-
short_term_constraint += m_long_term_effective_median_block_weight * 2 / 5;
4641-
uint64_t long_term_block_weight = std::min<uint64_t>(block_weight, short_term_constraint);
4642-
4643-
if (db_height == 1)
4644-
{
4645-
long_term_median = long_term_block_weight;
4646-
}
4647-
else
4648-
{
4649-
m_long_term_block_weights_cache_tip_hash = m_db->get_block_hash_from_height(db_height - 1);
4650-
m_long_term_block_weights_cache_rolling_median.insert(long_term_block_weight);
4651-
long_term_median = m_long_term_block_weights_cache_rolling_median.median();
4652-
}
46534622
m_long_term_effective_median_block_weight = std::max<uint64_t>(CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V5, long_term_median);
46544623

46554624
std::vector<uint64_t> weights;

tests/unit_tests/long_term_block_weight.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,3 +407,38 @@ TEST(long_term_block_weight, long_growth_spike_and_drop)
407407
ASSERT_GT(long_term_effective_median_block_weight, 300000 * 1.07);
408408
ASSERT_LT(long_term_effective_median_block_weight, 300000 * 1.09);
409409
}
410+
411+
TEST(long_term_block_weight, cache_matches_true_value)
412+
{
413+
PREFIX(16);
414+
415+
// Add big blocks to increase the block weight limit
416+
for (uint64_t h = 0; h <= 2000; ++h)
417+
{
418+
size_t w = bc->get_current_cumulative_block_weight_limit();
419+
uint64_t ltw = bc->get_next_long_term_block_weight(w);
420+
bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {});
421+
bc->update_next_cumulative_weight_limit();
422+
}
423+
424+
ASSERT_GT(bc->get_current_cumulative_block_weight_limit() * 10/17 , 300000);
425+
426+
// Add small blocks to the top of the chain
427+
for (uint64_t h = 2000; h <= 5001; ++h)
428+
{
429+
size_t w = (bc->get_current_cumulative_block_weight_median() * 10/17) - 1000;
430+
uint64_t ltw = bc->get_next_long_term_block_weight(w);
431+
bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {});
432+
bc->update_next_cumulative_weight_limit();
433+
}
434+
435+
// get the weight limit
436+
uint64_t weight_limit = bc->get_current_cumulative_block_weight_limit();
437+
// refresh the cache
438+
bc->m_long_term_block_weights_cache_rolling_median.clear();
439+
bc->get_long_term_block_weight_median(bc->get_db().height() - TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW, TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW);
440+
bc->update_next_cumulative_weight_limit();
441+
442+
// make sure the weight limit is the same
443+
ASSERT_EQ(weight_limit, bc->get_current_cumulative_block_weight_limit());
444+
}

0 commit comments

Comments
 (0)