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, 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 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 pub const fn size() -> usize {
94 80 }
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}