25 releases (12 breaking)

new 0.14.1 Nov 21, 2024
0.13.2 Oct 17, 2024
0.12.0 May 17, 2024
0.11.0 Mar 15, 2024
0.2.2 Nov 8, 2020

#28 in #ckb

Download history 279/week @ 2024-07-31 140/week @ 2024-08-07 69/week @ 2024-08-14 79/week @ 2024-08-21 145/week @ 2024-08-28 221/week @ 2024-09-04 101/week @ 2024-09-11 392/week @ 2024-09-18 187/week @ 2024-09-25 70/week @ 2024-10-02 94/week @ 2024-10-09 916/week @ 2024-10-16 555/week @ 2024-10-23 465/week @ 2024-10-30 287/week @ 2024-11-06 630/week @ 2024-11-13

2,163 downloads per month
Used in ckb-capsule

MIT license

34KB
680 lines

CKB Test Tool

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


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);
}

Dependencies

~19–49MB
~785K SLoC