Skip to content
Snippets Groups Projects
Commit 337027b1 authored by Gav's avatar Gav
Browse files

Block-creation using new infrastructure

parent 8628a67e
No related merge requests found
......@@ -59,7 +59,7 @@ mod tests {
}
}
fn construct_block(number: BlockNumber, parent_hash: Hash, state_root: Hash, txs: Vec<Transaction>) -> (Vec<u8>, Hash) {
fn construct_block(backend: &InMemory, number: BlockNumber, parent_hash: Hash, state_root: Hash, txs: Vec<Transaction>) -> (Vec<u8>, Hash) {
use triehash::ordered_trie_root;
let transactions = txs.into_iter().map(|transaction| {
......@@ -80,11 +80,32 @@ mod tests {
};
let hash = header.blake2_256();
(Block { header, transactions }.to_vec(), hash)
let mut overlay = OverlayedChanges::default();
for tx in transactions.iter() {
let _ = execute(
backend,
&mut overlay,
&executor(),
"execute_transaction",
&CallData(vec![].join(&number).join(tx))
).unwrap();
}
let header = Header::from_slice(&execute(
backend,
&mut overlay,
&executor(),
"finalise_block",
&CallData(vec![].join(&header))
).unwrap()).unwrap();
(vec![].join(&Block { header, transactions }), hash)
}
fn block1(genesis_hash: Hash) -> (Vec<u8>, Hash) {
fn block1(genesis_hash: Hash, backend: &InMemory) -> (Vec<u8>, Hash) {
construct_block(
backend,
1,
genesis_hash,
hex!("25e5b37074063ab75c889326246640729b40d0c86932edc527bc80db0e04fe5c"),
......@@ -108,7 +129,7 @@ mod tests {
let mut overlay = OverlayedChanges::default();
let backend = InMemory::from(storage);
let (b1data, _b1hash) = block1(genesis_hash);
let (b1data, _b1hash) = block1(genesis_hash, &backend);
let _ = execute(
&backend,
......
......@@ -29,6 +29,7 @@ impl CodeExecutor for NativeExecutor {
runtime_std::with_externalities(ext, || match method {
"execute_block" => safe_call(|| runtime::execute_block(&data.0)),
"execute_transaction" => safe_call(|| runtime::execute_transaction(&data.0)),
"finalise_block" => safe_call(|| runtime::finalise_block(&data.0)),
_ => Err(ErrorKind::MethodNotFound(method.to_owned()).into()),
})
} else {
......
......@@ -36,7 +36,7 @@ pub mod primitives;
pub mod runtime;
use runtime_std::prelude::*;
use codec::Slicable;
use codec::{Slicable, Joiner};
use runtime_std::print;
use primitives::{Block, Header, BlockNumber, UncheckedTransaction};
......@@ -55,6 +55,13 @@ pub fn execute_transaction(input: &[u8]) -> Vec<u8> {
Vec::new()
}
/// Execute a given, serialised, transaction. Returns the empty vector.
pub fn finalise_block(input: &[u8]) -> Vec<u8> {
let header = Header::from_slice(input).unwrap();
let header = runtime::system::internal::finalise_block(header);
Vec::new().join(&header)
}
/// Run whatever tests we have.
pub fn run_tests(input: &[u8]) -> Vec<u8> {
print("run_tests...");
......@@ -65,4 +72,4 @@ pub fn run_tests(input: &[u8]) -> Vec<u8> {
[stxs.len() as u8].to_vec()
}
impl_stubs!(execute_block, execute_transaction, run_tests);
impl_stubs!(execute_block, execute_transaction, finalise_block, run_tests);
No preview for this file type
No preview for this file type
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment