Tests for native/wasm runtime

This commit is contained in:
Gav
2018-01-25 19:28:28 +01:00
parent 53eea2bed6
commit ead42293bc
15 changed files with 105 additions and 32 deletions
+13 -4
View File
@@ -22,22 +22,28 @@
#[macro_use]
extern crate runtime_support;
#[cfg(test)]
#[cfg(feature = "with-std")]
extern crate rustc_hex;
mod codec;
#[macro_use]
mod support;
mod runtime;
pub mod runtime;
pub use codec::{endiansensitive, streamreader, joiner, slicable, keyedvec};
pub use support::{primitives, function, proposal, environment, storable};
#[cfg(test)]
#[cfg(feature = "with-std")]
pub use support::{testing, statichex};
use runtime_support::prelude::*;
use slicable::Slicable;
use primitives::{Block, UncheckedTransaction};
/// A simple test.
pub fn simple_test(input: &[u8]) -> Vec<u8> {
println!("Executing block");
Vec::new()
}
/// Execute a block, with `input` being the canonical serialisation of the block. Returns the
/// empty vector.
pub fn execute_block(input: &[u8]) -> Vec<u8> {
@@ -47,7 +53,10 @@ pub fn execute_block(input: &[u8]) -> Vec<u8> {
/// Execute a given, serialised, transaction. Returns the empty vector.
pub fn execute_transaction(input: &[u8]) -> Vec<u8> {
runtime::system::execute_transaction(&UncheckedTransaction::from_slice(input).unwrap());
println!("Deserialising... {:?}", input);
let utx = UncheckedTransaction::from_slice(input).unwrap();
println!("Forwarding... {:?}", utx);
runtime::system::execute_transaction(&utx);
Vec::new()
}
@@ -84,6 +84,7 @@ pub fn execute_block(mut block: Block) {
/// Execute a given transaction.
pub fn execute_transaction(utx: &UncheckedTransaction) {
println!("Executing...");
// Verify the signature is good.
assert!(utx.ed25519_verify(), "All transactions should be properly signed");
@@ -97,6 +98,8 @@ pub fn execute_transaction(utx: &UncheckedTransaction) {
// increment nonce in storage
(expected_nonce + 1).store(&nonce_key);
println!("Dispatching...");
// decode parameters and dispatch
tx.function.dispatch(&tx.signed, &tx.input_data);
}
@@ -21,8 +21,8 @@ use streamreader::StreamReader;
use runtime::{staking, session, timestamp, governance};
/// Public functions that can be dispatched to.
#[cfg_attr(test, derive(PartialEq, Debug))]
#[derive(Clone, Copy)]
#[cfg_attr(feature = "with-std", derive(PartialEq, Debug))]
pub enum Function {
StakingStake,
StakingUnstake,
@@ -22,8 +22,8 @@ pub mod proposal;
pub mod environment;
pub mod storable;
#[cfg(test)]
#[cfg(feature = "with-std")]
pub mod statichex;
#[cfg(test)]
#[macro_use]
#[cfg(feature = "with-std")]
pub mod testing;
@@ -23,7 +23,7 @@ use slicable::{Slicable, NonTrivialSlicable};
use function::Function;
use runtime_support::{mem, blake2_256, twox_128, twox_256, ed25519_verify};
#[cfg(test)]
#[cfg(feature = "with-std")]
use std::fmt;
/// The Ed25519 pubkey that identifies an account.
@@ -51,7 +51,7 @@ pub type TxOrder = u64;
pub type Hash = [u8; 32];
#[derive(Clone, Default)]
#[cfg_attr(test, derive(PartialEq, Debug))]
#[cfg_attr(feature = "with-std", derive(PartialEq, Debug))]
/// The digest of a block, useful for light-clients.
pub struct Digest {
/// All logs that have happened in the block.
@@ -59,7 +59,7 @@ pub struct Digest {
}
#[derive(Clone)]
#[cfg_attr(test, derive(PartialEq, Debug))]
#[cfg_attr(feature = "with-std", derive(PartialEq, Debug))]
/// The header for a block.
pub struct Header {
/// The parent block's "hash" (actually the Blake2-256 hash of its serialised header).
@@ -108,8 +108,8 @@ impl Slicable for Header {
impl NonTrivialSlicable for Header {}
#[cfg_attr(test, derive(PartialEq, Debug))]
/// A vetted and verified transaction from the external world.
#[cfg_attr(feature = "with-std", derive(PartialEq, Debug))]
pub struct Transaction {
/// Who signed it (note this is not a signature).
pub signed: AccountID,
@@ -187,14 +187,14 @@ impl UncheckedTransaction {
}
}
#[cfg(test)]
#[cfg(feature = "with-std")]
impl PartialEq for UncheckedTransaction {
fn eq(&self, other: &Self) -> bool {
self.signature.iter().eq(other.signature.iter()) && self.transaction == other.transaction
}
}
#[cfg(test)]
#[cfg(feature = "with-std")]
impl fmt::Debug for UncheckedTransaction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "UncheckedTransaction({:?})", self.transaction)
@@ -229,8 +229,8 @@ impl Slicable for UncheckedTransaction {
impl NonTrivialSlicable for UncheckedTransaction {}
#[cfg_attr(test, derive(PartialEq, Debug))]
/// A Polkadot relay chain block.
#[cfg_attr(feature = "with-std", derive(PartialEq, Debug))]
pub struct Block {
/// The header of the block.
pub header: Header,
@@ -25,8 +25,8 @@ use streamreader::StreamReader;
use runtime::{system, governance, staking, session};
/// Internal functions that can be dispatched to.
#[cfg_attr(test, derive(PartialEq, Debug))]
#[derive(Clone, Copy)]
#[cfg_attr(feature = "with-std", derive(PartialEq, Debug))]
pub enum InternalFunction {
SystemSetCode,
StakingSetSessionsPerEra,
@@ -52,7 +52,7 @@ impl InternalFunction {
}
/// An internal function.
#[cfg_attr(test, derive(PartialEq, Debug))]
#[cfg_attr(feature = "with-std", derive(PartialEq, Debug))]
pub struct Proposal {
/// The priviledged function to call.
pub function: InternalFunction,
@@ -8,6 +8,7 @@ extern "C" {
fn ext_memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8;
fn ext_memmove(dest: *mut u8, src: *const u8, n: usize) -> *mut u8;
fn ext_memset(dest: *mut u8, c: i32, n: usize) -> *mut u8;
fn ext_memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32;
fn ext_malloc(size: usize) -> *mut u8;
fn ext_free(ptr: *mut u8);
}
@@ -21,6 +22,12 @@ pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut
ext_memcpy(dest, src, n)
}
/// memcpy extern
#[no_mangle]
pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
ext_memcmp(s1, s2, n)
}
/// memmove extern
#[no_mangle]
pub unsafe extern "C" fn memmove(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 {