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

[Epoch Sync] EpochSyncProof validation #12020

Merged
merged 8 commits into from
Oct 18, 2024
Merged

Conversation

staffik
Copy link
Contributor

@staffik staffik commented Aug 29, 2024

Issue: #11932

Implements EpochSyncProof validation, according to:

Summary:

@staffik staffik force-pushed the epoch-sync-v4-validation branch from 5113cf7 to f80fc66 Compare September 5, 2024 13:30
@staffik staffik force-pushed the epoch-sync-v4-validation branch from f80fc66 to 00c1851 Compare September 23, 2024 12:35
@staffik staffik marked this pull request as ready for review September 23, 2024 12:43
@staffik staffik requested a review from a team as a code owner September 23, 2024 12:43
)?;

store_update.set_ser(
DBCol::BlockOrdinal,
&borsh::to_vec(&proof.current_epoch.merkle_proof_for_first_block.size()).unwrap(),
&index_to_bytes(last_header.block_ordinal()),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Concern: BlockHeader::block_ordinal() is supported since BlockHeaderV3.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think that's an issue. EpochSync to a block header version below V3 is unsupportable due to also not having epoch_data_hash.

Copy link
Contributor

Choose a reason for hiding this comment

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

But it will be worth making a test to check that trying to sync to a version below V3 will fail. I'll file an issue.

let last_epoch_data = &proof.past_epochs[proof.past_epochs.len() - 2];
let last_epoch_last_final_block_height = last_epoch_data.last_final_block_header.height();
let first_block_info_in_epoch =
BlockInfo::from_header(&last_header, last_epoch_last_final_block_height);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Concern: there is also from_header_and_endorsements() method, but from_header would copy endorsements from the header. cc @tayfunelmas

@staffik
Copy link
Contributor Author

staffik commented Sep 23, 2024

Looking into failing integration tests.

}
}

impl MerkleProofAccess for Store {
Copy link
Contributor

Choose a reason for hiding this comment

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

Very nice design to make it work for both ChainStore and Store!

chain/chain/src/store/merkle_proof.rs Show resolved Hide resolved
chain/chain/src/store/merkle_proof.rs Outdated Show resolved Hide resolved
chain/chain/src/store/merkle_proof.rs Outdated Show resolved Hide resolved
)?;

store_update.set_ser(
DBCol::BlockOrdinal,
&borsh::to_vec(&proof.current_epoch.merkle_proof_for_first_block.size()).unwrap(),
&index_to_bytes(last_header.block_ordinal()),
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think that's an issue. EpochSync to a block header version below V3 is unsupportable due to also not having epoch_data_hash.

)?;

store_update.set_ser(
DBCol::BlockOrdinal,
&borsh::to_vec(&proof.current_epoch.merkle_proof_for_first_block.size()).unwrap(),
&index_to_bytes(last_header.block_ordinal()),
Copy link
Contributor

Choose a reason for hiding this comment

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

But it will be worth making a test to check that trying to sync to a version below V3 will fail. I'll file an issue.

chain/client/src/sync/epoch.rs Outdated Show resolved Hide resolved
epoch_manager: &dyn EpochManagerAdapter,
) -> Result<(), Error> {
// Verify epoch.block_producers
let bp_hash = Chain::compute_bp_hash_from_validator_stakes(
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice refactoring for this function. I was worried earlier about the protocol version check in here, but this is good.

@@ -514,6 +508,148 @@ impl EpochSync {

Ok(())
}

// Verify EpochSyncProof
fn verify_proof(
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we add some tests (can add some to the testloop test) that messing with the proof will result in a failing verification? We could, e.g. check that tweaking every byte of a valid serialized proof will cause a failure one way or another. It doesn't cover all possible attacks but at least makes sure that we aren't forgetting any fields.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created follow-up issue for tests: #12260.
Might need fixing other issues first.

@robin-near
Copy link
Contributor

Great implementation!

@staffik
Copy link
Contributor Author

staffik commented Sep 25, 2024

Addressed PR comments here: 76bdb69
Needed to add two more fields to the proof.
Also, there are 2 things marked as todo and this PR does not includes tests.

Copy link

codecov bot commented Oct 18, 2024

Codecov Report

Attention: Patch coverage is 79.58115% with 78 lines in your changes missing coverage. Please review.

Project coverage is 71.62%. Comparing base (ba6c707) to head (2a35498).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
chain/chain/src/store/merkle_proof.rs 75.46% 25 Missing and 15 partials ⚠️
chain/client/src/sync/epoch.rs 83.92% 17 Missing and 10 partials ⚠️
chain/chain/src/test_utils/kv_runtime.rs 0.00% 10 Missing ⚠️
chain/chain-primitives/src/error.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12020      +/-   ##
==========================================
- Coverage   71.62%   71.62%   -0.01%     
==========================================
  Files         837      838       +1     
  Lines      167105   167319     +214     
  Branches   167105   167319     +214     
==========================================
+ Hits       119696   119842     +146     
- Misses      42180    42242      +62     
- Partials     5229     5235       +6     
Flag Coverage Δ
backward-compatibility 0.16% <0.00%> (-0.01%) ⬇️
db-migration 0.16% <0.00%> (-0.01%) ⬇️
genesis-check 1.25% <0.00%> (-0.01%) ⬇️
integration-tests 38.92% <79.58%> (+0.04%) ⬆️
linux 71.23% <79.58%> (-0.02%) ⬇️
linux-nightly 71.21% <79.58%> (+<0.01%) ⬆️
macos 54.24% <8.90%> (-0.07%) ⬇️
pytests 1.56% <0.00%> (-0.01%) ⬇️
sanity-checks 1.37% <0.00%> (-0.01%) ⬇️
unittests 65.35% <8.90%> (-0.09%) ⬇️
upgradability 0.21% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@staffik staffik added this pull request to the merge queue Oct 18, 2024
Merged via the queue into master with commit 2c40e53 Oct 18, 2024
28 of 30 checks passed
@staffik staffik deleted the epoch-sync-v4-validation branch October 18, 2024 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants