From 075c724ce408866cab7b00c8f35c22333cb0d23a Mon Sep 17 00:00:00 2001 From: Amiya Behera Date: Fri, 11 Oct 2024 17:19:30 +0530 Subject: [PATCH] add_incentive_count benchmarking department-funding --- .../department-funding/src/benchmarking.rs | 326 ++++++++++++++++++ .../department-funding/src/weights.rs | 134 +++++-- 2 files changed, 436 insertions(+), 24 deletions(-) diff --git a/custom-pallets/department-funding/src/benchmarking.rs b/custom-pallets/department-funding/src/benchmarking.rs index 44d6495..c53f312 100644 --- a/custom-pallets/department-funding/src/benchmarking.rs +++ b/custom-pallets/department-funding/src/benchmarking.rs @@ -17,6 +17,154 @@ use frame_support::{assert_noop, assert_ok}; mod benchmarks { use super::*; + fn full_schelling_game( + department_id: u64, + start_block_number: u64, + accounts: Vec, + ) { + let start_block_number = DepartmentFunding::::u64_to_block_saturated(start_block_number); + let tipping_name = TippingName::SmallTipper; + let funding_needed = DepartmentFunding::::u64_to_balance_saturated(10_000u64); + let content: Content = Content::IPFS( + "bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhvqy" + .as_bytes() + .to_vec(), + ); + + assert_ok!(DepartmentFunding::::create_department_required_fund( + RawOrigin::Signed(accounts[0].clone()).into(), + department_id, + content.clone(), + tipping_name, + funding_needed, + )); + + >::set_block_number(start_block_number); + assert_ok!(DepartmentFunding::::apply_staking_period( + RawOrigin::Signed(accounts[0].clone()).into(), + department_id + )); + + let phase_data = DepartmentFunding::::get_phase_data(); + + for j in 4..30 { + let stake = DepartmentFunding::::u64_to_balance_saturated(j * 100); + assert_ok!(DepartmentFunding::::apply_jurors( + RawOrigin::Signed(accounts[(j) as usize].clone()).into(), + department_id, + stake + )); + } + >::set_block_number(start_block_number + phase_data.staking_length); + + assert_ok!(DepartmentFunding::::pass_period( + RawOrigin::Signed(accounts[0].clone()).into(), + department_id + )); + assert_ok!(DepartmentFunding::::draw_jurors( + RawOrigin::Signed(accounts[0].clone()).into(), + department_id, + 5 + )); + assert_ok!(DepartmentFunding::::pass_period( + RawOrigin::Signed(accounts[0].clone()).into(), + department_id + )); + + let hash = sp_io::hashing::keccak_256("1salt2".as_bytes()); + assert_ok!(DepartmentFunding::::commit_vote( + RawOrigin::Signed(accounts[4].clone()).into(), + department_id, + hash + )); + + let hash = sp_io::hashing::keccak_256("1salt3".as_bytes()); + assert_ok!(DepartmentFunding::::commit_vote( + RawOrigin::Signed(accounts[7].clone()).into(), + department_id, + hash + )); + + let hash = sp_io::hashing::keccak_256("1salt4".as_bytes()); + assert_ok!(DepartmentFunding::::commit_vote( + RawOrigin::Signed(accounts[13].clone()).into(), + department_id, + hash + )); + + let hash = sp_io::hashing::keccak_256("1salt5".as_bytes()); + assert_ok!(DepartmentFunding::::commit_vote( + RawOrigin::Signed(accounts[14].clone()).into(), + department_id, + hash + )); + + let hash = sp_io::hashing::keccak_256("0salt6".as_bytes()); + assert_ok!(DepartmentFunding::::commit_vote( + RawOrigin::Signed(accounts[15].clone()).into(), + department_id, + hash + )); + + >::set_block_number( + phase_data.evidence_length + + start_block_number + + phase_data.staking_length + + phase_data.commit_length, + ); + + assert_ok!(DepartmentFunding::::pass_period( + RawOrigin::Signed(accounts[0].clone()).into(), + department_id + )); + + assert_ok!(DepartmentFunding::::reveal_vote( + RawOrigin::Signed(accounts[4].clone()).into(), + department_id, + 1, + "salt2".as_bytes().to_vec() + )); + + assert_ok!(DepartmentFunding::::reveal_vote( + RawOrigin::Signed(accounts[7].clone()).into(), + department_id, + 1, + "salt3".as_bytes().to_vec() + )); + + assert_ok!(DepartmentFunding::::reveal_vote( + RawOrigin::Signed(accounts[14].clone()).into(), + department_id, + 1, + "salt5".as_bytes().to_vec() + )); + + assert_ok!(DepartmentFunding::::reveal_vote( + RawOrigin::Signed(accounts[15].clone()).into(), + department_id, + 0, + "salt6".as_bytes().to_vec() + )); + + >::set_block_number( + phase_data.evidence_length + + start_block_number + + phase_data.staking_length + + phase_data.commit_length + + phase_data.vote_length, + ); + + assert_ok!(DepartmentFunding::::pass_period( + RawOrigin::Signed(accounts[0].clone()).into(), + department_id + )); + + // assert_ok!(ProjectTips::::add_incentive_count( + // RawOrigin::Signed(accounts[14].clone()).into(), + // project_id + // )); + } + #[benchmark] fn create_department_required_fund() { let department_id = 1; @@ -427,5 +575,183 @@ mod benchmarks { commit_vote(RawOrigin::Signed(accounts[4].clone()), department_id, hash); } + #[benchmark] + fn reveal_vote() { + let department_id = 1; + let tipping_name = TippingName::SmallTipper; + + let account1 = account::("account1", 1, SEED); + + let balance = DepartmentFunding::::u64_to_balance_saturated(100000000000000); + + let _ = ::Currency::deposit_creating(&account1, balance); + + let funding_needed = DepartmentFunding::::u64_to_balance_saturated(10_000u64); + // Dispatch a signed extrinsic. + let department_account_id = 5; + let content_department: Content = Content::IPFS( + "bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhvqy" + .as_bytes() + .to_vec(), + ); + + assert_ok!(>::create_department( + RawOrigin::Signed(account1.clone()).into(), + content_department + )); + + let content: Content = Content::IPFS( + "bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhvqy" + .as_bytes() + .to_vec(), + ); + + assert_ok!(DepartmentFunding::::create_department_required_fund( + RawOrigin::Signed(account1.clone()).into(), + department_id, + content.clone(), + tipping_name, + funding_needed, + )); + + let start_block_number = DepartmentFunding::::u64_to_block_saturated(50); + + >::set_block_number(start_block_number); + assert_ok!(DepartmentFunding::::apply_staking_period( + RawOrigin::Signed(account1.clone()).into(), + department_id + )); + + let account2 = account::("stake-account", 2, SEED); + + let balance = DepartmentFunding::::u64_to_balance_saturated(100000000000000); + + let _ = ::Currency::deposit_creating(&account2, balance); + + let stake = DepartmentFunding::::u64_to_balance_saturated(100); + + let mut accounts = vec![]; + + for j in 4..50 { + let account_number = account::("apply-juror-account", j, SEED); + let _ = ::Currency::deposit_creating(&account_number, balance); + accounts.push(account_number); + } + + for j in 4..30 { + let stake = DepartmentFunding::::u64_to_balance_saturated(j * 100); + assert_ok!(DepartmentFunding::::apply_jurors( + RawOrigin::Signed(accounts[(j) as usize].clone()).into(), + department_id.clone(), + stake + )); + } + + let phase_data = DepartmentFunding::::get_phase_data(); + + >::set_block_number(start_block_number + phase_data.staking_length); + + assert_ok!(DepartmentFunding::::pass_period( + RawOrigin::Signed(accounts[0].clone()).into(), + department_id + )); + + assert_ok!(DepartmentFunding::::draw_jurors( + RawOrigin::Signed(accounts[1].clone()).into(), + department_id, + 5 + )); + + assert_ok!(DepartmentFunding::::pass_period( + RawOrigin::Signed(accounts[0].clone()).into(), + department_id + )); + + let hash = sp_io::hashing::keccak_256("1salt2".as_bytes()); + + assert_ok!(DepartmentFunding::::commit_vote( + RawOrigin::Signed(accounts[4].clone()).into(), + department_id, + hash + )); + + let hash = sp_io::hashing::keccak_256("1salt3".as_bytes()); + assert_ok!(DepartmentFunding::::commit_vote( + RawOrigin::Signed(accounts[7].clone()).into(), + department_id, + hash + )); + + let hash = sp_io::hashing::keccak_256("1salt4".as_bytes()); + assert_ok!(DepartmentFunding::::commit_vote( + RawOrigin::Signed(accounts[13].clone()).into(), + department_id, + hash + )); + + let hash = sp_io::hashing::keccak_256("1salt5".as_bytes()); + assert_ok!(DepartmentFunding::::commit_vote( + RawOrigin::Signed(accounts[14].clone()).into(), + department_id, + hash + )); + + let hash = sp_io::hashing::keccak_256("0salt6".as_bytes()); + assert_ok!(DepartmentFunding::::commit_vote( + RawOrigin::Signed(accounts[15].clone()).into(), + department_id, + hash + )); + + >::set_block_number( + phase_data.evidence_length + + start_block_number + + phase_data.staking_length + + phase_data.commit_length, + ); + + assert_ok!(DepartmentFunding::::pass_period( + RawOrigin::Signed(accounts[0].clone()).into(), + department_id + )); + + #[extrinsic_call] + reveal_vote( + RawOrigin::Signed(accounts[4].clone()), + department_id, + 1, + "salt2".as_bytes().to_vec(), + ) + } + + #[benchmark] + fn add_incentive_count() { + let mut accounts = vec![]; + let balance = DepartmentFunding::::u64_to_balance_saturated(100000000000000); + + for j in 4..50 { + let account_number = account::("apply-juror-account", j, SEED); + let _ = ::Currency::deposit_creating(&account_number, balance); + accounts.push(account_number); + } + let block_number = DepartmentFunding::::u64_to_block_saturated(1000); + >::set_block_number(block_number); + let department_id = 1; + + let content_department: Content = Content::IPFS( + "bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhvqy" + .as_bytes() + .to_vec(), + ); + + assert_ok!(>::create_department( + RawOrigin::Signed(accounts[0].clone()).into(), + content_department + )); + full_schelling_game::(department_id, 1000, accounts.clone()); + #[extrinsic_call] + add_incentive_count(RawOrigin::Signed(accounts[14].clone()), department_id) + } + impl_benchmark_test_suite!(Template, crate::mock::new_test_ext(), crate::mock::Test); } diff --git a/custom-pallets/department-funding/src/weights.rs b/custom-pallets/department-funding/src/weights.rs index f6a5da5..35a8eb9 100644 --- a/custom-pallets/department-funding/src/weights.rs +++ b/custom-pallets/department-funding/src/weights.rs @@ -43,6 +43,8 @@ pub trait WeightInfo { fn pass_period() -> Weight; fn draw_jurors() -> Weight; fn commit_vote() -> Weight; + fn reveal_vote() -> Weight; + fn add_incentive_count() -> Weight; } /// Weights for `pallet_department_funding` using the Substrate node and recommended hardware. @@ -62,8 +64,8 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `345` // Estimated: `3810` - // Minimum execution time: 26_489_000 picoseconds. - Weight::from_parts(29_241_000, 3810) + // Minimum execution time: 25_303_000 picoseconds. + Weight::from_parts(26_362_000, 3810) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) } @@ -83,8 +85,8 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `306` // Estimated: `3771` - // Minimum execution time: 27_247_000 picoseconds. - Weight::from_parts(28_378_000, 3771) + // Minimum execution time: 24_374_000 picoseconds. + Weight::from_parts(25_155_000, 3771) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(5_u64)) } @@ -100,8 +102,8 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `443` // Estimated: `3908` - // Minimum execution time: 36_855_000 picoseconds. - Weight::from_parts(39_169_000, 3908) + // Minimum execution time: 35_775_000 picoseconds. + Weight::from_parts(37_003_000, 3908) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } @@ -115,8 +117,8 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `282` // Estimated: `3747` - // Minimum execution time: 20_613_000 picoseconds. - Weight::from_parts(21_229_000, 3747) + // Minimum execution time: 18_504_000 picoseconds. + Weight::from_parts(19_839_000, 3747) .saturating_add(T::DbWeight::get().reads(3_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } @@ -138,8 +140,8 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `2734` // Estimated: `6199` - // Minimum execution time: 163_900_000 picoseconds. - Weight::from_parts(170_151_000, 6199) + // Minimum execution time: 159_213_000 picoseconds. + Weight::from_parts(163_620_000, 6199) .saturating_add(T::DbWeight::get().reads(7_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) } @@ -155,11 +157,53 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `613` // Estimated: `4078` - // Minimum execution time: 22_024_000 picoseconds. - Weight::from_parts(22_581_000, 4078) + // Minimum execution time: 20_736_000 picoseconds. + Weight::from_parts(21_299_000, 4078) .saturating_add(T::DbWeight::get().reads(3_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } + /// Storage: `DepartmentFunding::ValidationBlock` (r:1 w:0) + /// Proof: `DepartmentFunding::ValidationBlock` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `SchellingGameShared::PeriodName` (r:1 w:0) + /// Proof: `SchellingGameShared::PeriodName` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `SchellingGameShared::VoteCommits` (r:1 w:1) + /// Proof: `SchellingGameShared::VoteCommits` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `SchellingGameShared::DecisionCount` (r:1 w:1) + /// Proof: `SchellingGameShared::DecisionCount` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn reveal_vote() -> Weight { + // Proof Size summary in bytes: + // Measured: `671` + // Estimated: `4136` + // Minimum execution time: 28_126_000 picoseconds. + Weight::from_parts(28_819_000, 4136) + .saturating_add(T::DbWeight::get().reads(4_u64)) + .saturating_add(T::DbWeight::get().writes(2_u64)) + } + /// Storage: `DepartmentFunding::ValidationBlock` (r:1 w:0) + /// Proof: `DepartmentFunding::ValidationBlock` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `SchellingGameShared::PeriodName` (r:1 w:0) + /// Proof: `SchellingGameShared::PeriodName` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `SchellingGameShared::DrawnJurors` (r:1 w:0) + /// Proof: `SchellingGameShared::DrawnJurors` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `SchellingGameShared::VoteCommits` (r:1 w:0) + /// Proof: `SchellingGameShared::VoteCommits` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `SchellingGameShared::DecisionCount` (r:1 w:0) + /// Proof: `SchellingGameShared::DecisionCount` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `SchellingGameShared::IncentiveAddedToCount` (r:1 w:1) + /// Proof: `SchellingGameShared::IncentiveAddedToCount` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `DepartmentFunding::IncentiveCount` (r:1 w:1) + /// Proof: `DepartmentFunding::IncentiveCount` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `Timestamp::Now` (r:1 w:0) + /// Proof: `Timestamp::Now` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `MaxEncodedLen`) + fn add_incentive_count() -> Weight { + // Proof Size summary in bytes: + // Measured: `983` + // Estimated: `4448` + // Minimum execution time: 37_056_000 picoseconds. + Weight::from_parts(37_903_000, 4448) + .saturating_add(T::DbWeight::get().reads(8_u64)) + .saturating_add(T::DbWeight::get().writes(2_u64)) + } } // For backwards compatibility and tests. @@ -178,8 +222,8 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `345` // Estimated: `3810` - // Minimum execution time: 26_489_000 picoseconds. - Weight::from_parts(29_241_000, 3810) + // Minimum execution time: 25_303_000 picoseconds. + Weight::from_parts(26_362_000, 3810) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().writes(3_u64)) } @@ -199,8 +243,8 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `306` // Estimated: `3771` - // Minimum execution time: 27_247_000 picoseconds. - Weight::from_parts(28_378_000, 3771) + // Minimum execution time: 24_374_000 picoseconds. + Weight::from_parts(25_155_000, 3771) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().writes(5_u64)) } @@ -216,8 +260,8 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `443` // Estimated: `3908` - // Minimum execution time: 36_855_000 picoseconds. - Weight::from_parts(39_169_000, 3908) + // Minimum execution time: 35_775_000 picoseconds. + Weight::from_parts(37_003_000, 3908) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().writes(2_u64)) } @@ -231,8 +275,8 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `282` // Estimated: `3747` - // Minimum execution time: 20_613_000 picoseconds. - Weight::from_parts(21_229_000, 3747) + // Minimum execution time: 18_504_000 picoseconds. + Weight::from_parts(19_839_000, 3747) .saturating_add(RocksDbWeight::get().reads(3_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } @@ -254,8 +298,8 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `2734` // Estimated: `6199` - // Minimum execution time: 163_900_000 picoseconds. - Weight::from_parts(170_151_000, 6199) + // Minimum execution time: 159_213_000 picoseconds. + Weight::from_parts(163_620_000, 6199) .saturating_add(RocksDbWeight::get().reads(7_u64)) .saturating_add(RocksDbWeight::get().writes(4_u64)) } @@ -271,9 +315,51 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `613` // Estimated: `4078` - // Minimum execution time: 22_024_000 picoseconds. - Weight::from_parts(22_581_000, 4078) + // Minimum execution time: 20_736_000 picoseconds. + Weight::from_parts(21_299_000, 4078) .saturating_add(RocksDbWeight::get().reads(3_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } + /// Storage: `DepartmentFunding::ValidationBlock` (r:1 w:0) + /// Proof: `DepartmentFunding::ValidationBlock` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `SchellingGameShared::PeriodName` (r:1 w:0) + /// Proof: `SchellingGameShared::PeriodName` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `SchellingGameShared::VoteCommits` (r:1 w:1) + /// Proof: `SchellingGameShared::VoteCommits` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `SchellingGameShared::DecisionCount` (r:1 w:1) + /// Proof: `SchellingGameShared::DecisionCount` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn reveal_vote() -> Weight { + // Proof Size summary in bytes: + // Measured: `671` + // Estimated: `4136` + // Minimum execution time: 28_126_000 picoseconds. + Weight::from_parts(28_819_000, 4136) + .saturating_add(RocksDbWeight::get().reads(4_u64)) + .saturating_add(RocksDbWeight::get().writes(2_u64)) + } + /// Storage: `DepartmentFunding::ValidationBlock` (r:1 w:0) + /// Proof: `DepartmentFunding::ValidationBlock` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `SchellingGameShared::PeriodName` (r:1 w:0) + /// Proof: `SchellingGameShared::PeriodName` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `SchellingGameShared::DrawnJurors` (r:1 w:0) + /// Proof: `SchellingGameShared::DrawnJurors` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `SchellingGameShared::VoteCommits` (r:1 w:0) + /// Proof: `SchellingGameShared::VoteCommits` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `SchellingGameShared::DecisionCount` (r:1 w:0) + /// Proof: `SchellingGameShared::DecisionCount` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `SchellingGameShared::IncentiveAddedToCount` (r:1 w:1) + /// Proof: `SchellingGameShared::IncentiveAddedToCount` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `DepartmentFunding::IncentiveCount` (r:1 w:1) + /// Proof: `DepartmentFunding::IncentiveCount` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `Timestamp::Now` (r:1 w:0) + /// Proof: `Timestamp::Now` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `MaxEncodedLen`) + fn add_incentive_count() -> Weight { + // Proof Size summary in bytes: + // Measured: `983` + // Estimated: `4448` + // Minimum execution time: 37_056_000 picoseconds. + Weight::from_parts(37_903_000, 4448) + .saturating_add(RocksDbWeight::get().reads(8_u64)) + .saturating_add(RocksDbWeight::get().writes(2_u64)) + } }