Skip to content

Commit 3a59277

Browse files
authored
Schema Migration fix for newer mainnet schema data (#2165)
# Goal The goal of this PR is to update the schemas migration with respect to the newer data on chain. Part of #2006 # Discussion - Only reset the name list as needed - Add in schema 19 so that it is re-added at the top of the list # Result of `try-runtime` [2024-09-26T20:42:48Z INFO runtime::schemas] Running pre_upgrade... [2024-09-26T20:42:48Z INFO runtime::schemas] Running storage migration... [2024-09-26T20:42:48Z INFO runtime::schemas] Storage migrated to version 4 read=15, write=15, bytes=11138 [2024-09-26T20:42:48Z INFO runtime::schemas] Migration Calculated weights=Weight { ref_time: 1824840000, proof_size: 11138 } [2024-09-26T20:42:48Z INFO runtime::schemas] Running post_upgrade... [2024-09-26T20:42:48Z INFO runtime::schemas] Finished post_upgrade
1 parent 3772960 commit 3a59277

File tree

1 file changed

+20
-18
lines changed
  • pallets/schemas/src/migration

1 file changed

+20
-18
lines changed

pallets/schemas/src/migration/v4.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
#[cfg(feature = "try-runtime")]
2+
use crate::types::SCHEMA_STORAGE_VERSION;
13
use crate::{
24
pallet::{SchemaInfos, SchemaNameToIds},
35
Config, Pallet, SchemaId, SchemaName, LOG_TARGET,
46
};
5-
#[cfg(feature = "try-runtime")]
6-
use crate::{types::SCHEMA_STORAGE_VERSION, SchemaVersionId};
77
use common_primitives::utils::{get_chain_type_by_genesis_hash, DetectedChainType};
88
use frame_support::{pallet_prelude::*, traits::OnRuntimeUpgrade, weights::Weight};
99
use frame_system::pallet_prelude::BlockNumberFor;
@@ -14,21 +14,23 @@ use sp_runtime::TryRuntimeError;
1414
use sp_std::{collections::btree_map::BTreeMap, vec::Vec};
1515

1616
/// get known schema names for mainnet
17-
pub fn get_known_schemas<T: Config>() -> BTreeMap<SchemaId, Vec<u8>> {
17+
pub fn get_known_schemas<T: Config>() -> BTreeMap<SchemaId, (Vec<u8>, bool)> {
1818
let genesis_block: BlockNumberFor<T> = 0u32.into();
1919
let genesis = <frame_system::Pallet<T>>::block_hash(genesis_block);
2020

2121
if let DetectedChainType::FrequencyMainNet =
2222
get_chain_type_by_genesis_hash(&genesis.encode()[..])
2323
{
2424
// only return what needs to change which for this case is only on mainnet
25+
// (schema_id, name, clear prior versions)
2526
BTreeMap::from([
26-
(5, b"dsnp.update".to_vec()),
27-
(6, b"dsnp.profile".to_vec()),
28-
(7, b"dsnp.public-key-key-agreement".to_vec()),
29-
(8, b"dsnp.public-follows".to_vec()),
30-
(9, b"dsnp.private-follows".to_vec()),
31-
(10, b"dsnp.private-connections".to_vec()),
27+
(5, (b"dsnp.update".to_vec(), true)),
28+
(19, (b"dsnp.update".to_vec(), false)),
29+
(6, (b"dsnp.profile".to_vec(), true)),
30+
(7, (b"dsnp.public-key-key-agreement".to_vec(), false)),
31+
(8, (b"dsnp.public-follows".to_vec(), false)),
32+
(9, (b"dsnp.private-follows".to_vec(), false)),
33+
(10, (b"dsnp.private-connections".to_vec(), false)),
3234
])
3335
} else {
3436
BTreeMap::new()
@@ -69,19 +71,17 @@ impl<T: Config> OnRuntimeUpgrade for MigrateToV4<T> {
6971
assert_eq!(onchain_version, SCHEMA_STORAGE_VERSION);
7072
// check to ensure updates took place
7173
let known_schemas = get_known_schemas::<T>();
72-
for (schema_id, schema_name) in known_schemas.into_iter() {
74+
for (schema_id, (schema_name, _should_clear)) in known_schemas.into_iter() {
7375
// safe to use unwrap since only used in try_runtime
7476
let bounded = BoundedVec::try_from(schema_name).unwrap();
7577
let parsed_name = SchemaName::try_parse::<T>(bounded, true).unwrap();
7678
let current_versions =
7779
SchemaNameToIds::<T>::get(parsed_name.namespace, parsed_name.descriptor);
7880

79-
let mut expected = SchemaVersionId::default();
80-
let _ = expected.add::<T>(schema_id);
81-
82-
assert_eq!(
83-
current_versions, expected,
84-
"Expected versions did not match for {}",
81+
// Just check that it contains the correct value, not position anymore
82+
assert!(
83+
current_versions.ids.iter().any(|&id| id == schema_id),
84+
"Current versions does not contain schema_id {}",
8585
schema_id
8686
);
8787
}
@@ -104,7 +104,7 @@ pub fn migrate_to_v4<T: Config>() -> Weight {
104104
let mut writes = 0u64;
105105
let mut bytes = 0u64;
106106

107-
for (schema_id, schema_name) in known_schemas.iter() {
107+
for (schema_id, (schema_name, should_clear)) in known_schemas.iter() {
108108
reads.saturating_inc();
109109

110110
if let Some(mut schema) = SchemaInfos::<T>::get(&schema_id) {
@@ -122,7 +122,9 @@ pub fn migrate_to_v4<T: Config>() -> Weight {
122122
.saturating_add(schema_version_id.encode().len() as u64);
123123

124124
// clear the old/wrong one in case they exist
125-
schema_version_id.ids.clear();
125+
if *should_clear {
126+
schema_version_id.ids.clear();
127+
}
126128
let _ = schema_version_id.add::<T>(*schema_id);
127129

128130
// set schema as having a name

0 commit comments

Comments
 (0)