Contracts remove deposit accounts (#14589)

* contracts: refactor currency to use fungible traits

* contracts: refactor currency to use fungible traits

* contracts: add minor improvements

* contracts: max holds config set

* contracts: fix some typos

* contracts: map token errors

* fix typo

* contracts: add 0 balance transfer to test

* contracts: not transfer if value is zero

* contracts: [WIP] add StorageDepositHold

* contracts: add storage deposit held event

* contracts: clean up some code and comments

* contracts: add deposit storage released event

* contracts: update comment

* contracts: update slash cannot kill account test

* contracts: fix tests

* contracts: add some comments to the slashing test

* contracts: add some comments to the slashing test

* contracts: remove references to Currency

* contracts: do not transfer if from equals to

* bound BalanceOf<T>

* added FixedPointOperand to Balance trait

* move migrate sequence to config

* remove commented out code

* Update frame/contracts/src/lib.rs

Co-authored-by: PG Herveou <pgherveou@gmail.com>

* remove Migrations generic

* make runtime use noop migrations

* restrict is_upgrade_supported

* undo is_upgrade_supported change

* Update bin/node/runtime/src/lib.rs

Co-authored-by: PG Herveou <pgherveou@gmail.com>

* add rust doc example for `Migrations`

* feature gate NoopMigration

* fix example code

* improve example

* wip

* remove FixedPointOperand from trait

* trait bound BalanceOf

* more trait bound BalanceOf

* update to use RuntimeHoldReason

* replace Fungible for Currency

* update runtime

* WIP

* make v10 benchmark generic over currency

* solve merge conflicts

* make v12 migration benchmarking generic over DepositPerItem and DepositPerByte

* give some format

* fix tests and old migrations

* add migration v13 placholder

* wip

* wip

* add benchmarking

* add weights

* wip

* [pallet_collective] Enforce prime is a valid member of collective in set_members extrinsic (#14354)

* Updated set_members extrinsic to enforce prime is valid member of collective

* Added additional tests for set_members extrinsic

* applied the code review suggestions

* update to docify 0.2.0 / crate-relative embed paths (#14570)

* Fix Society v2 migration (#14421)

* fix society v2 migration

* Update frame/society/src/migrations.rs

* Update frame/society/src/migrations.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update frame/society/src/migrations.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* update for versioned upgrade

* fix society v2 migration

* remove references to members being sorted from commnets

* fix type

* fix can_migrate check

* add sanity log

* fix sanity check

* kick ci

* kick ci

* run tests with --experimental flag

* versioned migration cleanup

* revert pipeline change

* use defensive!

* semicolons

* defensive and doc comment

* address pr comment

* feature gate the versioned migration

* defensive_unwrap_or

* fix test

* fix doc comment

* change defensive to a log warning

* remove can_migrate anti-pattern

* Update frame/society/Cargo.toml

Co-authored-by: Bastian Köcher <git@kchr.de>

* add experimental feature warning to doc comment

* update doc comment

* bump ci

* kick ci

* kick ci

* kick ci

---------

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: Bastian Köcher <git@kchr.de>

* Moves `Block` to `frame_system` instead of `construct_runtime` and removes `Header` and `BlockNumber` (#14437)

* Initial setup

* Adds node block

* Uses UncheckedExtrinsic and removes Where section

* Updates frame_system to use Block

* Adds deprecation warning

* Fixes pallet-timestamp

* Removes Header and BlockNumber

* Addresses review comments

* Addresses review comments

* Adds comment about compiler bug

* Removes where clause

* Refactors code

* Fixes errors in cargo check

* Fixes errors in cargo check

* Fixes warnings in cargo check

* Formatting

* Fixes construct_runtime tests

* Uses import instead of full path for BlockNumber

* Uses import instead of full path for Header

* Formatting

* Fixes construct_runtime tests

* Fixes imports in benchmarks

* Formatting

* Fixes construct_runtime tests

* Formatting

* Minor updates

* Fixes construct_runtime ui tests

* Fixes construct_runtime ui tests with 1.70

* Fixes docs

* Fixes docs

* Adds u128 mock block type

* Fixes split example

* fixes for cumulus

* ".git/.scripts/commands/fmt/fmt.sh"

* Updates new tests

* Fixes fully-qualified path in few places

* Formatting

* Update frame/examples/default-config/src/lib.rs

Co-authored-by: Juan <juangirini@gmail.com>

* Update frame/support/procedural/src/construct_runtime/mod.rs

Co-authored-by: Juan <juangirini@gmail.com>

* ".git/.scripts/commands/fmt/fmt.sh"

* Addresses some review comments

* Fixes build

* ".git/.scripts/commands/fmt/fmt.sh"

* Update frame/democracy/src/lib.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update frame/democracy/src/lib.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update frame/support/procedural/src/construct_runtime/mod.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update frame/support/procedural/src/construct_runtime/mod.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Addresses review comments

* Updates trait bounds

* Minor fix

* ".git/.scripts/commands/fmt/fmt.sh"

* Removes unnecessary bound

* ".git/.scripts/commands/fmt/fmt.sh"

* Updates test

* Fixes build

* Adds a bound for header

* ".git/.scripts/commands/fmt/fmt.sh"

* Removes where block

* Minor fix

* Minor fix

* Fixes tests

* ".git/.scripts/commands/update-ui/update-ui.sh" 1.70

* Updates test

* Update primitives/runtime/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/runtime/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Updates doc

* Updates doc

---------

Co-authored-by: command-bot <>
Co-authored-by: Juan <juangirini@gmail.com>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Bastian Köcher <git@kchr.de>

* Refactor the asset-conversion-tx-payment pallet (#14558)

* Code refactoring

* Fix imports

* Typo

* Update frame/asset-conversion/src/types.rs

Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com>

* Sync docs

---------

Co-authored-by: parity-processbot <>
Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com>

* wip

* wip

* wip

* improve try-runtime imports

* remove deposit account

* wip

* wip

* fix benchmark test

* improved rustdocs

* improved rustdocs

* remove log

* ignore variable

* reduce caller funding

* wip

* fix tests

* fix tests

* move v13 out

* add v14

* update v13 migration

* v13 migration

* benchmark v13_migration

* fix broken compilation

* ".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime=dev --target_dir=substrate --pallet=pallet_contracts

* remove all the `where BalanceOf`

* add Balance to Config

* improve docs

* add new deposit storage error

* remove todo message

* wip

* rename migration v13 pre rebase

* fix tests

* add missing migration;

* bump storage version

* apply review suggestions

* improved comment

* remove unnecessary code

* simplify migrations

* mock balance

* mock more for benchmarks

* make room for rebaes

* make room for rebase

* fix benchmarking tests

* fix benchmarking tests with caller

* improve cargo toml

* solve nit

* Update frame/contracts/src/lib.rs

Co-authored-by: Alexander Theißen <alex.theissen@me.com>

* Update frame/contracts/src/exec.rs

Co-authored-by: Alexander Theißen <alex.theissen@me.com>

* Update frame/contracts/src/exec.rs

Co-authored-by: Alexander Theißen <alex.theissen@me.com>

* Update frame/contracts/src/storage/meter.rs

Co-authored-by: Alexander Theißen <alex.theissen@me.com>

* review improvements

* remove extra events

* update cargo

* undo update cargo

* review updates

* wip

* wip

* fix test

* remove type Balance

* add extra fields to events

* fix zepter ci

* fix tests

* remove commented out code

* remove deposit_account from benchmarking

* update v15 migration

* wip

* remove deposit account from codebase

* add contract info to v15

* make try-runtime fixes

* fix wrong v14 logs

* add extra post upgrade steps

* remove old comments

* wip

* fix delegate deposit test

* ".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime=dev --target_dir=substrate --pallet=pallet_contracts

* wip

* wip

* wip

* implement review updates

* ".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime=dev --target_dir=substrate --pallet=pallet_contracts

* fix documentation

* fix consumers/providers

* fix consumers/providers

* do not check consumers in migration

* review comments addressed

* update fully qualified System

* wip

* wip

* Transfer to beneficiary after `transfer_on_hold` (#14767)

* transfer to beneficiary after transfer_on_hold

* wip

* add consumer comment

* review updates

* fix typo

* make clippy happy

* refactor `Terminated`

* rename ContractStatus to ContractState

* rename status to state

* replace Contribution::Alive to ContractState::Alive

* defer storage deposit charge

* ".git/.scripts/commands/fmt/fmt.sh"

* remove unused imports

* Update frame/contracts/src/migration/v15.rs

Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>

* Update frame/contracts/src/storage/meter.rs

Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>

---------

Co-authored-by: PG Herveou <pgherveou@gmail.com>
Co-authored-by: Toufeeq Pasha <47236805+ToufeeqP@users.noreply.github.com>
Co-authored-by: Sam Johnson <sam@durosoft.com>
Co-authored-by: Liam Aharon <liam.aharon@hotmail.com>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: gupnik <17176722+gupnik@users.noreply.github.com>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Jegor Sidorenko <5252494+jsidorenko@users.noreply.github.com>
Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com>
Co-authored-by: command-bot <>
Co-authored-by: Alexander Theißen <alex.theissen@me.com>
Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>
This commit is contained in:
Juan
2023-08-18 16:24:55 +02:00
committed by GitHub
parent 91d2ac11b3
commit 9c5315961e
15 changed files with 1931 additions and 1523 deletions
@@ -0,0 +1,327 @@
// 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 contracts' _reserved_ balance from the `deposit_account` to be _held_ in the contract's
//! account instead. Since [`Currency`](frame_support::traits::Currency) has been
//! [deprecated](https://github.com/paritytech/substrate/pull/12951), we need the deposits to be
//! handled by the [`frame_support::traits::fungible`] traits instead. For this transfer the
//! balance from the deposit account to the contract's account and hold it in there.
//! Then the deposit account is not needed anymore and we can get rid of it.
use crate::{
migration::{IsFinished, MigrationStep},
weights::WeightInfo,
AccountIdOf, BalanceOf, CodeHash, Config, HoldReason, Pallet, TrieId, Weight, LOG_TARGET,
};
#[cfg(feature = "try-runtime")]
use frame_support::{dispatch::Vec, traits::fungible::InspectHold};
use frame_support::{
pallet_prelude::*,
storage_alias,
traits::{
fungible::{Mutate, MutateHold},
tokens::{fungible::Inspect, Fortitude, Preservation},
},
BoundedBTreeMap, DefaultNoBound,
};
use frame_system::Pallet as System;
use sp_core::hexdisplay::HexDisplay;
#[cfg(feature = "try-runtime")]
use sp_runtime::TryRuntimeError;
use sp_runtime::{traits::Zero, Saturating};
mod old {
use super::*;
#[derive(
Encode, Decode, CloneNoBound, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen,
)]
#[scale_info(skip_type_params(T))]
pub struct ContractInfo<T: Config> {
pub trie_id: TrieId,
pub deposit_account: AccountIdOf<T>,
pub code_hash: CodeHash<T>,
pub storage_bytes: u32,
pub storage_items: u32,
pub storage_byte_deposit: BalanceOf<T>,
pub storage_item_deposit: BalanceOf<T>,
pub storage_base_deposit: BalanceOf<T>,
pub delegate_dependencies:
BoundedBTreeMap<CodeHash<T>, BalanceOf<T>, T::MaxDelegateDependencies>,
}
#[storage_alias]
pub type ContractInfoOf<T: Config> = StorageMap<
Pallet<T>,
Twox64Concat,
<T as frame_system::Config>::AccountId,
ContractInfo<T>,
>;
}
#[cfg(feature = "runtime-benchmarks")]
pub fn store_old_contract_info<T: Config>(account: T::AccountId, info: crate::ContractInfo<T>) {
use sp_runtime::traits::{Hash, TrailingZeroInput};
let entropy = (b"contract_depo_v1", account.clone()).using_encoded(T::Hashing::hash);
let deposit_account = Decode::decode(&mut TrailingZeroInput::new(entropy.as_ref()))
.expect("infinite length input; no invalid inputs for type; qed");
let info = old::ContractInfo {
trie_id: info.trie_id.clone(),
deposit_account,
code_hash: info.code_hash,
storage_bytes: Default::default(),
storage_items: Default::default(),
storage_byte_deposit: info.storage_byte_deposit,
storage_item_deposit: Default::default(),
storage_base_deposit: info.storage_base_deposit(),
delegate_dependencies: info.delegate_dependencies().clone(),
};
old::ContractInfoOf::<T>::insert(account, info);
}
#[derive(Encode, Decode, CloneNoBound, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
struct ContractInfo<T: Config> {
pub trie_id: TrieId,
pub code_hash: CodeHash<T>,
pub storage_bytes: u32,
pub storage_items: u32,
pub storage_byte_deposit: BalanceOf<T>,
pub storage_item_deposit: BalanceOf<T>,
pub storage_base_deposit: BalanceOf<T>,
pub delegate_dependencies:
BoundedBTreeMap<CodeHash<T>, BalanceOf<T>, T::MaxDelegateDependencies>,
}
#[storage_alias]
type ContractInfoOf<T: Config> =
StorageMap<Pallet<T>, Twox64Concat, <T as frame_system::Config>::AccountId, ContractInfo<T>>;
#[derive(Encode, Decode, MaxEncodedLen, DefaultNoBound)]
pub struct Migration<T: Config> {
last_account: Option<T::AccountId>,
}
impl<T: Config> MigrationStep for Migration<T> {
const VERSION: u16 = 15;
fn max_step_weight() -> Weight {
T::WeightInfo::v15_migration_step()
}
fn step(&mut self) -> (IsFinished, Weight) {
let mut iter = if let Some(last_account) = self.last_account.take() {
old::ContractInfoOf::<T>::iter_from(old::ContractInfoOf::<T>::hashed_key_for(
last_account,
))
} else {
old::ContractInfoOf::<T>::iter()
};
if let Some((account, old_contract)) = iter.next() {
let deposit_account = &old_contract.deposit_account;
System::<T>::dec_consumers(deposit_account);
// Get the deposit balance to transfer.
let total_deposit_balance = T::Currency::total_balance(deposit_account);
let reducible_deposit_balance = T::Currency::reducible_balance(
deposit_account,
Preservation::Expendable,
Fortitude::Force,
);
if total_deposit_balance > reducible_deposit_balance {
// This should never happen, as by design all balance in the deposit account is
// storage deposit and therefore reducible after decrementing the consumer
// reference.
log::warn!(
target: LOG_TARGET,
"Deposit account 0x{:?} for contract 0x{:?} has some non-reducible balance {:?} from a total of {:?} that will remain in there.",
HexDisplay::from(&deposit_account.encode()),
HexDisplay::from(&account.encode()),
total_deposit_balance.saturating_sub(reducible_deposit_balance),
total_deposit_balance
);
}
// Move balance reserved from the deposit account back to the contract account.
// Let the deposit account die.
log::debug!(
target: LOG_TARGET,
"Transferring {:?} from the deposit account 0x{:?} to the contract 0x{:?}.",
reducible_deposit_balance,
HexDisplay::from(&deposit_account.encode()),
HexDisplay::from(&account.encode())
);
let transferred_deposit_balance = T::Currency::transfer(
deposit_account,
&account,
reducible_deposit_balance,
Preservation::Expendable,
)
.unwrap_or_else(|err| {
log::error!(
target: LOG_TARGET,
"Failed to transfer {:?} from the deposit account 0x{:?} to the contract 0x{:?}, reason: {:?}.",
reducible_deposit_balance,
HexDisplay::from(&deposit_account.encode()),
HexDisplay::from(&account.encode()),
err
);
Zero::zero()
});
// Hold the reserved balance.
if transferred_deposit_balance == Zero::zero() {
log::warn!(
target: LOG_TARGET,
"No balance to hold as storage deposit on the contract 0x{:?}.",
HexDisplay::from(&account.encode())
);
} else {
log::debug!(
target: LOG_TARGET,
"Holding {:?} as storage deposit on the contract 0x{:?}.",
transferred_deposit_balance,
HexDisplay::from(&account.encode())
);
T::Currency::hold(
&HoldReason::StorageDepositReserve.into(),
&account,
transferred_deposit_balance,
)
.unwrap_or_else(|err| {
log::error!(
target: LOG_TARGET,
"Failed to hold {:?} as storage deposit on the contract 0x{:?}, reason: {:?}.",
transferred_deposit_balance,
HexDisplay::from(&account.encode()),
err
);
});
}
log::debug!(target: LOG_TARGET, "===");
let info = ContractInfo {
trie_id: old_contract.trie_id,
code_hash: old_contract.code_hash,
storage_bytes: old_contract.storage_bytes,
storage_items: old_contract.storage_items,
storage_byte_deposit: old_contract.storage_byte_deposit,
storage_item_deposit: old_contract.storage_item_deposit,
storage_base_deposit: old_contract.storage_base_deposit,
delegate_dependencies: old_contract.delegate_dependencies,
};
ContractInfoOf::<T>::insert(account.clone(), info);
// Store last key for next migration step
self.last_account = Some(account);
(IsFinished::No, T::WeightInfo::v15_migration_step())
} else {
log::info!(target: LOG_TARGET, "Done Migrating Storage Deposits.");
(IsFinished::Yes, T::WeightInfo::v15_migration_step())
}
}
#[cfg(feature = "try-runtime")]
fn pre_upgrade_step() -> Result<Vec<u8>, TryRuntimeError> {
let sample: Vec<_> = old::ContractInfoOf::<T>::iter().take(100).collect();
log::debug!(target: LOG_TARGET, "Taking sample of {} contracts", sample.len());
let state: Vec<(T::AccountId, old::ContractInfo<T>, BalanceOf<T>, BalanceOf<T>)> = sample
.iter()
.map(|(account, contract)| {
(
account.clone(),
contract.clone(),
T::Currency::total_balance(&account),
T::Currency::total_balance(&contract.deposit_account),
)
})
.collect();
Ok(state.encode())
}
#[cfg(feature = "try-runtime")]
fn post_upgrade_step(state: Vec<u8>) -> Result<(), TryRuntimeError> {
let sample =
<Vec<(T::AccountId, old::ContractInfo<T>, BalanceOf<T>, BalanceOf<T>)> as Decode>::decode(
&mut &state[..],
)
.expect("pre_upgrade_step provides a valid state; qed");
log::debug!(target: LOG_TARGET, "Validating sample of {} contracts", sample.len());
for (account, old_contract, old_account_balance, old_deposit_balance) in sample {
log::debug!(target: LOG_TARGET, "===");
log::debug!(target: LOG_TARGET, "Account: 0x{} ", HexDisplay::from(&account.encode()));
let on_hold =
T::Currency::balance_on_hold(&HoldReason::StorageDepositReserve.into(), &account);
let account_balance = T::Currency::total_balance(&account);
log::debug!(
target: LOG_TARGET,
"Validating balances match. Old deposit account's balance: {:?}. Contract's on hold: {:?}. Old contract's total balance: {:?}, Contract's total balance: {:?}.",
old_deposit_balance,
on_hold,
old_account_balance,
account_balance
);
ensure!(
old_account_balance.saturating_add(old_deposit_balance) == account_balance,
"total balance mismatch"
);
ensure!(old_deposit_balance == on_hold, "deposit mismatch");
ensure!(
!System::<T>::account_exists(&old_contract.deposit_account),
"deposit account still exists"
);
let migration_contract_info = ContractInfoOf::<T>::try_get(&account).unwrap();
let crate_contract_info = crate::ContractInfoOf::<T>::try_get(&account).unwrap();
ensure!(
migration_contract_info.trie_id == crate_contract_info.trie_id,
"trie_id mismatch"
);
ensure!(
migration_contract_info.code_hash == crate_contract_info.code_hash,
"code_hash mismatch"
);
ensure!(
migration_contract_info.storage_byte_deposit ==
crate_contract_info.storage_byte_deposit,
"storage_byte_deposit mismatch"
);
ensure!(
migration_contract_info.storage_base_deposit ==
crate_contract_info.storage_base_deposit(),
"storage_base_deposit mismatch"
);
ensure!(
&migration_contract_info.delegate_dependencies ==
crate_contract_info.delegate_dependencies(),
"delegate_dependencies mismatch"
);
}
Ok(())
}
}