Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
49 changes: 25 additions & 24 deletions light-base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ impl<TPlat: platform::PlatformRef, TChain> Client<TPlat, TChain> {
}
}
(None, Some(chain_information)) => {
StartServicesChainTy::RelayChain { chain_information }
StartServicesChainTy::SubstrateCompatible { chain_information }
}
(None, None) => {
// Checked above.
Expand Down Expand Up @@ -1110,7 +1110,7 @@ pub enum AddChainError {
}

enum StartServicesChainTy<'a, TPlat: platform::PlatformRef> {
RelayChain {
SubstrateCompatible {
chain_information: &'a chain::chain_information::ValidChainInformation,
},
Parachain {
Expand Down Expand Up @@ -1148,27 +1148,25 @@ fn start_services<TPlat: platform::PlatformRef>(
let network_service_chain = network_service.add_chain(network_service::ConfigChain {
log_name: log_name.clone(),
num_out_slots: 4,
grandpa_protocol_finalized_block_height: if let StartServicesChainTy::RelayChain {
chain_information,
} = &config
{
if matches!(
chain_information.as_ref().finality,
chain::chain_information::ChainInformationFinalityRef::Grandpa { .. }
) {
Some(chain_information.as_ref().finalized_block_header.number)
grandpa_protocol_finalized_block_height:
if let StartServicesChainTy::SubstrateCompatible { chain_information } = &config {
if matches!(
chain_information.as_ref().finality,
chain::chain_information::ChainInformationFinalityRef::Grandpa { .. }
) {
Some(chain_information.as_ref().finalized_block_header.number)
} else {
None
}
} else {
// Parachains never use GrandPa.
None
}
} else {
// Parachains never use GrandPa.
None
},
},
genesis_block_hash: header::hash_from_scale_encoded_header(
&genesis_block_scale_encoded_header,
),
best_block: match &config {
StartServicesChainTy::RelayChain { chain_information } => (
StartServicesChainTy::SubstrateCompatible { chain_information } => (
chain_information.as_ref().finalized_block_header.number,
chain_information
.as_ref()
Expand Down Expand Up @@ -1216,8 +1214,10 @@ fn start_services<TPlat: platform::PlatformRef>(
chain_type: sync_service::ConfigChainType::Parachain(
sync_service::ConfigParachain {
finalized_block_header,
para_id,
relay_chain_sync: relay_chain.runtime_service.clone(),
relay_chain: sync_service::ConfigRelayChain {
para_id,
relay_chain_sync: relay_chain.runtime_service.clone(),
},
},
),
}));
Expand All @@ -1236,8 +1236,8 @@ fn start_services<TPlat: platform::PlatformRef>(

(sync_service, runtime_service)
}
StartServicesChainTy::RelayChain { chain_information } => {
// Chain is a relay chain.
StartServicesChainTy::SubstrateCompatible { chain_information } => {
// Chain is a Substrate-compatible non-parachain chain.

// The sync service is leveraging the network service, downloads block headers,
// and verifies them, to determine what are the best and finalized blocks of the
Expand All @@ -1247,16 +1247,17 @@ fn start_services<TPlat: platform::PlatformRef>(
block_number_bytes,
platform: platform.clone(),
network_service: network_service_chain.clone(),
chain_type: sync_service::ConfigChainType::RelayChain(
sync_service::ConfigRelayChain {
chain_type: sync_service::ConfigChainType::SubstrateCompatible(
sync_service::ConfigSubstrateCompatible {
chain_information: chain_information.clone(),
runtime_code_hint: runtime_code_hint.map(|hint| {
sync_service::ConfigRelayChainRuntimeCodeHint {
sync_service::ConfigSubstrateCompatibleRuntimeCodeHint {
storage_value: hint.code,
merkle_value: hint.code_merkle_value,
closest_ancestor_excluding: hint.closest_ancestor_excluding,
}
}),
relay_chain: None,
},
),
}));
Expand Down
50 changes: 32 additions & 18 deletions light-base/src/sync_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ use smoldot::{
};

mod parachain;
mod standalone;
mod paraheads;
mod substrate_compat;

pub use network_service::Role;

Expand All @@ -66,20 +67,20 @@ pub struct Config<TPlat: PlatformRef> {
/// Access to the network, and index of the chain to sync from the point of view of the
/// network service.
pub network_service: Arc<network_service::NetworkServiceChain<TPlat>>,
/// Extra fields depending on whether the chain is a relay chain or a parachain.
/// Extra fields depending on whether the chain is a parachain.
pub chain_type: ConfigChainType<TPlat>,
}

/// See [`Config::chain_type`].
pub enum ConfigChainType<TPlat: PlatformRef> {
/// Chain is a relay chain.
RelayChain(ConfigRelayChain),
/// Chain is a Substrate-compatible non-parachain.
SubstrateCompatible(ConfigSubstrateCompatible<TPlat>),
/// Chain is a parachain.
Parachain(ConfigParachain<TPlat>),
}

/// See [`ConfigChainType::RelayChain`].
pub struct ConfigRelayChain {
/// See [`ConfigChainType::SubstrateCompatible`].
pub struct ConfigSubstrateCompatible<TPlat: PlatformRef> {
/// State of the finalized chain.
pub chain_information: chain::chain_information::ValidChainInformation,

Expand All @@ -89,13 +90,17 @@ pub struct ConfigRelayChain {
/// if it matches the Merkle value provided in the hint, use the storage value in the hint
/// instead of downloading it. If the hint doesn't match, an extra round-trip will be needed,
/// but if the hint matches it saves a big download.
pub runtime_code_hint: Option<ConfigRelayChainRuntimeCodeHint>,
pub runtime_code_hint: Option<ConfigSubstrateCompatibleRuntimeCodeHint>,

/// If this chain is a parachain, contains the information of the relay chain.
/// `None` if this chain isn't a parachain.
pub relay_chain: Option<ConfigRelayChain<TPlat>>,
}

/// See [`ConfigRelayChain::runtime_code_hint`].
pub struct ConfigRelayChainRuntimeCodeHint {
/// See [`ConfigSubstrateCompatible::runtime_code_hint`].
pub struct ConfigSubstrateCompatibleRuntimeCodeHint {
/// Storage value of the `:code` trie node corresponding to
/// [`ConfigRelayChainRuntimeCodeHint::merkle_value`].
/// [`ConfigSubstrateCompatibleRuntimeCodeHint::merkle_value`].
pub storage_value: Vec<u8>,
/// Merkle value of the `:code` trie node in the storage main trie.
pub merkle_value: Vec<u8>,
Expand All @@ -105,13 +110,19 @@ pub struct ConfigRelayChainRuntimeCodeHint {

/// See [`ConfigChainType::Parachain`].
pub struct ConfigParachain<TPlat: PlatformRef> {
/// Runtime service that synchronizes the relay chain of this parachain.
pub relay_chain_sync: Arc<runtime_service::RuntimeService<TPlat>>,
/// Parameters of the relay chain.
pub relay_chain: ConfigRelayChain<TPlat>,

/// SCALE-encoded header of a known finalized block of the parachain. Used in the situation
/// where the API user subscribes using [`SyncService::subscribe_all`] before any parachain
/// block can be gathered.
pub finalized_block_header: Vec<u8>,
}

/// See [`ConfigSubstrateCompatible::relay_chain`] and [`ConfigParachain::relay_chain`].
pub struct ConfigRelayChain<TPlat: PlatformRef> {
/// Runtime service that synchronizes the relay chain of this parachain.
pub relay_chain_sync: Arc<runtime_service::RuntimeService<TPlat>>,

/// Id of the parachain within the relay chain.
///
Expand Down Expand Up @@ -149,18 +160,21 @@ impl<TPlat: PlatformRef> SyncService<TPlat> {
config.platform.clone(),
config_parachain.finalized_block_header,
config.block_number_bytes,
config_parachain.relay_chain_sync.clone(),
config_parachain.para_id,
config_parachain.relay_chain.relay_chain_sync.clone(),
config_parachain.relay_chain.para_id,
from_foreground,
config.network_service.clone(),
)),
ConfigChainType::RelayChain(config_relay_chain) => {
Box::pin(standalone::start_standalone_chain(
ConfigChainType::SubstrateCompatible(config_substrate_compat) => {
Box::pin(substrate_compat::start_substrate_compatible_chain(
log_target.clone(),
config.platform.clone(),
config_relay_chain.chain_information,
config_substrate_compat.chain_information,
config.block_number_bytes,
config_relay_chain.runtime_code_hint,
config_substrate_compat
.relay_chain
.map(|rc| (rc.relay_chain_sync, rc.para_id)),
config_substrate_compat.runtime_code_hint,
from_foreground,
config.network_service.clone(),
))
Expand Down
Loading
Loading