solana_program/nonce/state/
current.rsuse {
crate::{
fee_calculator::FeeCalculator,
hash::{hashv, Hash},
pubkey::Pubkey,
},
serde_derive::{Deserialize, Serialize},
};
const DURABLE_NONCE_HASH_PREFIX: &[u8] = "DURABLE_NONCE".as_bytes();
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Deserialize, Serialize)]
pub struct DurableNonce(Hash);
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct Data {
pub authority: Pubkey,
pub durable_nonce: DurableNonce,
pub fee_calculator: FeeCalculator,
}
impl Data {
pub fn new(
authority: Pubkey,
durable_nonce: DurableNonce,
lamports_per_signature: u64,
) -> Self {
Data {
authority,
durable_nonce,
fee_calculator: FeeCalculator::new(lamports_per_signature),
}
}
pub fn blockhash(&self) -> Hash {
self.durable_nonce.0
}
pub fn get_lamports_per_signature(&self) -> u64 {
self.fee_calculator.lamports_per_signature
}
}
impl DurableNonce {
pub fn from_blockhash(blockhash: &Hash) -> Self {
Self(hashv(&[DURABLE_NONCE_HASH_PREFIX, blockhash.as_ref()]))
}
pub fn as_hash(&self) -> &Hash {
&self.0
}
}
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub enum State {
#[default]
Uninitialized,
Initialized(Data),
}
impl State {
pub fn new_initialized(
authority: &Pubkey,
durable_nonce: DurableNonce,
lamports_per_signature: u64,
) -> Self {
Self::Initialized(Data::new(*authority, durable_nonce, lamports_per_signature))
}
pub const fn size() -> usize {
80 }
}
#[cfg(test)]
mod test {
use {super::*, crate::nonce::state::Versions};
#[test]
fn default_is_uninitialized() {
assert_eq!(State::default(), State::Uninitialized)
}
#[test]
fn test_nonce_state_size() {
let data = Versions::new(State::Initialized(Data::default()));
let size = bincode::serialized_size(&data).unwrap();
assert_eq!(State::size() as u64, size);
}
}