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 6 commits
Commits
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
33 changes: 33 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ members = [
"client/cli",
"client/consensus/aura",
"client/consensus/babe",
"client/consensus/babe/rpc",
"client/consensus/manual-seal",
"client/consensus/pow",
"client/consensus/uncles",
Expand Down
34 changes: 27 additions & 7 deletions bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,21 @@ macro_rules! new_full_start {
import_setup = Some((block_import, grandpa_link, babe_link));
Ok(import_queue)
})?
.with_rpc_extensions(|client, pool, _backend, fetcher, _remote_blockchain| -> Result<RpcExtension, _> {
Ok(node_rpc::create(client, pool, node_rpc::LightDeps::none(fetcher)))
.with_rpc_extensions(|builder| -> Result<RpcExtension, _> {
let babe_link = import_setup.as_ref().map(|s| &s.2)
.expect("BabeLink is present for full services or set up failed; qed.");
let deps = node_rpc::FullDeps {
client: builder.client().clone(),
pool: builder.pool(),
select_chain: builder.select_chain().cloned()
.expect("SelectChain is present for full services or set up failed; qed."),
babe: node_rpc::BabeDeps {
keystore: builder.keystore(),
babe_config: sc_consensus_babe::BabeLink::config(babe_link).clone(),
shared_epoch_changes: sc_consensus_babe::BabeLink::epoch_changes(babe_link).clone()
}
};
Ok(node_rpc::create_full(deps))
})?;

(builder, import_setup, inherent_data_providers)
Expand Down Expand Up @@ -353,14 +366,21 @@ pub fn new_light(config: NodeConfiguration)
.with_finality_proof_provider(|client, backend|
Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, client)) as _)
)?
.with_rpc_extensions(|client, pool, _backend, fetcher, remote_blockchain| -> Result<RpcExtension, _> {
let fetcher = fetcher
.with_rpc_extensions(|builder,| ->
Result<RpcExtension, _>
{
let fetcher = builder.fetcher()
.ok_or_else(|| "Trying to start node RPC without active fetcher")?;
let remote_blockchain = remote_blockchain
let remote_blockchain = builder.remote_backend()
.ok_or_else(|| "Trying to start node RPC without active remote blockchain")?;

let light_deps = node_rpc::LightDeps { remote_blockchain, fetcher };
Ok(node_rpc::create(client, pool, Some(light_deps)))
let light_deps = node_rpc::LightDeps {
remote_blockchain,
fetcher,
client: builder.client().clone(),
pool: builder.pool(),
};
Ok(node_rpc::create_light(light_deps))
})?
.build()?;

Expand Down
9 changes: 8 additions & 1 deletion bin/node/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ node-primitives = { version = "2.0.0", path = "../primitives" }
node-runtime = { version = "2.0.0", path = "../runtime" }
sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" }
sp-api = { version = "2.0.0", path = "../../../primitives/api" }
pallet-contracts-rpc = { version = "0.8.0", path = "../../../frame/contracts/rpc/" }
pallet-contracts-rpc = { version = "0.8", path = "../../../frame/contracts/rpc/" }
pallet-transaction-payment-rpc = { version = "2.0.0", path = "../../../frame/transaction-payment/rpc/" }
substrate-frame-rpc-system = { version = "2.0.0", path = "../../../utils/frame/rpc/system" }
sp-transaction-pool = { version = "2.0.0", path = "../../../primitives/transaction-pool" }
sc-consensus-babe = { version = "0.8", path = "../../../client/consensus/babe" }
sc-consensus-babe-rpc = { version = "0.8", path = "../../../client/consensus/babe/rpc" }
sp-consensus-babe = { version = "0.8", path = "../../../primitives/consensus/babe" }
sc-keystore = { version = "2.0.0", path = "../../../client/keystore" }
sc-consensus-epochs = { version = "0.8", path = "../../../client/consensus/epochs" }
sp-consensus = { version = "0.8", path = "../../../primitives/consensus/common" }
sp-blockchain = { version = "2.0.0", path = "../../../primitives/blockchain" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sp-blockchain = { version = "2.0.0", path = "../../../primitives/blockchain" }
sp-blockchain = { version = "2.0.0", path = "../../../primitives/blockchain" }

newline at end of file

137 changes: 98 additions & 39 deletions bin/node/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,73 +29,132 @@
#![warn(missing_docs)]

use std::sync::Arc;
use std::{sync::Arc, fmt};

use node_primitives::{Block, BlockNumber, AccountId, Index, Balance};
use node_runtime::UncheckedExtrinsic;
use sp_api::ProvideRuntimeApi;
use sp_transaction_pool::TransactionPool;
use sp_blockchain::{Error as BlockChainError, HeaderMetadata, HeaderBackend};
use sp_consensus::SelectChain;
use sc_keystore::KeyStorePtr;
use sp_consensus_babe::BabeApi;
use sc_consensus_epochs::SharedEpochChanges;
use sc_consensus_babe::{Config, Epoch};
use sc_consensus_babe_rpc::BabeRPCHandler;

/// Light client extra dependencies.
pub struct LightDeps<F> {
pub struct LightDeps<C, F, P> {
/// client instance
pub client: Arc<C>,
/// txpool
pub pool: Arc<P>,
/// Remote access to the blockchain (async).
pub remote_blockchain: Arc<dyn sc_client::light::blockchain::RemoteBlockchain<Block>>,
/// Fetcher instance.
pub fetcher: Arc<F>,
}

impl<F> LightDeps<F> {
/// Create empty `LightDeps` with given `F` type.
///
/// This is a convenience method to be used in the service builder,
/// to make sure the type of the `LightDeps<F>` is matching.
pub fn none(_: Option<Arc<F>>) -> Option<Self> {
None
}
/// extra dependencies for babe.
pub struct BabeDeps {
/// babe config.
pub babe_config: Config,
/// shared reference to EpochChanges
pub shared_epoch_changes: SharedEpochChanges<Block, Epoch>,
/// shared reference to the Keystore
pub keystore: KeyStorePtr,
}

/// Instantiate all RPC extensions.
///
/// If you provide `LightDeps`, the system is configured for light client.
pub fn create<C, P, M, F>(
client: Arc<C>,
pool: Arc<P>,
light_deps: Option<LightDeps<F>>,
/// Full client dependecies
pub struct FullDeps<C, P, SC> {
/// client instance
pub client: Arc<C>,
/// txpool
pub pool: Arc<P>,
/// SelectChain instance
pub select_chain: SC,
/// babe specific dependencies.
pub babe: BabeDeps,
}


/// Instantiate all Full RPC extensions.
pub fn create_full<C, P, M, SC>(
deps: FullDeps<C, P, SC>,
) -> jsonrpc_core::IoHandler<M> where
C: ProvideRuntimeApi<Block>,
C: sc_client::blockchain::HeaderBackend<Block>,
C: HeaderBackend<Block> + HeaderMetadata<Block, Error=BlockChainError> + 'static,
C: Send + Sync + 'static,
C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,
C::Api: pallet_contracts_rpc::ContractsRuntimeApi<Block, AccountId, Balance, BlockNumber>,
C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance, UncheckedExtrinsic>,
F: sc_client::light::fetcher::Fetcher<Block> + 'static,
C::Api: BabeApi<Block>,
<C::Api as sp_api::ApiErrorExt>::Error: fmt::Debug,
P: TransactionPool + 'static,
M: jsonrpc_core::Metadata + Default,
SC: SelectChain<Block> +'static,
{
use substrate_frame_rpc_system::{FullSystem, LightSystem, SystemApi};
use substrate_frame_rpc_system::{FullSystem, SystemApi};
use pallet_contracts_rpc::{Contracts, ContractsApi};
use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};

let mut io = jsonrpc_core::IoHandler::default();
let FullDeps {
client,
pool,
select_chain,
babe
} = deps;
let BabeDeps {
keystore,
babe_config,
shared_epoch_changes,
} = babe;


io.extend_with(
SystemApi::to_delegate(FullSystem::new(client.clone(), pool))
);
// 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.extend_with(
ContractsApi::to_delegate(Contracts::new(client.clone()))
);
io.extend_with(
TransactionPaymentApi::to_delegate(TransactionPayment::new(client.clone()))
);
io.extend_with(
sc_consensus_babe_rpc::BabeApi::to_delegate(
BabeRPCHandler::new(client, shared_epoch_changes, keystore, babe_config, select_chain)
)
);

io
}

/// Instantiate all Light RPC extensions.
pub fn create_light<C, P, M, F>(
deps: LightDeps<C, F, P>,
) -> jsonrpc_core::IoHandler<M> where
C: sc_client::blockchain::HeaderBackend<Block>,
C: Send + Sync + 'static,
F: sc_client::light::fetcher::Fetcher<Block> + 'static,
P: TransactionPool + 'static,
M: jsonrpc_core::Metadata + Default,
{
use substrate_frame_rpc_system::{LightSystem, SystemApi};

let LightDeps {
client,
pool,
remote_blockchain,
fetcher
} = deps;
let mut io = jsonrpc_core::IoHandler::default();
io.extend_with(
SystemApi::<AccountId, Index>::to_delegate(LightSystem::new(client, remote_blockchain, fetcher, pool))
);

if let Some(LightDeps { remote_blockchain, fetcher }) = light_deps {
io.extend_with(
SystemApi::<AccountId, Index>::to_delegate(LightSystem::new(client, remote_blockchain, fetcher, pool))
);
} else {
io.extend_with(
SystemApi::to_delegate(FullSystem::new(client.clone(), pool))
);

// 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.extend_with(
ContractsApi::to_delegate(Contracts::new(client.clone()))
);
io.extend_with(
TransactionPaymentApi::to_delegate(TransactionPayment::new(client))
);
}
io
}
6 changes: 5 additions & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 219,
impl_version: 0,
impl_version: 3,
apis: RUNTIME_API_VERSIONS,
};

Expand Down Expand Up @@ -737,6 +737,10 @@ impl_runtime_apis! {
secondary_slots: true,
}
}

fn current_epoch_start() -> sp_consensus_babe::SlotNumber {
Babe::current_epoch_start()
}
}

impl sp_authority_discovery::AuthorityDiscoveryApi<Block> for Runtime {
Expand Down
1 change: 1 addition & 0 deletions client/consensus/babe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ sp-application-crypto = { version = "2.0.0", path = "../../../primitives/applica
num-bigint = "0.2.3"
num-rational = "0.2.2"
num-traits = "0.2.8"
serde = { version = "1.0.104", features=["derive"] }
sp-version = { version = "2.0.0", path = "../../../primitives/version" }
sp-io = { version = "2.0.0", path = "../../../primitives/io" }
sp-inherents = { version = "2.0.0", path = "../../../primitives/inherents" }
Expand Down
30 changes: 30 additions & 0 deletions client/consensus/babe/rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "sc-consensus-babe-rpc"
version = "0.8.0"
authors = ["Parity Technologies <[email protected]>"]
description = "RPC extensions for the BABE consensus algorithm"
edition = "2018"
license = "GPL-3.0"

[dependencies]
sc-consensus-babe = { version = "0.8.0", path = "../" }
jsonrpc-core = "14.0.3"
jsonrpc-core-client = "14.0.3"
jsonrpc-derive = "14.0.3"
sp-consensus-babe = { version = "0.8", path = "../../../../primitives/consensus/babe" }
serde = { version = "1.0.104", features=["derive"] }
sp-blockchain = { version = "2.0.0", path = "../../../../primitives/blockchain" }
sp-runtime = { version = "2.0.0", path = "../../../../primitives/runtime" }
sc-consensus-epochs = { version = "0.8", path = "../../epochs" }
futures = "0.3.1"
derive_more = "0.99.2"
sp-api = { version = "2.0.0", path = "../../../../primitives/api" }
sp-consensus = { version = "0.8", path = "../../../../primitives/consensus/common" }
sp-core = { version = "2.0.0", path = "../../../../primitives/core" }
sc-keystore = { version = "2.0.0", path = "../../../keystore" }

[dev-dependencies]
substrate-test-runtime-client = { version = "2.0.0", path = "../../../../test-utils/runtime/client" }
sp-application-crypto = { version = "2.0.0", path = "../../../../primitives/application-crypto" }
sp-keyring = { version = "2.0.0", path = "../../../../primitives/keyring" }
tempfile = "3.1.0"
Loading