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    pub fn is_genesis(&self) -> bool {
53        // Ensure the previous ledger root is zero.
54        *self.previous_state_root == Field::zero()
55            // Ensure the transactions root is nonzero.
56            && self.transactions_root != Field::zero()
57            // Ensure the finalize root is nonzero.
58            && self.finalize_root != Field::zero()
59            // Ensure the ratifications root is nonzero.
60            && self.ratifications_root != Field::zero()
61            // Ensure the solutions root is zero.
62            && self.solutions_root == Field::zero()
63            // Ensure the subdag root is zero.
64            && self.subdag_root == Field::zero()
65            // Ensure the metadata is a genesis metadata.
66            && self.metadata.is_genesis()
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73    use console::network::MainnetV0;
74
75    type CurrentNetwork = MainnetV0;
76
77    /// Returns the expected block header size by summing its subcomponent sizes.
78    /// Update this method if the contents of a block header have changed.
79    fn get_expected_size<N: Network>() -> usize {
80        // Previous state root, transactions root, finalize root, ratifications root, and solutions root size.
81        (Field::<N>::size_in_bytes() * 6)
82            // Metadata size.
83            + 1 + 8 + 4 + 16 + 16 + 8 + 8 + 8 + 8 + 8
84            // Add an additional 3 bytes for versioning.
85            + 1 + 2
86    }
87
88    #[test]
89    fn test_genesis_header_size() {
90        let rng = &mut TestRng::default();
91
92        // Prepare the expected size.
93        let expected_size = get_expected_size::<CurrentNetwork>();
94        // Prepare the genesis block header.
95        let genesis_header = crate::header::test_helpers::sample_block_header(rng);
96        // Ensure the size of the genesis block header is correct.
97        assert_eq!(expected_size, genesis_header.to_bytes_le().unwrap().len());
98    }
99
100    #[test]
101    fn test_genesis_header() {
102        let rng = &mut TestRng::default();
103
104        // Prepare the genesis block header.
105        let header = crate::header::test_helpers::sample_block_header(rng);
106        // Ensure the block header is a genesis block header.
107        assert!(header.is_genesis());
108
109        // Ensure the genesis block contains the following.
110        assert_eq!(*header.previous_state_root(), Field::zero());
111        assert_eq!(header.solutions_root(), Field::zero());
112        assert_eq!(header.subdag_root(), Field::zero());
113        assert_eq!(header.network(), CurrentNetwork::ID);
114        assert_eq!(header.round(), 0);
115        assert_eq!(header.height(), 0);
116        assert_eq!(header.cumulative_weight(), 0);
117        assert_eq!(header.coinbase_target(), CurrentNetwork::GENESIS_COINBASE_TARGET);
118        assert_eq!(header.proof_target(), CurrentNetwork::GENESIS_PROOF_TARGET);
119        assert_eq!(header.last_coinbase_target(), CurrentNetwork::GENESIS_COINBASE_TARGET);
120        assert_eq!(header.last_coinbase_timestamp(), CurrentNetwork::GENESIS_TIMESTAMP);
121        assert_eq!(header.timestamp(), CurrentNetwork::GENESIS_TIMESTAMP);
122
123        // Ensure the genesis block does *not* contain the following.
124        assert_ne!(header.transactions_root(), Field::zero());
125        assert_ne!(header.finalize_root(), Field::zero());
126        assert_ne!(header.ratifications_root(), Field::zero());
127    }
128}