1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use {
    super::Versions,
    crate::{fee_calculator::FeeCalculator, hash::Hash, pubkey::Pubkey},
    serde_derive::{Deserialize, Serialize},
};

#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Clone)]
pub struct Data {
    pub authority: Pubkey,
    pub blockhash: Hash,
    pub fee_calculator: FeeCalculator,
}

impl Data {
    pub fn new(authority: Pubkey, blockhash: Hash, lamports_per_signature: u64) -> Self {
        Data {
            authority,
            blockhash,
            fee_calculator: FeeCalculator::new(lamports_per_signature),
        }
    }
    pub fn get_lamports_per_signature(&self) -> u64 {
        self.fee_calculator.lamports_per_signature
    }
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub enum State {
    Uninitialized,
    Initialized(Data),
}

impl Default for State {
    fn default() -> Self {
        State::Uninitialized
    }
}

impl State {
    pub fn new_initialized(
        authority: &Pubkey,
        blockhash: &Hash,
        lamports_per_signature: u64,
    ) -> Self {
        Self::Initialized(Data::new(*authority, *blockhash, lamports_per_signature))
    }
    pub fn size() -> usize {
        let data = Versions::new_current(State::Initialized(Data::default()));
        bincode::serialized_size(&data).unwrap() as usize
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn default_is_uninitialized() {
        assert_eq!(State::default(), State::Uninitialized)
    }
}