Skip to main content

soil_client/client_api/
mod.rs

1// This file is part of Soil.
2
3// Copyright (C) Soil contributors.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
6
7//! Substrate client interfaces.
8#![warn(missing_docs)]
9
10pub mod backend;
11pub mod call_executor;
12pub mod client;
13pub mod execution_extensions;
14pub mod in_mem;
15pub mod leaves;
16pub mod notifications;
17pub mod proof_provider;
18
19pub use crate::blockchain;
20pub use crate::blockchain::HeaderBackend;
21pub use backend::*;
22pub use call_executor::*;
23pub use client::*;
24pub use notifications::*;
25pub use proof_provider::*;
26
27pub use subsoil::state_machine::{CompactProof, StorageProof};
28pub use subsoil::storage::{ChildInfo, PrefixedStorageKey, StorageData, StorageKey};
29
30/// Usage Information Provider interface
31pub trait UsageProvider<Block: subsoil::runtime::traits::Block> {
32	/// Get usage info about current client.
33	fn usage_info(&self) -> ClientInfo<Block>;
34}
35
36/// Utility methods for the client.
37pub mod utils {
38	use crate::blockchain::{Error, HeaderBackend, HeaderMetadata};
39	use subsoil::runtime::traits::Block as BlockT;
40
41	/// Returns a function for checking block ancestry, the returned function will
42	/// return `true` if the given hash (second parameter) is a descendent of the
43	/// base (first parameter). If the `current` parameter is defined, it should
44	/// represent the current block `hash` and its `parent hash`, if given the
45	/// function that's returned will assume that `hash` isn't part of the local DB
46	/// yet, and all searches in the DB will instead reference the parent.
47	pub fn is_descendent_of<Block: BlockT, T>(
48		client: &T,
49		current: Option<(Block::Hash, Block::Hash)>,
50	) -> impl Fn(&Block::Hash, &Block::Hash) -> Result<bool, Error> + '_
51	where
52		T: HeaderBackend<Block> + HeaderMetadata<Block, Error = Error>,
53	{
54		move |base, hash| {
55			if base == hash {
56				return Ok(false);
57			}
58
59			let mut hash = hash;
60			if let Some((current_hash, current_parent_hash)) = &current {
61				if base == current_hash {
62					return Ok(false);
63				}
64				if hash == current_hash {
65					if base == current_parent_hash {
66						return Ok(true);
67					} else {
68						hash = current_parent_hash;
69					}
70				}
71			}
72
73			let ancestor = crate::blockchain::lowest_common_ancestor(client, *hash, *base)?;
74
75			Ok(ancestor.hash == *base)
76		}
77	}
78}