solana_program/nonce/state/
current.rs

1use {
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/// Initialized data of a durable transaction nonce account.
16///
17/// This is stored within [`State`] for initialized nonce accounts.
18#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq, Clone)]
19pub struct Data {
20    /// Address of the account that signs transactions using the nonce account.
21    pub authority: Pubkey,
22    /// Durable nonce value derived from a valid previous blockhash.
23    pub durable_nonce: DurableNonce,
24    /// The fee calculator associated with the blockhash.
25    pub fee_calculator: FeeCalculator,
26}
27
28impl Data {
29    /// Create new durable transaction nonce data.
30    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    /// Hash value used as recent_blockhash field in Transactions.
43    /// Named blockhash for legacy reasons, but durable nonce and blockhash
44    /// have separate domains.
45    pub fn blockhash(&self) -> Hash {
46        self.durable_nonce.0
47    }
48
49    /// Get the cost per signature for the next transaction to use this nonce.
50    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    /// Hash value used as recent_blockhash field in Transactions.
61    pub fn as_hash(&self) -> &Hash {
62        &self.0
63    }
64}
65
66/// The state of a durable transaction nonce account.
67///
68/// When created in memory with [`State::default`] or when deserialized from an
69/// uninitialized account, a nonce account will be [`State::Uninitialized`].
70#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
71pub enum State {
72    Uninitialized,
73    Initialized(Data),
74}
75
76impl Default for State {
77    fn default() -> Self {
78        State::Uninitialized
79    }
80}
81
82impl State {
83    /// Create new durable transaction nonce state.
84    pub fn new_initialized(
85        authority: &Pubkey,
86        durable_nonce: DurableNonce,
87        lamports_per_signature: u64,
88    ) -> Self {
89        Self::Initialized(Data::new(*authority, durable_nonce, lamports_per_signature))
90    }
91
92    /// Get the serialized size of the nonce state.
93    pub const fn size() -> usize {
94        80 // see test_nonce_state_size.
95    }
96}
97
98#[cfg(test)]
99mod test {
100    use {super::*, crate::nonce::state::Versions};
101
102    #[test]
103    fn default_is_uninitialized() {
104        assert_eq!(State::default(), State::Uninitialized)
105    }
106
107    #[test]
108    fn test_nonce_state_size() {
109        let data = Versions::new(State::Initialized(Data::default()));
110        let size = bincode::serialized_size(&data).unwrap();
111        assert_eq!(State::size() as u64, size);
112    }
113}