diff --git a/Cargo.lock b/Cargo.lock index 283f7c77a368f..525c2fe030f4d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/Cargo.toml b/Cargo.toml index 782cdcd23a4e6..d3004fcadca5a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,7 @@ members = [ "client/executor/runtime-test", "client/finality-grandpa", "client/informant", + "client/light", "client/tracing", "client/keystore", "client/network", diff --git a/client/light/Cargo.toml b/client/light/Cargo.toml new file mode 100644 index 0000000000000..1ef35f72acc43 --- /dev/null +++ b/client/light/Cargo.toml @@ -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 "] +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 = [] diff --git a/client/service/src/client/light/backend.rs b/client/light/src/backend.rs similarity index 100% rename from client/service/src/client/light/backend.rs rename to client/light/src/backend.rs diff --git a/client/service/src/client/light/blockchain.rs b/client/light/src/blockchain.rs similarity index 97% rename from client/service/src/client/light/blockchain.rs rename to client/light/src/blockchain.rs index c7b20de594d67..9d557db887d29 100644 --- a/client/service/src/client/light/blockchain.rs +++ b/client/light/src/blockchain.rs @@ -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 { diff --git a/client/service/src/client/light/call_executor.rs b/client/light/src/call_executor.rs similarity index 100% rename from client/service/src/client/light/call_executor.rs rename to client/light/src/call_executor.rs diff --git a/client/service/src/client/light/fetcher.rs b/client/light/src/fetcher.rs similarity index 99% rename from client/service/src/client/light/fetcher.rs rename to client/light/src/fetcher.rs index 542255496739a..88d20cafc903f 100644 --- a/client/service/src/client/light/fetcher.rs +++ b/client/light/src/fetcher.rs @@ -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> { diff --git a/client/light/src/lib.rs b/client/light/src/lib.rs new file mode 100644 index 0000000000000..deea642bd39d0 --- /dev/null +++ b/client/light/src/lib.rs @@ -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 . + +//! 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>( + blockchain: Arc>, + executor: E, + spawn_handle: Box, +) -> LightDataChecker, B, S> + where + E: CodeExecutor, +{ + LightDataChecker::new(blockchain, executor, spawn_handle) +} + +/// Create an instance of light client blockchain backend. +pub fn new_light_blockchain>(storage: S) -> Arc> { + Arc::new(Blockchain::new(storage)) +} + +/// Create an instance of light client backend. +pub fn new_light_backend(blockchain: Arc>) -> Arc>> + where + B: BlockT, + S: BlockchainStorage, +{ + Arc::new(Backend::new(blockchain)) +} diff --git a/client/service/Cargo.toml b/client/service/Cargo.toml index 74cd71a698d51..f3687a2b8a660 100644 --- a/client/service/Cargo.toml +++ b/client/service/Cargo.toml @@ -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" } diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index aa680c3bcef0f..6e88042e367f0 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -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 = Client< >; /// Light client backend type. -pub type TLightBackend = crate::client::light::backend::Backend< +pub type TLightBackend = sc_light::Backend< sc_client_db::light::LightStorage, HashFor, >; /// Light call executor type. -pub type TLightCallExecutor = crate::client::light::call_executor::GenesisCallExecutor< - crate::client::light::backend::Backend< +pub type TLightCallExecutor = sc_light::GenesisCallExecutor< + sc_light::Backend< sc_client_db::light::LightStorage, HashFor >, crate::client::LocalCallExecutor< - crate::client::light::backend::Backend< + sc_light::Backend< sc_client_db::light::LightStorage, HashFor >, @@ -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, diff --git a/client/service/src/client/client.rs b/client/service/src/client/client.rs index a3d2489fd0850..99ad8b689e6de 100644 --- a/client/service/src/client/client.rs +++ b/client/service/src/client/client.rs @@ -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")] diff --git a/client/service/src/client/light/mod.rs b/client/service/src/client/light.rs similarity index 63% rename from client/service/src/client/light/mod.rs rename to client/service/src/client/light.rs index a3456f96a3786..8b9b65fc2fadd 100644 --- a/client/service/src/client/light/mod.rs +++ b/client/service/src/client/light.rs @@ -16,12 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -//! 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; - -/// Create an instance of light client blockchain backend. -pub fn new_light_blockchain>(storage: S) -> Arc> { - Arc::new(Blockchain::new(storage)) -} +use sc_light::{Backend, GenesisCallExecutor}; -/// Create an instance of light client backend. -pub fn new_light_backend(blockchain: Arc>) -> Arc>> - where - B: BlockT, - S: BlockchainStorage, -{ - Arc::new(Backend::new(blockchain)) -} /// Create an instance of light client. pub fn new_light( @@ -79,7 +58,12 @@ pub fn new_light( S: BlockchainStorage + '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( ClientConfig::default(), ) } - -/// Create an instance of fetch data checker. -pub fn new_fetch_checker>( - blockchain: Arc>, - executor: E, - spawn_handle: Box, -) -> LightDataChecker, B, S> - where - E: CodeExecutor, -{ - LightDataChecker::new(blockchain, executor, spawn_handle) -} diff --git a/client/service/test/Cargo.toml b/client/service/test/Cargo.toml index a887c24a87959..5835dc14c95dd 100644 --- a/client/service/test/Cargo.toml +++ b/client/service/test/Cargo.toml @@ -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" } diff --git a/client/service/test/src/client/light.rs b/client/service/test/src/client/light.rs index ec319e4832fdb..994d846c6a088 100644 --- a/client/service/test/src/client/light.rs +++ b/client/service/test/src/client/light.rs @@ -16,7 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use sc_service::client::light::{ +use sc_light::{ call_executor::{ GenesisCallExecutor, check_execution_proof, diff --git a/test-utils/client/Cargo.toml b/test-utils/client/Cargo.toml index a6924e4f27409..331c7e2801b6a 100644 --- a/test-utils/client/Cargo.toml +++ b/test-utils/client/Cargo.toml @@ -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" } diff --git a/test-utils/client/src/lib.rs b/test-utils/client/src/lib.rs index ffd93970f41da..2ab9e4066ddd1 100644 --- a/test-utils/client/src/lib.rs +++ b/test-utils/client/src/lib.rs @@ -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 = client::light::backend::Backend< +pub type LightBackend = sc_light::Backend< sc_client_db::light::LightStorage, BlakeTwo256, >; diff --git a/test-utils/runtime/client/Cargo.toml b/test-utils/runtime/client/Cargo.toml index d59a5f1fdaa36..1b41b63b99def 100644 --- a/test-utils/runtime/client/Cargo.toml +++ b/test-utils/runtime/client/Cargo.toml @@ -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" } diff --git a/test-utils/runtime/client/src/lib.rs b/test-utils/runtime/client/src/lib.rs index f2e049bc0f56b..4e9034fb4d493 100644 --- a/test-utils/runtime/client/src/lib.rs +++ b/test-utils/runtime/client/src/lib.rs @@ -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; /// 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, HashFor >, @@ -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());