ckb_testtool/
context.rs

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
use crate::tx_verifier::OutputsDataVerifier;
use ckb_chain_spec::consensus::{ConsensusBuilder, TYPE_ID_CODE_HASH};
use ckb_error::Error as CKBError;
use ckb_mock_tx_types::{MockCellDep, MockInfo, MockInput, MockTransaction, ReprMockTransaction};
use ckb_script::{TransactionScriptsVerifier, TxVerifyEnv};
use ckb_traits::{CellDataProvider, ExtensionProvider, HeaderProvider};
use ckb_types::{
    bytes::Bytes,
    core::{
        cell::{CellMeta, CellMetaBuilder, ResolvedTransaction},
        hardfork::{HardForks, CKB2021, CKB2023},
        Capacity, Cycle, DepType, EpochExt, HeaderBuilder, HeaderView, ScriptHashType,
        TransactionInfo, TransactionView,
    },
    packed::{Byte32, CellDep, CellDepBuilder, CellOutput, OutPoint, OutPointVec, Script},
    prelude::*,
};
use rand::{rngs::StdRng, thread_rng, Rng, SeedableRng};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};

/// Return a random hash
pub fn random_hash() -> Byte32 {
    let mut rng = thread_rng();
    let mut buf = [0u8; 32];
    rng.fill(&mut buf);
    buf.pack()
}

/// Return a random OutPoint
pub fn random_out_point() -> OutPoint {
    OutPoint::new_builder().tx_hash(random_hash()).build()
}

/// Return a random Type ID Script
pub fn random_type_id_script() -> Script {
    let args = random_hash().as_bytes();
    debug_assert_eq!(args.len(), 32);
    Script::new_builder()
        .code_hash(TYPE_ID_CODE_HASH.pack())
        .hash_type(ScriptHashType::Type.into())
        .args(args.pack())
        .build()
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Message {
    pub id: Byte32,
    pub message: String,
}

/// Verification Context
#[derive(Clone)]
pub struct Context {
    pub cells: HashMap<OutPoint, (CellOutput, Bytes)>,
    pub transaction_infos: HashMap<OutPoint, TransactionInfo>,
    pub headers: HashMap<Byte32, HeaderView>,
    pub epoches: HashMap<Byte32, EpochExt>,
    pub block_extensions: HashMap<Byte32, Bytes>,
    pub cells_by_data_hash: HashMap<Byte32, OutPoint>,
    pub cells_by_type_hash: HashMap<Byte32, OutPoint>,
    deterministic_rng: bool,
    capture_debug: bool,
    captured_messages: Arc<Mutex<Vec<Message>>>,
    contracts_dirs: Vec<PathBuf>,
    #[cfg(feature = "native-simulator")]
    simulator_binaries: HashMap<Byte32, PathBuf>,
    #[cfg(feature = "native-simulator")]
    simulator_bin_name: String,
}

impl Default for Context {
    fn default() -> Self {
        use std::env;
        // Get from $TOP/build/$MODE
        let mut contracts_dir = env::var("TOP").map(PathBuf::from).unwrap_or_default();

        contracts_dir.push("build");
        if !contracts_dir.exists() {
            contracts_dir.pop();
            contracts_dir.push("../build");
        }
        let contracts_dir = contracts_dir.join(env::var("MODE").unwrap_or("release".to_string()));

        Self {
            cells: Default::default(),
            transaction_infos: Default::default(),
            headers: Default::default(),
            epoches: Default::default(),
            block_extensions: Default::default(),
            cells_by_data_hash: Default::default(),
            cells_by_type_hash: Default::default(),
            deterministic_rng: false,
            capture_debug: Default::default(),
            captured_messages: Default::default(),
            contracts_dirs: vec![contracts_dir],
            #[cfg(feature = "native-simulator")]
            simulator_binaries: Default::default(),
            #[cfg(feature = "native-simulator")]
            simulator_bin_name: "lib<contract>_sim".to_string(),
        }
    }
}

impl Context {
    /// Create a new context with a deterministic random number generator, which can be used to generate deterministic out point of deployed contract
    pub fn new_with_deterministic_rng() -> Self {
        Self {
            deterministic_rng: true,
            ..Default::default()
        }
    }
    pub fn add_contract_dir(&mut self, path: &str) {
        self.contracts_dirs.push(path.into());
    }

    #[deprecated(since = "0.1.1", note = "Please use the deploy_cell function instead")]
    pub fn deploy_contract(&mut self, data: Bytes) -> OutPoint {
        self.deploy_cell(data)
    }

    /// Deploy a cell
    /// return the out-point of the cell
    pub fn deploy_cell(&mut self, data: Bytes) -> OutPoint {
        let data_hash = CellOutput::calc_data_hash(&data);
        if let Some(out_point) = self.cells_by_data_hash.get(&data_hash) {
            // contract has been deployed
            return out_point.to_owned();
        }
        let (out_point, type_id_script) = if self.deterministic_rng {
            let mut rng = StdRng::from_seed(data_hash.as_slice().try_into().unwrap());
            let mut tx_hash = [0u8; 32];
            rng.fill(&mut tx_hash);
            let mut script_args = [0u8; 32];
            rng.fill(&mut script_args);
            (
                OutPoint::new_builder().tx_hash(tx_hash.pack()).build(),
                Script::new_builder()
                    .code_hash(TYPE_ID_CODE_HASH.pack())
                    .hash_type(ScriptHashType::Type.into())
                    .args(script_args.as_slice().pack())
                    .build(),
            )
        } else {
            (random_out_point(), random_type_id_script())
        };
        let type_id_hash = type_id_script.calc_script_hash();
        let cell = {
            let cell = CellOutput::new_builder()
                .type_(Some(type_id_script).pack())
                .build();
            let occupied_capacity = cell
                .occupied_capacity(Capacity::bytes(data.len()).expect("data occupied capacity"))
                .expect("cell capacity");
            cell.as_builder().capacity(occupied_capacity.pack()).build()
        };
        self.cells.insert(out_point.clone(), (cell, data));
        self.cells_by_data_hash.insert(data_hash, out_point.clone());
        self.cells_by_type_hash
            .insert(type_id_hash, out_point.clone());
        out_point
    }

    pub fn deploy_cell_by_name(&mut self, filename: &str) -> OutPoint {
        let path = self.get_contract_path(filename).expect("get contract path");
        let data = std::fs::read(&path).unwrap_or_else(|_| panic!("read local file: {:?}", path));

        #[cfg(feature = "native-simulator")]
        {
            let native_path = self.get_native_simulator_path(filename);
            if native_path.is_some() {
                let code_hash = CellOutput::calc_data_hash(&data);
                self.simulator_binaries
                    .insert(code_hash, native_path.unwrap());
            }
        }

        self.deploy_cell(data.into())
    }

    fn get_contract_path(&self, filename: &str) -> Option<PathBuf> {
        for dir in &self.contracts_dirs {
            let path = dir.join(filename);
            if path.is_file() {
                return Some(path);
            }
        }

        None
    }

    #[cfg(feature = "native-simulator")]
    fn get_native_simulator_path(&self, filename: &str) -> Option<PathBuf> {
        let cdylib_name = format!(
            "{}.{}",
            self.simulator_bin_name
                .replace("<contract>", &filename.replace("-", "_")),
            std::env::consts::DLL_EXTENSION
        );
        for dir in &self.contracts_dirs {
            let path = dir.join(&cdylib_name);
            if path.is_file() {
                return Some(path);
            }
        }
        None
    }

    /// Insert a block header into context
    pub fn insert_header(&mut self, header: HeaderView) {
        self.headers.insert(header.hash(), header);
    }

    /// Link a cell with a block
    /// to make the load_header_by_cell syscalls works
    pub fn link_cell_with_block(
        &mut self,
        out_point: OutPoint,
        block_hash: Byte32,
        tx_index: usize,
    ) {
        let header = self
            .headers
            .get(&block_hash)
            .expect("can't find the header");
        self.transaction_infos.insert(
            out_point,
            TransactionInfo::new(header.number(), header.epoch(), block_hash, tx_index),
        );
    }

    #[deprecated(
        since = "0.1.1",
        note = "Please use the get_cell_by_data_hash function instead"
    )]
    pub fn get_contract_out_point(&self, data_hash: &Byte32) -> Option<OutPoint> {
        self.get_cell_by_data_hash(data_hash)
    }

    /// Get the out-point of a cell by data_hash
    /// the cell must has deployed to this context
    pub fn get_cell_by_data_hash(&self, data_hash: &Byte32) -> Option<OutPoint> {
        self.cells_by_data_hash.get(data_hash).cloned()
    }

    /// Create a cell with data
    /// return the out-point
    pub fn create_cell(&mut self, cell: CellOutput, data: Bytes) -> OutPoint {
        let out_point = random_out_point();
        self.create_cell_with_out_point(out_point.clone(), cell, data);
        out_point
    }

    /// Create cell with specified out-point and cell data
    pub fn create_cell_with_out_point(
        &mut self,
        out_point: OutPoint,
        cell: CellOutput,
        data: Bytes,
    ) {
        let data_hash = CellOutput::calc_data_hash(&data);
        self.cells_by_data_hash.insert(data_hash, out_point.clone());
        if let Some(_type) = cell.type_().to_opt() {
            let type_hash = _type.calc_script_hash();
            self.cells_by_type_hash.insert(type_hash, out_point.clone());
        }
        self.cells.insert(out_point, (cell, data));
    }

    #[deprecated(
        since = "0.1.1",
        note = "Please use the create_cell_with_out_point function instead"
    )]
    pub fn insert_cell(&mut self, out_point: OutPoint, cell: CellOutput, data: Bytes) {
        self.create_cell_with_out_point(out_point, cell, data)
    }

    /// Get cell output and data by out-point
    pub fn get_cell(&self, out_point: &OutPoint) -> Option<(CellOutput, Bytes)> {
        self.cells.get(out_point).cloned()
    }

    /// Build script with out_point, hash_type, args
    /// return none if the out-point is not exist
    pub fn build_script_with_hash_type(
        &mut self,
        out_point: &OutPoint,
        hash_type: ScriptHashType,
        args: Bytes,
    ) -> Option<Script> {
        let (cell, contract_data) = self.cells.get(out_point)?;
        let code_hash = match hash_type {
            ScriptHashType::Data | ScriptHashType::Data1 | ScriptHashType::Data2 => {
                CellOutput::calc_data_hash(contract_data)
            }
            ScriptHashType::Type => cell
                .type_()
                .to_opt()
                .expect("get cell's type hash")
                .calc_script_hash(),
        };
        Some(
            Script::new_builder()
                .code_hash(code_hash)
                .hash_type(hash_type.into())
                .args(args.pack())
                .build(),
        )
    }
    /// Build script with out_point, args and hash_type(ScriptHashType::Type)
    /// return none if the out-point is not exist
    pub fn build_script(&mut self, out_point: &OutPoint, args: Bytes) -> Option<Script> {
        self.build_script_with_hash_type(out_point, ScriptHashType::Type, args)
    }

    fn find_cell_dep_for_script(&self, script: &Script) -> CellDep {
        let out_point = match ScriptHashType::try_from(u8::from(script.hash_type()))
            .expect("invalid script hash type")
        {
            ScriptHashType::Data | ScriptHashType::Data1 | ScriptHashType::Data2 => self
                .get_cell_by_data_hash(&script.code_hash())
                .expect("find contract out point by data_hash"),
            ScriptHashType::Type => self
                .cells_by_type_hash
                .get(&script.code_hash())
                .cloned()
                .expect("find contract out point by type_hash"),
        };

        CellDep::new_builder()
            .out_point(out_point)
            .dep_type(DepType::Code.into())
            .build()
    }

    /// Complete cell deps for a transaction
    /// this function searches context cells; generate cell dep for referenced scripts.
    pub fn complete_tx(&mut self, tx: TransactionView) -> TransactionView {
        let mut cell_deps: Vec<CellDep> = Vec::new();

        for cell_dep in tx.cell_deps_iter() {
            cell_deps.push(cell_dep);
        }

        for i in tx.input_pts_iter() {
            if let Some((cell, _data)) = self.cells.get(&i) {
                let dep = self.find_cell_dep_for_script(&cell.lock());
                if !cell_deps.contains(&dep) {
                    cell_deps.push(dep);
                }
                if let Some(script) = cell.type_().to_opt() {
                    let dep = self.find_cell_dep_for_script(&script);
                    if !cell_deps.contains(&dep) {
                        cell_deps.push(dep);
                    }
                }
            }
        }

        for (cell, _data) in tx.outputs_with_data_iter() {
            if let Some(script) = cell.type_().to_opt() {
                let dep = self.find_cell_dep_for_script(&script);
                if !cell_deps.contains(&dep) {
                    cell_deps.push(dep);
                }
            }
        }

        tx.as_advanced_builder()
            .set_cell_deps(Vec::new())
            .cell_deps(cell_deps.pack())
            .build()
    }

    fn build_resolved_tx(&self, tx: &TransactionView) -> ResolvedTransaction {
        let input_cells = tx
            .inputs()
            .into_iter()
            .map(|input| {
                let previous_out_point = input.previous_output();
                let (input_output, input_data) = self.cells.get(&previous_out_point).unwrap();
                let tx_info_opt = self.transaction_infos.get(&previous_out_point);
                let mut b = CellMetaBuilder::from_cell_output(
                    input_output.to_owned(),
                    input_data.to_vec().into(),
                )
                .out_point(previous_out_point);
                if let Some(tx_info) = tx_info_opt {
                    b = b.transaction_info(tx_info.to_owned());
                }
                b.build()
            })
            .collect();
        let mut resolved_cell_deps = vec![];
        let mut resolved_dep_groups = vec![];
        tx.cell_deps().into_iter().for_each(|cell_dep| {
            let mut out_points = vec![];
            if cell_dep.dep_type() == DepType::DepGroup.into() {
                let (dep_group_output, dep_group_data) =
                    self.cells.get(&cell_dep.out_point()).unwrap();
                let dep_group_tx_info_opt = self.transaction_infos.get(&cell_dep.out_point());
                let mut b = CellMetaBuilder::from_cell_output(
                    dep_group_output.to_owned(),
                    dep_group_data.to_vec().into(),
                )
                .out_point(cell_dep.out_point());
                if let Some(tx_info) = dep_group_tx_info_opt {
                    b = b.transaction_info(tx_info.to_owned());
                }
                resolved_dep_groups.push(b.build());

                let sub_out_points =
                    OutPointVec::from_slice(dep_group_data).expect("Parsing dep group error!");
                out_points.extend(sub_out_points);
            } else {
                out_points.push(cell_dep.out_point());
            }

            for out_point in out_points {
                let (dep_output, dep_data) = self.cells.get(&out_point).unwrap();
                let tx_info_opt = self.transaction_infos.get(&out_point);
                let mut b = CellMetaBuilder::from_cell_output(
                    dep_output.to_owned(),
                    dep_data.to_vec().into(),
                )
                .out_point(out_point);
                if let Some(tx_info) = tx_info_opt {
                    b = b.transaction_info(tx_info.to_owned());
                }
                resolved_cell_deps.push(b.build());
            }
        });
        ResolvedTransaction {
            transaction: tx.clone(),
            resolved_cell_deps,
            resolved_inputs: input_cells,
            resolved_dep_groups,
        }
    }

    // check format and consensus rules
    fn verify_tx_consensus(&self, tx: &TransactionView) -> Result<(), CKBError> {
        OutputsDataVerifier::new(tx).verify()?;
        Ok(())
    }

    pub fn capture_debug(&self) -> bool {
        self.capture_debug
    }

    /// Capture debug output, default value is false
    pub fn set_capture_debug(&mut self, capture_debug: bool) {
        self.capture_debug = capture_debug;
    }

    /// return captured messages
    pub fn captured_messages(&self) -> Vec<Message> {
        self.captured_messages.lock().unwrap().clone()
    }

    /// Verify the transaction in CKB-VM
    pub fn verify_tx(&self, tx: &TransactionView, max_cycles: u64) -> Result<Cycle, CKBError> {
        self.verify_tx_consensus(tx)?;
        let resolved_tx = self.build_resolved_tx(tx);
        let consensus = ConsensusBuilder::default()
            .hardfork_switch(HardForks {
                ckb2021: CKB2021::new_dev_default(),
                ckb2023: CKB2023::new_dev_default(),
            })
            .build();
        let tip = HeaderBuilder::default().number(0.pack()).build();
        let tx_verify_env = TxVerifyEnv::new_submit(&tip);
        let mut verifier = TransactionScriptsVerifier::new(
            Arc::new(resolved_tx),
            self.clone(),
            Arc::new(consensus),
            Arc::new(tx_verify_env),
        );
        if self.capture_debug {
            let captured_messages = self.captured_messages.clone();
            verifier.set_debug_printer(move |id, message| {
                let msg = Message {
                    id: id.clone(),
                    message: message.to_string(),
                };
                captured_messages.lock().unwrap().push(msg);
            });
        } else {
            verifier.set_debug_printer(|_id, msg| {
                println!("[contract debug] {}", msg);
            });
        }

        #[cfg(feature = "native-simulator")]
        {
            self.native_simulator_verify(tx, verifier, max_cycles)
        }
        #[cfg(not(feature = "native-simulator"))]
        verifier.verify(max_cycles)
    }

    #[cfg(feature = "native-simulator")]
    fn native_simulator_verify<DL>(
        &self,
        tx: &TransactionView,
        verifier: TransactionScriptsVerifier<DL>,
        max_cycles: u64,
    ) -> Result<Cycle, CKBError>
    where
        DL: CellDataProvider + HeaderProvider + ExtensionProvider + Send + Sync + Clone + 'static,
    {
        let mut cycles: Cycle = 0;

        for (hash, group) in verifier.groups() {
            let code_hash = if group.script.hash_type() == ScriptHashType::Type.into() {
                let code_hash = group.script.code_hash();
                let out_point = match self.cells_by_type_hash.get(&code_hash) {
                    Some(out_point) => out_point,
                    None => panic!("unknow code hash(ScriptHashType::Type)"),
                };

                match self.cells.get(out_point) {
                    Some((_cell, bin)) => CellOutput::calc_data_hash(bin),
                    None => panic!("unknow code hash(ScriptHashType::Type) in deps"),
                }
            } else {
                group.script.code_hash()
            };

            let use_cycles = match self.simulator_binaries.get(&code_hash) {
                Some(sim_path) => self.run_simulator(sim_path, tx, group),
                None => {
                    group.script.code_hash();
                    verifier
                        .verify_single(group.group_type, hash, max_cycles)
                        .map_err(|e| e.source(group))?
                }
            };
            let r = cycles.overflowing_add(use_cycles);
            assert!(!r.1, "cycles overflow");
            cycles = r.0;
        }
        Ok(cycles)
    }

    #[cfg(feature = "native-simulator")]
    fn run_simulator(
        &self,
        sim_path: &PathBuf,
        tx: &TransactionView,
        group: &ckb_script::ScriptGroup,
    ) -> u64 {
        println!(
            "run native-simulator: {}",
            sim_path.file_name().unwrap().to_str().unwrap()
        );
        let tmp_dir = if !self.simulator_binaries.is_empty() {
            let tmp_dir = std::env::temp_dir().join("ckb-simulator-debugger");
            if !tmp_dir.exists() {
                std::fs::create_dir(tmp_dir.clone())
                    .expect("create tmp dir: ckb-simulator-debugger");
            }
            let tx_file: PathBuf = tmp_dir.join("ckb_running_tx.json");
            let dump_tx = self.dump_tx(&tx).unwrap();

            let tx_json = serde_json::to_string(&dump_tx).expect("dump tx to string");
            std::fs::write(&tx_file, tx_json).expect("write setup");

            std::env::set_var("CKB_TX_FILE", tx_file.to_str().unwrap());
            Some(tmp_dir)
        } else {
            None
        };
        let running_setup = tmp_dir.as_ref().unwrap().join("ckb_running_setup.json");

        let mut native_binaries = self
            .simulator_binaries
            .iter()
            .map(|(code_hash, path)| {
                let buf = vec![
                    code_hash.as_bytes().to_vec(),
                    vec![0xff],
                    0u32.to_le_bytes().to_vec(),
                    0u32.to_le_bytes().to_vec(),
                ]
                .concat();

                format!(
                    "\"0x{}\" : \"{}\",",
                    faster_hex::hex_string(&buf),
                    path.to_str().unwrap()
                )
            })
            .collect::<Vec<String>>()
            .concat();
        if !native_binaries.is_empty() {
            native_binaries.pop();
        }

        let native_binaries = format!("{{ {} }}", native_binaries);

        let setup = format!(
                "{{\"is_lock_script\": {}, \"is_output\": false, \"script_index\": {}, \"vm_version\": {}, \"native_binaries\": {}, \"run_type\": \"DynamicLib\" }}",
                group.group_type == ckb_script::ScriptGroupType::Lock,
                group.input_indices[0], 2, native_binaries
            );
        std::fs::write(&running_setup, setup).expect("write setup");
        std::env::set_var("CKB_RUNNING_SETUP", running_setup.to_str().unwrap());

        type CkbMainFunc<'a> =
            libloading::Symbol<'a, unsafe extern "C" fn(argc: i32, argv: *const *const i8) -> i8>;
        type SetScriptInfo<'a> = libloading::Symbol<
            'a,
            unsafe extern "C" fn(ptr: *const std::ffi::c_void, tx_ctx_id: u64, vm_ctx_id: u64),
        >;

        // ckb_x64_simulator::run_native_simulator(sim_path);
        unsafe {
            let lib = libloading::Library::new(sim_path).expect("Load library");

            let func: SetScriptInfo = lib
                .get(b"__set_script_info")
                .expect("load function : __update_spawn_info");
            func(std::ptr::null(), 0, 0);

            let func: CkbMainFunc = lib
                .get(b"__ckb_std_main")
                .expect("load function : __ckb_std_main");
            let argv = vec![];
            func(0, argv.as_ptr());
        }
        0
    }

    #[cfg(feature = "native-simulator")]
    pub fn set_simulator(&mut self, code_hash: Byte32, path: &str) {
        let path = PathBuf::from(path);
        assert!(path.is_file());
        self.simulator_binaries.insert(code_hash, path);
    }

    /// Dump the transaction in mock transaction format, so we can offload it to ckb debugger
    pub fn dump_tx(&self, tx: &TransactionView) -> Result<ReprMockTransaction, CKBError> {
        let rtx = self.build_resolved_tx(tx);
        let mut inputs = Vec::with_capacity(rtx.resolved_inputs.len());
        // We are doing it this way so we can keep original since value is available
        for (i, input) in rtx.resolved_inputs.iter().enumerate() {
            inputs.push(MockInput {
                input: rtx.transaction.inputs().get(i).unwrap(),
                output: input.cell_output.clone(),
                data: input.mem_cell_data.clone().unwrap(),
                header: input.transaction_info.clone().map(|info| info.block_hash),
            });
        }
        // MockTransaction keeps both types of cell deps in a single array, the order does
        // not really matter for now
        let mut cell_deps =
            Vec::with_capacity(rtx.resolved_cell_deps.len() + rtx.resolved_dep_groups.len());
        for dep in rtx.resolved_cell_deps.iter() {
            cell_deps.push(MockCellDep {
                cell_dep: CellDepBuilder::default()
                    .out_point(dep.out_point.clone())
                    .dep_type(DepType::Code.into())
                    .build(),
                output: dep.cell_output.clone(),
                data: dep.mem_cell_data.clone().unwrap(),
                header: dep.transaction_info.clone().map(|info| info.block_hash),
            });
        }
        for dep in rtx.resolved_dep_groups.iter() {
            cell_deps.push(MockCellDep {
                cell_dep: CellDepBuilder::default()
                    .out_point(dep.out_point.clone())
                    .dep_type(DepType::DepGroup.into())
                    .build(),
                output: dep.cell_output.clone(),
                data: dep.mem_cell_data.clone().unwrap(),
                header: dep.transaction_info.clone().map(|info| info.block_hash),
            });
        }
        let mut header_deps = Vec::with_capacity(rtx.transaction.header_deps().len());
        let mut extensions = Vec::new();
        for header_hash in rtx.transaction.header_deps_iter() {
            header_deps.push(self.get_header(&header_hash).unwrap());
            if let Some(extension) = self.get_block_extension(&header_hash) {
                extensions.push((header_hash, extension.unpack()));
            }
        }
        Ok(MockTransaction {
            mock_info: MockInfo {
                inputs,
                cell_deps,
                header_deps,
                extensions,
            },
            tx: rtx.transaction.data(),
        }
        .into())
    }
}

impl CellDataProvider for Context {
    // load Cell Data
    fn load_cell_data(&self, cell: &CellMeta) -> Option<Bytes> {
        cell.mem_cell_data
            .as_ref()
            .map(|data| Bytes::from(data.to_vec()))
            .or_else(|| self.get_cell_data(&cell.out_point))
    }

    fn get_cell_data(&self, out_point: &OutPoint) -> Option<Bytes> {
        self.cells
            .get(out_point)
            .map(|(_, data)| Bytes::from(data.to_vec()))
    }

    fn get_cell_data_hash(&self, out_point: &OutPoint) -> Option<Byte32> {
        self.cells
            .get(out_point)
            .map(|(_, data)| CellOutput::calc_data_hash(data))
    }
}

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

impl ExtensionProvider for Context {
    fn get_block_extension(
        &self,
        hash: &ckb_types::packed::Byte32,
    ) -> Option<ckb_types::packed::Bytes> {
        self.block_extensions.get(hash).map(|b| b.pack())
    }
}