Weights to u64 + Balances Weights (#5446)

Co-Authored-By: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
This commit is contained in:
Shawn Tabrizi
2020-04-16 10:43:18 +02:00
committed by GitHub
parent 1c7525fd4b
commit 980b635c8d
82 changed files with 636 additions and 428 deletions
+14 -19
View File
@@ -74,14 +74,14 @@ impl<T> Parameter for T where T: Codec + EncodeLike + Clone + Eq + fmt::Debug {}
/// # #[macro_use]
/// # extern crate frame_support;
/// # use frame_support::dispatch;
/// # use frame_support::weights::SimpleDispatchInfo;
/// # use frame_support::weights::{SimpleDispatchInfo, MINIMUM_WEIGHT};
/// # use frame_system::{self as system, Trait, ensure_signed};
/// decl_module! {
/// pub struct Module<T: Trait> for enum Call where origin: T::Origin {
///
/// // Private functions are dispatchable, but not available to other
/// // FRAME pallets.
/// #[weight = SimpleDispatchInfo::default()]
/// #[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
/// fn my_function(origin, var: u64) -> dispatch::DispatchResult {
/// // Your implementation
/// Ok(())
@@ -89,7 +89,7 @@ impl<T> Parameter for T where T: Codec + EncodeLike + Clone + Eq + fmt::Debug {}
///
/// // Public functions are both dispatchable and available to other
/// // FRAME pallets.
/// #[weight = SimpleDispatchInfo::default()]
/// #[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
/// pub fn my_public_function(origin) -> dispatch::DispatchResult {
/// // Your implementation
/// Ok(())
@@ -117,17 +117,17 @@ impl<T> Parameter for T where T: Codec + EncodeLike + Clone + Eq + fmt::Debug {}
/// # #[macro_use]
/// # extern crate frame_support;
/// # use frame_support::dispatch;
/// # use frame_support::weights::SimpleDispatchInfo;
/// # use frame_support::weights::{SimpleDispatchInfo, MINIMUM_WEIGHT};
/// # use frame_system::{self as system, Trait, ensure_signed};
/// decl_module! {
/// pub struct Module<T: Trait> for enum Call where origin: T::Origin {
/// #[weight = SimpleDispatchInfo::default()]
/// #[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
/// fn my_long_function(origin) -> dispatch::DispatchResult {
/// // Your implementation
/// Ok(())
/// }
///
/// #[weight = SimpleDispatchInfo::default()]
/// #[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
/// fn my_short_function(origin) {
/// // Your implementation
/// }
@@ -182,11 +182,11 @@ impl<T> Parameter for T where T: Codec + EncodeLike + Clone + Eq + fmt::Debug {}
/// # #[macro_use]
/// # extern crate frame_support;
/// # use frame_support::dispatch;
/// # use frame_support::weights::SimpleDispatchInfo;
/// # use frame_support::weights::{SimpleDispatchInfo, MINIMUM_WEIGHT};
/// # use frame_system::{self as system, Trait, ensure_signed, ensure_root};
/// decl_module! {
/// pub struct Module<T: Trait> for enum Call where origin: T::Origin {
/// #[weight = SimpleDispatchInfo::default()]
/// #[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
/// fn my_privileged_function(origin) -> dispatch::DispatchResult {
/// ensure_root(origin)?;
/// // Your implementation
@@ -2086,7 +2086,7 @@ macro_rules! __check_reserved_fn_name {
#[allow(dead_code)]
mod tests {
use super::*;
use crate::weights::{DispatchInfo, DispatchClass};
use crate::weights::{MINIMUM_WEIGHT, DispatchInfo, DispatchClass};
use crate::traits::{
CallMetadata, GetCallMetadata, GetCallName, OnInitialize, OnFinalize, OnRuntimeUpgrade
};
@@ -2112,22 +2112,22 @@ mod tests {
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin, T::AccountId: From<u32> {
/// Hi, this is a comment.
#[weight = SimpleDispatchInfo::default()]
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
fn aux_0(_origin) -> DispatchResult { unreachable!() }
#[weight = SimpleDispatchInfo::default()]
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
fn aux_1(_origin, #[compact] _data: u32,) -> DispatchResult { unreachable!() }
#[weight = SimpleDispatchInfo::default()]
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
fn aux_2(_origin, _data: i32, _data2: String) -> DispatchResult { unreachable!() }
#[weight = SimpleDispatchInfo::FixedNormal(3)]
fn aux_3(_origin) -> DispatchResult { unreachable!() }
#[weight = SimpleDispatchInfo::default()]
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
fn aux_4(_origin, _data: i32) -> DispatchResult { unreachable!() }
#[weight = SimpleDispatchInfo::default()]
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
fn aux_5(_origin, _data: i32, #[compact] _data2: u32,) -> DispatchResult { unreachable!() }
#[weight = SimpleDispatchInfo::FixedOperational(5)]
@@ -2291,11 +2291,6 @@ mod tests {
Call::<TraitImpl>::operational().get_dispatch_info(),
DispatchInfo { weight: 5, class: DispatchClass::Operational, pays_fee: true },
);
// default weight.
assert_eq!(
Call::<TraitImpl>::aux_0().get_dispatch_info(),
DispatchInfo { weight: 10_000, class: DispatchClass::Normal, pays_fee: true },
);
// custom basic
assert_eq!(
Call::<TraitImpl>::aux_3().get_dispatch_info(),
+2 -2
View File
@@ -35,7 +35,7 @@ pub use frame_metadata::{ModuleErrorMetadata, ErrorMetadata, DecodeDifferent};
///
/// ```
/// # use frame_support::{decl_error, decl_module};
/// # use frame_support::weights::SimpleDispatchInfo;
/// # use frame_support::weights::{SimpleDispatchInfo, MINIMUM_WEIGHT};
/// decl_error! {
/// /// Errors that can occur in my module.
/// pub enum MyError for Module<T: Trait> {
@@ -55,7 +55,7 @@ pub use frame_metadata::{ModuleErrorMetadata, ErrorMetadata, DecodeDifferent};
/// pub struct Module<T: Trait> for enum Call where origin: T::Origin {
/// type Error = MyError<T>;
///
/// #[weight = SimpleDispatchInfo::default()]
/// #[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
/// fn do_something(origin) -> frame_support::dispatch::DispatchResult {
/// Err(MyError::<T>::YouAreNotCoolEnough.into())
/// }
+2 -2
View File
@@ -334,7 +334,7 @@ mod tests {
mod event_module {
use crate::dispatch::DispatchResult;
use crate::weights::SimpleDispatchInfo;
use crate::weights::{SimpleDispatchInfo, MINIMUM_WEIGHT};
pub trait Trait: super::system::Trait {
type Balance;
@@ -352,7 +352,7 @@ mod tests {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
type Error = Error<T>;
#[weight = SimpleDispatchInfo::default()]
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
fn aux_0(_origin) -> DispatchResult { unreachable!() }
}
}
+73 -11
View File
@@ -50,7 +50,13 @@ use crate::dispatch::{DispatchErrorWithPostInfo, DispatchError};
pub use sp_runtime::transaction_validity::TransactionPriority;
/// Numeric range of a transaction weight.
pub type Weight = u32;
///
/// FRAME assumes a weight of `1_000_000_000_000` equals 1 second of compute on a standard
/// machine: (TODO: DEFINE STANDARD MACHINE SPECIFICATIONS)
pub type Weight = u64;
/// The smallest total weight an extrinsic should have.
pub const MINIMUM_WEIGHT: Weight = 10_000_000;
/// Means of weighing some particular kind of data (`T`).
pub trait WeighData<T> {
@@ -106,6 +112,25 @@ impl Default for DispatchClass {
}
}
// Implement traits for raw Weight value
impl<T> WeighData<T> for Weight {
fn weigh_data(&self, _: T) -> Weight {
return *self
}
}
impl<T> ClassifyDispatch<T> for Weight {
fn classify_dispatch(&self, _: T) -> DispatchClass {
DispatchClass::default()
}
}
impl<T> PaysFee<T> for Weight {
fn pays_fee(&self, _: T) -> bool {
true
}
}
impl From<SimpleDispatchInfo> for DispatchClass {
fn from(tx: SimpleDispatchInfo) -> Self {
match tx {
@@ -281,13 +306,6 @@ impl<T> PaysFee<T> for SimpleDispatchInfo {
}
}
impl Default for SimpleDispatchInfo {
fn default() -> Self {
// Default weight of all transactions.
SimpleDispatchInfo::FixedNormal(10_000)
}
}
impl SimpleDispatchInfo {
/// An _additive zero_ variant of SimpleDispatchInfo.
pub fn zero() -> Self {
@@ -390,24 +408,56 @@ impl<Call: Encode, Extra: Encode> GetDispatchInfo for sp_runtime::testing::TestX
}
}
/// The weight of database operations that the runtime can invoke.
#[derive(Clone, Copy, Eq, PartialEq, Default, RuntimeDebug, Encode, Decode)]
pub struct RuntimeDbWeight {
pub read: Weight,
pub write: Weight,
}
impl RuntimeDbWeight {
pub fn reads(self, r: Weight) -> Weight {
self.read.saturating_mul(r)
}
pub fn writes(self, w: Weight) -> Weight {
self.write.saturating_mul(w)
}
pub fn reads_writes(self, r: Weight, w: Weight) -> Weight {
let read_weight = self.read.saturating_mul(r);
let write_weight = self.write.saturating_mul(w);
read_weight.saturating_add(write_weight)
}
}
#[cfg(test)]
#[allow(dead_code)]
mod tests {
use crate::decl_module;
use crate::{decl_module, parameter_types, traits::Get};
use super::*;
pub trait Trait {
type Origin;
type Balance;
type BlockNumber;
type DbWeight: Get<RuntimeDbWeight>;
}
pub struct TraitImpl {}
parameter_types! {
pub const DbWeight: RuntimeDbWeight = RuntimeDbWeight {
read: 100,
write: 1000,
};
}
impl Trait for TraitImpl {
type Origin = u32;
type BlockNumber = u32;
type Balance = u32;
type DbWeight = DbWeight;
}
decl_module! {
@@ -417,18 +467,30 @@ mod tests {
fn f0(_origin) { unimplemented!(); }
// weight = a x 10 + b
#[weight = FunctionOf(|args: (&u32, &u32)| args.0 * 10 + args.1, DispatchClass::Normal, true)]
#[weight = FunctionOf(|args: (&u32, &u32)| (args.0 * 10 + args.1) as Weight, DispatchClass::Normal, true)]
fn f11(_origin, _a: u32, _eb: u32) { unimplemented!(); }
#[weight = FunctionOf(|_: (&u32, &u32)| 0, DispatchClass::Operational, true)]
fn f12(_origin, _a: u32, _eb: u32) { unimplemented!(); }
#[weight = T::DbWeight::get().reads(3) + T::DbWeight::get().writes(2) + 10_000]
fn f2(_origin) { unimplemented!(); }
#[weight = T::DbWeight::get().reads_writes(6, 5) + 40_000]
fn f21(_origin) { unimplemented!(); }
}
}
#[test]
fn weights_are_correct() {
assert_eq!(Call::<TraitImpl>::f0().get_dispatch_info().weight, 1000);
assert_eq!(Call::<TraitImpl>::f11(10, 20).get_dispatch_info().weight, 120);
assert_eq!(Call::<TraitImpl>::f11(10, 20).get_dispatch_info().class, DispatchClass::Normal);
assert_eq!(Call::<TraitImpl>::f0().get_dispatch_info().weight, 1000);
assert_eq!(Call::<TraitImpl>::f12(10, 20).get_dispatch_info().weight, 0);
assert_eq!(Call::<TraitImpl>::f12(10, 20).get_dispatch_info().class, DispatchClass::Operational);
assert_eq!(Call::<TraitImpl>::f2().get_dispatch_info().weight, 12300);
assert_eq!(Call::<TraitImpl>::f21().get_dispatch_info().weight, 45600);
assert_eq!(Call::<TraitImpl>::f2().get_dispatch_info().class, DispatchClass::Normal);
}
}