瀏覽代碼

runtime: bounty: Add weights.

Shamil Gadelshin 4 年之前
父節點
當前提交
ef0596c6f0

+ 2 - 1
runtime-modules/bounty/Cargo.toml

@@ -13,10 +13,10 @@ frame-support = { package = 'frame-support', default-features = false, git = 'ht
 frame-system = { package = 'frame-system', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a200cdb93c6af5763b9c7bf313fa708764ac88ca'}
 balances = { package = 'pallet-balances', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a200cdb93c6af5763b9c7bf313fa708764ac88ca'}
 common = { package = 'pallet-common', default-features = false, path = '../common'}
+sp-runtime = { package = 'sp-runtime', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a200cdb93c6af5763b9c7bf313fa708764ac88ca'}
 
 # Benchmarking
 frame-benchmarking = { package = 'frame-benchmarking', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a200cdb93c6af5763b9c7bf313fa708764ac88ca', optional = true}
-sp-runtime = { package = 'sp-runtime', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a200cdb93c6af5763b9c7bf313fa708764ac88ca'}
 
 [dev-dependencies]
 sp-io = { package = 'sp-io', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a200cdb93c6af5763b9c7bf313fa708764ac88ca'}
@@ -38,4 +38,5 @@ std = [
 	'frame-system/std',
 	'balances/std',
 	'common/std',
+	'sp-runtime/std',
 ]

+ 1 - 1
runtime-modules/bounty/src/benchmarking.rs

@@ -7,7 +7,7 @@ use sp_std::boxed::Box;
 use sp_std::vec;
 use sp_std::vec::Vec;
 
-use crate::{Call, Event, Module, Trait, BountyCreationParameters, Bounty};
+use crate::{Bounty, BountyCreationParameters, Call, Event, Module, Trait};
 
 fn assert_last_event<T: Trait>(generic_event: <T as Trait>::Event) {
     let events = System::<T>::events();

+ 15 - 1
runtime-modules/bounty/src/lib.rs

@@ -18,8 +18,18 @@ mod benchmarking;
 // TODO: add max entries limit
 // TODO: benchmark all bounty creation parameters
 
+/// pallet_bounty WeightInfo.
+/// Note: This was auto generated through the benchmark CLI using the `--weight-trait` flag
+pub trait WeightInfo {
+    fn create_bounty(i: u32) -> Weight;
+}
+
+type WeightInfoBounty<T> = <T as Trait>::WeightInfo;
+
+use frame_support::weights::Weight;
 use frame_support::{decl_error, decl_event, decl_module, decl_storage, Parameter};
 use frame_system::ensure_root;
+use sp_runtime::SaturatedConversion;
 use sp_std::vec::Vec;
 
 use common::origin::MemberOriginValidator;
@@ -40,6 +50,9 @@ pub trait Trait: frame_system::Trait + balances::Trait + common::Trait {
 
     /// Validates member ID and origin combination.
     type MemberOriginValidator: MemberOriginValidator<Self::Origin, MemberId<Self>, Self::AccountId>;
+
+    /// Weight information for extrinsics in this pallet.
+    type WeightInfo: WeightInfo;
 }
 
 /// Alias type for the BountyParameters.
@@ -178,7 +191,7 @@ decl_module! {
     pub struct Module<T: Trait> for enum Call where origin: T::Origin {
         fn deposit_event() = default;
 
-        #[weight = 10_000_000] // TODO: adjust weight
+        #[weight = WeightInfoBounty::<T>::create_bounty(params.metadata.len().saturated_into())]
         fn create_bounty(origin, params: BountyCreationParameters<T>) {
             Self::ensure_create_bounty_parameters_valid(&origin, &params)?;
 
@@ -203,6 +216,7 @@ decl_module! {
 }
 
 impl<T: Trait> Module<T> {
+    // Validates parameters for a bounty creation.
     fn ensure_create_bounty_parameters_valid(
         origin: &T::Origin,
         params: &BountyCreationParameters<T>,

+ 7 - 0
runtime-modules/bounty/src/tests/mocks.rs

@@ -69,6 +69,13 @@ impl Trait for Test {
     type Event = TestEvent;
     type BountyId = u64;
     type MemberOriginValidator = ();
+    type WeightInfo = ();
+}
+
+impl crate::WeightInfo for () {
+    fn create_bounty(_: u32) -> u64 {
+        0
+    }
 }
 
 impl common::Trait for Test {

+ 1 - 1
runtime/src/lib.rs

@@ -878,7 +878,7 @@ impl bounty::Trait for Runtime {
     type Event = Event;
     type BountyId = u64;
     type MemberOriginValidator = Members;
-//    type WeightInfo = weights::pallet_constitution::WeightInfo;
+    type WeightInfo = weights::bounty::WeightInfo;
 }
 
 /// Forum identifier for category

+ 16 - 0
runtime/src/weights/bounty.rs

@@ -0,0 +1,16 @@
+//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0
+
+#![allow(unused_parens)]
+#![allow(unused_imports)]
+
+use frame_support::weights::{constants::RocksDbWeight as DbWeight, Weight};
+
+pub struct WeightInfo;
+impl bounty::WeightInfo for WeightInfo {
+    fn create_bounty(i: u32) -> Weight {
+        (71_128_000 as Weight)
+            .saturating_add((125_000 as Weight).saturating_mul(i as Weight))
+            .saturating_add(DbWeight::get().reads(1 as Weight))
+            .saturating_add(DbWeight::get().writes(2 as Weight))
+    }
+}

+ 1 - 0
runtime/src/weights/mod.rs

@@ -24,6 +24,7 @@ pub mod pallet_timestamp;
 pub mod pallet_utility;
 
 // Joystream pallets
+pub mod bounty;
 pub mod council;
 pub mod forum;
 pub mod membership;