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

Commit

Permalink
Companion PR for Substrate #5375
Browse files Browse the repository at this point in the history
  • Loading branch information
octol committed Apr 27, 2020
1 parent 402e9a0 commit 927b045
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
2 changes: 2 additions & 0 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ polkadot-primitives = { path = "../primitives" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-rpc = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-finality-grandpa = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-finality-grandpa-rpc = { git = "https://github.com/paritytech/substrate", branch = "master" }
txpool-api = { package = "sp-transaction-pool", git = "https://github.com/paritytech/substrate", branch = "master" }
frame-rpc-system = { package = "substrate-frame-rpc-system", git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-transaction-payment-rpc = { git = "https://github.com/paritytech/substrate", branch = "master" }
Expand Down
21 changes: 19 additions & 2 deletions rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,23 @@

use std::sync::Arc;

use polkadot_primitives::{Block, AccountId, Nonce, Balance};
use polkadot_primitives::{Block, BlockNumber, AccountId, Nonce, Balance, Hash};
use sp_api::ProvideRuntimeApi;
use txpool_api::TransactionPool;

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

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

/// Instantiate all RPC extensions.
pub fn create_full<C, P, UE>(client: Arc<C>, pool: Arc<P>) -> RpcExtension where
pub fn create_full<C, P, UE>(client: Arc<C>, pool: Arc<P>, grandpa_deps: GrandpaDeps) -> RpcExtension where
C: ProvideRuntimeApi<Block>,
C: client::blockchain::HeaderBackend<Block>,
C: Send + Sync + 'static,
Expand All @@ -39,14 +47,23 @@ pub fn create_full<C, P, UE>(client: Arc<C>, pool: Arc<P>) -> RpcExtension where
{
use frame_rpc_system::{FullSystem, SystemApi};
use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};
use sc_finality_grandpa_rpc::{GrandpaApi, GrandpaRpcHandler};

let mut io = jsonrpc_core::IoHandler::default();
let GrandpaDeps {
shared_voter_state,
shared_authority_set,
} = grandpa_deps;

io.extend_with(
SystemApi::to_delegate(FullSystem::new(client.clone(), pool))
);
io.extend_with(
TransactionPaymentApi::to_delegate(TransactionPayment::new(client))
);
io.extend_with(
GrandpaApi::to_delegate(GrandpaRpcHandler::new(shared_voter_state, shared_authority_set))
);
io
}

Expand Down
22 changes: 18 additions & 4 deletions service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use polkadot_primitives::{parachain, Hash, BlockId, AccountId, Nonce, Balance};
#[cfg(feature = "full-node")]
use polkadot_network::{legacy::gossip::Known, protocol as network_protocol};
use service::{error::Error as ServiceError, ServiceBuilder};
use grandpa::{self, FinalityProofProvider as GrandpaFinalityProofProvider};
use grandpa::{self, FinalityProofProvider as GrandpaFinalityProofProvider, SharedVoterState};
use inherents::InherentDataProviders;
use sc_executor::native_executor_instance;
use log::info;
Expand Down Expand Up @@ -151,6 +151,7 @@ macro_rules! new_full_start {
set_prometheus_registry(&mut $config)?;

let mut import_setup = None;
let mut rpc_setup = None;
let inherent_data_providers = inherents::InherentDataProviders::new();
let builder = service::ServiceBuilder::new_full::<
Block, $runtime, $executor
Expand Down Expand Up @@ -202,10 +203,19 @@ macro_rules! new_full_start {
Ok(import_queue)
})?
.with_rpc_extensions(|builder| -> Result<polkadot_rpc::RpcExtension, _> {
Ok(polkadot_rpc::create_full(builder.client().clone(), builder.pool()))
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_voter_state = SharedVoterState::new(None);
let grandpa_deps = polkadot_rpc::GrandpaDeps {
shared_voter_state: shared_voter_state.clone(),
shared_authority_set: shared_authority_set.clone(),
};
rpc_setup = Some((shared_voter_state));
Ok(polkadot_rpc::create_full(builder.client().clone(), builder.pool(), grandpa_deps))
})?;

(builder, import_setup, inherent_data_providers)
(builder, import_setup, inherent_data_providers, rpc_setup)
}}
}

Expand Down Expand Up @@ -375,7 +385,7 @@ pub fn new_full<Runtime, Dispatch, Extrinsic>(
let authority_discovery_enabled = authority_discovery_enabled;
let slot_duration = slot_duration;

let (builder, mut import_setup, inherent_data_providers) = new_full_start!(config, Runtime, Dispatch);
let (builder, mut import_setup, inherent_data_providers, mut rpc_setup) = new_full_start!(config, Runtime, Dispatch);

let backend = builder.backend().clone();

Expand All @@ -389,6 +399,9 @@ pub fn new_full<Runtime, Dispatch, Extrinsic>(
let (block_import, link_half, babe_link) = import_setup.take()
.expect("Link Half and Block Import are present for Full Services or setup failed before. qed");

let shared_voter_state = rpc_setup.take()
.expect("SharedVoterState is present for Full Services or setup failed before. qed");

let client = service.client();
let known_oracle = client.clone();

Expand Down Expand Up @@ -604,6 +617,7 @@ pub fn new_full<Runtime, Dispatch, Extrinsic>(
telemetry_on_connect: Some(service.telemetry_on_connect_stream()),
voting_rule,
prometheus_registry: service.prometheus_registry(),
shared_voter_state,
};

service.spawn_essential_task(
Expand Down

0 comments on commit 927b045

Please sign in to comment.