1use ckb_mock_tx_types::{MockResourceLoader, MockTransaction};
2use ckb_script::ScriptGroupType;
3use ckb_types::H256;
4use ckb_types::core::HeaderView;
5use ckb_types::packed::{Byte32, CellOutput, OutPoint};
6use ckb_vm::Bytes;
7
8pub struct DummyResourceLoader {}
9
10impl MockResourceLoader for DummyResourceLoader {
11 fn get_header(&mut self, hash: H256) -> Result<Option<HeaderView>, String> {
12 return Err(format!("Header {:x} is missing!", hash));
13 }
14
15 fn get_live_cell(&mut self, out_point: OutPoint) -> Result<Option<(CellOutput, Bytes, Option<Byte32>)>, String> {
16 return Err(format!("Cell: {:?} is missing!", out_point));
17 }
18}
19
20pub struct HumanReadableCycles(pub u64);
21
22impl std::fmt::Display for HumanReadableCycles {
23 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24 write!(f, "{}", self.0)?;
25 if self.0 >= 1024 * 1024 {
26 write!(f, "({:.1}M)", self.0 as f64 / 1024. / 1024.)?;
27 } else if self.0 >= 1024 {
28 write!(f, "({:.1}K)", self.0 as f64 / 1024.)?;
29 } else {
30 }
31 Ok(())
32 }
33}
34
35pub fn get_script_hash_by_index(
38 mock_tx: &MockTransaction,
39 script_group_type: &ScriptGroupType,
40 cell_type: &str,
41 cell_index: usize,
42) -> Byte32 {
43 match (&script_group_type, cell_type) {
44 (ScriptGroupType::Lock, "input") => mock_tx.mock_info.inputs[cell_index].output.calc_lock_hash(),
45 (ScriptGroupType::Type, "input") => mock_tx.mock_info.inputs[cell_index]
46 .output
47 .type_()
48 .to_opt()
49 .expect("cell should have type script")
50 .calc_script_hash(),
51 (ScriptGroupType::Type, "output") => mock_tx
52 .tx
53 .raw()
54 .outputs()
55 .get(cell_index)
56 .expect("index out of bound")
57 .type_()
58 .to_opt()
59 .expect("cell should have type script")
60 .calc_script_hash(),
61 _ => panic!("Invalid specified script: {:?} {} {}", script_group_type, cell_type, cell_index),
62 }
63}