Skip to content

Commit

Permalink
pallet_referenda: ReferendumInfoFor storage migration
Browse files Browse the repository at this point in the history
  • Loading branch information
SunTiebing committed Jan 26, 2025
1 parent 65be024 commit ec04d62
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
2 changes: 2 additions & 0 deletions runtime/bifrost-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2022,6 +2022,7 @@ parameter_types! {
pub mod migrations {
#![allow(unused_imports)]
use super::*;
use crate::migration::update_referenda_referendum_info;
use migration::{
system_maker::SystemMakerClearPalletId, vsbond_auction::VSBondAuctionClearPalletId,
};
Expand All @@ -2032,6 +2033,7 @@ pub mod migrations {
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
bifrost_parachain_staking::migrations::v1::MigrateToV1<Runtime>,
bifrost_vtoken_voting::migration::v5::MigrateToV5<Runtime>,
update_referenda_referendum_info::MigrateReferendumInfoFor,
);
}

Expand Down
45 changes: 45 additions & 0 deletions runtime/bifrost-kusama/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,3 +792,48 @@ pub mod vsbond_auction {
}
}
}

pub mod update_referenda_referendum_info {
use crate::{Runtime, Weight};
use frame_support::traits::OnRuntimeUpgrade;
use pallet_referenda::{ReferendumIndex, ReferendumInfoFor, ReferendumInfoOf};

pub struct MigrateReferendumInfoFor;

impl OnRuntimeUpgrade for MigrateReferendumInfoFor {
fn on_runtime_upgrade() -> Weight {
let current_block_number = frame_system::Pallet::<Runtime>::block_number();
let mut weight: Weight = Weight::zero();

// Translate the storage and update accordingly
ReferendumInfoFor::<Runtime>::translate::<ReferendumInfoOf<Runtime, ()>, _>(
|_: ReferendumIndex, value: ReferendumInfoOf<Runtime, ()>| {
weight += <Runtime as frame_system::Config>::DbWeight::get().reads(1);

if let ReferendumInfoOf::<Runtime, ()>::Ongoing(mut status) = value {
// Check if there's an alarm and update the block numbers
if let Some((mut block, (mut end_schedule_block, value))) = status.alarm {
block = block
.saturating_sub(current_block_number)
.saturating_add(block);
end_schedule_block = end_schedule_block
.saturating_sub(current_block_number)
.saturating_add(end_schedule_block);

// Update the status with the new block numbers
status.alarm = Some((block, (end_schedule_block, value)));
}

// Wrap the updated status back into the ReferendumInfo enum
Some(ReferendumInfoOf::<Runtime, ()>::Ongoing(status))
} else {
// If the status isn't Ongoing, return it unchanged
Some(value)
}
},
);

weight + <Runtime as frame_system::Config>::DbWeight::get().writes(1)
}
}
}
2 changes: 2 additions & 0 deletions runtime/bifrost-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1943,13 +1943,15 @@ parameter_types! {
pub mod migrations {
#[allow(unused_imports)]
use super::*;
use crate::migration::update_referenda_referendum_info;

/// Unreleased migrations. Add new ones here:
pub type Unreleased = (
// permanent migration, do not remove
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
bifrost_parachain_staking::migrations::v1::MigrateToV1<Runtime>,
bifrost_vtoken_voting::migration::v5::MigrateToV5<Runtime>,
update_referenda_referendum_info::MigrateReferendumInfoFor,
);
}

Expand Down
45 changes: 45 additions & 0 deletions runtime/bifrost-polkadot/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,3 +480,48 @@ pub mod update_evm_min_gas_price {
}
}
}

pub mod update_referenda_referendum_info {
use crate::{Runtime, Weight};
use frame_support::traits::OnRuntimeUpgrade;
use pallet_referenda::{ReferendumIndex, ReferendumInfoFor, ReferendumInfoOf};

pub struct MigrateReferendumInfoFor;

impl OnRuntimeUpgrade for MigrateReferendumInfoFor {
fn on_runtime_upgrade() -> Weight {
let current_block_number = frame_system::Pallet::<Runtime>::block_number();
let mut weight: Weight = Weight::zero();

// Translate the storage and update accordingly
ReferendumInfoFor::<Runtime>::translate::<ReferendumInfoOf<Runtime, ()>, _>(
|_: ReferendumIndex, value: ReferendumInfoOf<Runtime, ()>| {
weight += <Runtime as frame_system::Config>::DbWeight::get().reads(1);

if let ReferendumInfoOf::<Runtime, ()>::Ongoing(mut status) = value {
// Check if there's an alarm and update the block numbers
if let Some((mut block, (mut end_schedule_block, value))) = status.alarm {
block = block
.saturating_sub(current_block_number)
.saturating_add(block);
end_schedule_block = end_schedule_block
.saturating_sub(current_block_number)
.saturating_add(end_schedule_block);

// Update the status with the new block numbers
status.alarm = Some((block, (end_schedule_block, value)));
}

// Wrap the updated status back into the ReferendumInfo enum
Some(ReferendumInfoOf::<Runtime, ()>::Ongoing(status))
} else {
// If the status isn't Ongoing, return it unchanged
Some(value)
}
},
);

weight + <Runtime as frame_system::Config>::DbWeight::get().writes(1)
}
}
}

0 comments on commit ec04d62

Please sign in to comment.