Update Substrate & Polkadot (#387)

* Update Substrate & Polkadot

* Enforce `ParachainSetCode`
This commit is contained in:
Bastian Köcher
2021-04-02 16:17:04 +02:00
committed by GitHub
parent ad5ece357e
commit c995f6c76e
14 changed files with 957 additions and 635 deletions
+17 -46
View File
@@ -54,7 +54,7 @@ mod relay_state_snapshot;
pub mod validate_block;
/// The pallet's configuration trait.
pub trait Config: frame_system::Config {
pub trait Config: frame_system::Config<OnSetCode = ParachainSetCode<Self>> {
/// The overarching event type.
type Event: From<Event> + Into<<Self as frame_system::Config>::Event>;
@@ -144,24 +144,6 @@ decl_module! {
// this is needed only if you are using events in your pallet
fn deposit_event() = default;
// TODO: figure out a better weight than this
// TODO: Bring back the correct validation checks: #374
#[weight = (0, DispatchClass::Operational)]
pub fn schedule_upgrade(origin, validation_function: Vec<u8>) {
ensure_root(origin)?;
Self::schedule_upgrade_impl(validation_function)?;
}
/// Schedule a validation function upgrade without further checks.
///
/// Same as [`Module::schedule_upgrade`], but without checking that the new `validation_function`
/// is correct. This makes it more flexible, but also opens the door to easily brick the chain.
#[weight = (0, DispatchClass::Operational)]
pub fn schedule_upgrade_without_checks(origin, validation_function: Vec<u8>) {
ensure_root(origin)?;
Self::schedule_upgrade_impl(validation_function)?;
}
/// Force an already scheduled validation function upgrade to happen on a particular block.
///
/// Note that coordinating this block for the upgrade has to happen independently on the relay
@@ -654,8 +636,8 @@ impl<T: Config> Module<T> {
Some(vfp.relay_parent_number + cfg.validation_upgrade_delay)
}
/// The implementation of the runtime upgrade scheduling.
fn schedule_upgrade_impl(validation_function: Vec<u8>) -> DispatchResult {
/// The implementation of the runtime upgrade functionality for parachains.
fn set_code_impl(validation_function: Vec<u8>) -> DispatchResult {
ensure!(
!PendingValidationFunction::exists(),
Error::<T>::OverlappingUpgrades
@@ -685,6 +667,14 @@ impl<T: Config> Module<T> {
}
}
pub struct ParachainSetCode<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> frame_system::SetCode for ParachainSetCode<T> {
fn set_code(code: Vec<u8>) -> DispatchResult {
Module::<T>::set_code_impl(code)
}
}
/// This struct provides ability to extend a message queue chain (MQC) and compute a new head.
///
/// MQC is an instance of a [hash chain] applied to a message queue. Using a hash chain it's possible
@@ -975,6 +965,7 @@ mod tests {
type BaseCallFilter = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ParachainSetCode<Self>;
}
impl Config for Test {
type Event = Event;
@@ -1242,26 +1233,6 @@ mod tests {
});
}
#[test]
fn requires_root() {
BlockTests::new().add(123, || {
assert_eq!(
ParachainSystem::schedule_upgrade(Origin::signed(1), Default::default()),
Err(sp_runtime::DispatchError::BadOrigin),
);
});
}
#[test]
fn requires_root_2() {
BlockTests::new().add(123, || {
assert_ok!(ParachainSystem::schedule_upgrade(
RawOrigin::Root.into(),
Default::default()
));
});
}
#[test]
fn events() {
BlockTests::new()
@@ -1271,7 +1242,7 @@ mod tests {
.add_with_post_test(
123,
|| {
assert_ok!(ParachainSystem::schedule_upgrade(
assert_ok!(System::set_code(
RawOrigin::Root.into(),
Default::default()
));
@@ -1304,14 +1275,14 @@ mod tests {
builder.host_config.validation_upgrade_delay = 1000;
})
.add(123, || {
assert_ok!(ParachainSystem::schedule_upgrade(
assert_ok!(System::set_code(
RawOrigin::Root.into(),
Default::default()
));
})
.add(234, || {
assert_eq!(
ParachainSystem::schedule_upgrade(RawOrigin::Root.into(), Default::default()),
System::set_code(RawOrigin::Root.into(), Default::default()),
Err(Error::<Test>::OverlappingUpgrades.into()),
)
});
@@ -1325,7 +1296,7 @@ mod tests {
!PendingValidationFunction::exists(),
"validation function must not exist yet"
);
assert_ok!(ParachainSystem::schedule_upgrade(
assert_ok!(System::set_code(
RawOrigin::Root.into(),
Default::default()
));
@@ -1354,7 +1325,7 @@ mod tests {
})
.add(123, || {
assert_eq!(
ParachainSystem::schedule_upgrade(RawOrigin::Root.into(), vec![0; 64]),
System::set_code(RawOrigin::Root.into(), vec![0; 64]),
Err(Error::<Test>::TooBig.into()),
);
});