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

Commit

Permalink
grandpa: Storage migration for srml-grandpa module.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimpo committed Oct 1, 2019
1 parent 08922b8 commit 67408c8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions srml/grandpa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ std = [
"session/std",
"finality-tracker/std",
]
migrate-authorities = []
19 changes: 19 additions & 0 deletions srml/grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ decl_event!(

decl_storage! {
trait Store for Module<T: Trait> as GrandpaFinality {
/// DEPRECATED
///
/// This used to store the current authority set, which has been migrated to the well-known
/// GRANDPA_AUTHORITES_KEY unhashed key.
#[cfg(feature = "migrate-authorities")]
pub(crate) Authorities get(authorities): AuthorityList;

/// State of the current authority set.
State get(state): StoredState<T::BlockNumber> = StoredState::Live;

Expand Down Expand Up @@ -172,6 +179,11 @@ decl_module! {
// FIXME: https://github.com/paritytech/substrate/issues/1112
}

fn on_initialize() {
#[cfg(feature = "migrate-authorities")]
Self::migrate_authorities();
}

fn on_finalize(block_number: T::BlockNumber) {
// check for scheduled pending authority set changes
if let Some(pending_change) = <PendingChange<T>>::get() {
Expand Down Expand Up @@ -341,6 +353,13 @@ impl<T: Trait> Module<T> {
Self::set_grandpa_authorities(authorities);
}
}

#[cfg(feature = "migrate-authorities")]
fn migrate_authorities() {
if Authorities::exists() {
Self::set_grandpa_authorities(&Authorities::take());
}
}
}

impl<T: Trait> Module<T> {
Expand Down
18 changes: 18 additions & 0 deletions srml/grandpa/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,21 @@ fn time_slot_have_sane_ord() {
];
assert!(FIXTURE.windows(2).all(|f| f[0] < f[1]));
}

#[test]
#[cfg(feature = "migrate-authorities")]
fn authorities_migration() {
use sr_primitives::traits::OnInitialize;

with_externalities(&mut new_test_ext(vec![]), || {
let authorities = to_authorities(vec![(1, 1), (2, 1), (3, 1)]);

Authorities::put(authorities.clone());
assert!(Grandpa::grandpa_authorities().is_empty());

Grandpa::on_initialize(1);

assert!(!Authorities::exists());
assert_eq!(Grandpa::grandpa_authorities(), authorities);
});
}

0 comments on commit 67408c8

Please sign in to comment.