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

Add a LightSyncState field to the chain spec #6894

Merged
23 commits merged into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
488d0a9
Reset code, almost ready for PR
expenses Aug 14, 2020
ddc7e3e
Merge remote-tracking branch 'origin/master' into ashley-chain-spec
expenses Aug 14, 2020
15dcb28
Improved build_hardcoded_spec
expenses Aug 14, 2020
ecb2f56
Fix line widths
expenses Aug 14, 2020
837c5f7
Fix tests
expenses Aug 14, 2020
42d88d5
Fix sc-service-test
expenses Aug 14, 2020
4288dc4
Suggestions from code review
expenses Aug 14, 2020
44ff9f9
Rename to LightSyncState
expenses Aug 14, 2020
a6b5491
It's not syncing :^(
expenses Aug 17, 2020
c67d005
t Merge remote-tracking branch 'origin/master' into ashley-chain-spec…
expenses Aug 17, 2020
346b711
It syncs!
expenses Aug 17, 2020
6447b85
Remove rpc call
expenses Aug 17, 2020
1adad2e
Convert spaces to tabs
expenses Aug 17, 2020
9372667
Moved sc-service things to export_sync_state.rs
expenses Aug 17, 2020
db2da96
Fix tests
expenses Aug 17, 2020
b530147
Wait for syncing with network_status_sinks
expenses Aug 17, 2020
004a8a4
Remove sc-network from node-template
expenses Aug 17, 2020
ea62d80
Apply suggestions from code review
expenses Aug 17, 2020
9b558eb
Various changes, split the flag up into 2 pieces to make testing easier.
expenses Aug 18, 2020
fd1d2b1
Update client/cli/src/commands/build_spec_cmd.rs
expenses Aug 18, 2020
57cbb15
Merge remote-tracking branch 'origin/master' into ashley-chain-spec
expenses Aug 19, 2020
fa51717
Revert a lot of changes
expenses Aug 19, 2020
285c5af
Merge remote-tracking branch 'origin/master' into ashley-chain-spec
expenses Aug 24, 2020
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
2 changes: 2 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 bin/node-template/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ sc-consensus = { version = "0.8.0-rc5", path = "../../../client/consensus/common
sc-finality-grandpa = { version = "0.8.0-rc5", path = "../../../client/finality-grandpa" }
sp-finality-grandpa = { version = "2.0.0-rc5", path = "../../../primitives/finality-grandpa" }
sc-client-api = { version = "2.0.0-rc5", path = "../../../client/api" }
sc-network = { version = "0.8.0-rc5", path = "../../../client/network" }
sp-runtime = { version = "2.0.0-rc5", path = "../../../primitives/runtime" }

# These dependencies are used for the node template's RPCs
Expand Down
20 changes: 14 additions & 6 deletions bin/node-template/node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::cli::Cli;
use crate::service;
use sc_cli::{SubstrateCli, RuntimeVersion, Role, ChainSpec};
use sc_service::PartialComponents;
use crate::service::new_partial;
use crate::service::{new_partial, new_light_base};

impl SubstrateCli for Cli {
fn impl_name() -> String {
Expand Down Expand Up @@ -69,11 +69,19 @@ pub fn run() -> sc_cli::Result<()> {
match &cli.subcommand {
Some(subcommand) => {
let runner = cli.create_runner(subcommand)?;
runner.run_subcommand(subcommand, |config| {
let PartialComponents { client, backend, task_manager, import_queue, .. }
= new_partial(&config)?;
Ok((client, backend, import_queue, task_manager))
})
runner.run_subcommand(
subcommand,
|config| {
let PartialComponents { client, backend, task_manager, import_queue, .. }
= new_partial(&config)?;
Ok((client, backend, import_queue, task_manager))
},
|config| {
let (task_manager, client, backend, network_status_sinks)
= new_light_base(config)?;
Ok((client, backend, network_status_sinks, task_manager))
},
)
expenses marked this conversation as resolved.
Show resolved Hide resolved
}
None => {
let runner = cli.create_runner(&cli.run)?;
Expand Down
31 changes: 21 additions & 10 deletions bin/node-template/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use sc_executor::native_executor_instance;
pub use sc_executor::NativeExecutor;
use sp_consensus_aura::sr25519::{AuthorityPair as AuraPair};
use sc_finality_grandpa::{FinalityProofProvider as GrandpaFinalityProofProvider, SharedVoterState};
use sc_service::NetworkStatusSinks;

// Our native executor instance.
native_executor_instance!(
Expand All @@ -21,6 +22,8 @@ native_executor_instance!(
type FullClient = sc_service::TFullClient<Block, RuntimeApi, Executor>;
type FullBackend = sc_service::TFullBackend<Block>;
type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;
type LightClient = sc_service::TLightClient<Block, RuntimeApi, Executor>;
type LightBackend = sc_service::TLightBackend<Block>;

pub fn new_partial(config: &Configuration) -> Result<sc_service::PartialComponents<
FullClient, FullBackend, FullSelectChain,
Expand Down Expand Up @@ -220,10 +223,12 @@ pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
Ok(task_manager)
}

/// Builds a new service for a light client.
pub fn new_light(config: Configuration) -> Result<TaskManager, ServiceError> {
pub(crate) fn new_light_base(config: Configuration) -> Result<(
TaskManager,
Arc<LightClient>, Arc<LightBackend>, NetworkStatusSinks<Block>,
), ServiceError> {
let (client, backend, keystore, mut task_manager, on_demand) =
sc_service::new_light_parts::<Block, RuntimeApi, Executor>(&config)?;
sc_service::new_light_parts::<Block, RuntimeApi, Executor>(&config)?;

let transaction_pool = Arc::new(sc_transaction_pool::BasicPool::new_light(
config.transaction_pool.clone(),
Expand Down Expand Up @@ -283,15 +288,21 @@ pub fn new_light(config: Configuration) -> Result<TaskManager, ServiceError> {
rpc_extensions_builder: Box::new(|_, _| ()),
telemetry_connection_sinks: sc_service::TelemetryConnectionSinks::default(),
config,
client,
client: client.clone(),
keystore,
backend,
network,
network_status_sinks,
backend: backend.clone(),
network: network.clone(),
network_status_sinks: network_status_sinks.clone(),
system_rpc_tx,
})?;
})?;

network_starter.start_network();
network_starter.start_network();

Ok(task_manager)
Ok((task_manager, client, backend, network_status_sinks))
}

/// Builds a new service for a light client.
pub fn new_light(config: Configuration) -> Result<TaskManager, ServiceError> {
let (task_manager, ..) = new_light_base(config)?;
Ok(task_manager)
}
2 changes: 1 addition & 1 deletion bin/node/cli/src/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async fn start_inner(chain_spec: Option<String>, log_level: String) -> Result<Cl
// Create the service. This is the most heavy initialization step.
let (task_manager, rpc_handlers) =
crate::service::new_light_base(config)
.map(|(components, rpc_handlers, _, _, _)| (components, rpc_handlers))
.map(|(task_manager, _, _, rpc_handlers, ..)| (task_manager, rpc_handlers))
.map_err(|e| format!("{:?}", e))?;

Ok(browser_utils::start_client(task_manager, rpc_handlers))
Expand Down
2 changes: 1 addition & 1 deletion bin/node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ pub(crate) mod tests {
Ok(sc_service_test::TestNetComponents::new(keep_alive, client, network, transaction_pool))
},
|config| {
let (keep_alive, _, client, network, transaction_pool) = new_light_base(config)?;
let (keep_alive, client, _, _, network, transaction_pool) = new_light_base(config)?;
Ok(sc_service_test::TestNetComponents::new(keep_alive, client, network, transaction_pool))
}
);
Expand Down
20 changes: 14 additions & 6 deletions bin/node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use node_executor::Executor;
use node_runtime::{Block, RuntimeApi};
use sc_cli::{Result, SubstrateCli, RuntimeVersion, Role, ChainSpec};
use sc_service::PartialComponents;
use crate::service::new_partial;
use crate::service::{new_partial, new_light_base};

impl SubstrateCli for Cli {
fn impl_name() -> String {
Expand Down Expand Up @@ -95,11 +95,19 @@ pub fn run() -> Result<()> {
}
Some(Subcommand::Base(subcommand)) => {
let runner = cli.create_runner(subcommand)?;
runner.run_subcommand(subcommand, |config| {
let PartialComponents { client, backend, task_manager, import_queue, ..}
= new_partial(&config)?;
Ok((client, backend, import_queue, task_manager))
})
runner.run_subcommand(
subcommand,
|config| {
let PartialComponents { client, backend, task_manager, import_queue, ..}
= new_partial(&config)?;
Ok((client, backend, import_queue, task_manager))
},
|config| {
let (task_manager, client, backend, _, network_status_sinks, ..) =
new_light_base(config)?;
Ok((client, backend, network_status_sinks, task_manager))
},
)
}
}
}
19 changes: 11 additions & 8 deletions bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use node_primitives::Block;
use node_runtime::RuntimeApi;
use sc_service::{
config::{Role, Configuration}, error::{Error as ServiceError},
RpcHandlers, TaskManager,
RpcHandlers, TaskManager, NetworkStatusSinks,
};
use sp_inherents::InherentDataProviders;
use sc_network::{Event, NetworkService};
Expand All @@ -43,6 +43,7 @@ type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;
type FullGrandpaBlockImport =
grandpa::GrandpaBlockImport<FullBackend, Block, FullClient, FullSelectChain>;
type LightClient = sc_service::TLightClient<Block, RuntimeApi, Executor>;
type LightBackend = sc_service::TLightBackend<Block>;

pub fn new_partial(config: &Configuration) -> Result<sc_service::PartialComponents<
FullClient, FullBackend, FullSelectChain,
Expand Down Expand Up @@ -342,8 +343,8 @@ pub fn new_full(config: Configuration)
}

pub fn new_light_base(config: Configuration) -> Result<(
TaskManager, RpcHandlers, Arc<LightClient>,
Arc<NetworkService<Block, <Block as BlockT>::Hash>>,
TaskManager, Arc<LightClient>, Arc<LightBackend>,
RpcHandlers, NetworkStatusSinks<Block>,
Arc<sc_transaction_pool::LightPool<Block, LightClient, sc_network::config::OnDemand<Block>>>
), ServiceError> {
let (client, backend, keystore, mut task_manager, on_demand) =
Expand Down Expand Up @@ -427,19 +428,21 @@ pub fn new_light_base(config: Configuration) -> Result<(
remote_blockchain: Some(backend.remote_blockchain()),
rpc_extensions_builder: Box::new(sc_service::NoopRpcExtensionBuilder(rpc_extensions)),
client: client.clone(),
backend: backend.clone(),
transaction_pool: transaction_pool.clone(),
config, keystore, backend, network_status_sinks, system_rpc_tx,
network_status_sinks: network_status_sinks.clone(),
config, keystore, system_rpc_tx,
network: network.clone(),
telemetry_connection_sinks: sc_service::TelemetryConnectionSinks::default(),
task_manager: &mut task_manager,
})?;

Ok((task_manager, rpc_handlers, client, network, transaction_pool))
Ok((task_manager, client, backend, rpc_handlers, network_status_sinks, transaction_pool))
}

/// Builds a new service for a light client.
pub fn new_light(config: Configuration) -> Result<TaskManager, ServiceError> {
new_light_base(config).map(|(task_manager, _, _, _, _)| {
new_light_base(config).map(|(task_manager, ..)| {
Copy link
Contributor

Choose a reason for hiding this comment

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

😮 I didn't know

Copy link
Contributor

Choose a reason for hiding this comment

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

woah 🤯

task_manager
})
}
Expand Down Expand Up @@ -515,7 +518,7 @@ mod tests {
Ok((node, (inherent_data_providers, setup_handles.unwrap())))
},
|config| {
let (keep_alive, _, client, network, transaction_pool) = new_light_base(config)?;
let (keep_alive, client, _, _, network, transaction_pool) = new_light_base(config)?;
Ok(sc_service_test::TestNetComponents::new(keep_alive, client, network, transaction_pool))
},
|service, &mut (ref inherent_data_providers, (ref mut block_import, ref babe_link))| {
Expand Down Expand Up @@ -665,7 +668,7 @@ mod tests {
Ok(sc_service_test::TestNetComponents::new(keep_alive, client, network, transaction_pool))
},
|config| {
let (keep_alive, _, client, network, transaction_pool) = new_light_base(config)?;
let (keep_alive, client, _, _, network, transaction_pool) = new_light_base(config)?;
Ok(sc_service_test::TestNetComponents::new(keep_alive, client, network, transaction_pool))
},
vec![
Expand Down
18 changes: 10 additions & 8 deletions client/api/src/in_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,16 @@ impl<Block: BlockT> light::Storage<Block> for Blockchain<Block>
Blockchain::finalize_header(self, id, None)
}

fn cache(&self) -> Option<Arc<dyn blockchain::Cache<Block>>> {
None
}

fn usage_info(&self) -> Option<UsageInfo> {
None
}
}

impl<Block: BlockT> light::ChtRootStorage<Block> for Blockchain<Block> {
fn header_cht_root(
&self,
_cht_size: NumberFor<Block>,
Expand All @@ -466,14 +476,6 @@ impl<Block: BlockT> light::Storage<Block> for Blockchain<Block>
.ok_or_else(|| sp_blockchain::Error::Backend(format!("Changes trie CHT for block {} not exists", block)))
.map(Some)
}

fn cache(&self) -> Option<Arc<dyn blockchain::Cache<Block>>> {
None
}

fn usage_info(&self) -> Option<UsageInfo> {
None
}
}

/// In-memory operation.
Expand Down
19 changes: 12 additions & 7 deletions client/api/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,9 @@ pub trait FetchChecker<Block: BlockT>: Send + Sync {


/// Light client blockchain storage.
pub trait Storage<Block: BlockT>: AuxStore + HeaderBackend<Block> + HeaderMetadata<Block, Error=ClientError> {
pub trait Storage<Block: BlockT>: AuxStore + HeaderBackend<Block>
+ HeaderMetadata<Block, Error=ClientError> + ChtRootStorage<Block>
{
/// Store new header. Should refuse to revert any finalized blocks.
///
/// Takes new authorities, the leaf state of the new block, and
Expand All @@ -254,6 +256,15 @@ pub trait Storage<Block: BlockT>: AuxStore + HeaderBackend<Block> + HeaderMetada
/// Get last finalized header.
fn last_finalized(&self) -> ClientResult<Block::Hash>;

/// Get storage cache.
fn cache(&self) -> Option<Arc<dyn BlockchainCache<Block>>>;

/// Get storage usage statistics.
fn usage_info(&self) -> Option<UsageInfo>;
}

/// Light client CHT root storage.
pub trait ChtRootStorage<Block: BlockT> {
/// Get headers CHT root for given block. Returns None if the block is not pruned (not a part of any CHT).
fn header_cht_root(
&self,
Expand All @@ -267,12 +278,6 @@ pub trait Storage<Block: BlockT>: AuxStore + HeaderBackend<Block> + HeaderMetada
cht_size: NumberFor<Block>,
block: NumberFor<Block>,
) -> ClientResult<Option<Block::Hash>>;

/// Get storage cache.
fn cache(&self) -> Option<Arc<dyn BlockchainCache<Block>>>;

/// Get storage usage statistics.
fn usage_info(&self) -> Option<UsageInfo>;
}

/// Remote header.
Expand Down
1 change: 1 addition & 0 deletions client/chain-spec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ serde_json = "1.0.41"
sp-runtime = { version = "2.0.0-rc5", path = "../../primitives/runtime" }
sp-chain-spec = { version = "2.0.0-rc5", path = "../../primitives/chain-spec" }
sc-telemetry = { version = "2.0.0-rc5", path = "../telemetry" }
codec = { package = "parity-scale-codec", version = "1.3.4" }
Loading