This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 379
Validate timestamps against relay chain slot #437
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
991af14
track the relay chain slot number
andresilva 385094b
add logic for validating timestamps against relay slot number
andresilva e951076
runtime: validate timestamps against relay chain slot
andresilva e152d0d
system: give some leeway to local slots
andresilva 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 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 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 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 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 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
117 changes: 117 additions & 0 deletions
117
pallets/parachain-system/src/validate_block/timestamp.rs
This file contains 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 |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright 2021 Parity Technologies (UK) Ltd. | ||
// This file is part of Substrate. | ||
|
||
// Substrate is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Substrate is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
//! Utility module to validate cumulus-runtime timestamps against the | ||
//! relay chain slot. | ||
|
||
use cumulus_primitives_core::{OnValidationData, Slot}; | ||
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. This should be its own pallet. Similar to The idea with using environmental was also a bad idea by me. We should probably store the intermediate data in the storage. |
||
use frame_support::traits::Get; | ||
|
||
use crate::{Config, PersistedValidationData, RelaySlot}; | ||
|
||
// Temporary global that holds the state as populated by the hooks `OnTimestampSet` or | ||
// `OnValidationData`. | ||
#[derive(Default)] | ||
pub(crate) struct TimestampValidationParams { | ||
relay_chain_slot: Option<Slot>, | ||
timestamp_slot: Option<Slot>, | ||
} | ||
|
||
// Stores the [`TimestampValidationParams`] that are being passed to `validate_block`. | ||
// | ||
// This value will only be set when a parachain validator validates a given `PoV`. | ||
environmental::environmental!(TIMESTAMP_VALIDATION_PARAMS: TimestampValidationParams); | ||
|
||
/// Set the [`TimestampValidationParams`] for the local context and execute the given closure in | ||
/// this context. | ||
#[cfg(not(feature = "std"))] | ||
pub(crate) fn run_with_timestamp_validation_params<R>( | ||
f: impl FnOnce() -> R, | ||
) -> R { | ||
let mut params = Default::default(); | ||
TIMESTAMP_VALIDATION_PARAMS::using(&mut params, f) | ||
} | ||
|
||
/// Utility to validate the timestamps set from a cumulus-enabled runtime against | ||
/// the relay chain slot number. To enable this validation the runtime hooks for | ||
/// `OnTimestampSet` and `OnValidationData` should be set to this struct using the | ||
/// appropriate slot duration of the relay chain. | ||
pub struct ValidateTimestampAgainstRelayChainSlot< | ||
T: pallet_timestamp::Config, | ||
RelaySlotDuration: Get<T::Moment>, | ||
>(sp_std::marker::PhantomData<(T, RelaySlotDuration)>); | ||
|
||
impl<T, RelaySlotDuration> OnValidationData | ||
for ValidateTimestampAgainstRelayChainSlot<T, RelaySlotDuration> | ||
where | ||
T: Config + pallet_timestamp::Config, | ||
RelaySlotDuration: Get<T::Moment>, | ||
{ | ||
fn on_validation_data(_data: &PersistedValidationData) { | ||
TIMESTAMP_VALIDATION_PARAMS::with(|p| { | ||
let relay_chain_slot = match RelaySlot::<T>::get() { | ||
Some(slot) => slot, | ||
_ => { | ||
// this should be unreachable as the relay slot should always be populated after | ||
// we have processed the validation data. | ||
return; | ||
} | ||
}; | ||
|
||
if let Some(timestamp_slot) = p.timestamp_slot { | ||
assert!( | ||
valid_slot(relay_chain_slot, timestamp_slot), | ||
"Timestamp slot must be consistent with relay chain slot: relay: {:?}, local: {:?}", | ||
relay_chain_slot, | ||
timestamp_slot, | ||
); | ||
} else { | ||
p.relay_chain_slot = Some(relay_chain_slot); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
impl<T, RelaySlotDuration> frame_support::traits::OnTimestampSet<T::Moment> | ||
for ValidateTimestampAgainstRelayChainSlot<T, RelaySlotDuration> | ||
where | ||
T: pallet_timestamp::Config, | ||
RelaySlotDuration: Get<T::Moment>, | ||
{ | ||
fn on_timestamp_set(moment: T::Moment) { | ||
use sp_runtime::SaturatedConversion; | ||
|
||
TIMESTAMP_VALIDATION_PARAMS::with(|p| { | ||
let timestamp_slot = moment / RelaySlotDuration::get(); | ||
let timestamp_slot = Slot::from(timestamp_slot.saturated_into::<u64>()); | ||
|
||
if let Some(relay_chain_slot) = p.relay_chain_slot { | ||
assert!( | ||
valid_slot(relay_chain_slot, timestamp_slot), | ||
"Timestamp slot must be consistent with relay chain slot: relay: {:?}, local: {:?}", | ||
relay_chain_slot, | ||
timestamp_slot, | ||
); | ||
} else { | ||
p.timestamp_slot = Some(timestamp_slot); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
fn valid_slot(relay_slot: Slot, local_slot: Slot) -> bool { | ||
relay_slot == local_slot || relay_slot + 1 == local_slot | ||
} |
This file contains 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 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any reason to add this here as a storage item.