Skip to main content

snarkvm_synthesizer_program/logic/finalize_global_state/
mod.rs

1// Copyright (c) 2019-2026 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 console::network::prelude::*;
17
18#[derive(Copy, Clone, Debug, PartialEq, Eq)]
19pub struct FinalizeGlobalState {
20    /// The block round.
21    block_round: u64,
22    /// The block height.
23    block_height: u32,
24    /// The block timestamp.
25    block_timestamp: Option<i64>, // TODO (raychu86): Consider adding the entire Metadata here instead.
26    /// The block-specific random seed.
27    random_seed: [u8; 32],
28    /// The block spend limit.
29    block_spend_limit: Option<u64>,
30    /// The block synthesis limit.
31    block_synthesis_limit: Option<u64>,
32}
33
34impl FinalizeGlobalState {
35    /// Initializes a new genesis global state.
36    #[inline]
37    pub fn new_genesis<N: Network>() -> Result<Self> {
38        // Initialize the parameters.
39        let block_round = 0;
40        let block_height = 0;
41        let block_cumulative_weight = 0;
42        let block_cumulative_proof_target = 0;
43        let previous_block_hash = N::BlockHash::default();
44        // Return the new global state.
45        Self::new::<N>(
46            block_round,
47            block_height,
48            None,
49            block_cumulative_weight,
50            block_cumulative_proof_target,
51            previous_block_hash,
52            None,
53            None,
54        )
55    }
56
57    /// Initializes a new global state from the given inputs.
58    #[inline]
59    pub fn new<N: Network>(
60        block_round: u64,
61        block_height: u32,
62        block_timestamp: Option<i64>,
63        block_cumulative_weight: u128,
64        block_cumulative_proof_target: u128,
65        previous_block_hash: N::BlockHash,
66        block_spend_limit: Option<u64>,
67        block_synthesis_limit: Option<u64>,
68    ) -> Result<Self> {
69        // Initialize the preimage, optionally including the block timestamp.
70        let preimage = to_bits_le![
71            block_round,
72            block_height,
73            block_cumulative_weight,
74            block_cumulative_proof_target,
75            (*previous_block_hash); 605
76        ]
77        .into_iter()
78        .chain(block_timestamp.into_iter().flat_map(|ts| to_bits_le![ts]))
79        .collect::<Vec<_>>();
80
81        // Hash the preimage to get the random seed.
82        let seed = N::hash_bhp768(&preimage)?.to_bytes_le()?;
83        // Ensure the seed is 32-bytes.
84        ensure!(seed.len() == 32, "Invalid seed length for finalize global state.");
85
86        // Convert the seed into a 32-byte array.
87        let mut random_seed = [0u8; 32];
88        random_seed.copy_from_slice(&seed[..32]);
89
90        Ok(Self { block_round, block_height, block_timestamp, random_seed, block_spend_limit, block_synthesis_limit })
91    }
92
93    /// Initializes a new global state.
94    #[inline]
95    pub const fn from(
96        block_round: u64,
97        block_height: u32,
98        block_timestamp: Option<i64>,
99        random_seed: [u8; 32],
100        block_spend_limit: Option<u64>,
101        block_synthesis_limit: Option<u64>,
102    ) -> Self {
103        Self { block_round, block_height, block_timestamp, random_seed, block_spend_limit, block_synthesis_limit }
104    }
105
106    /// Returns the block round.
107    #[inline]
108    pub const fn block_round(&self) -> u64 {
109        self.block_round
110    }
111
112    /// Returns the block height.
113    #[inline]
114    pub const fn block_height(&self) -> u32 {
115        self.block_height
116    }
117
118    /// Returns the random seed.
119    #[inline]
120    pub const fn random_seed(&self) -> &[u8; 32] {
121        &self.random_seed
122    }
123
124    /// Returns the block timestamp.
125    #[inline]
126    pub const fn block_timestamp(&self) -> Option<i64> {
127        self.block_timestamp
128    }
129
130    /// Returns the block spend limit.
131    #[inline]
132    pub const fn block_spend_limit(&self) -> Option<u64> {
133        self.block_spend_limit
134    }
135
136    /// Returns the block synthesis limit.
137    #[inline]
138    pub const fn block_synthesis_limit(&self) -> Option<u64> {
139        self.block_synthesis_limit
140    }
141}