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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#[allow(deprecated)]
use solana_sdk::sysvar::recent_blockhashes;
use {
    serde::{Deserialize, Serialize},
    solana_sdk::{
        clock::MAX_RECENT_BLOCKHASHES, fee_calculator::FeeCalculator, hash::Hash, timing::timestamp,
    },
    std::collections::HashMap,
};

#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, AbiExample)]
struct HashAge {
    fee_calculator: FeeCalculator,
    hash_index: u64,
    timestamp: u64,
}

/// Low memory overhead, so can be cloned for every checkpoint
#[frozen_abi(digest = "J66ssCYGtWdQu5oyJxFKFeZY86nUjThBdBeXQYuRPDvE")]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, AbiExample)]
pub struct BlockhashQueue {
    /// index of last hash to be registered
    last_hash_index: u64,

    /// last hash to be registered
    last_hash: Option<Hash>,

    ages: HashMap<Hash, HashAge>,

    /// hashes older than `max_age` will be dropped from the queue
    max_age: usize,
}

impl Default for BlockhashQueue {
    fn default() -> Self {
        Self::new(MAX_RECENT_BLOCKHASHES)
    }
}

impl BlockhashQueue {
    pub fn new(max_age: usize) -> Self {
        Self {
            ages: HashMap::new(),
            last_hash_index: 0,
            last_hash: None,
            max_age,
        }
    }

    pub fn last_hash(&self) -> Hash {
        self.last_hash.expect("no hash has been set")
    }

    pub fn get_lamports_per_signature(&self, hash: &Hash) -> Option<u64> {
        self.ages
            .get(hash)
            .map(|hash_age| hash_age.fee_calculator.lamports_per_signature)
    }

    /// Check if the age of the hash is within the queue's max age
    pub fn is_hash_valid(&self, hash: &Hash) -> bool {
        self.ages.get(hash).is_some()
    }

    /// Check if the age of the hash is within the specified age
    pub fn is_hash_valid_for_age(&self, hash: &Hash, max_age: usize) -> bool {
        self.ages
            .get(hash)
            .map(|age| Self::is_hash_index_valid(self.last_hash_index, max_age, age.hash_index))
            .unwrap_or(false)
    }

    pub fn get_hash_age(&self, hash: &Hash) -> Option<u64> {
        self.ages
            .get(hash)
            .map(|age| self.last_hash_index - age.hash_index)
    }

    pub fn genesis_hash(&mut self, hash: &Hash, lamports_per_signature: u64) {
        self.ages.insert(
            *hash,
            HashAge {
                fee_calculator: FeeCalculator::new(lamports_per_signature),
                hash_index: 0,
                timestamp: timestamp(),
            },
        );

        self.last_hash = Some(*hash);
    }

    fn is_hash_index_valid(last_hash_index: u64, max_age: usize, hash_index: u64) -> bool {
        last_hash_index - hash_index <= max_age as u64
    }

    pub fn register_hash(&mut self, hash: &Hash, lamports_per_signature: u64) {
        self.last_hash_index += 1;
        if self.ages.len() >= self.max_age {
            self.ages.retain(|_, age| {
                Self::is_hash_index_valid(self.last_hash_index, self.max_age, age.hash_index)
            });
        }

        self.ages.insert(
            *hash,
            HashAge {
                fee_calculator: FeeCalculator::new(lamports_per_signature),
                hash_index: self.last_hash_index,
                timestamp: timestamp(),
            },
        );

        self.last_hash = Some(*hash);
    }

    #[deprecated(
        since = "1.9.0",
        note = "Please do not use, will no longer be available in the future"
    )]
    #[allow(deprecated)]
    pub fn get_recent_blockhashes(&self) -> impl Iterator<Item = recent_blockhashes::IterItem> {
        (self.ages).iter().map(|(k, v)| {
            recent_blockhashes::IterItem(v.hash_index, k, v.fee_calculator.lamports_per_signature)
        })
    }

    pub(crate) fn get_max_age(&self) -> usize {
        self.max_age
    }
}
#[cfg(test)]
mod tests {
    #[allow(deprecated)]
    use solana_sdk::sysvar::recent_blockhashes::IterItem;
    use {
        super::*,
        bincode::serialize,
        solana_sdk::{clock::MAX_RECENT_BLOCKHASHES, hash::hash},
    };

    #[test]
    fn test_register_hash() {
        let last_hash = Hash::default();
        let mut hash_queue = BlockhashQueue::new(100);
        assert!(!hash_queue.is_hash_valid(&last_hash));
        hash_queue.register_hash(&last_hash, 0);
        assert!(hash_queue.is_hash_valid(&last_hash));
        assert_eq!(hash_queue.last_hash_index, 1);
    }

    #[test]
    fn test_reject_old_last_hash() {
        let mut hash_queue = BlockhashQueue::new(100);
        let last_hash = hash(&serialize(&0).unwrap());
        for i in 0..102 {
            let last_hash = hash(&serialize(&i).unwrap());
            hash_queue.register_hash(&last_hash, 0);
        }
        // Assert we're no longer able to use the oldest hash.
        assert!(!hash_queue.is_hash_valid(&last_hash));
        assert!(!hash_queue.is_hash_valid_for_age(&last_hash, 0));

        // Assert we are not able to use the oldest remaining hash.
        let last_valid_hash = hash(&serialize(&1).unwrap());
        assert!(hash_queue.is_hash_valid(&last_valid_hash));
        assert!(!hash_queue.is_hash_valid_for_age(&last_valid_hash, 0));
    }

    /// test that when max age is 0, that a valid last_hash still passes the age check
    #[test]
    fn test_queue_init_blockhash() {
        let last_hash = Hash::default();
        let mut hash_queue = BlockhashQueue::new(100);
        hash_queue.register_hash(&last_hash, 0);
        assert_eq!(last_hash, hash_queue.last_hash());
        assert!(hash_queue.is_hash_valid_for_age(&last_hash, 0));
    }

    #[test]
    fn test_get_recent_blockhashes() {
        let mut blockhash_queue = BlockhashQueue::new(MAX_RECENT_BLOCKHASHES);
        #[allow(deprecated)]
        let recent_blockhashes = blockhash_queue.get_recent_blockhashes();
        // Sanity-check an empty BlockhashQueue
        assert_eq!(recent_blockhashes.count(), 0);
        for i in 0..MAX_RECENT_BLOCKHASHES {
            let hash = hash(&serialize(&i).unwrap());
            blockhash_queue.register_hash(&hash, 0);
        }
        #[allow(deprecated)]
        let recent_blockhashes = blockhash_queue.get_recent_blockhashes();
        // Verify that the returned hashes are most recent
        #[allow(deprecated)]
        for IterItem(_slot, hash, _lamports_per_signature) in recent_blockhashes {
            assert!(blockhash_queue.is_hash_valid_for_age(hash, MAX_RECENT_BLOCKHASHES));
        }
    }

    #[test]
    fn test_len() {
        const MAX_AGE: usize = 10;
        let mut hash_queue = BlockhashQueue::new(MAX_AGE);
        assert_eq!(hash_queue.ages.len(), 0);

        for _ in 0..MAX_AGE {
            hash_queue.register_hash(&Hash::new_unique(), 0);
        }
        assert_eq!(hash_queue.ages.len(), MAX_AGE);

        // Show that the queue actually holds one more entry than the max age.
        // This is because the most recent hash is considered to have an age of 0,
        // which is likely the result of an unintentional off-by-one error in the past.
        hash_queue.register_hash(&Hash::new_unique(), 0);
        assert_eq!(hash_queue.ages.len(), MAX_AGE + 1);

        // Ensure that no additional entries beyond `MAX_AGE + 1` are added
        hash_queue.register_hash(&Hash::new_unique(), 0);
        assert_eq!(hash_queue.ages.len(), MAX_AGE + 1);
    }

    #[test]
    fn test_get_hash_age() {
        const MAX_AGE: usize = 10;
        let mut hash_list: Vec<Hash> = Vec::new();
        hash_list.resize_with(MAX_AGE + 1, Hash::new_unique);

        let mut hash_queue = BlockhashQueue::new(MAX_AGE);
        for hash in &hash_list {
            assert!(hash_queue.get_hash_age(hash).is_none());
        }

        for hash in &hash_list {
            hash_queue.register_hash(hash, 0);
        }

        // Note that the "age" of a hash in the queue is 0-indexed. So when processing
        // transactions in block 50, the hash for block 49 has an age of 0 despite
        // being one block in the past.
        for (age, hash) in hash_list.iter().rev().enumerate() {
            assert_eq!(hash_queue.get_hash_age(hash), Some(age as u64));
        }

        // When the oldest hash is popped, it should no longer return a hash age
        hash_queue.register_hash(&Hash::new_unique(), 0);
        assert!(hash_queue.get_hash_age(&hash_list[0]).is_none());
    }

    #[test]
    fn test_is_hash_valid_for_age() {
        const MAX_AGE: usize = 10;
        let mut hash_list: Vec<Hash> = Vec::new();
        hash_list.resize_with(MAX_AGE + 1, Hash::new_unique);

        let mut hash_queue = BlockhashQueue::new(MAX_AGE);
        for hash in &hash_list {
            assert!(!hash_queue.is_hash_valid_for_age(hash, MAX_AGE));
        }

        for hash in &hash_list {
            hash_queue.register_hash(hash, 0);
        }

        // Note that the "age" of a hash in the queue is 0-indexed. So when checking
        // the age of a hash is within max age, the hash from 11 slots ago is considered
        // to be within the max age of 10.
        for hash in &hash_list {
            assert!(hash_queue.is_hash_valid_for_age(hash, MAX_AGE));
        }

        // When max age is 0, only the most recent blockhash is still considered valid
        assert!(hash_queue.is_hash_valid_for_age(&hash_list[MAX_AGE], 0));
        assert!(!hash_queue.is_hash_valid_for_age(&hash_list[MAX_AGE - 1], 0));
    }
}