forked from centrifuge/centrifuge-chain
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
limit iterations on evict_anchors (centrifuge#969)
* limit iterations on evict_anchors * adapt tests to allow different MAX_LOOP_IN_TX values * add migration for evict_anchors * update versions * add nix * add migration to altair upgrade * add pre/post migrations from altair * fix migration on altair
- Loading branch information
Showing
12 changed files
with
269 additions
and
69 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,78 @@ | ||
use super::*; | ||
|
||
pub mod fix_evict_date { | ||
use super::*; | ||
|
||
use frame_support::{log, traits::Get, weights::Weight}; | ||
|
||
pub const HARDCODED_EVICTED_DATE: u32 = 19200; | ||
|
||
#[cfg(feature = "try-runtime")] | ||
use frame_support::ensure; // Not in prelude for try-runtime | ||
|
||
#[cfg(feature = "try-runtime")] | ||
pub fn pre_migrate<T: Config>() -> Result<(), &'static str> { | ||
ensure!( | ||
LatestEvictedDate::<T>::get() == None, | ||
"State already initialized" | ||
); | ||
Ok(()) | ||
} | ||
|
||
pub fn migrate<T: Config>() -> Weight { | ||
if LatestEvictedDate::<T>::get().is_none() { | ||
LatestEvictedDate::<T>::put(HARDCODED_EVICTED_DATE); | ||
log::info!("pallet_anchors: fix evict date"); | ||
return T::DbWeight::get().writes(1); | ||
} | ||
|
||
0 | ||
} | ||
|
||
#[cfg(feature = "try-runtime")] | ||
pub fn post_migrate<T: Config>() -> Result<(), &'static str> { | ||
ensure!( | ||
LatestEvictedDate::<T>::get() == Some(HARDCODED_EVICTED_DATE), | ||
"State not initialized" | ||
); | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
#[cfg(feature = "try-runtime")] | ||
mod test { | ||
use super::*; | ||
use crate::mock::{new_test_ext, Origin, Test}; | ||
use crate::{self as pallet_anchors}; | ||
use frame_support::assert_ok; | ||
|
||
#[test] | ||
fn evict_anchors_working_after_migration() { | ||
new_test_ext().execute_with(|| { | ||
// Check migration: | ||
assert_ok!(fix_evict_date::pre_migrate::<Test>()); | ||
assert!(fix_evict_date::post_migrate::<Test>().is_err()); | ||
|
||
fix_evict_date::migrate::<Test>(); | ||
|
||
assert_ok!(fix_evict_date::post_migrate::<Test>()); | ||
assert!(fix_evict_date::pre_migrate::<Test>().is_err()); | ||
|
||
// Check correct evict behaviour after migration: | ||
let current_day = common::MILLISECS_PER_DAY | ||
* (fix_evict_date::HARDCODED_EVICTED_DATE as u64 + MAX_LOOP_IN_TX * 3); | ||
|
||
pallet_timestamp::Pallet::<Test>::set_timestamp(current_day); | ||
|
||
assert_ok!(pallet_anchors::Pallet::<Test>::evict_anchors( | ||
Origin::signed(1) | ||
)); | ||
|
||
assert_eq!( | ||
LatestEvictedDate::<Test>::get(), | ||
Some(fix_evict_date::HARDCODED_EVICTED_DATE + MAX_LOOP_IN_TX as u32) | ||
); | ||
}); | ||
} | ||
} |
Oops, something went wrong.