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