verify.rs
1 // Copyright (c) 2019-2025 Alpha-Delta Network Inc. 2 // This file is part of the alphavm 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 18 use super::*; 19 20 impl<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 if let Err(err) = self.check_validity() { 43 anyhow::bail!("Header is malformed in block {expected_height}: {err}"); 44 } 45 46 // Ensure the previous state root is correct. 47 ensure!( 48 self.previous_state_root == expected_previous_state_root, 49 "Previous state root is incorrect in block {expected_height} (found '{}', expected '{}')", 50 self.previous_state_root, 51 expected_previous_state_root 52 ); 53 // Ensure the transactions root is correct. 54 ensure!( 55 self.transactions_root == expected_transactions_root, 56 "Transactions root is incorrect in block {expected_height} (found '{}', expected '{}')", 57 self.transactions_root, 58 expected_transactions_root 59 ); 60 // Ensure the finalize root is correct. 61 ensure!( 62 self.finalize_root == expected_finalize_root, 63 "Finalize root is incorrect in block {expected_height} (found '{}', expected '{}')", 64 self.finalize_root, 65 expected_finalize_root 66 ); 67 // Ensure the ratifications root is correct. 68 ensure!( 69 self.ratifications_root == expected_ratifications_root, 70 "Ratifications root is incorrect in block {expected_height} (found '{}', expected '{}')", 71 self.ratifications_root, 72 expected_ratifications_root 73 ); 74 // Ensure the solutions root is correct. 75 ensure!( 76 self.solutions_root == expected_solutions_root, 77 "Solutions root is incorrect in block {expected_height} (found '{}', expected '{}')", 78 self.solutions_root, 79 expected_solutions_root 80 ); 81 // Ensure the subdag root is correct. 82 ensure!( 83 self.subdag_root == expected_subdag_root, 84 "Subdag root is incorrect in block {expected_height} (found '{}', expected '{}')", 85 self.subdag_root, 86 expected_subdag_root 87 ); 88 // Ensure the block metadata is correct. 89 self.metadata.verify( 90 expected_round, 91 expected_height, 92 expected_cumulative_weight, 93 expected_cumulative_proof_target, 94 expected_coinbase_target, 95 expected_proof_target, 96 expected_last_coinbase_target, 97 expected_last_coinbase_timestamp, 98 expected_timestamp, 99 current_timestamp, 100 ) 101 } 102 }