Make everything compile and fix tests

This commit is contained in:
Bastian Köcher
2020-01-14 22:22:21 +01:00
parent f9a7199b65
commit 1bcb147669
26 changed files with 5214 additions and 3536 deletions
+4 -5
View File
@@ -16,15 +16,14 @@
#![cfg_attr(not(feature = "std"), no_std)]
use codec::{Decode, Encode};
use sp_runtime::traits::Block as BlockT;
///! The Cumulus runtime to make a runtime a parachain.
use rstd::vec::Vec;
use codec::{Encode, Decode};
use runtime_primitives::traits::Block as BlockT;
use sp_std::vec::Vec;
#[cfg(not(feature = "std"))]
#[doc(hidden)]
pub use rstd::slice;
pub use sp_std::slice;
#[macro_use]
pub mod validate_block;
+134 -114
View File
@@ -17,23 +17,38 @@
//! The actual implementation of the validate block functionality.
use crate::WitnessData;
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT};
use executive::ExecuteBlock;
use primitives::{Blake2Hasher, H256};
use frame_executive::ExecuteBlock;
use sp_runtime::traits::{Block as BlockT, HasherFor, Header as HeaderT};
use substrate_trie::{MemoryDB, read_trie_value, delta_trie_root, Layout};
use sp_trie::{delta_trie_root, read_trie_value, Layout, MemoryDB};
use rstd::{slice, ptr, cmp, vec::Vec, boxed::Box, mem};
use sp_std::{boxed::Box, vec::Vec};
use hash_db::{HashDB, EMPTY_PREFIX};
use trie_db::{Trie, TrieDB};
use parachain::{ValidationParams, ValidationResult};
use codec::{Decode, Encode};
/// Stores the global [`Storage`] instance.
///
/// As wasm is always executed with one thread, this global varibale is safe!
static mut STORAGE: Option<Box<dyn Storage>> = None;
/// The message to use as expect message while accessing the `STORAGE`.
const STORAGE_SET_EXPECT: &str =
"`STORAGE` needs to be set before calling this function.";
const STORAGE_ROOT_LEN: usize = 32;
/// Returns a mutable reference to the [`Storage`] implementation.
///
/// # Panic
///
/// Panics if the [`STORAGE`] is not initialized.
fn storage() -> &'static mut dyn Storage {
unsafe {
&mut **STORAGE
.as_mut()
.expect("`STORAGE` needs to be set before calling this function.")
}
}
/// Abstract the storage into a trait without `Block` generic.
trait Storage {
@@ -47,16 +62,17 @@ trait Storage {
fn remove(&mut self, key: &[u8]);
/// Calculate the storage root.
fn storage_root(&mut self) -> [u8; STORAGE_ROOT_LEN];
///
/// Returns the SCALE encoded hash.
fn storage_root(&mut self) -> Vec<u8>;
/// Clear all keys that start with the given prefix.
fn clear_prefix(&mut self, prefix: &[u8]);
}
/// Validate a given parachain block on a validator.
#[doc(hidden)]
pub fn validate_block<B: BlockT<Hash = H256>, E: ExecuteBlock<B>>(
params: ValidationParams,
) -> ValidationResult {
use codec::{Decode, Encode};
pub fn validate_block<B: BlockT, E: ExecuteBlock<B>>(params: ValidationParams) -> ValidationResult {
let block_data = crate::ParachainBlockData::<B>::decode(&mut &params.block_data[..])
.expect("Invalid parachain block data");
@@ -66,23 +82,28 @@ pub fn validate_block<B: BlockT<Hash = H256>, E: ExecuteBlock<B>>(
// TODO: Add `PolkadotInherent`.
let block = B::new(block_data.header, block_data.extrinsics);
assert!(parent_head.hash() == *block.header().parent_hash(), "Invalid parent hash");
assert!(
parent_head.hash() == *block.header().parent_hash(),
"Invalid parent hash"
);
let storage = WitnessStorage::<B>::new(
block_data.witness_data,
block_data.witness_data_storage_root,
).expect("Witness data and storage root always match; qed");
)
.expect("Witness data and storage root always match; qed");
let _guard = unsafe {
STORAGE = Some(Box::new(storage));
(
// Replace storage calls with our own implementations
rio::ext_get_allocated_storage.replace_implementation(ext_get_allocated_storage),
rio::ext_get_storage_into.replace_implementation(ext_get_storage_into),
rio::ext_set_storage.replace_implementation(ext_set_storage),
rio::ext_exists_storage.replace_implementation(ext_exists_storage),
rio::ext_clear_storage.replace_implementation(ext_clear_storage),
rio::ext_storage_root.replace_implementation(ext_storage_root),
sp_io::storage::host_read.replace_implementation(host_storage_read),
sp_io::storage::host_set.replace_implementation(host_storage_set),
sp_io::storage::host_get.replace_implementation(host_storage_get),
sp_io::storage::host_exists.replace_implementation(host_storage_exists),
sp_io::storage::host_clear.replace_implementation(host_storage_clear),
sp_io::storage::host_root.replace_implementation(host_storage_root),
sp_io::storage::host_clear_prefix.replace_implementation(host_clear_prefix),
)
};
@@ -94,24 +115,23 @@ pub fn validate_block<B: BlockT<Hash = H256>, E: ExecuteBlock<B>>(
/// The storage implementation used when validating a block that is using the
/// witness data as source.
struct WitnessStorage<B: BlockT> {
witness_data: MemoryDB<Blake2Hasher>,
witness_data: MemoryDB<HasherFor<B>>,
overlay: hashbrown::HashMap<Vec<u8>, Option<Vec<u8>>>,
storage_root: B::Hash,
}
impl<B: BlockT<Hash = H256>> WitnessStorage<B> {
impl<B: BlockT> WitnessStorage<B> {
/// Initialize from the given witness data and storage root.
///
/// Returns an error if given storage root was not found in the witness data.
fn new(
data: WitnessData,
storage_root: B::Hash,
) -> Result<Self, &'static str> {
fn new(data: WitnessData, storage_root: B::Hash) -> Result<Self, &'static str> {
let mut db = MemoryDB::default();
data.into_iter().for_each(|i| { db.insert(EMPTY_PREFIX, &i); });
data.into_iter().for_each(|i| {
db.insert(EMPTY_PREFIX, &i);
});
if !db.contains(&storage_root, EMPTY_PREFIX) {
return Err("Witness data does not contain given storage root.")
if !HashDB::contains(&db, &storage_root, EMPTY_PREFIX) {
return Err("Witness data does not contain given storage root.");
}
Ok(Self {
@@ -122,15 +142,36 @@ impl<B: BlockT<Hash = H256>> WitnessStorage<B> {
}
}
impl<B: BlockT<Hash = H256>> Storage for WitnessStorage<B> {
/// TODO: `TrieError` should implement `Debug` on `no_std`
fn unwrap_trie_error<R, T, E>(result: Result<R, Box<trie_db::TrieError<T, E>>>) -> R {
match result {
Ok(r) => r,
Err(error) => match *error {
trie_db::TrieError::InvalidStateRoot(_) => panic!("trie_db: Invalid state root"),
trie_db::TrieError::IncompleteDatabase(_) => panic!("trie_db: IncompleteDatabase"),
trie_db::TrieError::DecoderError(_, _) => panic!("trie_db: DecodeError"),
trie_db::TrieError::InvalidHash(_, _) => panic!("trie_db: InvalidHash"),
trie_db::TrieError::ValueAtIncompleteKey(_, _) => {
panic!("trie_db: ValueAtIncompleteKey")
}
},
}
}
impl<B: BlockT> Storage for WitnessStorage<B> {
fn get(&self, key: &[u8]) -> Option<Vec<u8>> {
self.overlay.get(key).cloned().or_else(|| {
read_trie_value::<Layout<Blake2Hasher>, _>(
&self.witness_data,
&self.storage_root,
key,
).ok()
}).unwrap_or(None)
self.overlay
.get(key)
.cloned()
.or_else(|| {
read_trie_value::<Layout<HasherFor<B>>, _>(
&self.witness_data,
&self.storage_root,
key,
)
.ok()
})
.unwrap_or(None)
}
fn insert(&mut self, key: &[u8], value: &[u8]) {
@@ -141,98 +182,77 @@ impl<B: BlockT<Hash = H256>> Storage for WitnessStorage<B> {
self.overlay.insert(key.to_vec(), None);
}
fn storage_root(&mut self) -> [u8; STORAGE_ROOT_LEN] {
let root = match delta_trie_root::<Layout<Blake2Hasher>, _, _, _, _>(
fn storage_root(&mut self) -> Vec<u8> {
let root = unwrap_trie_error(delta_trie_root::<Layout<HasherFor<B>>, _, _, _, _>(
&mut self.witness_data,
self.storage_root.clone(),
self.overlay.drain(),
) {
Ok(root) => root,
Err(e) => match *e {
trie_db::TrieError::InvalidStateRoot(_) => panic!("Invalid state root"),
trie_db::TrieError::IncompleteDatabase(_) => panic!("IncompleteDatabase"),
trie_db::TrieError::DecoderError(_, _) => panic!("DecodeError"),
));
root.encode()
}
fn clear_prefix(&mut self, prefix: &[u8]) {
self.overlay.iter_mut().for_each(|(k, v)| {
if k.starts_with(prefix) {
*v = None;
}
});
let trie = match TrieDB::<Layout<HasherFor<B>>>::new(&self.witness_data, &self.storage_root)
{
Ok(r) => r,
Err(_) => panic!(),
};
root.into()
}
}
let mut iter = unwrap_trie_error(trie.iter());
unwrap_trie_error(iter.seek(prefix));
unsafe fn ext_get_allocated_storage(
key_data: *const u8,
key_len: u32,
written_out: *mut u32,
) -> *mut u8 {
let key = slice::from_raw_parts(key_data, key_len as usize);
match STORAGE.as_mut().expect(STORAGE_SET_EXPECT).get(key) {
Some(value) => {
let mut out_value: Vec<_> = value.clone();
*written_out = out_value.len() as u32;
let ptr = out_value.as_mut_ptr();
mem::forget(out_value);
ptr
},
None => {
*written_out = u32::max_value();
ptr::null_mut()
for x in iter {
let (key, _) = unwrap_trie_error(x);
if !key.starts_with(prefix) {
break;
}
self.overlay.insert(key, None);
}
}
}
unsafe fn ext_set_storage(
key_data: *const u8,
key_len: u32,
value_data: *const u8,
value_len: u32,
) {
let key = slice::from_raw_parts(key_data, key_len as usize);
let value = slice::from_raw_parts(value_data, value_len as usize);
STORAGE.as_mut().expect(STORAGE_SET_EXPECT).insert(key, value);
}
unsafe fn ext_get_storage_into(
key_data: *const u8,
key_len: u32,
value_data: *mut u8,
value_len: u32,
value_offset: u32,
) -> u32 {
let key = slice::from_raw_parts(key_data, key_len as usize);
let out_value = slice::from_raw_parts_mut(value_data, value_len as usize);
match STORAGE.as_mut().expect(STORAGE_SET_EXPECT).get(key) {
fn host_storage_read(key: &[u8], value_out: &mut [u8], value_offset: u32) -> Option<u32> {
match storage().get(key) {
Some(value) => {
let value = &value[value_offset as usize..];
let len = cmp::min(value_len as usize, value.len());
out_value[..len].copy_from_slice(&value[..len]);
len as u32
},
None => {
u32::max_value()
let value_offset = value_offset as usize;
let data = &value[value_offset.min(value.len())..];
let written = sp_std::cmp::min(data.len(), value_out.len());
value_out[..written].copy_from_slice(&data[..written]);
Some(value.len() as u32)
}
None => None,
}
}
unsafe fn ext_exists_storage(key_data: *const u8, key_len: u32) -> u32 {
let key = slice::from_raw_parts(key_data, key_len as usize);
if STORAGE.as_mut().expect(STORAGE_SET_EXPECT).get(key).is_some() {
1
} else {
0
}
fn host_storage_set(key: &[u8], value: &[u8]) {
storage().insert(key, value);
}
unsafe fn ext_clear_storage(key_data: *const u8, key_len: u32) {
let key = slice::from_raw_parts(key_data, key_len as usize);
STORAGE.as_mut().expect(STORAGE_SET_EXPECT).remove(key);
fn host_storage_get(key: &[u8]) -> Option<Vec<u8>> {
storage().get(key).clone()
}
unsafe fn ext_storage_root(result: *mut u8) {
let res = STORAGE.as_mut().expect(STORAGE_SET_EXPECT).storage_root();
let result = slice::from_raw_parts_mut(result, STORAGE_ROOT_LEN);
result.copy_from_slice(&res);
fn host_storage_exists(key: &[u8]) -> bool {
storage().get(key).is_some()
}
fn host_storage_clear(key: &[u8]) {
storage().remove(key);
}
fn host_storage_root() -> Vec<u8> {
storage().storage_root()
}
fn host_clear_prefix(prefix: &[u8]) {
storage().clear_prefix(prefix)
}
+8 -12
View File
@@ -16,11 +16,11 @@
//! A module that enables a runtime to work as parachain.
#[cfg(test)]
mod tests;
#[cfg(not(feature = "std"))]
#[doc(hidden)]
pub mod implementation;
#[cfg(test)]
mod tests;
#[cfg(not(feature = "std"))]
#[doc(hidden)]
@@ -60,20 +60,16 @@ macro_rules! register_validate_block_impl {
use super::*;
#[no_mangle]
unsafe fn validate_block(
arguments: *const u8,
arguments_len: usize,
) -> u64 {
let params = $crate::validate_block::parachain::wasm_api::load_params(
arguments,
arguments_len,
);
unsafe fn validate_block(arguments: *const u8, arguments_len: usize) -> u64 {
let params =
$crate::validate_block::parachain::load_params(arguments, arguments_len);
let res = $crate::validate_block::implementation::validate_block::<
$block, $block_executor
$block,
$block_executor,
>(params);
$crate::validate_block::parachain::wasm_api::write_result(res)
$crate::validate_block::parachain::write_result(&res)
}
}
};
+51 -35
View File
@@ -16,18 +16,22 @@
use crate::{ParachainBlockData, WitnessData};
use rio::TestExternalities;
use keyring::AccountKeyring;
use runtime_primitives::{generic::BlockId, traits::{Block as BlockT, Header as HeaderT}};
use executor::{call_in_wasm, error::Result, WasmExecutionMethod};
use test_client::{
TestClientBuilder, TestClientBuilderExt, DefaultTestClientBuilderExt, Client, LongestChain,
runtime::{Block, Transfer, Hash, WASM_BINARY, Header}
};
use consensus_common::SelectChain;
use parachain::{ValidationParams, ValidationResult};
use sc_executor::{call_in_wasm, error::Result, WasmExecutionMethod};
use sp_blockchain::HeaderBackend;
use sp_consensus::SelectChain;
use sp_io::TestExternalities;
use sp_keyring::AccountKeyring;
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, Header as HeaderT},
};
use test_client::{
runtime::{Block, Hash, Header, Transfer, WASM_BINARY},
Client, DefaultTestClientBuilderExt, LongestChain, TestClientBuilder, TestClientBuilderExt,
};
use codec::{Encode, Decode};
use codec::{Decode, Encode};
fn call_validate_block(
parent_head: Header,
@@ -39,9 +43,16 @@ fn call_validate_block(
block_data: block_data.encode(),
parent_head: parent_head.encode(),
ingress: Vec::new(),
}.encode();
}
.encode();
call_in_wasm(
call_in_wasm::<
_,
(
sp_io::SubstrateHostFunctions,
sc_executor::deprecated_host_interface::SubstrateExternals,
),
>(
"validate_block",
&params,
WasmExecutionMethod::Interpreted,
@@ -60,25 +71,29 @@ fn create_extrinsics() -> Vec<<Block as BlockT>::Extrinsic> {
to: AccountKeyring::Bob.into(),
amount: 69,
nonce: 0,
}.into_signed_tx(),
}
.into_signed_tx(),
Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Charlie.into(),
amount: 100,
nonce: 1,
}.into_signed_tx(),
}
.into_signed_tx(),
Transfer {
from: AccountKeyring::Bob.into(),
to: AccountKeyring::Charlie.into(),
amount: 100,
nonce: 0,
}.into_signed_tx(),
}
.into_signed_tx(),
Transfer {
from: AccountKeyring::Charlie.into(),
to: AccountKeyring::Alice.into(),
amount: 500,
nonce: 0,
}.into_signed_tx(),
}
.into_signed_tx(),
]
}
@@ -90,20 +105,25 @@ fn build_block_with_proof(
client: &Client,
extrinsics: Vec<<Block as BlockT>::Extrinsic>,
) -> (Block, WitnessData) {
let block_id = BlockId::Hash(client.info().chain.best_hash);
let mut builder = client.new_block_at(
&block_id,
Default::default(),
true,
).expect("Initializes new block");
let block_id = BlockId::Hash(client.info().best_hash);
let mut builder = client
.new_block_at(&block_id, Default::default(), true)
.expect("Initializes new block");
extrinsics.into_iter().for_each(|e| builder.push(e).expect("Pushes an extrinsic"));
extrinsics
.into_iter()
.for_each(|e| builder.push(e).expect("Pushes an extrinsic"));
let (block, proof) = builder
.bake_and_extract_proof()
.expect("Finalizes block");
let built_block = builder.build().expect("Creates block");
(block, proof.expect("We enabled proof recording before."))
(
built_block.block,
built_block
.proof
.expect("We enabled proof recording before.")
.iter_nodes()
.collect(),
)
}
#[test]
@@ -118,7 +138,7 @@ fn validate_block_with_no_extrinsics() {
header.clone(),
extrinsics,
witness_data,
witness_data_storage_root
witness_data_storage_root,
);
let res_header = call_validate_block(parent_head, block_data).expect("Calls `validate_block`");
@@ -137,7 +157,7 @@ fn validate_block_with_extrinsics() {
header.clone(),
extrinsics,
witness_data,
witness_data_storage_root
witness_data_storage_root,
);
let res_header = call_validate_block(parent_head, block_data).expect("Calls `validate_block`");
@@ -154,11 +174,7 @@ fn validate_block_invalid_parent_hash() {
let (mut header, extrinsics) = block.deconstruct();
header.set_parent_hash(Hash::from_low_u64_be(1));
let block_data = ParachainBlockData::new(
header,
extrinsics,
witness_data,
witness_data_storage_root
);
let block_data =
ParachainBlockData::new(header, extrinsics, witness_data, witness_data_storage_root);
call_validate_block(parent_head, block_data).expect("Calls `validate_block`");
}