referrerpolicy=no-referrer-when-downgrade

relay_substrate_client/client/
mod.rs

1// Copyright 2019-2021 Parity Technologies (UK) Ltd.
2// This file is part of Parity Bridges Common.
3
4// Parity Bridges Common is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity Bridges Common is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity Bridges Common.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Layered Substrate client implementation.
18
19use crate::{Chain, ConnectionParams};
20
21use caching::CachingClient;
22use num_traits::Saturating;
23use rpc::RpcClient;
24use sp_version::RuntimeVersion;
25
26pub mod caching;
27pub mod rpc;
28
29mod rpc_api;
30mod subscription;
31mod traits;
32
33pub use subscription::{StreamDescription, Subscription, SubscriptionBroadcaster};
34pub use traits::Client;
35
36/// Type of RPC client with caching support.
37pub type RpcWithCachingClient<C> = CachingClient<C, RpcClient<C>>;
38
39/// Creates new RPC client with caching support.
40pub async fn rpc_with_caching<C: Chain>(params: ConnectionParams) -> RpcWithCachingClient<C> {
41	let rpc = rpc::RpcClient::<C>::new(params).await;
42	caching::CachingClient::new(rpc).await
43}
44
45/// The difference between best block number and number of its ancestor, that is enough
46/// for us to consider that ancestor an "ancient" block with dropped state.
47///
48/// The relay does not assume that it is connected to the archive node, so it always tries
49/// to use the best available chain state. But sometimes it still may use state of some
50/// old block. If the state of that block is already dropped, relay will see errors when
51/// e.g. it tries to prove something.
52///
53/// By default Substrate-based nodes are storing state for last 256 blocks. We'll use
54/// half of this value.
55pub const ANCIENT_BLOCK_THRESHOLD: u32 = 128;
56
57/// Returns `true` if we think that the state is already discarded for given block.
58pub fn is_ancient_block<N: From<u32> + PartialOrd + Saturating>(block: N, best: N) -> bool {
59	best.saturating_sub(block) >= N::from(ANCIENT_BLOCK_THRESHOLD)
60}
61
62/// Opaque GRANDPA authorities set.
63pub type OpaqueGrandpaAuthoritiesSet = Vec<u8>;
64
65/// A simple runtime version. It only includes the `spec_version` and `transaction_version`.
66#[derive(Copy, Clone, Debug)]
67pub struct SimpleRuntimeVersion {
68	/// Version of the runtime specification.
69	pub spec_version: u32,
70	/// All existing dispatches are fully compatible when this number doesn't change.
71	pub transaction_version: u32,
72}
73
74impl SimpleRuntimeVersion {
75	/// Create a new instance of `SimpleRuntimeVersion` from a `RuntimeVersion`.
76	pub const fn from_runtime_version(runtime_version: &RuntimeVersion) -> Self {
77		Self {
78			spec_version: runtime_version.spec_version,
79			transaction_version: runtime_version.transaction_version,
80		}
81	}
82}
83
84/// Chain runtime version in client
85#[derive(Copy, Clone, Debug)]
86pub enum ChainRuntimeVersion {
87	/// Auto query from chain.
88	Auto,
89	/// Custom runtime version, defined by user.
90	Custom(SimpleRuntimeVersion),
91}