Fix review comments

This commit is contained in:
Bastian Köcher
2019-04-17 11:38:33 +02:00
parent bc512d4743
commit 3f1de002b6
@@ -28,7 +28,7 @@ use rstd::{slice, ptr, cmp, vec::Vec, boxed::Box, mem};
use hash_db::HashDB; use hash_db::HashDB;
static mut STORAGE: Option<Box<StorageT>> = None; static mut STORAGE: Option<Box<Storage>> = None;
/// The message to use as expect message while accessing the `STORAGE`. /// The message to use as expect message while accessing the `STORAGE`.
const STORAGE_SET_EXPECT: &str = const STORAGE_SET_EXPECT: &str =
"`STORAGE` needs to be set before calling this function."; "`STORAGE` needs to be set before calling this function.";
@@ -36,11 +36,9 @@ const STORAGE_ROOT_LEN: usize = 32;
/// Extract the hashing algorithm type from the given block type. /// Extract the hashing algorithm type from the given block type.
type HashingOf<B> = <<B as BlockT>::Header as HeaderT>::Hashing; type HashingOf<B> = <<B as BlockT>::Header as HeaderT>::Hashing;
/// Extract the hash type from the given block type.
type HashOf<B> = <B as BlockT>::Hash;
/// Abstract the storage into a trait without `Block` generic. /// Abstract the storage into a trait without `Block` generic.
trait StorageT { trait Storage {
/// Retrieve the value for the given key. /// Retrieve the value for the given key.
fn get(&self, key: &[u8]) -> Option<Vec<u8>>; fn get(&self, key: &[u8]) -> Option<Vec<u8>>;
@@ -66,7 +64,7 @@ pub fn validate_block<B: BlockT, E: ExecuteBlock<B>>(
.expect("Could not decode parachain block."); .expect("Could not decode parachain block.");
// TODO: Add `PolkadotInherent`. // TODO: Add `PolkadotInherent`.
let block = B::new(block_data.header, block_data.extrinsics); let block = B::new(block_data.header, block_data.extrinsics);
let storage = Storage::<B>::new( let storage = WitnessStorage::<B>::new(
block_data.witness_data, block_data.witness_data,
block_data.witness_data_storage_root, block_data.witness_data_storage_root,
).expect("Witness data and storage root always match; qed"); ).expect("Witness data and storage root always match; qed");
@@ -87,20 +85,21 @@ pub fn validate_block<B: BlockT, E: ExecuteBlock<B>>(
E::execute_block(block); E::execute_block(block);
} }
/// The storage implementation used when validating a block. /// The storage implementation used when validating a block that is using the
struct Storage<B: BlockT> { /// witness data as source.
struct WitnessStorage<B: BlockT> {
witness_data: MemoryDB<<HashingOf<B> as HashT>::Hasher>, witness_data: MemoryDB<<HashingOf<B> as HashT>::Hasher>,
overlay: hashbrown::HashMap<Vec<u8>, Option<Vec<u8>>>, overlay: hashbrown::HashMap<Vec<u8>, Option<Vec<u8>>>,
storage_root: HashOf<B>, storage_root: B::Hash,
} }
impl<B: BlockT> Storage<B> { impl<B: BlockT> WitnessStorage<B> {
/// Initialize from the given witness data and storage root. /// Initialize from the given witness data and storage root.
/// ///
/// Returns an error if given storage root was not found in the witness data. /// Returns an error if given storage root was not found in the witness data.
fn new( fn new(
data: WitnessData, data: WitnessData,
storage_root: HashOf<B> storage_root: B::Hash,
) -> Result<Self, &'static str> { ) -> Result<Self, &'static str> {
let mut db = MemoryDB::default(); let mut db = MemoryDB::default();
data.into_iter().for_each(|i| { db.insert(&[], &i); }); data.into_iter().for_each(|i| { db.insert(&[], &i); });
@@ -117,12 +116,13 @@ impl<B: BlockT> Storage<B> {
} }
} }
impl<B: BlockT> StorageT for Storage<B> { impl<B: BlockT> Storage for WitnessStorage<B> {
fn get(&self, key: &[u8]) -> Option<Vec<u8>> { fn get(&self, key: &[u8]) -> Option<Vec<u8>> {
self.overlay.get(key).cloned().or_else(|| { self.overlay.get(key).cloned().or_else(|| {
read_trie_value( read_trie_value(
&self.witness_data, &self.witness_data,
&self.storage_root, key &self.storage_root,
key,
).ok() ).ok()
}).unwrap_or(None) }).unwrap_or(None)
} }