snarkvm_ledger_block/header/
verify.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#![allow(clippy::too_many_arguments)]
17
18use super::*;
19
20impl<N: Network> Header<N> {
21    /// Ensures the block header is correct.
22    pub fn verify(
23        &self,
24        expected_previous_state_root: N::StateRoot,
25        expected_transactions_root: Field<N>,
26        expected_finalize_root: Field<N>,
27        expected_ratifications_root: Field<N>,
28        expected_solutions_root: Field<N>,
29        expected_subdag_root: Field<N>,
30        expected_round: u64,
31        expected_height: u32,
32        expected_cumulative_weight: u128,
33        expected_cumulative_proof_target: u128,
34        expected_coinbase_target: u64,
35        expected_proof_target: u64,
36        expected_last_coinbase_target: u64,
37        expected_last_coinbase_timestamp: i64,
38        expected_timestamp: i64,
39        current_timestamp: i64,
40    ) -> Result<()> {
41        // Ensure the block header is well-formed.
42        ensure!(self.is_valid(), "Header is malformed in block {expected_height}");
43        // Ensure the previous state root is correct.
44        ensure!(
45            self.previous_state_root == expected_previous_state_root,
46            "Previous state root is incorrect in block {expected_height} (found '{}', expected '{}')",
47            self.previous_state_root,
48            expected_previous_state_root
49        );
50        // Ensure the transactions root is correct.
51        ensure!(
52            self.transactions_root == expected_transactions_root,
53            "Transactions root is incorrect in block {expected_height} (found '{}', expected '{}')",
54            self.transactions_root,
55            expected_transactions_root
56        );
57        // Ensure the finalize root is correct.
58        ensure!(
59            self.finalize_root == expected_finalize_root,
60            "Finalize root is incorrect in block {expected_height} (found '{}', expected '{}')",
61            self.finalize_root,
62            expected_finalize_root
63        );
64        // Ensure the ratifications root is correct.
65        ensure!(
66            self.ratifications_root == expected_ratifications_root,
67            "Ratifications root is incorrect in block {expected_height} (found '{}', expected '{}')",
68            self.ratifications_root,
69            expected_ratifications_root
70        );
71        // Ensure the solutions root is correct.
72        ensure!(
73            self.solutions_root == expected_solutions_root,
74            "Solutions root is incorrect in block {expected_height} (found '{}', expected '{}')",
75            self.solutions_root,
76            expected_solutions_root
77        );
78        // Ensure the subdag root is correct.
79        ensure!(
80            self.subdag_root == expected_subdag_root,
81            "Subdag root is incorrect in block {expected_height} (found '{}', expected '{}')",
82            self.subdag_root,
83            expected_subdag_root
84        );
85        // Ensure the block metadata is correct.
86        self.metadata.verify(
87            expected_round,
88            expected_height,
89            expected_cumulative_weight,
90            expected_cumulative_proof_target,
91            expected_coinbase_target,
92            expected_proof_target,
93            expected_last_coinbase_target,
94            expected_last_coinbase_timestamp,
95            expected_timestamp,
96            current_timestamp,
97        )
98    }
99}