#syscalls #ckb #write #module #contract #help #dynamic

ckb-std

This library contains serveral modules help you write CKB contract with Rust

37 releases

new 0.16.4 Nov 20, 2024
0.16.1 Oct 28, 2024
0.15.3 Mar 25, 2024
0.14.3 Aug 25, 2023
0.1.1 Mar 25, 2020

#6 in #ckb

Download history 321/week @ 2024-07-29 132/week @ 2024-08-05 150/week @ 2024-08-12 71/week @ 2024-08-19 227/week @ 2024-08-26 406/week @ 2024-09-02 179/week @ 2024-09-09 126/week @ 2024-09-16 261/week @ 2024-09-23 105/week @ 2024-09-30 46/week @ 2024-10-07 179/week @ 2024-10-14 168/week @ 2024-10-21 706/week @ 2024-10-28 245/week @ 2024-11-04 121/week @ 2024-11-11

1,257 downloads per month
Used in 4 crates

MIT license

275KB
7K SLoC

C 4K SLoC // 0.1% comments Rust 3K SLoC // 0.0% comments

ckb-std

Crates.io

This library contains several modules that help you write CKB contract with Rust.

Usage

Documentation

Modules

  • syscalls module: defines CKB syscalls
  • high_level module: defines high level APIs
  • dynamic_loading module: dynamic loading primitives
  • debug! macro: a println! like macro helps debugging
  • entry! macro: defines contract entry point
  • default_alloc! macro: defines global allocator for no-std rust
  • dummy_atomic module: dummy atomic operations
  • logger module: colored logger implementation
  • type_id module: Type ID implementation (feature type-id)

Memory allocator

Default allocator uses a mixed allocation strategy:

  • Fixed block heap, only allocate fixed size(64B) memory block
  • Dynamic memory heap, allocate any size memory block

User can invoke macro with arguments to customize the heap size. The default heap size arguments are:

(fixed heap size 4KB, dynamic heap size 516KB, dynamic heap min memory block 64B)

Use the macro with arguments to change it:

default_alloc!(4 * 1024, 516 * 1024, 64)

Beware, use difference heap size or memory block size may affect the verification result of the contract, some runtime errors such as out of memory may occur; you should always test the contract after customizing.

Examples

Check examples and tests to learn how to use.

See also ckb-tool which helps you write tests.

Upgrading Issues

Starting from ckb-std 0.16.0, RISC-V atomic instructions are generated by default. However, ckb-vm doesn't directly support atomic instructions. To address this, ckb-std provides the following solutions:

  1. Use the "dummy-atomic" feature (enabled by default)
  2. Adjust Rust compilation flags by adding -C target-feature=-a to RUSTFLAGS

For more detailed information on compilation flags, refer to the CKB Script Templates repository.

Dependencies

~1.5–7.5MB
~67K SLoC