Skip to content

Commit b4f0e84

Browse files
Mesh 2188/migrations (#1699)
* Add asset migrations * Add checkpoint migrations * Add compliance manager migrations * Add corporate-actions migrations * Addd external-agents migrations * Add nft migrations * Add portfolio migrations * Add pallet-settlement migrations * Add statistics migrations * Add sto migrations * Add identity migrations * Add AssetIdentifiers missing migration * Add missing on_runtime_upgrade * Count migrated items * Add missing cargo.lock
1 parent 140b6e3 commit b4f0e84

File tree

33 files changed

+2153
-60
lines changed

33 files changed

+2153
-60
lines changed

Cargo.lock

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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+
}

pallets/asset/src/checkpoint/mod.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@
4141
4242
#[cfg(feature = "runtime-benchmarks")]
4343
pub mod benchmarking;
44+
mod migrations;
4445

4546
use codec::{Decode, Encode};
4647
use frame_support::{
4748
decl_error, decl_module, decl_storage,
4849
dispatch::{DispatchError, DispatchResult},
4950
ensure,
5051
traits::UnixTime,
52+
weights::Weight,
5153
};
5254
use frame_system::ensure_root;
5355
use sp_runtime::traits::SaturatedConversion;
@@ -64,14 +66,16 @@ use polymesh_common_utilities::{
6466
GC_DID,
6567
};
6668
use polymesh_primitives::asset::AssetID;
67-
use polymesh_primitives::{asset::CheckpointId, storage_migration_ver, IdentityId, Moment};
69+
use polymesh_primitives::{
70+
asset::CheckpointId, storage_migrate_on, storage_migration_ver, IdentityId, Moment,
71+
};
6872

6973
use crate::Config;
7074

7175
type Asset<T> = crate::Module<T>;
7276
type ExternalAgents<T> = pallet_external_agents::Module<T>;
7377

74-
storage_migration_ver!(1);
78+
storage_migration_ver!(2);
7579

7680
decl_storage! {
7781
trait Store for Module<T: Config> as Checkpoint {
@@ -155,7 +159,7 @@ decl_storage! {
155159
double_map hasher(blake2_128_concat) AssetID, hasher(twox_64_concat) ScheduleId => Vec<CheckpointId>;
156160

157161
/// Storage version.
158-
StorageVersion get(fn storage_version) build(|_| Version::new(1)): Version;
162+
StorageVersion get(fn storage_version) build(|_| Version::new(2)): Version;
159163
}
160164
}
161165

@@ -165,6 +169,13 @@ decl_module! {
165169

166170
fn deposit_event() = default;
167171

172+
fn on_runtime_upgrade() -> Weight {
173+
storage_migrate_on!(StorageVersion, 2, {
174+
migrations::migrate_to_v2::<T>();
175+
});
176+
Weight::zero()
177+
}
178+
168179
/// Creates a single checkpoint at the current time.
169180
///
170181
/// # Arguments

pallets/asset/src/lib.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,15 @@ pub mod benchmarking;
7979
pub mod checkpoint;
8080

8181
mod error;
82+
mod migrations;
8283
mod types;
8384

8485
use codec::{Decode, Encode};
8586
use core::mem;
8687
use currency::*;
8788
use frame_support::dispatch::{DispatchError, DispatchResult};
8889
use frame_support::traits::Get;
90+
use frame_support::weights::Weight;
8991
use frame_support::BoundedBTreeSet;
9092
use frame_support::{decl_module, decl_storage, ensure};
9193
use frame_system::ensure_root;
@@ -115,9 +117,9 @@ use polymesh_primitives::asset_metadata::{
115117
};
116118
use polymesh_primitives::settlement::InstructionId;
117119
use polymesh_primitives::{
118-
extract_auth, storage_migration_ver, AssetIdentifier, Balance, Document, DocumentId,
119-
IdentityId, Memo, PortfolioId, PortfolioKind, PortfolioUpdateReason, SecondaryKey, Ticker,
120-
WeightMeter,
120+
extract_auth, storage_migrate_on, storage_migration_ver, AssetIdentifier, Balance, Document,
121+
DocumentId, IdentityId, Memo, PortfolioId, PortfolioKind, PortfolioUpdateReason, SecondaryKey,
122+
Ticker, WeightMeter,
121123
};
122124

123125
pub use error::Error;
@@ -132,7 +134,7 @@ type Identity<T> = pallet_identity::Module<T>;
132134
type Portfolio<T> = pallet_portfolio::Module<T>;
133135
type Statistics<T> = pallet_statistics::Module<T>;
134136

135-
storage_migration_ver!(4);
137+
storage_migration_ver!(5);
136138

137139
decl_storage! {
138140
trait Store for Module<T: Config> as Asset {
@@ -249,7 +251,7 @@ decl_storage! {
249251
pub AssetNonce: map hasher(identity) T::AccountId => u64;
250252

251253
/// Storage version.
252-
StorageVersion get(fn storage_version) build(|_| Version::new(4)): Version;
254+
StorageVersion get(fn storage_version) build(|_| Version::new(5)): Version;
253255
}
254256

255257
add_extra_genesis {
@@ -284,16 +286,23 @@ decl_module! {
284286

285287
type Error = Error<T>;
286288

287-
/// initialize the default event for this module
288-
fn deposit_event() = default;
289-
290289
const AssetNameMaxLength: u32 = T::AssetNameMaxLength::get();
291290
const FundingRoundNameMaxLength: u32 = T::FundingRoundNameMaxLength::get();
292291
const AssetMetadataNameMaxLength: u32 = T::AssetMetadataNameMaxLength::get();
293292
const AssetMetadataValueMaxLength: u32 = T::AssetMetadataValueMaxLength::get();
294293
const AssetMetadataTypeDefMaxLength: u32 = T::AssetMetadataTypeDefMaxLength::get();
295294
const MaxAssetMediators: u32 = T::MaxAssetMediators::get();
296295

296+
/// initialize the default event for this module
297+
fn deposit_event() = default;
298+
299+
fn on_runtime_upgrade() -> Weight {
300+
storage_migrate_on!(StorageVersion, 5, {
301+
migrations::migrate_to_v5::<T>();
302+
});
303+
Weight::zero()
304+
}
305+
297306
/// Registers a unique ticker or extends validity of an existing ticker.
298307
/// NB: Ticker validity does not get carry forward when renewing ticker.
299308
///

0 commit comments

Comments
 (0)