// This file is part of Bizinikiwi. // Copyright (C) Parity Technologies (UK) Ltd. // SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 // This program 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. // This program 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 this program. If not, see . //! Bizinikiwi blockchain API. mod chain_full; #[cfg(test)] mod tests; use std::sync::Arc; use crate::SubscriptionTaskExecutor; use jsonrpsee::{core::async_trait, PendingSubscriptionSink}; use pezsc_client_api::BlockchainEvents; use pezsp_rpc::{list::ListOrValue, number::NumberOrHex}; use pezsp_runtime::{ generic::SignedBlock, traits::{Block as BlockT, NumberFor}, }; use self::error::Error; use pezsc_client_api::BlockBackend; pub use pezsc_rpc_api::chain::*; use pezsp_blockchain::HeaderBackend; /// Blockchain backend API #[async_trait] trait ChainBackend: Send + Sync + 'static where Block: BlockT + 'static, Block::Header: Unpin, Client: HeaderBackend + BlockchainEvents + 'static, { /// Get client reference. fn client(&self) -> &Arc; /// Tries to unwrap passed block hash, or uses best block hash otherwise. fn unwrap_or_best(&self, hash: Option) -> Block::Hash { match hash { None => self.client().info().best_hash, Some(hash) => hash, } } /// Get header of a block. fn header(&self, hash: Option) -> Result, Error>; /// Get header and body of a block. fn block(&self, hash: Option) -> Result>, Error>; /// Get hash of the n-th block in the canon chain. /// /// By default returns latest block hash. fn block_hash(&self, number: Option) -> Result, Error> { match number { None => Ok(Some(self.client().info().best_hash)), Some(num_or_hex) => { // FIXME <2329>: Database seems to limit the block number to u32 for no reason 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 = >::from(block_num); self.client().hash(block_num).map_err(client_err) }, } } /// Get hash of the last finalized block in the canon chain. fn finalized_head(&self) -> Result { Ok(self.client().info().finalized_hash) } /// All new head subscription fn subscribe_all_heads(&self, pending: PendingSubscriptionSink); /// New best head subscription fn subscribe_new_heads(&self, pending: PendingSubscriptionSink); /// Finalized head subscription fn subscribe_finalized_heads(&self, pending: PendingSubscriptionSink); } /// Create new state API that works on full node. pub fn new_full( client: Arc, executor: SubscriptionTaskExecutor, ) -> Chain where Block: BlockT + 'static, Block::Header: Unpin, Client: BlockBackend + HeaderBackend + BlockchainEvents + 'static, { Chain { backend: Box::new(self::chain_full::FullChain::new(client, executor)) } } /// Chain API with subscriptions support. pub struct Chain { backend: Box>, } #[async_trait] impl ChainApiServer, Block::Hash, Block::Header, SignedBlock> for Chain where Block: BlockT + 'static, Block::Header: Unpin, Client: HeaderBackend + BlockchainEvents + 'static, { fn header(&self, hash: Option) -> Result, Error> { self.backend.header(hash) } fn block(&self, hash: Option) -> Result>, Error> { self.backend.block(hash) } fn block_hash( &self, number: Option>, ) -> Result>, 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::>()?, )), } } fn finalized_head(&self) -> Result { 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: pezsp_blockchain::Error) -> Error { Error::Client(Box::new(err)) }