Skip to content

Commit

Permalink
limit iterations on evict_anchors (centrifuge#969)
Browse files Browse the repository at this point in the history
* 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
lemunozm authored Sep 13, 2022
1 parent 0c9382d commit a7b0c6f
Show file tree
Hide file tree
Showing 12 changed files with 269 additions and 69 deletions.
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "centrifuge-chain"
version = "0.10.19"
version = "0.10.20"
authors = ["Centrifuge <admin@centrifuge.io>"]
description = "Centrifuge chain implementation in Rust."
build = "build.rs"
Expand Down Expand Up @@ -198,6 +198,7 @@ cli = [
]
try-runtime = [
"centrifuge-runtime/try-runtime",
"altair-runtime/try-runtime",
"try-runtime-cli"
]

Expand Down
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
};

# This is a hash of all the Cargo dependencies, for reproducibility.
cargoSha256 = "kFHDl4I5rcr70DlXOA45JijbGE/s3rhQhq2mSOUeH1c=";
cargoSha256 = "sha256-cR0wDbc3hJr0wXUbOKjsBppVmQQLLPeZs4O/j3f1MBc=";

nativeBuildInputs = with pkgs; [ clang git-mock pkg-config ];
buildInputs = with pkgs; [ openssl ] ++ (
Expand Down
1 change: 1 addition & 0 deletions pallets/anchors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
]
try-runtime = ["frame-support/try-runtime"]
std = [
'codec/std',
'sp-core/std',
Expand Down
2 changes: 1 addition & 1 deletion pallets/anchors/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ benchmarks! {
)?;
}

pallet_timestamp::Pallet::<T>::set_timestamp(day(2));
pallet_timestamp::Pallet::<T>::set_timestamp(day(MAX_LOOP_IN_TX));

}: _(RawOrigin::Signed(caller))
verify {
Expand Down
11 changes: 9 additions & 2 deletions pallets/anchors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

pub mod migration;

mod common;

type BalanceOf<T> =
Expand All @@ -58,7 +60,7 @@ type BalanceOf<T> =
const PRE_COMMIT_EXPIRATION_DURATION_BLOCKS: u32 = 800;

/// Determines how many loop iterations are allowed to run at a time inside the runtime.
const MAX_LOOP_IN_TX: u64 = 500;
const MAX_LOOP_IN_TX: u64 = 100;

/// date 3000-01-01 -> 376200 days from unix epoch
const STORAGE_MAX_DAYS: u32 = 376200;
Expand Down Expand Up @@ -385,10 +387,15 @@ pub mod pallet {
.ok_or(ArithmeticError::Overflow)?;

// store yesterday as the last day of eviction
let yesterday = today_in_days_from_epoch
let mut yesterday = today_in_days_from_epoch
.checked_sub(1)
.ok_or(ArithmeticError::Underflow)?;

// Avoid to iterate more than 500 days
if yesterday > evict_date + MAX_LOOP_IN_TX as u32 {
yesterday = evict_date + MAX_LOOP_IN_TX as u32 - 1;
}

// remove child tries starting from day next to last evicted day
let _evicted_trie_count =
Self::evict_anchor_child_tries(evict_date, today_in_days_from_epoch);
Expand Down
78 changes: 78 additions & 0 deletions pallets/anchors/src/migration.rs
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)
);
});
}
}
Loading

0 comments on commit a7b0c6f

Please sign in to comment.