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

Commit c50844a

Browse files
seunlanlegetomusdrwandresilva
authored
Adds babe rpc support (#4729)
* babe_epochAuthorship remove test-helpers from sp-keyring, bump spec_version, impl_version * bump Cargo.lock * add BabeRPC to node-rpc * rename to BabeApi, remove err_derive * pass &ServiceBuilder to with_rpc_extensions callback * sc-consensus-babe-rpc * Update client/consensus/babe/src/lib.rs Co-Authored-By: Tomasz Drwięga <[email protected]> * Better docs, code style chanegs Co-Authored-By: André Silva <[email protected]> * new line at the end of Cargo.toml Co-authored-by: Tomasz Drwięga <[email protected]> Co-authored-by: André Silva <[email protected]>
1 parent 35866b0 commit c50844a

File tree

16 files changed

+490
-69
lines changed

16 files changed

+490
-69
lines changed

Cargo.lock

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ members = [
2323
"client/cli",
2424
"client/consensus/aura",
2525
"client/consensus/babe",
26+
"client/consensus/babe/rpc",
2627
"client/consensus/manual-seal",
2728
"client/consensus/pow",
2829
"client/consensus/uncles",

bin/node/cli/src/service.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,21 @@ macro_rules! new_full_start {
9595
import_setup = Some((block_import, grandpa_link, babe_link));
9696
Ok(import_queue)
9797
})?
98-
.with_rpc_extensions(|client, pool, _backend, fetcher, _remote_blockchain| -> Result<RpcExtension, _> {
99-
Ok(node_rpc::create(client, pool, node_rpc::LightDeps::none(fetcher)))
98+
.with_rpc_extensions(|builder| -> Result<RpcExtension, _> {
99+
let babe_link = import_setup.as_ref().map(|s| &s.2)
100+
.expect("BabeLink is present for full services or set up failed; qed.");
101+
let deps = node_rpc::FullDeps {
102+
client: builder.client().clone(),
103+
pool: builder.pool(),
104+
select_chain: builder.select_chain().cloned()
105+
.expect("SelectChain is present for full services or set up failed; qed."),
106+
babe: node_rpc::BabeDeps {
107+
keystore: builder.keystore(),
108+
babe_config: sc_consensus_babe::BabeLink::config(babe_link).clone(),
109+
shared_epoch_changes: sc_consensus_babe::BabeLink::epoch_changes(babe_link).clone()
110+
}
111+
};
112+
Ok(node_rpc::create_full(deps))
100113
})?;
101114

102115
(builder, import_setup, inherent_data_providers)
@@ -352,14 +365,21 @@ pub fn new_light(config: NodeConfiguration)
352365
.with_finality_proof_provider(|client, backend|
353366
Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, client)) as _)
354367
)?
355-
.with_rpc_extensions(|client, pool, _backend, fetcher, remote_blockchain| -> Result<RpcExtension, _> {
356-
let fetcher = fetcher
368+
.with_rpc_extensions(|builder,| ->
369+
Result<RpcExtension, _>
370+
{
371+
let fetcher = builder.fetcher()
357372
.ok_or_else(|| "Trying to start node RPC without active fetcher")?;
358-
let remote_blockchain = remote_blockchain
373+
let remote_blockchain = builder.remote_backend()
359374
.ok_or_else(|| "Trying to start node RPC without active remote blockchain")?;
360375

361-
let light_deps = node_rpc::LightDeps { remote_blockchain, fetcher };
362-
Ok(node_rpc::create(client, pool, Some(light_deps)))
376+
let light_deps = node_rpc::LightDeps {
377+
remote_blockchain,
378+
fetcher,
379+
client: builder.client().clone(),
380+
pool: builder.pool(),
381+
};
382+
Ok(node_rpc::create_light(light_deps))
363383
})?
364384
.build()?;
365385

bin/node/rpc/Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ node-primitives = { version = "2.0.0", path = "../primitives" }
1212
node-runtime = { version = "2.0.0", path = "../runtime" }
1313
sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" }
1414
sp-api = { version = "2.0.0", path = "../../../primitives/api" }
15-
pallet-contracts-rpc = { version = "0.8.0", path = "../../../frame/contracts/rpc/" }
15+
pallet-contracts-rpc = { version = "0.8", path = "../../../frame/contracts/rpc/" }
1616
pallet-transaction-payment-rpc = { version = "2.0.0", path = "../../../frame/transaction-payment/rpc/" }
1717
substrate-frame-rpc-system = { version = "2.0.0", path = "../../../utils/frame/rpc/system" }
1818
sp-transaction-pool = { version = "2.0.0", path = "../../../primitives/transaction-pool" }
19+
sc-consensus-babe = { version = "0.8", path = "../../../client/consensus/babe" }
20+
sc-consensus-babe-rpc = { version = "0.8", path = "../../../client/consensus/babe/rpc" }
21+
sp-consensus-babe = { version = "0.8", path = "../../../primitives/consensus/babe" }
22+
sc-keystore = { version = "2.0.0", path = "../../../client/keystore" }
23+
sc-consensus-epochs = { version = "0.8", path = "../../../client/consensus/epochs" }
24+
sp-consensus = { version = "0.8", path = "../../../primitives/consensus/common" }
25+
sp-blockchain = { version = "2.0.0", path = "../../../primitives/blockchain" }

bin/node/rpc/src/lib.rs

Lines changed: 96 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,73 +29,130 @@
2929
3030
#![warn(missing_docs)]
3131

32-
use std::sync::Arc;
32+
use std::{sync::Arc, fmt};
3333

3434
use node_primitives::{Block, BlockNumber, AccountId, Index, Balance};
3535
use node_runtime::UncheckedExtrinsic;
3636
use sp_api::ProvideRuntimeApi;
3737
use sp_transaction_pool::TransactionPool;
38+
use sp_blockchain::{Error as BlockChainError, HeaderMetadata, HeaderBackend};
39+
use sp_consensus::SelectChain;
40+
use sc_keystore::KeyStorePtr;
41+
use sp_consensus_babe::BabeApi;
42+
use sc_consensus_epochs::SharedEpochChanges;
43+
use sc_consensus_babe::{Config, Epoch};
44+
use sc_consensus_babe_rpc::BabeRPCHandler;
3845

3946
/// Light client extra dependencies.
40-
pub struct LightDeps<F> {
47+
pub struct LightDeps<C, F, P> {
48+
/// The client instance to use.
49+
pub client: Arc<C>,
50+
/// Transaction pool instance.
51+
pub pool: Arc<P>,
4152
/// Remote access to the blockchain (async).
4253
pub remote_blockchain: Arc<dyn sc_client::light::blockchain::RemoteBlockchain<Block>>,
4354
/// Fetcher instance.
4455
pub fetcher: Arc<F>,
4556
}
4657

47-
impl<F> LightDeps<F> {
48-
/// Create empty `LightDeps` with given `F` type.
49-
///
50-
/// This is a convenience method to be used in the service builder,
51-
/// to make sure the type of the `LightDeps<F>` is matching.
52-
pub fn none(_: Option<Arc<F>>) -> Option<Self> {
53-
None
54-
}
58+
/// Extra dependencies for BABE.
59+
pub struct BabeDeps {
60+
/// BABE protocol config.
61+
pub babe_config: Config,
62+
/// BABE pending epoch changes.
63+
pub shared_epoch_changes: SharedEpochChanges<Block, Epoch>,
64+
/// The keystore that manages the keys of the node.
65+
pub keystore: KeyStorePtr,
5566
}
5667

57-
/// Instantiate all RPC extensions.
58-
///
59-
/// If you provide `LightDeps`, the system is configured for light client.
60-
pub fn create<C, P, M, F>(
61-
client: Arc<C>,
62-
pool: Arc<P>,
63-
light_deps: Option<LightDeps<F>>,
68+
/// Full client dependencies.
69+
pub struct FullDeps<C, P, SC> {
70+
/// The client instance to use.
71+
pub client: Arc<C>,
72+
/// Transaction pool instance.
73+
pub pool: Arc<P>,
74+
/// The SelectChain Strategy
75+
pub select_chain: SC,
76+
/// BABE specific dependencies.
77+
pub babe: BabeDeps,
78+
}
79+
80+
/// Instantiate all Full RPC extensions.
81+
pub fn create_full<C, P, M, SC>(
82+
deps: FullDeps<C, P, SC>,
6483
) -> jsonrpc_core::IoHandler<M> where
6584
C: ProvideRuntimeApi<Block>,
66-
C: sc_client::blockchain::HeaderBackend<Block>,
85+
C: HeaderBackend<Block> + HeaderMetadata<Block, Error=BlockChainError> + 'static,
6786
C: Send + Sync + 'static,
6887
C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,
6988
C::Api: pallet_contracts_rpc::ContractsRuntimeApi<Block, AccountId, Balance, BlockNumber>,
7089
C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance, UncheckedExtrinsic>,
71-
F: sc_client::light::fetcher::Fetcher<Block> + 'static,
90+
C::Api: BabeApi<Block>,
91+
<C::Api as sp_api::ApiErrorExt>::Error: fmt::Debug,
7292
P: TransactionPool + 'static,
7393
M: jsonrpc_core::Metadata + Default,
94+
SC: SelectChain<Block> +'static,
7495
{
75-
use substrate_frame_rpc_system::{FullSystem, LightSystem, SystemApi};
96+
use substrate_frame_rpc_system::{FullSystem, SystemApi};
7697
use pallet_contracts_rpc::{Contracts, ContractsApi};
7798
use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};
7899

79100
let mut io = jsonrpc_core::IoHandler::default();
101+
let FullDeps {
102+
client,
103+
pool,
104+
select_chain,
105+
babe
106+
} = deps;
107+
let BabeDeps {
108+
keystore,
109+
babe_config,
110+
shared_epoch_changes,
111+
} = babe;
112+
113+
io.extend_with(
114+
SystemApi::to_delegate(FullSystem::new(client.clone(), pool))
115+
);
116+
// Making synchronous calls in light client freezes the browser currently,
117+
// more context: https://github.com/paritytech/substrate/pull/3480
118+
// These RPCs should use an asynchronous caller instead.
119+
io.extend_with(
120+
ContractsApi::to_delegate(Contracts::new(client.clone()))
121+
);
122+
io.extend_with(
123+
TransactionPaymentApi::to_delegate(TransactionPayment::new(client.clone()))
124+
);
125+
io.extend_with(
126+
sc_consensus_babe_rpc::BabeApi::to_delegate(
127+
BabeRPCHandler::new(client, shared_epoch_changes, keystore, babe_config, select_chain)
128+
)
129+
);
130+
131+
io
132+
}
133+
134+
/// Instantiate all Light RPC extensions.
135+
pub fn create_light<C, P, M, F>(
136+
deps: LightDeps<C, F, P>,
137+
) -> jsonrpc_core::IoHandler<M> where
138+
C: sc_client::blockchain::HeaderBackend<Block>,
139+
C: Send + Sync + 'static,
140+
F: sc_client::light::fetcher::Fetcher<Block> + 'static,
141+
P: TransactionPool + 'static,
142+
M: jsonrpc_core::Metadata + Default,
143+
{
144+
use substrate_frame_rpc_system::{LightSystem, SystemApi};
145+
146+
let LightDeps {
147+
client,
148+
pool,
149+
remote_blockchain,
150+
fetcher
151+
} = deps;
152+
let mut io = jsonrpc_core::IoHandler::default();
153+
io.extend_with(
154+
SystemApi::<AccountId, Index>::to_delegate(LightSystem::new(client, remote_blockchain, fetcher, pool))
155+
);
80156

81-
if let Some(LightDeps { remote_blockchain, fetcher }) = light_deps {
82-
io.extend_with(
83-
SystemApi::<AccountId, Index>::to_delegate(LightSystem::new(client, remote_blockchain, fetcher, pool))
84-
);
85-
} else {
86-
io.extend_with(
87-
SystemApi::to_delegate(FullSystem::new(client.clone(), pool))
88-
);
89-
90-
// Making synchronous calls in light client freezes the browser currently,
91-
// more context: https://github.com/paritytech/substrate/pull/3480
92-
// These RPCs should use an asynchronous caller instead.
93-
io.extend_with(
94-
ContractsApi::to_delegate(Contracts::new(client.clone()))
95-
);
96-
io.extend_with(
97-
TransactionPaymentApi::to_delegate(TransactionPayment::new(client))
98-
);
99-
}
100157
io
101158
}

bin/node/runtime/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
8383
// implementation changes and behavior does not, then leave spec_version as
8484
// is and increment impl_version.
8585
spec_version: 220,
86-
impl_version: 0,
86+
impl_version: 1,
8787
apis: RUNTIME_API_VERSIONS,
8888
};
8989

@@ -737,6 +737,10 @@ impl_runtime_apis! {
737737
secondary_slots: true,
738738
}
739739
}
740+
741+
fn current_epoch_start() -> sp_consensus_babe::SlotNumber {
742+
Babe::current_epoch_start()
743+
}
740744
}
741745

742746
impl sp_authority_discovery::AuthorityDiscoveryApi<Block> for Runtime {

client/consensus/babe/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ sp-application-crypto = { version = "2.0.0", path = "../../../primitives/applica
1414
num-bigint = "0.2.3"
1515
num-rational = "0.2.2"
1616
num-traits = "0.2.8"
17+
serde = { version = "1.0.104", features = ["derive"] }
1718
sp-version = { version = "2.0.0", path = "../../../primitives/version" }
1819
sp-io = { version = "2.0.0", path = "../../../primitives/io" }
1920
sp-inherents = { version = "2.0.0", path = "../../../primitives/inherents" }
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[package]
2+
name = "sc-consensus-babe-rpc"
3+
version = "0.8.0"
4+
authors = ["Parity Technologies <[email protected]>"]
5+
description = "RPC extensions for the BABE consensus algorithm"
6+
edition = "2018"
7+
license = "GPL-3.0"
8+
9+
[dependencies]
10+
sc-consensus-babe = { version = "0.8.0", path = "../" }
11+
jsonrpc-core = "14.0.3"
12+
jsonrpc-core-client = "14.0.3"
13+
jsonrpc-derive = "14.0.3"
14+
sp-consensus-babe = { version = "0.8", path = "../../../../primitives/consensus/babe" }
15+
serde = { version = "1.0.104", features=["derive"] }
16+
sp-blockchain = { version = "2.0.0", path = "../../../../primitives/blockchain" }
17+
sp-runtime = { version = "2.0.0", path = "../../../../primitives/runtime" }
18+
sc-consensus-epochs = { version = "0.8", path = "../../epochs" }
19+
futures = "0.3.1"
20+
derive_more = "0.99.2"
21+
sp-api = { version = "2.0.0", path = "../../../../primitives/api" }
22+
sp-consensus = { version = "0.8", path = "../../../../primitives/consensus/common" }
23+
sp-core = { version = "2.0.0", path = "../../../../primitives/core" }
24+
sc-keystore = { version = "2.0.0", path = "../../../keystore" }
25+
26+
[dev-dependencies]
27+
substrate-test-runtime-client = { version = "2.0.0", path = "../../../../test-utils/runtime/client" }
28+
sp-application-crypto = { version = "2.0.0", path = "../../../../primitives/application-crypto" }
29+
sp-keyring = { version = "2.0.0", path = "../../../../primitives/keyring" }
30+
tempfile = "3.1.0"

0 commit comments

Comments
 (0)