Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
xxuejie committed Nov 26, 2020
0 parents commit dc0efed
Show file tree
Hide file tree
Showing 6 changed files with 640 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
20 changes: 20 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "ckb-x64-simulator"
description = "A simulator that allows running CKB smart contracts on x64 environment for tooling benefits"
version = "0.1.0"
license = "MIT"
authors = ["Nervos Core Dev <[email protected]>"]
edition = "2018"

[lib]
crate-type = ["lib", "staticlib", "cdylib"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ckb-standalone-debugger = { git = "https://github.com/xxuejie/ckb-standalone-debugger", rev = "7c62220" }
ckb-types = { git = "https://github.com/nervosnetwork/ckb", rev = "79d0cc3" }
lazy_static = "1.4"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# ckb-x64-simulator

ckb-x64-simulator provides a simulator environment, which can be used to compile CKB smart contracts to native x64 environment. The result here, is that all the existing toolings on x64 environment, such as valgrind, address sanitizer, undefined behavior sanitizer, code coverage tools, etc. can be used to ensure the security of smart contracts. One day we might reach the point that RISC-V based toolings have caught up, so this simulator can be sunset, but at the moment now, it provides a good tradeoff to boost smart contract security.

While this simulator is written in pure Rust, C based APIs are exposed so it can also be linked against a C based smart contract.
36 changes: 36 additions & 0 deletions include/api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef CKB_X64_SIMULATOR_API_H_
#define CKB_X64_SIMULATOR_API_H_

#include <stddef.h>
#include <stdint.h>

int ckb_exit(int8_t code);
int ckb_load_tx_hash(void* addr, uint64_t* len, size_t offset);
int ckb_load_transaction(void* addr, uint64_t* len, size_t offset);
int ckb_load_script_hash(void* addr, uint64_t* len, size_t offset);
int ckb_load_script(void* addr, uint64_t* len, size_t offset);
int ckb_debug(const char* s);

int ckb_load_cell(void* addr, uint64_t* len, size_t offset, size_t index,
size_t source);
int ckb_load_input(void* addr, uint64_t* len, size_t offset, size_t index,
size_t source);
int ckb_load_header(void* addr, uint64_t* len, size_t offset, size_t index,
size_t source);
int ckb_load_witness(void* addr, uint64_t* len, size_t offset, size_t index,
size_t source);
int ckb_load_cell_by_field(void* addr, uint64_t* len, size_t offset,
size_t index, size_t source, size_t field);
int ckb_load_header_by_field(void* addr, uint64_t* len, size_t offset,
size_t index, size_t source, size_t field);
int ckb_load_input_by_field(void* addr, uint64_t* len, size_t offset,
size_t index, size_t source, size_t field);
int ckb_load_cell_data(void* addr, uint64_t* len, size_t offset, size_t index,
size_t source);

int ckb_dlopen2(const uint8_t* dep_cell_hash, uint8_t hash_type,
uint8_t* aligned_addr, size_t aligned_size, void** handle,
size_t* consumed_size);
void* ckb_dlsym(void* handle, const char* symbol);

#endif /* CKB_X64_SIMULATOR_API_H_ */
43 changes: 43 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
pub const SYS_EXIT: u64 = 93;
pub const SYS_LOAD_TRANSACTION: u64 = 2051;
pub const SYS_LOAD_SCRIPT: u64 = 2052;
pub const SYS_LOAD_TX_HASH: u64 = 2061;
pub const SYS_LOAD_SCRIPT_HASH: u64 = 2062;
pub const SYS_LOAD_CELL: u64 = 2071;
pub const SYS_LOAD_HEADER: u64 = 2072;
pub const SYS_LOAD_INPUT: u64 = 2073;
pub const SYS_LOAD_WITNESS: u64 = 2074;
pub const SYS_LOAD_CELL_BY_FIELD: u64 = 2081;
pub const SYS_LOAD_HEADER_BY_FIELD: u64 = 2082;
pub const SYS_LOAD_INPUT_BY_FIELD: u64 = 2083;
pub const SYS_LOAD_CELL_DATA_AS_CODE: u64 = 2091;
pub const SYS_LOAD_CELL_DATA: u64 = 2092;
pub const SYS_DEBUG: u64 = 2177;

pub const CKB_SUCCESS: i32 = 0;
pub const CKB_INDEX_OUT_OF_BOUND: i32 = 1;
pub const CKB_ITEM_MISSING: i32 = 2;

pub const SOURCE_INPUT: u64 = 1;
pub const SOURCE_OUTPUT: u64 = 2;
pub const SOURCE_CELL_DEP: u64 = 3;
pub const SOURCE_HEADER_DEP: u64 = 4;
pub const SOURCE_GROUP_INPUT: u64 = 0x0100000000000001;
pub const SOURCE_GROUP_OUTPUT: u64 = 0x0100000000000002;
pub const SOURCE_GROUP_CELL_DEP: u64 = 0x0100000000000003;
pub const SOURCE_GROUP_HEADER_DEP: u64 = 0x0100000000000004;

pub const CELL_FIELD_CAPACITY: u64 = 0;
pub const CELL_FIELD_DATA_HASH: u64 = 1;
pub const CELL_FIELD_LOCK: u64 = 2;
pub const CELL_FIELD_LOCK_HASH: u64 = 3;
pub const CELL_FIELD_TYPE: u64 = 4;
pub const CELL_FIELD_TYPE_HASH: u64 = 5;
pub const CELL_FIELD_OCCUPIED_CAPACITY: u64 = 6;

pub const HEADER_FIELD_EPOCH_NUMBER: u64 = 0;
pub const HEADER_FIELD_EPOCH_START_BLOCK_NUMBER: u64 = 1;
pub const HEADER_FIELD_EPOCH_LENGTH: u64 = 2;

pub const INPUT_FIELD_OUT_POINT: u64 = 0;
pub const INPUT_FIELD_SINCE: u64 = 1;
Loading

0 comments on commit dc0efed

Please sign in to comment.