This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Add a LightSyncState
field to the chain spec
#6894
Merged
Merged
Changes from 4 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
488d0a9
Reset code, almost ready for PR
expenses ddc7e3e
Merge remote-tracking branch 'origin/master' into ashley-chain-spec
expenses 15dcb28
Improved build_hardcoded_spec
expenses ecb2f56
Fix line widths
expenses 837c5f7
Fix tests
expenses 42d88d5
Fix sc-service-test
expenses 4288dc4
Suggestions from code review
expenses 44ff9f9
Rename to LightSyncState
expenses a6b5491
It's not syncing :^(
expenses c67d005
t Merge remote-tracking branch 'origin/master' into ashley-chain-spec…
expenses 346b711
It syncs!
expenses 6447b85
Remove rpc call
expenses 1adad2e
Convert spaces to tabs
expenses 9372667
Moved sc-service things to export_sync_state.rs
expenses db2da96
Fix tests
expenses b530147
Wait for syncing with network_status_sinks
expenses 004a8a4
Remove sc-network from node-template
expenses ea62d80
Apply suggestions from code review
expenses 9b558eb
Various changes, split the flag up into 2 pieces to make testing easier.
expenses fd1d2b1
Update client/cli/src/commands/build_spec_cmd.rs
expenses 57cbb15
Merge remote-tracking branch 'origin/master' into ashley-chain-spec
expenses fa51717
Revert a lot of changes
expenses 285c5af
Merge remote-tracking branch 'origin/master' into ashley-chain-spec
expenses File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
//! Substrate chain configurations. | ||
#![warn(missing_docs)] | ||
|
||
use std::{borrow::Cow, fs::File, path::PathBuf, sync::Arc, collections::HashMap}; | ||
use serde::{Serialize, Deserialize}; | ||
|
@@ -26,6 +27,7 @@ use serde_json as json; | |
use crate::{RuntimeGenesis, ChainType, extension::GetExtension, Properties}; | ||
use sc_network::config::MultiaddrWithPeerId; | ||
use sc_telemetry::TelemetryEndpoints; | ||
use sp_runtime::traits::Block as BlockT; | ||
|
||
enum GenesisSource<G> { | ||
File(PathBuf), | ||
|
@@ -157,6 +159,7 @@ struct ClientSpec<E> { | |
consensus_engine: (), | ||
#[serde(skip_serializing)] | ||
genesis: serde::de::IgnoredAny, | ||
hardcoded_sync: Option<SerializableHardcodedSync>, | ||
} | ||
|
||
/// A type denoting empty extensions. | ||
|
@@ -245,6 +248,7 @@ impl<G, E> ChainSpec<G, E> { | |
extensions, | ||
consensus_engine: (), | ||
genesis: Default::default(), | ||
hardcoded_sync: None, | ||
}; | ||
|
||
ChainSpec { | ||
|
@@ -257,6 +261,11 @@ impl<G, E> ChainSpec<G, E> { | |
fn chain_type(&self) -> ChainType { | ||
self.client_spec.chain_type.clone() | ||
} | ||
|
||
/// Hardcode infomation to allow light clients to sync quickly into the chain spec. | ||
fn set_hardcoded_sync(&mut self, hardcoded_sync: SerializableHardcodedSync) { | ||
self.client_spec.hardcoded_sync = Some(hardcoded_sync); | ||
} | ||
} | ||
|
||
impl<G, E: serde::de::DeserializeOwned> ChainSpec<G, E> { | ||
|
@@ -284,16 +293,15 @@ impl<G, E: serde::de::DeserializeOwned> ChainSpec<G, E> { | |
} | ||
} | ||
|
||
impl<G: RuntimeGenesis, E: serde::Serialize + Clone + 'static> ChainSpec<G, E> { | ||
/// Dump to json string. | ||
pub fn as_json(&self, raw: bool) -> Result<String, String> { | ||
#[derive(Serialize, Deserialize)] | ||
struct Container<G, E> { | ||
#[serde(flatten)] | ||
client_spec: ClientSpec<E>, | ||
genesis: Genesis<G>, | ||
#[derive(Serialize, Deserialize)] | ||
struct JsonContainer<G, E> { | ||
#[serde(flatten)] | ||
client_spec: ClientSpec<E>, | ||
genesis: Genesis<G>, | ||
} | ||
|
||
}; | ||
impl<G: RuntimeGenesis, E: serde::Serialize + Clone + 'static> ChainSpec<G, E> { | ||
fn json_container(&self, raw: bool) -> Result<JsonContainer<G, E>, String> { | ||
let genesis = match (raw, self.genesis.resolve()?) { | ||
(true, Genesis::Runtime(g)) => { | ||
let storage = g.build_storage()?; | ||
|
@@ -313,13 +321,25 @@ impl<G: RuntimeGenesis, E: serde::Serialize + Clone + 'static> ChainSpec<G, E> { | |
}, | ||
(_, genesis) => genesis, | ||
}; | ||
let container = Container { | ||
Ok(JsonContainer { | ||
client_spec: self.client_spec.clone(), | ||
genesis, | ||
}; | ||
}) | ||
} | ||
|
||
/// Dump to json string. | ||
pub fn as_json(&self, raw: bool) -> Result<String, String> { | ||
let container = self.json_container(raw)?; | ||
json::to_string_pretty(&container) | ||
.map_err(|e| format!("Error generating spec json: {}", e)) | ||
} | ||
|
||
/// Dump to json value | ||
pub fn as_json_value(&self, raw: bool) -> Result<json::Value, String> { | ||
let container = self.json_container(raw)?; | ||
json::to_value(container) | ||
.map_err(|e| format!("Error generating spec json: {}", e)) | ||
} | ||
} | ||
|
||
impl<G, E> crate::ChainSpec for ChainSpec<G, E> | ||
|
@@ -367,6 +387,10 @@ where | |
ChainSpec::as_json(self, raw) | ||
} | ||
|
||
fn as_json_value(&self, raw: bool) -> Result<serde_json::Value, String> { | ||
ChainSpec::as_json_value(self, raw) | ||
} | ||
|
||
fn as_storage_builder(&self) -> &dyn BuildStorage { | ||
self | ||
} | ||
|
@@ -378,6 +402,49 @@ where | |
fn set_storage(&mut self, storage: Storage) { | ||
self.genesis = GenesisSource::Storage(storage); | ||
} | ||
|
||
fn set_hardcoded_sync(&mut self, hardcoded_sync: SerializableHardcodedSync) { | ||
ChainSpec::set_hardcoded_sync(self, hardcoded_sync) | ||
} | ||
} | ||
|
||
/// Hardcoded infomation that allows light clients to sync quickly. | ||
pub struct HardcodedSync<Block: BlockT> { | ||
/// The header of the best finalized block. | ||
pub header: <Block as BlockT>::Header, | ||
/// A list of all CHTs in the chain. | ||
pub chts: Vec<<Block as BlockT>::Hash>, | ||
} | ||
|
||
impl<Block: BlockT> HardcodedSync<Block> { | ||
/// Convert into a `SerializableHardcodecSync`. | ||
pub fn to_serializable(&self) -> SerializableHardcodedSync { | ||
use codec::Encode; | ||
|
||
SerializableHardcodedSync { | ||
header: StorageData(self.header.encode()), | ||
chts: self.chts.iter().map(|hash| StorageData(hash.encode())).collect(), | ||
} | ||
} | ||
|
||
/// Convert from a `SerializableHardcodecSync`. | ||
pub fn from_serializable(serialized: &SerializableHardcodedSync) -> Result<Self, codec::Error> { | ||
Ok(Self { | ||
header: codec::Decode::decode(&mut &serialized.header.0[..])?, | ||
chts: serialized.chts.iter() | ||
.map(|cht| codec::Decode::decode(&mut &cht.0[..])) | ||
.collect::<Result<_, _>>()?, | ||
}) | ||
} | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
#[serde(deny_unknown_fields)] | ||
/// The serializable form of `HardcodedSync`. Created using `HardcodedSync::serialize`. | ||
expenses marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub struct SerializableHardcodedSync { | ||
header: StorageData, | ||
chts: Vec<StorageData>, | ||
Comment on lines
+435
to
+436
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that |
||
} | ||
|
||
#[cfg(test)] | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.