snarkvm_ledger_block/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> Block<N> {
19 /// Specifies the number of genesis transactions.
20 pub const NUM_GENESIS_TRANSACTIONS: usize = 4;
21
22 /// Returns `true` if the block is a genesis block.
23 pub fn is_genesis(&self) -> bool {
24 // Ensure the previous block hash is zero.
25 self.previous_hash == N::BlockHash::default()
26 // Ensure the header is a genesis block header.
27 && self.header.is_genesis()
28 // Ensure the genesis authority is a beacon.
29 && self.authority.is_beacon()
30 // Ensure there is the correct number of ratification operations in the genesis block.
31 && self.ratifications.len() == 1
32 // Ensure there are no solutions in the genesis block.
33 && self.solutions.is_empty()
34 // Ensure there is the correct number of accepted transaction in the genesis block.
35 && self.transactions.num_accepted() == Self::NUM_GENESIS_TRANSACTIONS
36 // Ensure there is the correct number of rejected transaction in the genesis block.
37 && self.transactions.num_rejected() == 0
38 // Ensure there is the correct number of finalize operations in the genesis block.
39 && self.transactions.num_finalize() == 2 * Self::NUM_GENESIS_TRANSACTIONS
40 // Ensure there are no aborted transaction IDs in the genesis block.
41 && self.aborted_transaction_ids.is_empty()
42 }
43}
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48 use console::network::MainnetV0;
49
50 type CurrentNetwork = MainnetV0;
51
52 #[test]
53 fn test_genesis() {
54 // Load the genesis block.
55 let genesis_block = Block::<CurrentNetwork>::read_le(CurrentNetwork::genesis_bytes()).unwrap();
56 assert!(genesis_block.is_genesis());
57 }
58}