Skip to main content

snarkvm_ledger_block/header/
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
18impl<N: Network> Header<N> {
19    /// Initializes the genesis block header.
20    pub fn genesis(
21        ratifications: &Ratifications<N>,
22        transactions: &Transactions<N>,
23        ratified_finalize_operations: Vec<FinalizeOperation<N>>,
24    ) -> Result<Self> {
25        #[cfg(not(any(debug_assertions, feature = "test")))]
26        ensure!(ratifications.len() == 1, "The genesis block must contain exactly 1 ratification");
27        #[cfg(not(any(debug_assertions, feature = "test")))]
28        ensure!(!ratified_finalize_operations.is_empty(), "The genesis block must contain ratify-finalize operations");
29
30        // Prepare a genesis block header.
31        let previous_state_root = Into::<N::StateRoot>::into(Field::zero());
32        let transactions_root = transactions.to_transactions_root()?;
33        let finalize_root = transactions.to_finalize_root(ratified_finalize_operations)?;
34        let ratifications_root = ratifications.to_ratifications_root()?;
35        let solutions_root = Field::zero();
36        let subdag_root = Field::zero();
37        let metadata = Metadata::genesis()?;
38
39        // Return the genesis block header.
40        Self::from(
41            previous_state_root,
42            transactions_root,
43            finalize_root,
44            ratifications_root,
45            solutions_root,
46            subdag_root,
47            metadata,
48        )
49    }
50
51    /// Returns `true` if the block header is a genesis block header.
52    ///
53    /// Return an error if the header is malformed.
54    pub fn is_genesis(&self) -> Result<bool> {
55        if !self.metadata.is_genesis()? {
56            return Ok(false);
57        }
58
59        // Ensure the header is valid otherwise
60        ensure!(*self.previous_state_root == Field::zero(), "Invalid previous state root");
61        ensure!(self.transactions_root != Field::zero(), "Invalid transactions root");
62        ensure!(self.finalize_root != Field::zero(), "Invalid finalize root");
63        ensure!(self.ratifications_root != Field::zero(), "Invalid ratifications root");
64        ensure!(self.solutions_root == Field::zero(), "Invalid solutions root");
65        ensure!(self.subdag_root == Field::zero(), "Invalid subdag root");
66
67        Ok(true)
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74    use console::network::MainnetV0;
75
76    type CurrentNetwork = MainnetV0;
77
78    /// Returns the expected block header size by summing its subcomponent sizes.
79    /// Update this method if the contents of a block header have changed.
80    fn get_expected_size<N: Network>() -> usize {
81        // Previous state root, transactions root, finalize root, ratifications root, and solutions root size.
82        (Field::<N>::size_in_bytes() * 6)
83            // Metadata size.
84            + 1 + 8 + 4 + 16 + 16 + 8 + 8 + 8 + 8 + 8
85            // Add an additional 3 bytes for versioning.
86            + 1 + 2
87    }
88
89    #[test]
90    fn test_genesis_header_size() {
91        let rng = &mut TestRng::default();
92
93        // Prepare the expected size.
94        let expected_size = get_expected_size::<CurrentNetwork>();
95        // Prepare the genesis block header.
96        let genesis_header = crate::header::test_helpers::sample_block_header(rng);
97        // Ensure the size of the genesis block header is correct.
98        assert_eq!(expected_size, genesis_header.to_bytes_le().unwrap().len());
99    }
100
101    #[test]
102    fn test_genesis_header() {
103        let rng = &mut TestRng::default();
104
105        // Prepare the genesis block header.
106        let header = crate::header::test_helpers::sample_block_header(rng);
107        // Ensure the block header is a genesis block header.
108        assert!(header.is_genesis().unwrap());
109
110        // Ensure the genesis block contains the following.
111        assert_eq!(*header.previous_state_root(), Field::zero());
112        assert_eq!(header.solutions_root(), Field::zero());
113        assert_eq!(header.subdag_root(), Field::zero());
114        assert_eq!(header.network(), CurrentNetwork::ID);
115        assert_eq!(header.round(), 0);
116        assert_eq!(header.height(), 0);
117        assert_eq!(header.cumulative_weight(), 0);
118        assert_eq!(header.coinbase_target(), CurrentNetwork::GENESIS_COINBASE_TARGET);
119        assert_eq!(header.proof_target(), CurrentNetwork::GENESIS_PROOF_TARGET);
120        assert_eq!(header.last_coinbase_target(), CurrentNetwork::GENESIS_COINBASE_TARGET);
121        assert_eq!(header.last_coinbase_timestamp(), CurrentNetwork::GENESIS_TIMESTAMP);
122        assert_eq!(header.timestamp(), CurrentNetwork::GENESIS_TIMESTAMP);
123
124        // Ensure the genesis block does *not* contain the following.
125        assert_ne!(header.transactions_root(), Field::zero());
126        assert_ne!(header.finalize_root(), Field::zero());
127        assert_ne!(header.ratifications_root(), Field::zero());
128    }
129}