27 unstable releases

new 0.15.1 Mar 12, 2025
0.14.1 Nov 21, 2024
0.12.0 May 17, 2024
0.11.0 Mar 15, 2024
0.2.2 Nov 8, 2020

#290 in Magic Beans

Download history 648/week @ 2024-11-20 417/week @ 2024-11-27 728/week @ 2024-12-04 1152/week @ 2024-12-11 1108/week @ 2024-12-18 1040/week @ 2024-12-25 1062/week @ 2025-01-01 956/week @ 2025-01-08 1148/week @ 2025-01-15 702/week @ 2025-01-22 132/week @ 2025-01-29 461/week @ 2025-02-05 433/week @ 2025-02-12 556/week @ 2025-02-19 1551/week @ 2025-02-26 702/week @ 2025-03-05

3,279 downloads per month
Used in ckb-capsule

MIT license

36KB
652 lines

CKB Test Tool

A helper library for writing CKB script test cases. It is migrated from capsule

[dependencies]
ckb-testtool = "0.15"

Usage

See https://docs.rs/ckb-testtool/latest/ckb_testtool/

License

MIT


lib.rs:

ckb-testtool

This module provides testing context for CKB contracts.

To setup a contract verification context, you may need to import ckb modules to build the transaction structure or calculate the hash result.

ckb-testtool crate provides re-exports of ckb modules.

Example

use ckb_testtool::context::Context;
use ckb_testtool::ckb_types::{
    bytes::Bytes,
    core::TransactionBuilder,
    packed::*,
    prelude::*,
};
use std::fs;

// Max cycles of verification.
const MAX_CYCLES: u64 = 10_000_000;

#[test]
fn test_basic() {
    // Init testing context.
    let mut context = Context::default();
    let contract_bin: Bytes = fs::read("my_contract").unwrap().into();

    // Deploy contract.
    let out_point = context.deploy_cell(contract_bin);

    // Prepare scripts and cell dep.
    let lock_script = context
        .build_script(&out_point, Default::default())
        .expect("script");

    // Prepare input cell.
    let input_out_point = context.create_cell(
        CellOutput::new_builder()
            .capacity(1000u64.pack())
            .lock(lock_script.clone())
            .build(),
        Bytes::new(),
    );
    let input = CellInput::new_builder()
        .previous_output(input_out_point)
        .build();

    // Outputs.
    let outputs = vec![
        CellOutput::new_builder()
            .capacity(500u64.pack())
            .lock(lock_script.clone())
            .build(),
        CellOutput::new_builder()
            .capacity(500u64.pack())
            .lock(lock_script)
            .build(),
    ];

    let outputs_data = vec![Bytes::new(); 2];

    // Build transaction.
    let tx = TransactionBuilder::default()
        .input(input)
        .outputs(outputs)
        .outputs_data(outputs_data.pack())
        .build();

    let tx = context.complete_tx(tx);

    // Run.
    let cycles = context
        .verify_tx(&tx, MAX_CYCLES)
        .expect("pass verification");
    println!("consume cycles: {}", cycles);
}

The ckb-testtool also supports native simulation. To use this mode, you need to enable the native-simulator feature. Next, you should create a new native simulation project. In the new project, you only need to add one line in main.rs:

ckb_std::entry_simulator!(script::program_entry);

Recompile it. ckb-testtool will automatically locate it in the contract search path. You can refer to path tests/test-contracts/native-simulators/simple-spawn-sim for relevant examples.

Dependencies

~15–44MB
~694K SLoC