Add Version Checks on Para Upgrade (#2261)

* add version checks on para ugprade

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <git@kchr.de>

* remove unneeded imports and errors

* fix test error path

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
This commit is contained in:
joe petrowski
2023-03-02 16:48:14 +01:00
committed by GitHub
parent 58547085e7
commit 3249186fe6
2 changed files with 100 additions and 19 deletions
+36 -1
View File
@@ -32,10 +32,11 @@ use frame_support::{
use frame_system::RawOrigin;
use hex_literal::hex;
use relay_chain::HrmpChannelId;
use sp_core::H256;
use sp_core::{blake2_256, H256};
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
DispatchErrorWithPostInfo,
};
use sp_version::RuntimeVersion;
use std::cell::RefCell;
@@ -974,3 +975,37 @@ fn test() {
.add(1, || {})
.add(2, || {});
}
#[test]
fn upgrade_version_checks_should_work() {
let test_data = vec![
("test", 0, 1, Err(frame_system::Error::<Test>::SpecVersionNeedsToIncrease)),
("test", 1, 0, Err(frame_system::Error::<Test>::SpecVersionNeedsToIncrease)),
("test", 1, 1, Err(frame_system::Error::<Test>::SpecVersionNeedsToIncrease)),
("test", 1, 2, Err(frame_system::Error::<Test>::SpecVersionNeedsToIncrease)),
("test2", 1, 1, Err(frame_system::Error::<Test>::InvalidSpecName)),
];
for (spec_name, spec_version, impl_version, expected) in test_data.into_iter() {
let version = RuntimeVersion {
spec_name: spec_name.into(),
spec_version,
impl_version,
..Default::default()
};
let read_runtime_version = ReadRuntimeVersion(version.encode());
let mut ext = new_test_ext();
ext.register_extension(sp_core::traits::ReadRuntimeVersionExt::new(read_runtime_version));
ext.execute_with(|| {
let new_code = vec![1, 2, 3, 4];
let new_code_hash = sp_core::H256(blake2_256(&new_code));
let _authorize =
ParachainSystem::authorize_upgrade(RawOrigin::Root.into(), new_code_hash, true);
let res = ParachainSystem::enact_authorized_upgrade(RawOrigin::None.into(), new_code);
assert_eq!(expected.map_err(DispatchErrorWithPostInfo::from), res);
});
}
}