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

Commit

Permalink
Revamp some of the weights (#4759)
Browse files Browse the repository at this point in the history
* Remove free transaction dos vectors.

* Bump spec version

* Indentation.

* Update frame/support/src/weights.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
  • Loading branch information
gavofyork and bkchr authored Jan 29, 2020
1 parent 89e079f commit 922b36c
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 27 deletions.
6 changes: 3 additions & 3 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_name: create_runtime_str!("substrate-node"),
authoring_version: 10,
// Per convention: if the runtime behavior changes, increment spec_version
// and set impl_version to equal spec_version. If only runtime
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 207,
impl_version: 208,
spec_version: 208,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
};

Expand Down
2 changes: 1 addition & 1 deletion frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ mod tests {
fn some_root_operation(origin) {
let _ = frame_system::ensure_root(origin);
}
#[weight = SimpleDispatchInfo::FreeNormal]
#[weight = SimpleDispatchInfo::InsecureFreeNormal]
fn some_unsigned_message(origin) {
let _ = frame_system::ensure_none(origin);
}
Expand Down
2 changes: 1 addition & 1 deletion frame/identity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ decl_module! {
/// - `S + 2` storage mutations.
/// - One event.
/// # </weight>
#[weight = SimpleDispatchInfo::FreeOperational]
#[weight = SimpleDispatchInfo::FixedNormal(100_000)]
fn kill_identity(origin, target: <T::Lookup as StaticLookup>::Source) {
T::ForceOrigin::try_origin(origin)
.map(|_| ())
Expand Down
4 changes: 2 additions & 2 deletions frame/nicks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ decl_module! {
/// - One storage read/write.
/// - One event.
/// # </weight>
#[weight = SimpleDispatchInfo::FreeOperational]
#[weight = SimpleDispatchInfo::FixedNormal(70_000)]
fn kill_name(origin, target: <T::Lookup as StaticLookup>::Source) {
T::ForceOrigin::try_origin(origin)
.map(|_| ())
Expand Down Expand Up @@ -222,7 +222,7 @@ decl_module! {
/// - One storage read/write.
/// - One event.
/// # </weight>
#[weight = SimpleDispatchInfo::FreeOperational]
#[weight = SimpleDispatchInfo::FixedNormal(70_000)]
fn force_name(origin, target: <T::Lookup as StaticLookup>::Source, name: Vec<u8>) {
T::ForceOrigin::try_origin(origin)
.map(|_| ())
Expand Down
14 changes: 7 additions & 7 deletions frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ decl_module! {
}

/// The ideal number of validators.
#[weight = SimpleDispatchInfo::FreeOperational]
#[weight = SimpleDispatchInfo::FixedNormal(5_000)]
fn set_validator_count(origin, #[compact] new: u32) {
ensure_root(origin)?;
ValidatorCount::put(new);
Expand All @@ -1163,7 +1163,7 @@ decl_module! {
/// # <weight>
/// - No arguments.
/// # </weight>
#[weight = SimpleDispatchInfo::FreeOperational]
#[weight = SimpleDispatchInfo::FixedNormal(5_000)]
fn force_no_eras(origin) {
ensure_root(origin)?;
ForceEra::put(Forcing::ForceNone);
Expand All @@ -1175,21 +1175,21 @@ decl_module! {
/// # <weight>
/// - No arguments.
/// # </weight>
#[weight = SimpleDispatchInfo::FreeOperational]
#[weight = SimpleDispatchInfo::FixedNormal(5_000)]
fn force_new_era(origin) {
ensure_root(origin)?;
ForceEra::put(Forcing::ForceNew);
}

/// Set the validators who cannot be slashed (if any).
#[weight = SimpleDispatchInfo::FreeOperational]
#[weight = SimpleDispatchInfo::FixedNormal(5_000)]
fn set_invulnerables(origin, validators: Vec<T::AccountId>) {
ensure_root(origin)?;
<Invulnerables<T>>::put(validators);
}

/// Force a current staker to become completely unstaked, immediately.
#[weight = SimpleDispatchInfo::FreeOperational]
#[weight = SimpleDispatchInfo::FixedNormal(10_000)]
fn force_unstake(origin, stash: T::AccountId) {
ensure_root(origin)?;

Expand All @@ -1204,7 +1204,7 @@ decl_module! {
/// # <weight>
/// - One storage write
/// # </weight>
#[weight = SimpleDispatchInfo::FreeOperational]
#[weight = SimpleDispatchInfo::FixedNormal(5_000)]
fn force_new_era_always(origin) {
ensure_root(origin)?;
ForceEra::put(Forcing::ForceAlways);
Expand All @@ -1217,7 +1217,7 @@ decl_module! {
/// # <weight>
/// - One storage write.
/// # </weight>
#[weight = SimpleDispatchInfo::FreeOperational]
#[weight = SimpleDispatchInfo::FixedNormal(1_000_000)]
fn cancel_deferred_slash(origin, era: EraIndex, slash_indices: Vec<u32>) {
T::SlashCancelOrigin::try_origin(origin)
.map(|_| ())
Expand Down
2 changes: 1 addition & 1 deletion frame/sudo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ decl_module! {
/// - One DB write (event).
/// - Unknown weight of derivative `proposal` execution.
/// # </weight>
#[weight = SimpleDispatchInfo::FreeOperational]
#[weight = SimpleDispatchInfo::FixedNormal(50_000)]
fn sudo(origin, proposal: Box<T::Proposal>) {
// This is a public call, so we ensure that the origin is some signed account.
let sender = ensure_signed(origin)?;
Expand Down
15 changes: 5 additions & 10 deletions frame/support/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,10 @@ impl From<SimpleDispatchInfo> for DispatchClass {
match tx {
SimpleDispatchInfo::FixedOperational(_) => DispatchClass::Operational,
SimpleDispatchInfo::MaxOperational => DispatchClass::Operational,
SimpleDispatchInfo::FreeOperational => DispatchClass::Operational,

SimpleDispatchInfo::FixedNormal(_) => DispatchClass::Normal,
SimpleDispatchInfo::MaxNormal => DispatchClass::Normal,
SimpleDispatchInfo::FreeNormal => DispatchClass::Normal,
SimpleDispatchInfo::InsecureFreeNormal => DispatchClass::Normal,
}
}
}
Expand Down Expand Up @@ -178,26 +177,23 @@ pub enum SimpleDispatchInfo {
FixedNormal(Weight),
/// A normal dispatch with the maximum weight.
MaxNormal,
/// A normal dispatch with no weight.
FreeNormal,
/// A normal dispatch with no weight. Base and bytes fees still need to be paid.
InsecureFreeNormal,
/// An operational dispatch with fixed weight.
FixedOperational(Weight),
/// An operational dispatch with the maximum weight.
MaxOperational,
/// An operational dispatch with no weight.
FreeOperational,
}

impl<T> WeighData<T> for SimpleDispatchInfo {
fn weigh_data(&self, _: T) -> Weight {
match self {
SimpleDispatchInfo::FixedNormal(w) => *w,
SimpleDispatchInfo::MaxNormal => Bounded::max_value(),
SimpleDispatchInfo::FreeNormal => Bounded::min_value(),
SimpleDispatchInfo::InsecureFreeNormal => Bounded::min_value(),

SimpleDispatchInfo::FixedOperational(w) => *w,
SimpleDispatchInfo::MaxOperational => Bounded::max_value(),
SimpleDispatchInfo::FreeOperational => Bounded::min_value(),
}
}
}
Expand All @@ -213,11 +209,10 @@ impl<T> PaysFee<T> for SimpleDispatchInfo {
match self {
SimpleDispatchInfo::FixedNormal(_) => true,
SimpleDispatchInfo::MaxNormal => true,
SimpleDispatchInfo::FreeNormal => true,
SimpleDispatchInfo::InsecureFreeNormal => true,

SimpleDispatchInfo::FixedOperational(_) => true,
SimpleDispatchInfo::MaxOperational => true,
SimpleDispatchInfo::FreeOperational => false,
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions frame/transaction-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ mod tests {

let len = 100;

// like a FreeOperational
// This is a completely free (and thus wholly insecure/DoS-ridden) transaction.
let operational_transaction = DispatchInfo {
weight: 0,
class: DispatchClass::Operational,
Expand All @@ -489,7 +489,7 @@ mod tests {
.is_ok()
);

// like a FreeNormal
// like a InsecureFreeNormal
let free_transaction = DispatchInfo {
weight: 0,
class: DispatchClass::Normal,
Expand Down

0 comments on commit 922b36c

Please sign in to comment.