Test multiple blocks/txs per block.

Better debug tracing.
This commit is contained in:
Gav
2018-01-31 12:33:40 +00:00
parent e9958a8e5c
commit 6fd19a630a
9 changed files with 116 additions and 30 deletions
@@ -84,7 +84,8 @@ impl Slicable for Vec<u8> {
fn set_as_slice<F: Fn(&mut [u8], usize) -> bool>(fill_slice: &F) -> Option<Self> {
u32::set_as_slice(fill_slice).and_then(|len| {
let mut v = Vec::with_capacity(len as usize);
unsafe { v.set_len(len as usize); }
v.resize(len as usize, 0);
// unsafe { v.set_len(len as usize); }
if fill_slice(&mut v, 4) {
Some(v)
} else {
@@ -106,7 +107,7 @@ impl<T: Slicable> NonTrivialSlicable for Vec<T> where Vec<T>: Slicable {}
impl<T: NonTrivialSlicable> Slicable for Vec<T> {
fn from_slice(value: &[u8]) -> Option<Self> {
let len = Self::size_of(&value[0..4])?;
let len = Self::size_of(value)?;
let mut off = 4;
let mut r = Vec::new();
while off < len {
+12 -1
View File
@@ -37,6 +37,7 @@ pub mod runtime;
use runtime_std::prelude::*;
use codec::Slicable;
use runtime_std::print;
use primitives::{Block, UncheckedTransaction};
/// Execute a block, with `input` being the canonical serialisation of the block. Returns the
@@ -53,4 +54,14 @@ pub fn execute_transaction(input: &[u8]) -> Vec<u8> {
Vec::new()
}
impl_stubs!(execute_block, execute_transaction);
/// Run whatever tests we have.
pub fn run_tests(input: &[u8]) -> Vec<u8> {
print("run_tests...");
let block = Block::from_slice(input).unwrap();
print("deserialised block.");
let stxs = block.transactions.iter().map(Slicable::to_vec).collect::<Vec<_>>();
print("reserialised transactions.");
[stxs.len() as u8].to_vec()
}
impl_stubs!(execute_block, execute_transaction, run_tests);
@@ -18,6 +18,7 @@
use runtime_std::prelude::*;
use runtime_std::cell::RefCell;
use runtime_std::print;
use codec::KeyedVec;
use support::{storage, StorageVec};
use primitives::{BlockNumber, AccountID};
@@ -18,7 +18,7 @@
//! and depositing logs.
use runtime_std::prelude::*;
use runtime_std::{mem, print, storage_root, enumerated_trie_root};
use runtime_std::{mem, storage_root, enumerated_trie_root};
use codec::{KeyedVec, Slicable};
use support::{Hashable, storage, with_env};
use primitives::{Block, BlockNumber, Hash, UncheckedTransaction, TxOrder};
@@ -76,12 +76,14 @@ pub mod internal {
// check transaction trie root represents the transactions.
let txs = block.transactions.iter().map(Slicable::to_vec).collect::<Vec<_>>();
let txs_root = enumerated_trie_root(&txs.iter().map(Vec::as_slice).collect::<Vec<_>>());
// println!("TR: {}", ::support::HexDisplay::from(&txs_root));
let txs = txs.iter().map(Vec::as_slice).collect::<Vec<_>>();
let txs_root = enumerated_trie_root(&txs);
assert!(header.transaction_root == txs_root, "Transaction trie root must be valid.");
// execute transactions
block.transactions.iter().for_each(execute_transaction);
for tx in &block.transactions {
execute_transaction(tx);
}
staking::internal::check_new_era();
session::internal::check_rotate_session();
@@ -89,9 +91,10 @@ pub mod internal {
// any final checks
final_checks(&block);
// check storage root.
assert!(header.state_root == storage_root(), "Storage root must match that calculated.");
let storage_root = storage_root();
debug_assert_hash(&header.state_root, &storage_root);
assert!(header.state_root == storage_root, "Storage root must match that calculated.");
// store the header hash in storage; we can't do it before otherwise there would be a
// cyclic dependency.
@@ -117,6 +120,17 @@ pub mod internal {
// decode parameters and dispatch
tx.function.dispatch(&tx.signed, &tx.input_data);
}
#[cfg(feature = "with-std")]
fn debug_assert_hash(given: &Hash, expected: &Hash) {
use support::HexDisplay;
if given != expected {
println!("Hash: given={}, expected={}", HexDisplay::from(given), HexDisplay::from(expected));
}
}
#[cfg(not(feature = "with-std"))]
fn debug_assert_hash(_given: &Hash, _expected: &Hash) {}
}
fn final_checks(_block: &Block) {
+2
View File
@@ -4,6 +4,8 @@
#![feature(alloc)]
#![cfg_attr(feature = "strict", deny(warnings))]
#[macro_use]
#[macro_export]
extern crate alloc;
pub use alloc::vec;