referrerpolicy=no-referrer-when-downgrade

polkadot_runtime_parachains/shared/
migration.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14use super::*;
15use codec::{Decode, Encode};
16use frame_support::{
17	pallet_prelude::ValueQuery, traits::UncheckedOnRuntimeUpgrade, weights::Weight,
18};
19
20#[cfg(feature = "try-runtime")]
21const LOG_TARGET: &str = "runtime::shared";
22
23pub mod v0 {
24	use super::*;
25	use alloc::collections::vec_deque::VecDeque;
26
27	use frame_support::storage_alias;
28
29	/// All allowed relay-parents storage at version 0.
30	#[storage_alias]
31	pub(crate) type AllowedRelayParents<T: Config> = StorageValue<
32		Pallet<T>,
33		super::v0::AllowedRelayParentsTracker<<T as frame_system::Config>::Hash, BlockNumberFor<T>>,
34		ValueQuery,
35	>;
36
37	#[derive(Encode, Decode, Default, TypeInfo)]
38	pub struct AllowedRelayParentsTracker<Hash, BlockNumber> {
39		// The past relay parents, paired with state roots, that are viable to build upon.
40		//
41		// They are in ascending chronologic order, so the newest relay parents are at
42		// the back of the deque.
43		//
44		// (relay_parent, state_root)
45		pub buffer: VecDeque<(Hash, Hash)>,
46
47		// The number of the most recent relay-parent, if any.
48		// If the buffer is empty, this value has no meaning and may
49		// be nonsensical.
50		pub latest_number: BlockNumber,
51	}
52
53	// Required to workaround #64.
54	impl<Hash: PartialEq + Copy, BlockNumber: AtLeast32BitUnsigned + Copy>
55		AllowedRelayParentsTracker<Hash, BlockNumber>
56	{
57		/// Returns block number of the earliest block the buffer would contain if
58		/// `now` is pushed into it.
59		pub(crate) fn hypothetical_earliest_block_number(
60			&self,
61			now: BlockNumber,
62			max_ancestry_len: u32,
63		) -> BlockNumber {
64			let allowed_ancestry_len = max_ancestry_len.min(self.buffer.len() as u32);
65
66			now - allowed_ancestry_len.into()
67		}
68	}
69
70	impl<Hash, BlockNumber> From<AllowedRelayParentsTracker<Hash, BlockNumber>>
71		for super::AllowedRelayParentsTracker<Hash, BlockNumber>
72	{
73		fn from(value: AllowedRelayParentsTracker<Hash, BlockNumber>) -> Self {
74			Self {
75				latest_number: value.latest_number,
76				buffer: value
77					.buffer
78					.into_iter()
79					.map(|(relay_parent, state_root)| super::RelayParentInfo {
80						relay_parent,
81						state_root,
82						claim_queue: Default::default(),
83					})
84					.collect(),
85			}
86		}
87	}
88}
89
90mod v1 {
91	use super::*;
92
93	#[cfg(feature = "try-runtime")]
94	use frame_support::{
95		ensure,
96		traits::{GetStorageVersion, StorageVersion},
97	};
98
99	pub struct VersionUncheckedMigrateToV1<T>(core::marker::PhantomData<T>);
100
101	impl<T: Config> UncheckedOnRuntimeUpgrade for VersionUncheckedMigrateToV1<T> {
102		#[cfg(feature = "try-runtime")]
103		fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
104			log::trace!(target: LOG_TARGET, "Running pre_upgrade() for shared MigrateToV1");
105			let bytes = u32::to_ne_bytes(v0::AllowedRelayParents::<T>::get().buffer.len() as u32);
106
107			Ok(bytes.to_vec())
108		}
109
110		fn on_runtime_upgrade() -> Weight {
111			let mut weight: Weight = Weight::zero();
112
113			// Read old storage.
114			let old_rp_tracker = v0::AllowedRelayParents::<T>::take();
115
116			super::AllowedRelayParents::<T>::set(old_rp_tracker.into());
117
118			weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
119
120			weight
121		}
122
123		#[cfg(feature = "try-runtime")]
124		fn post_upgrade(state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
125			log::trace!(target: LOG_TARGET, "Running post_upgrade() for shared MigrateToV1");
126			ensure!(
127				Pallet::<T>::on_chain_storage_version() >= StorageVersion::new(1),
128				"Storage version should be >= 1 after the migration"
129			);
130
131			let relay_parent_count = u32::from_ne_bytes(
132				state
133					.try_into()
134					.expect("u32::from_ne_bytes(to_ne_bytes(u32)) always works; qed"),
135			);
136
137			let rp_tracker = AllowedRelayParents::<T>::get();
138
139			ensure!(
140				relay_parent_count as usize == rp_tracker.buffer.len(),
141				"Number of allowed relay parents should be the same as the one before the upgrade."
142			);
143
144			Ok(())
145		}
146	}
147}
148
149/// Migrate shared module storage to v1.
150pub type MigrateToV1<T> = frame_support::migrations::VersionedMigration<
151	0,
152	1,
153	v1::VersionUncheckedMigrateToV1<T>,
154	Pallet<T>,
155	<T as frame_system::Config>::DbWeight,
156>;
157
158#[cfg(test)]
159mod tests {
160	use super::{v1::VersionUncheckedMigrateToV1, *};
161	use crate::mock::{new_test_ext, MockGenesisConfig, Test};
162	use frame_support::traits::UncheckedOnRuntimeUpgrade;
163	use polkadot_primitives::Hash;
164
165	#[test]
166	fn migrate_to_v1() {
167		new_test_ext(MockGenesisConfig::default()).execute_with(|| {
168			let rp_tracker = v0::AllowedRelayParentsTracker {
169				latest_number: 9,
170				buffer: (0..10u64)
171					.into_iter()
172					.map(|idx| (Hash::from_low_u64_ne(idx), Hash::from_low_u64_ne(2 * idx)))
173					.collect::<VecDeque<_>>(),
174			};
175
176			v0::AllowedRelayParents::<Test>::put(rp_tracker);
177
178			<VersionUncheckedMigrateToV1<Test> as UncheckedOnRuntimeUpgrade>::on_runtime_upgrade();
179
180			let rp_tracker = AllowedRelayParents::<Test>::get();
181
182			assert_eq!(rp_tracker.buffer.len(), 10);
183
184			for idx in 0..10u64 {
185				let relay_parent = Hash::from_low_u64_ne(idx);
186				let state_root = Hash::from_low_u64_ne(2 * idx);
187				let (info, block_num) = rp_tracker.acquire_info(relay_parent, None).unwrap();
188
189				assert!(info.claim_queue.is_empty());
190				assert_eq!(info.relay_parent, relay_parent);
191				assert_eq!(info.state_root, state_root);
192				assert_eq!(block_num as u64, idx);
193			}
194		});
195	}
196}