Skip to content

Commit

Permalink
mock call contract (ontio#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
laizy authored Apr 17, 2020
1 parent 9051fdf commit 612440c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
18 changes: 18 additions & 0 deletions ontio-std/src/mock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ impl RuntimeHandle {
self.inner.borrow_mut().witness = addr.into_iter().map(|a| a.as_ref().clone()).collect();
self
}

pub fn on_contract_call(
&self, func: impl Fn(&Address, &[u8]) -> Option<Vec<u8>> + 'static,
) -> &Self {
self.inner.borrow_mut().call_contract = Some(Box::new(func));
self
}
}

pub fn build_runtime() -> RuntimeHandle {
Expand All @@ -78,3 +85,14 @@ pub fn build_runtime() -> RuntimeHandle {
let handle = RuntimeHandle { inner: inner };
handle
}

#[test]
fn test_call_contract() {
assert_eq!(crate::runtime::call_contract(&Address::repeat_byte(1), &[1, 2]), Some(vec![]));

build_runtime().on_contract_call(|_addr, _data| -> Option<Vec<u8>> { Some(vec![1, 2, 3]) });
assert_eq!(
crate::runtime::call_contract(&Address::repeat_byte(1), &[1, 2]),
Some(vec![1, 2, 3])
);
}
47 changes: 47 additions & 0 deletions ontio-std/src/mock/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ pub(crate) struct RuntimeInner {
pub(crate) tx_hash: H256,
pub(crate) witness: Vec<Address>,
pub(crate) notify: Vec<Vec<u8>>,
pub(crate) call_contract: Option<Box<dyn Fn(&Address, &[u8]) -> Option<Vec<u8>>>>,
pub(crate) call_output: Vec<u8>,
}

impl RuntimeInner {
fn call_contract(&mut self, addr: &Address, data: &[u8]) {
if let Some(call) = &self.call_contract {
self.call_output = (call)(addr, data).unwrap_or_default();
}
}
}

impl Runtime {
Expand Down Expand Up @@ -73,11 +83,24 @@ impl Runtime {
fn notify(&self, msg: &[u8]) {
self.inner.borrow_mut().notify.push(msg.to_vec());
}

fn sha256(&self, data: &[u8]) -> H256 {
let mut hasher = sha2::Sha256::new();
hasher.input(data);
H256::from_slice(hasher.result().as_slice())
}

fn call_contract(&self, addr: &Address, data: &[u8]) {
self.inner.borrow_mut().call_contract(addr, data);
}

fn get_call_output(&self) -> Vec<u8> {
self.inner.borrow().call_output.clone()
}

fn call_output_length(&self) -> u32 {
self.inner.borrow().call_output.len() as u32
}
}

thread_local!(static RUNTIME: RefCell<Runtime> = RefCell::new(Runtime::default()));
Expand Down Expand Up @@ -198,4 +221,28 @@ mod env {
ptr::copy(hash.as_ptr(), h256, 32);
});
}

#[no_mangle]
pub unsafe extern "C" fn ontio_call_contract(
addr: *const u8, input_ptr: *const u8, input_len: u32,
) {
let addr = Address::from_slice(slice::from_raw_parts(addr, 20));
let input = slice::from_raw_parts(input_ptr, input_len as usize);
RUNTIME.with(|r| {
r.borrow().call_contract(&addr, input);
});
}

#[no_mangle]
pub unsafe extern "C" fn ontio_call_output_length() -> u32 {
RUNTIME.with(|r| r.borrow().call_output_length())
}

#[no_mangle]
pub unsafe extern "C" fn ontio_get_call_output(dest: *mut u8) {
RUNTIME.with(|r| {
let res = r.borrow().get_call_output();
ptr::copy(res.as_ptr(), dest, res.len());
})
}
}
2 changes: 1 addition & 1 deletion ontio-std/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fn test_to_neo_bytes() {
(90123123981293054321, "71e975a9c4a7b5e204"),
];

for (data, exp) in case_data.into_iter() {
for (data, exp) in case_data.iter() {
let res = i128_to_neo_bytes(*data);
let r = to_hex_string(res.as_slice());
assert_eq!(r, exp.to_string());
Expand Down

0 comments on commit 612440c

Please sign in to comment.