Skip to content

Commit

Permalink
Remove client.backend (paritytech#2960)
Browse files Browse the repository at this point in the history
* generalize tree_root to remove client.backend dependency

* replace client.backend.blockchain.header with client.header

* move used_state_cache_size into client info

* Create intermediate Setup State. Fixes paritytech#1134

* remove client.backend from finality proof

* update node-template

* move memory backend into test helper mode

* move test helper into client

* starting the big refactor, remove unused functions

* apply_finality

* apply_finality

* replacing more .backend from environment with client directly

* remove .backend from grandpa by using traits

* remove .backend from babe

* remove .backend from tests where it is not needed

* remove .backend from tests

* fixing tests

* fixing tests

* fixing more tests

* fixing tests

* fix all forks test

* fix style

* fixing unnecessary allocation

* remove old test.

* fix service docs

* apply suggestion

* minor clean ups

* turns out the test-helper features actually is being used!

* fixing line length.

* fix line length

* minor cleaning

* Apply suggestions from code review

thanks, @basti

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* address grumbles

* simplify finalize block on client

* move block back into inner function

* Apply suggestions from code review

Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com>

* use as.ref instead of match

* Update core/client/src/backend.rs

Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com>
  • Loading branch information
gnunicorn and Demi-Marie authored Aug 30, 2019
1 parent 500b1d5 commit 409f5aa
Show file tree
Hide file tree
Showing 30 changed files with 626 additions and 571 deletions.
6 changes: 3 additions & 3 deletions core/cli/src/informant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! Console informant. Prints sync progress and block events. Runs on the calling thread.
use client::{backend::Backend, BlockchainEvents};
use client::BlockchainEvents;
use futures::{Future, Stream};
use futures03::{StreamExt as _, TryStreamExt as _};
use log::{info, warn};
Expand Down Expand Up @@ -48,8 +48,8 @@ pub fn build(service: &impl AbstractService) -> impl Future<Item = (), Error = (
if let Some((ref last_num, ref last_hash)) = last_best {
if n.header.parent_hash() != last_hash && n.is_new_best {
let tree_route = ::client::blockchain::tree_route(
#[allow(deprecated)]
client.backend().blockchain(),
|id| client.header(&id)?.ok_or_else(
|| client::error::Error::UnknownBlock(format!("{:?}", id))),
BlockId::Hash(last_hash.clone()),
BlockId::Hash(n.hash),
);
Expand Down
34 changes: 24 additions & 10 deletions core/client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,16 @@ pub fn new_client<E, S, Block, RA>(
genesis_storage: S,
execution_strategies: ExecutionStrategies,
keystore: Option<primitives::traits::BareCryptoStorePtr>,
) -> Result<
client::Client<Backend<Block>,
client::LocalCallExecutor<Backend<Block>, E>, Block, RA>, client::error::Error
) -> Result<(
client::Client<
Backend<Block>,
client::LocalCallExecutor<Backend<Block>, E>,
Block,
RA,
>,
Arc<Backend<Block>>,
),
client::error::Error,
>
where
Block: BlockT<Hash=H256>,
Expand All @@ -208,7 +215,10 @@ pub fn new_client<E, S, Block, RA>(
{
let backend = Arc::new(Backend::new(settings, CANONICALIZATION_DELAY)?);
let executor = client::LocalCallExecutor::new(backend.clone(), executor, keystore);
Ok(client::Client::new(backend, executor, genesis_storage, execution_strategies)?)
Ok((
client::Client::new(backend.clone(), executor, genesis_storage, execution_strategies)?,
backend,
))
}

pub(crate) mod columns {
Expand Down Expand Up @@ -871,7 +881,9 @@ impl<Block: BlockT<Hash=H256>> Backend<Block> {
// cannot find tree route with empty DB.
if meta.best_hash != Default::default() {
let tree_route = ::client::blockchain::tree_route(
&self.blockchain,
|id| self.blockchain.header(id)?.ok_or_else(
|| client::error::Error::UnknownBlock(format!("{:?}", id))
),
BlockId::Hash(meta.best_hash),
BlockId::Hash(route_to),
)?;
Expand Down Expand Up @@ -2018,6 +2030,7 @@ mod tests {
#[test]
fn tree_route_works() {
let backend = Backend::<Block>::new_test(1000, 100);
let blockchain = backend.blockchain();
let block0 = insert_header(&backend, 0, Default::default(), Vec::new(), Default::default());

// fork from genesis: 3 prong.
Expand All @@ -2031,7 +2044,7 @@ mod tests {

{
let tree_route = ::client::blockchain::tree_route(
backend.blockchain(),
|id| blockchain.header(id)?.ok_or_else(|| client::error::Error::UnknownBlock(format!("{:?}", id))),
BlockId::Hash(a3),
BlockId::Hash(b2)
).unwrap();
Expand All @@ -2043,7 +2056,7 @@ mod tests {

{
let tree_route = ::client::blockchain::tree_route(
backend.blockchain(),
|id| blockchain.header(id)?.ok_or_else(|| client::error::Error::UnknownBlock(format!("{:?}", id))),
BlockId::Hash(a1),
BlockId::Hash(a3),
).unwrap();
Expand All @@ -2055,7 +2068,7 @@ mod tests {

{
let tree_route = ::client::blockchain::tree_route(
backend.blockchain(),
|id| blockchain.header(id)?.ok_or_else(|| client::error::Error::UnknownBlock(format!("{:?}", id))),
BlockId::Hash(a3),
BlockId::Hash(a1),
).unwrap();
Expand All @@ -2067,7 +2080,7 @@ mod tests {

{
let tree_route = ::client::blockchain::tree_route(
backend.blockchain(),
|id| blockchain.header(id)?.ok_or_else(|| client::error::Error::UnknownBlock(format!("{:?}", id))),
BlockId::Hash(a2),
BlockId::Hash(a2),
).unwrap();
Expand All @@ -2081,13 +2094,14 @@ mod tests {
#[test]
fn tree_route_child() {
let backend = Backend::<Block>::new_test(1000, 100);
let blockchain = backend.blockchain();

let block0 = insert_header(&backend, 0, Default::default(), Vec::new(), Default::default());
let block1 = insert_header(&backend, 1, block0, Vec::new(), Default::default());

{
let tree_route = ::client::blockchain::tree_route(
backend.blockchain(),
|id| blockchain.header(id)?.ok_or_else(|| client::error::Error::UnknownBlock(format!("{:?}", id))),
BlockId::Hash(block0),
BlockId::Hash(block1),
).unwrap();
Expand Down
10 changes: 5 additions & 5 deletions core/client/db/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl<Block: BlockT> LightStorage<Block> {
let meta = self.meta.read();
if meta.best_hash != Default::default() {
let tree_route = ::client::blockchain::tree_route(
self,
|id| self.header(id)?.ok_or_else(|| client::error::Error::UnknownBlock(format!("{:?}", id))),
BlockId::Hash(meta.best_hash),
BlockId::Hash(route_to),
)?;
Expand Down Expand Up @@ -780,7 +780,7 @@ pub(crate) mod tests {

{
let tree_route = ::client::blockchain::tree_route(
&db,
|id| db.header(id)?.ok_or_else(|| client::error::Error::UnknownBlock(format!("{:?}", id))),
BlockId::Hash(a3),
BlockId::Hash(b2)
).unwrap();
Expand All @@ -792,7 +792,7 @@ pub(crate) mod tests {

{
let tree_route = ::client::blockchain::tree_route(
&db,
|id| db.header(id)?.ok_or_else(|| client::error::Error::UnknownBlock(format!("{:?}", id))),
BlockId::Hash(a1),
BlockId::Hash(a3),
).unwrap();
Expand All @@ -804,7 +804,7 @@ pub(crate) mod tests {

{
let tree_route = ::client::blockchain::tree_route(
&db,
|id| db.header(id)?.ok_or_else(|| client::error::Error::UnknownBlock(format!("{:?}", id))),
BlockId::Hash(a3),
BlockId::Hash(a1),
).unwrap();
Expand All @@ -816,7 +816,7 @@ pub(crate) mod tests {

{
let tree_route = ::client::blockchain::tree_route(
&db,
|id| db.header(id)?.ok_or_else(|| client::error::Error::UnknownBlock(format!("{:?}", id))),
BlockId::Hash(a2),
BlockId::Hash(a2),
).unwrap();
Expand Down
60 changes: 59 additions & 1 deletion core/client/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use sr_primitives::{generic::BlockId, Justification, StorageOverlay, ChildrenSto
use sr_primitives::traits::{Block as BlockT, NumberFor};
use state_machine::backend::Backend as StateBackend;
use state_machine::ChangesTrieStorage as StateChangesTrieStorage;
use consensus::well_known_cache_keys;
use consensus::{well_known_cache_keys, BlockOrigin};
use hash_db::Hasher;
use trie::MemoryDB;
use parking_lot::Mutex;
Expand All @@ -34,6 +34,25 @@ pub type StorageCollection = Vec<(Vec<u8>, Option<Vec<u8>>)>;
/// In memory arrays of storage values for multiple child tries.
pub type ChildStorageCollection = Vec<(Vec<u8>, StorageCollection)>;

/// Import operation wrapper
pub struct ClientImportOperation<
Block: BlockT,
H: Hasher<Out=Block::Hash>,
B: Backend<Block, H>,
> {
pub(crate) op: B::BlockImportOperation,
pub(crate) notify_imported: Option<(
Block::Hash,
BlockOrigin,
Block::Header,
bool,
Option<(
StorageCollection,
ChildStorageCollection,
)>)>,
pub(crate) notify_finalized: Vec<Block::Hash>,
}

/// State of a new block.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NewBlockState {
Expand Down Expand Up @@ -105,6 +124,45 @@ pub trait BlockImportOperation<Block, H> where
fn mark_head(&mut self, id: BlockId<Block>) -> error::Result<()>;
}

/// Finalize Facilities
pub trait Finalizer<Block: BlockT, H: Hasher<Out=Block::Hash>, B: Backend<Block, H>> {
/// Mark all blocks up to given as finalized in operation. If a
/// justification is provided it is stored with the given finalized
/// block (any other finalized blocks are left unjustified).
///
/// If the block being finalized is on a different fork from the current
/// best block the finalized block is set as best, this might be slightly
/// inaccurate (i.e. outdated). Usages that require determining an accurate
/// best block should use `SelectChain` instead of the client.
fn apply_finality(
&self,
operation: &mut ClientImportOperation<Block, H, B>,
id: BlockId<Block>,
justification: Option<Justification>,
notify: bool,
) -> error::Result<()>;


/// Finalize a block. This will implicitly finalize all blocks up to it and
/// fire finality notifications.
///
/// If the block being finalized is on a different fork from the current
/// best block, the finalized block is set as best. This might be slightly
/// inaccurate (i.e. outdated). Usages that require determining an accurate
/// best block should use `SelectChain` instead of the client.
///
/// Pass a flag to indicate whether finality notifications should be propagated.
/// This is usually tied to some synchronization state, where we don't send notifications
/// while performing major synchronization work.
fn finalize_block(
&self,
id: BlockId<Block>,
justification: Option<Justification>,
notify: bool,
) -> error::Result<()>;

}

/// Provides access to an auxiliary database.
pub trait AuxStore {
/// Insert auxiliary data into key-value store. Deletions occur after insertions.
Expand Down
14 changes: 2 additions & 12 deletions core/client/src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,21 +197,11 @@ impl<Block: BlockT> TreeRoute<Block> {
}

/// Compute a tree-route between two blocks. See tree-route docs for more details.
pub fn tree_route<Block: BlockT, Backend: HeaderBackend<Block>>(
backend: &Backend,
pub fn tree_route<Block: BlockT, F: Fn(BlockId<Block>) -> Result<<Block as BlockT>::Header>>(
load_header: F,
from: BlockId<Block>,
to: BlockId<Block>,
) -> Result<TreeRoute<Block>> {
use sr_primitives::traits::Header;

let load_header = |id: BlockId<Block>| {
match backend.header(id) {
Ok(Some(hdr)) => Ok(hdr),
Ok(None) => Err(Error::UnknownBlock(format!("Unknown block {:?}", id))),
Err(e) => Err(e),
}
};

let mut from = load_header(from)?;
let mut to = load_header(to)?;

Expand Down
Loading

0 comments on commit 409f5aa

Please sign in to comment.