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

Add missing fields to the light sync state #7225

Merged
18 commits merged into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
d3e8c97
Initial draft
expenses Sep 23, 2020
1eb1297
Add an iterator that helps us get most items
expenses Sep 28, 2020
a118cea
Merge remote-tracking branch 'origin/master' into ashley-improve-sync…
expenses Sep 28, 2020
ccfb11d
Revert changes to grandpa
expenses Sep 28, 2020
b3e8f09
Merge remote-tracking branch 'origin/master' into ashley-improve-sync…
expenses Sep 29, 2020
35a580a
Change fields to just be the grandpa authority set and babe epoch cha…
expenses Sep 29, 2020
a8f0966
Only use the fields we need from the shared authority set
expenses Sep 30, 2020
fdd282f
Switch to RPC call
expenses Oct 6, 2020
32c12ed
Merge remote-tracking branch 'origin/master' into ashley-improve-sync…
expenses Oct 6, 2020
7f28aa9
Revert "Only use the fields we need from the shared authority set"
expenses Oct 9, 2020
6e7855b
Merge remote-tracking branch 'origin/master' into ashley-improve-sync…
expenses Oct 9, 2020
81ac0ab
Add babe_finalized_block_weight from `ashley-improve-sync-state-WIP-l…
expenses Oct 9, 2020
136231b
Fix rpc test
expenses Oct 9, 2020
d94adf1
Move sync state rpc stuff into sc-sync-state-rpc
expenses Oct 12, 2020
d56cafc
Merge remote-tracking branch 'origin/master' into ashley-improve-sync…
expenses Oct 12, 2020
49f1d47
Remove as_json_value and remove unwraps from sc-sync-state-rpc
expenses Oct 12, 2020
b6af3e9
Merge remote-tracking branch 'origin/master' into ashley-improve-sync…
expenses Oct 14, 2020
750448b
Add clone_inner to SharedAuthoritySet
expenses Oct 15, 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
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions client/chain-spec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" }
sp-chain-spec = { version = "2.0.0", path = "../../primitives/chain-spec" }
sc-telemetry = { version = "2.0.0", path = "../telemetry" }
codec = { package = "parity-scale-codec", version = "1.3.4" }
sp-consensus-slots = { version = "0.8.0-rc6", path = "../../primitives/consensus/slots" }
sc-consensus-babe = { version = "0.8.0-rc6", path = "../consensus/babe" }
sp-finality-grandpa = { version = "2.0.0-rc6", path = "../../primitives/finality-grandpa" }
46 changes: 40 additions & 6 deletions client/chain-spec/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +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;
use sp_runtime::traits::{Block as BlockT, NumberFor};

enum GenesisSource<G> {
File(PathBuf),
Expand Down Expand Up @@ -264,7 +264,7 @@ impl<G, E> ChainSpec<G, E> {

/// Hardcode infomation to allow light clients to sync quickly into the chain spec.
fn set_light_sync_state(&mut self, light_sync_state: SerializableLightSyncState) {
self.client_spec.light_sync_state = Some(light_sync_state);
self.client_spec.light_sync_state = Some(light_sync_state);
}
}

Expand Down Expand Up @@ -400,7 +400,13 @@ where
/// Hardcoded infomation that allows light clients to sync quickly.
pub struct LightSyncState<Block: BlockT> {
/// The header of the best finalized block.
pub header: <Block as BlockT>::Header,
pub finalized_block_header: <Block as BlockT>::Header,
pub babe_finalized_block1_slot_number: sp_consensus_slots::SlotNumber,
pub babe_finalized_block_epoch_information: sc_consensus_babe::NextEpochDescriptor,
pub babe_finalized_next_epoch_transition: sc_consensus_babe::NextEpochDescriptor,
andresilva marked this conversation as resolved.
Show resolved Hide resolved
pub grandpa_after_finalized_block_authorities_set_id: sp_finality_grandpa::SetId,
pub grandpa_finalized_triggered_authorities: sp_finality_grandpa::AuthorityList,
pub grandpa_finalized_scheduled_change: sp_finality_grandpa::ScheduledChange<NumberFor<Block>>,
}

impl<Block: BlockT> LightSyncState<Block> {
Expand All @@ -409,14 +415,36 @@ impl<Block: BlockT> LightSyncState<Block> {
use codec::Encode;

SerializableLightSyncState {
header: StorageData(self.header.encode()),
finalized_block_header: StorageData(self.finalized_block_header.encode()),
babe_finalized_block1_slot_number: self.babe_finalized_block1_slot_number,
babe_finalized_block_epoch_information:
StorageData(self.babe_finalized_block_epoch_information.encode()),
babe_finalized_next_epoch_transition:
StorageData(self.babe_finalized_next_epoch_transition.encode()),
grandpa_after_finalized_block_authorities_set_id:
self.grandpa_after_finalized_block_authorities_set_id,
grandpa_finalized_triggered_authorities:
StorageData(self.grandpa_finalized_triggered_authorities.encode()),
grandpa_finalized_scheduled_change:
StorageData(self.grandpa_finalized_scheduled_change.encode()),
}
}

/// Convert from a `SerializableLightSyncState`.
pub fn from_serializable(serialized: &SerializableLightSyncState) -> Result<Self, codec::Error> {
Ok(Self {
header: codec::Decode::decode(&mut &serialized.header.0[..])?,
finalized_block_header: codec::Decode::decode(&mut &serialized.finalized_block_header.0[..])?,
babe_finalized_block1_slot_number: serialized.babe_finalized_block1_slot_number,
babe_finalized_block_epoch_information:
codec::Decode::decode(&mut &serialized.babe_finalized_block_epoch_information.0[..])?,
babe_finalized_next_epoch_transition:
codec::Decode::decode(&mut &serialized.babe_finalized_next_epoch_transition.0[..])?,
grandpa_after_finalized_block_authorities_set_id:
serialized.grandpa_after_finalized_block_authorities_set_id,
grandpa_finalized_triggered_authorities:
codec::Decode::decode(&mut &serialized.grandpa_finalized_triggered_authorities.0[..])?,
grandpa_finalized_scheduled_change:
codec::Decode::decode(&mut &serialized.grandpa_finalized_scheduled_change.0[..])?,
})
}
}
Expand All @@ -426,7 +454,13 @@ impl<Block: BlockT> LightSyncState<Block> {
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct SerializableLightSyncState {
header: StorageData,
finalized_block_header: StorageData,
babe_finalized_block1_slot_number: sp_consensus_slots::SlotNumber,
babe_finalized_block_epoch_information: StorageData,
babe_finalized_next_epoch_transition: StorageData,
grandpa_after_finalized_block_authorities_set_id: sp_finality_grandpa::SetId,
grandpa_finalized_triggered_authorities: StorageData,
grandpa_finalized_scheduled_change: StorageData,
}

#[cfg(test)]
Expand Down
2 changes: 2 additions & 0 deletions client/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ sp-state-machine = { version = "0.8.0", path = "../../primitives/state-machine"
sc-telemetry = { version = "2.0.0", path = "../telemetry" }
substrate-prometheus-endpoint = { path = "../../utils/prometheus" , version = "0.8.0"}
sp-keyring = { version = "2.0.0", path = "../../primitives/keyring" }
sp-api = { version = "2.0.0", path = "../../primitives/api" }
sc-consensus-babe = { version = "0.8.0", path = "../consensus/babe" }
names = "0.11.0"
structopt = "0.3.8"
sc-tracing = { version = "2.0.0", path = "../tracing" }
Expand Down
15 changes: 13 additions & 2 deletions client/cli/src/commands/build_sync_spec_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::error;
use crate::params::{SharedParams, NetworkParams};
use crate::params::{SharedParams, NetworkParams, ImportParams};
use crate::CliConfiguration;
use log::info;
use sc_network::config::build_multiaddr;
Expand Down Expand Up @@ -57,6 +57,10 @@ pub struct BuildSyncSpecCmd {
#[allow(missing_docs)]
#[structopt(flatten)]
pub network_params: NetworkParams,

#[allow(missing_docs)]
#[structopt(flatten)]
pub import_params: ImportParams,
}

impl BuildSyncSpecCmd {
Expand All @@ -70,7 +74,10 @@ impl BuildSyncSpecCmd {
) -> error::Result<()>
where
B: BlockT,
CL: sp_blockchain::HeaderBackend<B>,
CL: sp_blockchain::HeaderBackend<B> + sp_api::ProvideRuntimeApi<B> +
sc_client_api::AuxStore,
<CL as sp_api::ProvideRuntimeApi<B>>::Api:
sc_consensus_babe::BabeApi<B, Error=sp_blockchain::Error>,
{
if self.sync_first {
network_status_sinks.status_stream(std::time::Duration::from_secs(1)).filter(|status| {
Expand Down Expand Up @@ -110,4 +117,8 @@ impl CliConfiguration for BuildSyncSpecCmd {
fn network_params(&self) -> Option<&NetworkParams> {
Some(&self.network_params)
}

fn import_params(&self) -> Option<&ImportParams> {
Some(&self.import_params)
}
}
5 changes: 5 additions & 0 deletions client/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ sc-tracing = { version = "2.0.0", path = "../tracing" }
sp-tracing = { version = "2.0.0", path = "../../primitives/tracing" }
tracing = "0.1.19"
parity-util-mem = { version = "0.7.0", default-features = false, features = ["primitive-types"] }
sc-consensus-babe = { version = "0.8.0", path = "../consensus/babe" }
sp-consensus-babe = { version = "0.8.0", path = "../../primitives/consensus/babe" }
sc-consensus-epochs = { version = "0.8.0", path = "../consensus/epochs" }
grandpa = { version = "0.8.0", package = "sc-finality-grandpa", path = "../finality-grandpa" }
grandpa-primitives = { version = "2.0.0", package = "sp-finality-grandpa", path = "../../primitives/finality-grandpa" }

[target.'cfg(not(target_os = "unknown"))'.dependencies]
tempfile = "3.1.0"
Expand Down
67 changes: 63 additions & 4 deletions client/service/src/chain_ops/build_sync_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,82 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

use sp_runtime::traits::Block as BlockT;
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, NumberFor, One};
use sp_blockchain::HeaderBackend;
use std::sync::Arc;
use sp_runtime::ConsensusEngineId;
use sp_runtime::generic::BlockId;
use sc_client_api::{CallExecutor, ExecutorProvider};

fn backwards_consensus_log_iter<'a, TBl, TCl, CL, FN, T>(
client: Arc<TCl>, mut number: NumberFor<TBl>, engine_id: &'a ConsensusEngineId, func: FN,
) -> impl Iterator<Item = Result<T, sp_blockchain::Error>> + 'a
where
TBl: BlockT,
TCl: HeaderBackend<TBl> + 'a,
CL: codec::Decode,
FN: Fn(CL) -> Option<T> + 'a,
{
let id = sp_runtime::generic::OpaqueDigestItemId::Consensus(engine_id);

std::iter::from_fn(move || {
let header = match client.header(BlockId::Number(number)) {
Ok(Some(header)) => header,
Ok(None) => return Some(Some(Err(sp_blockchain::Error::Msg("Missing header".into())))),
Err(err) => return Some(Some(Err(err))),
};

let change = header.digest()
.convert_first(|l| l.try_to(id).and_then(&func));

number -= NumberFor::<TBl>::one();
Some(change.map(Ok))
}).filter_map(|optional_log_item| optional_log_item)
}
andresilva marked this conversation as resolved.
Show resolved Hide resolved

/// Build a `LightSyncState` from the CHT roots stored in a backend.
pub fn build_light_sync_state<TBl, TCl>(
client: Arc<TCl>,
) -> Result<sc_chain_spec::LightSyncState<TBl>, sp_blockchain::Error>
where
TBl: BlockT,
TCl: HeaderBackend<TBl>,
TCl: HeaderBackend<TBl> + sp_api::ProvideRuntimeApi<TBl> + sc_client_api::AuxStore,
<TCl as sp_api::ProvideRuntimeApi<TBl>>::Api:
sc_consensus_babe::BabeApi<TBl, Error=sp_blockchain::Error>,
{
let finalized_number = client.info().finalized_number;
let finalized_hash = client.info().finalized_hash;
let header = client.header(BlockId::Hash(finalized_hash))?.unwrap();
let finalized_header = client.header(BlockId::Hash(finalized_hash))?.unwrap();

let mut scheduled_changes = backwards_consensus_log_iter(
client.clone(), finalized_number, &grandpa_primitives::GRANDPA_ENGINE_ID,
|log| match log {
grandpa_primitives::ConsensusLog::<NumberFor<TBl>>::ScheduledChange(change) => Some(change),
grandpa_primitives::ConsensusLog::<NumberFor<TBl>>::ForcedChange(_, change) => Some(change),
_ => None
}
);
let future_change = scheduled_changes.next().unwrap()?;
let current_authorities = scheduled_changes.next().unwrap()?.next_authorities;

let mut babe_epoch_descriptors = backwards_consensus_log_iter(
client.clone(), finalized_number, &sp_consensus_babe::BABE_ENGINE_ID,
|log| match log {
sp_consensus_babe::ConsensusLog::NextEpochData(descriptor)=> Some(descriptor),
_ => None
}
);

let next_desc = babe_epoch_descriptors.next().unwrap()?;
let current_desc = babe_epoch_descriptors.next().unwrap()?;

Ok(sc_chain_spec::LightSyncState {
header
finalized_block_header: finalized_header,
babe_finalized_block1_slot_number: 0,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Need a way to get this as well.

babe_finalized_block_epoch_information: current_desc,
babe_finalized_next_epoch_transition: next_desc,
grandpa_finalized_triggered_authorities: current_authorities,
grandpa_after_finalized_block_authorities_set_id: 0,
andresilva marked this conversation as resolved.
Show resolved Hide resolved
grandpa_finalized_scheduled_change: future_change,
})
}