Skip to content

Commit f7a9a74

Browse files
committed
fix: add migration v12 fix
1 parent c463c64 commit f7a9a74

File tree

13 files changed

+177
-53
lines changed

13 files changed

+177
-53
lines changed

Cargo.lock

+6-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/collator/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "astar-collator"
3-
version = "5.33.0"
3+
version = "5.33.1"
44
description = "Astar collator implementation in Rust."
55
build = "build.rs"
66
default-run = "astar-collator"

primitives/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ xcm-executor = { workspace = true }
3636
# ORML dependencies
3737
orml-traits = { workspace = true }
3838

39+
pallet-contracts = { workspace = true }
40+
3941
# Frontier dependencies
4042
pallet-evm = { workspace = true }
4143

@@ -67,8 +69,10 @@ std = [
6769
"fp-evm/std",
6870
"pallet-assets/std",
6971
"pallet-evm/std",
72+
"pallet-contracts/std",
7073
"pallet-evm-precompile-assets-erc20/std",
7174
"pallet-evm-precompile-dispatch/std",
7275
"sp-arithmetic/std",
7376
]
7477
runtime-benchmarks = ["xcm-builder/runtime-benchmarks", "pallet-assets/runtime-benchmarks"]
78+
try-runtime = ["pallet-contracts/try-runtime"]

primitives/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ pub mod testing;
4444
/// Oracle & price primitives.
4545
pub mod oracle;
4646

47+
/// Common Migrations
48+
pub mod migrations;
49+
4750
/// Benchmark primitives
4851
#[cfg(feature = "runtime-benchmarks")]
4952
pub mod benchmarks;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
use frame_support::{
2+
pallet_prelude::*, storage_alias, traits::fungible::Inspect, DefaultNoBound, Identity,
3+
};
4+
use pallet_contracts::{
5+
migration::{IsFinished, MigrationStep},
6+
weights::WeightInfo,
7+
Config, Determinism, Pallet,
8+
};
9+
use parity_scale_codec::{Decode, Encode};
10+
use scale_info::prelude::format;
11+
#[cfg(feature = "try-runtime")]
12+
use sp_runtime::TryRuntimeError;
13+
use sp_std::marker::PhantomData;
14+
15+
const LOG_TARGET: &str = "runtime::contracts";
16+
17+
type BalanceOf<T> =
18+
<<T as Config>::Currency as Inspect<<T as frame_system::Config>::AccountId>>::Balance;
19+
type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
20+
type CodeHash<T> = <T as frame_system::Config>::Hash;
21+
type CodeVec<T> = BoundedVec<u8, <T as Config>::MaxCodeLen>;
22+
23+
mod old {
24+
use super::*;
25+
26+
#[storage_alias]
27+
pub type CodeInfoOf<T: Config> = StorageMap<Pallet<T>, Twox64Concat, CodeHash<T>, CodeInfo<T>>;
28+
}
29+
30+
#[derive(Encode, Decode, scale_info::TypeInfo, MaxEncodedLen)]
31+
#[codec(mel_bound())]
32+
#[scale_info(skip_type_params(T))]
33+
pub struct CodeInfo<T: Config> {
34+
owner: AccountIdOf<T>,
35+
#[codec(compact)]
36+
deposit: BalanceOf<T>,
37+
#[codec(compact)]
38+
refcount: u64,
39+
determinism: Determinism,
40+
code_len: u32,
41+
}
42+
43+
#[storage_alias]
44+
pub type CodeInfoOf<T: Config> = StorageMap<Pallet<T>, Identity, CodeHash<T>, CodeInfo<T>>;
45+
46+
#[storage_alias]
47+
pub type PristineCode<T: Config> = StorageMap<Pallet<T>, Identity, CodeHash<T>, CodeVec<T>>;
48+
49+
#[derive(Encode, Decode, MaxEncodedLen, DefaultNoBound)]
50+
pub struct Migration<T: Config> {
51+
last_code_hash: Option<CodeHash<T>>,
52+
_phantom: PhantomData<T>,
53+
}
54+
55+
/// Logic as follows,
56+
/// Since we need to modifiy `CodeInfoOf` mapping we cannot use `iter()` or `drain()` on it as
57+
/// that will be undefined behaviour, so we are iterating over keys of `PristineCode` mappings
58+
/// which are code hashes.
59+
///
60+
/// Migration Weights: Reusing v12 migration weights as most heavy operation which is moving
61+
/// code info is same.
62+
impl<T: Config> MigrationStep for Migration<T> {
63+
const VERSION: u16 = 15;
64+
65+
fn max_step_weight() -> Weight {
66+
T::WeightInfo::v12_migration_step(T::MaxCodeLen::get())
67+
}
68+
69+
fn step(&mut self) -> (IsFinished, Weight) {
70+
let mut iter = if let Some(last_key) = self.last_code_hash.take() {
71+
PristineCode::<T>::iter_keys_from(PristineCode::<T>::hashed_key_for(last_key))
72+
} else {
73+
PristineCode::<T>::iter_keys()
74+
};
75+
76+
if let Some(code_hash) = iter.next() {
77+
log::debug!(
78+
target: LOG_TARGET,
79+
"Migrating CodeInfoOf for code_hash {:?}",
80+
code_hash
81+
);
82+
83+
let code_info = old::CodeInfoOf::<T>::take(code_hash)
84+
.expect(format!("No CodeInfo found for code_hash: {:?}", code_hash).as_str());
85+
let code_len = code_info.code_len;
86+
87+
CodeInfoOf::<T>::insert(code_hash, code_info);
88+
89+
self.last_code_hash = Some(code_hash);
90+
(IsFinished::No, T::WeightInfo::v12_migration_step(code_len))
91+
} else {
92+
log::debug!(target: LOG_TARGET, "No more CodeInfo to migrate");
93+
(IsFinished::Yes, T::WeightInfo::v12_migration_step(0))
94+
}
95+
}
96+
97+
#[cfg(feature = "try-runtime")]
98+
fn pre_upgrade_step() -> Result<sp_std::vec::Vec<u8>, TryRuntimeError> {
99+
let len = 100;
100+
let sample: Vec<_> = old::CodeInfoOf::<T>::iter_keys().take(len).collect();
101+
log::debug!(
102+
target: LOG_TARGET,
103+
"Taking sample of {} CodeInfoOf(s)",
104+
sample.len()
105+
);
106+
107+
Ok(sample.encode())
108+
}
109+
110+
#[cfg(feature = "try-runtime")]
111+
fn post_upgrade_step(state: Vec<u8>) -> Result<(), TryRuntimeError> {
112+
let state = <Vec<CodeHash<T>> as Decode>::decode(&mut &state[..]).unwrap();
113+
114+
log::debug!(
115+
target: LOG_TARGET,
116+
"Validating state of {} Codeinfo(s)",
117+
state.len()
118+
);
119+
for hash in state {
120+
ensure!(
121+
old::CodeInfoOf::<T>::get(&hash).is_none(),
122+
"Old CodeInfoFor is not none!"
123+
);
124+
let _ = CodeInfoOf::<T>::get(&hash)
125+
.expect(format!("CodeInfo for code_hash {:?} not found!", hash).as_str());
126+
}
127+
Ok(())
128+
}
129+
}

primitives/src/migrations/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use frame_support::traits::{OnRuntimeUpgrade, StorageVersion};
2+
use frame_support::weights::Weight;
3+
use sp_core::Get;
4+
use sp_std::marker::PhantomData;
5+
6+
pub mod contract_v12_fix;
7+
8+
pub struct ForceContractsVersion<T: pallet_contracts::Config, const V: u16> {
9+
_phantom: PhantomData<T>,
10+
}
11+
12+
impl<T: pallet_contracts::Config, const V: u16> OnRuntimeUpgrade for ForceContractsVersion<T, V> {
13+
fn on_runtime_upgrade() -> Weight {
14+
StorageVersion::new(V).put::<pallet_contracts::Pallet<T>>();
15+
<T as frame_system::Config>::DbWeight::get().reads_writes(1, 1)
16+
}
17+
}

runtime/astar/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "astar-runtime"
3-
version = "5.33.0"
3+
version = "5.33.1"
44
build = "build.rs"
55
authors.workspace = true
66
edition.workspace = true

runtime/astar/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
148148
spec_name: create_runtime_str!("astar"),
149149
impl_name: create_runtime_str!("astar"),
150150
authoring_version: 1,
151-
spec_version: 81,
151+
spec_version: 82,
152152
impl_version: 0,
153153
apis: RUNTIME_API_VERSIONS,
154154
transaction_version: 2,

runtime/local/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "local-runtime"
3-
version = "5.33.0"
3+
version = "5.33.1"
44
build = "build.rs"
55
authors.workspace = true
66
edition.workspace = true

runtime/shibuya/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "shibuya-runtime"
3-
version = "5.33.0"
3+
version = "5.33.1"
44
build = "build.rs"
55
authors.workspace = true
66
edition.workspace = true

runtime/shibuya/src/lib.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
173173
spec_name: create_runtime_str!("shibuya"),
174174
impl_name: create_runtime_str!("shibuya"),
175175
authoring_version: 1,
176-
spec_version: 123,
176+
spec_version: 124,
177177
impl_version: 0,
178178
apis: RUNTIME_API_VERSIONS,
179179
transaction_version: 2,
@@ -712,14 +712,11 @@ impl pallet_contracts::Config for Runtime {
712712
type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent;
713713
type Debug = ();
714714
type Environment = ();
715-
type Migrations = (
716-
pallet_contracts::migration::v12::Migration<Runtime, Balances>,
717-
pallet_contracts::migration::v13::Migration<Runtime>,
718-
pallet_contracts::migration::v14::Migration<Runtime, Balances>,
719-
pallet_contracts::migration::v15::Migration<Runtime>,
720-
);
715+
type Migrations = (astar_primitives::migrations::contract_v12_fix::Migration<Runtime>,);
721716
}
722717

718+
use custom_migrations::ForceContractsVersion;
719+
723720
// These values are based on the Astar 2.0 Tokenomics Modeling report.
724721
parameter_types! {
725722
pub const TransactionLengthFeeFactor: Balance = 23_500_000_000_000; // 0.000_023_500_000_000_000 SBY per byte
@@ -1370,7 +1367,10 @@ pub type Executive = frame_executive::Executive<
13701367
/// All migrations that will run on the next runtime upgrade.
13711368
///
13721369
/// Once done, migrations should be removed from the tuple.
1373-
pub type Migrations = (pallet_contracts::Migration<Runtime>,);
1370+
pub type Migrations = (
1371+
custom_migrations::ForceContractsVersion<Runtime, 14>,
1372+
pallet_contracts::Migration<Runtime>,
1373+
);
13741374

13751375
type EventRecord = frame_system::EventRecord<
13761376
<Runtime as frame_system::Config>::RuntimeEvent,

runtime/shiden/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "shiden-runtime"
3-
version = "5.33.0"
3+
version = "5.33.1"
44
build = "build.rs"
55
authors.workspace = true
66
edition.workspace = true
@@ -237,6 +237,7 @@ runtime-benchmarks = [
237237
"pallet-dynamic-evm-base-fee/runtime-benchmarks",
238238
]
239239
try-runtime = [
240+
"astar-primitives/try-runtime",
240241
"fp-self-contained/try-runtime",
241242
"log",
242243
"frame-try-runtime/try-runtime",

runtime/shiden/src/lib.rs

+3-34
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
149149
spec_name: create_runtime_str!("shiden"),
150150
impl_name: create_runtime_str!("shiden"),
151151
authoring_version: 1,
152-
spec_version: 120,
152+
spec_version: 121,
153153
impl_version: 0,
154154
apis: RUNTIME_API_VERSIONS,
155155
transaction_version: 2,
@@ -666,12 +666,7 @@ impl pallet_contracts::Config for Runtime {
666666
type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent;
667667
type Debug = ();
668668
type Environment = ();
669-
type Migrations = (
670-
pallet_contracts::migration::v12::Migration<Runtime, Balances>,
671-
pallet_contracts::migration::v13::Migration<Runtime>,
672-
pallet_contracts::migration::v14::Migration<Runtime, Balances>,
673-
pallet_contracts::migration::v15::Migration<Runtime>,
674-
);
669+
type Migrations = (astar_primitives::migrations::contract_v12_fix::Migration<Runtime>,);
675670
}
676671

677672
parameter_types! {
@@ -1105,40 +1100,14 @@ pub type Executive = frame_executive::Executive<
11051100
Migrations,
11061101
>;
11071102

1108-
parameter_types! {
1109-
pub const DappStakingMigrationName: &'static str = "DappStakingMigration";
1110-
}
11111103
/// All migrations that will run on the next runtime upgrade.
11121104
///
11131105
/// Once done, migrations should be removed from the tuple.
11141106
pub type Migrations = (
1115-
// Part of shiden-119
1116-
frame_support::migrations::RemovePallet<
1117-
DappStakingMigrationName,
1118-
<Runtime as frame_system::Config>::DbWeight,
1119-
>,
1120-
// Part of shiden-119
1121-
RecalculationEraFix,
1107+
astar_primitives::migrations::ForceContractsVersion<Runtime, 14>,
11221108
pallet_contracts::Migration<Runtime>,
11231109
);
11241110

1125-
use frame_support::traits::OnRuntimeUpgrade;
1126-
pub struct RecalculationEraFix;
1127-
impl OnRuntimeUpgrade for RecalculationEraFix {
1128-
fn on_runtime_upgrade() -> Weight {
1129-
let first_dapp_staking_v3_era = 743;
1130-
1131-
let expected_recalculation_era =
1132-
InflationCycleConfig::eras_per_cycle().saturating_add(first_dapp_staking_v3_era);
1133-
1134-
pallet_inflation::ActiveInflationConfig::<Runtime>::mutate(|config| {
1135-
config.recalculation_era = expected_recalculation_era;
1136-
});
1137-
1138-
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(1, 1)
1139-
}
1140-
}
1141-
11421111
type EventRecord = frame_system::EventRecord<
11431112
<Runtime as frame_system::Config>::RuntimeEvent,
11441113
<Runtime as frame_system::Config>::Hash,

0 commit comments

Comments
 (0)