Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

WeightInfo for Vesting Pallet #7103

Merged
11 commits merged into from
Sep 16, 2020
6 changes: 5 additions & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,14 +849,18 @@ impl pallet_society::Trait for Runtime {

parameter_types! {
pub const MinVestedTransfer: Balance = 100 * DOLLARS;
// For weight estimation, we assume that the most locks on an individual account will be 50.
// This number may need to be adjusted in the future if this assumption no longer holds true.
pub const MaxLocks: u32 = 50;
shawntabrizi marked this conversation as resolved.
Show resolved Hide resolved
}

impl pallet_vesting::Trait for Runtime {
type Event = Event;
type Currency = Balances;
type BlockNumberToBalance = ConvertInto;
type MinVestedTransfer = MinVestedTransfer;
type WeightInfo = ();
type MaxLocks = MaxLocks;
type WeightInfo = weights::pallet_vesting::WeightInfo;
}

construct_runtime!(
Expand Down
1 change: 1 addition & 0 deletions bin/node/runtime/src/weights/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ pub mod pallet_democracy;
pub mod pallet_proxy;
pub mod pallet_timestamp;
pub mod pallet_utility;
pub mod pallet_vesting;
63 changes: 63 additions & 0 deletions bin/node/runtime/src/weights/pallet_vesting.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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};

pub struct WeightInfo;
impl pallet_vesting::WeightInfo for WeightInfo {
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
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))
}
}
44 changes: 38 additions & 6 deletions frame/vesting/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -62,7 +61,7 @@ benchmarks! {
_ { }

vest_locked {
let l in 0 .. MAX_LOCKS;
let l in 0 .. T::MaxLocks::get();

let caller = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
Expand All @@ -86,7 +85,7 @@ benchmarks! {
}

vest_unlocked {
let l in 0 .. MAX_LOCKS;
let l in 0 .. T::MaxLocks::get();

let caller = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
Expand All @@ -110,7 +109,7 @@ benchmarks! {
}

vest_other_locked {
let l in 0 .. MAX_LOCKS;
let l in 0 .. T::MaxLocks::get();

let other: T::AccountId = account("other", 0, SEED);
let other_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(other.clone());
Expand All @@ -137,7 +136,7 @@ benchmarks! {
}

vest_other_unlocked {
let l in 0 .. MAX_LOCKS;
let l in 0 .. T::MaxLocks::get();

let other: T::AccountId = account("other", 0, SEED);
let other_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(other.clone());
Expand All @@ -164,7 +163,7 @@ benchmarks! {
}

vested_transfer {
let l in 0 .. MAX_LOCKS;
let l in 0 .. T::MaxLocks::get();

let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
Expand Down Expand Up @@ -193,6 +192,38 @@ benchmarks! {
"Lock not created",
);
}

force_vested_transfer {
let l in 0 .. T::MaxLocks::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)]
Expand All @@ -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>());
});
}
}
62 changes: 62 additions & 0 deletions frame/vesting/src/default_weights.rs
Original file line number Diff line number Diff line change
@@ -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))
}
}
41 changes: 16 additions & 25 deletions frame/vesting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ 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;

Expand All @@ -70,14 +71,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 {
Expand All @@ -93,6 +87,10 @@ pub trait Trait: frame_system::Trait {
/// The minimum amount transferred to call `vested_transfer`.
type MinVestedTransfer: Get<BalanceOf<Self>>;

/// The maximum number of balance locks for a user. Used for weight estimation,
/// and not enforced by this pallet.
type MaxLocks: Get<u32>;

/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}
Expand Down Expand Up @@ -171,7 +169,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.
Expand Down Expand Up @@ -213,12 +211,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(T::MaxLocks::get())
.max(T::WeightInfo::vest_unlocked(T::MaxLocks::get()))
]
fn vest(origin) -> DispatchResult {
let who = ensure_signed(origin)?;
Self::update_lock(who)
Expand All @@ -238,12 +234,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(T::MaxLocks::get())
.max(T::WeightInfo::vest_other_unlocked(T::MaxLocks::get()))
]
fn vest_other(origin, target: <T::Lookup as StaticLookup>::Source) -> DispatchResult {
ensure_signed(origin)?;
Self::update_lock(T::Lookup::lookup(target)?)
Expand All @@ -264,10 +258,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(T::MaxLocks::get())]
pub fn vested_transfer(
origin,
target: <T::Lookup as StaticLookup>::Source,
Expand Down Expand Up @@ -303,10 +295,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(T::MaxLocks::get())]
pub fn force_vested_transfer(
origin,
source: <T::Lookup as StaticLookup>::Source,
Expand Down Expand Up @@ -479,6 +469,7 @@ mod tests {
type Currency = Balances;
type BlockNumberToBalance = Identity;
type MinVestedTransfer = MinVestedTransfer;
type MaxLocks = ();
type WeightInfo = ();
}
type System = frame_system::Module<Test>;
Expand Down