Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 67408c8

Browse files
committed
grandpa: Storage migration for srml-grandpa module.
1 parent 08922b8 commit 67408c8

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

srml/grandpa/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ std = [
3535
"session/std",
3636
"finality-tracker/std",
3737
]
38+
migrate-authorities = []

srml/grandpa/src/lib.rs

+19
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ decl_event!(
137137

138138
decl_storage! {
139139
trait Store for Module<T: Trait> as GrandpaFinality {
140+
/// DEPRECATED
141+
///
142+
/// This used to store the current authority set, which has been migrated to the well-known
143+
/// GRANDPA_AUTHORITES_KEY unhashed key.
144+
#[cfg(feature = "migrate-authorities")]
145+
pub(crate) Authorities get(authorities): AuthorityList;
146+
140147
/// State of the current authority set.
141148
State get(state): StoredState<T::BlockNumber> = StoredState::Live;
142149

@@ -172,6 +179,11 @@ decl_module! {
172179
// FIXME: https://github.com/paritytech/substrate/issues/1112
173180
}
174181

182+
fn on_initialize() {
183+
#[cfg(feature = "migrate-authorities")]
184+
Self::migrate_authorities();
185+
}
186+
175187
fn on_finalize(block_number: T::BlockNumber) {
176188
// check for scheduled pending authority set changes
177189
if let Some(pending_change) = <PendingChange<T>>::get() {
@@ -341,6 +353,13 @@ impl<T: Trait> Module<T> {
341353
Self::set_grandpa_authorities(authorities);
342354
}
343355
}
356+
357+
#[cfg(feature = "migrate-authorities")]
358+
fn migrate_authorities() {
359+
if Authorities::exists() {
360+
Self::set_grandpa_authorities(&Authorities::take());
361+
}
362+
}
344363
}
345364

346365
impl<T: Trait> Module<T> {

srml/grandpa/src/tests.rs

+18
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,21 @@ fn time_slot_have_sane_ord() {
310310
];
311311
assert!(FIXTURE.windows(2).all(|f| f[0] < f[1]));
312312
}
313+
314+
#[test]
315+
#[cfg(feature = "migrate-authorities")]
316+
fn authorities_migration() {
317+
use sr_primitives::traits::OnInitialize;
318+
319+
with_externalities(&mut new_test_ext(vec![]), || {
320+
let authorities = to_authorities(vec![(1, 1), (2, 1), (3, 1)]);
321+
322+
Authorities::put(authorities.clone());
323+
assert!(Grandpa::grandpa_authorities().is_empty());
324+
325+
Grandpa::on_initialize(1);
326+
327+
assert!(!Authorities::exists());
328+
assert_eq!(Grandpa::grandpa_authorities(), authorities);
329+
});
330+
}

0 commit comments

Comments
 (0)