pub trait ConvertBlockNumber<L, N> {
// Required methods
fn equivalent_moment_in_time(local_moment: L) -> N;
fn equivalent_block_duration(local_duration: L) -> N;
}Expand description
Converts previous (local) block number into the new one. May just be identity functions if sticking with the local block number.
Required Methods§
Sourcefn equivalent_moment_in_time(local_moment: L) -> N
fn equivalent_moment_in_time(local_moment: L) -> N
Converts to the new type and finds the equivalent moment in time as from the view of the new block provider
§Example usage
ⓘ
// Let's say you are a parachain and switching block providers to the relay chain.
// This will return what the relay block number was at the moment the previous provider's
// number was `local_moment`.
fn equivalent_moment_in_time(local_moment: u32) -> u32 {
// How long it's been since 'local_moment' from the parachains pov.
let local_block_number = System::block_number();
let local_duration = u32::abs_diff(local_block_number, local_moment);
// How many blocks that is from the relay's pov.
let relay_duration = Self::equivalent_block_duration(local_duration);
// What the relay block number must have been at 'local_moment'.
let relay_block_number = ParachainSystem::last_relay_block_number();
if local_block_number >= local_moment {
// Moment was in past.
relay_block_number.saturating_sub(relay_duration)
} else {
// Moment is in future.
relay_block_number.saturating_add(relay_duration)
}
}Sourcefn equivalent_block_duration(local_duration: L) -> N
fn equivalent_block_duration(local_duration: L) -> N
Returns the equivalent number of new blocks it would take to fulfill the same amount of time in seconds as the old blocks.
For instance - If you previously had 12s blocks and are now following the relay chain’s 6, one local block is equivalent to 2 relay blocks in duration.
§Visualized
6s 6s
|---------||---------|
12s
|--------------------|
^ Two 6s relay blocks passed per one 12s local block.§Example Usage
ⓘ
// Following the scenerio above.
fn equivalent_block_duration(local_duration: u32) -> u32 {
local_duration.saturating_mul(2)
}Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.