mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-28 12:07:57 +00:00
SlotDuration: Always fetch the slot duration from the runtime (#10509)
* SlotDuration: Always fetch the slot duration from the runtime The slot duration should always be fetched from the runtime instead of being cached in the db. The slot duration is only fetched on startup of the node, so the performance isn't that important. This is especially helpful for the case when the slot duration of a chain should be changed through a runtime upgrade (there be dragons, so take care). * Fix docs * Remove logging * Fix warning
This commit is contained in:
@@ -329,7 +329,9 @@ pub struct BabeIntermediate<B: BlockT> {
|
||||
/// Intermediate key for Babe engine.
|
||||
pub static INTERMEDIATE_KEY: &[u8] = b"babe1";
|
||||
|
||||
/// A slot duration. Create with `get_or_compute`.
|
||||
/// A slot duration.
|
||||
///
|
||||
/// Create with [`Self::get`].
|
||||
// FIXME: Once Rust has higher-kinded types, the duplication between this
|
||||
// and `super::babe::Config` can be eliminated.
|
||||
// https://github.com/paritytech/substrate/issues/2434
|
||||
@@ -337,39 +339,33 @@ pub static INTERMEDIATE_KEY: &[u8] = b"babe1";
|
||||
pub struct Config(sc_consensus_slots::SlotDuration<BabeGenesisConfiguration>);
|
||||
|
||||
impl Config {
|
||||
/// Either fetch the slot duration from disk or compute it from the genesis
|
||||
/// state.
|
||||
pub fn get_or_compute<B: BlockT, C>(client: &C) -> ClientResult<Self>
|
||||
/// Fetch the config from the runtime.
|
||||
pub fn get<B: BlockT, C>(client: &C) -> ClientResult<Self>
|
||||
where
|
||||
C: AuxStore + ProvideRuntimeApi<B> + UsageProvider<B>,
|
||||
C::Api: BabeApi<B>,
|
||||
{
|
||||
trace!(target: "babe", "Getting slot duration");
|
||||
match sc_consensus_slots::SlotDuration::get_or_compute(client, |a, b| {
|
||||
let has_api_v1 = a.has_api_with::<dyn BabeApi<B>, _>(&b, |v| v == 1)?;
|
||||
let has_api_v2 = a.has_api_with::<dyn BabeApi<B>, _>(&b, |v| v == 2)?;
|
||||
|
||||
if has_api_v1 {
|
||||
#[allow(deprecated)]
|
||||
{
|
||||
Ok(a.configuration_before_version_2(b)?.into())
|
||||
}
|
||||
} else if has_api_v2 {
|
||||
a.configuration(b).map_err(Into::into)
|
||||
} else {
|
||||
Err(sp_blockchain::Error::VersionInvalid(
|
||||
"Unsupported or invalid BabeApi version".to_string(),
|
||||
))
|
||||
let best_block_id = BlockId::Hash(client.usage_info().chain.best_hash);
|
||||
let runtime_api = client.runtime_api();
|
||||
|
||||
let version = runtime_api.api_version::<dyn BabeApi<B>>(&best_block_id)?;
|
||||
|
||||
let slot_duration = if version == Some(1) {
|
||||
#[allow(deprecated)]
|
||||
{
|
||||
runtime_api.configuration_before_version_2(&best_block_id)?.into()
|
||||
}
|
||||
})
|
||||
.map(Self)
|
||||
{
|
||||
Ok(s) => Ok(s),
|
||||
Err(s) => {
|
||||
warn!(target: "babe", "Failed to get slot duration");
|
||||
Err(s)
|
||||
},
|
||||
}
|
||||
} else if version == Some(2) {
|
||||
runtime_api.configuration(&best_block_id)?
|
||||
} else {
|
||||
return Err(sp_blockchain::Error::VersionInvalid(
|
||||
"Unsupported or invalid BabeApi version".to_string(),
|
||||
))
|
||||
};
|
||||
|
||||
Ok(Self(sc_consensus_slots::SlotDuration::new(slot_duration)))
|
||||
}
|
||||
|
||||
/// Get the inner slot duration
|
||||
|
||||
Reference in New Issue
Block a user