Skip to content

Commit

Permalink
refactor: remove stale from perseverance as well
Browse files Browse the repository at this point in the history
  • Loading branch information
j4m1ef0rd committed Oct 3, 2024
1 parent 798e8d4 commit cb01a23
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 60 deletions.
1 change: 1 addition & 0 deletions state-chain/pallets/cf-broadcast/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use cf_runtime_upgrade_utilities::{PlaceholderMigration, VersionedMigration};

mod initialize_broadcast_timeout_storage;
mod migrate_timeouts;
pub mod remove_aborted_broadcasts;

pub type PalletMigration<T, I> = (
VersionedMigration<Pallet<T, I>, initialize_broadcast_timeout_storage::Migration<T, I>, 6, 7>,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::*;

// Highest stale aborted broadcasts as of 3/10/2024:
// Mainnet
pub const ETHEREUM_MAX_ABORTED_BROADCAST_BERGHAIN: BroadcastId = 11592;
pub const ARBITRUM_MAX_ABORTED_BROADCAST_BERGHAIN: BroadcastId = 426;
// Perseverance testnet
pub const ETHEREUM_MAX_ABORTED_BROADCAST_PERSEVERANCE: BroadcastId = 1609;
pub const ARBITRUM_MAX_ABORTED_BROADCAST_PERSEVERANCE: BroadcastId = 665;
pub const POLKADOT_MAX_ABORTED_BROADCAST_PERSEVERANCE: BroadcastId = 634;

pub struct EthereumMigration;
pub struct ArbitrumMigration;

pub fn remove_stale_and_all_older<T: Config<I>, I: 'static>(latest_stale_broadcast: BroadcastId) {
AbortedBroadcasts::<T, I>::mutate(|aborted| {
aborted.retain(|id| id > &latest_stale_broadcast);
});
}

#[cfg(feature = "try-runtime")]
pub fn assert_removed<T: Config<I>, I: 'static>(latest_stale_broadcast: BroadcastId) {
let aborted_broadcasts = AbortedBroadcasts::<T, I>::get();
if let Some(first) = aborted_broadcasts.first() {
assert!(*first > latest_stale_broadcast, "Aborted broadcast {first} was not removed");
}
}
1 change: 0 additions & 1 deletion state-chain/runtime/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
pub mod housekeeping;
pub mod reap_old_accounts;
pub mod remove_aborted_broadcasts;
pub mod serialize_solana_broadcast;
49 changes: 42 additions & 7 deletions state-chain/runtime/src/migrations/housekeeping.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use crate::{migrations::remove_aborted_broadcasts, Runtime};
use crate::Runtime;
use cf_chains::instances::{ArbitrumInstance, EthereumInstance, PolkadotInstance};
use cf_runtime_upgrade_utilities::genesis_hashes;
use frame_support::{traits::OnRuntimeUpgrade, weights::Weight};
use pallet_cf_broadcast::migrations::remove_aborted_broadcasts;
#[cfg(feature = "try-runtime")]
use sp_runtime::DispatchError;
#[cfg(feature = "try-runtime")]
use sp_std::vec::Vec;

pub struct Migration;

Expand All @@ -11,11 +15,24 @@ impl OnRuntimeUpgrade for Migration {
match genesis_hashes::genesis_hash::<Runtime>() {
genesis_hashes::BERGHAIN => {
log::info!("🧹 Housekeeping, removing stale aborted broadcasts");
remove_aborted_broadcasts::EthereumMigration::on_runtime_upgrade();
remove_aborted_broadcasts::ArbitrumMigration::on_runtime_upgrade();
remove_aborted_broadcasts::remove_stale_and_all_older::<Runtime, EthereumInstance>(
remove_aborted_broadcasts::ETHEREUM_MAX_ABORTED_BROADCAST_BERGHAIN,
);
remove_aborted_broadcasts::remove_stale_and_all_older::<Runtime, ArbitrumInstance>(
remove_aborted_broadcasts::ARBITRUM_MAX_ABORTED_BROADCAST_BERGHAIN,
);
},
genesis_hashes::PERSEVERANCE => {
log::info!("🧹 No housekeeping required for Perseverance.");
log::info!("🧹 Housekeeping, removing stale aborted broadcasts");
remove_aborted_broadcasts::remove_stale_and_all_older::<Runtime, EthereumInstance>(
remove_aborted_broadcasts::ETHEREUM_MAX_ABORTED_BROADCAST_PERSEVERANCE,
);
remove_aborted_broadcasts::remove_stale_and_all_older::<Runtime, ArbitrumInstance>(
remove_aborted_broadcasts::ARBITRUM_MAX_ABORTED_BROADCAST_PERSEVERANCE,
);
remove_aborted_broadcasts::remove_stale_and_all_older::<Runtime, PolkadotInstance>(
remove_aborted_broadcasts::POLKADOT_MAX_ABORTED_BROADCAST_PERSEVERANCE,
);
},
genesis_hashes::SISYPHOS => {
log::info!("🧹 No housekeeping required for Sisyphos.");
Expand All @@ -30,11 +47,29 @@ impl OnRuntimeUpgrade for Migration {
fn post_upgrade(_state: Vec<u8>) -> Result<(), DispatchError> {
match genesis_hashes::genesis_hash::<Runtime>() {
genesis_hashes::BERGHAIN => {
remove_aborted_broadcasts::EthereumMigration::post_upgrade();
remove_aborted_broadcasts::ArbitrumMigration::post_upgrade();
log::info!(
"Housekeeping post_upgrade, checking stale aborted broadcasts are removed."
);
remove_aborted_broadcasts::assert_removed::<Runtime, EthereumInstance>(
remove_aborted_broadcasts::ETHEREUM_MAX_ABORTED_BROADCAST_BERGHAIN,
);
remove_aborted_broadcasts::assert_removed::<Runtime, ArbitrumInstance>(
remove_aborted_broadcasts::ARBITRUM_MAX_ABORTED_BROADCAST_BERGHAIN,
);
},
genesis_hashes::PERSEVERANCE => {
log::info!("Skipping housekeeping post_upgrade for Perseverance.");
log::info!(
"Housekeeping post_upgrade, checking stale aborted broadcasts are removed."
);
remove_aborted_broadcasts::assert_removed::<Runtime, EthereumInstance>(
remove_aborted_broadcasts::ETHEREUM_MAX_ABORTED_BROADCAST_PERSEVERANCE,
);
remove_aborted_broadcasts::assert_removed::<Runtime, ArbitrumInstance>(
remove_aborted_broadcasts::ARBITRUM_MAX_ABORTED_BROADCAST_PERSEVERANCE,
);
remove_aborted_broadcasts::assert_removed::<Runtime, PolkadotInstance>(
remove_aborted_broadcasts::POLKADOT_MAX_ABORTED_BROADCAST_PERSEVERANCE,
);
},
genesis_hashes::SISYPHOS => {
log::info!("Skipping housekeeping post_upgrade for Sisyphos.");
Expand Down
52 changes: 0 additions & 52 deletions state-chain/runtime/src/migrations/remove_aborted_broadcasts.rs

This file was deleted.

0 comments on commit cb01a23

Please sign in to comment.