1// This file is part of Substrate.
23// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
56// 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.
1011// 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.
1516// 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/>.
1819//! Substrate blockchain API.
2021pub mod error;
2223use error::Error;
24use jsonrpsee::proc_macros::rpc;
25use sp_rpc::{list::ListOrValue, number::NumberOrHex};
2627#[rpc(client, server)]
28pub trait ChainApi<Number, Hash, Header, SignedBlock> {
29/// Get header.
30#[method(name = "chain_getHeader", blocking)]
31fn header(&self, hash: Option<Hash>) -> Result<Option<Header>, Error>;
3233/// Get header and body of a block.
34#[method(name = "chain_getBlock", blocking)]
35fn block(&self, hash: Option<Hash>) -> Result<Option<SignedBlock>, Error>;
3637/// 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)]
41fn block_hash(
42&self,
43 hash: Option<ListOrValue<NumberOrHex>>,
44 ) -> Result<ListOrValue<Option<Hash>>, Error>;
4546/// Get hash of the last finalized block in the canon chain.
47#[method(name = "chain_getFinalizedHead", aliases = ["chain_getFinalisedHead"], blocking)]
48fn finalized_head(&self) -> Result<Hash, Error>;
4950/// All head subscription.
51#[subscription(
52 name = "chain_subscribeAllHeads" => "chain_allHead",
53 unsubscribe = "chain_unsubscribeAllHeads",
54 item = Header
55 )]
56fn subscribe_all_heads(&self);
5758/// 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 )]
66fn subscribe_new_heads(&self);
6768/// 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 )]
76fn subscribe_finalized_heads(&self);
77}