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}
29
30impl FinalizeGlobalState {
31 #[inline]
33 pub fn new_genesis<N: Network>() -> Result<Self> {
34 let block_round = 0;
36 let block_height = 0;
37 let block_cumulative_weight = 0;
38 let block_cumulative_proof_target = 0;
39 let previous_block_hash = N::BlockHash::default();
40 Self::new::<N>(
42 block_round,
43 block_height,
44 None,
45 block_cumulative_weight,
46 block_cumulative_proof_target,
47 previous_block_hash,
48 )
49 }
50
51 #[inline]
53 pub fn new<N: Network>(
54 block_round: u64,
55 block_height: u32,
56 block_timestamp: Option<i64>,
57 block_cumulative_weight: u128,
58 block_cumulative_proof_target: u128,
59 previous_block_hash: N::BlockHash,
60 ) -> Result<Self> {
61 let preimage = to_bits_le![
63 block_round,
64 block_height,
65 block_cumulative_weight,
66 block_cumulative_proof_target,
67 (*previous_block_hash); 605
68 ]
69 .into_iter()
70 .chain(block_timestamp.into_iter().flat_map(|ts| to_bits_le![ts]))
71 .collect::<Vec<_>>();
72
73 let seed = N::hash_bhp768(&preimage)?.to_bytes_le()?;
75 ensure!(seed.len() == 32, "Invalid seed length for finalize global state.");
77
78 let mut random_seed = [0u8; 32];
80 random_seed.copy_from_slice(&seed[..32]);
81
82 Ok(Self { block_round, block_height, block_timestamp, random_seed })
83 }
84
85 #[inline]
87 pub const fn from(
88 block_round: u64,
89 block_height: u32,
90 block_timestamp: Option<i64>,
91 random_seed: [u8; 32],
92 ) -> Self {
93 Self { block_round, block_height, block_timestamp, random_seed }
94 }
95
96 #[inline]
98 pub const fn block_round(&self) -> u64 {
99 self.block_round
100 }
101
102 #[inline]
104 pub const fn block_height(&self) -> u32 {
105 self.block_height
106 }
107
108 #[inline]
110 pub const fn random_seed(&self) -> &[u8; 32] {
111 &self.random_seed
112 }
113
114 #[inline]
116 pub const fn block_timestamp(&self) -> Option<i64> {
117 self.block_timestamp
118 }
119}