Skip to content

Commit

Permalink
Merge pull request #6 from bandprotocol/add-oei-time
Browse files Browse the repository at this point in the history
Add oei time
  • Loading branch information
songwongtp authored Aug 16, 2021
2 parents a6859c2 + b65dc07 commit 0d55409
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 379 deletions.
354 changes: 22 additions & 332 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2018"
crate-type = ["cdylib"]

[dependencies]
owasm = { git = "https://github.com/bandprotocol/owasm", rev = "1893224c9fab600bd748a23ccade7b1837ce7069" }
owasm-vm = "0.1.12"
failure = "0.1.6"

[profile.release]
Expand Down
48 changes: 22 additions & 26 deletions api/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
#include <stdint.h>
#include <stdlib.h>

enum Error
{
enum Error {
Error_NoError = 0,
Error_SpanTooSmallError = 1,
Error_ValidationError = 2,
Expand Down Expand Up @@ -46,49 +45,42 @@ typedef int32_t Error;
* One side allocates space and creates a `span` for the counterpart to read or write
* without needing to worry about memory management.
*/
typedef struct Span
{
typedef struct Span {
uint8_t *ptr;
uintptr_t len;
uintptr_t cap;
} Span;

typedef struct env_t
{
typedef struct env_t {
uint8_t _private[0];
} env_t;

typedef struct EnvDispatcher
{
Error (*get_calldata)(env_t *, Span *calldata);
Error (*set_return_data)(env_t *, Span data);
int64_t (*get_ask_count)(env_t *);
int64_t (*get_min_count)(env_t *);
Error (*get_ans_count)(env_t *, int64_t *);
Error (*ask_external_data)(env_t *, int64_t eid, int64_t did, Span data);
Error (*get_external_data_status)(env_t *, int64_t eid, int64_t vid, int64_t *status);
Error (*get_external_data)(env_t *, int64_t eid, int64_t vid, Span *data);
typedef struct EnvDispatcher {
Error (*get_calldata)(env_t*, Span *calldata);
Error (*set_return_data)(env_t*, Span data);
int64_t (*get_ask_count)(env_t*);
int64_t (*get_min_count)(env_t*);
int64_t (*get_prepare_time)(env_t*);
Error (*get_execute_time)(env_t*, int64_t*);
Error (*get_ans_count)(env_t*, int64_t*);
Error (*ask_external_data)(env_t*, int64_t eid, int64_t did, Span data);
Error (*get_external_data_status)(env_t*, int64_t eid, int64_t vid, int64_t *status);
Error (*get_external_data)(env_t*, int64_t eid, int64_t vid, Span *data);
} EnvDispatcher;

typedef struct Env
{
typedef struct Env {
env_t *env;
EnvDispatcher dis;
} Env;

typedef struct RunOutput
{
typedef struct RunOutput {
uint32_t gas_used;
} RunOutput;

// cache
typedef struct cache_t
{
} cache_t;

cache_t *init_cache(uint32_t cache_size);
typedef struct cache_t {

void release_cache(cache_t *cache);
} cache_t;

Error do_compile(Span input, Span *output);

Expand All @@ -99,3 +91,7 @@ Error do_run(cache_t *cache,
bool is_prepare,
Env env,
RunOutput *output);

cache_t *init_cache(uint32_t size);

void release_cache(cache_t *cache);
4 changes: 4 additions & 0 deletions api/cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ package api
// int64_t cGetAskCount_cgo(env_t *e) { return cGetAskCount(e); }
// int64_t cGetMinCount(env_t *e);
// int64_t cGetMinCount_cgo(env_t *e) { return cGetMinCount(e); }
// int64_t cGetPrepareTime(env_t *e);
// int64_t cGetPrepareTime_cgo(env_t *e) { return cGetPrepareTime(e); }
// Error cGetExecuteTime(env_t *e, int64_t *val);
// Error cGetExecuteTime_cgo(env_t *e, int64_t *val) { return cGetExecuteTime(e, val); }
// Error cGetAnsCount(env_t *e, int64_t *val);
// Error cGetAnsCount_cgo(env_t *e, int64_t *val) { return cGetAnsCount(e, val); }
// Error cAskExternalData(env_t *e, int64_t eid, int64_t did, Span data);
Expand Down
8 changes: 8 additions & 0 deletions api/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ func (env *MockEnv) GetMinCount() int64 {
return 0
}

func (env *MockEnv) GetPrepareTime() int64 {
return 0
}

func (env *MockEnv) GetExecuteTime() (int64, error) {
return 0, nil
}

func (env *MockEnv) GetAnsCount() (int64, error) {
return 0, nil
}
17 changes: 17 additions & 0 deletions api/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type EnvInterface interface {
SetReturnData([]byte) error
GetAskCount() int64
GetMinCount() int64
GetPrepareTime() int64
GetExecuteTime() (int64, error)
GetAnsCount() (int64, error)
AskExternalData(eid int64, did int64, data []byte) error
GetExternalDataStatus(eid int64, vid int64) (int64, error)
Expand Down Expand Up @@ -50,6 +52,21 @@ func cGetMinCount(e *C.env_t) C.int64_t {
return C.int64_t((*(*envIntl)(unsafe.Pointer(e))).ext.GetMinCount())
}

//export cGetPrepareTime
func cGetPrepareTime(e *C.env_t) C.int64_t {
return C.int64_t((*(*envIntl)(unsafe.Pointer(e))).ext.GetPrepareTime())
}

//export cGetExecuteTime
func cGetExecuteTime(e *C.env_t, val *C.int64_t) C.Error {
v, err := (*(*envIntl)(unsafe.Pointer(e))).ext.GetExecuteTime()
if err != nil {
return toCError(err)
}
*val = C.int64_t(v)
return C.Error_NoError
}

//export cGetAnsCount
func cGetAnsCount(e *C.env_t, val *C.int64_t) C.Error {
v, err := (*(*envIntl)(unsafe.Pointer(e))).ext.GetAnsCount()
Expand Down
6 changes: 6 additions & 0 deletions api/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ package api
// int64_t cGetAskCount_cgo(env_t *e);
// typedef int64_t (*get_min_count_fn)(env_t*);
// int64_t cGetMinCount_cgo(env_t *e);
// typedef int64_t (*get_prepare_time_fn)(env_t*);
// int64_t cGetPrepareTime_cgo(env_t *e);
// typedef int64_t (*get_execute_time_fn)(env_t*);
// int64_t cGetExecuteTime_cgo(env_t *e);
// typedef int64_t (*get_ans_count_fn)(env_t*);
// int64_t cGetAnsCount_cgo(env_t *e);
// typedef void (*ask_external_data_fn)(env_t*, int64_t eid, int64_t did);
Expand Down Expand Up @@ -73,6 +77,8 @@ func (vm Vm) run(code []byte, gasLimit uint32, spanSize int64, isPrepare bool, e
set_return_data: C.set_return_data_fn(C.cSetReturnData_cgo),
get_ask_count: C.get_ask_count_fn(C.cGetAskCount_cgo),
get_min_count: C.get_min_count_fn(C.cGetMinCount_cgo),
get_prepare_time: C.get_prepare_time_fn(C.cGetPrepareTime_cgo),
get_execute_time: C.get_execute_time_fn(C.cGetExecuteTime_cgo),
get_ans_count: C.get_ans_count_fn(C.cGetAnsCount_cgo),
ask_external_data: C.ask_external_data_fn(C.cAskExternalData_cgo),
get_external_data_status: C.get_external_data_status_fn(C.cGetExternalDataStatus_cgo),
Expand Down
Binary file modified api/libgo_owasm.dylib
Binary file not shown.
Binary file modified api/libgo_owasm.so
Binary file not shown.
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ func (e *Env) GetMinCount() int64 {
return 20000
}

func (e *Env) GetPrepareTime() int64 {
return 1623403856
}

func (e *Env) GetExecuteTime() int64 {
return 1623403868
}

func (e *Env) GetAnsCount() int64 {
return 30000
}
Expand Down
4 changes: 3 additions & 1 deletion src/env.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::span::Span;
use owasm::core::error::Error;
use owasm_vm::error::Error;

#[repr(C)]
pub struct env_t {
Expand All @@ -13,6 +13,8 @@ pub struct EnvDispatcher {
pub set_return_data: extern "C" fn(*mut env_t, data: Span) -> Error,
pub get_ask_count: extern "C" fn(*mut env_t) -> i64,
pub get_min_count: extern "C" fn(*mut env_t) -> i64,
pub get_prepare_time: extern "C" fn(*mut env_t) -> i64,
pub get_execute_time: extern "C" fn(*mut env_t, &mut i64) -> Error,
pub get_ans_count: extern "C" fn(*mut env_t, &mut i64) -> Error,
pub ask_external_data: extern "C" fn(*mut env_t, eid: i64, did: i64, data: Span) -> Error,
pub get_external_data_status: extern "C" fn(*mut env_t, eid: i64, vid: i64, status: &mut i64) -> Error,
Expand Down
24 changes: 13 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ mod span;
mod vm;

use env::{Env, RunOutput};
use owasm::core;

use span::Span;

use failure::{bail, Error};
use std::panic::{catch_unwind};
use std::panic::catch_unwind;

use owasm::core::cache::{Cache, CacheOptions};
use owasm_vm;
use owasm_vm::cache::{Cache, CacheOptions};
use owasm_vm::error::Error as OwasmError;

// Cache initializing section
#[repr(C)]
Expand All @@ -20,12 +22,12 @@ pub extern "C" fn init_cache(size: u32) -> *mut cache_t {
let r = catch_unwind(|| do_init_cache(size)).unwrap_or_else(|_| bail!("Caught panic"));
match r {
Ok(t) => t as *mut cache_t,
Err(_) => std::ptr::null_mut()
Err(_) => std::ptr::null_mut(),
}
}

fn do_init_cache(size: u32) -> Result<*mut Cache, Error> {
let cache = Cache::new( CacheOptions { cache_size: size });
let cache = Cache::new(CacheOptions { cache_size: size });
let out = Box::new(cache);
let res = Ok(Box::into_raw(out));
res
Expand All @@ -41,11 +43,11 @@ pub unsafe extern "C" fn release_cache(cache: *mut cache_t) {

// Compile and execute section
#[no_mangle]
pub extern "C" fn do_compile(input: Span, output: &mut Span) -> owasm::core::error::Error {
match core::compile(input.read()) {
pub extern "C" fn do_compile(input: Span, output: &mut Span) -> OwasmError {
match owasm_vm::compile(input.read()) {
Ok(out) => {
output.write(&out);
owasm::core::error::Error::NoError
OwasmError::NoError
}
Err(e) => e,
}
Expand All @@ -60,12 +62,12 @@ pub extern "C" fn do_run(
is_prepare: bool,
env: Env,
output: &mut RunOutput,
) -> owasm::core::error::Error {
) -> OwasmError {
let vm_env = vm::VMEnv::new(env, span_size);
match core::run(cache, code.read(), gas_limit, is_prepare, vm_env) {
match owasm_vm::run(cache, code.read(), gas_limit, is_prepare, vm_env) {
Ok(gas_used) => {
output.gas_used = gas_used;
owasm::core::error::Error::NoError
OwasmError::NoError
}
Err(e) => e,
}
Expand Down
10 changes: 7 additions & 3 deletions src/span.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use owasm::core::error::Error;
use owasm_vm::error::Error;

#[derive(Copy, Clone)]
#[repr(C)]
Expand Down Expand Up @@ -28,10 +28,14 @@ impl Span {

/// Creates a writable `span` with the provided `ptr` as the starting memory location,
/// and initial capacity `cap`. The created span has zero length.
pub fn create_writable(ptr: *mut u8, cap: usize) -> Span { Span { ptr, len: 0, cap } }
pub fn create_writable(ptr: *mut u8, cap: usize) -> Span {
Span { ptr, len: 0, cap }
}

/// Returns a read-only view of the `span`.
pub fn read(&self) -> &[u8] { unsafe { std::slice::from_raw_parts(self.ptr, self.len) } }
pub fn read(&self) -> &[u8] {
unsafe { std::slice::from_raw_parts(self.ptr, self.len) }
}

/// Appends data into the `span`. Returns NoError if the write is successful.
/// The function may fail if the given data exceeeds the capacity of the `span`.
Expand Down
28 changes: 23 additions & 5 deletions src/vm.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::env::Env;
use crate::span::Span;

use owasm::core::error::Error;
use owasm::core::vm;
use owasm_vm::error::Error;
use owasm_vm::vm;

pub struct VMEnv {
env: Env, // The execution environment for callbacks to Golang.
Expand All @@ -19,7 +19,9 @@ impl VMEnv {
}

impl vm::Env for VMEnv {
fn get_span_size(&self) -> i64 { self.span_size }
fn get_span_size(&self) -> i64 {
self.span_size
}

fn get_calldata(&self) -> Result<Vec<u8>, Error> {
let mut mem: Vec<u8> = Vec::with_capacity(self.span_size as usize);
Expand All @@ -42,9 +44,25 @@ impl vm::Env for VMEnv {
}
}

fn get_ask_count(&self) -> i64 { (self.env.dis.get_ask_count)(self.env.env) }
fn get_ask_count(&self) -> i64 {
(self.env.dis.get_ask_count)(self.env.env)
}

fn get_min_count(&self) -> i64 { (self.env.dis.get_min_count)(self.env.env) }
fn get_min_count(&self) -> i64 {
(self.env.dis.get_min_count)(self.env.env)
}

fn get_prepare_time(&self) -> i64 {
(self.env.dis.get_prepare_time)(self.env.env)
}

fn get_execute_time(&self) -> Result<i64, Error> {
let mut execute_time = 0;
match (self.env.dis.get_execute_time)(self.env.env, &mut execute_time) {
Error::NoError => Ok(execute_time),
err => Err(err),
}
}

fn get_ans_count(&self) -> Result<i64, Error> {
let mut ans_count = 0;
Expand Down

0 comments on commit 0d55409

Please sign in to comment.