1// Copyright 2019-2021 Parity Technologies (UK) Ltd.
2// This file is part of Parity Bridges Common.
34// 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.
89// 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.
1314// 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/>.
1617//! The most generic Substrate node RPC interface.
1819use crate::{Chain, ChainWithGrandpa, TransactionStatusOf};
2021use 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;
3031/// 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")]
36async fn health(&self) -> RpcResult<Health>;
37/// Return system properties.
38#[method(name = "properties")]
39async fn properties(&self) -> RpcResult<sc_chain_spec::Properties>;
40}
4142/// 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")]
47async fn block_hash(&self, block_number: Option<C::BlockNumber>) -> RpcResult<C::Hash>;
48/// Return block header by its hash.
49#[method(name = "getHeader")]
50async fn header(&self, block_hash: Option<C::Hash>) -> RpcResult<C::Header>;
51/// Return best finalized block hash.
52#[method(name = "getFinalizedHead")]
53async fn finalized_head(&self) -> RpcResult<C::Hash>;
54/// Return signed block (with justifications) by its hash.
55#[method(name = "getBlock")]
56async 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 )]
63async fn subscribe_new_heads(&self);
64/// Subscribe to finalized headers.
65#[subscription(
66 name = "subscribeFinalizedHeads" => "finalizedHead",
67 unsubscribe = "unsubscribeFinalizedHeads",
68 item = C::Header
69 )]
70async fn subscribe_finalized_heads(&self);
71}
7273/// 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")]
78async fn submit_extrinsic(&self, extrinsic: Bytes) -> RpcResult<C::Hash>;
79/// Return vector of pending extrinsics from the transaction pool.
80#[method(name = "pendingExtrinsics")]
81async fn pending_extrinsics(&self) -> RpcResult<Vec<Bytes>>;
82/// Submit and watch for extrinsic state.
83#[subscription(name = "submitAndWatchExtrinsic", unsubscribe = "unwatchExtrinsic", item = TransactionStatusOf<C>)]
84async fn submit_and_watch_extrinsic(&self, extrinsic: Bytes);
85}
8687/// 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")]
92async fn runtime_version(&self) -> RpcResult<RuntimeVersion>;
93/// Call given runtime method.
94#[method(name = "call")]
95async 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")]
103async 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")]
110async fn prove_storage(
111&self,
112 keys: Vec<StorageKey>,
113 hash: Option<C::Hash>,
114 ) -> RpcResult<ReadProof<C::Hash>>;
115}
116117/// 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)]
122async fn subscribe_justifications(&self);
123}
124125// 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)]
131async fn subscribe_justifications(&self);
132}
133134/// 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")]
139async fn account_next_index(&self, account_id: C::AccountId) -> RpcResult<C::Nonce>;
140}
141142/// 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")]
147async fn fee_details(
148&self,
149 extrinsic: Bytes,
150 at_block: Option<C::Hash>,
151 ) -> RpcResult<FeeDetails<NumberOrHex>>;
152}