/ ledger / block / src / header / metadata / verify.rs
verify.rs
 1  // Copyright (c) 2025 ADnet Contributors
 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 alphavm_utilities::ensure_equals;
19  
20  use super::*;
21  
22  impl<N: Network> Metadata<N> {
23      /// Ensures the block metadata is correct.
24      pub fn verify(
25          &self,
26          expected_round: u64,
27          expected_height: u32,
28          expected_cumulative_weight: u128,
29          expected_cumulative_proof_target: u128,
30          expected_coinbase_target: u64,
31          expected_proof_target: u64,
32          expected_last_coinbase_target: u64,
33          expected_last_coinbase_timestamp: i64,
34          expected_timestamp: i64,
35          current_timestamp: i64,
36      ) -> Result<()> {
37          // Ensure the block metadata is well-formed.
38          if let Err(err) = self.check_validity() {
39              bail!("Metadata is malformed in block {expected_height}: {err}");
40          }
41  
42          ensure_equals!(self.round, expected_round, "Round is incorrect in block {expected_height}");
43          ensure_equals!(self.height, expected_height, "Height is incorrect in block {expected_height}");
44          ensure_equals!(
45              self.cumulative_weight,
46              expected_cumulative_weight,
47              "Cumulative weight is incorrect in block {expected_height}"
48          );
49          ensure_equals!(
50              self.cumulative_proof_target,
51              expected_cumulative_proof_target,
52              "Cumulative proof target is incorrect in block {expected_height}"
53          );
54          ensure_equals!(
55              self.coinbase_target,
56              expected_coinbase_target,
57              "Coinbase target is incorrect in block {expected_height}"
58          );
59          ensure_equals!(
60              self.proof_target,
61              expected_proof_target,
62              "Proof target is incorrect in block {expected_height}"
63          );
64          ensure_equals!(
65              self.last_coinbase_target,
66              expected_last_coinbase_target,
67              "Last coinbase target is incorrect in block {expected_height}"
68          );
69          ensure_equals!(
70              self.last_coinbase_timestamp,
71              expected_last_coinbase_timestamp,
72              "Last coinbase timestamp is incorrect in block {expected_height}"
73          );
74          ensure_equals!(self.timestamp, expected_timestamp, "Timestamp is incorrect in block {expected_height}");
75          ensure!(
76              self.timestamp <= current_timestamp,
77              "Timestamp is in the future in block {expected_height} (found '{}', expected before '{}')",
78              self.timestamp,
79              current_timestamp
80          );
81  
82          Ok(())
83      }
84  }