|
| 1 | +use sp_runtime::runtime_logger::RuntimeLogger; |
| 2 | +use sp_std::collections::btree_map::BTreeMap; |
| 3 | + |
| 4 | +use super::*; |
| 5 | + |
| 6 | +mod v1 { |
| 7 | + use super::*; |
| 8 | + use polymesh_primitives::Ticker; |
| 9 | + |
| 10 | + decl_storage! { |
| 11 | + trait Store for Module<T: Config> as Checkpoint { |
| 12 | + // This storage changed the Ticker key to AssetID. |
| 13 | + pub TotalSupply get(fn total_supply_at): |
| 14 | + double_map hasher(blake2_128_concat) Ticker, hasher(twox_64_concat) CheckpointId => polymesh_primitives::Balance; |
| 15 | + |
| 16 | + // This storage changed the Ticker key to AssetID. |
| 17 | + pub Balance get(fn balance_at_checkpoint): |
| 18 | + double_map hasher(blake2_128_concat) (Ticker, CheckpointId), hasher(twox_64_concat) IdentityId => polymesh_primitives::Balance; |
| 19 | + |
| 20 | + // This storage changed the Ticker key to AssetID. |
| 21 | + pub CheckpointIdSequence get(fn checkpoint_id_sequence): |
| 22 | + map hasher(blake2_128_concat) Ticker => CheckpointId; |
| 23 | + |
| 24 | + // This storage changed the Ticker key to AssetID. |
| 25 | + pub BalanceUpdates get(fn balance_updates): |
| 26 | + double_map hasher(blake2_128_concat) Ticker, hasher(twox_64_concat) IdentityId => Vec<CheckpointId>; |
| 27 | + |
| 28 | + // This storage changed the Ticker key to AssetID. |
| 29 | + pub Timestamps get(fn timestamps): |
| 30 | + double_map hasher(blake2_128_concat) Ticker, hasher(twox_64_concat) CheckpointId => Moment; |
| 31 | + |
| 32 | + // This storage changed the Ticker key to AssetID. |
| 33 | + pub ScheduleIdSequence get(fn schedule_id_sequence): |
| 34 | + map hasher(blake2_128_concat) Ticker => ScheduleId; |
| 35 | + |
| 36 | + // This storage changed the Ticker key to AssetID. |
| 37 | + pub CachedNextCheckpoints get(fn cached_next_checkpoints): |
| 38 | + map hasher(blake2_128_concat) Ticker => Option<NextCheckpoints>; |
| 39 | + |
| 40 | + // This storage changed the Ticker key to AssetID. |
| 41 | + pub ScheduledCheckpoints get(fn scheduled_checkpoints): |
| 42 | + double_map hasher(blake2_128_concat) Ticker, hasher(twox_64_concat) ScheduleId => Option<ScheduleCheckpoints>; |
| 43 | + |
| 44 | + // This storage changed the Ticker key to AssetID. |
| 45 | + pub ScheduleRefCount get(fn schedule_ref_count): |
| 46 | + double_map hasher(blake2_128_concat) Ticker, hasher(twox_64_concat) ScheduleId => u32; |
| 47 | + |
| 48 | + // This storage changed the Ticker key to AssetID. |
| 49 | + pub SchedulePoints get(fn schedule_points): |
| 50 | + double_map hasher(blake2_128_concat) Ticker, hasher(twox_64_concat) ScheduleId => Vec<CheckpointId>; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + decl_module! { |
| 55 | + pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin { } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +pub(crate) fn migrate_to_v2<T: Config>() { |
| 60 | + RuntimeLogger::init(); |
| 61 | + let mut ticker_to_asset_id = BTreeMap::new(); |
| 62 | + |
| 63 | + // Removes all elements in the old storage and inserts it in the new storage |
| 64 | + |
| 65 | + let mut count = 0; |
| 66 | + log::info!("Updating types for the TotalSupply storage"); |
| 67 | + v1::TotalSupply::drain().for_each(|(ticker, checkpoint_id, balance)| { |
| 68 | + count += 1; |
| 69 | + let asset_id = ticker_to_asset_id |
| 70 | + .entry(ticker) |
| 71 | + .or_insert(AssetID::from(ticker)); |
| 72 | + TotalSupply::insert(asset_id, checkpoint_id, balance); |
| 73 | + }); |
| 74 | + log::info!("{:?} items migrated", count); |
| 75 | + |
| 76 | + let mut count = 0; |
| 77 | + log::info!("Updating types for the Balance storage"); |
| 78 | + v1::Balance::drain().for_each(|((ticker, checkpoint_id), did, balance)| { |
| 79 | + count += 1; |
| 80 | + let asset_id = ticker_to_asset_id |
| 81 | + .entry(ticker) |
| 82 | + .or_insert(AssetID::from(ticker)); |
| 83 | + Balance::insert((asset_id, checkpoint_id), did, balance); |
| 84 | + }); |
| 85 | + log::info!("{:?} items migrated", count); |
| 86 | + |
| 87 | + let mut count = 0; |
| 88 | + log::info!("Updating types for the CheckpointIdSequence storage"); |
| 89 | + v1::CheckpointIdSequence::drain().for_each(|(ticker, checkpoint_id)| { |
| 90 | + count += 1; |
| 91 | + let asset_id = ticker_to_asset_id |
| 92 | + .entry(ticker) |
| 93 | + .or_insert(AssetID::from(ticker)); |
| 94 | + CheckpointIdSequence::insert(asset_id, checkpoint_id); |
| 95 | + }); |
| 96 | + log::info!("{:?} items migrated", count); |
| 97 | + |
| 98 | + let mut count = 0; |
| 99 | + log::info!("Updating types for the BalanceUpdates storage"); |
| 100 | + v1::BalanceUpdates::drain().for_each(|(ticker, did, checkpoint_id)| { |
| 101 | + count += 1; |
| 102 | + let asset_id = ticker_to_asset_id |
| 103 | + .entry(ticker) |
| 104 | + .or_insert(AssetID::from(ticker)); |
| 105 | + BalanceUpdates::insert(asset_id, did, checkpoint_id); |
| 106 | + }); |
| 107 | + log::info!("{:?} items migrated", count); |
| 108 | + |
| 109 | + let mut count = 0; |
| 110 | + log::info!("Updating types for the Timestamps storage"); |
| 111 | + v1::Timestamps::drain().for_each(|(ticker, checkpoint_id, when)| { |
| 112 | + count += 1; |
| 113 | + let asset_id = ticker_to_asset_id |
| 114 | + .entry(ticker) |
| 115 | + .or_insert(AssetID::from(ticker)); |
| 116 | + Timestamps::insert(asset_id, checkpoint_id, when); |
| 117 | + }); |
| 118 | + log::info!("{:?} items migrated", count); |
| 119 | + |
| 120 | + let mut count = 0; |
| 121 | + log::info!("Updating types for the ScheduleIdSequence storage"); |
| 122 | + v1::ScheduleIdSequence::drain().for_each(|(ticker, schedule_id)| { |
| 123 | + count += 1; |
| 124 | + let asset_id = ticker_to_asset_id |
| 125 | + .entry(ticker) |
| 126 | + .or_insert(AssetID::from(ticker)); |
| 127 | + ScheduleIdSequence::insert(asset_id, schedule_id); |
| 128 | + }); |
| 129 | + log::info!("{:?} items migrated", count); |
| 130 | + |
| 131 | + let mut count = 0; |
| 132 | + log::info!("Updating types for the CachedNextCheckpoints storage"); |
| 133 | + v1::CachedNextCheckpoints::drain().for_each(|(ticker, next_checkpoint)| { |
| 134 | + count += 1; |
| 135 | + let asset_id = ticker_to_asset_id |
| 136 | + .entry(ticker) |
| 137 | + .or_insert(AssetID::from(ticker)); |
| 138 | + CachedNextCheckpoints::insert(asset_id, next_checkpoint); |
| 139 | + }); |
| 140 | + log::info!("{:?} items migrated", count); |
| 141 | + |
| 142 | + let mut count = 0; |
| 143 | + log::info!("Updating types for the ScheduledCheckpoints storage"); |
| 144 | + v1::ScheduledCheckpoints::drain().for_each(|(ticker, schedule_id, next_checkpoint)| { |
| 145 | + count += 1; |
| 146 | + let asset_id = ticker_to_asset_id |
| 147 | + .entry(ticker) |
| 148 | + .or_insert(AssetID::from(ticker)); |
| 149 | + ScheduledCheckpoints::insert(asset_id, schedule_id, next_checkpoint); |
| 150 | + }); |
| 151 | + log::info!("{:?} items migrated", count); |
| 152 | + |
| 153 | + let mut count = 0; |
| 154 | + log::info!("Updating types for the ScheduleRefCount storage"); |
| 155 | + v1::ScheduleRefCount::drain().for_each(|(ticker, schedule_id, ref_count)| { |
| 156 | + count += 1; |
| 157 | + let asset_id = ticker_to_asset_id |
| 158 | + .entry(ticker) |
| 159 | + .or_insert(AssetID::from(ticker)); |
| 160 | + ScheduleRefCount::insert(asset_id, schedule_id, ref_count); |
| 161 | + }); |
| 162 | + log::info!("{:?} items migrated", count); |
| 163 | + |
| 164 | + let mut count = 0; |
| 165 | + log::info!("Updating types for the SchedulePoints storage"); |
| 166 | + v1::SchedulePoints::drain().for_each(|(ticker, schedule_id, checkpoint_id)| { |
| 167 | + count += 1; |
| 168 | + let asset_id = ticker_to_asset_id |
| 169 | + .entry(ticker) |
| 170 | + .or_insert(AssetID::from(ticker)); |
| 171 | + SchedulePoints::insert(asset_id, schedule_id, checkpoint_id); |
| 172 | + }); |
| 173 | + log::info!("{:?} items migrated", count); |
| 174 | +} |
0 commit comments