Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Staking] Kill unnecessary storage bloat (#970)
Co-authored-by: Joshy Orndorff <JoshOrndorff@users.noreply.github.com>
- Loading branch information
1 parent
ee43e58
commit 0dac2df
Showing
6 changed files
with
151 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2019-2021 PureStake Inc. | ||
// This file is part of Moonbeam. | ||
|
||
// Moonbeam is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Moonbeam is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
//! # Migrations | ||
use crate::{Config, Points, Round, Staked}; | ||
use frame_support::{ | ||
pallet_prelude::PhantomData, | ||
traits::{Get, OnRuntimeUpgrade}, | ||
weights::Weight, | ||
}; | ||
|
||
/// Migration to purge staking storage bloat for `Points` and `AtStake` storage items | ||
pub struct PurgeStaleStorage<T>(PhantomData<T>); | ||
impl<T: Config> OnRuntimeUpgrade for PurgeStaleStorage<T> { | ||
fn on_runtime_upgrade() -> Weight { | ||
log::info!(target: "PurgeStaleStorage", "running migration to remove storage bloat"); | ||
let current_round = <Round<T>>::get().current; | ||
let payment_delay = T::RewardPaymentDelay::get(); | ||
let db_weight = T::DbWeight::get(); | ||
let (reads, mut writes) = (3u64, 0u64); | ||
if current_round <= payment_delay { | ||
// early enough so no storage bloat exists yet | ||
// (only relevant for chains <= payment_delay rounds old) | ||
return db_weight.reads(reads); | ||
} | ||
// already paid out at the beginning of current round | ||
let most_recent_round_to_kill = current_round - payment_delay; | ||
for i in 1..=most_recent_round_to_kill { | ||
writes += 2u64; | ||
<Staked<T>>::remove(i); | ||
<Points<T>>::remove(i); | ||
} | ||
// 5% of the max block weight as safety margin for computation | ||
db_weight.reads(reads) + db_weight.writes(writes) + 25_000_000_000 | ||
} | ||
|
||
#[cfg(feature = "try-runtime")] | ||
fn pre_upgrade() -> Result<(), &'static str> { | ||
// trivial migration | ||
Ok(()) | ||
} | ||
|
||
#[cfg(feature = "try-runtime")] | ||
fn post_upgrade() -> Result<(), &'static str> { | ||
// expect only the storage items for the last 2 rounds to be stored | ||
let staked_count = Staked::<T>::iter().count() as u64; | ||
let points_count = Points::<T>::iter().count() as u64; | ||
let delay = T::RewardPaymentDelay::get(); | ||
assert_eq!( | ||
staked_count, delay, | ||
"Expected {} for `Staked` count, Found: {}", | ||
delay, staked_count | ||
); | ||
assert_eq!( | ||
points_count, delay, | ||
"Expected {} for `Points` count, Found: {}", | ||
delay, staked_count | ||
); | ||
Ok(()) | ||
} | ||
} |
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