oasis_core_runtime/consensus/tendermint/verifier/
cache.rs

1use std::num::NonZeroUsize;
2
3use tendermint::block::Height;
4use tendermint_light_client::types::LightBlock as TMLightBlock;
5
6use crate::common::crypto::{hash::Hash, signature::PublicKey};
7
8/// Verifier cache.
9pub struct Cache {
10    pub last_verified_height: u64,
11    pub last_verified_round: u64,
12    pub last_verified_epoch: u64,
13    pub last_verified_block: Option<TMLightBlock>,
14    pub verified_state_roots: lru::LruCache<u64, (Hash, u64)>,
15    pub host_node_id: PublicKey,
16}
17
18impl Cache {
19    /// Latest known and verified consensus layer height.
20    pub fn latest_known_height(&self) -> Option<u64> {
21        self.last_verified_block
22            .as_ref()
23            .map(|b| b.signed_header.header.height.value())
24    }
25
26    /// Process a new verified consensus layer block and update the cache if needed.
27    pub fn update_verified_block(&mut self, verified_block: &TMLightBlock) {
28        let h = |b: &TMLightBlock| -> Height { b.signed_header.header.height };
29        if let Some(last_verified_block) = self.last_verified_block.as_ref() {
30            if h(verified_block) <= h(last_verified_block) {
31                return;
32            }
33        }
34        self.last_verified_block = Some(verified_block.clone())
35    }
36}
37
38impl Cache {
39    /// Construct a new verifier cache.
40    pub fn new(host_node_id: PublicKey) -> Self {
41        Self {
42            last_verified_height: 0,
43            last_verified_round: 0,
44            last_verified_epoch: 0,
45            last_verified_block: None,
46            verified_state_roots: lru::LruCache::new(NonZeroUsize::new(128).unwrap()),
47            host_node_id,
48        }
49    }
50}