Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
port sync state rpc
  • Loading branch information
niklasad1 committed Sep 3, 2021
commit 75286edb854ebddf035800fab62f79d53d50dbae
7 changes: 3 additions & 4 deletions bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use pallet_mmr_rpc::{MmrApiServer, MmrRpc};
use pallet_transaction_payment_rpc::{TransactionPaymentApiServer, TransactionPaymentRpc};
use sc_consensus_babe_rpc::{BabeApiServer, BabeRpc};
use sc_finality_grandpa_rpc::{GrandpaApiServer, GrandpaRpc};
use sc_sync_state_rpc::SyncStateRpc;
use sc_sync_state_rpc::{SyncStateRpc, SyncStateRpcApiServer};
use substrate_frame_rpc_system::{SystemApiServer, SystemRpc, SystemRpcBackendFull};

type FullClient =
Expand Down Expand Up @@ -199,8 +199,7 @@ pub fn new_partial(
deny_unsafe,
)
.expect("TODO: error handling")
.into_rpc_module()
.expect("TODO: error handling");
.into_rpc();
let transaction_payment_rpc = TransactionPaymentRpc::new(client2.clone()).into_rpc();
let system_rpc_backend =
SystemRpcBackendFull::new(client2.clone(), transaction_pool2.clone(), deny_unsafe);
Expand Down Expand Up @@ -753,7 +752,7 @@ mod tests {
sc_consensus_babe::authorship::claim_slot(slot.into(), &epoch, &keystore)
.map(|(digest, _)| digest)
{
break (babe_pre_digest, epoch_descriptor);
break (babe_pre_digest, epoch_descriptor)
}

slot += 1;
Expand Down
6 changes: 3 additions & 3 deletions client/consensus/babe/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ where
match claim {
PreDigest::Primary { .. } => {
claims.entry(key).or_default().primary.push(slot);
}
},
PreDigest::SecondaryPlain { .. } => {
claims.entry(key).or_default().secondary.push(slot);
}
},
PreDigest::SecondaryVRF { .. } => {
claims.entry(key).or_default().secondary_vrf.push(slot.into());
}
},
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion client/rpc-api/src/author/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub trait AuthorApi<Hash, BlockHash> {
name = "submitAndWatchExtrinsic",
aliases = "author_extrinsicUpdate",
unsubscribe_aliases = "author_unwatchExtrinsic",
item = TransactionStatus<Hash, BlockHash>
item = TransactionStatus<Hash, BlockHash>
)]
fn watch_extrinsic(&self, bytes: Bytes);
}
77 changes: 44 additions & 33 deletions client/sync-state-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@

#![deny(unused_crate_dependencies)]

use jsonrpsee::{types::error::Error as JsonRpseeError, RpcModule};
use jsonrpsee::{
proc_macros::rpc,
types::{error::Error as JsonRpseeError, JsonRpcResult},
};
use sc_client_api::StorageData;
use sp_blockchain::HeaderBackend;
use sp_runtime::{
Expand Down Expand Up @@ -87,7 +90,7 @@ fn serialize_encoded<S: serde::Serializer, T: codec::Encode>(
/// chain-spec as an extension.
pub type LightSyncStateExtension = Option<serde_json::Value>;

/// Hardcoded infomation that allows light clients to sync quickly.
/// Hardcoded information that allows light clients to sync quickly.
#[derive(serde::Serialize, Clone)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
Expand All @@ -106,6 +109,16 @@ pub struct LightSyncState<Block: BlockT> {
sc_finality_grandpa::AuthoritySet<<Block as BlockT>::Hash, NumberFor<Block>>,
}

/// An api for sync state RPC calls.
#[rpc(client, server, namespace = "sync_state")]
pub trait SyncStateRpcApi {
/// Returns the JSON serialized chainspec running the node, with a sync state.
// NOTE(niklasad1): I changed to `JsonValue` -> `String` as the chainspec
// already returns a JSON String.
#[method(name = "genSyncSpec")]
fn system_gen_sync_spec(&self, raw: bool) -> JsonRpcResult<String>;
}

/// An api for sync state RPC calls.
pub struct SyncStateRpc<Block: BlockT, Client> {
chain_spec: Box<dyn sc_chain_spec::ChainSpec>,
Expand Down Expand Up @@ -137,37 +150,6 @@ where
}
}

/// Convert this [`SyncStateRpc`] to a RPC module.
pub fn into_rpc_module(self) -> Result<RpcModule<Self>, JsonRpseeError> {
let mut module = RpcModule::new(self);

// Returns the json-serialized chainspec running the node, with a sync state.
module.register_method("sync_state_genSyncSpec", |params, sync_state| {
sync_state.deny_unsafe.check_if_safe()?;

let raw = params.one()?;
let current_sync_state =
sync_state.build_sync_state().map_err(|e| JsonRpseeError::to_call_error(e))?;
let mut chain_spec = sync_state.chain_spec.cloned_box();

let extension = sc_chain_spec::get_extension_mut::<LightSyncStateExtension>(
chain_spec.extensions_mut(),
)
.ok_or_else(|| {
JsonRpseeError::from(anyhow::anyhow!(
"Could not find `LightSyncState` chain-spec extension!"
))
})?;

let val = serde_json::to_value(&current_sync_state)
.map_err(|e| JsonRpseeError::to_call_error(e))?;
*extension = Some(val);

chain_spec.as_json(raw).map_err(|e| anyhow::anyhow!(e).into())
})?;
Ok(module)
}

fn build_sync_state(&self) -> Result<LightSyncState<Block>, Error<Block>> {
let finalized_hash = self.client.info().finalized_hash;
let finalized_header = self
Expand All @@ -187,3 +169,32 @@ where
})
}
}

impl<Block, Backend> SyncStateRpcApiServer for SyncStateRpc<Block, Backend>
where
Block: BlockT,
Backend: HeaderBackend<Block> + sc_client_api::AuxStore + 'static,
{
fn system_gen_sync_spec(&self, raw: bool) -> JsonRpcResult<String> {
self.deny_unsafe.check_if_safe()?;

let current_sync_state =
self.build_sync_state().map_err(|e| JsonRpseeError::to_call_error(e))?;
let mut chain_spec = self.chain_spec.cloned_box();

let extension = sc_chain_spec::get_extension_mut::<LightSyncStateExtension>(
chain_spec.extensions_mut(),
)
.ok_or_else(|| {
JsonRpseeError::from(anyhow::anyhow!(
"Could not find `LightSyncState` chain-spec extension!"
))
})?;

let val = serde_json::to_value(&current_sync_state)
.map_err(|e| JsonRpseeError::to_call_error(e))?;
*extension = Some(val);

chain_spec.as_json(raw).map_err(|e| anyhow::anyhow!(e).into())
}
}
5 changes: 2 additions & 3 deletions test-utils/test-runner/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ where
use sp_consensus_babe::AuthorityId;
let config = match config_or_chain_spec {
ConfigOrChainSpec::Config(config) => config,
ConfigOrChainSpec::ChainSpec(chain_spec, task_executor) => {
default_config(task_executor, chain_spec)
}
ConfigOrChainSpec::ChainSpec(chain_spec, task_executor) =>
default_config(task_executor, chain_spec),
};

let executor = NativeElseWasmExecutor::<T::ExecutorDispatch>::new(
Expand Down