Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Commit

Permalink
Unify rpc api and implementation name (paritytech#11469)
Browse files Browse the repository at this point in the history
* Unify rpc api and implementation name

Signed-off-by: koushiro <[email protected]>

* MauanlSeal ==> ManualSealRpc

Signed-off-by: koushiro <[email protected]>

* Remove extra Rpc naming in the structs

Signed-off-by: koushiro <[email protected]>

* Update doc

Signed-off-by: koushiro <[email protected]>

* fix merge

Co-authored-by: Shawn Tabrizi <[email protected]>
  • Loading branch information
2 people authored and DaviRain-Su committed Aug 23, 2022
1 parent 941c5ae commit fe7626b
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 83 deletions.
8 changes: 4 additions & 4 deletions bin/node-template/node/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ where
C::Api: BlockBuilder<Block>,
P: TransactionPool + 'static,
{
use pallet_transaction_payment_rpc::{TransactionPaymentApiServer, TransactionPaymentRpc};
use substrate_frame_rpc_system::{SystemApiServer, SystemRpc};
use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};
use substrate_frame_rpc_system::{System, SystemApiServer};

let mut module = RpcModule::new(());
let FullDeps { client, pool, deny_unsafe } = deps;

module.merge(SystemRpc::new(client.clone(), pool.clone(), deny_unsafe).into_rpc())?;
module.merge(TransactionPaymentRpc::new(client).into_rpc())?;
module.merge(System::new(client.clone(), pool.clone(), deny_unsafe).into_rpc())?;
module.merge(TransactionPayment::new(client).into_rpc())?;

// Extend this RPC with a custom API by using the following syntax.
// `YourRpcStruct` should have a reference to a client, which is needed
Expand Down
37 changes: 16 additions & 21 deletions bin/node/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,10 @@ use jsonrpsee::RpcModule;
use node_primitives::{AccountId, Balance, Block, BlockNumber, Hash, Index};
use sc_client_api::AuxStore;
use sc_consensus_babe::{Config, Epoch};
use sc_consensus_babe_rpc::BabeRpc;
use sc_consensus_epochs::SharedEpochChanges;
use sc_finality_grandpa::{
FinalityProofProvider, GrandpaJustificationStream, SharedAuthoritySet, SharedVoterState,
};
use sc_finality_grandpa_rpc::GrandpaRpc;
use sc_rpc::SubscriptionTaskExecutor;
pub use sc_rpc_api::DenyUnsafe;
use sc_transaction_pool_api::TransactionPool;
Expand Down Expand Up @@ -120,15 +118,15 @@ where
B: sc_client_api::Backend<Block> + Send + Sync + 'static,
B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,
{
use pallet_contracts_rpc::{ContractsApiServer, ContractsRpc};
use pallet_mmr_rpc::{MmrApiServer, MmrRpc};
use pallet_transaction_payment_rpc::{TransactionPaymentApiServer, TransactionPaymentRpc};
use sc_consensus_babe_rpc::BabeApiServer;
use sc_finality_grandpa_rpc::GrandpaApiServer;
use pallet_contracts_rpc::{Contracts, ContractsApiServer};
use pallet_mmr_rpc::{Mmr, MmrApiServer};
use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};
use sc_consensus_babe_rpc::{Babe, BabeApiServer};
use sc_finality_grandpa_rpc::{Grandpa, GrandpaApiServer};
use sc_rpc::dev::{Dev, DevApiServer};
use sc_sync_state_rpc::{SyncStateRpc, SyncStateRpcApiServer};
use substrate_frame_rpc_system::{SystemApiServer, SystemRpc};
use substrate_state_trie_migration_rpc::StateMigrationApiServer;
use sc_sync_state_rpc::{SyncState, SyncStateApiServer};
use substrate_frame_rpc_system::{System, SystemApiServer};
use substrate_state_trie_migration_rpc::{StateMigration, StateMigrationApiServer};

let mut io = RpcModule::new(());
let FullDeps { client, pool, select_chain, chain_spec, deny_unsafe, babe, grandpa } = deps;
Expand All @@ -142,15 +140,15 @@ where
finality_provider,
} = grandpa;

io.merge(SystemRpc::new(client.clone(), pool, deny_unsafe).into_rpc())?;
io.merge(System::new(client.clone(), pool, deny_unsafe).into_rpc())?;
// Making synchronous calls in light client freezes the browser currently,
// more context: https://github.com/paritytech/substrate/pull/3480
// These RPCs should use an asynchronous caller instead.
io.merge(ContractsRpc::new(client.clone()).into_rpc())?;
io.merge(MmrRpc::new(client.clone()).into_rpc())?;
io.merge(TransactionPaymentRpc::new(client.clone()).into_rpc())?;
io.merge(Contracts::new(client.clone()).into_rpc())?;
io.merge(Mmr::new(client.clone()).into_rpc())?;
io.merge(TransactionPayment::new(client.clone()).into_rpc())?;
io.merge(
BabeRpc::new(
Babe::new(
client.clone(),
shared_epoch_changes.clone(),
keystore,
Expand All @@ -161,7 +159,7 @@ where
.into_rpc(),
)?;
io.merge(
GrandpaRpc::new(
Grandpa::new(
subscription_executor,
shared_authority_set.clone(),
shared_voter_state,
Expand All @@ -172,14 +170,11 @@ where
)?;

io.merge(
SyncStateRpc::new(chain_spec, client.clone(), shared_authority_set, shared_epoch_changes)?
SyncState::new(chain_spec, client.clone(), shared_authority_set, shared_epoch_changes)?
.into_rpc(),
)?;

io.merge(
substrate_state_trie_migration_rpc::MigrationRpc::new(client.clone(), backend, deny_unsafe)
.into_rpc(),
)?;
io.merge(StateMigration::new(client.clone(), backend, deny_unsafe).into_rpc())?;
io.merge(Dev::new(client, deny_unsafe).into_rpc())?;

Ok(io)
Expand Down
23 changes: 9 additions & 14 deletions client/beefy/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,17 @@ pub trait BeefyApi<Notification, Hash> {
}

/// Implements the BeefyApi RPC trait for interacting with BEEFY.
pub struct BeefyRpcHandler<Block: BlockT> {
pub struct Beefy<Block: BlockT> {
signed_commitment_stream: BeefySignedCommitmentStream<Block>,
beefy_best_block: Arc<RwLock<Option<Block::Hash>>>,
executor: SubscriptionTaskExecutor,
}

impl<Block> BeefyRpcHandler<Block>
impl<Block> Beefy<Block>
where
Block: BlockT,
{
/// Creates a new BeefyRpcHandler instance.
/// Creates a new Beefy Rpc handler instance.
pub fn new(
signed_commitment_stream: BeefySignedCommitmentStream<Block>,
best_block_stream: BeefyBestBlockStream<Block>,
Expand All @@ -131,8 +131,7 @@ where
}

#[async_trait]
impl<Block> BeefyApiServer<notification::EncodedSignedCommitment, Block::Hash>
for BeefyRpcHandler<Block>
impl<Block> BeefyApiServer<notification::EncodedSignedCommitment, Block::Hash> for Beefy<Block>
where
Block: BlockT,
{
Expand Down Expand Up @@ -174,24 +173,20 @@ mod tests {
use sp_runtime::traits::{BlakeTwo256, Hash};
use substrate_test_runtime_client::runtime::Block;

fn setup_io_handler() -> (RpcModule<BeefyRpcHandler<Block>>, BeefySignedCommitmentSender<Block>)
{
fn setup_io_handler() -> (RpcModule<Beefy<Block>>, BeefySignedCommitmentSender<Block>) {
let (_, stream) = BeefyBestBlockStream::<Block>::channel();
setup_io_handler_with_best_block_stream(stream)
}

fn setup_io_handler_with_best_block_stream(
best_block_stream: BeefyBestBlockStream<Block>,
) -> (RpcModule<BeefyRpcHandler<Block>>, BeefySignedCommitmentSender<Block>) {
) -> (RpcModule<Beefy<Block>>, BeefySignedCommitmentSender<Block>) {
let (commitment_sender, commitment_stream) =
BeefySignedCommitmentStream::<Block>::channel();

let handler = BeefyRpcHandler::new(
commitment_stream,
best_block_stream,
sc_rpc::testing::test_executor(),
)
.expect("Setting up the BEEFY RPC handler works");
let handler =
Beefy::new(commitment_stream, best_block_stream, sc_rpc::testing::test_executor())
.expect("Setting up the BEEFY RPC handler works");

(handler.into_rpc(), commitment_sender)
}
Expand Down
12 changes: 6 additions & 6 deletions client/consensus/babe/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub trait BabeApi {
}

/// Provides RPC methods for interacting with Babe.
pub struct BabeRpc<B: BlockT, C, SC> {
pub struct Babe<B: BlockT, C, SC> {
/// shared reference to the client.
client: Arc<C>,
/// shared reference to EpochChanges
Expand All @@ -64,8 +64,8 @@ pub struct BabeRpc<B: BlockT, C, SC> {
deny_unsafe: DenyUnsafe,
}

impl<B: BlockT, C, SC> BabeRpc<B, C, SC> {
/// Creates a new instance of the BabeRpc handler.
impl<B: BlockT, C, SC> Babe<B, C, SC> {
/// Creates a new instance of the Babe Rpc handler.
pub fn new(
client: Arc<C>,
shared_epoch_changes: SharedEpochChanges<B, Epoch>,
Expand All @@ -79,7 +79,7 @@ impl<B: BlockT, C, SC> BabeRpc<B, C, SC> {
}

#[async_trait]
impl<B: BlockT, C, SC> BabeApiServer for BabeRpc<B, C, SC>
impl<B: BlockT, C, SC> BabeApiServer for Babe<B, C, SC>
where
B: BlockT,
C: ProvideRuntimeApi<B>
Expand Down Expand Up @@ -239,7 +239,7 @@ mod tests {

fn test_babe_rpc_module(
deny_unsafe: DenyUnsafe,
) -> BabeRpc<Block, TestClient, sc_consensus::LongestChain<Backend, Block>> {
) -> Babe<Block, TestClient, sc_consensus::LongestChain<Backend, Block>> {
let builder = TestClientBuilder::new();
let (client, longest_chain) = builder.build_with_longest_chain();
let client = Arc::new(client);
Expand All @@ -250,7 +250,7 @@ mod tests {
let epoch_changes = link.epoch_changes().clone();
let keystore = create_temp_keystore::<AuthorityPair>(Sr25519Keyring::Alice).0;

BabeRpc::new(client.clone(), epoch_changes, keystore, config, longest_chain, deny_unsafe)
Babe::new(client.clone(), epoch_changes, keystore, config, longest_chain, deny_unsafe)
}

#[tokio::test]
Expand Down
14 changes: 7 additions & 7 deletions client/finality-grandpa/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ pub trait GrandpaApi<Notification, Hash, Number> {
}

/// Provides RPC methods for interacting with GRANDPA.
pub struct GrandpaRpc<AuthoritySet, VoterState, Block: BlockT, ProofProvider> {
pub struct Grandpa<AuthoritySet, VoterState, Block: BlockT, ProofProvider> {
executor: SubscriptionTaskExecutor,
authority_set: AuthoritySet,
voter_state: VoterState,
justification_stream: GrandpaJustificationStream<Block>,
finality_proof_provider: Arc<ProofProvider>,
}
impl<AuthoritySet, VoterState, Block: BlockT, ProofProvider>
GrandpaRpc<AuthoritySet, VoterState, Block, ProofProvider>
Grandpa<AuthoritySet, VoterState, Block, ProofProvider>
{
/// Prepare a new [`GrandpaRpc`]
/// Prepare a new [`Grandpa`] Rpc handler.
pub fn new(
executor: SubscriptionTaskExecutor,
authority_set: AuthoritySet,
Expand All @@ -91,7 +91,7 @@ impl<AuthoritySet, VoterState, Block: BlockT, ProofProvider>
#[async_trait]
impl<AuthoritySet, VoterState, Block, ProofProvider>
GrandpaApiServer<JustificationNotification, Block::Hash, NumberFor<Block>>
for GrandpaRpc<AuthoritySet, VoterState, Block, ProofProvider>
for Grandpa<AuthoritySet, VoterState, Block, ProofProvider>
where
VoterState: ReportVoterState + Send + Sync + 'static,
AuthoritySet: ReportAuthoritySet + Send + Sync + 'static,
Expand Down Expand Up @@ -243,7 +243,7 @@ mod tests {
fn setup_io_handler<VoterState>(
voter_state: VoterState,
) -> (
RpcModule<GrandpaRpc<TestAuthoritySet, VoterState, Block, TestFinalityProofProvider>>,
RpcModule<Grandpa<TestAuthoritySet, VoterState, Block, TestFinalityProofProvider>>,
GrandpaJustificationSender<Block>,
)
where
Expand All @@ -256,7 +256,7 @@ mod tests {
voter_state: VoterState,
finality_proof: Option<FinalityProof<Header>>,
) -> (
RpcModule<GrandpaRpc<TestAuthoritySet, VoterState, Block, TestFinalityProofProvider>>,
RpcModule<Grandpa<TestAuthoritySet, VoterState, Block, TestFinalityProofProvider>>,
GrandpaJustificationSender<Block>,
)
where
Expand All @@ -266,7 +266,7 @@ mod tests {
let finality_proof_provider = Arc::new(TestFinalityProofProvider { finality_proof });
let executor = Arc::new(TaskExecutor::default());

let rpc = GrandpaRpc::new(
let rpc = Grandpa::new(
executor,
TestAuthoritySet,
voter_state,
Expand Down
8 changes: 4 additions & 4 deletions client/rpc/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub fn new_full<BE, Block: BlockT, Client>(
executor: SubscriptionTaskExecutor,
deny_unsafe: DenyUnsafe,
rpc_max_payload: Option<usize>,
) -> (StateApi<Block, Client>, ChildState<Block, Client>)
) -> (State<Block, Client>, ChildState<Block, Client>)
where
Block: BlockT + 'static,
Block::Hash: Unpin,
Expand All @@ -192,17 +192,17 @@ where
rpc_max_payload,
));
let backend = Box::new(self::state_full::FullState::new(client, executor, rpc_max_payload));
(StateApi { backend, deny_unsafe }, ChildState { backend: child_backend })
(State { backend, deny_unsafe }, ChildState { backend: child_backend })
}

/// State API with subscriptions support.
pub struct StateApi<Block, Client> {
pub struct State<Block, Client> {
backend: Box<dyn StateBackend<Block, Client>>,
/// Whether to deny unsafe calls
deny_unsafe: DenyUnsafe,
}

impl<Block, Client> StateApiServer<Block::Hash> for StateApi<Block, Client>
impl<Block, Client> StateApiServer<Block::Hash> for State<Block, Client>
where
Block: BlockT + 'static,
Client: Send + Sync + 'static,
Expand Down
10 changes: 5 additions & 5 deletions client/sync-state-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
//! ```
//!
//! If the [`LightSyncStateExtension`] is not added as an extension to the chain spec,
//! the [`SyncStateRpc`] will fail at instantiation.
//! the [`SyncState`] will fail at instantiation.
#![deny(unused_crate_dependencies)]

Expand Down Expand Up @@ -125,21 +125,21 @@ pub struct LightSyncState<Block: BlockT> {

/// An api for sync state RPC calls.
#[rpc(client, server)]
pub trait SyncStateRpcApi {
pub trait SyncStateApi {
/// Returns the JSON serialized chainspec running the node, with a sync state.
#[method(name = "sync_state_genSyncSpec")]
fn system_gen_sync_spec(&self, raw: bool) -> RpcResult<serde_json::Value>;
}

/// An api for sync state RPC calls.
pub struct SyncStateRpc<Block: BlockT, Client> {
pub struct SyncState<Block: BlockT, Client> {
chain_spec: Box<dyn sc_chain_spec::ChainSpec>,
client: Arc<Client>,
shared_authority_set: SharedAuthoritySet<Block>,
shared_epoch_changes: SharedEpochChanges<Block>,
}

impl<Block, Client> SyncStateRpc<Block, Client>
impl<Block, Client> SyncState<Block, Client>
where
Block: BlockT,
Client: HeaderBackend<Block> + sc_client_api::AuxStore + 'static,
Expand Down Expand Up @@ -180,7 +180,7 @@ where
}
}

impl<Block, Backend> SyncStateRpcApiServer for SyncStateRpc<Block, Backend>
impl<Block, Backend> SyncStateApiServer for SyncState<Block, Backend>
where
Block: BlockT,
Backend: HeaderBackend<Block> + sc_client_api::AuxStore + 'static,
Expand Down
6 changes: 3 additions & 3 deletions frame/contracts/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,12 @@ where
}

/// Contracts RPC methods.
pub struct ContractsRpc<Client, Block> {
pub struct Contracts<Client, Block> {
client: Arc<Client>,
_marker: PhantomData<Block>,
}

impl<Client, Block> ContractsRpc<Client, Block> {
impl<Client, Block> Contracts<Client, Block> {
/// Create new `Contracts` with the given reference to the client.
pub fn new(client: Arc<Client>) -> Self {
Self { client, _marker: Default::default() }
Expand All @@ -193,7 +193,7 @@ impl<Client, Block, AccountId, Balance, Hash>
AccountId,
Balance,
Hash,
> for ContractsRpc<Client, Block>
> for Contracts<Client, Block>
where
Block: BlockT,
Client: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
Expand Down
7 changes: 3 additions & 4 deletions frame/merkle-mountain-range/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,20 @@ pub trait MmrApi<BlockHash> {
}

/// MMR RPC methods.
pub struct MmrRpc<Client, Block> {
pub struct Mmr<Client, Block> {
client: Arc<Client>,
_marker: PhantomData<Block>,
}

impl<C, B> MmrRpc<C, B> {
impl<C, B> Mmr<C, B> {
/// Create new `Mmr` with the given reference to the client.
pub fn new(client: Arc<C>) -> Self {
Self { client, _marker: Default::default() }
}
}

#[async_trait]
impl<Client, Block, MmrHash> MmrApiServer<<Block as BlockT>::Hash>
for MmrRpc<Client, (Block, MmrHash)>
impl<Client, Block, MmrHash> MmrApiServer<<Block as BlockT>::Hash> for Mmr<Client, (Block, MmrHash)>
where
Block: BlockT,
Client: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
Expand Down
Loading

0 comments on commit fe7626b

Please sign in to comment.