solana_program/nonce/state/
current.rs1use {
2 crate::{
3 fee_calculator::FeeCalculator,
4 hash::{hashv, Hash},
5 pubkey::Pubkey,
6 },
7 serde_derive::{Deserialize, Serialize},
8};
9
10const DURABLE_NONCE_HASH_PREFIX: &[u8] = "DURABLE_NONCE".as_bytes();
11
12#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Deserialize, Serialize)]
13pub struct DurableNonce(Hash);
14
15#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq, Clone)]
19pub struct Data {
20 pub authority: Pubkey,
22 pub durable_nonce: DurableNonce,
24 pub fee_calculator: FeeCalculator,
26}
27
28impl Data {
29 pub fn new(
31 authority: Pubkey,
32 durable_nonce: DurableNonce,
33 lamports_per_signature: u64,
34 ) -> Self {
35 Data {
36 authority,
37 durable_nonce,
38 fee_calculator: FeeCalculator::new(lamports_per_signature),
39 }
40 }
41
42 pub fn blockhash(&self) -> Hash {
46 self.durable_nonce.0
47 }
48
49 pub fn get_lamports_per_signature(&self) -> u64 {
51 self.fee_calculator.lamports_per_signature
52 }
53}
54
55impl DurableNonce {
56 pub fn from_blockhash(blockhash: &Hash) -> Self {
57 Self(hashv(&[DURABLE_NONCE_HASH_PREFIX, blockhash.as_ref()]))
58 }
59
60 pub fn as_hash(&self) -> &Hash {
62 &self.0
63 }
64}
65
66#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq, Clone)]
71pub enum State {
72 #[default]
73 Uninitialized,
74 Initialized(Data),
75}
76
77impl State {
78 pub fn new_initialized(
80 authority: &Pubkey,
81 durable_nonce: DurableNonce,
82 lamports_per_signature: u64,
83 ) -> Self {
84 Self::Initialized(Data::new(*authority, durable_nonce, lamports_per_signature))
85 }
86
87 pub const fn size() -> usize {
89 80 }
91}
92
93#[cfg(test)]
94mod test {
95 use {super::*, crate::nonce::state::Versions};
96
97 #[test]
98 fn default_is_uninitialized() {
99 assert_eq!(State::default(), State::Uninitialized)
100 }
101
102 #[test]
103 fn test_nonce_state_size() {
104 let data = Versions::new(State::Initialized(Data::default()));
105 let size = bincode::serialized_size(&data).unwrap();
106 assert_eq!(State::size() as u64, size);
107 }
108}