use console::network::prelude::*;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct FinalizeGlobalState {
block_round: u64,
block_height: u32,
block_timestamp: Option<i64>, random_seed: [u8; 32],
}
impl FinalizeGlobalState {
#[inline]
pub fn new_genesis<N: Network>() -> Result<Self> {
let block_round = 0;
let block_height = 0;
let block_cumulative_weight = 0;
let block_cumulative_proof_target = 0;
let previous_block_hash = N::BlockHash::default();
Self::new::<N>(
block_round,
block_height,
None,
block_cumulative_weight,
block_cumulative_proof_target,
previous_block_hash,
)
}
#[inline]
pub fn new<N: Network>(
block_round: u64,
block_height: u32,
block_timestamp: Option<i64>,
block_cumulative_weight: u128,
block_cumulative_proof_target: u128,
previous_block_hash: N::BlockHash,
) -> Result<Self> {
let preimage = to_bits_le![
block_round,
block_height,
block_cumulative_weight,
block_cumulative_proof_target,
(*previous_block_hash); 605
]
.into_iter()
.chain(block_timestamp.into_iter().flat_map(|ts| to_bits_le![ts]))
.collect::<Vec<_>>();
let seed = N::hash_bhp768(&preimage)?.to_bytes_le()?;
ensure!(seed.len() == 32, "Invalid seed length for finalize global state.");
let mut random_seed = [0u8; 32];
random_seed.copy_from_slice(&seed[..32]);
Ok(Self { block_round, block_height, block_timestamp, random_seed })
}
#[inline]
pub const fn from(
block_round: u64,
block_height: u32,
block_timestamp: Option<i64>,
random_seed: [u8; 32],
) -> Self {
Self { block_round, block_height, block_timestamp, random_seed }
}
#[inline]
pub const fn block_round(&self) -> u64 {
self.block_round
}
#[inline]
pub const fn block_height(&self) -> u32 {
self.block_height
}
#[inline]
pub const fn random_seed(&self) -> &[u8; 32] {
&self.random_seed
}
#[inline]
pub const fn block_timestamp(&self) -> Option<i64> {
self.block_timestamp
}
}