What changed, and why it matters
This Monero update fixes how the network checks timestamps on alternative blockchains (alt-chains). Before the fix, an alt-chain could include too many old timestamps when deciding if a new block's timestamp is acceptable, which could let an attacker craft a side-chain that looks valid even though its timestamps are manipulated. The patch makes alt-chain timestamp checks use the same rules as the main chain and adds a test to confirm the behavior.
Review and merge promptly; this is a consensus-affecting bug fix. Nodes should upgrade to avoid chain-split or acceptance of invalid alt-chains. Run the new gen_block_ts_alt_chain core test to verify the fix.
Security signals we found
Consensus rule inconsistency between main-chain and alt-chain timestamp validation
Potential acceptance of alt-chain blocks with manipulated timestamps
Added regression test specifically targeting alt-chain timestamp median behavior
Change in timestamp window collection logic that limits data used for median calculation
Evidence from the diff
The patch modifies Blockchain::complete_timestamps_vector and Blockchain::build_alt_chain. Previously, build_alt_chain pushed every ancestor’s timestamp into the timestamps vector without capping it at BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW, and complete_timestamps_vector then walked back from start_top_height without a clear bound, potentially collecting more than the intended window of timestamps from the main chain. This caused alt-chain median timestamp validation to differ from main-chain validation. The fix caps the number of alt-chain timestamps collected to BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW and rewrites complete_timestamps_vector to collect exactly min(need_elements, start_top_height + 1) timestamps, aligning alt-chain and main-chain timestamp validation. A new core test, gen_block_ts_alt_chain, demonstrates a scenario where an alt-chain with a wide timestamp gap would previously be accepted incorrectly.
Changed components
src/cryptonote_core/blockchain.cpptests/core_tests/block_validation.cpptests/core_tests/block_validation.htests/core_tests/chaingen_main.cppInspect captured patch +43 / −7
### src/cryptonote_core/blockchain.cpp
@@ -1772,14 +1772,13 @@ bool Blockchain::complete_timestamps_vector(uint64_t start_top_height, std::vect
return true;
CRITICAL_REGION_LOCAL(m_blockchain_lock);
- size_t need_elements = BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW - timestamps.size();
+ const size_t need_elements = BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW - timestamps.size();
CHECK_AND_ASSERT_MES(start_top_height < m_db->height(), false, "internal error: passed start_height not < " << " m_db->height() -- " << start_top_height << " >= " << m_db->height());
- size_t stop_offset = start_top_height > need_elements ? start_top_height - need_elements : 0;
- timestamps.reserve(timestamps.size() + start_top_height - stop_offset);
- while (start_top_height != stop_offset)
+ const size_t count = std::min<uint64_t>(need_elements, start_top_height + 1);
+ timestamps.reserve(timestamps.size() + count);
+ for (size_t i = 0; i < count; ++i)
{
- timestamps.push_back(m_db->get_block_timestamp(start_top_height));
- --start_top_height;
+ timestamps.push_back(m_db->get_block_timestamp(start_top_height - i));
}
return true;
}
@@ -1800,7 +1799,9 @@ bool Blockchain::build_alt_chain(const crypto::hash &prev_id, std::list<block_ex
bei.cumulative_difficulty = data.cumulative_difficulty_high;
bei.cumulative_difficulty = (bei.cumulative_difficulty << 64) + data.cumulative_difficulty_low;
bei.already_generated_coins = data.already_generated_coins;
- timestamps.push_back(bei.bl.timestamp);
+ // Ancestors are visited newest first.
+ if (timestamps.size() < BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW)
+ timestamps.push_back(bei.bl.timestamp);
alt_chain.push_front(std::move(bei));
found = m_db->get_alt_block(bei.bl.prev_id, &data, &blob);
}
### tests/core_tests/block_validation.cpp
@@ -142,6 +142,35 @@ bool gen_block_ts_in_future::generate(std::vector<test_event_entry>& events) con
return true;
}
+bool gen_block_ts_alt_chain::generate(std::vector<test_event_entry>& events) const
+{
+ BLOCK_VALIDATION_INIT_GENERATE();
+ REWIND_BLOCKS_N(events, fork, blk_0, miner_account, BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW);
+ REWIND_BLOCKS_N(events, main_tip, fork, miner_account, BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW + 1);
+
+ // Wide timestamp gaps keep difficulty at one.
+ const uint64_t low = fork.timestamp + 100 * DIFFICULTY_TARGET_V1;
+ const uint64_t high = low + 100 * DIFFICULTY_TARGET_V1;
+ block alt_tip = fork;
+ // The oldest high timestamp raises the full-alt median above the last-60 median.
+ for (size_t i = 0; i <= BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW; ++i)
+ {
+ const uint64_t timestamp = (i == 0 || i > BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW / 2) ? high : low;
+ block next;
+ if (!generator.construct_block_manually(next, alt_tip, miner_account, test_generator::bf_timestamp, 0, 0, timestamp))
+ return false;
+ events.push_back(next);
+ alt_tip = next;
+ }
+
+ block valid;
+ if (!generator.construct_block_manually(valid, alt_tip, miner_account, test_generator::bf_timestamp, 0, 0, (low + high) / 2))
+ return false;
+ events.push_back(valid);
+ DO_CALLBACK(events, "check_block_accepted");
+ return true;
+}
+
bool gen_block_invalid_prev_id::generate(std::vector<test_event_entry>& events) const
{
BLOCK_VALIDATION_INIT_GENERATE();
### tests/core_tests/block_validation.h
@@ -104,6 +104,11 @@ struct gen_block_ts_in_future : public gen_block_verification_base<1>
bool generate(std::vector<test_event_entry>& events) const;
};
+struct gen_block_ts_alt_chain : public gen_block_accepted_base<2 * BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW + 3>
+{
+ bool generate(std::vector<test_event_entry>& events) const;
+};
+
struct gen_block_invalid_prev_id : public gen_block_verification_base<1>
{
bool generate(std::vector<test_event_entry>& events) const;
### tests/core_tests/chaingen_main.cpp
@@ -114,6 +114,7 @@ int main(int argc, char* argv[])
GENERATE_AND_PLAY(gen_block_ts_not_checked);
GENERATE_AND_PLAY(gen_block_ts_in_past);
GENERATE_AND_PLAY(gen_block_ts_in_future);
+ GENERATE_AND_PLAY(gen_block_ts_alt_chain);
GENERATE_AND_PLAY(gen_block_invalid_prev_id);
GENERATE_AND_PLAY(gen_block_invalid_nonce);
GENERATE_AND_PLAY(gen_block_no_miner_tx);Why this scored 61/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.