From 050c0b76a835d4eb3f7c3ac0b0d42a4752c92ce7 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Tue, 30 May 2017 13:53:18 +0300 Subject: [PATCH] remove demos (moved to https://github.com/nikvolf/wasm-tools) --- js-runner/.gitignore | 1 - js-runner/build.sh | 33 ---- js-runner/index.html | 343 ----------------------------------- js-runner/linker_emcc.sh | 4 - js-runner/start.sh | 2 - rust-runner/.gitignore | 2 - rust-runner/Cargo.toml | 8 - rust-runner/src/alloc.rs | 27 --- rust-runner/src/call_args.rs | 69 ------- rust-runner/src/main.rs | 103 ----------- rust-runner/src/runtime.rs | 180 ------------------ samples/empty_contract.rs | 27 --- samples/import_contract.c | 43 ----- samples/logger_contract.rs | 135 -------------- samples/static_contract.c | 38 ---- samples/static_contract.rs | 8 - samples/storage.rs | 55 ------ 17 files changed, 1078 deletions(-) delete mode 100644 js-runner/.gitignore delete mode 100755 js-runner/build.sh delete mode 100644 js-runner/index.html delete mode 100755 js-runner/linker_emcc.sh delete mode 100644 js-runner/start.sh delete mode 100644 rust-runner/.gitignore delete mode 100644 rust-runner/Cargo.toml delete mode 100644 rust-runner/src/alloc.rs delete mode 100644 rust-runner/src/call_args.rs delete mode 100644 rust-runner/src/main.rs delete mode 100644 rust-runner/src/runtime.rs delete mode 100644 samples/empty_contract.rs delete mode 100644 samples/import_contract.c delete mode 100644 samples/logger_contract.rs delete mode 100644 samples/static_contract.c delete mode 100644 samples/static_contract.rs delete mode 100644 samples/storage.rs diff --git a/js-runner/.gitignore b/js-runner/.gitignore deleted file mode 100644 index c585e19..0000000 --- a/js-runner/.gitignore +++ /dev/null @@ -1 +0,0 @@ -out \ No newline at end of file diff --git a/js-runner/build.sh b/js-runner/build.sh deleted file mode 100755 index cc62c83..0000000 --- a/js-runner/build.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/sh - -# "Compile rust source and put it as a tested contract" - -mkdir -p out - -file=$1 -if [ ${file: -3} == ".rs" ] -then - # Rust is compiled with rustc - rustc $file -o out/contract.js -O --target wasm32-unknown-emscripten -C linker=./linker_emcc.sh - - # Gas injector - cargo run --manifest-path=./../gas/Cargo.toml --release -- ./out/contract.wasm ./out/contract.wasm - - # Allocator replacer - cargo run --manifest-path=./../ext/Cargo.toml --release -- ./out/contract.wasm ./out/contract.wasm - - # Symbols optimizer - cargo run --manifest-path=./../opt/Cargo.toml --release -- ./out/contract.wasm ./out/contract.wasm - -else - # c/c++ can be compiled directly by emcc - emcc $file -O3 -s WASM=1 -s SIDE_MODULE=1 -o out/contract.wasm - - # Gas injector - cargo run --manifest-path=./../gas/Cargo.toml --release -- ./out/contract.wasm ./out/contract.wasm - - # Symbols optimizer - cargo run --manifest-path=./../opt/Cargo.toml --release -- ./out/contract.wasm ./out/contract.wasm -fi - - diff --git a/js-runner/index.html b/js-runner/index.html deleted file mode 100644 index 2ba4e3a..0000000 --- a/js-runner/index.html +++ /dev/null @@ -1,343 +0,0 @@ - - - - - - -
- - Full source with preamble -
- -
- - -
- -
- -
- -
-
- -
- -
- -
-
- -
- - - Gas cost: 0 - - - \ No newline at end of file diff --git a/js-runner/linker_emcc.sh b/js-runner/linker_emcc.sh deleted file mode 100755 index e61cb99..0000000 --- a/js-runner/linker_emcc.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -args="$*" -filtered_args=${args/ERROR_ON_UNDEFINED_SYMBOLS\=1/ERROR_ON_UNDEFINED_SYMBOLS\=0} -emcc $filtered_args \ No newline at end of file diff --git a/js-runner/start.sh b/js-runner/start.sh deleted file mode 100644 index fd970b6..0000000 --- a/js-runner/start.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/sh -python -m SimpleHTTPServer 8000 \ No newline at end of file diff --git a/rust-runner/.gitignore b/rust-runner/.gitignore deleted file mode 100644 index f2f9e58..0000000 --- a/rust-runner/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -target -Cargo.lock \ No newline at end of file diff --git a/rust-runner/Cargo.toml b/rust-runner/Cargo.toml deleted file mode 100644 index efc0b4a..0000000 --- a/rust-runner/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "rust-runner" -version = "0.1.0" -authors = ["NikVolf "] - -[dependencies] -parity-wasm = { git="https://github.com/nikvolf/parity-wasm" } -wasm-utils = { path = "../" } \ No newline at end of file diff --git a/rust-runner/src/alloc.rs b/rust-runner/src/alloc.rs deleted file mode 100644 index 095e179..0000000 --- a/rust-runner/src/alloc.rs +++ /dev/null @@ -1,27 +0,0 @@ -use parity_wasm::interpreter::{self, ModuleInstance}; -use runtime::Runtime; - -pub struct Arena { - pub runtime: Runtime, -} - -#[derive(Debug)] -pub struct Error; - -impl Arena { - pub fn alloc(&self, size: u32) -> Result { - // todo: maybe use unsafe cell since it has nothing to do with threads - let previous_top = self.runtime.env().dynamic_top.get(); - self.runtime.env().dynamic_top.set(previous_top + size); - Ok(previous_top) - } -} - -impl interpreter::UserFunctionInterface for Arena { - fn call(&mut self, _module: &ModuleInstance, context: interpreter::CallerContext) -> Result, interpreter::Error> { - let amount = context.value_stack.pop_as::()?; - self.alloc(amount as u32) - .map(|val| Some((val as i32).into())) - .map_err(|e| interpreter::Error::Trap(format!("Allocator failure: {}", "todo: format arg"))) - } -} \ No newline at end of file diff --git a/rust-runner/src/call_args.rs b/rust-runner/src/call_args.rs deleted file mode 100644 index c36b4e5..0000000 --- a/rust-runner/src/call_args.rs +++ /dev/null @@ -1,69 +0,0 @@ -use parity_wasm::interpreter; -use runtime; - -use WasmMemoryPtr; - -fn write_u32(dst: &mut [u8], val: u32) { - dst[0] = (val & 0x000000ff) as u8; - dst[1] = ((val & 0x0000ff00) >> 8) as u8; - dst[2] = ((val & 0x00ff0000) >> 16) as u8; - dst[3] = ((val & 0xff000000) >> 24) as u8; -} - -#[derive(Debug)] -pub enum Error { - Allocator(runtime::ErrorAlloc), - Interpreter(interpreter::Error), -} - -impl From for Error { - fn from(err: runtime::ErrorAlloc) -> Self { - Error::Allocator(err) - } -} - -impl From for Error { - fn from(err: interpreter::Error) -> Self { - Error::Interpreter(err) - } -} - -pub fn init( - memory: &interpreter::MemoryInstance, - runtime: &mut runtime::Runtime, - input: &[u8], -) -> Result { - let mut input_ptr_slc = [0u8; 4]; - let mut input_length = [0u8; 4]; - - let descriptor_ptr = runtime.alloc(16)?; - - println!("descriptor_ptr: {}", descriptor_ptr); - - if input.len() > 0 { - let input_ptr = runtime.alloc(input.len() as u32)?; - write_u32(&mut input_ptr_slc, input_ptr); - write_u32(&mut input_length, input.len() as u32); - memory.set(input_ptr, input)?; - println!("input_ptr: {}", input_ptr); - } else { - write_u32(&mut input_ptr_slc, 0); - write_u32(&mut input_length, 0); - } - - memory.set(descriptor_ptr, &input_ptr_slc)?; - memory.set(descriptor_ptr+4, &input_length)?; - - // zero result ptr/len - memory.set(descriptor_ptr+8, &[0u8; 4])?; - memory.set(descriptor_ptr+12, &[0u8; 4])?; - - println!("descriptor: {:?}", memory.get(descriptor_ptr, 16)); - - Ok(descriptor_ptr as i32) -} - -fn _read_u32(slc: &[u8]) -> u32 { - use std::ops::Shl; - (slc[0] as u32) + (slc[1] as u32).shl(8) + (slc[2] as u32).shl(16) + (slc[3] as u32).shl(24) -} \ No newline at end of file diff --git a/rust-runner/src/main.rs b/rust-runner/src/main.rs deleted file mode 100644 index dd432f2..0000000 --- a/rust-runner/src/main.rs +++ /dev/null @@ -1,103 +0,0 @@ -/* - - Rust contract demo runner - -*/ - -extern crate parity_wasm; -extern crate wasm_utils; - -mod call_args; -mod runtime; - -use std::env; -use std::sync::Arc; -use parity_wasm::interpreter::{self, ModuleInstanceInterface}; -use parity_wasm::elements; - -pub const DEFAULT_MEMORY_INDEX: interpreter::ItemIndex = interpreter::ItemIndex::Internal(0); -pub type WasmMemoryPtr = i32; - -fn main() { - // First, load wasm contract as a module - wasm_utils::init_log(); - - let args = env::args().collect::>(); - if args.len() != 2 { - println!("Usage: {} contract.wasm", args[0]); - return; - } - - let module = parity_wasm::deserialize_file(&args[1]).expect("Module deserialization to succeed"); - - let program = parity_wasm::interpreter::ProgramInstance::new() - .expect("Program instance to be created"); - - // Add module to the programm - let module_instance = program.add_module("contract", module).expect("Module to be added successfully"); - - { - let env_instance = program.module("env").expect("env module to exist"); - let env_memory = env_instance.memory(interpreter::ItemIndex::Internal(0)) - .expect("liner memory to exist"); - - // Second, create runtime and program instance - let mut runtime = runtime::Runtime::with_params( - env_memory.clone(), // memory shared ptr - 5*1024*1024, // default stack space - 65536, // runner arbitrary gas limit - ); - - // Initialize call descriptor - let descriptor = call_args::init( - &*env_memory, - &mut runtime, - &[3u8; 128], - ).expect("call descriptor initialization to succeed"); - - // create native env module with native add && sub implementations - let functions = interpreter::UserFunctions { - executor: &mut runtime, - functions: vec![ - interpreter::UserFunction { - name: "_storage_read".to_owned(), - params: vec![elements::ValueType::I32, elements::ValueType::I32], - result: Some(elements::ValueType::I32), - }, - interpreter::UserFunction { - name: "_storage_write".to_owned(), - params: vec![elements::ValueType::I32, elements::ValueType::I32], - result: Some(elements::ValueType::I32), - }, - interpreter::UserFunction { - name: "_malloc".to_owned(), - params: vec![elements::ValueType::I32], - result: Some(elements::ValueType::I32), - }, - interpreter::UserFunction { - name: "_debug".to_owned(), - params: vec![elements::ValueType::I32, elements::ValueType::I32], - result: None, - }, - interpreter::UserFunction { - name: "gas".to_owned(), - params: vec![elements::ValueType::I32], - result: None, - }, - interpreter::UserFunction { - name: "_free".to_owned(), - params: vec![elements::ValueType::I32], - result: None, - }, - ], - }; - let native_env_instance = Arc::new(interpreter::env_native_module(env_instance, functions).unwrap()); - - // Form ExecutionParams (payload + env link) - let params = interpreter::ExecutionParams::with_external("env".into(), native_env_instance) - .add_argument(interpreter::RuntimeValue::I32(descriptor)); - - module_instance.execute_export("_call", params) - .expect("_call to execute successfully"); - } -} \ No newline at end of file diff --git a/rust-runner/src/runtime.rs b/rust-runner/src/runtime.rs deleted file mode 100644 index 58ffa79..0000000 --- a/rust-runner/src/runtime.rs +++ /dev/null @@ -1,180 +0,0 @@ -use std::sync::Arc; -use std::collections::HashMap; - -use parity_wasm::interpreter; - -#[derive(Hash, PartialEq, Eq, Debug)] -pub struct StorageKey([u8; 32]); - -#[derive(Debug, Default)] -pub struct StorageValue([u8; 32]); - -struct ErrorStorage; - -impl StorageKey { - // todo: deal with memory views - fn from_mem(vec: Vec) -> Result { - if vec.len() != 32 { return Err(ErrorStorage); } - let mut result = StorageKey([0u8; 32]); - result.0.copy_from_slice(&vec[0..32]); - Ok(result) - } -} - -impl StorageValue { - // todo: deal with memory views - // todo: deal with variable-length values when it comes - fn from_mem(vec: Vec) -> Result { - if vec.len() != 32 { return Err(ErrorStorage); } - let mut result = StorageValue([0u8; 32]); - result.0.copy_from_slice(&vec[0..32]); - Ok(result) - } - - fn as_slice(&self) -> &[u8] { - &self.0 - } -} - -pub struct Runtime { - gas_counter: u64, - gas_limit: u64, - dynamic_top: u32, - storage: HashMap, - memory: Arc, -} - -#[derive(Debug)] -pub struct ErrorAlloc; - -impl Runtime { - pub fn with_params(memory: Arc, stack_space: u32, gas_limit: u64) -> Runtime { - Runtime { - gas_counter: 0, - gas_limit: gas_limit, - dynamic_top: stack_space, - storage: HashMap::new(), - memory: memory, - } - } - - pub fn storage_write(&mut self, context: interpreter::CallerContext) - -> Result, interpreter::Error> - { - let val_ptr = context.value_stack.pop_as::()?; - let key_ptr = context.value_stack.pop_as::()?; - - let key = StorageKey::from_mem(self.memory.get(key_ptr as u32, 32)?) - .map_err(|_| interpreter::Error::Trap("Memory access violation".to_owned()))?; - let val = StorageValue::from_mem(self.memory.get(val_ptr as u32, 32)?) - .map_err(|_| interpreter::Error::Trap("Memory access violation".to_owned()))?; - - println!("write storage {:?} = {:?}", key, val); - - self.storage.insert(key, val); - - Ok(Some(0i32.into())) - } - - pub fn storage_read(&mut self, context: interpreter::CallerContext) - -> Result, interpreter::Error> - { - // arguments passed are in backward order (since it is stack) - let val_ptr = context.value_stack.pop_as::()?; - let key_ptr = context.value_stack.pop_as::()?; - - let key = StorageKey::from_mem(self.memory.get(key_ptr as u32, 32)?) - .map_err(|_| interpreter::Error::Trap("Memory access violation".to_owned()))?; - let empty = StorageValue([0u8; 32]); - let val = self.storage.get(&key).unwrap_or(&empty); - - self.memory.set(val_ptr as u32, val.as_slice())?; - - println!("read storage {:?} (evaluated as {:?})", key, val); - - Ok(Some(0.into())) - } - - pub fn malloc(&mut self, context: interpreter::CallerContext) - -> Result, interpreter::Error> - { - let amount = context.value_stack.pop_as::()? as u32; - let previous_top = self.dynamic_top; - self.dynamic_top = previous_top + amount; - Ok(Some((previous_top as i32).into())) - } - - pub fn alloc(&mut self, amount: u32) -> Result { - let previous_top = self.dynamic_top; - self.dynamic_top = previous_top + amount; - Ok(previous_top.into()) - } - - fn gas(&mut self, context: interpreter::CallerContext) - -> Result, interpreter::Error> - { - let prev = self.gas_counter; - let update = context.value_stack.pop_as::()? as u64; - if prev + update > self.gas_limit { - // exceeds gas - Err(interpreter::Error::Trap(format!("Gas exceeds limits of {}", self.gas_limit))) - } else { - self.gas_counter = prev + update; - Ok(None) - } - } - - fn debug(&mut self, context: interpreter::CallerContext) - -> Result, interpreter::Error> - { - let msg_len = context.value_stack.pop_as::()? as u32; - let msg_ptr = context.value_stack.pop_as::()? as u32; - - let msg = unsafe { String::from_utf8_unchecked(self.memory.get(msg_ptr, msg_len as usize)?) }; - println!("DEBUG: {}", msg); - - Ok(None) - } - - fn user_trap(&mut self, _context: interpreter::CallerContext) - -> Result, interpreter::Error> - { - Err(interpreter::Error::Trap("unknown trap".to_owned())) - } - - fn user_noop(&mut self, - _context: interpreter::CallerContext - ) -> Result, interpreter::Error> { - Ok(None) - } -} - -impl interpreter::UserFunctionExecutor for Runtime { - fn execute(&mut self, name: &str, context: interpreter::CallerContext) - -> Result, interpreter::Error> - { - match name { - "_malloc" => { - self.malloc(context) - }, - "_free" => { - self.user_noop(context) - }, - "_storage_read" => { - self.storage_read(context) - }, - "_storage_write" => { - self.storage_write(context) - }, - "gas" => { - self.gas(context) - }, - "_debug" => { - self.debug(context) - }, - _ => { - self.user_trap(context) - } - } - } -} \ No newline at end of file diff --git a/samples/empty_contract.rs b/samples/empty_contract.rs deleted file mode 100644 index f226f2b..0000000 --- a/samples/empty_contract.rs +++ /dev/null @@ -1,27 +0,0 @@ -#![feature(link_args)] -#![no_main] - -// as it is experimental preamble -#![allow(dead_code)] - -#[link_args = "-s WASM=1 -s NO_EXIT_RUNTIME=1 -s NO_FILESYSTEM=1"] -extern {} - -#[no_mangle] -pub fn call() { -} - -/* This produces the following code (after injecting gas counter & optimizing) -(module - (type (;0;) (func)) - (type (;1;) (func (param i32))) - (import "env" "memory" (memory (;0;) 256 256)) - (import "env" "table" (table (;0;) 0 0 anyfunc)) - (import "env" "gas" (func (;0;) (type 1))) - (func (;1;) (type 0) - i32.const 2 - call 0 - nop) - (export "_call" (func 1)) - (data (i32.const 1212) "\1c\05")) -*/ \ No newline at end of file diff --git a/samples/import_contract.c b/samples/import_contract.c deleted file mode 100644 index c46c08e..0000000 --- a/samples/import_contract.c +++ /dev/null @@ -1,43 +0,0 @@ -int data; - -extern void log_event(void* ptr); - -int main() { - log_event(0); -} - -void call() { - log_event(0); -} - -/* produces the following code (with gas counter and call optimization) -(module - (type (;0;) (func (param i32) (result i32))) - (type (;1;) (func)) - (type (;2;) (func (param i32))) - (import "env" "memoryBase" (global (;0;) i32)) - (import "env" "memory" (memory (;0;) 256)) - (import "env" "table" (table (;0;) 0 anyfunc)) - (import "env" "gas" (func (;0;) (type 2))) - (func (;1;) (type 0) (param i32) (result i32) - i32.const 2 - call 0 - block i32 ;; label = @1 - i32.const 13 - call 0 - get_global 0 - i32.const 5242880 - i32.add - get_global 0 - i32.const 5242880 - i32.add - i32.load - get_local 0 - i32.load offset=4 - i32.add - i32.store - i32.const 0 - end) - (export "_call" (func 1))) - -*/ diff --git a/samples/logger_contract.rs b/samples/logger_contract.rs deleted file mode 100644 index 6e2af87..0000000 --- a/samples/logger_contract.rs +++ /dev/null @@ -1,135 +0,0 @@ -#![feature(link_args)] -#![no_main] - -// as it is experimental preamble -#![allow(dead_code)] - -use std::slice; - -#[link_args = "-s NO_EXIT_RUNTIME=1 -s NO_FILESYSTEM=1 -s"] -extern {} - -/// Wrapper over storage read/write externs -/// Storage api is a key-value storage where both key and value are 32 bytes in len -mod storage { - pub struct Error; - - #[link(name = "env")] - extern { - fn storage_read(key: *const u8, dst: *mut u8) -> i32; - fn storage_write(key: *const u8, src: *const u8) -> i32; - } - - /// Performs read from storage to the specified slice `dst` - /// Can return `Error` if data is read from outside of the storage boundaries - pub fn read(key: &[u8; 32], dst: &mut [u8; 32]) -> Result<(), Error> { - match unsafe { - let mut dst = dst; - storage_read(key.as_ptr(), dst.as_mut_ptr()) - } { - x if x < 0 => Err(Error), - _ => Ok(()), - } - } - - /// Performs write to the storage from the specified `src` - pub fn write(key: &[u8; 32], src: &[u8; 32]) -> Result<(), Error> { - match unsafe { - storage_write(key.as_ptr(), src.as_ptr()) - } { - x if x < 0 => Err(Error), - _ => Ok(()), - } - } -} - -/// Safe wrapper for call context -struct CallArgs { - context: Box<[u8]>, - result: Vec, -} - -unsafe fn read_ptr_mut(slc: &[u8]) -> *mut u8 { - std::ptr::null_mut().offset(read_u32(slc) as isize) -} - -fn read_u32(slc: &[u8]) -> u32 { - use std::ops::Shl; - (slc[0] as u32) + (slc[1] as u32).shl(8) + (slc[2] as u32).shl(16) + (slc[3] as u32).shl(24) -} - -fn write_u32(dst: &mut [u8], val: u32) { - dst[0] = (val & 0x000000ff) as u8; - dst[1] = ((val & 0x0000ff00) >> 8) as u8; - dst[2] = ((val & 0x00ff0000) >> 16) as u8; - dst[3] = ((val & 0xff000000) >> 24) as u8; -} - -fn write_ptr(dst: &mut [u8], ptr: *mut u8) { - // todo: consider: add assert that arch is 32bit - write_u32(dst, ptr as usize as u32); -} - -impl CallArgs { - pub fn from_raw(ptr: *mut u8) -> CallArgs { - let desc_slice = unsafe { slice::from_raw_parts(ptr, 4 * 4) }; - - let context_ptr = unsafe { read_ptr_mut(&desc_slice[0..4]) }; - let context_len = read_u32(&desc_slice[4..8]) as usize; - - let result_ptr = unsafe { read_ptr_mut(&desc_slice[8..12]) }; - let result_len = read_u32(&desc_slice[12..16]) as usize; - - CallArgs { - context: unsafe { Box::<[u8]>::from_raw(slice::from_raw_parts_mut(context_ptr, context_len)) }, - result: unsafe { Vec::from_raw_parts(result_ptr, result_len, result_len) }, - } - } - - pub fn context(&self) -> &[u8] { - &self.context - } - - pub fn result_mut(&mut self) -> &mut Vec { - &mut self.result - } - - pub fn save(self, ptr: *mut u8) { - let dst = unsafe { slice::from_raw_parts_mut(ptr.offset(8), 2 * 4) }; - let context = self.context; - let mut result = self.result; - - // context unmodified and memory is managed in calling code - std::mem::forget(context); - - if result.len() > 0 { - // result - write_ptr(&mut dst[0..4], result.as_mut_ptr()); - write_u32(&mut dst[4..8], result.len() as u32); - // managed in calling code - std::mem::forget(result); - } - } -} - -#[no_mangle] -pub fn call(descriptor: *mut u8) { - // This initializes safe wrapper for contract input and output - let mut ctx = CallArgs::from_raw(descriptor); - - // Copies all contract input data to the separate buffer - let data = ctx.context().to_vec(); - - let storage_key = [1u8; 32]; - let mut storage_val = [0u8; 32]; - storage_val.copy_from_slice(&data[0..32]); - - // Sets the key [1, 1, 1 ..., 1] to the first 32 bytes of passed input - let _ = storage::write(&storage_key, &mut storage_val); - - // Returns all that passed to this contract as an output - *ctx.result_mut() = data; - - // Saves the wrapper state to commit return stream - ctx.save(descriptor); -} diff --git a/samples/static_contract.c b/samples/static_contract.c deleted file mode 100644 index 8735bae..0000000 --- a/samples/static_contract.c +++ /dev/null @@ -1,38 +0,0 @@ -int data; - -int call(void* descriptor) { - int* input_length = (int*)(descriptor+4); - data += *input_length; -} - -/* produces the following code (with gas counter and call optimization) -(module - (type (;0;) (func (param i32) (result i32))) - (type (;1;) (func)) - (type (;2;) (func (param i32))) - (import "env" "memoryBase" (global (;0;) i32)) - (import "env" "memory" (memory (;0;) 256)) - (import "env" "table" (table (;0;) 0 anyfunc)) - (import "env" "gas" (func (;0;) (type 2))) - (func (;1;) (type 0) (param i32) (result i32) - i32.const 2 - call 0 - block i32 ;; label = @1 - i32.const 13 - call 0 - get_global 0 - i32.const 5242880 - i32.add - get_global 0 - i32.const 5242880 - i32.add - i32.load - get_local 0 - i32.load offset=4 - i32.add - i32.store - i32.const 0 - end) - (export "_call" (func 1))) - -*/ diff --git a/samples/static_contract.rs b/samples/static_contract.rs deleted file mode 100644 index 05b6bf7..0000000 --- a/samples/static_contract.rs +++ /dev/null @@ -1,8 +0,0 @@ -extern { - #[link(name="env")] - fn log_event(id: *const u8); -} - -fn main() { - unsafe { log_event(::std::ptr::null()); } -} \ No newline at end of file diff --git a/samples/storage.rs b/samples/storage.rs deleted file mode 100644 index 97402a7..0000000 --- a/samples/storage.rs +++ /dev/null @@ -1,55 +0,0 @@ -#![feature(link_args)] -#![no_main] - -// as it is experimental preamble -#![allow(dead_code)] - -use std::slice; - -#[link_args = "-s NO_EXIT_RUNTIME=1 -s NO_FILESYSTEM=1 -s"] -extern {} - -/// Wrapper over storage read/write externs -/// Storage api is a key-value storage where both key and value are 32 bytes in len -mod storage { - pub struct Error; - - #[link(name = "env")] - extern { - fn storage_read(key: *const u8, dst: *mut u8) -> i32; - fn storage_write(key: *const u8, src: *const u8) -> i32; - } - - /// Performs read from storage to the specified slice `dst`, using all slice length - /// Can return `Error` if data is read from outside of the storage boundaries - pub fn read(key: &[u8; 32], dst: &mut [u8; 32]) -> Result<(), Error> { - match unsafe { - let mut dst = dst; - storage_read(key.as_ptr(), dst.as_mut_ptr()) - } { - x if x < 0 => Err(Error), - _ => Ok(()), - } - } - - /// Performs write to the storage from the specified slice `src` - pub fn write(key: &[u8; 32], src: &[u8; 32]) -> Result<(), Error> { - match unsafe { - storage_write(key.as_ptr(), src.as_ptr()) - } { - x if x < 0 => Err(Error), - _ => Ok(()), - } - } -} - -#[no_mangle] -pub fn call(_descriptor: *mut u8) { - let storage_key = [1u8; 32]; - let mut storage_val = [2u8; 32]; - let storage_dup_key = [3u8; 32]; - - let _ = storage::write(&storage_key, &storage_val); - let _ = storage::read(&storage_dup_key, &mut storage_val); - let _ = storage::write(&storage_key, &storage_val); -}