sc_rpc_api/chain/
mod.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 blockchain API.
20
21pub mod error;
22
23use error::Error;
24use jsonrpsee::proc_macros::rpc;
25use sp_rpc::{list::ListOrValue, number::NumberOrHex};
26
27#[rpc(client, server)]
28pub trait ChainApi<Number, Hash, Header, SignedBlock> {
29	/// Get header.
30	#[method(name = "chain_getHeader", blocking)]
31	fn header(&self, hash: Option<Hash>) -> Result<Option<Header>, Error>;
32
33	/// Get header and body of a block.
34	#[method(name = "chain_getBlock", blocking)]
35	fn block(&self, hash: Option<Hash>) -> Result<Option<SignedBlock>, Error>;
36
37	/// Get hash of the n-th block in the canon chain.
38	///
39	/// By default returns latest block hash.
40	#[method(name = "chain_getBlockHash", aliases = ["chain_getHead"], blocking)]
41	fn block_hash(
42		&self,
43		hash: Option<ListOrValue<NumberOrHex>>,
44	) -> Result<ListOrValue<Option<Hash>>, Error>;
45
46	/// Get hash of the last finalized block in the canon chain.
47	#[method(name = "chain_getFinalizedHead", aliases = ["chain_getFinalisedHead"], blocking)]
48	fn finalized_head(&self) -> Result<Hash, Error>;
49
50	/// All head subscription.
51	#[subscription(
52		name = "chain_subscribeAllHeads" => "chain_allHead",
53		unsubscribe = "chain_unsubscribeAllHeads",
54		item = Header
55	)]
56	fn subscribe_all_heads(&self);
57
58	/// New head subscription.
59	#[subscription(
60		name = "chain_subscribeNewHeads" => "chain_newHead",
61		aliases = ["subscribe_newHead", "chain_subscribeNewHead"],
62		unsubscribe = "chain_unsubscribeNewHeads",
63		unsubscribe_aliases = ["unsubscribe_newHead", "chain_unsubscribeNewHead"],
64		item = Header
65	)]
66	fn subscribe_new_heads(&self);
67
68	/// Finalized head subscription.
69	#[subscription(
70		name = "chain_subscribeFinalizedHeads" => "chain_finalizedHead",
71		aliases = ["chain_subscribeFinalisedHeads"],
72		unsubscribe = "chain_unsubscribeFinalizedHeads",
73		unsubscribe_aliases = ["chain_unsubscribeFinalisedHeads"],
74		item = Header
75	)]
76	fn subscribe_finalized_heads(&self);
77}