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

chore: add an example named is13 #61

Merged
merged 3 commits into from
May 29, 2019
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
**/*.rs.bk
Cargo.lock
.deps/
/examples/is13
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ You can also run the tests:
make test
```

CKB VM has already included RISC-V binaries used in tests, so you don't need a RISC-V compiler to build binaries. However if you do want to play with your own binaries, a RISC-V compiler might be needed. [riscv-tools](https://github.com/riscv/riscv-tools) can be a good starting point here, or if you are an expert on GNU toolchain, you might also compile upstream GCC from source with RISC-V support. CKB VM is using standard RISC-V instructions and ELF binary format, so theoretically any RISC-V compatible compilers are able to produce contracts used in CKB VM(tho bug reports are very welcome if you find breakage).
CKB VM has already included RISC-V binaries used in tests, so you don't need a RISC-V compiler to build binaries. However if you do want to play with your own binaries, a RISC-V compiler might be needed. [riscv-tools](https://github.com/riscv/riscv-tools) can be a good starting point here, or if you are an expert on GNU toolchain, you might also compile upstream GCC from source with RISC-V support, [here](./examples/is13.rs) is an example. CKB VM is using standard RISC-V instructions and ELF binary format, so theoretically any RISC-V compatible compilers are able to produce contracts used in CKB VM(tho bug reports are very welcome if you find breakage).
25 changes: 25 additions & 0 deletions examples/is13.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <string.h>

int is13(char* data) {
if (strcmp(data, "13") == 0) {
return 0;
}
if (strcmp(data, "0xd") == 0) {
return 0;
}
if (strcmp(data, "0o15") == 0) {
return 0;
}
if (strcmp(data, "0b1101") == 0) {
return 0;
}
return 1;
}

int main(int argc, char* argv[]) {
if (argc == 1) {
return 1;
}
return is13(argv[1]);
}

45 changes: 45 additions & 0 deletions examples/is13.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Example "is-thirteen" is a useless(almost) program for just checking if a number is equal to 13.
//
// But it's also important because it shows how to compile a program from C source code and then run
// it by ckb-vm. So, Let's start.
//
// To build the "is13.c" to riscv output, you will need to install "riscv-gnu-toolchain" in your system.
//
// $ git clone --recursive https://github.com/riscv/riscv-gnu-toolchain
// $ cd riscv-gnu-toolchain
// $ mkdir build && cd build
// $ ../configure --prefix=/opt/riscv --with-arch=rv32imac --with-abi=ilp32
// $ make
//
// On my ubuntu machine, it takes 1 hours. Sad.
//
// Then, you can build "is13.c" by "riscv32-unknown-elf-gcc"
//
// $ riscv32-unknown-elf-gcc -o is13 is13.c
doitian marked this conversation as resolved.
Show resolved Hide resolved
//
// Where can you find the "riscv32-unknown-elf-gcc", depending on the previous "../configure --prefix=xxxx"
//
// Now, you have the "is13" binary! Copy the file to this directory, call it by ckb-vm, as shown in the Rust
// code below. And feel free to run this example by command "cargo":
//
// $ cargo run --example is13 13
// or $ cargo run --example is13 0xd
// or $ cargo run --example is13 HELLO
use bytes::Bytes;
use std::io::Read;

fn main() {
let args: Vec<Bytes> = std::env::args().map(|a| a.into()).collect();

let mut file = std::fs::File::open("examples/is13").unwrap();
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).unwrap();
let buffer = Bytes::from(buffer);

let r = ckb_vm::run::<u32, ckb_vm::SparseMemory<u32>>(&buffer, &args[..]).unwrap();
match r {
1 => println!("{:?} is not thirteen", args[1]),
0 => println!("{:?} is thirteen", args[1]),
_ => panic!(""),
}
}