referrerpolicy=no-referrer-when-downgrade

relay_substrate_client/client/
rpc_api.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//! The most generic Substrate node RPC interface.
18
19use crate::{Chain, ChainWithGrandpa, TransactionStatusOf};
20
21use jsonrpsee::proc_macros::rpc;
22use pallet_transaction_payment_rpc_runtime_api::FeeDetails;
23use sc_rpc_api::{state::ReadProof, system::Health};
24use sp_core::{
25	storage::{StorageData, StorageKey},
26	Bytes,
27};
28use sp_rpc::number::NumberOrHex;
29use sp_version::RuntimeVersion;
30
31/// RPC methods of Substrate `system` namespace, that we are using.
32#[rpc(client, client_bounds(C: Chain), namespace = "system")]
33pub(crate) trait SubstrateSystem<C> {
34	/// Return node health.
35	#[method(name = "health")]
36	async fn health(&self) -> RpcResult<Health>;
37	/// Return system properties.
38	#[method(name = "properties")]
39	async fn properties(&self) -> RpcResult<sc_chain_spec::Properties>;
40}
41
42/// RPC methods of Substrate `chain` namespace, that we are using.
43#[rpc(client, client_bounds(C: Chain), namespace = "chain")]
44pub(crate) trait SubstrateChain<C> {
45	/// Get block hash by its number.
46	#[method(name = "getBlockHash")]
47	async fn block_hash(&self, block_number: Option<C::BlockNumber>) -> RpcResult<C::Hash>;
48	/// Return block header by its hash.
49	#[method(name = "getHeader")]
50	async fn header(&self, block_hash: Option<C::Hash>) -> RpcResult<C::Header>;
51	/// Return best finalized block hash.
52	#[method(name = "getFinalizedHead")]
53	async fn finalized_head(&self) -> RpcResult<C::Hash>;
54	/// Return signed block (with justifications) by its hash.
55	#[method(name = "getBlock")]
56	async fn block(&self, block_hash: Option<C::Hash>) -> RpcResult<C::SignedBlock>;
57	/// Subscribe to best headers.
58	#[subscription(
59		name = "subscribeNewHeads" => "newHead",
60		unsubscribe = "unsubscribeNewHeads",
61		item = C::Header
62	)]
63	async fn subscribe_new_heads(&self);
64	/// Subscribe to finalized headers.
65	#[subscription(
66		name = "subscribeFinalizedHeads" => "finalizedHead",
67		unsubscribe = "unsubscribeFinalizedHeads",
68		item = C::Header
69	)]
70	async fn subscribe_finalized_heads(&self);
71}
72
73/// RPC methods of Substrate `author` namespace, that we are using.
74#[rpc(client, client_bounds(C: Chain), namespace = "author")]
75pub(crate) trait SubstrateAuthor<C> {
76	/// Submit extrinsic to the transaction pool.
77	#[method(name = "submitExtrinsic")]
78	async fn submit_extrinsic(&self, extrinsic: Bytes) -> RpcResult<C::Hash>;
79	/// Return vector of pending extrinsics from the transaction pool.
80	#[method(name = "pendingExtrinsics")]
81	async fn pending_extrinsics(&self) -> RpcResult<Vec<Bytes>>;
82	/// Submit and watch for extrinsic state.
83	#[subscription(name = "submitAndWatchExtrinsic", unsubscribe = "unwatchExtrinsic", item = TransactionStatusOf<C>)]
84	async fn submit_and_watch_extrinsic(&self, extrinsic: Bytes);
85}
86
87/// RPC methods of Substrate `state` namespace, that we are using.
88#[rpc(client, client_bounds(C: Chain), namespace = "state")]
89pub(crate) trait SubstrateState<C> {
90	/// Get current runtime version.
91	#[method(name = "getRuntimeVersion")]
92	async fn runtime_version(&self) -> RpcResult<RuntimeVersion>;
93	/// Call given runtime method.
94	#[method(name = "call")]
95	async fn call(
96		&self,
97		method: String,
98		data: Bytes,
99		at_block: Option<C::Hash>,
100	) -> RpcResult<Bytes>;
101	/// Get value of the runtime storage.
102	#[method(name = "getStorage")]
103	async fn storage(
104		&self,
105		key: StorageKey,
106		at_block: Option<C::Hash>,
107	) -> RpcResult<Option<StorageData>>;
108	/// Get proof of the runtime storage value.
109	#[method(name = "getReadProof")]
110	async fn prove_storage(
111		&self,
112		keys: Vec<StorageKey>,
113		hash: Option<C::Hash>,
114	) -> RpcResult<ReadProof<C::Hash>>;
115}
116
117/// RPC methods of Substrate `grandpa` namespace, that we are using.
118#[rpc(client, client_bounds(C: ChainWithGrandpa), namespace = "grandpa")]
119pub(crate) trait SubstrateGrandpa<C> {
120	/// Subscribe to GRANDPA justifications.
121	#[subscription(name = "subscribeJustifications", unsubscribe = "unsubscribeJustifications", item = Bytes)]
122	async fn subscribe_justifications(&self);
123}
124
125// TODO: Use `ChainWithBeefy` instead of `Chain` after #1606 is merged
126/// RPC methods of Substrate `beefy` namespace, that we are using.
127#[rpc(client, client_bounds(C: Chain), namespace = "beefy")]
128pub(crate) trait SubstrateBeefy<C> {
129	/// Subscribe to BEEFY justifications.
130	#[subscription(name = "subscribeJustifications", unsubscribe = "unsubscribeJustifications", item = Bytes)]
131	async fn subscribe_justifications(&self);
132}
133
134/// RPC methods of Substrate `system` frame pallet, that we are using.
135#[rpc(client, client_bounds(C: Chain), namespace = "system")]
136pub(crate) trait SubstrateFrameSystem<C> {
137	/// Return index of next account transaction.
138	#[method(name = "accountNextIndex")]
139	async fn account_next_index(&self, account_id: C::AccountId) -> RpcResult<C::Nonce>;
140}
141
142/// RPC methods of Substrate `pallet_transaction_payment` frame pallet, that we are using.
143#[rpc(client, client_bounds(C: Chain), namespace = "payment")]
144pub(crate) trait SubstrateTransactionPayment<C> {
145	/// Query transaction fee details.
146	#[method(name = "queryFeeDetails")]
147	async fn fee_details(
148		&self,
149		extrinsic: Bytes,
150		at_block: Option<C::Hash>,
151	) -> RpcResult<FeeDetails<NumberOrHex>>;
152}