Skip to content
This repository has been archived by the owner on Dec 2, 2022. It is now read-only.

Commit

Permalink
VerifyStage: headers validation using preferified hashes.
Browse files Browse the repository at this point in the history
The preverified hashes is a list of known precomputed hashes of every 192-th block in the chain:

hash(0), hash(192), hash(384), hash(576), ...

When we have a HeaderSlice and need to verify it, the algorithm verifies that the top of the slice matches one of the preverified hashes, and that all blocks down to the root of the slice are properly connected by the parent_hash field.

For example, if we have a HeaderSlice[192...384] (with block headers from 192 to 384 inclusive), it verifies that:

hash(slice[384]) == preverified hash(384)
hash(slice[383]) == slice[384].parent_hash
hash(slice[382]) == slice[383].parent_hash
...
hash(slice[192]) == slice[193].parent_hash

Thus verifying hashes of all the headers.
  • Loading branch information
battlmonstr committed Aug 29, 2021
1 parent 00d54dc commit d923dd1
Show file tree
Hide file tree
Showing 6 changed files with 120,907 additions and 8 deletions.
10 changes: 7 additions & 3 deletions src/downloader/downloader_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use crate::downloader::{
chain_config::{ChainConfig, ChainsConfig},
headers::{
fetch_receive_stage::FetchReceiveStage, fetch_request_stage::FetchRequestStage,
header_slices::HeaderSlices, refill_stage::RefillStage, retry_stage::RetryStage,
save_stage::SaveStage, verify_stage::VerifyStage,
header_slices::HeaderSlices, preverified_hashes_config::PreverifiedHashesConfig,
refill_stage::RefillStage, retry_stage::RetryStage, save_stage::SaveStage,
verify_stage::VerifyStage,
},
opts::Opts,
sentry_client,
Expand Down Expand Up @@ -79,7 +80,10 @@ impl Downloader {

let retry_stage = RetryStage::new(Arc::clone(&header_slices));

let verify_stage = VerifyStage::new(Arc::clone(&header_slices));
let verify_stage = VerifyStage::new(
Arc::clone(&header_slices),
PreverifiedHashesConfig::new(&self.opts.chain_name)?,
);

let save_stage = SaveStage::new(Arc::clone(&header_slices));

Expand Down
2 changes: 2 additions & 0 deletions src/downloader/headers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod header_slices;

pub mod fetch_receive_stage;
pub mod fetch_request_stage;
pub mod preverified_hashes_config;
pub mod refill_stage;
pub mod retry_stage;
pub mod save_stage;
Expand All @@ -14,5 +15,6 @@ pub use ui_crossterm::HeaderSlicesView;

#[cfg(not(feature = "crossterm"))]
pub mod ui_tracing;

#[cfg(not(feature = "crossterm"))]
pub use ui_tracing::HeaderSlicesView;
Loading

0 comments on commit d923dd1

Please sign in to comment.