Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Update ckb js vm #462

Draft
wants to merge 2 commits into
base: v2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion examples/ckb-js-script/deps/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
ckb-js-vm is a binary built for javascript engine on CKB-VM, you can rebuild it from https://github.com/nervosnetwork/ckb-js-vm
# ckb-js-vm

`ckb-js-vm` is a binary built for javascript engine on CKB-VM, you can rebuild it from https://github.com/nervosnetwork/ckb-js-vm

This binary is locked with the commit hash [c158de314783151b6fb023420dd5d5bff2bc2772](https://github.com/nervosnetwork/ckb-js-vm/commit/c158de314783151b6fb023420dd5d5bff2bc2772)

`compile.awk` is copied from https://github.com/nervosnetwork/ckb-js-vm/blob/c158de314783151b6fb023420dd5d5bff2bc2772/tools/compile.awk and used to compile js source code into binary for CKB-VM.
Binary file modified examples/ckb-js-script/deps/ckb-js-vm
Binary file not shown.
14 changes: 14 additions & 0 deletions examples/ckb-js-script/deps/compile.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/awk -f

{
# Check if the line starts with "Script log: "
if ($0 ~ /^Script log: /) {
# Remove "Script log: " from the line
line_data = gensub(/^Script log: /, "", "g")

# If the remaining line is not empty, print it
if (line_data != "") {
print line_data
}
}
}
114 changes: 114 additions & 0 deletions website/docs/script/ScriptHeaders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,120 @@ export const ScriptHeaders: ScriptHeadersType = {
</div>,
],
},
js: {
time: "5 - 7 min",
topics: [
{ label: "Script", link: "/docs/tech-explanation/script" },
{
label: "CKB-VM",
link: "/docs/tech-explanation/ckb-vm",
},
{ label: "Cell Model", link: "/docs/tech-explanation/cell-model" },
{
label: "Transaction",
link: "/docs/tech-explanation/glossary#transaction",
},
],
tools: [
<div>
<a
href="https://git-scm.com/"
target="_blank"
rel="noopener noreferrer"
>
git
</a>
,
<a
href="https://www.tutorialspoint.com/unix_commands/make.htm"
target="_blank"
rel="noopener noreferrer"
>
make
</a>
,
<a
href="https://www.gnu.org/software/sed/"
target="_blank"
rel="noopener noreferrer"
>
sed
</a>
,
<a
href="https://www.gnu.org/software/bash/"
target="_blank"
rel="noopener noreferrer"
>
bash
</a>
,
<a
href="https://linux.die.net/man/1/sha256sum"
target="_blank"
rel="noopener noreferrer"
>
sha256sum
</a>
{" and others Unix utilities."}
</div>,
<div>
<a href={"https://www.rust-lang.org/"}>Rust</a>
{" and riscv64 target: "}{" "}
<code>rustup target add riscv64imac-unknown-none-elf</code>
</div>,
<div>
<a
href={
"https://releases.llvm.org/16.0.0/tools/clang/docs/ReleaseNotes.html"
}
>
Clang 18+
</a>

<ul>
<li>
Debian / Ubuntu:{" "}
<code>
wget https://apt.llvm.org/llvm.sh && chmod +x llvm.sh && sudo
./llvm.sh 18 && rm llvm.sh
</code>
</li>
<li>
Fedora 39+: <code>sudo dnf -y install clang</code>
</li>
<li>
Archlinux: <code>sudo pacman --noconfirm -Syu clang</code>
</li>
<li>
MacOS (with <a href="https://brew.sh/">Homebrew</a>):{" "}
<code>brew install llvm@18</code>
</li>
<li>
Windows (with <a href="https://scoop.sh/">Scoop</a>):{" "}
<code>scoop install llvm yasm</code>
</li>
</ul>
</div>,
<div>
<a href="https://github.com/cargo-generate/cargo-generate">
cargo-generate
</a>
</div>,
<div>
<a href="https://github.com/nervosnetwork/ckb-standalone-debugger">
{"ckb-debugger >= v0.118.0"}
</a>
<ul>
<li>
cargo install --git
https://github.com/nervosnetwork/ckb-standalone-debugger --tag
v0.118.0 ckb-debugger{" "}
</li>
</ul>
</div>,
],
},
typeid: {
time: "5 - 7 min",
topics: [
Expand Down
6 changes: 0 additions & 6 deletions website/docs/script/_script-in-js.mdx

This file was deleted.

30 changes: 18 additions & 12 deletions website/docs/script/js-script.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import ScriptTools from "./_ScriptTools.mdx";
# Tutorial: Run JavaScript Code on CKB

<TutorialHeader
time={ScriptHeaders.basic.time}
topics={ScriptHeaders.basic.topics}
tools={ScriptHeaders.basic.tools}
time={ScriptHeaders.js.time}
topics={ScriptHeaders.js.topics}
tools={ScriptHeaders.js.tools}
/>

## The High-Level Idea
Expand All @@ -40,7 +40,7 @@ The `ckb-js-vm` is a binary that can be used both in the CLI and in the on-chain
binary and give it a try to see if it works as expected.

:::info
You will need `clang 16+` to build the `ckb-js-vm` binary:
You will need `clang 18+` to build the `ckb-js-vm` binary:
:::

```sh
Expand Down Expand Up @@ -87,11 +87,10 @@ ckb-debugger --read-file tests/examples/hello.js --bin build/ckb-js-vm -- -r
```

```bash
Run from file, local access enabled. For testing only.
hello, world
Script log: Run from file, local access enabled. For Testing only.
Script log: hello, world
Run result: 0
Total cycles consumed: 30081070(2.9m)
Transfer cycles: 125121(122.2k), running cycles: 2955949(2.8m)
All cycles: 3016765(2.9M)
```

```mdx-code-block
Expand All @@ -102,7 +101,7 @@ Transfer cycles: 125121(122.2k), running cycles: 2955949(2.8m)
With the `-r` option, `ckb-js-vm` will read a local JS file via CKB-Debugger. This function is
intended for testing purposes and does not function in a production environment. However, we can see the
running output, which includes a `hello, world` message. The run result is 0, indicating that the `hello.js` Script executes successfully.
Also, you can see how many `cycles`(the overhead required to execute a Script) are needed to run the JS Script in the output as well.
Also, you can see how many [cycles](/docs/script/vm-cycle-limits)(the overhead required to execute a Script) are needed to run the JS Script in the output as well.

## Integrate ckb-js-vm

Expand Down Expand Up @@ -191,12 +190,13 @@ cd my-first-script-workspace
mkdir deps
```

Copy the `ckb-js-vm` binary we built before into the `deps` folder. When you're done, it should look like this:
Copy `ckb-js-vm/tools/compile.awk` and the `ckb-js-vm` binary we built before into the `deps` folder. When you're done, it should look like this:

```sh
--build
--contracts
--deps
--compile.awk
--ckb-js-vm
...
```
Expand Down Expand Up @@ -238,7 +238,13 @@ console.log("hello, ckb-js-script!");
#### Compile the `hello.js` into binary with CKB-Debugger

```sh
ckb-debugger --read-file js/hello.js --bin deps/ckb-js-vm -- -c | awk '/Run result: 0/{exit} {print}' | xxd -r -p > js/build/hello.bc
ckb-debugger --read-file js/hello.js --bin deps/ckb-js-vm -- -c | awk -f deps/compile.awk | xxd -r -p > js/build/hello.bc
```

If you are using Mac, change `awk` to `gawk` which can be installed by `brew install gawk`:

```sh
ckb-debugger --read-file js/hello.js --bin deps/ckb-js-vm -- -c | gawk -f deps/compile.awk | xxd -r -p > js/build/hello.bc
```

#### Write tests for the `hello.js` Script
Expand Down Expand Up @@ -429,7 +435,7 @@ cargo test -- --nocapture
```sh
running 1 test
[contract debug] hello, ckb-js-script!
consume cycles: 3070458
consume cycles: 2996560
test tests::hello_script ... ok
```

Expand Down
Loading