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

Commit

Permalink
add: missing RPC (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
AurevoirXavier authored Jun 2, 2020
1 parent e514218 commit ac93234
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 23 deletions.
80 changes: 80 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions bin/node-template/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ name = "node-template"
# anonymous
node-template-runtime = { path = "../runtime" }
# crates
futures = "0.3.4"
jsonrpc-core = "14.0.3"
log = "0.4.8"
structopt = "0.3.13"
codec = { package = "parity-scale-codec", version = "1.3.0" }
futures = { version = "0.3.4" }
jsonrpc-core = { version = "14.0.3" }
log = { version = "0.4.8" }
structopt = { version = "0.3.13" }
# darwinia
darwinia-balances-rpc = { path = "../../../frame/balances/rpc" }
darwinia-claims = { path = "../../../frame/claims" }
Expand All @@ -29,16 +30,21 @@ darwinia-staking-rpc = { path = "../../../frame/staking/rpc" }
# substrate
frame-benchmarking-cli = { optional = true, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" }
pallet-im-online = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" }
pallet-transaction-payment-rpc = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" }
sc-basic-authorship = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" }
sc-cli = { optional = true, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" }
sc-client-api = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" }
sc-consensus = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" }
sc-consensus-babe = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" }
sc-consensus-babe-rpc = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" }
sc-consensus-epochs = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" }
sc-executor = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" }
sc-finality-grandpa = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" }
sc-finality-grandpa-rpc = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" }
sc-keystore = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" }
sc-network = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" }
sc-rpc = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" }
sc-rpc-api = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" }
sc-service = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" }
sc-transaction-pool = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" }
sp-api = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" }
Expand All @@ -51,6 +57,7 @@ sp-inherents = { git = "https://github.com/darwinia-network/substrate.git", tag
sp-finality-grandpa = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" }
sp-runtime = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" }
sp-transaction-pool = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" }
substrate-frame-rpc-system = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" }

[build-dependencies]
# substrate
Expand Down
76 changes: 68 additions & 8 deletions bin/node-template/node/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,107 @@
// --- std ---
use std::sync::Arc;
// --- substrate ---
use sc_finality_grandpa::{SharedAuthoritySet, SharedVoterState};
use sp_api::ProvideRuntimeApi;
// --- darwinia ---
use node_template_runtime::{opaque::Block, AccountId, Balance, BlockNumber, Hash, Power};
use node_template_runtime::{
opaque::Block,
primitives::{AccountId, Balance, BlockNumber, Hash, Nonce, Power},
};

/// A type representing all RPC extensions.
pub type RpcExtension = jsonrpc_core::IoHandler<sc_rpc::Metadata>;

/// Extra dependencies for BABE.
pub struct BabeDeps {
/// BABE protocol config.
pub babe_config: sc_consensus_babe::Config,
/// BABE pending epoch changes.
pub shared_epoch_changes:
sc_consensus_epochs::SharedEpochChanges<Block, sc_consensus_babe::Epoch>,
/// The keystore that manages the keys of the node.
pub keystore: sc_keystore::KeyStorePtr,
}

/// Extra dependencies for GRANDPA
pub struct GrandpaDeps {
/// Voting round info.
pub shared_voter_state: SharedVoterState,
pub shared_voter_state: sc_finality_grandpa::SharedVoterState,
/// Authority set info.
pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,
pub shared_authority_set: sc_finality_grandpa::SharedAuthoritySet<Hash, BlockNumber>,
}

/// Full client dependencies.
pub struct FullDeps<C> {
pub struct FullDeps<C, P, SC> {
/// The client instance to use.
pub client: Arc<C>,
/// Transaction pool instance.
pub pool: Arc<P>,
/// The SelectChain Strategy
pub select_chain: SC,
/// Whether to deny unsafe calls
pub deny_unsafe: sc_rpc_api::DenyUnsafe,
/// BABE specific dependencies.
pub babe: BabeDeps,
/// GRANDPA specific dependencies.
pub grandpa: GrandpaDeps,
}

pub fn create<C>(deps: FullDeps<C>) -> RpcExtension
pub fn create<C, P, SC, UE>(deps: FullDeps<C, P, SC>) -> RpcExtension
where
C: ProvideRuntimeApi<Block>,
C: sp_blockchain::HeaderBackend<Block>,
C: sp_blockchain::HeaderBackend<Block>
+ sp_blockchain::HeaderMetadata<Block, Error = sp_blockchain::Error>,
C: 'static + Send + Sync,
C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>,
C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance, UE>,
C::Api: sc_consensus_babe::BabeApi<Block>,
C::Api: darwinia_balances_rpc::BalancesRuntimeApi<Block, AccountId, Balance>,
C::Api: darwinia_staking_rpc::StakingRuntimeApi<Block, AccountId, Power>,
P: 'static + sp_transaction_pool::TransactionPool,
SC: 'static + sp_consensus::SelectChain<Block>,
UE: 'static + Send + Sync + codec::Codec,
{
// --- substrate ---
use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};
use sc_consensus_babe_rpc::{BabeApi, BabeRpcHandler};
use sc_finality_grandpa_rpc::GrandpaRpcHandler;
use substrate_frame_rpc_system::{FullSystem, SystemApi};
// --- darwinia ---
use darwinia_balances_rpc::{Balances, BalancesApi};
use darwinia_staking_rpc::{Staking, StakingApi};

let FullDeps { client, grandpa } = deps;
let FullDeps {
client,
pool,
select_chain,
deny_unsafe,
babe,
grandpa,
} = deps;

let mut io = jsonrpc_core::IoHandler::default();
io.extend_with(SystemApi::to_delegate(FullSystem::new(
client.clone(),
pool,
)));
io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(
client.clone(),
)));
{
let BabeDeps {
keystore,
babe_config,
shared_epoch_changes,
} = babe;
io.extend_with(BabeApi::to_delegate(BabeRpcHandler::new(
client.clone(),
shared_epoch_changes,
keystore,
babe_config,
select_chain,
deny_unsafe,
)));
};
{
let GrandpaDeps {
shared_voter_state,
Expand Down
50 changes: 39 additions & 11 deletions bin/node-template/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,52 @@ macro_rules! new_full_start {
Ok(import_queue)
},
)?
.with_rpc_extensions(|builder| -> Result<crate::rpc::RpcExtension, _> {
.with_rpc_extensions_builder(|builder| {
let grandpa_link = import_setup
.as_ref()
.map(|s| &s.1)
.expect("GRANDPA LinkHalf is present for full services or set up failed; qed.");
let shared_authority_set = grandpa_link.shared_authority_set();

let shared_authority_set = grandpa_link.shared_authority_set().clone();
let shared_voter_state = sc_finality_grandpa::SharedVoterState::empty();
let deps = crate::rpc::FullDeps {
client: builder.client().clone(),
grandpa: crate::rpc::GrandpaDeps {
shared_voter_state: shared_voter_state.clone(),
shared_authority_set: shared_authority_set.clone(),
},
};

rpc_setup = Some((shared_voter_state));
rpc_setup = Some((shared_voter_state.clone()));

Ok(crate::rpc::create(deps))
let babe_link = import_setup
.as_ref()
.map(|s| &s.2)
.expect("BabeLink is present for full services or set up failed; qed.");

let babe_config = babe_link.config().clone();
let shared_epoch_changes = babe_link.epoch_changes().clone();

let client = builder.client().clone();
let pool = builder.pool().clone();
let select_chain = builder
.select_chain()
.cloned()
.expect("SelectChain is present for full services or set up failed; qed.");
let keystore = builder.keystore().clone();

Ok(move |deny_unsafe| -> crate::rpc::RpcExtension {
let deps = crate::rpc::FullDeps {
client: client.clone(),
pool: pool.clone(),
select_chain: select_chain.clone(),
deny_unsafe,
babe: crate::rpc::BabeDeps {
babe_config: babe_config.clone(),
shared_epoch_changes: shared_epoch_changes.clone(),
keystore: keystore.clone(),
},
grandpa: crate::rpc::GrandpaDeps {
shared_voter_state: shared_voter_state.clone(),
shared_authority_set: shared_authority_set.clone(),
},
};

crate::rpc::create(deps)
})
})?;

(builder, import_setup, inherent_data_providers, rpc_setup)
Expand Down
Loading

0 comments on commit ac93234

Please sign in to comment.