new crate sc-light (#6235)

* sc-light

* remove unused deps

* fix line width

* move more fns to sc_light
This commit is contained in:
Seun Lanlege
2020-06-09 15:35:35 +01:00
committed by GitHub
parent d29baf9945
commit b0aa4cfe0d
18 changed files with 145 additions and 63 deletions
+22
View File
@@ -6337,6 +6337,24 @@ dependencies = [
"tempfile",
]
[[package]]
name = "sc-light"
version = "2.0.0-rc3"
dependencies = [
"hash-db",
"lazy_static",
"parity-scale-codec",
"parking_lot 0.10.2",
"sc-client-api",
"sc-executor",
"sp-api",
"sp-blockchain",
"sp-core",
"sp-externalities",
"sp-runtime",
"sp-state-machine",
]
[[package]]
name = "sc-network"
version = "0.8.0-rc3"
@@ -6613,6 +6631,7 @@ dependencies = [
"sc-finality-grandpa",
"sc-informant",
"sc-keystore",
"sc-light",
"sc-network",
"sc-offchain",
"sc-rpc",
@@ -6663,6 +6682,7 @@ dependencies = [
"sc-client-api",
"sc-client-db",
"sc-executor",
"sc-light",
"sc-network",
"sc-service",
"sp-api",
@@ -8069,6 +8089,7 @@ dependencies = [
"sc-client-db",
"sc-consensus",
"sc-executor",
"sc-light",
"sc-service",
"sp-blockchain",
"sp-consensus",
@@ -8130,6 +8151,7 @@ dependencies = [
"sc-block-builder",
"sc-client-api",
"sc-consensus",
"sc-light",
"sc-service",
"sp-api",
"sp-blockchain",
+1
View File
@@ -38,6 +38,7 @@ members = [
"client/executor/runtime-test",
"client/finality-grandpa",
"client/informant",
"client/light",
"client/tracing",
"client/keystore",
"client/network",
+27
View File
@@ -0,0 +1,27 @@
[package]
description = "components for a light client"
name = "sc-light"
version = "2.0.0-rc3"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
homepage = "https://substrate.dev"
repository = "https://github.com/paritytech/substrate/"
documentation = "https://docs.rs/sc-light"
[dependencies]
parking_lot = "0.10.0"
lazy_static = "1.4.0"
hash-db = "0.15.2"
sp-runtime = { version = "2.0.0-rc2", path = "../../primitives/runtime" }
sp-externalities = { version = "0.8.0-rc2", path = "../../primitives/externalities" }
sp-blockchain = { version = "2.0.0-rc2", path = "../../primitives/blockchain" }
sp-core = { version = "2.0.0-rc2", path = "../../primitives/core" }
sp-state-machine = { version = "0.8.0-rc2", path = "../../primitives/state-machine" }
sc-client-api = { version = "2.0.0-rc2", path = "../api" }
sp-api = { version = "2.0.0-rc2", path = "../../primitives/api" }
codec = { package = "parity-scale-codec", version = "1.3.0" }
sc-executor = { version = "0.8.0-rc2", path = "../executor" }
[features]
default = []
@@ -25,8 +25,7 @@ use sp_runtime::{Justification, generic::BlockId};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, NumberFor, Zero};
use sp_blockchain::{
HeaderMetadata, CachedHeaderMetadata,
Error as ClientError, Result as ClientResult,
HeaderMetadata, CachedHeaderMetadata, Error as ClientError, Result as ClientResult,
};
pub use sc_client_api::{
backend::{
@@ -42,7 +41,7 @@ pub use sc_client_api::{
},
cht,
};
use super::fetcher::RemoteHeaderRequest;
use crate::fetcher::RemoteHeaderRequest;
/// Light client blockchain.
pub struct Blockchain<S> {
@@ -46,8 +46,8 @@ pub use sc_client_api::{
},
cht,
};
use super::blockchain::{Blockchain};
use super::call_executor::check_execution_proof;
use crate::blockchain::Blockchain;
use crate::call_executor::check_execution_proof;
/// Remote data checker.
pub struct LightDataChecker<E, H, B: BlockT, S: BlockchainStorage<B>> {
+57
View File
@@ -0,0 +1,57 @@
// This file is part of Substrate.
// Copyright (C) 2017-2020 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Light client components.
use sp_runtime::traits::{Block as BlockT, HashFor};
use sc_client_api::CloneableSpawn;
use std::sync::Arc;
use sp_core::traits::CodeExecutor;
pub mod backend;
pub mod blockchain;
pub mod call_executor;
pub mod fetcher;
pub use {backend::*, blockchain::*, call_executor::*, fetcher::*};
/// Create an instance of fetch data checker.
pub fn new_fetch_checker<E, B: BlockT, S: BlockchainStorage<B>>(
blockchain: Arc<Blockchain<S>>,
executor: E,
spawn_handle: Box<dyn CloneableSpawn>,
) -> LightDataChecker<E, HashFor<B>, B, S>
where
E: CodeExecutor,
{
LightDataChecker::new(blockchain, executor, spawn_handle)
}
/// Create an instance of light client blockchain backend.
pub fn new_light_blockchain<B: BlockT, S: BlockchainStorage<B>>(storage: S) -> Arc<Blockchain<S>> {
Arc::new(Blockchain::new(storage))
}
/// Create an instance of light client backend.
pub fn new_light_backend<B, S>(blockchain: Arc<Blockchain<S>>) -> Arc<Backend<S, HashFor<B>>>
where
B: BlockT,
S: BlockchainStorage<B>,
{
Arc::new(Backend::new(blockchain))
}
+1
View File
@@ -55,6 +55,7 @@ sp-application-crypto = { version = "2.0.0-rc3", path = "../../primitives/applic
sp-consensus = { version = "0.8.0-rc3", path = "../../primitives/consensus/common" }
sc-network = { version = "0.8.0-rc3", path = "../network" }
sc-chain-spec = { version = "2.0.0-rc3", path = "../chain-spec" }
sc-light = { version = "2.0.0-rc3", path = "../light" }
sc-client-api = { version = "2.0.0-rc3", path = "../api" }
sp-api = { version = "2.0.0-rc3", path = "../../primitives/api" }
sc-client-db = { version = "0.8.0-rc3", default-features = false, path = "../db" }
+13 -12
View File
@@ -19,13 +19,14 @@
use crate::{
Service, NetworkStatus, NetworkState, error::Error, DEFAULT_PROTOCOL_ID, MallocSizeOfWasm,
start_rpc_servers, build_network_future, TransactionPoolAdapter, TaskManager, SpawnTaskHandle,
status_sinks, metrics::MetricsService, client::{Client, ClientConfig},
status_sinks, metrics::MetricsService,
client::{light, Client, ClientConfig},
config::{Configuration, KeystoreConfig, PrometheusConfig, OffchainWorkerConfig},
};
use sc_client_api::{
BlockchainEvents, backend::RemoteBackend, light::RemoteBlockchain,
execution_extensions::ExtensionsFactory, ExecutorProvider, CallExecutor, ForkBlocks, BadBlocks,
CloneableSpawn, UsageProvider,
self, BlockchainEvents, light::RemoteBlockchain, execution_extensions::ExtensionsFactory,
ExecutorProvider, CallExecutor, ForkBlocks, BadBlocks, CloneableSpawn, UsageProvider,
backend::RemoteBackend,
};
use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedSender};
use sc_chain_spec::get_extension;
@@ -179,19 +180,19 @@ pub type TLightClient<TBl, TRtApi, TExecDisp> = Client<
>;
/// Light client backend type.
pub type TLightBackend<TBl> = crate::client::light::backend::Backend<
pub type TLightBackend<TBl> = sc_light::Backend<
sc_client_db::light::LightStorage<TBl>,
HashFor<TBl>,
>;
/// Light call executor type.
pub type TLightCallExecutor<TBl, TExecDisp> = crate::client::light::call_executor::GenesisCallExecutor<
crate::client::light::backend::Backend<
pub type TLightCallExecutor<TBl, TExecDisp> = sc_light::GenesisCallExecutor<
sc_light::Backend<
sc_client_db::light::LightStorage<TBl>,
HashFor<TBl>
>,
crate::client::LocalCallExecutor<
crate::client::light::backend::Backend<
sc_light::Backend<
sc_client_db::light::LightStorage<TBl>,
HashFor<TBl>
>,
@@ -415,18 +416,18 @@ impl ServiceBuilder<(), (), (), (), (), (), (), (), (), (), ()> {
};
sc_client_db::light::LightStorage::new(db_settings)?
};
let light_blockchain = crate::client::light::new_light_blockchain(db_storage);
let light_blockchain = sc_light::new_light_blockchain(db_storage);
let fetch_checker = Arc::new(
crate::client::light::new_fetch_checker::<_, TBl, _>(
sc_light::new_fetch_checker::<_, TBl, _>(
light_blockchain.clone(),
executor.clone(),
Box::new(task_manager.spawn_handle()),
),
);
let fetcher = Arc::new(sc_network::config::OnDemand::new(fetch_checker));
let backend = crate::client::light::new_light_backend(light_blockchain);
let backend = sc_light::new_light_backend(light_blockchain);
let remote_blockchain = backend.remote_blockchain();
let client = Arc::new(crate::client::light::new_light(
let client = Arc::new(light::new_light(
backend.clone(),
config.chain_spec.as_storage_builder(),
executor,
@@ -84,10 +84,9 @@ use sp_utils::mpsc::{TracingUnboundedSender, tracing_unbounded};
use sp_blockchain::Error;
use prometheus_endpoint::Registry;
use super::{
genesis,
light::{call_executor::prove_execution, fetcher::ChangesProof},
block_rules::{BlockRules, LookupResult as BlockLookupResult},
genesis, block_rules::{BlockRules, LookupResult as BlockLookupResult},
};
use sc_light::{call_executor::prove_execution, fetcher::ChangesProof};
use rand::Rng;
#[cfg(feature="test-helpers")]
@@ -16,12 +16,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Light client components.
pub mod backend;
pub mod blockchain;
pub mod call_executor;
pub mod fetcher;
//! Light client utilities.
use std::sync::Arc;
@@ -37,24 +32,8 @@ use super::client::{Client,ClientConfig};
use sc_client_api::{
light::Storage as BlockchainStorage, CloneableSpawn,
};
use self::backend::Backend;
use self::blockchain::Blockchain;
use self::call_executor::GenesisCallExecutor;
use self::fetcher::LightDataChecker;
use sc_light::{Backend, GenesisCallExecutor};
/// Create an instance of light client blockchain backend.
pub fn new_light_blockchain<B: BlockT, S: BlockchainStorage<B>>(storage: S) -> Arc<Blockchain<S>> {
Arc::new(Blockchain::new(storage))
}
/// Create an instance of light client backend.
pub fn new_light_backend<B, S>(blockchain: Arc<Blockchain<S>>) -> Arc<Backend<S, HashFor<B>>>
where
B: BlockT,
S: BlockchainStorage<B>,
{
Arc::new(Backend::new(blockchain))
}
/// Create an instance of light client.
pub fn new_light<B, S, RA, E>(
@@ -79,7 +58,12 @@ pub fn new_light<B, S, RA, E>(
S: BlockchainStorage<B> + 'static,
E: CodeExecutor + RuntimeInfo + Clone + 'static,
{
let local_executor = LocalCallExecutor::new(backend.clone(), code_executor, spawn_handle.clone(), ClientConfig::default());
let local_executor = LocalCallExecutor::new(
backend.clone(),
code_executor,
spawn_handle.clone(),
ClientConfig::default()
);
let executor = GenesisCallExecutor::new(backend.clone(), local_executor);
Client::new(
backend,
@@ -92,15 +76,3 @@ pub fn new_light<B, S, RA, E>(
ClientConfig::default(),
)
}
/// Create an instance of fetch data checker.
pub fn new_fetch_checker<E, B: BlockT, S: BlockchainStorage<B>>(
blockchain: Arc<Blockchain<S>>,
executor: E,
spawn_handle: Box<dyn CloneableSpawn>,
) -> LightDataChecker<E, HashFor<B>, B, S>
where
E: CodeExecutor,
{
LightDataChecker::new(blockchain, executor, spawn_handle)
}
+1
View File
@@ -20,6 +20,7 @@ log = "0.4.8"
env_logger = "0.7.0"
fdlimit = "0.1.4"
parking_lot = "0.10.0"
sc-light = { version = "2.0.0-rc3", path = "../../light" }
sp-blockchain = { version = "2.0.0-rc3", path = "../../../primitives/blockchain" }
sp-api = { version = "2.0.0-rc3", path = "../../../primitives/api" }
sp-state-machine = { version = "0.8.0-rc3", path = "../../../primitives/state-machine" }
@@ -16,7 +16,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use sc_service::client::light::{
use sc_light::{
call_executor::{
GenesisCallExecutor,
check_execution_proof,
+1
View File
@@ -13,6 +13,7 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
sc-client-api = { version = "2.0.0-rc3", path = "../../client/api" }
sc-light = { version = "2.0.0-rc3", path = "../../client/light" }
sc-client-db = { version = "0.8.0-rc3", features = ["test-helpers"], path = "../../client/db" }
sp-consensus = { version = "0.8.0-rc3", path = "../../primitives/consensus/common" }
sc-executor = { version = "0.8.0-rc3", path = "../../client/executor" }
+1 -1
View File
@@ -46,7 +46,7 @@ use sp_runtime::traits::{Block as BlockT, BlakeTwo256};
use sc_service::client::{LocalCallExecutor, ClientConfig};
/// Test client light database backend.
pub type LightBackend<Block> = client::light::backend::Backend<
pub type LightBackend<Block> = sc_light::Backend<
sc_client_db::light::LightStorage<Block>,
BlakeTwo256,
>;
@@ -12,6 +12,7 @@ publish = false
targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
sc-light = { version = "2.0.0-rc3", path = "../../../client/light" }
sp-consensus = { version = "0.8.0-rc3", path = "../../../primitives/consensus/common" }
sc-block-builder = { version = "0.8.0-rc3", path = "../../../client/block-builder" }
substrate-test-client = { version = "2.0.0-rc3", path = "../../client" }
@@ -35,9 +35,9 @@ use sp_core::{sr25519, ChangesTrieConfiguration};
use sp_core::storage::{ChildInfo, Storage, StorageChild};
use substrate_test_runtime::genesismap::{GenesisConfig, additional_storage_with_genesis};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, Hash as HashT, NumberFor, HashFor};
use sc_service::client::light::fetcher::{
Fetcher, RemoteHeaderRequest, RemoteReadRequest, RemoteReadChildRequest,
use sc_client_api::light::{
RemoteCallRequest, RemoteChangesRequest, RemoteBodyRequest,
Fetcher, RemoteHeaderRequest, RemoteReadRequest, RemoteReadChildRequest,
};
/// A prelude to import in tests.
@@ -75,10 +75,10 @@ pub type Executor = client::LocalCallExecutor<
pub type LightBackend = substrate_test_client::LightBackend<substrate_test_runtime::Block>;
/// Test client light executor.
pub type LightExecutor = client::light::call_executor::GenesisCallExecutor<
pub type LightExecutor = sc_light::GenesisCallExecutor<
LightBackend,
client::LocalCallExecutor<
client::light::backend::Backend<
sc_light::Backend<
sc_client_db::light::LightStorage<substrate_test_runtime::Block>,
HashFor<substrate_test_runtime::Block>
>,
@@ -347,7 +347,7 @@ pub fn new_light() -> (
) {
let storage = sc_client_db::light::LightStorage::new_test();
let blockchain = Arc::new(client::light::blockchain::Blockchain::new(storage));
let blockchain = Arc::new(sc_light::Blockchain::new(storage));
let backend = Arc::new(LightBackend::new(blockchain.clone()));
let executor = new_native_executor();
let local_call_executor = client::LocalCallExecutor::new(backend.clone(), executor, sp_core::tasks::executor(), Default::default());