ckb_traits/
cell_data_provider.rs

1use ckb_types::{
2    bytes::Bytes,
3    core::cell::CellMeta,
4    packed::{Byte32, OutPoint},
5};
6
7/// Trait for cell_data storage
8pub trait CellDataProvider {
9    /// Load cell_data from memory, fallback to storage access
10    fn load_cell_data(&self, cell: &CellMeta) -> Option<Bytes> {
11        cell.mem_cell_data
12            .as_ref()
13            .map(ToOwned::to_owned)
14            .or_else(|| self.get_cell_data(&cell.out_point))
15    }
16
17    /// Load cell_data_hash from memory, fallback to storage access
18    fn load_cell_data_hash(&self, cell: &CellMeta) -> Option<Byte32> {
19        cell.mem_cell_data_hash
20            .as_ref()
21            .map(ToOwned::to_owned)
22            .or_else(|| self.get_cell_data_hash(&cell.out_point))
23    }
24
25    /// Fetch cell_data from storage
26    fn get_cell_data(&self, out_point: &OutPoint) -> Option<Bytes>;
27
28    /// Fetch cell_data_hash from storage, please note that loading a large amount of cell data
29    /// and calculating hash may be a performance bottleneck, so here is a separate fn designed
30    /// to facilitate caching.
31    ///
32    /// In unit test or other scenarios that are not performance bottlenecks, you may use the
33    /// results of `get_cell_data` to calculate hash as a default implementation:
34    /// self.get_cell_data(out_point).map(|data| CellOutput::calc_data_hash(&data))
35    fn get_cell_data_hash(&self, out_point: &OutPoint) -> Option<Byte32>;
36}