ckb-script 0.119.0

CKB component to run the type/lock scripts.
Documentation
use ckb_traits::{CellDataProvider, ExtensionProvider, HeaderProvider};
use ckb_types::{
    bytes::Bytes,
    core::{cell::CellMeta, Capacity, HeaderView},
    packed::{self, Byte32, CellOutput, OutPoint},
    prelude::*,
};
use std::collections::HashMap;

#[derive(Default, Clone)]
pub(crate) struct MockDataLoader {
    pub(crate) headers: HashMap<Byte32, HeaderView>,
    pub(crate) extensions: HashMap<Byte32, packed::Bytes>,
}

impl CellDataProvider for MockDataLoader {
    fn get_cell_data(&self, _out_point: &OutPoint) -> Option<Bytes> {
        None
    }

    fn get_cell_data_hash(&self, _out_point: &OutPoint) -> Option<Byte32> {
        None
    }
}

impl HeaderProvider for MockDataLoader {
    fn get_header(&self, block_hash: &Byte32) -> Option<HeaderView> {
        self.headers.get(block_hash).cloned()
    }
}

impl ExtensionProvider for MockDataLoader {
    fn get_block_extension(&self, hash: &Byte32) -> Option<packed::Bytes> {
        self.extensions.get(hash).cloned()
    }
}

pub(crate) fn new_mock_data_loader() -> MockDataLoader {
    MockDataLoader::default()
}

pub(crate) fn build_cell_meta(capacity_bytes: usize, data: Bytes) -> CellMeta {
    let capacity = Capacity::bytes(capacity_bytes).expect("capacity bytes overflow");
    let builder = CellOutput::new_builder().capacity(capacity.pack());
    let data_hash = CellOutput::calc_data_hash(&data);
    CellMeta {
        out_point: OutPoint::default(),
        transaction_info: None,
        cell_output: builder.build(),
        data_bytes: data.len() as u64,
        mem_cell_data: Some(data),
        mem_cell_data_hash: Some(data_hash),
    }
}