Switch to shiny new fast, RLP-less trie (#795)

* Bump codec

* Fix tests

* Patricia trie builds

* Introduce trie

* Some yak shaving.

* Some fixes

* Remove RLP ref

* Fixes

* It builds!

* Some tests fixed

* Another test fix

* Rejig more hashes

* substrate-trie::iterator_works test

* Update lock

* Polish

* Docs

* Undo incorrect "fix" for tests

* Fix nits
This commit is contained in:
Gav Wood
2018-09-25 15:32:22 +01:00
committed by Arkadiy Paronyan
parent b02c274374
commit 82d6ca3484
90 changed files with 1977 additions and 1129 deletions
+14 -15
View File
@@ -23,9 +23,8 @@ use runtime_primitives::generic::BlockId;
use runtime_primitives::traits::{Block as BlockT, NumberFor};
use state_machine::backend::Backend as StateBackend;
use state_machine::ChangesTrieStorage as StateChangesTrieStorage;
use patricia_trie::NodeCodec;
use hashdb::Hasher;
use memorydb::MemoryDB;
use hash_db::Hasher;
use trie::MemoryDB;
/// State of a new block.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -49,14 +48,14 @@ impl NewBlockState {
}
/// Block insertion operation. Keeps hold if the inserted block state and data.
pub trait BlockImportOperation<Block, H, C>
pub trait BlockImportOperation<Block, H>
where
Block: BlockT,
H: Hasher,
C: NodeCodec<H>,
{
/// Associated state backend type.
type State: StateBackend<H, C>;
type State: StateBackend<H>;
/// Returns pending state. Returns None for backends with locally-unavailable state data.
fn state(&self) -> error::Result<Option<&Self::State>>;
@@ -73,7 +72,7 @@ where
/// has been used to check justification of this block).
fn update_authorities(&mut self, authorities: Vec<AuthorityId>);
/// Inject storage data into the database.
fn update_storage(&mut self, update: <Self::State as StateBackend<H, C>>::Transaction) -> error::Result<()>;
fn update_storage(&mut self, update: <Self::State as StateBackend<H>>::Transaction) -> error::Result<()>;
/// Inject storage data into the database replacing any existing data.
fn reset_storage<I: Iterator<Item=(Vec<u8>, Vec<u8>)>>(&mut self, iter: I) -> error::Result<()>;
/// Inject changes trie data into the database.
@@ -88,18 +87,18 @@ where
///
/// The same applies for live `BlockImportOperation`s: while an import operation building on a parent `P`
/// is alive, the state for `P` should not be pruned.
pub trait Backend<Block, H, C>: Send + Sync
pub trait Backend<Block, H>: Send + Sync
where
Block: BlockT,
H: Hasher,
C: NodeCodec<H>,
{
/// Associated block insertion operation type.
type BlockImportOperation: BlockImportOperation<Block, H, C>;
type BlockImportOperation: BlockImportOperation<Block, H>;
/// Associated blockchain backend type.
type Blockchain: ::blockchain::Backend<Block>;
/// Associated state backend type.
type State: StateBackend<H, C>;
type State: StateBackend<H>;
/// Changes trie storage.
type ChangesTrieStorage: StateChangesTrieStorage<H>;
@@ -123,17 +122,17 @@ where
}
/// Mark for all Backend implementations, that are making use of state data, stored locally.
pub trait LocalBackend<Block, H, C>: Backend<Block, H, C>
pub trait LocalBackend<Block, H>: Backend<Block, H>
where
Block: BlockT,
H: Hasher,
C: NodeCodec<H>,
{}
/// Mark for all Backend implementations, that are fetching required state data from remote nodes.
pub trait RemoteBackend<Block, H, C>: Backend<Block, H, C>
pub trait RemoteBackend<Block, H>: Backend<Block, H>
where
Block: BlockT,
H: Hasher,
C: NodeCodec<H>,
{}
+10 -12
View File
@@ -23,20 +23,18 @@ use runtime_primitives::traits::{Header as HeaderT, Hash, Block as BlockT, One,
use runtime_primitives::generic::BlockId;
use {backend, error, Client, CallExecutor};
use runtime_primitives::{ApplyResult, ApplyOutcome};
use patricia_trie::NodeCodec;
use primitives::{Blake2Hasher, RlpCodec};
use hashdb::Hasher;
use rlp::Encodable;
use primitives::{Blake2Hasher};
use hash_db::Hasher;
/// Utility for building new (valid) blocks from a stream of extrinsics.
pub struct BlockBuilder<B, E, Block, H, C>
pub struct BlockBuilder<B, E, Block, H>
where
B: backend::Backend<Block, H, C>,
E: CallExecutor<Block, H, C> + Clone,
B: backend::Backend<Block, H>,
E: CallExecutor<Block, H> + Clone,
Block: BlockT,
H: Hasher,
H::Out: Encodable + Ord,
C: NodeCodec<H>,
H::Out: Ord,
{
header: <Block as BlockT>::Header,
extrinsics: Vec<<Block as BlockT>::Extrinsic>,
@@ -45,10 +43,10 @@ where
changes: state_machine::OverlayedChanges,
}
impl<B, E, Block> BlockBuilder<B, E, Block, Blake2Hasher, RlpCodec>
impl<B, E, Block> BlockBuilder<B, E, Block, Blake2Hasher>
where
B: backend::Backend<Block, Blake2Hasher, RlpCodec>,
E: CallExecutor<Block, Blake2Hasher, RlpCodec> + Clone,
B: backend::Backend<Block, Blake2Hasher>,
E: CallExecutor<Block, Blake2Hasher> + Clone,
Block: BlockT,
{
/// Create a new instance of builder from the given client, building on the latest block.
+12 -14
View File
@@ -21,12 +21,10 @@ use runtime_primitives::traits::Block as BlockT;
use state_machine::{self, OverlayedChanges, Ext,
CodeExecutor, ExecutionManager, native_when_possible};
use executor::{RuntimeVersion, RuntimeInfo};
use patricia_trie::NodeCodec;
use hashdb::Hasher;
use rlp::Encodable;
use memorydb::MemoryDB;
use hash_db::Hasher;
use trie::MemoryDB;
use codec::Decode;
use primitives::{Blake2Hasher, RlpCodec};
use primitives::{Blake2Hasher};
use primitives::storage::well_known_keys;
use backend;
@@ -42,12 +40,12 @@ pub struct CallResult {
}
/// Method call executor.
pub trait CallExecutor<B, H, C>
pub trait CallExecutor<B, H>
where
B: BlockT,
H: Hasher,
H::Out: Ord + Encodable,
C: NodeCodec<H>,
H::Out: Ord,
{
/// Externalities error type.
type Error: state_machine::Error;
@@ -70,7 +68,7 @@ where
///
/// No changes are made.
fn call_at_state<
S: state_machine::Backend<H, C>,
S: state_machine::Backend<H>,
F: FnOnce(Result<Vec<u8>, Self::Error>, Result<Vec<u8>, Self::Error>) -> Result<Vec<u8>, Self::Error>,
>(&self,
state: &S,
@@ -83,7 +81,7 @@ where
/// Execute a call to a contract on top of given state, gathering execution proof.
///
/// No changes are made.
fn prove_at_state<S: state_machine::Backend<H, C>>(&self,
fn prove_at_state<S: state_machine::Backend<H>>(&self,
state: S,
overlay: &mut OverlayedChanges,
method: &str,
@@ -117,9 +115,9 @@ impl<B, E> Clone for LocalCallExecutor<B, E> where E: Clone {
}
}
impl<B, E, Block> CallExecutor<Block, Blake2Hasher, RlpCodec> for LocalCallExecutor<B, E>
impl<B, E, Block> CallExecutor<Block, Blake2Hasher> for LocalCallExecutor<B, E>
where
B: backend::LocalBackend<Block, Blake2Hasher, RlpCodec>,
B: backend::LocalBackend<Block, Blake2Hasher>,
E: CodeExecutor<Blake2Hasher> + RuntimeInfo,
Block: BlockT,
{
@@ -160,7 +158,7 @@ where
}
fn call_at_state<
S: state_machine::Backend<Blake2Hasher, RlpCodec>,
S: state_machine::Backend<Blake2Hasher>,
F: FnOnce(Result<Vec<u8>, Self::Error>, Result<Vec<u8>, Self::Error>) -> Result<Vec<u8>, Self::Error>,
>(&self,
state: &S,
@@ -180,7 +178,7 @@ where
).map_err(Into::into)
}
fn prove_at_state<S: state_machine::Backend<Blake2Hasher, RlpCodec>>(&self,
fn prove_at_state<S: state_machine::Backend<Blake2Hasher>>(&self,
state: S,
changes: &mut OverlayedChanges,
method: &str,
+16 -20
View File
@@ -23,11 +23,9 @@
//! root has. A correct proof implies that the claimed block is identical to the one
//! we discarded.
use hashdb;
use hash_db;
use heapsize::HeapSizeOf;
use patricia_trie::NodeCodec;
use rlp::Encodable;
use triehash;
use trie;
use primitives::H256;
use runtime_primitives::traits::{As, Header as HeaderT, SimpleArithmetic, One};
@@ -68,16 +66,16 @@ pub fn compute_root<Header, Hasher, I>(
) -> Option<Hasher::Out>
where
Header: HeaderT,
Hasher: hashdb::Hasher,
Hasher::Out: Ord + Encodable,
Hasher: hash_db::Hasher,
Hasher::Out: Ord,
I: IntoIterator<Item=Option<Header::Hash>>,
{
build_pairs::<Header, I>(cht_size, cht_num, hashes)
.map(|pairs| triehash::trie_root::<Hasher, _, _, _>(pairs))
.map(|pairs| trie::trie_root::<Hasher, _, _, _>(pairs))
}
/// Build CHT-based header proof.
pub fn build_proof<Header, Hasher, Codec, I>(
pub fn build_proof<Header, Hasher, I>(
cht_size: u64,
cht_num: Header::Number,
block_num: Header::Number,
@@ -85,16 +83,15 @@ pub fn build_proof<Header, Hasher, Codec, I>(
) -> Option<Vec<Vec<u8>>>
where
Header: HeaderT,
Hasher: hashdb::Hasher,
Hasher::Out: Ord + Encodable + HeapSizeOf,
Codec: NodeCodec<Hasher>,
Hasher: hash_db::Hasher,
Hasher::Out: Ord + HeapSizeOf,
I: IntoIterator<Item=Option<Header::Hash>>,
{
let transaction = build_pairs::<Header, I>(cht_size, cht_num, hashes)?
.into_iter()
.map(|(k, v)| (k, Some(v)))
.collect::<Vec<_>>();
let storage = InMemoryState::<Hasher, Codec>::default().update(transaction);
let storage = InMemoryState::<Hasher>::default().update(transaction);
let (value, proof) = prove_read(storage, &encode_cht_key(block_num)).ok()?;
if value.is_none() {
None
@@ -104,7 +101,7 @@ pub fn build_proof<Header, Hasher, Codec, I>(
}
/// Check CHT-based header proof.
pub fn check_proof<Header, Hasher, Codec>(
pub fn check_proof<Header, Hasher>(
local_root: Header::Hash,
local_number: Header::Number,
remote_hash: Header::Hash,
@@ -113,14 +110,13 @@ pub fn check_proof<Header, Hasher, Codec>(
where
Header: HeaderT,
Header::Hash: AsRef<[u8]>,
Hasher: hashdb::Hasher,
Hasher::Out: Ord + Encodable + HeapSizeOf,
Codec: NodeCodec<Hasher>,
Hasher: hash_db::Hasher,
Hasher::Out: Ord + HeapSizeOf,
{
let mut root: Hasher::Out = Default::default();
root.as_mut().copy_from_slice(local_root.as_ref());
let local_cht_key = encode_cht_key(local_number);
let local_cht_value = read_proof_check::<Hasher, Codec>(root, remote_proof,
let local_cht_value = read_proof_check::<Hasher>(root, remote_proof,
&local_cht_key).map_err(|e| ClientError::from(e))?;
let local_cht_value = local_cht_value.ok_or_else(|| ClientErrorKind::InvalidHeaderProof)?;
let local_hash = decode_cht_value(&local_cht_value).ok_or_else(|| ClientErrorKind::InvalidHeaderProof)?;
@@ -214,7 +210,7 @@ pub fn decode_cht_value(value: &[u8]) -> Option<H256> {
#[cfg(test)]
mod tests {
use primitives::{Blake2Hasher, RlpCodec};
use primitives::{Blake2Hasher};
use test_client::runtime::Header;
use super::*;
@@ -263,13 +259,13 @@ mod tests {
#[test]
fn build_proof_fails_when_querying_wrong_block() {
assert!(build_proof::<Header, Blake2Hasher, RlpCodec, _>(
assert!(build_proof::<Header, Blake2Hasher, _>(
SIZE, 0, (SIZE * 1000) as u64, vec![Some(1.into()); SIZE as usize]).is_none());
}
#[test]
fn build_proof_works() {
assert!(build_proof::<Header, Blake2Hasher, RlpCodec, _>(
assert!(build_proof::<Header, Blake2Hasher, _>(
SIZE, 0, (SIZE / 2) as u64, vec![Some(1.into()); SIZE as usize]).is_some());
}
}
+20 -20
View File
@@ -23,7 +23,7 @@ use primitives::AuthorityId;
use runtime_primitives::{bft::Justification, generic::{BlockId, SignedBlock, Block as RuntimeBlock}};
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, Zero, As, NumberFor, CurrentHeight, BlockNumberToHash};
use runtime_primitives::BuildStorage;
use primitives::{Blake2Hasher, RlpCodec, H256};
use primitives::{Blake2Hasher, H256};
use primitives::storage::{StorageKey, StorageData};
use primitives::storage::well_known_keys;
use codec::{Encode, Decode};
@@ -183,7 +183,7 @@ impl<Block: BlockT> JustifiedHeader<Block> {
pub fn new_in_mem<E, Block, S>(
executor: E,
genesis_storage: S,
) -> error::Result<Client<in_mem::Backend<Block, Blake2Hasher, RlpCodec>, LocalCallExecutor<in_mem::Backend<Block, Blake2Hasher, RlpCodec>, E>, Block>>
) -> error::Result<Client<in_mem::Backend<Block, Blake2Hasher>, LocalCallExecutor<in_mem::Backend<Block, Blake2Hasher>, E>, Block>>
where
E: CodeExecutor<Blake2Hasher> + RuntimeInfo,
S: BuildStorage,
@@ -196,8 +196,8 @@ pub fn new_in_mem<E, Block, S>(
}
impl<B, E, Block> Client<B, E, Block> where
B: backend::Backend<Block, Blake2Hasher, RlpCodec>,
E: CallExecutor<Block, Blake2Hasher, RlpCodec>,
B: backend::Backend<Block, Blake2Hasher>,
E: CallExecutor<Block, Blake2Hasher>,
Block: BlockT,
{
/// Creates new Substrate Client with given blockchain and code executor.
@@ -311,20 +311,20 @@ impl<B, E, Block> Client<B, E, Block> where
let cht_num = cht::block_to_cht_number(cht_size, block_num).ok_or_else(proof_error)?;
let cht_start = cht::start_number(cht_size, cht_num);
let headers = (cht_start.as_()..).map(|num| self.block_hash(As::sa(num)).unwrap_or_default());
let proof = cht::build_proof::<Block::Header, Blake2Hasher, RlpCodec, _>(cht_size, cht_num, block_num, headers)
let proof = cht::build_proof::<Block::Header, Blake2Hasher, _>(cht_size, cht_num, block_num, headers)
.ok_or_else(proof_error)?;
Ok((header, proof))
}
/// Create a new block, built on the head of the chain.
pub fn new_block(&self) -> error::Result<block_builder::BlockBuilder<B, E, Block, Blake2Hasher, RlpCodec>>
pub fn new_block(&self) -> error::Result<block_builder::BlockBuilder<B, E, Block, Blake2Hasher>>
where E: Clone
{
block_builder::BlockBuilder::new(self)
}
/// Create a new block, built on top of `parent`.
pub fn new_block_at(&self, parent: &BlockId<Block>) -> error::Result<block_builder::BlockBuilder<B, E, Block, Blake2Hasher, RlpCodec>>
pub fn new_block_at(&self, parent: &BlockId<Block>) -> error::Result<block_builder::BlockBuilder<B, E, Block, Blake2Hasher>>
where E: Clone
{
block_builder::BlockBuilder::at_block(parent, &self)
@@ -726,8 +726,8 @@ impl<B, E, Block> Client<B, E, Block> where
}
impl<B, E, Block> CurrentHeight for Client<B, E, Block> where
B: backend::Backend<Block, Blake2Hasher, RlpCodec>,
E: CallExecutor<Block, Blake2Hasher, RlpCodec> + Clone,
B: backend::Backend<Block, Blake2Hasher>,
E: CallExecutor<Block, Blake2Hasher> + Clone,
Block: BlockT,
{
type BlockNumber = <Block::Header as HeaderT>::Number;
@@ -737,8 +737,8 @@ impl<B, E, Block> CurrentHeight for Client<B, E, Block> where
}
impl<B, E, Block> BlockNumberToHash for Client<B, E, Block> where
B: backend::Backend<Block, Blake2Hasher, RlpCodec>,
E: CallExecutor<Block, Blake2Hasher, RlpCodec> + Clone,
B: backend::Backend<Block, Blake2Hasher>,
E: CallExecutor<Block, Blake2Hasher> + Clone,
Block: BlockT,
{
type BlockNumber = <Block::Header as HeaderT>::Number;
@@ -750,8 +750,8 @@ impl<B, E, Block> BlockNumberToHash for Client<B, E, Block> where
impl<B, E, Block> bft::BlockImport<Block> for Client<B, E, Block>
where
B: backend::Backend<Block, Blake2Hasher, RlpCodec>,
E: CallExecutor<Block, Blake2Hasher, RlpCodec>,
B: backend::Backend<Block, Blake2Hasher>,
E: CallExecutor<Block, Blake2Hasher>,
Block: BlockT,
{
fn import_block(
@@ -779,8 +779,8 @@ impl<B, E, Block> bft::BlockImport<Block> for Client<B, E, Block>
impl<B, E, Block> bft::Authorities<Block> for Client<B, E, Block>
where
B: backend::Backend<Block, Blake2Hasher, RlpCodec>,
E: CallExecutor<Block, Blake2Hasher, RlpCodec>,
B: backend::Backend<Block, Blake2Hasher>,
E: CallExecutor<Block, Blake2Hasher>,
Block: BlockT,
{
fn authorities(&self, at: &BlockId<Block>) -> Result<Vec<AuthorityId>, bft::Error> {
@@ -802,7 +802,7 @@ impl<B, E, Block> bft::Authorities<Block> for Client<B, E, Block>
impl<B, E, Block> BlockchainEvents<Block> for Client<B, E, Block>
where
E: CallExecutor<Block, Blake2Hasher, RlpCodec>,
E: CallExecutor<Block, Blake2Hasher>,
Block: BlockT,
{
/// Get block import event stream.
@@ -826,8 +826,8 @@ where
impl<B, E, Block> ChainHead<Block> for Client<B, E, Block>
where
B: backend::Backend<Block, Blake2Hasher, RlpCodec>,
E: CallExecutor<Block, Blake2Hasher, RlpCodec>,
B: backend::Backend<Block, Blake2Hasher>,
E: CallExecutor<Block, Blake2Hasher>,
Block: BlockT,
{
fn best_block_header(&self) -> error::Result<<Block as BlockT>::Header> {
@@ -837,8 +837,8 @@ where
impl<B, E, Block> BlockBody<Block> for Client<B, E, Block>
where
B: backend::Backend<Block, Blake2Hasher, RlpCodec>,
E: CallExecutor<Block, Blake2Hasher, RlpCodec>,
B: backend::Backend<Block, Blake2Hasher>,
E: CallExecutor<Block, Blake2Hasher>,
Block: BlockT,
{
fn block_body(&self, id: &BlockId<Block>) -> error::Result<Option<Vec<<Block as BlockT>::Extrinsic>>> {
+4 -4
View File
@@ -50,7 +50,7 @@ mod tests {
use test_client;
use test_client::runtime::genesismap::{GenesisConfig, additional_storage_with_genesis};
use test_client::runtime::{Hash, Transfer, Block, BlockNumber, Header, Digest, Extrinsic};
use primitives::{Blake2Hasher, RlpCodec, ed25519::{Public, Pair}};
use primitives::{Blake2Hasher, ed25519::{Public, Pair}};
native_executor_instance!(Executor, test_client::runtime::api::dispatch, test_client::runtime::VERSION, include_bytes!("../../test-runtime/wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm"));
@@ -59,13 +59,13 @@ mod tests {
}
fn construct_block(
backend: &InMemory<Blake2Hasher, RlpCodec>,
backend: &InMemory<Blake2Hasher>,
number: BlockNumber,
parent_hash: Hash,
state_root: Hash,
txs: Vec<Transfer>
) -> (Vec<u8>, Hash) {
use triehash::ordered_trie_root;
use trie::ordered_trie_root;
let transactions = txs.into_iter().map(|tx| {
let signature = Pair::from(Keyring::from_public(Public::from_raw(tx.from.0)).unwrap())
@@ -124,7 +124,7 @@ mod tests {
(vec![].and(&Block { header, extrinsics: transactions }), hash)
}
fn block1(genesis_hash: Hash, backend: &InMemory<Blake2Hasher, RlpCodec>) -> (Vec<u8>, Hash) {
fn block1(genesis_hash: Hash, backend: &InMemory<Blake2Hasher>) -> (Vec<u8>, Hash) {
construct_block(
backend,
1,
+19 -22
View File
@@ -30,10 +30,9 @@ use runtime_primitives::bft::Justification;
use blockchain::{self, BlockStatus, HeaderBackend};
use state_machine::backend::{Backend as StateBackend, InMemory};
use state_machine::InMemoryChangesTrieStorage;
use patricia_trie::NodeCodec;
use hashdb::Hasher;
use hash_db::Hasher;
use heapsize::HeapSizeOf;
use memorydb::MemoryDB;
use trie::MemoryDB;
struct PendingBlock<B: BlockT> {
block: StoredBlock<B>,
@@ -304,22 +303,22 @@ impl<Block: BlockT> light::blockchain::Storage<Block> for Blockchain<Block>
}
/// In-memory operation.
pub struct BlockImportOperation<Block: BlockT, H: Hasher, C: NodeCodec<H>> {
pub struct BlockImportOperation<Block: BlockT, H: Hasher> {
pending_block: Option<PendingBlock<Block>>,
pending_authorities: Option<Vec<AuthorityId>>,
old_state: InMemory<H, C>,
new_state: Option<InMemory<H, C>>,
old_state: InMemory<H>,
new_state: Option<InMemory<H>>,
changes_trie_update: Option<MemoryDB<H>>,
}
impl<Block, H, C> backend::BlockImportOperation<Block, H, C> for BlockImportOperation<Block, H, C>
impl<Block, H> backend::BlockImportOperation<Block, H> for BlockImportOperation<Block, H>
where
Block: BlockT,
H: Hasher,
C: NodeCodec<H>,
H::Out: HeapSizeOf,
{
type State = InMemory<H, C>;
type State = InMemory<H>;
fn state(&self) -> error::Result<Option<&Self::State>> {
Ok(Some(&self.old_state))
@@ -344,7 +343,7 @@ where
self.pending_authorities = Some(authorities);
}
fn update_storage(&mut self, update: <InMemory<H, C> as StateBackend<H, C>>::Transaction) -> error::Result<()> {
fn update_storage(&mut self, update: <InMemory<H> as StateBackend<H>>::Transaction) -> error::Result<()> {
self.new_state = Some(self.old_state.update(update));
Ok(())
}
@@ -361,27 +360,27 @@ where
}
/// In-memory backend. Keeps all states and blocks in memory. Useful for testing.
pub struct Backend<Block, H, C>
pub struct Backend<Block, H>
where
Block: BlockT,
H: Hasher,
C: NodeCodec<H>,
H::Out: HeapSizeOf + From<Block::Hash>,
{
states: RwLock<HashMap<Block::Hash, InMemory<H, C>>>,
states: RwLock<HashMap<Block::Hash, InMemory<H>>>,
changes_trie_storage: InMemoryChangesTrieStorage<H>,
blockchain: Blockchain<Block>,
}
impl<Block, H, C> Backend<Block, H, C>
impl<Block, H> Backend<Block, H>
where
Block: BlockT,
H: Hasher,
C: NodeCodec<H>,
H::Out: HeapSizeOf + From<Block::Hash>,
{
/// Create a new instance of in-mem backend.
pub fn new() -> Backend<Block, H, C> {
pub fn new() -> Backend<Block, H> {
Backend {
states: RwLock::new(HashMap::new()),
changes_trie_storage: InMemoryChangesTrieStorage::new(),
@@ -390,16 +389,15 @@ where
}
}
impl<Block, H, C> backend::Backend<Block, H, C> for Backend<Block, H, C>
impl<Block, H> backend::Backend<Block, H> for Backend<Block, H>
where
Block: BlockT,
H: Hasher,
H::Out: HeapSizeOf + From<Block::Hash>,
C: NodeCodec<H> + Send + Sync,
{
type BlockImportOperation = BlockImportOperation<Block, H, C>;
type BlockImportOperation = BlockImportOperation<Block, H>;
type Blockchain = Blockchain<Block>;
type State = InMemory<H, C>;
type State = InMemory<H>;
type ChangesTrieStorage = InMemoryChangesTrieStorage<H>;
fn begin_operation(&self, block: BlockId<Block>) -> error::Result<Self::BlockImportOperation> {
@@ -471,12 +469,11 @@ where
}
}
impl<Block, H, C> backend::LocalBackend<Block, H, C> for Backend<Block, H, C>
impl<Block, H> backend::LocalBackend<Block, H> for Backend<Block, H>
where
Block: BlockT,
H: Hasher,
H::Out: HeapSizeOf + From<Block::Hash>,
C: NodeCodec<H> + Send + Sync,
{}
impl<Block: BlockT> Cache<Block> {
+6 -6
View File
@@ -20,6 +20,7 @@
#![recursion_limit="128"]
extern crate substrate_bft as bft;
extern crate substrate_trie as trie;
extern crate parity_codec as codec;
extern crate substrate_primitives as primitives;
extern crate sr_io as runtime_io;
@@ -33,12 +34,12 @@ extern crate substrate_state_machine as state_machine;
extern crate fnv;
extern crate futures;
extern crate parking_lot;
extern crate triehash;
extern crate patricia_trie;
extern crate hashdb;
extern crate trie_root;
extern crate trie_db;
extern crate hash_db;
extern crate rlp;
extern crate heapsize;
extern crate memorydb;
extern crate memory_db;
#[macro_use] extern crate error_chain;
#[macro_use] extern crate log;
@@ -62,8 +63,7 @@ pub use call_executor::{CallResult, CallExecutor, LocalCallExecutor};
pub use client::{
new_in_mem,
BlockBody, BlockStatus, BlockOrigin, ImportNotifications, FinalityNotifications, BlockchainEvents,
Client, ClientInfo, ChainHead,
ImportResult, JustifiedHeader,
Client, ClientInfo, ChainHead, ImportResult, JustifiedHeader,
};
pub use notifications::{StorageEventStream, StorageChangeSet};
pub use state_machine::ExecutionStrategy;
+12 -13
View File
@@ -31,9 +31,8 @@ use blockchain::HeaderBackend as BlockchainHeaderBackend;
use error::{Error as ClientError, ErrorKind as ClientErrorKind, Result as ClientResult};
use light::blockchain::{Blockchain, Storage as BlockchainStorage};
use light::fetcher::{Fetcher, RemoteReadRequest};
use patricia_trie::NodeCodec;
use hashdb::Hasher;
use memorydb::MemoryDB;
use hash_db::Hasher;
use trie::MemoryDB;
use heapsize::HeapSizeOf;
/// Light client backend.
@@ -69,12 +68,12 @@ impl<S, F> Backend<S, F> {
}
}
impl<S, F, Block, H, C> ClientBackend<Block, H, C> for Backend<S, F> where
impl<S, F, Block, H> ClientBackend<Block, H> for Backend<S, F> where
Block: BlockT,
S: BlockchainStorage<Block>,
F: Fetcher<Block>,
H: Hasher,
C: NodeCodec<H>,
H::Out: HeapSizeOf,
{
type BlockImportOperation = ImportOperation<Block, S, F>;
@@ -131,23 +130,23 @@ impl<S, F, Block, H, C> ClientBackend<Block, H, C> for Backend<S, F> where
}
}
impl<S, F, Block, H, C> RemoteBackend<Block, H, C> for Backend<S, F>
impl<S, F, Block, H> RemoteBackend<Block, H> for Backend<S, F>
where
Block: BlockT,
S: BlockchainStorage<Block>,
F: Fetcher<Block>,
H: Hasher,
H::Out: HeapSizeOf,
C: NodeCodec<H>,
{}
impl<S, F, Block, H, C> BlockImportOperation<Block, H, C> for ImportOperation<Block, S, F>
impl<S, F, Block, H> BlockImportOperation<Block, H> for ImportOperation<Block, S, F>
where
Block: BlockT,
F: Fetcher<Block>,
S: BlockchainStorage<Block>,
H: Hasher,
C: NodeCodec<H>,
{
type State = OnDemandState<Block, S, F>;
@@ -172,7 +171,7 @@ where
self.authorities = Some(authorities);
}
fn update_storage(&mut self, _update: <Self::State as StateBackend<H, C>>::Transaction) -> ClientResult<()> {
fn update_storage(&mut self, _update: <Self::State as StateBackend<H>>::Transaction) -> ClientResult<()> {
// we're not storing anything locally => ignore changes
Ok(())
}
@@ -188,13 +187,13 @@ where
}
}
impl<Block, S, F, H, C> StateBackend<H, C> for OnDemandState<Block, S, F>
impl<Block, S, F, H> StateBackend<H> for OnDemandState<Block, S, F>
where
Block: BlockT,
S: BlockchainStorage<Block>,
F: Fetcher<Block>,
H: Hasher,
C: NodeCodec<H>,
{
type Error = ClientError;
type Transaction = ();
@@ -234,7 +233,7 @@ impl<Block, S, F, H, C> StateBackend<H, C> for OnDemandState<Block, S, F>
Vec::new()
}
fn try_into_trie_backend(self) -> Option<TrieBackend<Self::TrieBackendStorage, H, C>> {
fn try_into_trie_backend(self) -> Option<TrieBackend<Self::TrieBackendStorage, H>> {
None
}
}
@@ -25,9 +25,7 @@ use runtime_primitives::generic::BlockId;
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT};
use state_machine::{Backend as StateBackend, CodeExecutor, OverlayedChanges,
execution_proof_check, ExecutionManager};
use patricia_trie::NodeCodec;
use hashdb::Hasher;
use rlp::Encodable;
use hash_db::Hasher;
use blockchain::Backend as ChainBackend;
use call_executor::{CallExecutor, CallResult};
@@ -36,43 +34,41 @@ use light::fetcher::{Fetcher, RemoteCallRequest};
use executor::RuntimeVersion;
use codec::Decode;
use heapsize::HeapSizeOf;
use memorydb::MemoryDB;
use trie::MemoryDB;
/// Call executor that executes methods on remote node, querying execution proof
/// and checking proof by re-executing locally.
pub struct RemoteCallExecutor<B, F, H, C> {
pub struct RemoteCallExecutor<B, F, H> {
blockchain: Arc<B>,
fetcher: Arc<F>,
_hasher: PhantomData<H>,
_codec: PhantomData<C>,
}
impl<B, F, H, C> Clone for RemoteCallExecutor<B, F, H, C> {
impl<B, F, H> Clone for RemoteCallExecutor<B, F, H> {
fn clone(&self) -> Self {
RemoteCallExecutor {
blockchain: self.blockchain.clone(),
fetcher: self.fetcher.clone(),
_hasher: Default::default(),
_codec: Default::default(),
}
}
}
impl<B, F, H, C> RemoteCallExecutor<B, F, H, C> {
impl<B, F, H> RemoteCallExecutor<B, F, H> {
/// Creates new instance of remote call executor.
pub fn new(blockchain: Arc<B>, fetcher: Arc<F>) -> Self {
RemoteCallExecutor { blockchain, fetcher, _hasher: PhantomData, _codec: PhantomData }
RemoteCallExecutor { blockchain, fetcher, _hasher: PhantomData }
}
}
impl<B, F, Block, H, C> CallExecutor<Block, H, C> for RemoteCallExecutor<B, F, H, C>
impl<B, F, Block, H> CallExecutor<Block, H> for RemoteCallExecutor<B, F, H>
where
Block: BlockT,
B: ChainBackend<Block>,
F: Fetcher<Block>,
H: Hasher,
H::Out: Ord + Encodable,
C: NodeCodec<H>
H::Out: Ord,
{
type Error = ClientError;
@@ -100,7 +96,7 @@ where
}
fn call_at_state<
S: StateBackend<H, C>,
S: StateBackend<H>,
FF: FnOnce(Result<Vec<u8>, Self::Error>, Result<Vec<u8>, Self::Error>) -> Result<Vec<u8>, Self::Error>
>(&self,
_state: &S,
@@ -112,7 +108,7 @@ where
Err(ClientErrorKind::NotAvailableOnLightClient.into())
}
fn prove_at_state<S: StateBackend<H, C>>(
fn prove_at_state<S: StateBackend<H>>(
&self,
_state: S,
_changes: &mut OverlayedChanges,
@@ -128,7 +124,7 @@ where
}
/// Check remote execution proof using given backend.
pub fn check_execution_proof<Header, E, H, C>(
pub fn check_execution_proof<Header, E, H>(
executor: &E,
request: &RemoteCallRequest<Header>,
remote_proof: Vec<Vec<u8>>
@@ -137,15 +133,15 @@ pub fn check_execution_proof<Header, E, H, C>(
Header: HeaderT,
E: CodeExecutor<H>,
H: Hasher,
H::Out: Ord + Encodable + HeapSizeOf,
C: NodeCodec<H>,
H::Out: Ord + HeapSizeOf,
{
let local_state_root = request.header.state_root();
let mut root: H::Out = Default::default();
root.as_mut().copy_from_slice(local_state_root.as_ref());
let mut changes = OverlayedChanges::default();
let local_result = execution_proof_check::<H, C, _>(
let local_result = execution_proof_check::<H, _>(
root,
remote_proof,
&mut changes,
@@ -161,7 +157,6 @@ mod tests {
use test_client;
use executor::NativeExecutionDispatch;
use super::*;
use primitives::RlpCodec;
#[test]
fn execution_proof_is_generated_and_checked() {
@@ -176,7 +171,7 @@ mod tests {
// check remote execution proof locally
let local_executor = test_client::LocalExecutor::new();
check_execution_proof::<_, _, _, RlpCodec>(&local_executor, &RemoteCallRequest {
check_execution_proof(&local_executor, &RemoteCallRequest {
block: test_client::runtime::Hash::default(),
header: test_client::runtime::Header {
state_root: remote_block_storage_root.into(),
+12 -16
View File
@@ -18,9 +18,7 @@
use futures::IntoFuture;
use hashdb::Hasher;
use patricia_trie::NodeCodec;
use rlp::Encodable;
use hash_db::Hasher;
use heapsize::HeapSizeOf;
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT};
use state_machine::{CodeExecutor, read_proof_check};
@@ -115,28 +113,26 @@ pub trait FetchChecker<Block: BlockT>: Send + Sync {
}
/// Remote data checker.
pub struct LightDataChecker<E, H, C> {
pub struct LightDataChecker<E, H> {
executor: E,
_hasher: PhantomData<H>,
_codec: PhantomData<C>,
}
impl<E, H, C> LightDataChecker<E, H, C> {
impl<E, H> LightDataChecker<E, H> {
/// Create new light data checker.
pub fn new(executor: E) -> Self {
Self {
executor, _hasher: PhantomData, _codec: PhantomData
executor, _hasher: PhantomData
}
}
}
impl<E, Block, H, C> FetchChecker<Block> for LightDataChecker<E, H, C>
impl<E, Block, H> FetchChecker<Block> for LightDataChecker<E, H>
where
Block: BlockT,
E: CodeExecutor<H>,
H: Hasher,
C: NodeCodec<H> + Sync + Send,
H::Out: Ord + Encodable + HeapSizeOf,
H::Out: Ord + HeapSizeOf,
{
fn check_header_proof(
&self,
@@ -147,7 +143,7 @@ impl<E, Block, H, C> FetchChecker<Block> for LightDataChecker<E, H, C>
let remote_header = remote_header.ok_or_else(||
ClientError::from(ClientErrorKind::InvalidHeaderProof))?;
let remote_header_hash = remote_header.hash();
cht::check_proof::<Block::Header, H, C>(
cht::check_proof::<Block::Header, H>(
request.cht_root,
request.block,
remote_header_hash,
@@ -162,7 +158,7 @@ impl<E, Block, H, C> FetchChecker<Block> for LightDataChecker<E, H, C>
) -> ClientResult<Option<Vec<u8>>> {
let mut root: H::Out = Default::default();
root.as_mut().copy_from_slice(request.header.state_root().as_ref());
read_proof_check::<H, C>(root, remote_proof, &request.key).map_err(Into::into)
read_proof_check::<H>(root, remote_proof, &request.key).map_err(Into::into)
}
fn check_execution_proof(
@@ -170,7 +166,7 @@ impl<E, Block, H, C> FetchChecker<Block> for LightDataChecker<E, H, C>
request: &RemoteCallRequest<Block::Header>,
remote_proof: Vec<Vec<u8>>
) -> ClientResult<CallResult> {
check_execution_proof::<_, _, H, C>(&self.executor, request, remote_proof)
check_execution_proof::<_, _, H>(&self.executor, request, remote_proof)
}
}
@@ -186,7 +182,7 @@ pub mod tests {
use in_mem::{Blockchain as InMemoryBlockchain};
use light::fetcher::{Fetcher, FetchChecker, LightDataChecker,
RemoteCallRequest, RemoteHeaderRequest};
use primitives::{Blake2Hasher, RlpCodec};
use primitives::{Blake2Hasher};
use primitives::storage::well_known_keys;
use runtime_primitives::generic::BlockId;
use state_machine::Backend;
@@ -213,7 +209,7 @@ pub mod tests {
}
fn prepare_for_read_proof_check() -> (
LightDataChecker<executor::NativeExecutor<test_client::LocalExecutor>, Blake2Hasher, RlpCodec>,
LightDataChecker<executor::NativeExecutor<test_client::LocalExecutor>, Blake2Hasher>,
Header, Vec<Vec<u8>>, usize)
{
// prepare remote client
@@ -242,7 +238,7 @@ pub mod tests {
}
fn prepare_for_header_proof_check(insert_cht: bool) -> (
LightDataChecker<executor::NativeExecutor<test_client::LocalExecutor>, Blake2Hasher, RlpCodec>,
LightDataChecker<executor::NativeExecutor<test_client::LocalExecutor>, Blake2Hasher>,
Hash, Header, Vec<Vec<u8>>)
{
// prepare remote client
+6 -7
View File
@@ -23,7 +23,7 @@ pub mod fetcher;
use std::sync::Arc;
use primitives::{Blake2Hasher, RlpCodec};
use primitives::{Blake2Hasher};
use runtime_primitives::BuildStorage;
use runtime_primitives::traits::Block as BlockT;
use state_machine::{CodeExecutor, ExecutionStrategy};
@@ -34,8 +34,7 @@ use light::backend::Backend;
use light::blockchain::{Blockchain, Storage as BlockchainStorage};
use light::call_executor::RemoteCallExecutor;
use light::fetcher::{Fetcher, LightDataChecker};
use hashdb::Hasher;
use patricia_trie::NodeCodec;
use hash_db::Hasher;
/// Create an instance of light client blockchain backend.
pub fn new_light_blockchain<B: BlockT, S: BlockchainStorage<B>, F>(storage: S) -> Arc<Blockchain<S, F>> {
@@ -53,7 +52,7 @@ pub fn new_light<B, S, F, GS>(
backend: Arc<Backend<S, F>>,
fetcher: Arc<F>,
genesis_storage: GS,
) -> ClientResult<Client<Backend<S, F>, RemoteCallExecutor<Blockchain<S, F>, F, Blake2Hasher, RlpCodec>, B>>
) -> ClientResult<Client<Backend<S, F>, RemoteCallExecutor<Blockchain<S, F>, F, Blake2Hasher>, B>>
where
B: BlockT,
S: BlockchainStorage<B>,
@@ -65,13 +64,13 @@ pub fn new_light<B, S, F, GS>(
}
/// Create an instance of fetch data checker.
pub fn new_fetch_checker<E, H, C>(
pub fn new_fetch_checker<E, H>(
executor: E,
) -> LightDataChecker<E, H, C>
) -> LightDataChecker<E, H>
where
E: CodeExecutor<H>,
H: Hasher,
C: NodeCodec<H>,
{
LightDataChecker::new(executor)
}