snarkvm_ledger_block/header/genesis.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
// Copyright 2024 Aleo Network Foundation
// This file is part of the snarkVM library.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use super::*;
impl<N: Network> Header<N> {
/// Initializes the genesis block header.
pub fn genesis(
ratifications: &Ratifications<N>,
transactions: &Transactions<N>,
ratified_finalize_operations: Vec<FinalizeOperation<N>>,
) -> Result<Self> {
#[cfg(not(debug_assertions))]
ensure!(ratifications.len() == 1, "The genesis block must contain exactly 1 ratification");
#[cfg(not(debug_assertions))]
ensure!(!ratified_finalize_operations.is_empty(), "The genesis block must contain ratify-finalize operations");
// Prepare a genesis block header.
let previous_state_root = Into::<N::StateRoot>::into(Field::zero());
let transactions_root = transactions.to_transactions_root()?;
let finalize_root = transactions.to_finalize_root(ratified_finalize_operations)?;
let ratifications_root = ratifications.to_ratifications_root()?;
let solutions_root = Field::zero();
let subdag_root = Field::zero();
let metadata = Metadata::genesis()?;
// Return the genesis block header.
Self::from(
previous_state_root,
transactions_root,
finalize_root,
ratifications_root,
solutions_root,
subdag_root,
metadata,
)
}
/// Returns `true` if the block header is a genesis block header.
pub fn is_genesis(&self) -> bool {
// Ensure the previous ledger root is zero.
*self.previous_state_root == Field::zero()
// Ensure the transactions root is nonzero.
&& self.transactions_root != Field::zero()
// Ensure the finalize root is nonzero.
&& self.finalize_root != Field::zero()
// Ensure the ratifications root is nonzero.
&& self.ratifications_root != Field::zero()
// Ensure the solutions root is zero.
&& self.solutions_root == Field::zero()
// Ensure the subdag root is zero.
&& self.subdag_root == Field::zero()
// Ensure the metadata is a genesis metadata.
&& self.metadata.is_genesis()
}
}
#[cfg(test)]
mod tests {
use super::*;
use console::network::MainnetV0;
type CurrentNetwork = MainnetV0;
/// Returns the expected block header size by summing its subcomponent sizes.
/// Update this method if the contents of a block header have changed.
fn get_expected_size<N: Network>() -> usize {
// Previous state root, transactions root, finalize root, ratifications root, and solutions root size.
(Field::<N>::size_in_bytes() * 6)
// Metadata size.
+ 1 + 8 + 4 + 16 + 16 + 8 + 8 + 8 + 8 + 8
// Add an additional 3 bytes for versioning.
+ 1 + 2
}
#[test]
fn test_genesis_header_size() {
let rng = &mut TestRng::default();
// Prepare the expected size.
let expected_size = get_expected_size::<CurrentNetwork>();
// Prepare the genesis block header.
let genesis_header = crate::header::test_helpers::sample_block_header(rng);
// Ensure the size of the genesis block header is correct.
assert_eq!(expected_size, genesis_header.to_bytes_le().unwrap().len());
}
#[test]
fn test_genesis_header() {
let rng = &mut TestRng::default();
// Prepare the genesis block header.
let header = crate::header::test_helpers::sample_block_header(rng);
// Ensure the block header is a genesis block header.
assert!(header.is_genesis());
// Ensure the genesis block contains the following.
assert_eq!(*header.previous_state_root(), Field::zero());
assert_eq!(header.solutions_root(), Field::zero());
assert_eq!(header.subdag_root(), Field::zero());
assert_eq!(header.network(), CurrentNetwork::ID);
assert_eq!(header.round(), 0);
assert_eq!(header.height(), 0);
assert_eq!(header.cumulative_weight(), 0);
assert_eq!(header.coinbase_target(), CurrentNetwork::GENESIS_COINBASE_TARGET);
assert_eq!(header.proof_target(), CurrentNetwork::GENESIS_PROOF_TARGET);
assert_eq!(header.last_coinbase_target(), CurrentNetwork::GENESIS_COINBASE_TARGET);
assert_eq!(header.last_coinbase_timestamp(), CurrentNetwork::GENESIS_TIMESTAMP);
assert_eq!(header.timestamp(), CurrentNetwork::GENESIS_TIMESTAMP);
// Ensure the genesis block does *not* contain the following.
assert_ne!(header.transactions_root(), Field::zero());
assert_ne!(header.finalize_root(), Field::zero());
assert_ne!(header.ratifications_root(), Field::zero());
}
}