referrerpolicy=no-referrer-when-downgrade

sc_client_api/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// 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.
10
11// 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.
15
16// 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/>.
18
19//! Substrate client interfaces.
20#![warn(missing_docs)]
21
22pub 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;
30
31pub 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;
38
39pub use sp_state_machine::{CompactProof, StorageProof};
40pub use sp_storage::{ChildInfo, PrefixedStorageKey, StorageData, StorageKey};
41
42/// Usage Information Provider interface
43pub trait UsageProvider<Block: sp_runtime::traits::Block> {
44	/// Get usage info about current client.
45	fn usage_info(&self) -> ClientInfo<Block>;
46}
47
48/// Utility methods for the client.
49pub mod utils {
50	use sp_blockchain::{Error, HeaderBackend, HeaderMetadata};
51	use sp_runtime::traits::Block as BlockT;
52
53	/// 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.
59	pub 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> + '_
63	where
64		T: HeaderBackend<Block> + HeaderMetadata<Block, Error = Error>,
65	{
66		move |base, hash| {
67			if base == hash {
68				return Ok(false)
69			}
70
71			let mut hash = hash;
72			if let Some((current_hash, current_parent_hash)) = &current {
73				if base == current_hash {
74					return Ok(false)
75				}
76				if hash == current_hash {
77					if base == current_parent_hash {
78						return Ok(true)
79					} else {
80						hash = current_parent_hash;
81					}
82				}
83			}
84
85			let ancestor = sp_blockchain::lowest_common_ancestor(client, *hash, *base)?;
86
87			Ok(ancestor.hash == *base)
88		}
89	}
90}