#![deny(unused_crate_dependencies)]
use tp_runtime::traits::{Block as BlockT, NumberFor};
use tp_blockchain::HeaderBackend;
use std::sync::Arc;
use tp_runtime::generic::BlockId;
use tetsy_jsonrpc_derive::rpc;
type SharedAuthoritySet<TBl> =
tc_finality_grandpa::SharedAuthoritySet<<TBl as BlockT>::Hash, NumberFor<TBl>>;
type SharedEpochChanges<TBl> = tc_consensus_epochs::SharedEpochChanges<TBl, tc_consensus_babe::Epoch>;
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
enum Error<Block: BlockT> {
#[error(transparent)]
Blockchain(#[from] tp_blockchain::Error),
#[error("Failed to load the block weight for block {0:?}")]
LoadingBlockWeightFailed(<Block as BlockT>::Hash),
#[error("JsonRpc error: {0}")]
JsonRpc(String),
}
impl<Block: BlockT> From<Error<Block>> for tetsy_jsonrpc_core::Error {
fn from(error: Error<Block>) -> Self {
let message = match error {
Error::JsonRpc(s) => s,
_ => error.to_string(),
};
tetsy_jsonrpc_core::Error {
message,
code: tetsy_jsonrpc_core::ErrorCode::ServerError(1),
data: None,
}
}
}
#[rpc]
pub trait SyncStateRpcApi {
#[rpc(name = "sync_state_genSyncSpec", returns = "tetsy_jsonrpc_core::Value")]
fn system_gen_sync_spec(&self, raw: bool)
-> tetsy_jsonrpc_core::Result<tetsy_jsonrpc_core::Value>;
}
pub struct SyncStateRpcHandler<TBl: BlockT, TCl> {
chain_spec: Box<dyn tc_chain_spec::ChainSpec>,
client: Arc<TCl>,
shared_authority_set: SharedAuthoritySet<TBl>,
shared_epoch_changes: SharedEpochChanges<TBl>,
deny_unsafe: tc_rpc_api::DenyUnsafe,
}
impl<TBl, TCl> SyncStateRpcHandler<TBl, TCl>
where
TBl: BlockT,
TCl: HeaderBackend<TBl> + tc_client_api::AuxStore + 'static,
{
pub fn new(
chain_spec: Box<dyn tc_chain_spec::ChainSpec>,
client: Arc<TCl>,
shared_authority_set: SharedAuthoritySet<TBl>,
shared_epoch_changes: SharedEpochChanges<TBl>,
deny_unsafe: tc_rpc_api::DenyUnsafe,
) -> Self {
Self {
chain_spec, client, shared_authority_set, shared_epoch_changes, deny_unsafe,
}
}
fn build_sync_state(&self) -> Result<tc_chain_spec::LightSyncState<TBl>, Error<TBl>> {
let finalized_hash = self.client.info().finalized_hash;
let finalized_header = self.client.header(BlockId::Hash(finalized_hash))?
.ok_or_else(|| tp_blockchain::Error::MissingHeader(finalized_hash.to_string()))?;
let finalized_block_weight = tc_consensus_babe::aux_schema::load_block_weight(
&*self.client,
finalized_hash,
)?
.ok_or_else(|| Error::LoadingBlockWeightFailed(finalized_hash))?;
Ok(tc_chain_spec::LightSyncState {
finalized_block_header: finalized_header,
babe_epoch_changes: self.shared_epoch_changes.lock().clone(),
babe_finalized_block_weight: finalized_block_weight,
grandpa_authority_set: self.shared_authority_set.clone_inner(),
})
}
}
impl<TBl, TCl> SyncStateRpcApi for SyncStateRpcHandler<TBl, TCl>
where
TBl: BlockT,
TCl: HeaderBackend<TBl> + tc_client_api::AuxStore + 'static,
{
fn system_gen_sync_spec(&self, raw: bool)
-> tetsy_jsonrpc_core::Result<tetsy_jsonrpc_core::Value>
{
if let Err(err) = self.deny_unsafe.check_if_safe() {
return Err(err.into());
}
let mut chain_spec = self.chain_spec.cloned_box();
let sync_state = self.build_sync_state()
.map_err(map_error::<TBl,Error<TBl>>)?;
chain_spec.set_light_sync_state(sync_state.to_serializable());
let string = chain_spec.as_json(raw).map_err(map_error::<TBl,_>)?;
serde_json::from_str(&string).map_err(|err| map_error::<TBl,_>(err))
}
}
fn map_error<Block: BlockT, S: ToString>(error: S) -> tetsy_jsonrpc_core::Error {
Error::<Block>::JsonRpc(error.to_string()).into()
}