1// This file is part of Substrate.
23// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
56// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
1011// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
1516// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
1819//! Substrate client interfaces.
20#![warn(missing_docs)]
2122pub mod backend;
23pub mod call_executor;
24pub mod client;
25pub mod execution_extensions;
26pub mod in_mem;
27pub mod leaves;
28pub mod notifications;
29pub mod proof_provider;
3031pub use backend::*;
32pub use call_executor::*;
33pub use client::*;
34pub use notifications::*;
35pub use proof_provider::*;
36pub use sp_blockchain as blockchain;
37pub use sp_blockchain::HeaderBackend;
3839pub use sp_state_machine::{CompactProof, StorageProof};
40pub use sp_storage::{ChildInfo, PrefixedStorageKey, StorageData, StorageKey};
4142/// Usage Information Provider interface
43pub trait UsageProvider<Block: sp_runtime::traits::Block> {
44/// Get usage info about current client.
45fn usage_info(&self) -> ClientInfo<Block>;
46}
4748/// Utility methods for the client.
49pub mod utils {
50use sp_blockchain::{Error, HeaderBackend, HeaderMetadata};
51use sp_runtime::traits::Block as BlockT;
5253/// Returns a function for checking block ancestry, the returned function will
54 /// return `true` if the given hash (second parameter) is a descendent of the
55 /// base (first parameter). If the `current` parameter is defined, it should
56 /// represent the current block `hash` and its `parent hash`, if given the
57 /// function that's returned will assume that `hash` isn't part of the local DB
58 /// yet, and all searches in the DB will instead reference the parent.
59pub fn is_descendent_of<Block: BlockT, T>(
60 client: &T,
61 current: Option<(Block::Hash, Block::Hash)>,
62 ) -> impl Fn(&Block::Hash, &Block::Hash) -> Result<bool, Error> + '_
63where
64T: HeaderBackend<Block> + HeaderMetadata<Block, Error = Error>,
65 {
66move |base, hash| {
67if base == hash {
68return Ok(false)
69 }
7071let mut hash = hash;
72if let Some((current_hash, current_parent_hash)) = ¤t {
73if base == current_hash {
74return Ok(false)
75 }
76if hash == current_hash {
77if base == current_parent_hash {
78return Ok(true)
79 } else {
80 hash = current_parent_hash;
81 }
82 }
83 }
8485let ancestor = sp_blockchain::lowest_common_ancestor(client, *hash, *base)?;
8687Ok(ancestor.hash == *base)
88 }
89 }
90}