mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-08 00:48:01 +00:00
8c7d217091
* chore/error: remove from str conversion and add deprecation notifications * fixup changes * fix test looking for gone ::Msg variant * another test fix * one is duplicate, the other is not, so duplicates reported are n-1 * darn spaces Co-authored-by: Andronik Ordian <write@reusable.software> * remove pointless doc comments of error variants without any value * low hanging fruits (for a tall person) * moar error type variants * avoid the storage modules for now They are in need of a refactor, and the pain is rather large removing all String error and DefaultError occurences. * chore remove pointless error generic * fix test for mocks, add a bunch of non_exhaustive * max line width * test fixes due to error changes * fin * error outputs... again * undo stderr adjustments * Update client/consensus/slots/src/lib.rs Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * remove closure clutter Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * more error types * introduce ApiError * extract Mock error * ApiError refactor * even more error types * the last for now * chore unused deps * another extraction * reduce should panic, due to extended error messages * error test happiness * shift error lines by one * doc tests * white space Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * Into -> From Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * remove pointless codec Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * avoid pointless self import Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> Co-authored-by: Bernhard Schuster <bernhard@parity.io> Co-authored-by: Andronik Ordian <write@reusable.software> Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
143 lines
4.5 KiB
Rust
143 lines
4.5 KiB
Rust
// Copyright 2020 Parity Technologies (UK) Ltd.
|
|
// This file is part of Substrate.
|
|
|
|
// Substrate 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.
|
|
|
|
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//! A RPC handler to create sync states for light clients.
|
|
//! Currently only usable with BABE + GRANDPA.
|
|
|
|
#![deny(unused_crate_dependencies)]
|
|
|
|
use sp_runtime::traits::{Block as BlockT, NumberFor};
|
|
use sp_blockchain::HeaderBackend;
|
|
use std::sync::Arc;
|
|
use sp_runtime::generic::BlockId;
|
|
|
|
use jsonrpc_derive::rpc;
|
|
|
|
type SharedAuthoritySet<TBl> =
|
|
sc_finality_grandpa::SharedAuthoritySet<<TBl as BlockT>::Hash, NumberFor<TBl>>;
|
|
type SharedEpochChanges<TBl> = sc_consensus_epochs::SharedEpochChanges<TBl, sc_consensus_babe::Epoch>;
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
#[allow(missing_docs)]
|
|
enum Error<Block: BlockT> {
|
|
#[error(transparent)]
|
|
Blockchain(#[from] sp_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 jsonrpc_core::Error {
|
|
fn from(error: Error<Block>) -> Self {
|
|
let message = match error {
|
|
Error::JsonRpc(s) => s,
|
|
_ => error.to_string(),
|
|
};
|
|
jsonrpc_core::Error {
|
|
message,
|
|
code: jsonrpc_core::ErrorCode::ServerError(1),
|
|
data: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// An api for sync state RPC calls.
|
|
#[rpc]
|
|
pub trait SyncStateRpcApi {
|
|
/// Returns the json-serialized chainspec running the node, with a sync state.
|
|
#[rpc(name = "sync_state_genSyncSpec", returns = "jsonrpc_core::Value")]
|
|
fn system_gen_sync_spec(&self, raw: bool)
|
|
-> jsonrpc_core::Result<jsonrpc_core::Value>;
|
|
}
|
|
|
|
/// The handler for sync state RPC calls.
|
|
pub struct SyncStateRpcHandler<TBl: BlockT, TCl> {
|
|
chain_spec: Box<dyn sc_chain_spec::ChainSpec>,
|
|
client: Arc<TCl>,
|
|
shared_authority_set: SharedAuthoritySet<TBl>,
|
|
shared_epoch_changes: SharedEpochChanges<TBl>,
|
|
deny_unsafe: sc_rpc_api::DenyUnsafe,
|
|
}
|
|
|
|
impl<TBl, TCl> SyncStateRpcHandler<TBl, TCl>
|
|
where
|
|
TBl: BlockT,
|
|
TCl: HeaderBackend<TBl> + sc_client_api::AuxStore + 'static,
|
|
{
|
|
/// Create a new handler.
|
|
pub fn new(
|
|
chain_spec: Box<dyn sc_chain_spec::ChainSpec>,
|
|
client: Arc<TCl>,
|
|
shared_authority_set: SharedAuthoritySet<TBl>,
|
|
shared_epoch_changes: SharedEpochChanges<TBl>,
|
|
deny_unsafe: sc_rpc_api::DenyUnsafe,
|
|
) -> Self {
|
|
Self {
|
|
chain_spec, client, shared_authority_set, shared_epoch_changes, deny_unsafe,
|
|
}
|
|
}
|
|
|
|
fn build_sync_state(&self) -> Result<sc_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(|| 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_else(|| Error::LoadingBlockWeightFailed(finalized_hash))?;
|
|
|
|
Ok(sc_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> + sc_client_api::AuxStore + 'static,
|
|
{
|
|
fn system_gen_sync_spec(&self, raw: bool)
|
|
-> jsonrpc_core::Result<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) -> jsonrpc_core::Error {
|
|
Error::<Block>::JsonRpc(error.to_string()).into()
|
|
}
|