Skip to content

Commit 7f70bd3

Browse files
committed
Fix checkpoint sync: skip difficulty validation after fast-start
Headers after the checkpoint were rejected because validate_difficulty() needed the header at (height - 2016) which doesn't exist when starting from a checkpoint. Now skips difficulty validation for the first 2016 headers after any checkpoint (hardcoded or dynamic). Also: skip difficulty validation when chain has fewer than 2016 headers (covers any non-genesis start scenario).
1 parent dde6e8f commit 7f70bd3

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

src/impl/ltc/coin/header_chain.hpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,10 +608,23 @@ class HeaderChain {
608608

609609
/// Validate that the header's nBits matches the expected difficulty.
610610
bool validate_difficulty(const BlockHeaderType& header, uint32_t new_height) {
611-
// For the first few blocks or when we don't have enough history, skip validation
612611
int64_t interval = m_params.difficulty_adjustment_interval();
613612
if (new_height < 2) return true; // genesis + first block
614613

614+
// After a fast-start checkpoint, skip difficulty validation until we have
615+
// enough headers for the retarget lookback (interval = 2016).
616+
// Without this, get_ancestor() fails because the checkpoint doesn't have
617+
// the previous 2016 headers needed for difficulty calculation.
618+
if (m_params.fast_start_checkpoint.has_value()) {
619+
uint32_t cp_h = m_params.fast_start_checkpoint->height;
620+
if (new_height > cp_h && new_height < cp_h + static_cast<uint32_t>(interval) + 10)
621+
return true; // trust difficulty within first retarget window after checkpoint
622+
}
623+
// Also skip when we have a dynamic checkpoint (no fast_start_checkpoint set
624+
// but chain started at a non-zero height)
625+
if (m_headers.size() < static_cast<size_t>(interval + 10))
626+
return true;
627+
615628
// Get tip (the block we're building on)
616629
auto prev_it = m_headers.find(header.m_previous_block);
617630
if (prev_it == m_headers.end()) return false;

0 commit comments

Comments
 (0)