Skip to content

Commit

Permalink
fix referenda migration
Browse files Browse the repository at this point in the history
  • Loading branch information
SunTiebing committed Feb 27, 2025
1 parent ab33779 commit a0a07b2
Showing 1 changed file with 37 additions and 31 deletions.
68 changes: 37 additions & 31 deletions runtime/bifrost-polkadot/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,46 +482,52 @@ pub mod update_evm_min_gas_price {
}

pub mod update_referenda_referendum_info {
use crate::{Runtime, Weight};
use crate::{Referenda, Runtime, Weight};
use frame_support::pallet_prelude::StorageVersion;
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)));
}
if StorageVersion::get::<Referenda>() == 1 {
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)
}
},
);
// 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)
weight + <Runtime as frame_system::Config>::DbWeight::get().writes(1)
} else {
<Runtime as frame_system::Config>::DbWeight::get().reads(1)
}
}
}
}

0 comments on commit a0a07b2

Please sign in to comment.