mod chain_full;
#[cfg(test)]
mod tests;
use std::sync::Arc;
use crate::SubscriptionTaskExecutor;
use jsonrpsee::{core::async_trait, PendingSubscriptionSink};
use sc_client_api::BlockchainEvents;
use sp_rpc::{list::ListOrValue, number::NumberOrHex};
use sp_runtime::{
generic::SignedBlock,
traits::{Block as BlockT, NumberFor},
};
use self::error::Error;
use sc_client_api::BlockBackend;
pub use sc_rpc_api::chain::*;
use sp_blockchain::HeaderBackend;
#[async_trait]
trait ChainBackend<Client, Block: BlockT>: Send + Sync + 'static
where
Block: BlockT + 'static,
Block::Header: Unpin,
Client: HeaderBackend<Block> + BlockchainEvents<Block> + 'static,
{
fn client(&self) -> &Arc<Client>;
fn unwrap_or_best(&self, hash: Option<Block::Hash>) -> Block::Hash {
match hash {
None => self.client().info().best_hash,
Some(hash) => hash,
}
}
fn header(&self, hash: Option<Block::Hash>) -> Result<Option<Block::Header>, Error>;
fn block(&self, hash: Option<Block::Hash>) -> Result<Option<SignedBlock<Block>>, Error>;
fn block_hash(&self, number: Option<NumberOrHex>) -> Result<Option<Block::Hash>, Error> {
match number {
None => Ok(Some(self.client().info().best_hash)),
Some(num_or_hex) => {
let block_num: u32 = num_or_hex.try_into().map_err(|_| {
Error::Other(format!(
"`{:?}` > u32::MAX, the max block number is u32.",
num_or_hex
))
})?;
let block_num = <NumberFor<Block>>::from(block_num);
self.client().hash(block_num).map_err(client_err)
},
}
}
fn finalized_head(&self) -> Result<Block::Hash, Error> {
Ok(self.client().info().finalized_hash)
}
fn subscribe_all_heads(&self, pending: PendingSubscriptionSink);
fn subscribe_new_heads(&self, pending: PendingSubscriptionSink);
fn subscribe_finalized_heads(&self, pending: PendingSubscriptionSink);
}
pub fn new_full<Block: BlockT, Client>(
client: Arc<Client>,
executor: SubscriptionTaskExecutor,
) -> Chain<Block, Client>
where
Block: BlockT + 'static,
Block::Header: Unpin,
Client: BlockBackend<Block> + HeaderBackend<Block> + BlockchainEvents<Block> + 'static,
{
Chain { backend: Box::new(self::chain_full::FullChain::new(client, executor)) }
}
pub struct Chain<Block: BlockT, Client> {
backend: Box<dyn ChainBackend<Client, Block>>,
}
#[async_trait]
impl<Block, Client> ChainApiServer<NumberFor<Block>, Block::Hash, Block::Header, SignedBlock<Block>>
for Chain<Block, Client>
where
Block: BlockT + 'static,
Block::Header: Unpin,
Client: HeaderBackend<Block> + BlockchainEvents<Block> + 'static,
{
fn header(&self, hash: Option<Block::Hash>) -> Result<Option<Block::Header>, Error> {
self.backend.header(hash)
}
fn block(&self, hash: Option<Block::Hash>) -> Result<Option<SignedBlock<Block>>, Error> {
self.backend.block(hash)
}
fn block_hash(
&self,
number: Option<ListOrValue<NumberOrHex>>,
) -> Result<ListOrValue<Option<Block::Hash>>, Error> {
match number {
None => self.backend.block_hash(None).map(ListOrValue::Value),
Some(ListOrValue::Value(number)) => self
.backend
.block_hash(Some(number))
.map(ListOrValue::Value)
.map_err(Into::into),
Some(ListOrValue::List(list)) => Ok(ListOrValue::List(
list.into_iter()
.map(|number| self.backend.block_hash(Some(number)))
.collect::<Result<_, _>>()?,
)),
}
}
fn finalized_head(&self) -> Result<Block::Hash, Error> {
self.backend.finalized_head()
}
fn subscribe_all_heads(&self, pending: PendingSubscriptionSink) {
self.backend.subscribe_all_heads(pending);
}
fn subscribe_new_heads(&self, pending: PendingSubscriptionSink) {
self.backend.subscribe_new_heads(pending)
}
fn subscribe_finalized_heads(&self, pending: PendingSubscriptionSink) {
self.backend.subscribe_finalized_heads(pending)
}
}
fn client_err(err: sp_blockchain::Error) -> Error {
Error::Client(Box::new(err))
}