Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): always pass the correct timestamp window to header validatior #5624

Merged

Conversation

sdbondi
Copy link
Member

@sdbondi sdbondi commented Aug 10, 2023

Description

fix(core): sanity check for timestamp window length checks for the correct length of the timestamp window
fix(core): always include the correct timestamp window to header validation
tests(core): add a reorg test of greater size than median timestamp window size
chore: remove allocations when logging hashes in blockchain database

Motivation and Context

sanity_check_timestamp_count (previously check_timestamp_count) allowed timestamp windows of min(median_window_size, header.height - 1) or greater. However, the window size must always be the correct size for a given block. If it is not, the median timestamp calculation will be incorrect. This check also subtracted 1 from the header height BEFORE checking the height is correct. This would cause a panic/underflow if a header of height 0 is passed in.

In the reorg logic, we passed in the same timestamp window for the candidate block when validating orphan chains, this could cause correct reorgs to fail. This PR also ensures that timestamps are sorted as this is required for a correct median calculation.

How Has This Been Tested?

Adds a new unit test it_does_a_sanity_check_on_the_number_of_timestamps_provided
Adds block chain unit test it_links_many_orphan_branches_to_main_chain_with_greater_reorg_than_median_timestamp_window
Existing test it_links_many_orphan_branches_to_main_chain failed after the fix to check_timestamp_count. Fixed in this PR
Manually caused a about 90 block reorg on localnet nodes

What process can a PR reviewer use to test or verify this change?

Create a reorg using localnet

Breaking Changes

  • None
  • Requires data directory on base node to be deleted
  • Requires hard fork
  • Other - Please specify

@sdbondi sdbondi force-pushed the core-validation-minor-fix-ts-count branch 5 times, most recently from ebab778 to cc27e92 Compare August 11, 2023 06:31
@sdbondi sdbondi changed the title wip: fix median timestamps validation fix(core): always include the correct timestamp window to header validatior Aug 11, 2023
@ghpbot-tari-project ghpbot-tari-project added P-acks_required Process - Requires more ACKs or utACKs P-reviews_required Process - Requires a review from a lead maintainer to be merged labels Aug 11, 2023
@sdbondi sdbondi marked this pull request as ready for review August 11, 2023 06:32
@github-actions
Copy link

github-actions bot commented Aug 11, 2023

Test Results (CI)

1 191 tests   1 191 ✔️  17m 43s ⏱️
     37 suites         0 💤
       1 files           0

Results for commit 8ce5df4.

♻️ This comment has been updated with latest results.

@github-actions
Copy link

github-actions bot commented Aug 11, 2023

Test Results (Integration tests)

  2 files  11 suites   23m 35s ⏱️
27 tests 25 ✔️ 0 💤 2
29 runs  27 ✔️ 0 💤 2

For more details on these failures, see this check.

Results for commit 8ce5df4.

♻️ This comment has been updated with latest results.

@sdbondi sdbondi force-pushed the core-validation-minor-fix-ts-count branch 3 times, most recently from 6ba4bc2 to 4f2f415 Compare August 11, 2023 06:58
@sdbondi sdbondi force-pushed the core-validation-minor-fix-ts-count branch from 4f2f415 to a1ff08e Compare August 11, 2023 07:07
Copy link
Contributor

@brianp brianp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks functional, but I what I don't understand reasoning behind the original, and new implementation. So I don't know if the code is correct. 😅

Comment on lines +98 to +109
let expected_timestamp_count = cmp::min(consensus_constants.median_timestamp_count() as u64, header.height);
// Empty `timestamps` is never valid
if timestamps.is_empty() {
return Err(ValidationError::IncorrectNumberOfTimestampsProvided {
expected: expected_timestamp_count,
actual: 0,
});
}

if timestamps.len() as u64 != expected_timestamp_count {
return Err(ValidationError::IncorrectNumberOfTimestampsProvided {
actual: timestamps.len() as u64,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code looks fine.

but tbh I don't really understand it just yet. It looks like we're receiving a slice of timestamps and seeing if the number of timestamps is the same as the median timestamp count. But this slice could contain the same single timestamp repeated and this sanity check passes.
So what I don't yet understand is why we're using this method of counting timestamps as a validation.

Copy link
Member Author

@sdbondi sdbondi Aug 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup the blockchain db code is super hard to reason about. It's not strictly a header validation since the timestamps come from headers previously inserted into the main/orphan chains so the contract we're making in the HeaderChainLinkedValidator trait is that the caller must provide the correct timestamps. This is why I renamed it to sanity_check because it's more about asserting an invariant we require when calling the validator. The other option is to take it out completely or to panic, but I went with the least risky approach.

@sdbondi sdbondi changed the title fix(core): always include the correct timestamp window to header validatior fix(core): always pass the correct timestamp window to header validatior Aug 11, 2023
@sdbondi sdbondi force-pushed the core-validation-minor-fix-ts-count branch from 00c6fb4 to ab2f300 Compare August 11, 2023 09:05
SWvheerden
SWvheerden previously approved these changes Aug 11, 2023
@ghpbot-tari-project ghpbot-tari-project removed the P-reviews_required Process - Requires a review from a lead maintainer to be merged label Aug 11, 2023
Copy link
Contributor

@hansieodendaal hansieodendaal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definite improvement
One comment nit

utACK

base_layer/core/src/chain_storage/blockchain_database.rs Outdated Show resolved Hide resolved
@SWvheerden SWvheerden merged commit 29700c3 into tari-project:development Aug 11, 2023
@sdbondi sdbondi deleted the core-validation-minor-fix-ts-count branch August 14, 2023 04:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P-acks_required Process - Requires more ACKs or utACKs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants