eth_rlp_verify/
lib.rs

1pub mod block_header;
2pub mod constants;
3pub mod eras;
4
5use crate::block_header::BlockHeader as VerifiableBlockHeader;
6
7/// Verifies the validity of an Ethereum block header based on the block number and expected hash.
8///
9/// This function determines the appropriate Ethereum era based on the block number, retrieves the corresponding
10/// verification function, and verifies the block header by comparing its computed hash with the expected block hash.
11/// The verification process ensures that the block is authentic and belongs to the correct place in the blockchain.
12///
13/// # Arguments
14///
15/// - `block_number`: A `u64` representing the block number of the block being verified. The block number is used
16///   to determine the correct era (e.g., London, Paris, Shapella) and apply the corresponding block verification logic.
17/// - `block_header`: A `VerifiableBlockHeader` struct containing the block header data that needs to be verified.
18/// - `block_hash`: A string slice (`&str`) representing the expected hash of the block. This hash will be compared
19///   to the computed hash of the block header to determine if the block is valid.
20///
21/// # Returns
22///
23/// A `bool` indicating whether the block header is valid:
24/// - `true`: The block hash matches the computed hash, and the block header is valid.
25/// - `false`: The block number is not within a supported era, or the computed block hash does not match the expected hash.
26///
27/// # Example
28///
29/// ```rust
30/// let block_number = 15_537_394; // A block from the Paris era
31/// let block_header = fetch_block_header_from_db(block_number); // Fetch the block header from the database
32/// let block_hash = "0xabc..."; // Expected block hash for verification
33///
34/// let is_valid = verify_block(block_number, block_header, block_hash);
35/// if is_valid {
36///     println!("Block header is valid!");
37/// } else {
38///     println!("Invalid block header.");
39/// }
40/// ```
41///
42/// # Era Determination
43///
44/// The function uses `eras::determine_era` to determine the correct era based on the `block_number`.
45/// This ensures that the correct block structure and verification rules are applied for each Ethereum upgrade:
46/// - **Genesis to London**: The era from the Genesis block to the London upgrade.
47/// - **London to Paris**: The era starting with the London upgrade and ending with the Paris (The Merge) upgrade.
48/// - **Paris to Shapella**: The era from the Paris upgrade to the Shapella (Shanghai + Capella) upgrade.
49/// - **Shapella to Dencun**: The era starting with Shapella and continuing into the Dencun upgrade.
50///
51/// # Notes
52///
53/// - If the block number is not within the supported range, the function returns `false`.
54/// - This function is designed to be future-proof, allowing for additional Ethereum eras and upgrades to be supported
55///   by simply adding them to the `eras::determine_era` function.
56pub fn verify_block(
57    block_number: u64,
58    block_header: VerifiableBlockHeader,
59    block_hash: &str,
60) -> bool {
61    match eras::determine_era(block_number) {
62        Some(verify_fn) => verify_fn(block_hash.to_string(), block_header),
63        None => false, // If the block number is out of the supported range
64    }
65}