-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use CountedStorageMap instead of StorageMap for Jurors in court pallet (
#707) * Use CountedStorageMap instead of StorageMap for Jurors in court pallet * Add migration for Jurors in Court from StorageMap to CountedStorageMap * Remove storage migration for RemoveDisputesOfResolvedMarkets
- Loading branch information
1 parent
87264f8
commit 789d565
Showing
5 changed files
with
107 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
use crate::{Config, Jurors, Pallet}; | ||
use frame_support::{ | ||
dispatch::Weight, | ||
log, | ||
pallet_prelude::PhantomData, | ||
traits::{Get, OnRuntimeUpgrade, StorageVersion}, | ||
}; | ||
|
||
const COURT_REQUIRED_STORAGE_VERSION: u16 = 0; | ||
const COURT_NEXT_STORAGE_VERSION: u16 = 1; | ||
|
||
pub struct JurorsCountedStorageMapMigration<T>(PhantomData<T>); | ||
|
||
impl<T: Config> OnRuntimeUpgrade for JurorsCountedStorageMapMigration<T> { | ||
fn on_runtime_upgrade() -> Weight | ||
where | ||
T: Config, | ||
{ | ||
let mut total_weight = T::DbWeight::get().reads(1); | ||
|
||
if StorageVersion::get::<Pallet<T>>() != COURT_REQUIRED_STORAGE_VERSION { | ||
log::info!("Skipping storage migration of jurors of court already up to date"); | ||
return total_weight; | ||
} | ||
log::info!("Starting storage migration of jurors of court"); | ||
Jurors::<T>::initialize_counter(); | ||
total_weight = total_weight.saturating_add( | ||
T::DbWeight::get().writes(Jurors::<T>::count().saturating_add(1) as u64), | ||
); | ||
|
||
StorageVersion::new(COURT_NEXT_STORAGE_VERSION).put::<Pallet<T>>(); | ||
total_weight = total_weight.saturating_add(T::DbWeight::get().writes(1)); | ||
|
||
log::info!("Completed storage migration of jurors of court"); | ||
total_weight | ||
} | ||
|
||
#[cfg(feature = "try-runtime")] | ||
fn pre_upgrade() -> Result<(), &'static str> { | ||
Ok(()) | ||
} | ||
|
||
#[cfg(feature = "try-runtime")] | ||
fn post_upgrade() -> Result<(), &'static str> { | ||
let court_storage_version = StorageVersion::get::<Pallet<T>>(); | ||
assert_eq!( | ||
court_storage_version, COURT_NEXT_STORAGE_VERSION, | ||
"found unexpected court pallet storage version. Found: {:?}. Expected: {:?}", | ||
court_storage_version, COURT_NEXT_STORAGE_VERSION, | ||
); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::{mock::*, Juror}; | ||
use frame_support::Hashable; | ||
|
||
#[test] | ||
fn test_on_runtime_upgrade_on_untouched_chain() { | ||
ExtBuilder::default().build().execute_with(|| { | ||
setup_chain(); | ||
JurorsCountedStorageMapMigration::<Runtime>::on_runtime_upgrade(); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn on_runtime_upgrade_updates_storage_versions() { | ||
ExtBuilder::default().build().execute_with(|| { | ||
setup_chain(); | ||
JurorsCountedStorageMapMigration::<Runtime>::on_runtime_upgrade(); | ||
assert_eq!(StorageVersion::get::<Pallet<Runtime>>(), COURT_NEXT_STORAGE_VERSION,); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_on_runtime_upgrade() { | ||
ExtBuilder::default().build().execute_with(|| { | ||
setup_chain(); | ||
let alice_hash = ALICE.blake2_128_concat(); | ||
let alice_juror = Juror { status: crate::JurorStatus::Ok }; | ||
frame_support::migration::put_storage_value( | ||
b"Court", | ||
b"Jurors", | ||
&alice_hash, | ||
alice_juror.clone(), | ||
); | ||
assert_eq!(Jurors::<Runtime>::count(), 0_u32); | ||
JurorsCountedStorageMapMigration::<Runtime>::on_runtime_upgrade(); | ||
assert_eq!(Jurors::<Runtime>::count(), 1_u32); | ||
assert_eq!(Jurors::<Runtime>::get(&ALICE), Some(alice_juror)); | ||
}); | ||
} | ||
|
||
fn setup_chain() { | ||
StorageVersion::new(COURT_REQUIRED_STORAGE_VERSION).put::<Pallet<Runtime>>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,7 +67,6 @@ | |
extern crate alloc; | ||
|
||
mod benchmarks; | ||
pub mod migrations; | ||
pub mod mock; | ||
mod tests; | ||
pub mod weights; | ||
|
This file was deleted.
Oops, something went wrong.