Browse Source

runtime: bounty: Add benchmarks.

Shamil Gadelshin 4 years ago
parent
commit
b33fa82ae3

+ 5 - 3
Cargo.lock

@@ -2275,7 +2275,7 @@ dependencies = [
 
 [[package]]
 name = "joystream-node"
-version = "4.0.2"
+version = "4.1.0"
 dependencies = [
  "frame-benchmarking",
  "frame-benchmarking-cli",
@@ -2336,7 +2336,7 @@ dependencies = [
 
 [[package]]
 name = "joystream-node-runtime"
-version = "8.0.1"
+version = "8.1.0"
 dependencies = [
  "frame-benchmarking",
  "frame-executive",
@@ -2351,6 +2351,7 @@ dependencies = [
  "pallet-authorship",
  "pallet-babe",
  "pallet-balances",
+ "pallet-bounty",
  "pallet-common",
  "pallet-constitution",
  "pallet-content-directory",
@@ -3736,8 +3737,9 @@ dependencies = [
 
 [[package]]
 name = "pallet-bounty"
-version = "1.0.0"
+version = "1.0.1"
 dependencies = [
+ "frame-benchmarking",
  "frame-support",
  "frame-system",
  "pallet-balances",

+ 1 - 1
node/Cargo.toml

@@ -3,7 +3,7 @@ authors = ['Joystream contributors']
 build = 'build.rs'
 edition = '2018'
 name = 'joystream-node'
-version = '4.0.2'
+version = '4.1.0'
 default-run = "joystream-node"
 
 [[bin]]

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

@@ -1,6 +1,6 @@
 [package]
 name = 'pallet-bounty'
-version = '1.0.0'
+version = '1.0.1'
 authors = ['Joystream contributors']
 edition = '2018'
 
@@ -14,14 +14,21 @@ frame-system = { package = 'frame-system', default-features = false, git = 'http
 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'}
 
+# 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'}
 sp-core = { package = 'sp-core', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a200cdb93c6af5763b9c7bf313fa708764ac88ca'}
 sp-runtime = { package = 'sp-runtime', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a200cdb93c6af5763b9c7bf313fa708764ac88ca'}
 
-
 [features]
 default = ['std']
+runtime-benchmarks = [
+	"frame-benchmarking",
+	"sp-runtime/runtime-benchmarks"
+]
 std = [
 	'serde',
 	'codec/std',

+ 59 - 0
runtime-modules/bounty/src/benchmarking.rs

@@ -0,0 +1,59 @@
+#![cfg(feature = "runtime-benchmarks")]
+
+use frame_benchmarking::benchmarks;
+use frame_system::Module as System;
+use frame_system::{EventRecord, RawOrigin};
+use sp_std::boxed::Box;
+use sp_std::vec;
+use sp_std::vec::Vec;
+
+use crate::{Call, Event, Module, Trait, BountyCreationParameters, Bounty};
+
+fn assert_last_event<T: Trait>(generic_event: <T as Trait>::Event) {
+    let events = System::<T>::events();
+    let system_event: <T as frame_system::Trait>::Event = generic_event.into();
+    // compare to the last event record
+    let EventRecord { event, .. } = &events[events.len() - 1];
+    assert_eq!(event, &system_event);
+}
+
+const MAX_BYTES: u32 = 50000;
+
+benchmarks! {
+    _{ }
+
+    create_bounty{
+        let i in 1 .. MAX_BYTES;
+        let metadata = vec![0u8].repeat(i as usize);
+
+        let params = BountyCreationParameters::<T>{
+            metadata,
+            ..Default::default()
+        };
+
+    }: _ (RawOrigin::Root, params.clone())
+    verify {
+        let bounty = Bounty::<T>{
+            creation_params: params,
+        };
+
+        let bounty_id: T::BountyId = 1u32.into();
+
+        assert_eq!(Module::<T>::bounties(bounty_id), bounty);
+        assert_last_event::<T>(Event::<T>::BountyCreated(bounty_id).into());
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use crate::tests::mocks::{build_test_externalities, Test};
+    use frame_support::assert_ok;
+
+    #[test]
+    fn create_bounty() {
+        build_test_externalities().execute_with(|| {
+            assert_ok!(test_benchmark_create_bounty::<Test>());
+        });
+    }
+}

+ 6 - 0
runtime-modules/bounty/src/lib.rs

@@ -12,6 +12,12 @@
 #[cfg(test)]
 pub(crate) mod tests;
 
+#[cfg(feature = "runtime-benchmarks")]
+mod benchmarking;
+
+// TODO: add max entries limit
+// TODO: benchmark all bounty creation parameters
+
 use frame_support::{decl_error, decl_event, decl_module, decl_storage, Parameter};
 use frame_system::ensure_root;
 use sp_std::vec::Vec;

+ 7 - 4
runtime/Cargo.toml

@@ -4,7 +4,7 @@ edition = '2018'
 name = 'joystream-node-runtime'
 # Follow convention: https://github.com/Joystream/substrate-runtime-joystream/issues/1
 # {Authoring}.{Spec}.{Impl} of the RuntimeVersion
-version = '8.0.1'
+version = '8.1.0'
 
 [dependencies]
 # Third-party dependencies
@@ -76,8 +76,9 @@ proposals-engine = { package = 'pallet-proposals-engine', default-features = fal
 proposals-discussion = { package = 'pallet-proposals-discussion', default-features = false, path = '../runtime-modules/proposals/discussion'}
 proposals-codex = { package = 'pallet-proposals-codex', default-features = false, path = '../runtime-modules/proposals/codex'}
 content-directory = { package = 'pallet-content-directory', default-features = false, path = '../runtime-modules/content-directory' }
-pallet_constitution = { package = 'pallet-constitution', default-features = false, path = '../runtime-modules/constitution' }
+pallet-constitution = { package = 'pallet-constitution', default-features = false, path = '../runtime-modules/constitution' }
 staking-handler = { package = 'pallet-staking-handler', default-features = false, path = '../runtime-modules/staking-handler'}
+bounty = { package = 'pallet-bounty', default-features = false, path = '../runtime-modules/bounty'}
 
 [dev-dependencies]
 sp-io = { package = 'sp-io', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a200cdb93c6af5763b9c7bf313fa708764ac88ca'}
@@ -149,8 +150,9 @@ std = [
     'proposals-discussion/std',
     'proposals-codex/std',
     'content-directory/std',
-    'pallet_constitution/std',
+    'pallet-constitution/std',
     'staking-handler/std',
+    'bounty/std',
 ]
 runtime-benchmarks = [
     "frame-system/runtime-benchmarks",
@@ -167,12 +169,13 @@ runtime-benchmarks = [
     "proposals-discussion/runtime-benchmarks",
     "proposals-engine/runtime-benchmarks",
     "proposals-codex/runtime-benchmarks",
-    "pallet_constitution/runtime-benchmarks",
+    "pallet-constitution/runtime-benchmarks",
     "working-group/runtime-benchmarks",
     "forum/runtime-benchmarks",
     "membership/runtime-benchmarks",
     "council/runtime-benchmarks",
     "referendum/runtime-benchmarks",
+    "bounty/runtime-benchmarks",
     "hex-literal",
 ]
 

+ 10 - 2
runtime/src/lib.rs

@@ -96,8 +96,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
     spec_name: create_runtime_str!("joystream-node"),
     impl_name: create_runtime_str!("joystream-node"),
     authoring_version: 8,
-    spec_version: 0,
-    impl_version: 1,
+    spec_version: 1,
+    impl_version: 0,
     apis: crate::runtime_api::EXPORTED_RUNTIME_API_VERSIONS,
     transaction_version: 1,
 };
@@ -874,6 +874,13 @@ impl pallet_constitution::Trait for Runtime {
     type WeightInfo = weights::pallet_constitution::WeightInfo;
 }
 
+impl bounty::Trait for Runtime {
+    type Event = Event;
+    type BountyId = u64;
+    type MemberOriginValidator = Members;
+//    type WeightInfo = weights::pallet_constitution::WeightInfo;
+}
+
 /// Forum identifier for category
 pub type CategoryId = u64;
 
@@ -926,6 +933,7 @@ construct_runtime!(
         Forum: forum::{Module, Call, Storage, Event<T>, Config<T>},
         ContentDirectory: content_directory::{Module, Call, Storage, Event<T>, Config<T>},
         Constitution: pallet_constitution::{Module, Call, Storage, Event},
+        Bounty: bounty::{Module, Call, Storage, Event<T>},
         // --- Storage
         DataObjectTypeRegistry: data_object_type_registry::{Module, Call, Storage, Event<T>, Config<T>},
         DataDirectory: data_directory::{Module, Call, Storage, Event<T>},

+ 2 - 0
runtime/src/runtime_api.rs

@@ -294,6 +294,7 @@ impl_runtime_apis! {
             use crate::ImOnline;
             use crate::Council;
             use crate::Referendum;
+            use crate::Bounty;
 
 
             // Trying to add benchmarks directly to the Session Pallet caused cyclic dependency issues.
@@ -383,6 +384,7 @@ impl_runtime_apis! {
             add_benchmark!(params, batches, working_group, ContentDirectoryWorkingGroup);
             add_benchmark!(params, batches, referendum, Referendum);
             add_benchmark!(params, batches, council, Council);
+            add_benchmark!(params, batches, bounty, Bounty);
 
             if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) }
             Ok(batches)

+ 2 - 1
scripts/generate-weights.sh

@@ -53,4 +53,5 @@ benchmark working_group
 benchmark council
 benchmark referendum
 benchmark forum
-benchmark membership
+benchmark membership
+benchmark bounty