snarkvm_ledger_block/header/metadata/
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> Metadata<N> {
21    /// Ensures the block metadata is correct.
22    pub fn verify(
23        &self,
24        expected_round: u64,
25        expected_height: u32,
26        expected_cumulative_weight: u128,
27        expected_cumulative_proof_target: u128,
28        expected_coinbase_target: u64,
29        expected_proof_target: u64,
30        expected_last_coinbase_target: u64,
31        expected_last_coinbase_timestamp: i64,
32        expected_timestamp: i64,
33        current_timestamp: i64,
34    ) -> Result<()> {
35        // Ensure the block metadata is well-formed.
36        ensure!(self.is_valid(), "Metadata is malformed in block {expected_height}");
37        // Ensure the round is correct.
38        ensure!(
39            self.round == expected_round,
40            "Round is incorrect in block {expected_height} (found '{}', expected '{}')",
41            self.round,
42            expected_round
43        );
44        // Ensure the height is correct.
45        ensure!(
46            self.height == expected_height,
47            "Height is incorrect in block {expected_height} (found '{}', expected '{}')",
48            self.height,
49            expected_height
50        );
51        // Ensure the cumulative weight is correct.
52        ensure!(
53            self.cumulative_weight == expected_cumulative_weight,
54            "Cumulative weight is incorrect in block {expected_height} (found '{}', expected '{}')",
55            self.cumulative_weight,
56            expected_cumulative_weight
57        );
58        // Ensure the cumulative proof target is correct.
59        ensure!(
60            self.cumulative_proof_target == expected_cumulative_proof_target,
61            "Cumulative proof target is incorrect in block {expected_height} (found '{}', expected '{}')",
62            self.cumulative_proof_target,
63            expected_cumulative_proof_target
64        );
65        // Ensure the coinbase target is correct.
66        ensure!(
67            self.coinbase_target == expected_coinbase_target,
68            "Coinbase target is incorrect in block {expected_height} (found '{}', expected '{}')",
69            self.coinbase_target,
70            expected_coinbase_target
71        );
72        // Ensure the proof target is correct.
73        ensure!(
74            self.proof_target == expected_proof_target,
75            "Proof target is incorrect in block {expected_height} (found '{}', expected '{}')",
76            self.proof_target,
77            expected_proof_target
78        );
79        // Ensure the last coinbase target is correct.
80        ensure!(
81            self.last_coinbase_target == expected_last_coinbase_target,
82            "Last coinbase target is incorrect in block {expected_height} (found '{}', expected '{}')",
83            self.last_coinbase_target,
84            expected_last_coinbase_target
85        );
86        // Ensure the last coinbase timestamp is correct.
87        ensure!(
88            self.last_coinbase_timestamp == expected_last_coinbase_timestamp,
89            "Last coinbase timestamp is incorrect in block {expected_height} (found '{}', expected '{}')",
90            self.last_coinbase_timestamp,
91            expected_last_coinbase_timestamp
92        );
93        // Ensure the timestamp is correct.
94        ensure!(
95            self.timestamp == expected_timestamp,
96            "Timestamp is incorrect in block {expected_height} (found '{}', expected '{}')",
97            self.timestamp,
98            expected_timestamp
99        );
100        // Ensure the timestamp is after the current timestamp.
101        ensure!(
102            self.timestamp <= current_timestamp,
103            "Timestamp is in the future in block {expected_height} (found '{}', expected before '{}')",
104            self.timestamp,
105            current_timestamp
106        );
107        // Return success.
108        Ok(())
109    }
110}