referrerpolicy=no-referrer-when-downgrade
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
// This file is part of Parity Bridges Common.

// Parity Bridges Common is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity Bridges Common is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common.  If not, see <http://www.gnu.org/licenses/>.

//! The most generic Substrate node RPC interface.

use crate::{Chain, ChainWithGrandpa, TransactionStatusOf};

use jsonrpsee::proc_macros::rpc;
use pallet_transaction_payment_rpc_runtime_api::FeeDetails;
use sc_rpc_api::{state::ReadProof, system::Health};
use sp_core::{
	storage::{StorageData, StorageKey},
	Bytes,
};
use sp_rpc::number::NumberOrHex;
use sp_version::RuntimeVersion;

/// RPC methods of Substrate `system` namespace, that we are using.
#[rpc(client, client_bounds(C: Chain), namespace = "system")]
pub(crate) trait SubstrateSystem<C> {
	/// Return node health.
	#[method(name = "health")]
	async fn health(&self) -> RpcResult<Health>;
	/// Return system properties.
	#[method(name = "properties")]
	async fn properties(&self) -> RpcResult<sc_chain_spec::Properties>;
}

/// RPC methods of Substrate `chain` namespace, that we are using.
#[rpc(client, client_bounds(C: Chain), namespace = "chain")]
pub(crate) trait SubstrateChain<C> {
	/// Get block hash by its number.
	#[method(name = "getBlockHash")]
	async fn block_hash(&self, block_number: Option<C::BlockNumber>) -> RpcResult<C::Hash>;
	/// Return block header by its hash.
	#[method(name = "getHeader")]
	async fn header(&self, block_hash: Option<C::Hash>) -> RpcResult<C::Header>;
	/// Return best finalized block hash.
	#[method(name = "getFinalizedHead")]
	async fn finalized_head(&self) -> RpcResult<C::Hash>;
	/// Return signed block (with justifications) by its hash.
	#[method(name = "getBlock")]
	async fn block(&self, block_hash: Option<C::Hash>) -> RpcResult<C::SignedBlock>;
	/// Subscribe to best headers.
	#[subscription(
		name = "subscribeNewHeads" => "newHead",
		unsubscribe = "unsubscribeNewHeads",
		item = C::Header
	)]
	async fn subscribe_new_heads(&self);
	/// Subscribe to finalized headers.
	#[subscription(
		name = "subscribeFinalizedHeads" => "finalizedHead",
		unsubscribe = "unsubscribeFinalizedHeads",
		item = C::Header
	)]
	async fn subscribe_finalized_heads(&self);
}

/// RPC methods of Substrate `author` namespace, that we are using.
#[rpc(client, client_bounds(C: Chain), namespace = "author")]
pub(crate) trait SubstrateAuthor<C> {
	/// Submit extrinsic to the transaction pool.
	#[method(name = "submitExtrinsic")]
	async fn submit_extrinsic(&self, extrinsic: Bytes) -> RpcResult<C::Hash>;
	/// Return vector of pending extrinsics from the transaction pool.
	#[method(name = "pendingExtrinsics")]
	async fn pending_extrinsics(&self) -> RpcResult<Vec<Bytes>>;
	/// Submit and watch for extrinsic state.
	#[subscription(name = "submitAndWatchExtrinsic", unsubscribe = "unwatchExtrinsic", item = TransactionStatusOf<C>)]
	async fn submit_and_watch_extrinsic(&self, extrinsic: Bytes);
}

/// RPC methods of Substrate `state` namespace, that we are using.
#[rpc(client, client_bounds(C: Chain), namespace = "state")]
pub(crate) trait SubstrateState<C> {
	/// Get current runtime version.
	#[method(name = "getRuntimeVersion")]
	async fn runtime_version(&self) -> RpcResult<RuntimeVersion>;
	/// Call given runtime method.
	#[method(name = "call")]
	async fn call(
		&self,
		method: String,
		data: Bytes,
		at_block: Option<C::Hash>,
	) -> RpcResult<Bytes>;
	/// Get value of the runtime storage.
	#[method(name = "getStorage")]
	async fn storage(
		&self,
		key: StorageKey,
		at_block: Option<C::Hash>,
	) -> RpcResult<Option<StorageData>>;
	/// Get proof of the runtime storage value.
	#[method(name = "getReadProof")]
	async fn prove_storage(
		&self,
		keys: Vec<StorageKey>,
		hash: Option<C::Hash>,
	) -> RpcResult<ReadProof<C::Hash>>;
}

/// RPC methods of Substrate `grandpa` namespace, that we are using.
#[rpc(client, client_bounds(C: ChainWithGrandpa), namespace = "grandpa")]
pub(crate) trait SubstrateGrandpa<C> {
	/// Subscribe to GRANDPA justifications.
	#[subscription(name = "subscribeJustifications", unsubscribe = "unsubscribeJustifications", item = Bytes)]
	async fn subscribe_justifications(&self);
}

// TODO: Use `ChainWithBeefy` instead of `Chain` after #1606 is merged
/// RPC methods of Substrate `beefy` namespace, that we are using.
#[rpc(client, client_bounds(C: Chain), namespace = "beefy")]
pub(crate) trait SubstrateBeefy<C> {
	/// Subscribe to BEEFY justifications.
	#[subscription(name = "subscribeJustifications", unsubscribe = "unsubscribeJustifications", item = Bytes)]
	async fn subscribe_justifications(&self);
}

/// RPC methods of Substrate `system` frame pallet, that we are using.
#[rpc(client, client_bounds(C: Chain), namespace = "system")]
pub(crate) trait SubstrateFrameSystem<C> {
	/// Return index of next account transaction.
	#[method(name = "accountNextIndex")]
	async fn account_next_index(&self, account_id: C::AccountId) -> RpcResult<C::Nonce>;
}

/// RPC methods of Substrate `pallet_transaction_payment` frame pallet, that we are using.
#[rpc(client, client_bounds(C: Chain), namespace = "payment")]
pub(crate) trait SubstrateTransactionPayment<C> {
	/// Query transaction fee details.
	#[method(name = "queryFeeDetails")]
	async fn fee_details(
		&self,
		extrinsic: Bytes,
		at_block: Option<C::Hash>,
	) -> RpcResult<FeeDetails<NumberOrHex>>;
}