Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
eef15f1
upgrade to wasmi 0.29
agryaznov May 3, 2023
dee28ff
prepare cleanup
agryaznov May 3, 2023
efeb0bd
sync ref_time w engine from the stack frame
agryaznov May 3, 2023
4fdd5c8
proc_macro: sync gas in host funcs
agryaznov May 4, 2023
cac61c6
clean benchmarks & schedule: w_base = w_i64const
agryaznov May 5, 2023
be0df3c
scale gas values btw engine and gas meter
agryaznov May 5, 2023
8a32757
(re)instrumentation & code_cache removed
agryaznov May 5, 2023
f8abec4
remove gas() host fn, continue clean-up
agryaznov May 8, 2023
22440ae
address review comments
agryaznov Jun 9, 2023
c94821d
Merge branch 'master' into ag-wasmeter
agryaznov Jun 9, 2023
2010ff7
move from CodeStorage&PrefabWasmModule to PristineCode&WasmBlob
agryaznov Jun 9, 2023
100cdcd
refactor: no reftime_limit&schedule passes, no CodeStorage
agryaznov Jun 9, 2023
85a0340
bugs fixing
agryaznov Jun 10, 2023
505f750
fix tests: expected deposit amount
agryaznov Jun 10, 2023
c25f634
fix prepare::tests
agryaznov Jun 10, 2023
7f46e7e
update tests and fix bugs
agryaznov Jun 10, 2023
7bf4fbe
update docs
agryaznov Jun 11, 2023
cc05e88
bump wasmi 0.30.0
agryaznov Jun 12, 2023
0e2b0ab
benchmarks updated, tests pass
agryaznov Jun 12, 2023
e6e3563
refactoring
agryaznov Jun 12, 2023
f4ffcf2
s/OwnerInfo/CodeInfo/g;
agryaznov Jun 12, 2023
a50b7cf
migration: draft, compiles
agryaznov Jun 12, 2023
c6b9f74
migration: draft, runs
agryaznov Jun 13, 2023
132e9ce
migration: draft, runs (fixing)
agryaznov Jun 13, 2023
9935e92
deposits repaid non pro rata
agryaznov Jun 13, 2023
0b9125a
deposits repaid pro rata
agryaznov Jun 13, 2023
08a15b7
better try-runtime output
agryaznov Jun 13, 2023
5f6c1ac
even better try-runtime output
agryaznov Jun 13, 2023
b1290f0
benchmark migration
agryaznov Jun 14, 2023
9ffca5d
Merge branch 'master' into ag-wasmeter
agryaznov Jun 14, 2023
94f8476
fix merge leftover
agryaznov Jun 14, 2023
ba67751
add forgotten fixtures, fix docs
agryaznov Jun 14, 2023
0ee3293
address review comments
agryaznov Jun 14, 2023
a45c538
ci fixes
agryaznov Jun 14, 2023
f24a267
cleanup
agryaznov Jun 15, 2023
e6c21f3
benchmarks::prepare to return DispatchError
agryaznov Jun 15, 2023
444a97b
Merge branch 'master' into ag-wasmeter
agryaznov Jun 15, 2023
d3f59b2
".git/.scripts/commands/bench/bench.sh" pallet dev pallet_contracts
Jun 15, 2023
f12927d
store memory limits to CodeInfo
agryaznov Jun 16, 2023
0a9b351
ci: roll back weights
agryaznov Jun 16, 2023
cff9da7
Merge branch 'master' of https://github.com/paritytech/substrate into…
Jun 16, 2023
f0e24c5
".git/.scripts/commands/bench-vm/bench-vm.sh" pallet dev pallet_contr…
Jun 16, 2023
2e07393
drive-by: update Readme and pallet rustdoc
agryaznov Jun 16, 2023
f465ecf
Merge branch 'master' into ag-wasmeter
agryaznov Jun 17, 2023
94be691
".git/.scripts/commands/bench/bench.sh" pallet dev pallet_contracts
Jun 17, 2023
721d4b6
".git/.scripts/commands/bench/bench.sh" pallet dev pallet_contracts
Jun 17, 2023
e226c0e
use wasmi 0.29
agryaznov Jun 18, 2023
e57596f
Merge branch 'master' of https://github.com/paritytech/substrate into…
Jun 18, 2023
d687f1c
".git/.scripts/commands/bench/bench.sh" pallet dev pallet_contracts
Jun 18, 2023
76a8c20
use wasmi 0.30 again
agryaznov Jun 20, 2023
fbc43ae
query memory limits from wasmi
agryaznov Jun 20, 2023
cd54ff2
save: scan_exports ported, compiles
agryaznov Jun 20, 2023
b548064
save (wip, not compiles)
agryaznov Jun 20, 2023
0b4bb91
query memory limits from wasmi
agryaznov Jun 20, 2023
b15d5a9
better migration types
agryaznov Jun 20, 2023
8eb8710
ci: pull weights from master
agryaznov Jun 20, 2023
dae3f32
Merge branch 'master' into ag-wasmeter
agryaznov Jun 21, 2023
f42db5d
refactoring
agryaznov Jun 21, 2023
b789e6b
".git/.scripts/commands/bench-vm/bench-vm.sh" pallet dev pallet_contr…
Jun 21, 2023
3cadbb1
Merge branch 'ag-wasmeter' into ag-wasmi-validation
agryaznov Jun 21, 2023
eba148b
scan_imports ported
agryaznov Jun 21, 2023
ca5938d
scan_export ported, other checks removed
agryaznov Jun 22, 2023
c2c3d9b
tests fixed
agryaznov Jun 22, 2023
56dc2df
drop wasmparser and parity-wasm dependencies
agryaznov Jun 23, 2023
086e219
typo fix
agryaznov Jun 27, 2023
2544972
addressing review comments
agryaznov Jun 27, 2023
387bec9
refactor
agryaznov Jun 27, 2023
8be0537
address review comments
agryaznov Jun 29, 2023
ffdb8d1
optimize migration
agryaznov Jun 30, 2023
eaf7a31
Merge branch 'master' into ag-wasmeter
agryaznov Jun 30, 2023
f4dad0f
".git/.scripts/commands/bench/bench.sh" pallet dev pallet_contracts
Jun 30, 2023
b5ad1de
another review round comments addressed
agryaznov Jun 30, 2023
0e0155f
ci fix one
agryaznov Jun 30, 2023
d139757
clippy fix
agryaznov Jun 30, 2023
670ff1a
ci fix two
agryaznov Jun 30, 2023
18236e1
Merge branch 'ag-wasmeter' into ag-wasmi-validation
agryaznov Jul 3, 2023
0f83c77
allow stored modules to have no memory imports
agryaznov Jul 3, 2023
ca111cf
Merge branch 'master' into ag-wasmi-validation
agryaznov Jul 3, 2023
bf52770
rollback: allow stored modules to have no memory imports
agryaznov Jul 3, 2023
824ef6d
Merge branch 'master' of https://github.com/paritytech/substrate into…
Jul 3, 2023
b55dbcb
".git/.scripts/commands/bench/bench.sh" pallet dev pallet_contracts
Jul 3, 2023
39fb730
address review comments
agryaznov Jul 4, 2023
ac9d5c2
Merge branch 'ag-wasmi-validation'
agryaznov Jul 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
migration: draft, compiles
  • Loading branch information
agryaznov committed Jun 13, 2023
commit a50b7cfd7e019e3bbc04cb96b9f70d39c33d8bac
1 change: 1 addition & 0 deletions frame/contracts/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

pub mod v10;
pub mod v11;
pub mod v12;
pub mod v9;

use crate::{weights::WeightInfo, Config, Error, MigrationInProgress, Pallet, Weight, LOG_TARGET};
Expand Down
197 changes: 197 additions & 0 deletions frame/contracts/src/migration/v12.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Move the `determinism` field to `CodeInfo` (ex. `OwnerInfo`), remove `CodeStorage` and repay
//! deposits.

use crate::{
migration::{IsFinished, Migrate},
weights::WeightInfo,
AccountIdOf, BalanceOf, CodeHash, Config, Determinism, Pallet, Weight, LOG_TARGET,
};

use codec::{Decode, Encode};
use frame_support::{
codec, pallet_prelude::*, storage_alias, BoundedVec, DefaultNoBound, Identity,
};
#[cfg(feature = "try-runtime")]
use sp_runtime::TryRuntimeError;
use sp_std::{marker::PhantomData, prelude::*};

mod old {
use super::*;

#[derive(Encode, Decode)]
pub struct OwnerInfo<T: Config> {
pub owner: AccountIdOf<T>,
#[codec(compact)]
pub deposit: BalanceOf<T>,
#[codec(compact)]
pub refcount: u64,
}

#[derive(Encode, Decode)]
pub struct PrefabWasmModule {
#[codec(compact)]
pub instruction_weights_version: u32,
#[codec(compact)]
pub initial: u32,
#[codec(compact)]
pub maximum: u32,
pub code: Vec<u8>,
pub determinism: Determinism,
}

#[storage_alias]
pub type OwnerInfoOf<T: Config> =
StorageMap<Pallet<T>, Twox64Concat, CodeHash<T>, OwnerInfo<T>>;

#[storage_alias]
pub type CodeStorage<T: Config> =
StorageMap<Pallet<T>, Identity, CodeHash<T>, PrefabWasmModule>;
}

#[derive(Encode, Decode)]
pub struct CodeInfo<T: Config> {
owner: AccountIdOf<T>,
#[codec(compact)]
deposit: BalanceOf<T>,
#[codec(compact)]
refcount: u64,
determinism: Determinism,
}

#[storage_alias]
pub type CodeInfoOf<T: Config> = StorageMap<Pallet<T>, Twox64Concat, CodeHash<T>, CodeInfo<T>>;

#[cfg(feature = "runtime-benchmarks")]
pub fn store_old_owner_info<T: Config>(len: usize) {
use sp_runtime::traits::Hash;
let info = old::OnwerInfo {
owner: Default::default(),
deposit: BalanceOf::<T>::max(),
refcount: u64::MAX,
};
let hash = T::Hashing::hash(&info.owner.into());
old::OwnerInfoOf::<T>::insert(hash, info);
}

#[derive(Encode, Decode, MaxEncodedLen, DefaultNoBound)]
pub struct Migration<T: Config> {
last_key: Option<BoundedVec<u8, ConstU32<256>>>,
_phantom: PhantomData<T>,
}

impl<T: Config> Migrate for Migration<T> {
const VERSION: u16 = 12;

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_key.take() {
old::OwnerInfoOf::<T>::iter_from(last_key.to_vec())
} else {
old::OwnerInfoOf::<T>::iter()
};

if let Some((key, old)) = iter.next() {
log::debug!(target: LOG_TARGET, "Migrating OwnerInfo for code_hash {:?}", key);
let module = old::CodeStorage::<T>::take(key).unwrap_or_else(|| {
log::error!(
target: LOG_TARGET,
"No PrefabWasmModule found for code_hash: {:?}",
key
);
panic!();
});
let freed_bytes = module.encode().len();
log::debug!(
target: LOG_TARGET,
"Freed 1 storage item with size (in bytes): {:?}",
freed_bytes
);

// TODO: remove this, as calculated determinism added bytes
let freed_bytes = old.encode().len();

let info = CodeInfo {
determinism: module.determinism,
owner: old.owner,
deposit: old.deposit,
refcount: old.refcount,
};

let occupied_bytes = info.encode().len() - freed_bytes;

CodeInfoOf::<T>::insert(key, info);
log::debug!(
target: LOG_TARGET,
"Occupied more (for storing determinism) bytes: {:?}",
occupied_bytes
);

self.last_key = Some(iter.last_raw_key().to_vec().try_into().unwrap());

// TODO: repay deposit
// TODO: write benchmark and pass corrent len instead of freed_bytes
(IsFinished::No, T::WeightInfo::v12_migration_step(freed_bytes as u32))
} else {
log::debug!(target: LOG_TARGET, "No more owner_info to migrate");
(IsFinished::Yes, T::WeightInfo::v12_migration_step(0))
}
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade_step() -> Result<Vec<u8>, TryRuntimeError> {
let sample: Vec<_> = old::OwnerInfoOf::<T>::iter()
.take(100)
.map(|(k, v)| {
let module = old::CodeStorage::<T>::get(k)
.expect("No PrefabWasmModule found for code_hash: {:?}");
let info: CodeInfo<T> = CodeInfo {
determinism: module.determinism,
deposit: v.deposit,
refcount: v.refcount,
owner: v.owner,
};
(k, info)
})
.collect();

log::debug!(target: LOG_TARGET, "Taking sample of {} owner infos", sample.len());
Ok(sample.encode())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade_step(state: Vec<u8>) -> Result<(), TryRuntimeError> {
let sample = <Vec<(CodeHash<T>, CodeInfo<T>)> as Decode>::decode(&mut &state[..]).unwrap();

log::debug!(target: LOG_TARGET, "Validating sample of {} code infos", sample.len());
for (code_hash, old) in sample {
let info = CodeInfoOf::<T>::get(&code_hash).unwrap();

ensure!(info.determinism == old.determinism, "invalid determinism");
ensure!(info.owner == old.owner, "invalid owner");
ensure!(info.deposit == old.deposit, "invalid deposit");
ensure!(info.refcount == old.refcount, "invalid refcount");
}

Ok(())
}
}
33 changes: 33 additions & 0 deletions frame/contracts/src/weights.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.