-
Notifications
You must be signed in to change notification settings - Fork 434
fix(contracts): migrate CodeInfoOf<T>
from Twox64Concat
hashing to Identity
as defined in pallet-contracts
#1200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f7a9a74
fix: add migration v12 fix
ashutoshvarma 308579a
Merge branch 'master' into fix/contacts-migration
ashutoshvarma c21c7d4
fix: build
ashutoshvarma 9eaf9fa
feat: take new contracts into account
ashutoshvarma 664cb89
feat: add fixed v12, v14 migrations
ashutoshvarma 1a253c3
feat: add noop migration to ensure pallet versions
ashutoshvarma 3e069a3
feat: bump semver to v5.34.0
ashutoshvarma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
// This file is part of Astar. | ||
|
||
// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd. | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
// Astar is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Astar is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Astar. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use frame_support::{ | ||
pallet_prelude::*, storage_alias, traits::fungible::Inspect, DefaultNoBound, Identity, | ||
}; | ||
use pallet_contracts::{ | ||
migration::{IsFinished, MigrationStep}, | ||
weights::WeightInfo, | ||
Config, Determinism, Pallet, | ||
}; | ||
use parity_scale_codec::{Decode, Encode}; | ||
#[cfg(feature = "try-runtime")] | ||
use sp_runtime::TryRuntimeError; | ||
use sp_std::marker::PhantomData; | ||
|
||
const LOG_TARGET: &str = "runtime::contracts"; | ||
|
||
type BalanceOf<T> = | ||
<<T as Config>::Currency as Inspect<<T as frame_system::Config>::AccountId>>::Balance; | ||
type AccountIdOf<T> = <T as frame_system::Config>::AccountId; | ||
type CodeHash<T> = <T as frame_system::Config>::Hash; | ||
type CodeVec<T> = BoundedVec<u8, <T as Config>::MaxCodeLen>; | ||
|
||
mod old { | ||
use super::*; | ||
|
||
#[storage_alias] | ||
pub type CodeInfoOf<T: Config> = StorageMap<Pallet<T>, Twox64Concat, CodeHash<T>, CodeInfo<T>>; | ||
} | ||
|
||
#[derive(Encode, Decode, scale_info::TypeInfo, MaxEncodedLen)] | ||
#[codec(mel_bound())] | ||
#[scale_info(skip_type_params(T))] | ||
pub struct CodeInfo<T: Config> { | ||
owner: AccountIdOf<T>, | ||
#[codec(compact)] | ||
deposit: BalanceOf<T>, | ||
#[codec(compact)] | ||
refcount: u64, | ||
determinism: Determinism, | ||
code_len: u32, | ||
} | ||
|
||
#[storage_alias] | ||
pub type CodeInfoOf<T: Config> = StorageMap<Pallet<T>, Identity, CodeHash<T>, CodeInfo<T>>; | ||
|
||
#[storage_alias] | ||
pub type PristineCode<T: Config> = StorageMap<Pallet<T>, Identity, CodeHash<T>, CodeVec<T>>; | ||
|
||
#[derive(Encode, Decode, MaxEncodedLen, DefaultNoBound)] | ||
pub struct Migration<T: Config> { | ||
last_code_hash: Option<CodeHash<T>>, | ||
_phantom: PhantomData<T>, | ||
} | ||
|
||
/// Logic as follows, | ||
/// Since we need to modifiy `CodeInfoOf` mapping we cannot use `iter()` or `drain()` on it as | ||
/// that will be undefined behaviour, so we are iterating over keys of `PristineCode` mappings | ||
/// which are code hashes. | ||
/// | ||
/// Migration Weights: Reusing v12 migration weights as most heavy operation which is moving | ||
/// code info is same. | ||
impl<T: Config> MigrationStep for Migration<T> { | ||
const VERSION: u16 = 15; | ||
|
||
fn max_step_weight() -> Weight { | ||
T::WeightInfo::v12_migration_step(T::MaxCodeLen::get()) | ||
} | ||
|
||
fn step(&mut self) -> (IsFinished, Weight) { | ||
let mut iter = if let Some(last_key) = self.last_code_hash.take() { | ||
PristineCode::<T>::iter_keys_from(PristineCode::<T>::hashed_key_for(last_key)) | ||
} else { | ||
PristineCode::<T>::iter_keys() | ||
}; | ||
|
||
if let Some(code_hash) = iter.next() { | ||
if let Some(code_info) = old::CodeInfoOf::<T>::take(code_hash) { | ||
log::debug!( | ||
target: LOG_TARGET, | ||
"Migrating CodeInfoOf for code_hash {:?}", | ||
code_hash | ||
); | ||
|
||
let code_len = code_info.code_len; | ||
|
||
CodeInfoOf::<T>::insert(code_hash, code_info); | ||
|
||
self.last_code_hash = Some(code_hash); | ||
(IsFinished::No, T::WeightInfo::v12_migration_step(code_len)) | ||
} else { | ||
log::warn!( | ||
target: LOG_TARGET, | ||
"No CodeInfo found for code_hash {:?}, maybe new contract?", | ||
code_hash | ||
); | ||
// old CodeInfo not found, it's newly deployed contract | ||
self.last_code_hash = Some(code_hash); | ||
(IsFinished::No, T::WeightInfo::v12_migration_step(0)) | ||
} | ||
} else { | ||
log::debug!(target: LOG_TARGET, "No more CodeInfo to migrate"); | ||
(IsFinished::Yes, T::WeightInfo::v12_migration_step(0)) | ||
} | ||
} | ||
|
||
#[cfg(feature = "try-runtime")] | ||
fn pre_upgrade_step() -> Result<sp_std::vec::Vec<u8>, TryRuntimeError> { | ||
let len = 100; | ||
let sample: sp_std::vec::Vec<_> = old::CodeInfoOf::<T>::iter_keys().take(len).collect(); | ||
log::debug!( | ||
target: LOG_TARGET, | ||
"Taking sample of {} CodeInfoOf(s)", | ||
sample.len() | ||
); | ||
|
||
Ok(sample.encode()) | ||
} | ||
|
||
#[cfg(feature = "try-runtime")] | ||
fn post_upgrade_step(state: sp_std::vec::Vec<u8>) -> Result<(), TryRuntimeError> { | ||
let state = <sp_std::vec::Vec<CodeHash<T>> as Decode>::decode(&mut &state[..]).unwrap(); | ||
|
||
log::debug!( | ||
target: LOG_TARGET, | ||
"Validating state of {} Codeinfo(s)", | ||
state.len() | ||
); | ||
for hash in state { | ||
ensure!( | ||
old::CodeInfoOf::<T>::get(&hash).is_none(), | ||
"Old CodeInfoFor is not none!" | ||
); | ||
let _ = CodeInfoOf::<T>::get(&hash).expect( | ||
scale_info::prelude::format!("CodeInfo for code_hash {:?} not found!", hash) | ||
.as_str(), | ||
); | ||
} | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// This file is part of Astar. | ||
|
||
// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd. | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
// Astar is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Astar is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Astar. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use frame_support::traits::{OnRuntimeUpgrade, StorageVersion}; | ||
use frame_support::weights::Weight; | ||
use sp_core::Get; | ||
use sp_std::marker::PhantomData; | ||
|
||
pub mod contract_v12_fix; | ||
|
||
pub struct ForceContractsVersion<T: pallet_contracts::Config, const V: u16> { | ||
_phantom: PhantomData<T>, | ||
} | ||
|
||
impl<T: pallet_contracts::Config, const V: u16> OnRuntimeUpgrade for ForceContractsVersion<T, V> { | ||
fn on_runtime_upgrade() -> Weight { | ||
StorageVersion::new(V).put::<pallet_contracts::Pallet<T>>(); | ||
<T as frame_system::Config>::DbWeight::get().reads_writes(1, 1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big deal, but isn't this a single write only?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are right, I'l change this