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
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
rename ContainsCountUpperBound -> ContainsLengthBound
  • Loading branch information
gui1117 committed Apr 22, 2020
commit 952afa85bc52f7bf9972c3f69f8057dcffdb6294
10 changes: 6 additions & 4 deletions frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ use frame_support::{
traits::{
Currency, Get, LockableCurrency, LockIdentifier, ReservableCurrency, WithdrawReasons,
ChangeMembers, OnUnbalanced, WithdrawReason, Contains, BalanceStatus, InitializeMembers,
ContainsCountUpperBound,
ContainsLengthBound,
}
};
use sp_phragmen::{build_support_map, ExtendedBalance, VoteWeight, PhragmenResult};
Expand Down Expand Up @@ -879,9 +879,11 @@ impl<T: Trait> Contains<T::AccountId> for Module<T> {
}
}

/// Implementation uses a parameter type so calling is cost-free.
impl<T: Trait> ContainsCountUpperBound for Module<T> {
fn count_upper_bound() -> usize {
impl<T: Trait> ContainsLengthBound for Module<T> {
fn min_len() -> usize { 0 }

/// Implementation uses a parameter type so calling is cost-free.
fn max_len() -> usize {
Self::desired_members() as usize
}
}
Expand Down
9 changes: 4 additions & 5 deletions frame/support/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,10 @@ pub trait Contains<T: Ord> {
fn add(t: &T) { unimplemented!() }
}

/// A trait for querying an upper bound on the number of element in a type that implements
/// `Contains`.
pub trait ContainsCountUpperBound {
/// An upper bound for the number of element contained.
fn count_upper_bound() -> usize;
/// A trait for querying bound for the length of an implementation of `Contains`
pub trait ContainsLengthBound {
fn min_len() -> usize;
fn max_len() -> usize;
}

/// Determiner to say whether a given account is unused.
Expand Down
19 changes: 9 additions & 10 deletions frame/treasury/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ use sp_runtime::{Permill, ModuleId, Percent, RuntimeDebug, traits::{
Zero, StaticLookup, AccountIdConversion, Saturating, Hash, BadOrigin
}};
use frame_support::weights::Weight;
use frame_support::traits::{Contains, ContainsCountUpperBound, EnsureOrigin};
use frame_support::traits::{Contains, ContainsLengthBound, EnsureOrigin};
use codec::{Encode, Decode};
use frame_system::{self as system, ensure_signed, ensure_root};

Expand All @@ -125,9 +125,8 @@ pub trait Trait: frame_system::Trait {

/// Origin from which tippers must come.
///
/// `ContainsCountUpperBound::count_upper_bound` must be cost free.
/// (i.e. no storage read or heavy operation)
type Tippers: Contains<Self::AccountId> + ContainsCountUpperBound;
/// `ContainsLengthBound::max_len` must be cost free (i.e. no storage read or heavy operation).
type Tippers: Contains<Self::AccountId> + ContainsLengthBound;

/// The period for which a tip remains open after is has achieved threshold tippers.
type TipCountdown: Get<Self::BlockNumber>;
Expand Down Expand Up @@ -479,15 +478,15 @@ decl_module! {
/// # <weight>
/// - Complexity: `O(R + T)` where `R` length of `reason`, `T` is the number of tippers.
/// - `O(T)`: decoding `Tipper` vec of length `T`
/// `T` is charged as upper bound given by `ContainsCountUpperBound`.
/// `T` is charged as upper bound given by `ContainsLengthBound`.
/// The actual cost depends on the implementation of `T::Tippers`.
/// - `O(R)`: hashing and encoding of reason of length `R`
/// - DbReads: `Tippers`, `Reasons`
/// - DbWrites: `Reasons`, `Tips`
/// # </weight>
#[weight = 110_000_000
+ 4_000 * reason.len() as Weight
+ 480_000 * T::Tippers::count_upper_bound() as Weight
+ 480_000 * T::Tippers::max_len() as Weight
+ T::DbWeight::get().reads_writes(2, 2)]
fn tip_new(origin, reason: Vec<u8>, who: T::AccountId, tip_value: BalanceOf<T>) {
let tipper = ensure_signed(origin)?;
Expand Down Expand Up @@ -520,15 +519,15 @@ decl_module! {
/// # <weight>
/// - Complexity: `O(T)` where `T` is the number of tippers.
/// decoding `Tipper` vec of length `T`, insert tip and check closing,
/// `T` is charged as upper bound given by `ContainsCountUpperBound`.
/// `T` is charged as upper bound given by `ContainsLengthBound`.
/// The actual cost depends on the implementation of `T::Tippers`.
///
/// Actually weight could be lower as it depends on how many tips are in `OpenTip` but it
/// is weighted as if almost full i.e of length `T-1`.
/// - DbReads: `Tippers`, `Tips`
/// - DbWrites: `Tips`
/// # </weight>
#[weight = 68_000_000 + 2_000_000 * T::Tippers::count_upper_bound() as Weight
#[weight = 68_000_000 + 2_000_000 * T::Tippers::max_len() as Weight
+ T::DbWeight::get().reads_writes(2, 1)]
fn tip(origin, hash: T::Hash, tip_value: BalanceOf<T>) {
let tipper = ensure_signed(origin)?;
Expand All @@ -553,12 +552,12 @@ decl_module! {
/// # <weight>
/// - Complexity: `O(T)` where `T` is the number of tippers.
/// decoding `Tipper` vec of length `T`.
/// `T` is charged as upper bound given by `ContainsCountUpperBound`.
/// `T` is charged as upper bound given by `ContainsLengthBound`.
/// The actual cost depends on the implementation of `T::Tippers`.
/// - DbReads: `Tips`, `Tippers`, `tip finder`
/// - DbWrites: `Reasons`, `Tips`, `Tippers`, `tip finder`
/// # </weight>
#[weight = 220_000_000 + 1_100_000 * T::Tippers::count_upper_bound() as Weight
#[weight = 220_000_000 + 1_100_000 * T::Tippers::max_len() as Weight
+ T::DbWeight::get().reads_writes(3, 3)]
fn close_tip(origin, hash: T::Hash) {
ensure_signed(origin)?;
Expand Down
5 changes: 3 additions & 2 deletions frame/treasury/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,11 @@ impl Contains<u64> for TenToFourteen {
})
}
}
impl ContainsCountUpperBound for TenToFourteen {
fn count_upper_bound() -> usize {
impl ContainsLengthBound for TenToFourteen {
fn max_len() -> usize {
TEN_TO_FOURTEEN.with(|v| v.borrow().len())
}
fn min_len() -> usize { 0 }
}
parameter_types! {
pub const ProposalBond: Permill = Permill::from_percent(5);
Expand Down