#![deny(unused_crate_dependencies)]
use std::sync::Arc;
use jsonrpsee::{
core::{async_trait, Error as JsonRpseeError, RpcResult},
proc_macros::rpc,
types::{error::CallError, ErrorObject},
};
use sc_client_api::StorageData;
use sc_consensus_babe::{BabeWorkerHandle, Error as BabeError};
use sp_blockchain::HeaderBackend;
use sp_runtime::traits::{Block as BlockT, NumberFor};
type SharedAuthoritySet<TBl> =
sc_consensus_grandpa::SharedAuthoritySet<<TBl as BlockT>::Hash, NumberFor<TBl>>;
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error<Block: BlockT> {
#[error(transparent)]
Blockchain(#[from] sp_blockchain::Error),
#[error("Failed to load the block weight for block {0:?}")]
LoadingBlockWeightFailed(Block::Hash),
#[error("Failed to load the BABE epoch data: {0}")]
LoadingEpochDataFailed(BabeError<Block>),
#[error("JsonRpc error: {0}")]
JsonRpc(String),
#[error(
"The light sync state extension is not provided by the chain spec. \
Read the `sc-sync-state-rpc` crate docs on how to do this!"
)]
LightSyncStateExtensionNotFound,
}
impl<Block: BlockT> From<Error<Block>> for JsonRpseeError {
fn from(error: Error<Block>) -> Self {
let message = match error {
Error::JsonRpc(s) => s,
_ => error.to_string(),
};
CallError::Custom(ErrorObject::owned(1, message, None::<()>)).into()
}
}
fn serialize_encoded<S: serde::Serializer, T: codec::Encode>(
val: &T,
s: S,
) -> Result<S::Ok, S::Error> {
let encoded = StorageData(val.encode());
serde::Serialize::serialize(&encoded, s)
}
pub type LightSyncStateExtension = Option<serde_json::Value>;
#[derive(serde::Serialize, Clone)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct LightSyncState<Block: BlockT> {
#[serde(serialize_with = "serialize_encoded")]
pub finalized_block_header: <Block as BlockT>::Header,
#[serde(serialize_with = "serialize_encoded")]
pub babe_epoch_changes: sc_consensus_epochs::EpochChangesFor<Block, sc_consensus_babe::Epoch>,
pub babe_finalized_block_weight: sc_consensus_babe::BabeBlockWeight,
#[serde(serialize_with = "serialize_encoded")]
pub grandpa_authority_set:
sc_consensus_grandpa::AuthoritySet<<Block as BlockT>::Hash, NumberFor<Block>>,
}
#[rpc(client, server)]
pub trait SyncStateApi {
#[method(name = "sync_state_genSyncSpec")]
async fn system_gen_sync_spec(&self, raw: bool) -> RpcResult<serde_json::Value>;
}
pub struct SyncState<Block: BlockT, Client> {
chain_spec: Box<dyn sc_chain_spec::ChainSpec>,
client: Arc<Client>,
shared_authority_set: SharedAuthoritySet<Block>,
babe_worker_handle: BabeWorkerHandle<Block>,
}
impl<Block, Client> SyncState<Block, Client>
where
Block: BlockT,
Client: HeaderBackend<Block> + sc_client_api::AuxStore + 'static,
{
pub fn new(
chain_spec: Box<dyn sc_chain_spec::ChainSpec>,
client: Arc<Client>,
shared_authority_set: SharedAuthoritySet<Block>,
babe_worker_handle: BabeWorkerHandle<Block>,
) -> Result<Self, Error<Block>> {
if sc_chain_spec::get_extension::<LightSyncStateExtension>(chain_spec.extensions())
.is_some()
{
Ok(Self { chain_spec, client, shared_authority_set, babe_worker_handle })
} else {
Err(Error::<Block>::LightSyncStateExtensionNotFound)
}
}
async fn build_sync_state(&self) -> Result<LightSyncState<Block>, Error<Block>> {
let epoch_changes = self
.babe_worker_handle
.epoch_data()
.await
.map_err(Error::LoadingEpochDataFailed)?;
let finalized_hash = self.client.info().finalized_hash;
let finalized_header = self
.client
.header(finalized_hash)?
.ok_or_else(|| sp_blockchain::Error::MissingHeader(finalized_hash.to_string()))?;
let finalized_block_weight =
sc_consensus_babe::aux_schema::load_block_weight(&*self.client, finalized_hash)?
.ok_or(Error::LoadingBlockWeightFailed(finalized_hash))?;
Ok(LightSyncState {
finalized_block_header: finalized_header,
babe_epoch_changes: epoch_changes,
babe_finalized_block_weight: finalized_block_weight,
grandpa_authority_set: self.shared_authority_set.clone_inner(),
})
}
}
#[async_trait]
impl<Block, Backend> SyncStateApiServer for SyncState<Block, Backend>
where
Block: BlockT,
Backend: HeaderBackend<Block> + sc_client_api::AuxStore + 'static,
{
async fn system_gen_sync_spec(&self, raw: bool) -> RpcResult<serde_json::Value> {
let current_sync_state = self.build_sync_state().await?;
let mut chain_spec = self.chain_spec.cloned_box();
let extension = sc_chain_spec::get_extension_mut::<LightSyncStateExtension>(
chain_spec.extensions_mut(),
)
.ok_or(Error::<Block>::LightSyncStateExtensionNotFound)?;
let val = serde_json::to_value(¤t_sync_state)?;
*extension = Some(val);
let json_str = chain_spec.as_json(raw).map_err(|e| Error::<Block>::JsonRpc(e))?;
serde_json::from_str(&json_str).map_err(Into::into)
}
}