Skip to content

Commit 364f731

Browse files
gnunicornarkpar
authored andcommitted
removes use of sc_client::Client from sc-rpc (paritytech#5063)
* removes use of sc_client::Client from sc-rpc * remove Client impl from sc-finality-benches * remove client impl from sc-finality-grandpa * read_proof accepts iterator * remove generic Executor param from ExecutorProvider * fix long ass line * code style changes * merge with master Co-authored-by: Arkadiy Paronyan <[email protected]>
1 parent b2350c8 commit 364f731

File tree

34 files changed

+739
-544
lines changed

34 files changed

+739
-544
lines changed

Cargo.lock

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

bin/node-template/node/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ sp-consensus = { version = "0.8.0-alpha.2", path = "../../../primitives/consensu
3030
grandpa = { version = "0.8.0-alpha.2", package = "sc-finality-grandpa", path = "../../../client/finality-grandpa" }
3131
grandpa-primitives = { version = "2.0.0-alpha.2", package = "sp-finality-grandpa", path = "../../../primitives/finality-grandpa" }
3232
sc-client = { version = "0.8.0-alpha.2", path = "../../../client/" }
33+
sc-client-api = { version = "2.0.0-alpha.2", path = "../../../client/api" }
3334
sp-runtime = { version = "2.0.0-alpha.2", path = "../../../primitives/runtime" }
3435
sc-basic-authorship = { path = "../../../client/basic-authorship" , version = "0.8.0-alpha.2"}
3536

bin/node-template/node/src/service.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
use std::sync::Arc;
44
use std::time::Duration;
55
use sc_client::LongestChain;
6+
use sc_client_api::ExecutorProvider;
67
use node_template_runtime::{self, GenesisConfig, opaque::Block, RuntimeApi};
78
use sc_service::{error::{Error as ServiceError}, AbstractService, Configuration, ServiceBuilder};
89
use sp_inherents::InherentDataProviders;
910
use sc_executor::native_executor_instance;
1011
pub use sc_executor::NativeExecutor;
1112
use sp_consensus_aura::sr25519::{AuthorityPair as AuraPair};
12-
use grandpa::{self, FinalityProofProvider as GrandpaFinalityProofProvider};
13+
use grandpa::{self, FinalityProofProvider as GrandpaFinalityProofProvider, StorageAndProofProvider};
1314

1415
// Our native executor instance.
1516
native_executor_instance!(
@@ -24,6 +25,7 @@ native_executor_instance!(
2425
/// be able to perform chain operations.
2526
macro_rules! new_full_start {
2627
($config:expr) => {{
28+
use std::sync::Arc;
2729
let mut import_setup = None;
2830
let inherent_data_providers = sp_inherents::InherentDataProviders::new();
2931

@@ -42,7 +44,7 @@ macro_rules! new_full_start {
4244
.ok_or_else(|| sc_service::Error::SelectChainRequired)?;
4345

4446
let (grandpa_block_import, grandpa_link) =
45-
grandpa::block_import(client.clone(), &*client, select_chain)?;
47+
grandpa::block_import(client.clone(), &(client.clone() as Arc<_>), select_chain)?;
4648

4749
let aura_block_import = sc_consensus_aura::AuraBlockImport::<_, _, _, AuraPair>::new(
4850
grandpa_block_import.clone(), client.clone(),
@@ -87,9 +89,11 @@ pub fn new_full(config: Configuration<GenesisConfig>)
8789
.expect("Link Half and Block Import are present for Full Services or setup failed before. qed");
8890

8991
let service = builder
90-
.with_finality_proof_provider(|client, backend|
91-
Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, client)) as _)
92-
)?
92+
.with_finality_proof_provider(|client, backend| {
93+
// GenesisAuthoritySetProvider is implemented for StorageAndProofProvider
94+
let provider = client as Arc<dyn StorageAndProofProvider<_, _>>;
95+
Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, provider)) as _)
96+
})?
9397
.build()?;
9498

9599
if participates_in_consensus {
@@ -201,7 +205,10 @@ pub fn new_light(config: Configuration<GenesisConfig>)
201205
.map(|fetcher| fetcher.checker().clone())
202206
.ok_or_else(|| "Trying to start light import queue without active fetch checker")?;
203207
let grandpa_block_import = grandpa::light_block_import(
204-
client.clone(), backend, &*client.clone(), Arc::new(fetch_checker),
208+
client.clone(),
209+
backend,
210+
&(client.clone() as Arc<_>),
211+
Arc::new(fetch_checker),
205212
)?;
206213
let finality_proof_import = grandpa_block_import.clone();
207214
let finality_proof_request_builder =
@@ -218,8 +225,10 @@ pub fn new_light(config: Configuration<GenesisConfig>)
218225

219226
Ok((import_queue, finality_proof_request_builder))
220227
})?
221-
.with_finality_proof_provider(|client, backend|
222-
Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, client)) as _)
223-
)?
228+
.with_finality_proof_provider(|client, backend| {
229+
// GenesisAuthoritySetProvider is implemented for StorageAndProofProvider
230+
let provider = client as Arc<dyn StorageAndProofProvider<_, _>>;
231+
Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, provider)) as _)
232+
})?
224233
.build()
225234
}

bin/node/cli/src/service.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::sync::Arc;
2222

2323
use sc_consensus_babe;
2424
use sc_client::{self, LongestChain};
25-
use grandpa::{self, FinalityProofProvider as GrandpaFinalityProofProvider};
25+
use grandpa::{self, FinalityProofProvider as GrandpaFinalityProofProvider, StorageAndProofProvider};
2626
use node_executor;
2727
use node_primitives::Block;
2828
use node_runtime::{GenesisConfig, RuntimeApi};
@@ -45,6 +45,7 @@ use sc_offchain::OffchainWorkers;
4545
/// be able to perform chain operations.
4646
macro_rules! new_full_start {
4747
($config:expr) => {{
48+
use std::sync::Arc;
4849
type RpcExtension = jsonrpc_core::IoHandler<sc_rpc::Metadata>;
4950
let mut import_setup = None;
5051
let inherent_data_providers = sp_inherents::InherentDataProviders::new();
@@ -64,7 +65,7 @@ macro_rules! new_full_start {
6465
.ok_or_else(|| sc_service::Error::SelectChainRequired)?;
6566
let (grandpa_block_import, grandpa_link) = grandpa::block_import(
6667
client.clone(),
67-
&*client,
68+
&(client.clone() as Arc<_>),
6869
select_chain,
6970
)?;
7071
let justification_import = grandpa_block_import.clone();
@@ -116,6 +117,7 @@ macro_rules! new_full {
116117
($config:expr, $with_startup_data: expr) => {{
117118
use futures::prelude::*;
118119
use sc_network::Event;
120+
use sc_client_api::ExecutorProvider;
119121

120122
let (
121123
is_authority,
@@ -139,9 +141,11 @@ macro_rules! new_full {
139141
let (builder, mut import_setup, inherent_data_providers) = new_full_start!($config);
140142

141143
let service = builder
142-
.with_finality_proof_provider(|client, backend|
143-
Ok(Arc::new(grandpa::FinalityProofProvider::new(backend, client)) as _)
144-
)?
144+
.with_finality_proof_provider(|client, backend| {
145+
// GenesisAuthoritySetProvider is implemented for StorageAndProofProvider
146+
let provider = client as Arc<dyn grandpa::StorageAndProofProvider<_, _>>;
147+
Ok(Arc::new(grandpa::FinalityProofProvider::new(backend, provider)) as _)
148+
})?
145149
.build()?;
146150

147151
let (block_import, grandpa_link, babe_link) = import_setup.take()
@@ -255,8 +259,7 @@ type ConcreteBlock = node_primitives::Block;
255259
type ConcreteClient =
256260
Client<
257261
Backend<ConcreteBlock>,
258-
LocalCallExecutor<Backend<ConcreteBlock>,
259-
NativeExecutor<node_executor::Executor>>,
262+
LocalCallExecutor<Backend<ConcreteBlock>, NativeExecutor<node_executor::Executor>>,
260263
ConcreteBlock,
261264
node_runtime::RuntimeApi
262265
>;
@@ -317,7 +320,7 @@ pub fn new_light(config: NodeConfiguration)
317320
let grandpa_block_import = grandpa::light_block_import(
318321
client.clone(),
319322
backend,
320-
&*client,
323+
&(client.clone() as Arc<_>),
321324
Arc::new(fetch_checker),
322325
)?;
323326

@@ -342,9 +345,11 @@ pub fn new_light(config: NodeConfiguration)
342345

343346
Ok((import_queue, finality_proof_request_builder))
344347
})?
345-
.with_finality_proof_provider(|client, backend|
346-
Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, client)) as _)
347-
)?
348+
.with_finality_proof_provider(|client, backend| {
349+
// GenesisAuthoritySetProvider is implemented for StorageAndProofProvider
350+
let provider = client as Arc<dyn StorageAndProofProvider<_, _>>;
351+
Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, provider)) as _)
352+
})?
348353
.with_rpc_extensions(|builder,| ->
349354
Result<RpcExtension, _>
350355
{

client/api/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ sp-runtime = { version = "2.0.0-alpha.2", default-features = false, path = "../.
3434
sp-state-machine = { version = "0.8.0-alpha.2", path = "../../primitives/state-machine" }
3535
sc-telemetry = { version = "2.0.0-alpha.2", path = "../telemetry" }
3636
sp-trie = { version = "2.0.0-alpha.2", path = "../../primitives/trie" }
37+
sp-storage = { version = "2.0.0-alpha.2", path = "../../primitives/storage" }
3738
sp-transaction-pool = { version = "2.0.0-alpha.2", path = "../../primitives/transaction-pool" }
3839

3940
[dev-dependencies]

client/api/src/backend.rs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use sp_state_machine::{
2626
ChangesTrieState, ChangesTrieStorage as StateChangesTrieStorage, ChangesTrieTransaction,
2727
StorageCollection, ChildStorageCollection,
2828
};
29+
use sp_storage::{StorageData, StorageKey, ChildInfo};
2930
use crate::{
3031
blockchain::{
3132
Backend as BlockchainBackend, well_known_cache_keys
@@ -38,6 +39,7 @@ use sp_consensus::BlockOrigin;
3839
use parking_lot::RwLock;
3940

4041
pub use sp_state_machine::Backend as StateBackend;
42+
use std::marker::PhantomData;
4143

4244
/// Extracts the state backend type for the given backend.
4345
pub type StateBackendFor<B, Block> = <B as Backend<Block>>::State;
@@ -237,6 +239,123 @@ pub trait AuxStore {
237239
fn get_aux(&self, key: &[u8]) -> sp_blockchain::Result<Option<Vec<u8>>>;
238240
}
239241

242+
/// An `Iterator` that iterates keys in a given block under a prefix.
243+
pub struct KeyIterator<'a, State, Block> {
244+
state: State,
245+
prefix: Option<&'a StorageKey>,
246+
current_key: Vec<u8>,
247+
_phantom: PhantomData<Block>,
248+
}
249+
250+
impl <'a, State, Block> KeyIterator<'a, State, Block> {
251+
/// create a KeyIterator instance
252+
pub fn new(state: State, prefix: Option<&'a StorageKey>, current_key: Vec<u8>) -> Self {
253+
Self {
254+
state,
255+
prefix,
256+
current_key,
257+
_phantom: PhantomData,
258+
}
259+
}
260+
}
261+
262+
impl<'a, State, Block> Iterator for KeyIterator<'a, State, Block> where
263+
Block: BlockT,
264+
State: StateBackend<HashFor<Block>>,
265+
{
266+
type Item = StorageKey;
267+
268+
fn next(&mut self) -> Option<Self::Item> {
269+
let next_key = self.state
270+
.next_storage_key(&self.current_key)
271+
.ok()
272+
.flatten()?;
273+
// this terminates the iterator the first time it fails.
274+
if let Some(prefix) = self.prefix {
275+
if !next_key.starts_with(&prefix.0[..]) {
276+
return None;
277+
}
278+
}
279+
self.current_key = next_key.clone();
280+
Some(StorageKey(next_key))
281+
}
282+
}
283+
/// Provides acess to storage primitives
284+
pub trait StorageProvider<Block: BlockT, B: Backend<Block>> {
285+
/// Given a `BlockId` and a key, return the value under the key in that block.
286+
fn storage(&self, id: &BlockId<Block>, key: &StorageKey) -> sp_blockchain::Result<Option<StorageData>>;
287+
288+
/// Given a `BlockId` and a key prefix, return the matching storage keys in that block.
289+
fn storage_keys(&self, id: &BlockId<Block>, key_prefix: &StorageKey) -> sp_blockchain::Result<Vec<StorageKey>>;
290+
291+
/// Given a `BlockId` and a key, return the value under the hash in that block.
292+
fn storage_hash(&self, id: &BlockId<Block>, key: &StorageKey) -> sp_blockchain::Result<Option<Block::Hash>>;
293+
294+
/// Given a `BlockId` and a key prefix, return the matching child storage keys and values in that block.
295+
fn storage_pairs(
296+
&self,
297+
id: &BlockId<Block>,
298+
key_prefix: &StorageKey
299+
) -> sp_blockchain::Result<Vec<(StorageKey, StorageData)>>;
300+
301+
/// Given a `BlockId` and a key prefix, return a `KeyIterator` iterates matching storage keys in that block.
302+
fn storage_keys_iter<'a>(
303+
&self,
304+
id: &BlockId<Block>,
305+
prefix: Option<&'a StorageKey>,
306+
start_key: Option<&StorageKey>
307+
) -> sp_blockchain::Result<KeyIterator<'a, B::State, Block>>;
308+
309+
/// Given a `BlockId`, a key and a child storage key, return the value under the key in that block.
310+
fn child_storage(
311+
&self,
312+
id: &BlockId<Block>,
313+
storage_key: &StorageKey,
314+
child_info: ChildInfo,
315+
key: &StorageKey
316+
) -> sp_blockchain::Result<Option<StorageData>>;
317+
318+
/// Given a `BlockId`, a key prefix, and a child storage key, return the matching child storage keys.
319+
fn child_storage_keys(
320+
&self,
321+
id: &BlockId<Block>,
322+
child_storage_key: &StorageKey,
323+
child_info: ChildInfo,
324+
key_prefix: &StorageKey
325+
) -> sp_blockchain::Result<Vec<StorageKey>>;
326+
327+
/// Given a `BlockId`, a key and a child storage key, return the hash under the key in that block.
328+
fn child_storage_hash(
329+
&self,
330+
id: &BlockId<Block>,
331+
storage_key: &StorageKey,
332+
child_info: ChildInfo,
333+
key: &StorageKey
334+
) -> sp_blockchain::Result<Option<Block::Hash>>;
335+
336+
/// Get longest range within [first; last] that is possible to use in `key_changes`
337+
/// and `key_changes_proof` calls.
338+
/// Range could be shortened from the beginning if some changes tries have been pruned.
339+
/// Returns Ok(None) if changes tries are not supported.
340+
fn max_key_changes_range(
341+
&self,
342+
first: NumberFor<Block>,
343+
last: BlockId<Block>,
344+
) -> sp_blockchain::Result<Option<(NumberFor<Block>, BlockId<Block>)>>;
345+
346+
/// Get pairs of (block, extrinsic) where key has been changed at given blocks range.
347+
/// Works only for runtimes that are supporting changes tries.
348+
///
349+
/// Changes are returned in descending order (i.e. last block comes first).
350+
fn key_changes(
351+
&self,
352+
first: NumberFor<Block>,
353+
last: BlockId<Block>,
354+
storage_key: Option<&StorageKey>,
355+
key: &StorageKey
356+
) -> sp_blockchain::Result<Vec<(NumberFor<Block>, u32)>>;
357+
}
358+
240359
/// Client backend.
241360
///
242361
/// Manages the data layer.

client/api/src/call_executor.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ use sp_externalities::Extensions;
2929
use sp_core::NativeOrEncoded;
3030

3131
use sp_api::{ProofRecorder, InitializeBlock, StorageTransactionCache};
32+
use crate::execution_extensions::ExecutionExtensions;
33+
34+
/// Executor Provider
35+
pub trait ExecutorProvider<Block: BlockT> {
36+
/// executor instance
37+
type Executor: CallExecutor<Block>;
38+
/// Get call executor reference.
39+
fn executor(&self) -> &Self::Executor;
40+
41+
/// Get a reference to the execution extensions.
42+
fn execution_extensions(&self) -> &ExecutionExtensions<Block>;
43+
}
3244

3345
/// Method call executor.
3446
pub trait CallExecutor<B: BlockT> {

client/api/src/client.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use futures::channel::mpsc;
2121
use sp_core::storage::StorageKey;
2222
use sp_runtime::{
2323
traits::{Block as BlockT, NumberFor},
24-
generic::BlockId
24+
generic::{BlockId, SignedBlock}
2525
};
2626
use sp_consensus::BlockOrigin;
2727

@@ -76,9 +76,13 @@ pub trait BlockchainEvents<Block: BlockT> {
7676
/// Fetch block body by ID.
7777
pub trait BlockBody<Block: BlockT> {
7878
/// Get block body by ID. Returns `None` if the body is not stored.
79-
fn block_body(&self,
79+
fn block_body(
80+
&self,
8081
id: &BlockId<Block>
8182
) -> sp_blockchain::Result<Option<Vec<<Block as BlockT>::Extrinsic>>>;
83+
84+
/// Get full block by id.
85+
fn block(&self, id: &BlockId<Block>) -> sp_blockchain::Result<Option<SignedBlock<Block>>>;
8286
}
8387

8488
/// Provide a list of potential uncle headers for a given block.

client/api/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub mod client;
2323
pub mod execution_extensions;
2424
pub mod light;
2525
pub mod notifications;
26+
pub mod proof_provider;
2627

2728
pub use sp_blockchain as blockchain;
2829
pub use backend::*;
@@ -31,6 +32,7 @@ pub use call_executor::*;
3132
pub use client::*;
3233
pub use light::*;
3334
pub use notifications::*;
35+
pub use proof_provider::*;
3436

3537
pub use sp_state_machine::{StorageProof, ExecutionStrategy};
3638

0 commit comments

Comments
 (0)