WeightInfo for Vesting Pallet (#7103)

* WeightInfo for Vesting Pallet

* clean up weight docs

* Update lib.rs

* try to pipe max locks

* Update for new type

* add warning when locks > MaxLocks

* Update lib.rs

* fix compile

* remove aliasing, fix trait def

* Update
This commit is contained in:
Shawn Tabrizi
2020-09-16 21:48:10 +02:00
committed by GitHub
parent 0a6f7b08d7
commit 9aa8698cfc
37 changed files with 273 additions and 71 deletions
+38 -6
View File
@@ -28,7 +28,6 @@ use sp_runtime::traits::Bounded;
use crate::Module as Vesting;
const SEED: u32 = 0;
const MAX_LOCKS: u32 = 20;
type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
@@ -62,7 +61,7 @@ benchmarks! {
_ { }
vest_locked {
let l in 0 .. MAX_LOCKS;
let l in 0 .. MaxLocksOf::<T>::get();
let caller = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
@@ -86,7 +85,7 @@ benchmarks! {
}
vest_unlocked {
let l in 0 .. MAX_LOCKS;
let l in 0 .. MaxLocksOf::<T>::get();
let caller = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
@@ -110,7 +109,7 @@ benchmarks! {
}
vest_other_locked {
let l in 0 .. MAX_LOCKS;
let l in 0 .. MaxLocksOf::<T>::get();
let other: T::AccountId = account("other", 0, SEED);
let other_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(other.clone());
@@ -137,7 +136,7 @@ benchmarks! {
}
vest_other_unlocked {
let l in 0 .. MAX_LOCKS;
let l in 0 .. MaxLocksOf::<T>::get();
let other: T::AccountId = account("other", 0, SEED);
let other_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(other.clone());
@@ -164,7 +163,7 @@ benchmarks! {
}
vested_transfer {
let l in 0 .. MAX_LOCKS;
let l in 0 .. MaxLocksOf::<T>::get();
let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
@@ -193,6 +192,38 @@ benchmarks! {
"Lock not created",
);
}
force_vested_transfer {
let l in 0 .. MaxLocksOf::<T>::get();
let source: T::AccountId = account("source", 0, SEED);
let source_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(source.clone());
T::Currency::make_free_balance_be(&source, BalanceOf::<T>::max_value());
let target: T::AccountId = account("target", 0, SEED);
let target_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(target.clone());
// Give target existing locks
add_locks::<T>(&target, l as u8);
let transfer_amount = T::MinVestedTransfer::get();
let vesting_schedule = VestingInfo {
locked: transfer_amount,
per_block: 10.into(),
starting_block: 1.into(),
};
}: _(RawOrigin::Root, source_lookup, target_lookup, vesting_schedule)
verify {
assert_eq!(
T::MinVestedTransfer::get(),
T::Currency::free_balance(&target),
"Transfer didn't happen",
);
assert_eq!(
Vesting::<T>::vesting_balance(&target),
Some(T::MinVestedTransfer::get()),
"Lock not created",
);
}
}
#[cfg(test)]
@@ -209,6 +240,7 @@ mod tests {
assert_ok!(test_benchmark_vest_other_locked::<Test>());
assert_ok!(test_benchmark_vest_other_unlocked::<Test>());
assert_ok!(test_benchmark_vested_transfer::<Test>());
assert_ok!(test_benchmark_force_vested_transfer::<Test>());
});
}
}
@@ -0,0 +1,62 @@
// 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.
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0-rc6
#![allow(unused_parens)]
#![allow(unused_imports)]
use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight};
impl crate::WeightInfo for () {
fn vest_locked(l: u32, ) -> Weight {
(82109000 as Weight)
.saturating_add((332000 as Weight).saturating_mul(l as Weight))
.saturating_add(DbWeight::get().reads(2 as Weight))
.saturating_add(DbWeight::get().writes(1 as Weight))
}
fn vest_unlocked(l: u32, ) -> Weight {
(88419000 as Weight)
.saturating_add((3000 as Weight).saturating_mul(l as Weight))
.saturating_add(DbWeight::get().reads(2 as Weight))
.saturating_add(DbWeight::get().writes(2 as Weight))
}
fn vest_other_locked(l: u32, ) -> Weight {
(81277000 as Weight)
.saturating_add((321000 as Weight).saturating_mul(l as Weight))
.saturating_add(DbWeight::get().reads(3 as Weight))
.saturating_add(DbWeight::get().writes(2 as Weight))
}
fn vest_other_unlocked(l: u32, ) -> Weight {
(87584000 as Weight)
.saturating_add((19000 as Weight).saturating_mul(l as Weight))
.saturating_add(DbWeight::get().reads(3 as Weight))
.saturating_add(DbWeight::get().writes(3 as Weight))
}
fn vested_transfer(l: u32, ) -> Weight {
(185916000 as Weight)
.saturating_add((625000 as Weight).saturating_mul(l as Weight))
.saturating_add(DbWeight::get().reads(3 as Weight))
.saturating_add(DbWeight::get().writes(3 as Weight))
}
fn force_vested_transfer(l: u32, ) -> Weight {
(185916000 as Weight)
.saturating_add((625000 as Weight).saturating_mul(l as Weight))
.saturating_add(DbWeight::get().reads(4 as Weight))
.saturating_add(DbWeight::get().writes(4 as Weight))
}
}
+16 -25
View File
@@ -61,8 +61,10 @@ use frame_support::traits::{
use frame_system::{ensure_signed, ensure_root};
mod benchmarking;
mod default_weights;
type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
type MaxLocksOf<T> = <<T as Trait>::Currency as LockableCurrency<<T as frame_system::Trait>::AccountId>>::MaxLocks;
pub trait WeightInfo {
fn vest_locked(l: u32, ) -> Weight;
@@ -70,14 +72,7 @@ pub trait WeightInfo {
fn vest_other_locked(l: u32, ) -> Weight;
fn vest_other_unlocked(l: u32, ) -> Weight;
fn vested_transfer(l: u32, ) -> Weight;
}
impl WeightInfo for () {
fn vest_locked(_l: u32, ) -> Weight { 1_000_000_000 }
fn vest_unlocked(_l: u32, ) -> Weight { 1_000_000_000 }
fn vest_other_locked(_l: u32, ) -> Weight { 1_000_000_000 }
fn vest_other_unlocked(_l: u32, ) -> Weight { 1_000_000_000 }
fn vested_transfer(_l: u32, ) -> Weight { 1_000_000_000 }
fn force_vested_transfer(l: u32, ) -> Weight;
}
pub trait Trait: frame_system::Trait {
@@ -171,7 +166,7 @@ decl_storage! {
decl_event!(
pub enum Event<T> where AccountId = <T as frame_system::Trait>::AccountId, Balance = BalanceOf<T> {
/// The amount vested has been updated. This could indicate more funds are available. The
/// balance given is the amount which is left unvested (and thus locked).
/// balance given is the amount which is left unvested (and thus locked).
/// \[account, unvested\]
VestingUpdated(AccountId, Balance),
/// An \[account\] has become fully vested. No further vesting can happen.
@@ -213,12 +208,10 @@ decl_module! {
/// - DbWeight: 2 Reads, 2 Writes
/// - Reads: Vesting Storage, Balances Locks, [Sender Account]
/// - Writes: Vesting Storage, Balances Locks, [Sender Account]
/// - Benchmark:
/// - Unlocked: 48.76 + .048 * l µs (min square analysis)
/// - Locked: 44.43 + .284 * l µs (min square analysis)
/// - Using 50 µs fixed. Assuming less than 50 locks on any user, else we may want factor in number of locks.
/// # </weight>
#[weight = 50_000_000 + T::DbWeight::get().reads_writes(2, 2)]
#[weight = T::WeightInfo::vest_locked(MaxLocksOf::<T>::get())
.max(T::WeightInfo::vest_unlocked(MaxLocksOf::<T>::get()))
]
fn vest(origin) -> DispatchResult {
let who = ensure_signed(origin)?;
Self::update_lock(who)
@@ -238,12 +231,10 @@ decl_module! {
/// - DbWeight: 3 Reads, 3 Writes
/// - Reads: Vesting Storage, Balances Locks, Target Account
/// - Writes: Vesting Storage, Balances Locks, Target Account
/// - Benchmark:
/// - Unlocked: 44.3 + .294 * l µs (min square analysis)
/// - Locked: 48.16 + .103 * l µs (min square analysis)
/// - Using 50 µs fixed. Assuming less than 50 locks on any user, else we may want factor in number of locks.
/// # </weight>
#[weight = 50_000_000 + T::DbWeight::get().reads_writes(3, 3)]
#[weight = T::WeightInfo::vest_other_locked(MaxLocksOf::<T>::get())
.max(T::WeightInfo::vest_other_unlocked(MaxLocksOf::<T>::get()))
]
fn vest_other(origin, target: <T::Lookup as StaticLookup>::Source) -> DispatchResult {
ensure_signed(origin)?;
Self::update_lock(T::Lookup::lookup(target)?)
@@ -264,10 +255,8 @@ decl_module! {
/// - DbWeight: 3 Reads, 3 Writes
/// - Reads: Vesting Storage, Balances Locks, Target Account, [Sender Account]
/// - Writes: Vesting Storage, Balances Locks, Target Account, [Sender Account]
/// - Benchmark: 100.3 + .365 * l µs (min square analysis)
/// - Using 100 µs fixed. Assuming less than 50 locks on any user, else we may want factor in number of locks.
/// # </weight>
#[weight = 100_000_000 + T::DbWeight::get().reads_writes(3, 3)]
#[weight = T::WeightInfo::vested_transfer(MaxLocksOf::<T>::get())]
pub fn vested_transfer(
origin,
target: <T::Lookup as StaticLookup>::Source,
@@ -303,10 +292,8 @@ decl_module! {
/// - DbWeight: 4 Reads, 4 Writes
/// - Reads: Vesting Storage, Balances Locks, Target Account, Source Account
/// - Writes: Vesting Storage, Balances Locks, Target Account, Source Account
/// - Benchmark: 100.3 + .365 * l µs (min square analysis)
/// - Using 100 µs fixed. Assuming less than 50 locks on any user, else we may want factor in number of locks.
/// # </weight>
#[weight = 100_000_000 + T::DbWeight::get().reads_writes(4, 4)]
#[weight = T::WeightInfo::force_vested_transfer(MaxLocksOf::<T>::get())]
pub fn force_vested_transfer(
origin,
source: <T::Lookup as StaticLookup>::Source,
@@ -463,12 +450,16 @@ mod tests {
type OnKilledAccount = ();
type SystemWeightInfo = ();
}
parameter_types! {
pub const MaxLocks: u32 = 10;
}
impl pallet_balances::Trait for Test {
type Balance = u64;
type DustRemoval = ();
type Event = ();
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
type MaxLocks = MaxLocks;
type WeightInfo = ();
}
parameter_types! {