1mod chain_full;
22
23#[cfg(test)]
24mod tests;
25
26use std::sync::Arc;
27
28use crate::SubscriptionTaskExecutor;
29
30use jsonrpsee::{core::async_trait, PendingSubscriptionSink};
31use sc_client_api::BlockchainEvents;
32use sp_rpc::{list::ListOrValue, number::NumberOrHex};
33use sp_runtime::{
34 generic::SignedBlock,
35 traits::{Block as BlockT, NumberFor},
36};
37
38use self::error::Error;
39
40use sc_client_api::BlockBackend;
41pub use sc_rpc_api::chain::*;
42use sp_blockchain::HeaderBackend;
43
44#[async_trait]
46trait ChainBackend<Client, Block: BlockT>: Send + Sync + 'static
47where
48 Block: BlockT + 'static,
49 Block::Header: Unpin,
50 Client: HeaderBackend<Block> + BlockchainEvents<Block> + 'static,
51{
52 fn client(&self) -> &Arc<Client>;
54
55 fn unwrap_or_best(&self, hash: Option<Block::Hash>) -> Block::Hash {
57 match hash {
58 None => self.client().info().best_hash,
59 Some(hash) => hash,
60 }
61 }
62
63 fn header(&self, hash: Option<Block::Hash>) -> Result<Option<Block::Header>, Error>;
65
66 fn block(&self, hash: Option<Block::Hash>) -> Result<Option<SignedBlock<Block>>, Error>;
68
69 fn block_hash(&self, number: Option<NumberOrHex>) -> Result<Option<Block::Hash>, Error> {
73 match number {
74 None => Ok(Some(self.client().info().best_hash)),
75 Some(num_or_hex) => {
76 let block_num: u32 = num_or_hex.try_into().map_err(|_| {
78 Error::Other(format!(
79 "`{:?}` > u32::MAX, the max block number is u32.",
80 num_or_hex
81 ))
82 })?;
83 let block_num = <NumberFor<Block>>::from(block_num);
84 self.client().hash(block_num).map_err(client_err)
85 },
86 }
87 }
88
89 fn finalized_head(&self) -> Result<Block::Hash, Error> {
91 Ok(self.client().info().finalized_hash)
92 }
93
94 fn subscribe_all_heads(&self, pending: PendingSubscriptionSink);
96
97 fn subscribe_new_heads(&self, pending: PendingSubscriptionSink);
99
100 fn subscribe_finalized_heads(&self, pending: PendingSubscriptionSink);
102}
103
104pub fn new_full<Block: BlockT, Client>(
106 client: Arc<Client>,
107 executor: SubscriptionTaskExecutor,
108) -> Chain<Block, Client>
109where
110 Block: BlockT + 'static,
111 Block::Header: Unpin,
112 Client: BlockBackend<Block> + HeaderBackend<Block> + BlockchainEvents<Block> + 'static,
113{
114 Chain { backend: Box::new(self::chain_full::FullChain::new(client, executor)) }
115}
116
117pub struct Chain<Block: BlockT, Client> {
119 backend: Box<dyn ChainBackend<Client, Block>>,
120}
121
122#[async_trait]
123impl<Block, Client> ChainApiServer<NumberFor<Block>, Block::Hash, Block::Header, SignedBlock<Block>>
124 for Chain<Block, Client>
125where
126 Block: BlockT + 'static,
127 Block::Header: Unpin,
128 Client: HeaderBackend<Block> + BlockchainEvents<Block> + 'static,
129{
130 fn header(&self, hash: Option<Block::Hash>) -> Result<Option<Block::Header>, Error> {
131 self.backend.header(hash)
132 }
133
134 fn block(&self, hash: Option<Block::Hash>) -> Result<Option<SignedBlock<Block>>, Error> {
135 self.backend.block(hash)
136 }
137
138 fn block_hash(
139 &self,
140 number: Option<ListOrValue<NumberOrHex>>,
141 ) -> Result<ListOrValue<Option<Block::Hash>>, Error> {
142 match number {
143 None => self.backend.block_hash(None).map(ListOrValue::Value),
144 Some(ListOrValue::Value(number)) => self
145 .backend
146 .block_hash(Some(number))
147 .map(ListOrValue::Value)
148 .map_err(Into::into),
149 Some(ListOrValue::List(list)) => Ok(ListOrValue::List(
150 list.into_iter()
151 .map(|number| self.backend.block_hash(Some(number)))
152 .collect::<Result<_, _>>()?,
153 )),
154 }
155 }
156
157 fn finalized_head(&self) -> Result<Block::Hash, Error> {
158 self.backend.finalized_head()
159 }
160
161 fn subscribe_all_heads(&self, pending: PendingSubscriptionSink) {
162 self.backend.subscribe_all_heads(pending);
163 }
164
165 fn subscribe_new_heads(&self, pending: PendingSubscriptionSink) {
166 self.backend.subscribe_new_heads(pending)
167 }
168
169 fn subscribe_finalized_heads(&self, pending: PendingSubscriptionSink) {
170 self.backend.subscribe_finalized_heads(pending)
171 }
172}
173
174fn client_err(err: sp_blockchain::Error) -> Error {
175 Error::Client(Box::new(err))
176}