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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
mod current;
pub use current::{Data, DurableNonce, State};
use {
    crate::hash::Hash,
    serde_derive::{Deserialize, Serialize},
};

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub enum Versions {
    Legacy(Box<State>),
    /// Current variants have durable nonce and blockhash domains separated.
    Current(Box<State>),
}

impl Versions {
    pub fn new(state: State, separate_domains: bool) -> Self {
        if separate_domains {
            Self::Current(Box::new(state))
        } else {
            Self::Legacy(Box::new(state))
        }
    }

    pub fn state(&self) -> &State {
        match self {
            Self::Legacy(state) => state,
            Self::Current(state) => state,
        }
    }

    /// Returns true if the durable nonce is not in the blockhash domain.
    pub fn separate_domains(&self) -> bool {
        match self {
            Self::Legacy(_) => false,
            Self::Current(_) => true,
        }
    }

    /// Checks if the recent_blockhash field in Transaction verifies, and
    /// returns nonce account data if so.
    pub fn verify_recent_blockhash(
        &self,
        recent_blockhash: &Hash, // Transaction.message.recent_blockhash
        separate_domains: bool,
    ) -> Option<&Data> {
        let state = match self {
            Self::Legacy(state) => {
                if separate_domains {
                    // Legacy durable nonces are invalid and should not
                    // allow durable transactions.
                    return None;
                } else {
                    state
                }
            }
            Self::Current(state) => state,
        };
        match **state {
            State::Uninitialized => None,
            State::Initialized(ref data) => (recent_blockhash == &data.blockhash()).then(|| data),
        }
    }

    // Upgrades legacy nonces out of chain blockhash domains.
    pub fn upgrade(self) -> Option<Self> {
        match self {
            Self::Legacy(mut state) => {
                match *state {
                    // An Uninitialized legacy nonce cannot verify a durable
                    // transaction. The nonce will be upgraded to Current
                    // version when initialized. Therefore there is no need to
                    // upgrade Uninitialized legacy nonces.
                    State::Uninitialized => None,
                    State::Initialized(ref mut data) => {
                        data.durable_nonce = DurableNonce::from_blockhash(
                            &data.blockhash(),
                            true, // separate_domains
                        );
                        Some(Self::Current(state))
                    }
                }
            }
            Self::Current(_) => None,
        }
    }
}

impl From<Versions> for State {
    fn from(versions: Versions) -> Self {
        match versions {
            Versions::Legacy(state) => *state,
            Versions::Current(state) => *state,
        }
    }
}

#[cfg(test)]
mod tests {
    use {
        super::*,
        crate::{fee_calculator::FeeCalculator, hash::hashv, pubkey::Pubkey},
    };

    #[test]
    fn test_verify_recent_blockhash() {
        let blockhash: Hash = hashv(&[&[171u8; 32]]);
        let versions = Versions::Legacy(Box::new(State::Uninitialized));
        for separate_domains in [false, true] {
            assert_eq!(
                versions.verify_recent_blockhash(&blockhash, separate_domains),
                None
            );
            assert_eq!(
                versions.verify_recent_blockhash(&Hash::default(), separate_domains),
                None
            );
        }
        let versions = Versions::Current(Box::new(State::Uninitialized));
        for separate_domains in [false, true] {
            assert_eq!(
                versions.verify_recent_blockhash(&blockhash, separate_domains),
                None
            );
            assert_eq!(
                versions.verify_recent_blockhash(&Hash::default(), separate_domains),
                None
            );
        }
        let durable_nonce =
            DurableNonce::from_blockhash(&blockhash, /*separate_domains:*/ false);
        let data = Data {
            authority: Pubkey::new_unique(),
            durable_nonce,
            fee_calculator: FeeCalculator {
                lamports_per_signature: 2718,
            },
        };
        let versions = Versions::Legacy(Box::new(State::Initialized(data.clone())));
        let separate_domains = false;
        assert_eq!(
            versions.verify_recent_blockhash(&Hash::default(), separate_domains),
            None
        );
        assert_eq!(
            versions.verify_recent_blockhash(&blockhash, separate_domains),
            Some(&data)
        );
        assert_eq!(
            versions.verify_recent_blockhash(&data.blockhash(), separate_domains),
            Some(&data)
        );
        assert_eq!(
            versions.verify_recent_blockhash(durable_nonce.as_hash(), separate_domains),
            Some(&data)
        );
        let separate_domains = true;
        assert_eq!(
            versions.verify_recent_blockhash(&Hash::default(), separate_domains),
            None
        );
        assert_eq!(
            versions.verify_recent_blockhash(&blockhash, separate_domains),
            None
        );
        assert_eq!(
            versions.verify_recent_blockhash(&data.blockhash(), separate_domains),
            None
        );
        assert_eq!(
            versions.verify_recent_blockhash(durable_nonce.as_hash(), separate_domains),
            None
        );
        let durable_nonce =
            DurableNonce::from_blockhash(&blockhash, /*separate_domains:*/ true);
        assert_ne!(data.durable_nonce, durable_nonce);
        let data = Data {
            durable_nonce,
            ..data
        };
        let versions = Versions::Current(Box::new(State::Initialized(data.clone())));
        for separate_domains in [false, true] {
            assert_eq!(
                versions.verify_recent_blockhash(&blockhash, separate_domains),
                None
            );
            assert_eq!(
                versions.verify_recent_blockhash(&Hash::default(), separate_domains),
                None
            );
            assert_eq!(
                versions.verify_recent_blockhash(&data.blockhash(), separate_domains),
                Some(&data)
            );
            assert_eq!(
                versions.verify_recent_blockhash(durable_nonce.as_hash(), separate_domains),
                Some(&data)
            );
        }
    }

    #[test]
    fn test_nonce_versions_upgrade() {
        // Uninitialized
        let versions = Versions::Legacy(Box::new(State::Uninitialized));
        assert_eq!(versions.upgrade(), None);
        // Initialized
        let blockhash: Hash = hashv(&[&[171u8; 32]]);
        let durable_nonce =
            DurableNonce::from_blockhash(&blockhash, /*separate_domains:*/ false);
        let data = Data {
            authority: Pubkey::new_unique(),
            durable_nonce,
            fee_calculator: FeeCalculator {
                lamports_per_signature: 2718,
            },
        };
        let versions = Versions::Legacy(Box::new(State::Initialized(data.clone())));
        let durable_nonce =
            DurableNonce::from_blockhash(&blockhash, /*separate_domains:*/ true);
        assert_ne!(data.durable_nonce, durable_nonce);
        let data = Data {
            durable_nonce,
            ..data
        };
        let versions = versions.upgrade().unwrap();
        assert_eq!(
            versions,
            Versions::Current(Box::new(State::Initialized(data)))
        );
        assert_eq!(versions.upgrade(), None);
    }
}