Skip to main content

snarkvm_ledger_block/header/metadata/
genesis.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
16use super::*;
17
18use snarkvm_utilities::ensure_equals;
19
20const GENESIS_ROUND: u64 = 0;
21const GENESIS_HEIGHT: u32 = 0;
22const GENESIS_CUMULATIVE_WEIGHT: u128 = 0;
23const GENESIS_CUMULATIVE_PROOF_TARGET: u128 = 0;
24
25impl<N: Network> Metadata<N> {
26    /// Initializes the genesis metadata.
27    pub fn genesis() -> Result<Self> {
28        Self::new(
29            N::ID,
30            GENESIS_ROUND,
31            GENESIS_HEIGHT,
32            GENESIS_CUMULATIVE_WEIGHT,
33            GENESIS_CUMULATIVE_PROOF_TARGET,
34            N::GENESIS_COINBASE_TARGET,
35            N::GENESIS_PROOF_TARGET,
36            N::GENESIS_COINBASE_TARGET,
37            N::GENESIS_TIMESTAMP,
38            N::GENESIS_TIMESTAMP,
39        )
40        .with_context(|| "Failed to create genesis block")
41    }
42
43    /// Returns `true` if the metadata is a genesis metadata.
44    ///
45    /// Return an error if the block is at height 0 but not a valid genesis header.
46    pub fn is_genesis(&self) -> Result<bool> {
47        // Only blocks at height 0 are genesis blocks.
48        if self.round != 0u64 {
49            return Ok(false);
50        }
51
52        // Check the genesis block header is valid otherwise.
53        // We cannot call `Self::check_validity` here as that function calls `is_genesis` internally.
54        ensure!(self.network == N::ID, "Invalid network ID");
55        ensure!(self.round == GENESIS_ROUND, "Genesis block not at genesis round");
56        ensure!(self.height == GENESIS_HEIGHT, "Genesis block not at genesis height");
57
58        ensure_equals!(self.cumulative_weight, GENESIS_CUMULATIVE_WEIGHT, "Invalid cumulative weight");
59        ensure_equals!(
60            self.cumulative_proof_target,
61            GENESIS_CUMULATIVE_PROOF_TARGET,
62            "Invalid cumulative proof target"
63        );
64        ensure_equals!(self.timestamp, N::GENESIS_TIMESTAMP, "Invalid timestamp");
65        ensure_equals!(self.last_coinbase_timestamp, N::GENESIS_TIMESTAMP, "Invalid last coinbase timestamp");
66        ensure_equals!(self.coinbase_target, N::GENESIS_COINBASE_TARGET, "Invalid coinbase target for genesis block");
67        ensure_equals!(
68            self.last_coinbase_target,
69            N::GENESIS_COINBASE_TARGET,
70            "Invalid last coinbase target for genesis block"
71        );
72        ensure_equals!(self.proof_target, N::GENESIS_PROOF_TARGET, "Invalid proof target for genesis block");
73
74        Ok(true)
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81    use console::network::MainnetV0;
82
83    type CurrentNetwork = MainnetV0;
84
85    /// Returns the expected metadata size by summing its subcomponent sizes.
86    /// Update this method if the contents of the metadata have changed.
87    fn get_expected_size() -> usize {
88        // Metadata size.
89        1 + 8 + 4 + 16 + 16 + 8 + 8 + 8 + 8 + 8
90            // Add an additional 2 bytes for versioning.
91            + 2
92    }
93
94    #[test]
95    fn test_genesis_metadata_size() {
96        let rng = &mut TestRng::default();
97
98        // Prepare the expected size.
99        let expected_size = get_expected_size();
100        // Prepare the genesis metadata.
101        let genesis_metadata = crate::header::metadata::test_helpers::sample_block_metadata(rng);
102        // Ensure the size of the genesis metadata is correct.
103        assert_eq!(expected_size, genesis_metadata.to_bytes_le().unwrap().len());
104    }
105
106    #[test]
107    fn test_genesis_metadata() {
108        let rng = &mut TestRng::default();
109
110        // Prepare the genesis metadata.
111        let metadata = crate::header::metadata::test_helpers::sample_block_metadata(rng);
112        // Ensure the metadata is a genesis metadata.
113        assert!(metadata.is_genesis().unwrap());
114
115        // Ensure the genesis block contains the following.
116        assert_eq!(metadata.network(), CurrentNetwork::ID);
117        assert_eq!(metadata.round(), 0);
118        assert_eq!(metadata.height(), 0);
119        assert_eq!(metadata.cumulative_weight(), 0);
120        assert_eq!(metadata.cumulative_proof_target(), 0);
121        assert_eq!(metadata.coinbase_target(), CurrentNetwork::GENESIS_COINBASE_TARGET);
122        assert_eq!(metadata.proof_target(), CurrentNetwork::GENESIS_PROOF_TARGET);
123        assert_eq!(metadata.last_coinbase_target(), CurrentNetwork::GENESIS_COINBASE_TARGET);
124        assert_eq!(metadata.last_coinbase_timestamp(), CurrentNetwork::GENESIS_TIMESTAMP);
125        assert_eq!(metadata.timestamp(), CurrentNetwork::GENESIS_TIMESTAMP);
126    }
127}