Features needed for reserve-backed stablecoins (#7152)

* Features needed for reserve-backed stablecoins

* Builds & tests.

* Double map for an efficient destroy.

* Update frame/assets/src/lib.rs

Co-authored-by: Nikolay Volf <nikvolf@gmail.com>

* ED/zombie-count/refs

Feature: ED/minimum balance enforcement
Feature: enforce zombie count
Feature: allow system-alive accounts to exist, but add reference

* Update frame/assets/src/lib.rs

Co-authored-by: Nikolay Volf <nikvolf@gmail.com>

* Update frame/assets/Cargo.toml

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>

* Docs

* Some tests

* More tests

* Allow for max_zombies to be adjusted

* Test for set_max_zombies

* Tests and a couple of fixes

* First few benchmarks

* Benchmarks.

* Fix error message in test

* Fixes

* Fixes

* Fixes

* cargo run --release --features runtime-benchmarks --manifest-path bin/node/cli/Cargo.toml -- benchmark --chain dev --steps 50 --repeat 20 --extrinsic * --execution=wasm --wasm-execution=compiled --output ./bin/node/runtime/src/weights --header ./HEADER --pallet pallet_assets

* Update frame/assets/src/lib.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Fixes

* Fixes

* Fixes

* cargo run --release --features runtime-benchmarks --manifest-path bin/node/cli/Cargo.toml -- benchmark --chain dev --steps 50 --repeat 20 --extrinsic * --execution=wasm --wasm-execution=compiled --output ./bin/node/runtime/src/weights --header ./HEADER --pallet pallet_assets

* Fixes

* Update default weight

* Add proper verification to benchmarks

* minor improvements to tests

* Update frame/assets/src/benchmarking.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Fix

* New weights system

* fix compile

* cargo run --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_assets --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/assets/src/weights.rs --template=./.maintain/frame-weight-template.hbs

Co-authored-by: Nikolay Volf <nikvolf@gmail.com>
Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
Co-authored-by: Parity Benchmarking Bot <admin@parity.io>
Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
This commit is contained in:
Gavin Wood
2020-12-04 16:57:09 +01:00
committed by GitHub
parent 947b82df68
commit 80af50b830
10 changed files with 1616 additions and 183 deletions
+298
View File
@@ -0,0 +1,298 @@
// This file is part of Substrate.
// Copyright (C) 2020 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.
//! Assets pallet benchmarking.
use super::*;
use sp_std::prelude::*;
use sp_runtime::traits::Bounded;
use frame_system::RawOrigin as SystemOrigin;
use frame_benchmarking::{benchmarks, account, whitelisted_caller};
use crate::Module as Assets;
const SEED: u32 = 0;
fn create_default_asset<T: Config>(max_zombies: u32)
-> (T::AccountId, <T::Lookup as StaticLookup>::Source)
{
let caller: T::AccountId = whitelisted_caller();
let caller_lookup = T::Lookup::unlookup(caller.clone());
let root = SystemOrigin::Root.into();
assert!(Assets::<T>::force_create(
root,
Default::default(),
caller_lookup.clone(),
max_zombies,
1u32.into(),
).is_ok());
(caller, caller_lookup)
}
fn create_default_minted_asset<T: Config>(max_zombies: u32, amount: T::Balance)
-> (T::AccountId, <T::Lookup as StaticLookup>::Source)
{
let (caller, caller_lookup) = create_default_asset::<T>(max_zombies);
assert!(Assets::<T>::mint(
SystemOrigin::Signed(caller.clone()).into(),
Default::default(),
caller_lookup.clone(),
amount,
).is_ok());
(caller, caller_lookup)
}
fn add_zombies<T: Config>(minter: T::AccountId, n: u32) {
let origin = SystemOrigin::Signed(minter);
for i in 0..n {
let target = account("zombie", i, SEED);
let target_lookup = T::Lookup::unlookup(target);
assert!(Assets::<T>::mint(origin.clone().into(), Default::default(), target_lookup, 100u32.into()).is_ok());
}
}
fn assert_last_event<T: Config>(generic_event: <T as Config>::Event) {
let events = frame_system::Module::<T>::events();
let system_event: <T as frame_system::Config>::Event = generic_event.into();
// compare to the last event record
let frame_system::EventRecord { event, .. } = &events[events.len() - 1];
assert_eq!(event, &system_event);
}
benchmarks! {
_ { }
create {
let caller: T::AccountId = whitelisted_caller();
let caller_lookup = T::Lookup::unlookup(caller.clone());
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
}: _(SystemOrigin::Signed(caller.clone()), Default::default(), caller_lookup, 1, 1u32.into())
verify {
assert_last_event::<T>(RawEvent::Created(Default::default(), caller.clone(), caller).into());
}
force_create {
let caller: T::AccountId = whitelisted_caller();
let caller_lookup = T::Lookup::unlookup(caller.clone());
}: _(SystemOrigin::Root, Default::default(), caller_lookup, 1, 1u32.into())
verify {
assert_last_event::<T>(RawEvent::ForceCreated(Default::default(), caller).into());
}
destroy {
let z in 0 .. 10_000;
let (caller, _) = create_default_asset::<T>(10_000);
add_zombies::<T>(caller.clone(), z);
}: _(SystemOrigin::Signed(caller), Default::default(), 10_000)
verify {
assert_last_event::<T>(RawEvent::Destroyed(Default::default()).into());
}
force_destroy {
let z in 0 .. 10_000;
let (caller, _) = create_default_asset::<T>(10_000);
add_zombies::<T>(caller.clone(), z);
}: _(SystemOrigin::Root, Default::default(), 10_000)
verify {
assert_last_event::<T>(RawEvent::Destroyed(Default::default()).into());
}
mint {
let (caller, caller_lookup) = create_default_asset::<T>(10);
let amount = T::Balance::from(100u32);
}: _(SystemOrigin::Signed(caller.clone()), Default::default(), caller_lookup, amount)
verify {
assert_last_event::<T>(RawEvent::Issued(Default::default(), caller, amount).into());
}
burn {
let amount = T::Balance::from(100u32);
let (caller, caller_lookup) = create_default_minted_asset::<T>(10, amount);
}: _(SystemOrigin::Signed(caller.clone()), Default::default(), caller_lookup, amount)
verify {
assert_last_event::<T>(RawEvent::Burned(Default::default(), caller, amount).into());
}
transfer {
let amount = T::Balance::from(100u32);
let (caller, caller_lookup) = create_default_minted_asset::<T>(10, amount);
let target: T::AccountId = account("target", 0, SEED);
let target_lookup = T::Lookup::unlookup(target.clone());
}: _(SystemOrigin::Signed(caller.clone()), Default::default(), target_lookup, amount)
verify {
assert_last_event::<T>(RawEvent::Transferred(Default::default(), caller, target, amount).into());
}
force_transfer {
let amount = T::Balance::from(100u32);
let (caller, caller_lookup) = create_default_minted_asset::<T>(10, amount);
let target: T::AccountId = account("target", 0, SEED);
let target_lookup = T::Lookup::unlookup(target.clone());
}: _(SystemOrigin::Signed(caller.clone()), Default::default(), caller_lookup, target_lookup, amount)
verify {
assert_last_event::<T>(RawEvent::ForceTransferred(Default::default(), caller, target, amount).into());
}
freeze {
let (caller, caller_lookup) = create_default_minted_asset::<T>(10, 100u32.into());
}: _(SystemOrigin::Signed(caller.clone()), Default::default(), caller_lookup)
verify {
assert_last_event::<T>(RawEvent::Frozen(Default::default(), caller).into());
}
thaw {
let (caller, caller_lookup) = create_default_minted_asset::<T>(10, 100u32.into());
assert!(Assets::<T>::freeze(
SystemOrigin::Signed(caller.clone()).into(),
Default::default(),
caller_lookup.clone()
).is_ok());
}: _(SystemOrigin::Signed(caller.clone()), Default::default(), caller_lookup)
verify {
assert_last_event::<T>(RawEvent::Thawed(Default::default(), caller).into());
}
transfer_ownership {
let (caller, _) = create_default_asset::<T>(10);
let target: T::AccountId = account("target", 0, SEED);
let target_lookup = T::Lookup::unlookup(target.clone());
}: _(SystemOrigin::Signed(caller), Default::default(), target_lookup)
verify {
assert_last_event::<T>(RawEvent::OwnerChanged(Default::default(), target).into());
}
set_team {
let (caller, _) = create_default_asset::<T>(10);
let target0 = T::Lookup::unlookup(account("target", 0, SEED));
let target1 = T::Lookup::unlookup(account("target", 1, SEED));
let target2 = T::Lookup::unlookup(account("target", 2, SEED));
}: _(SystemOrigin::Signed(caller), Default::default(), target0.clone(), target1.clone(), target2.clone())
verify {
assert_last_event::<T>(RawEvent::TeamChanged(
Default::default(),
account("target", 0, SEED),
account("target", 1, SEED),
account("target", 2, SEED),
).into());
}
set_max_zombies {
let (caller, _) = create_default_asset::<T>(10);
let max_zombies: u32 = 100;
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
}: _(SystemOrigin::Signed(caller), Default::default(), max_zombies)
verify {
assert_last_event::<T>(RawEvent::MaxZombiesChanged(Default::default(), max_zombies).into());
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::{new_test_ext, Test};
#[test]
fn create() {
new_test_ext().execute_with(|| {
assert!(test_benchmark_create::<Test>().is_ok());
});
}
#[test]
fn force_create() {
new_test_ext().execute_with(|| {
assert!(test_benchmark_force_create::<Test>().is_ok());
});
}
#[test]
fn destroy() {
new_test_ext().execute_with(|| {
assert!(test_benchmark_destroy::<Test>().is_ok());
});
}
#[test]
fn force_destroy() {
new_test_ext().execute_with(|| {
assert!(test_benchmark_force_destroy::<Test>().is_ok());
});
}
#[test]
fn mint() {
new_test_ext().execute_with(|| {
assert!(test_benchmark_mint::<Test>().is_ok());
});
}
#[test]
fn burn() {
new_test_ext().execute_with(|| {
assert!(test_benchmark_burn::<Test>().is_ok());
});
}
#[test]
fn transfer() {
new_test_ext().execute_with(|| {
assert!(test_benchmark_transfer::<Test>().is_ok());
});
}
#[test]
fn force_transfer() {
new_test_ext().execute_with(|| {
assert!(test_benchmark_force_transfer::<Test>().is_ok());
});
}
#[test]
fn freeze() {
new_test_ext().execute_with(|| {
assert!(test_benchmark_freeze::<Test>().is_ok());
});
}
#[test]
fn thaw() {
new_test_ext().execute_with(|| {
assert!(test_benchmark_thaw::<Test>().is_ok());
});
}
#[test]
fn transfer_ownership() {
new_test_ext().execute_with(|| {
assert!(test_benchmark_transfer_ownership::<Test>().is_ok());
});
}
#[test]
fn set_team() {
new_test_ext().execute_with(|| {
assert!(test_benchmark_set_team::<Test>().is_ok());
});
}
#[test]
fn set_max_zombies() {
new_test_ext().execute_with(|| {
assert!(test_benchmark_set_max_zombies::<Test>().is_ok());
});
}
}
File diff suppressed because it is too large Load Diff
+207
View File
@@ -0,0 +1,207 @@
// This file is part of Substrate.
// Copyright (C) 2020 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.
//! Autogenerated weights for pallet_assets
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0
//! DATE: 2020-12-03, STEPS: [50, ], REPEAT: 20, LOW RANGE: [], HIGH RANGE: []
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128
// Executed Command:
// target/release/substrate
// benchmark
// --chain=dev
// --steps=50
// --repeat=20
// --pallet=pallet_assets
// --extrinsic=*
// --execution=wasm
// --wasm-execution=compiled
// --heap-pages=4096
// --output=./frame/assets/src/weights.rs
// --template=./.maintain/frame-weight-template.hbs
#![allow(unused_parens)]
#![allow(unused_imports)]
use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
use sp_std::marker::PhantomData;
/// Weight functions needed for pallet_assets.
pub trait WeightInfo {
fn create() -> Weight;
fn force_create() -> Weight;
fn destroy(z: u32, ) -> Weight;
fn force_destroy(z: u32, ) -> Weight;
fn mint() -> Weight;
fn burn() -> Weight;
fn transfer() -> Weight;
fn force_transfer() -> Weight;
fn freeze() -> Weight;
fn thaw() -> Weight;
fn transfer_ownership() -> Weight;
fn set_team() -> Weight;
fn set_max_zombies() -> Weight;
}
/// Weights for pallet_assets using the Substrate node and recommended hardware.
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
fn create() -> Weight {
(58_077_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
fn force_create() -> Weight {
(30_497_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
fn destroy(z: u32, ) -> Weight {
(0 as Weight)
.saturating_add((1_153_000 as Weight).saturating_mul(z as Weight))
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
.saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(z as Weight)))
}
fn force_destroy(z: u32, ) -> Weight {
(0 as Weight)
.saturating_add((1_153_000 as Weight).saturating_mul(z as Weight))
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
.saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(z as Weight)))
}
fn mint() -> Weight {
(45_600_000 as Weight)
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
fn burn() -> Weight {
(40_143_000 as Weight)
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
fn transfer() -> Weight {
(58_903_000 as Weight)
.saturating_add(T::DbWeight::get().reads(4 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
fn force_transfer() -> Weight {
(59_025_000 as Weight)
.saturating_add(T::DbWeight::get().reads(4 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
fn freeze() -> Weight {
(43_308_000 as Weight)
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
fn thaw() -> Weight {
(43_383_000 as Weight)
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
fn transfer_ownership() -> Weight {
(31_380_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
fn set_team() -> Weight {
(32_049_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
fn set_max_zombies() -> Weight {
(57_745_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
}
// For backwards compatibility and tests
impl WeightInfo for () {
fn create() -> Weight {
(58_077_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
fn force_create() -> Weight {
(30_497_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
fn destroy(z: u32, ) -> Weight {
(0 as Weight)
.saturating_add((1_153_000 as Weight).saturating_mul(z as Weight))
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
.saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(z as Weight)))
}
fn force_destroy(z: u32, ) -> Weight {
(0 as Weight)
.saturating_add((1_153_000 as Weight).saturating_mul(z as Weight))
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
.saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(z as Weight)))
}
fn mint() -> Weight {
(45_600_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
}
fn burn() -> Weight {
(40_143_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
}
fn transfer() -> Weight {
(58_903_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(4 as Weight))
.saturating_add(RocksDbWeight::get().writes(3 as Weight))
}
fn force_transfer() -> Weight {
(59_025_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(4 as Weight))
.saturating_add(RocksDbWeight::get().writes(3 as Weight))
}
fn freeze() -> Weight {
(43_308_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
fn thaw() -> Weight {
(43_383_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
fn transfer_ownership() -> Weight {
(31_380_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
fn set_team() -> Weight {
(32_049_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
fn set_max_zombies() -> Weight {
(57_745_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
}