Skip to content

Commit e872ef3

Browse files
committed
Upgrade ckb-std, also allow contracts without simulator code
1 parent e186ad7 commit e872ef3

File tree

15 files changed

+192
-66
lines changed

15 files changed

+192
-66
lines changed

.github/workflows/rust.yml

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ jobs:
3333
make generate CRATE=rlib TEMPLATE=x64-simulator-crate DESTINATION=crates TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
3434
make generate CRATE=c1 TEMPLATE=contract TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
3535
make generate CRATE=c2 TEMPLATE=atomics-contract TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
36-
make generate CRATE=c3 TEMPLATE=stack-reorder-contract TEMPLATE_TYPE=--path TEMPLATE_REPO=..
36+
make generate CRATE=c3 TEMPLATE=stack-reorder-contract TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
37+
make generate CRATE=c4 TEMPLATE=contract-without-simulator TEMPLATE_TYPE=--path TEMPLATE_REPO=..
3738
- name: Submodules
3839
run: cd test-workspace &&
3940
git submodule add https://github.com/nervosnetwork/ckb-c-stdlib deps/ckb-c-stdlib &&
@@ -85,7 +86,8 @@ jobs:
8586
make generate CRATE=rlib TEMPLATE=x64-simulator-crate DESTINATION=crates TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
8687
make generate CRATE=c1 TEMPLATE=contract TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
8788
make generate CRATE=c2 TEMPLATE=atomics-contract TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
88-
make generate CRATE=c3 TEMPLATE=stack-reorder-contract TEMPLATE_TYPE=--path TEMPLATE_REPO=..
89+
make generate CRATE=c3 TEMPLATE=stack-reorder-contract TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
90+
make generate CRATE=c4 TEMPLATE=contract-without-simulator TEMPLATE_TYPE=--path TEMPLATE_REPO=..
8991
- name: Submodules
9092
run: cd test-workspace &&
9193
git submodule add https://github.com/nervosnetwork/ckb-c-stdlib deps/ckb-c-stdlib &&
@@ -125,7 +127,8 @@ jobs:
125127
make generate CRATE=rlib TEMPLATE=x64-simulator-crate DESTINATION=crates TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
126128
make generate CRATE=c1 TEMPLATE=contract TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
127129
make generate CRATE=c2 TEMPLATE=atomics-contract TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
128-
make generate CRATE=c3 TEMPLATE=stack-reorder-contract TEMPLATE_TYPE=--path TEMPLATE_REPO=..
130+
make generate CRATE=c3 TEMPLATE=stack-reorder-contract TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
131+
make generate CRATE=c4 TEMPLATE=contract-without-simulator TEMPLATE_TYPE=--path TEMPLATE_REPO=..
129132
- name: Submodules
130133
run: cd test-workspace &&
131134
git submodule add https://github.com/nervosnetwork/ckb-c-stdlib deps/ckb-c-stdlib &&
@@ -165,7 +168,8 @@ jobs:
165168
make generate CRATE=rlib TEMPLATE=x64-simulator-crate DESTINATION=crates TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
166169
make generate CRATE=c1 TEMPLATE=contract TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
167170
make generate CRATE=c2 TEMPLATE=atomics-contract TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
168-
make generate CRATE=c3 TEMPLATE=stack-reorder-contract TEMPLATE_TYPE=--path TEMPLATE_REPO=..
171+
make generate CRATE=c3 TEMPLATE=stack-reorder-contract TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
172+
make generate CRATE=c4 TEMPLATE=contract-without-simulator TEMPLATE_TYPE=--path TEMPLATE_REPO=..
169173
- name: Submodules
170174
run: cd test-workspace &&
171175
git submodule add https://github.com/nervosnetwork/ckb-c-stdlib deps/ckb-c-stdlib &&
@@ -199,7 +203,8 @@ jobs:
199203
make generate CRATE=rlib TEMPLATE=x64-simulator-crate DESTINATION=crates TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
200204
make generate CRATE=c1 TEMPLATE=contract TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
201205
make generate CRATE=c2 TEMPLATE=atomics-contract TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
202-
make generate CRATE=c3 TEMPLATE=stack-reorder-contract TEMPLATE_TYPE=--path TEMPLATE_REPO=..
206+
make generate CRATE=c3 TEMPLATE=stack-reorder-contract TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
207+
make generate CRATE=c4 TEMPLATE=contract-without-simulator TEMPLATE_TYPE=--path TEMPLATE_REPO=..
203208
- name: Submodules
204209
run: cd test-workspace &&
205210
git submodule add https://github.com/nervosnetwork/ckb-c-stdlib deps/ckb-c-stdlib &&
@@ -211,50 +216,6 @@ jobs:
211216
- name: Run all checks
212217
run: cd test-contract && make build test check clippy
213218

214-
# For now, github action's latest arm64-based macos runner does not support docker. We will have to
215-
# Use an order version for now to test reproducible build on macOS. Maybe when macOS 15 is out we can
216-
# re-test later.
217-
#
218-
# References:
219-
# * https://github.com/orgs/community/discussions/69211
220-
# * https://github.com/marketplace/actions/setup-docker-on-macos
221-
# NOTE: this test is not stable right now, disabling it till we can find a better solution
222-
# macos-x64-reproducible-build:
223-
#
224-
# runs-on: macos-13
225-
#
226-
# steps:
227-
# - uses: actions/checkout@v3
228-
# with:
229-
# submodules: true
230-
# - name: Setup Docker on macOS
231-
# run: brew install docker colima && colima start
232-
# - name: Install llvm 18
233-
# run: brew install llvm@18
234-
# - name: Install riscv64 target
235-
# run: rustup target add riscv64imac-unknown-none-elf
236-
# - name: Install cargo generate
237-
# run: cargo install cargo-generate
238-
# - name: Generate workspace
239-
# run: cargo generate --path . workspace --name test-workspace
240-
# - name: Generate crates && contracts
241-
# run: cd test-workspace &&
242-
# make generate CRATE=clib TEMPLATE=c-wrapper-crate DESTINATION=crates TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
243-
# make generate CRATE=rlib TEMPLATE=x64-simulator-crate DESTINATION=crates TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
244-
# make generate CRATE=c1 TEMPLATE=contract TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
245-
# make generate CRATE=c2 TEMPLATE=atomics-contract TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
246-
# make generate CRATE=c3 TEMPLATE=stack-reorder-contract TEMPLATE_TYPE=--path TEMPLATE_REPO=..
247-
# - name: Submodules
248-
# run: cd test-workspace &&
249-
# git submodule add https://github.com/nervosnetwork/ckb-c-stdlib deps/ckb-c-stdlib &&
250-
# git submodule add https://github.com/xxuejie/lib-dummy-atomics deps/lib-dummy-atomics
251-
# - name: Reproducible build runs
252-
# run: cd test-workspace && ./scripts/reproducible_build_docker --update && ./scripts/reproducible_build_docker --no-clean
253-
# - name: Generate standalone contract
254-
# run: cargo generate --path . standalone-contract --name test-contract
255-
# - name: Reproducible build runs
256-
# run: cd test-contract && ./scripts/reproducible_build_docker --update && ./scripts/reproducible_build_docker --no-clean
257-
258219
windows-build:
259220

260221
runs-on: windows-2019
@@ -284,7 +245,8 @@ jobs:
284245
make generate CRATE=rlib TEMPLATE=x64-simulator-crate DESTINATION=crates TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
285246
make generate CRATE=c1 TEMPLATE=contract TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
286247
make generate CRATE=c2 TEMPLATE=atomics-contract TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
287-
make generate CRATE=c3 TEMPLATE=stack-reorder-contract TEMPLATE_TYPE=--path TEMPLATE_REPO=..
248+
make generate CRATE=c3 TEMPLATE=stack-reorder-contract TEMPLATE_TYPE=--path TEMPLATE_REPO=.. &&
249+
make generate CRATE=c4 TEMPLATE=contract-without-simulator TEMPLATE_TYPE=--path TEMPLATE_REPO=..
288250
- name: Submodules
289251
run: cd test-workspace &&
290252
git submodule add https://github.com/nervosnetwork/ckb-c-stdlib deps/ckb-c-stdlib &&
@@ -329,6 +291,7 @@ jobs:
329291
gmake generate CRATE=c1 TEMPLATE=contract TEMPLATE_TYPE=--path TEMPLATE_REPO=..
330292
gmake generate CRATE=c2 TEMPLATE=atomics-contract TEMPLATE_TYPE=--path TEMPLATE_REPO=..
331293
gmake generate CRATE=c3 TEMPLATE=stack-reorder-contract TEMPLATE_TYPE=--path TEMPLATE_REPO=..
294+
gmake generate CRATE=c4 TEMPLATE=contract-without-simulator TEMPLATE_TYPE=--path TEMPLATE_REPO=..
332295
git submodule add https://github.com/nervosnetwork/ckb-c-stdlib deps/ckb-c-stdlib
333296
git submodule add https://github.com/xxuejie/lib-dummy-atomics deps/lib-dummy-atomics
334297
gmake build test check clippy

atomics-contract/Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
ckb-std = { version = "0.16.3", default-features = false, features = ["allocator", "calc-hash", "ckb-types", "libc"] }
7+
ckb-std = { version = "0.17.0", default-features = false, features = ["allocator", "calc-hash", "ckb-types", "libc"] }
88
log = { version = "0.4.20", default-features = false }
99

1010
[build-dependencies]
1111
cc = "1.0"
12-
13-
[features]
14-
native-simulator = ["ckb-std/native-simulator"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The files in this directory are used to generate code and will be removed automatically after the project template is generated.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
// generated unit test for contract {{project-name}}
3+
#[test]
4+
fn test_{{crate_name}}() {
5+
// deploy contract
6+
let mut context = Context::default();
7+
let contract_bin: Bytes = Loader::default().load_binary("{{project-name}}");
8+
let out_point = context.deploy_cell(contract_bin);
9+
10+
// prepare scripts
11+
let lock_script = context
12+
.build_script(&out_point, Bytes::from(vec![42]))
13+
.expect("script");
14+
15+
// prepare cells
16+
let input_out_point = context.create_cell(
17+
CellOutput::new_builder()
18+
.capacity(1000u64.pack())
19+
.lock(lock_script.clone())
20+
.build(),
21+
Bytes::new(),
22+
);
23+
let input = CellInput::new_builder()
24+
.previous_output(input_out_point)
25+
.build();
26+
let outputs = vec![
27+
CellOutput::new_builder()
28+
.capacity(500u64.pack())
29+
.lock(lock_script.clone())
30+
.build(),
31+
CellOutput::new_builder()
32+
.capacity(500u64.pack())
33+
.lock(lock_script)
34+
.build(),
35+
];
36+
37+
let outputs_data = vec![Bytes::new(); 2];
38+
39+
// build transaction
40+
let tx = TransactionBuilder::default()
41+
.input(input)
42+
.outputs(outputs)
43+
.outputs_data(outputs_data.pack())
44+
.build();
45+
let tx = context.complete_tx(tx);
46+
47+
// run
48+
let cycles = context
49+
.verify_tx(&tx, 10_000_000)
50+
.expect("pass verification");
51+
println!("consume cycles: {}", cycles);
52+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/build
2+
/target
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "{{project-name}}"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
ckb-std = "0.17.0"
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# We cannot use $(shell pwd), which will return unix path format on Windows,
2+
# making it hard to use.
3+
cur_dir = $(dir $(abspath $(firstword $(MAKEFILE_LIST))))
4+
5+
TOP := $(cur_dir)
6+
# RUSTFLAGS that are likely to be tweaked by developers. For example,
7+
# while we enable debug logs by default here, some might want to strip them
8+
# for minimal code size / consumed cycles.
9+
CUSTOM_RUSTFLAGS := -C debug-assertions
10+
# RUSTFLAGS that are less likely to be tweaked by developers. Most likely
11+
# one would want to keep the default values here.
12+
FULL_RUSTFLAGS := -C target-feature=+zba,+zbb,+zbc,+zbs,-a $(CUSTOM_RUSTFLAGS)
13+
# Additional cargo args to append here. For example, one can use
14+
# make test CARGO_ARGS="-- --nocapture" so as to inspect data emitted to
15+
# stdout in unit tests
16+
CARGO_ARGS :=
17+
MODE := release
18+
# Tweak this to change the clang version to use for building C code. By default
19+
# we use a bash script with somes heuristics to find clang in current system.
20+
CLANG := $(shell $(TOP)/scripts/find_clang)
21+
AR := $(subst clang,llvm-ar,$(CLANG))
22+
OBJCOPY := $(subst clang,llvm-objcopy,$(CLANG))
23+
# When this is set to some value, the generated binaries will be copied over
24+
BUILD_DIR :=
25+
# Generated binaries to copy. By convention, a Rust crate's directory name will
26+
# likely match the crate name, which is also the name of the final binary.
27+
# However if this is not the case, you can tweak this variable. As the name hints,
28+
# more than one binary is supported here.
29+
BINARIES := $(notdir $(shell pwd))
30+
31+
ifeq (release,$(MODE))
32+
MODE_ARGS := --release
33+
endif
34+
35+
default: build test
36+
37+
build:
38+
RUSTFLAGS="$(FULL_RUSTFLAGS)" TARGET_CC="$(CLANG)" TARGET_AR="$(AR)" \
39+
cargo build --target=riscv64imac-unknown-none-elf $(MODE_ARGS) $(CARGO_ARGS)
40+
@set -eu; \
41+
if [ "x$(BUILD_DIR)" != "x" ]; then \
42+
for binary in $(BINARIES); do \
43+
echo "Copying binary $$binary to build directory"; \
44+
cp $(TOP)/target/riscv64imac-unknown-none-elf/$(MODE)/$$binary $(TOP)/$(BUILD_DIR); \
45+
cp $(TOP)/$(BUILD_DIR)/$$binary $(TOP)/$(BUILD_DIR)/$$binary.debug; \
46+
$(OBJCOPY) --strip-debug --strip-all $(TOP)/$(BUILD_DIR)/$$binary; \
47+
done \
48+
fi
49+
50+
# test, check, clippy and fmt here are provided for completeness,
51+
# there is nothing wrong invoking cargo directly instead of make.
52+
test:
53+
cargo test $(CARGO_ARGS)
54+
55+
check:
56+
cargo check $(CARGO_ARGS)
57+
58+
clippy:
59+
cargo clippy $(CARGO_ARGS)
60+
61+
fmt:
62+
cargo fmt $(CARGO_ARGS)
63+
64+
# Arbitrary cargo command is supported here. For example:
65+
#
66+
# make cargo CARGO_CMD=expand CARGO_ARGS="--ugly"
67+
#
68+
# Invokes:
69+
# cargo expand --ugly
70+
CARGO_CMD :=
71+
cargo:
72+
cargo $(CARGO_CMD) $(CARGO_ARGS)
73+
74+
clean:
75+
cargo clean
76+
77+
prepare:
78+
rustup target add riscv64imac-unknown-none-elf
79+
80+
.PHONY: build test check clippy fmt cargo clean prepare
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# {{project-name}}
2+
3+
TODO: Write this readme
4+
5+
*This contract was bootstrapped with [ckb-script-templates].*
6+
7+
[ckb-script-templates]: https://github.com/cryptape/ckb-script-templates
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[template]
2+
cargo_generate_version = ">=0.16.0"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![cfg_attr(not(test), no_std)]
2+
#![cfg_attr(not(test), no_main)]
3+
4+
#[cfg(test)]
5+
extern crate alloc;
6+
7+
#[cfg(not(test))]
8+
ckb_std::entry!(program_entry);
9+
// By default, the following heap configuration is used:
10+
// * 16KB fixed heap
11+
// * 1.2MB(rounded up to be 16-byte aligned) dynamic heap
12+
// * Minimal memory block in dynamic heap is 64 bytes
13+
// For more details, please refer to ckb-std's default_alloc macro
14+
// and the buddy-alloc alloc implementation.
15+
ckb_std::default_alloc!(16384, 1258306, 64);
16+
17+
pub fn program_entry() -> i8 {
18+
ckb_std::debug!("This is a sample contract!");
19+
20+
0
21+
}

0 commit comments

Comments
 (0)