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, Default, Serialize, Deserialize, PartialEq, Eq, Clone)]
71pub enum State {
72    #[default]
73    Uninitialized,
74    Initialized(Data),
75}
76
77impl State {
78    /// Create new durable transaction nonce state.
79    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    /// Get the serialized size of the nonce state.
88    pub const fn size() -> usize {
89        80 // see test_nonce_state_size.
90    }
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}