mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 14:37:57 +00:00
Tests for native/wasm runtime
This commit is contained in:
@@ -34,8 +34,10 @@ extern crate serde;
|
||||
extern crate parity_wasm;
|
||||
extern crate byteorder;
|
||||
extern crate rustc_hex;
|
||||
#[macro_use]
|
||||
extern crate native_runtime;
|
||||
extern crate runtime_support;
|
||||
extern crate memcmp;
|
||||
|
||||
#[macro_use]
|
||||
extern crate error_chain;
|
||||
|
||||
@@ -33,3 +33,53 @@ impl CodeExecutor for NativeExecutor {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use primitives::twox_128;
|
||||
use native_runtime::testing::{TestExternalities, one, two};
|
||||
use native_runtime::statichex::StaticHexInto;
|
||||
use native_runtime::keyedvec::KeyedVec;
|
||||
use native_runtime::runtime::staking::balance;
|
||||
|
||||
fn tx() -> Vec<u8> { "679fcf0a846b4224c84ecad7d91a26241c46d00cb53d6480a363274e8965ee34b0b80b4b2e3836d3d8f8f12c0c1aef7350af587d9aee3883561d11726068ac0a2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee00000000000000000228000000d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a4500000000000000".convert() }
|
||||
|
||||
#[test]
|
||||
fn execution_with_native_equivalent_code_runs_native_ok() {
|
||||
let one = one();
|
||||
let two = two();
|
||||
|
||||
let mut t = TestExternalities { storage: map![
|
||||
twox_128(&one.to_keyed_vec(b"sta:bal:")).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0]
|
||||
], };
|
||||
|
||||
let native_equivalent_code = include_bytes!("../../wasm-runtime/target/wasm32-unknown-unknown/release/runtime_polkadot.compact.wasm");
|
||||
NativeExecutor.call(&mut t, &native_equivalent_code[..], "execute_transaction", &CallData(tx()));
|
||||
|
||||
runtime_support::with_externalities(&mut t, || {
|
||||
assert_eq!(balance(&one), 42);
|
||||
assert_eq!(balance(&two), 69);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn execution_with_foreign_code_runs_wasm_ok() {
|
||||
let one = one();
|
||||
let two = two();
|
||||
|
||||
let mut t = TestExternalities { storage: map![
|
||||
twox_128(&one.to_keyed_vec(b"sta:bal:")).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0]
|
||||
], };
|
||||
|
||||
let mut foreign_code = include_bytes!("../../wasm-runtime/target/wasm32-unknown-unknown/release/runtime_polkadot.wasm");
|
||||
NativeExecutor.call(&mut t, &foreign_code[..], "execute_transaction", &CallData(tx()));
|
||||
|
||||
runtime_support::with_externalities(&mut t, || {
|
||||
assert_eq!(balance(&one), 42);
|
||||
assert_eq!(balance(&two), 69);
|
||||
});
|
||||
}
|
||||
|
||||
// TODO: test panics.
|
||||
}
|
||||
|
||||
@@ -87,6 +87,15 @@ impl_function_executor!(this: FunctionExecutor<'e, E>,
|
||||
ext_print_num(number: u64) => {
|
||||
println!("Runtime: {}", number);
|
||||
},
|
||||
ext_memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 => {
|
||||
if let (Ok(sl1), Ok(sl2))
|
||||
= (this.memory.get(s1, n as usize), this.memory.get(s2, n as usize)) {
|
||||
use memcmp::Memcmp;
|
||||
(&sl1).memcmp(&sl2) as i32
|
||||
} else {
|
||||
0
|
||||
}
|
||||
},
|
||||
ext_memcpy(dest: *mut u8, src: *const u8, count: usize) -> *mut u8 => {
|
||||
let _ = this.memory.copy_nonoverlapping(src as usize, dest as usize, count as usize);
|
||||
println!("memcpy {} from {}, {} bytes", dest, src, count);
|
||||
@@ -242,22 +251,7 @@ mod tests {
|
||||
use super::*;
|
||||
use rustc_hex::FromHex;
|
||||
use state_machine::ExternalitiesError;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct TestExternalities {
|
||||
storage: HashMap<Vec<u8>, Vec<u8>>,
|
||||
}
|
||||
impl Externalities for TestExternalities {
|
||||
fn storage(&self, key: &[u8]) -> ::std::result::Result<&[u8], ExternalitiesError> {
|
||||
Ok(self.storage.get(&key.to_vec()).map_or(&[] as &[u8], Vec::as_slice))
|
||||
}
|
||||
|
||||
fn set_storage(&mut self, key: Vec<u8>, value: Vec<u8>) {
|
||||
self.storage.insert(key, value);
|
||||
}
|
||||
|
||||
fn chain_id(&self) -> u64 { 42 }
|
||||
}
|
||||
use native_runtime::testing::{TestExternalities, one, two};
|
||||
|
||||
#[test]
|
||||
fn storage_should_work() {
|
||||
|
||||
Reference in New Issue
Block a user