snarkvm_synthesizer_program/logic/finalize_global_state/
mod.rs1use console::network::prelude::*;
17
18#[derive(Copy, Clone, Debug, PartialEq, Eq)]
19pub struct FinalizeGlobalState {
20 block_round: u64,
22 block_height: u32,
24 block_timestamp: Option<i64>, random_seed: [u8; 32],
28 block_spend_limit: Option<u64>,
30 block_synthesis_limit: Option<u64>,
32}
33
34impl FinalizeGlobalState {
35 #[inline]
37 pub fn new_genesis<N: Network>() -> Result<Self> {
38 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 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 #[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 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 let seed = N::hash_bhp768(&preimage)?.to_bytes_le()?;
83 ensure!(seed.len() == 32, "Invalid seed length for finalize global state.");
85
86 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 #[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 #[inline]
108 pub const fn block_round(&self) -> u64 {
109 self.block_round
110 }
111
112 #[inline]
114 pub const fn block_height(&self) -> u32 {
115 self.block_height
116 }
117
118 #[inline]
120 pub const fn random_seed(&self) -> &[u8; 32] {
121 &self.random_seed
122 }
123
124 #[inline]
126 pub const fn block_timestamp(&self) -> Option<i64> {
127 self.block_timestamp
128 }
129
130 #[inline]
132 pub const fn block_spend_limit(&self) -> Option<u64> {
133 self.block_spend_limit
134 }
135
136 #[inline]
138 pub const fn block_synthesis_limit(&self) -> Option<u64> {
139 self.block_synthesis_limit
140 }
141}