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
//! TX verification cache

use ckb_script::TransactionSnapshot;
use ckb_types::{
    core::{Capacity, Cycle},
    packed::Byte32,
};
use std::sync::Arc;

/// TX verification lru cache
pub type TxVerificationCache = lru::LruCache<Byte32, CacheEntry>;

const CACHE_SIZE: usize = 1000 * 30;

/// Initialize cache
pub fn init_cache() -> TxVerificationCache {
    lru::LruCache::new(CACHE_SIZE)
}

#[derive(Clone, Debug)]
/// TX verification lru entry
pub enum CacheEntry {
    /// Completed
    Completed(Completed),
    /// Suspended
    Suspended(Suspended),
}

/// Suspended state
#[derive(Clone, Debug)]
pub struct Suspended {
    /// Cached tx fee
    pub fee: Capacity,
    /// Snapshot
    pub snap: Arc<TransactionSnapshot>,
}

/// Completed entry
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Completed {
    /// Cached tx cycles
    pub cycles: Cycle,
    /// Cached tx fee
    pub fee: Capacity,
}

impl CacheEntry {
    /// Constructs a completed CacheEntry
    pub fn completed(cycles: Cycle, fee: Capacity) -> Self {
        CacheEntry::Completed(Completed { cycles, fee })
    }

    /// Constructs a Suspended CacheEntry
    pub fn suspended(snap: Arc<TransactionSnapshot>, fee: Capacity) -> Self {
        CacheEntry::Suspended(Suspended { snap, fee })
    }
}