From 5bc7bb1b2185988177ee5d47828643130eb8d0ba Mon Sep 17 00:00:00 2001 From: Siddharth Teli Date: Sun, 27 Aug 2023 12:23:32 +0530 Subject: [PATCH 1/3] Add: verify in farming pallet benchmark. --- pallets/farming/src/benchmarking.rs | 45 ++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/pallets/farming/src/benchmarking.rs b/pallets/farming/src/benchmarking.rs index 7dc34ae..e66c6eb 100644 --- a/pallets/farming/src/benchmarking.rs +++ b/pallets/farming/src/benchmarking.rs @@ -15,6 +15,11 @@ const USDT: u128 = 2; const W3G_DECIMALS: u128 = 1_000_000_000_000_000_000; const USDT_DECIMALS: u128 = 1_000_000; +// Helper to assert last event. +fn assert_last_event(generic_event: ::Event) { + frame_system::Pallet::::assert_last_event(generic_event.into()); +} + fn setup() -> DispatchResult { let alice: T::AccountId = account("alice", 0, 0); let bob: T::AccountId = account("bob", 0, 0); @@ -51,7 +56,10 @@ fn setup() -> DispatchResult { benchmarks! { set_admin { let alice: T::AccountId = account("alice", 0, 0); - }: _(RawOrigin::Root,alice) + }: _(RawOrigin::Root,alice.clone()) + verify { + assert_eq!(Admin::::get(), Some(alice)); + } create_pool { let alice: T::AccountId = account("alice", 0, 0); @@ -59,6 +67,10 @@ benchmarks! { setup::()?; assert_ok!(Farming::::set_admin(RawOrigin::Root.into(),alice.clone())); }: _(RawOrigin::Signed(alice),T::BlockNumber::from(1u32),T::BlockNumber::from(10u32),T::BlockNumber::from(10u32),W3G,USDT,10 * USDT_DECIMALS) + verify { + assert_eq!(NextPoolId::::get(), 1); + assert_last_event::(Event::::PoolCreated(0).into()); + } staking { let alice: T::AccountId = account("alice", 0, 0); @@ -75,7 +87,15 @@ benchmarks! { 10 * USDT_DECIMALS, )); System::::set_block_number(T::BlockNumber::from(10u32)); - }: _(RawOrigin::Signed(bob),0,10 * W3G_DECIMALS) + }: _(RawOrigin::Signed(bob.clone()),0,10 * W3G_DECIMALS) + verify { + assert_eq!(AccountPoolIdLocked::::get((bob.clone(), 0)), Some(StakingInfo { + staking_balance: 10 * W3G_DECIMALS, + is_claimed: false, + })); + assert_eq!(Pools::::get(0).unwrap().total_locked, 10 * W3G_DECIMALS); + assert_last_event::(Event::::Staking(bob, 0, 10 * W3G_DECIMALS).into()); + } claim { let alice: T::AccountId = account("alice", 0, 0); @@ -98,7 +118,20 @@ benchmarks! { 10 * W3G_DECIMALS, )); System::::set_block_number(T::BlockNumber::from(30u32)); - }: _(RawOrigin::Signed(bob),0) + }: _(RawOrigin::Signed(bob.clone()),0) + verify { + assert_eq!(AccountPoolIdLocked::::get((bob.clone(), 0)), Some(StakingInfo { + staking_balance: 10 * W3G_DECIMALS, + is_claimed: true, + })); + assert_eq!(Pools::::get(0).unwrap().total_locked, 10 * W3G_DECIMALS); + assert_last_event::(Event::::Claim( + bob, + 0, + 10 * W3G_DECIMALS, + 10 * USDT_DECIMALS, + ).into()); + } force_claim { let alice: T::AccountId = account("alice", 0, 0); @@ -120,7 +153,11 @@ benchmarks! { 0, 10 * W3G_DECIMALS, )); - }: _(RawOrigin::Signed(alice),bob,0) + }: _(RawOrigin::Signed(alice.clone()),bob,0) + verify { + assert_eq!(AccountPoolIdLocked::::get((alice, 0)), None); + assert_eq!(Pools::::get(0).unwrap().total_locked, 0); + } impl_benchmark_test_suite!(Farming, crate::mock::new_test_ext(), crate::mock::Test); } From 671adcb463cab69368c2605b9d4a2a3fedb28031 Mon Sep 17 00:00:00 2001 From: Siddharth Teli Date: Sun, 27 Aug 2023 14:49:37 +0530 Subject: [PATCH 2/3] Add: verify in launchpad pallet benchmark. --- pallets/launchpad/src/benchmarking.rs | 47 ++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/pallets/launchpad/src/benchmarking.rs b/pallets/launchpad/src/benchmarking.rs index 7f85f49..7aadd16 100644 --- a/pallets/launchpad/src/benchmarking.rs +++ b/pallets/launchpad/src/benchmarking.rs @@ -15,6 +15,11 @@ const USDT: u128 = 2; const W3G_DECIMALS: u128 = 1_000_000_000_000_000_000; const USDT_DECIMALS: u128 = 1_000_000; +// Helper to assert last event. +fn assert_last_event(generic_event: ::Event) { + frame_system::Pallet::::assert_last_event(generic_event.into()); +} + fn setup() -> DispatchResult { let alice: T::AccountId = account("alice", 0, 0); let bob: T::AccountId = account("bob", 0, 0); @@ -53,7 +58,11 @@ benchmarks! { let alice: T::AccountId = account("alice", 0, 0); setup::()?; }: _(RawOrigin::Signed(alice),T::BlockNumber::from(10u32),T::BlockNumber::from(10u32),W3G,USDT,10 * W3G_DECIMALS,1 * USDT_DECIMALS) - + verify { + assert_eq!(NextPoolId::::get(), 1); + assert!(Pools::::get(0).is_some()); + assert_last_event::(Event::::PoolCreated(0).into()); + } buy_token { let alice: T::AccountId = account("alice", 0, 0); let bob: T::AccountId = account("bob", 0, 0); @@ -68,7 +77,15 @@ benchmarks! { 1 * USDT_DECIMALS, )); System::::set_block_number(T::BlockNumber::from(10u32)); - }: _(RawOrigin::Signed(bob),0,2) + }: _(RawOrigin::Signed(bob.clone()), 0, 2) + verify { + assert_eq!(AccountPoolIdLocked::::get((bob.clone(), 0)), Some(ClaimInfo { + balance: 2 * W3G_DECIMALS, + is_claimed: false, + })); + assert_eq!(Pools::::get(0).unwrap().raise_amount, 8 * W3G_DECIMALS); + assert_last_event::(Event::::BuyToken(bob, 0, 2 * W3G_DECIMALS).into()); + } claim { let alice: T::AccountId = account("alice", 0, 0); @@ -90,7 +107,19 @@ benchmarks! { 2 )); System::::set_block_number(T::BlockNumber::from(21u32)); - }: _(RawOrigin::Signed(bob),0) + }: _(RawOrigin::Signed(bob.clone()),0) + verify { + assert_eq!(AccountPoolIdLocked::::get((bob.clone(), 0)), Some(ClaimInfo { + balance: 2 * W3G_DECIMALS, + is_claimed: true, + })); + assert_eq!(Pools::::get(0).unwrap().raise_amount, 8 * W3G_DECIMALS); + assert_last_event::(Event::::Claim( + bob, + 0, + 2 * W3G_DECIMALS, + ).into()); + } owner_claim { let alice: T::AccountId = account("alice", 0, 0); @@ -106,7 +135,17 @@ benchmarks! { 1 * USDT_DECIMALS, )); System::::set_block_number(T::BlockNumber::from(21u32)); - }: _(RawOrigin::Signed(alice),0) + }: _(RawOrigin::Signed(alice.clone()),0) + verify { + assert_eq!(AccountPoolIdLocked::::get((bob.clone(), 0)), None); + assert_eq!(Pools::::get(0).unwrap().raise_amount, 10 * W3G_DECIMALS); + assert_last_event::(Event::::OwnerClaim( + alice, + 0, + 10 * W3G_DECIMALS, + 0, + ).into()); + } impl_benchmark_test_suite!(Launchpad, crate::mock::new_test_ext(), crate::mock::Test); } From e6304a94b3b177b42d203a8d8a8479a9484391ed Mon Sep 17 00:00:00 2001 From: Siddharth Teli Date: Tue, 29 Aug 2023 12:52:23 +0530 Subject: [PATCH 3/3] Add: verify in marketplace pallet benchmark. --- pallets/marketplace/src/benchmarking.rs | 53 +++++++++++++++++++++---- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/pallets/marketplace/src/benchmarking.rs b/pallets/marketplace/src/benchmarking.rs index 4451697..9bfab20 100644 --- a/pallets/marketplace/src/benchmarking.rs +++ b/pallets/marketplace/src/benchmarking.rs @@ -17,6 +17,11 @@ const BLOCK: u32 = 1; type NonFungibleTokenIdOf = ::NonFungibleTokenId; type TokenIdOf = ::TokenId; +// Helper to assert last event. +fn assert_last_event(generic_event: ::Event) { + frame_system::Pallet::::assert_last_event(generic_event.into()); +} + pub fn lookup_of_account( who: T::AccountId, ) -> <::Lookup as StaticLookup>::Source { @@ -49,23 +54,38 @@ benchmarks! { set_admin { let alice: T::AccountId = account("alice", 0, 0); - }: _(RawOrigin::Root,alice) + }: _(RawOrigin::Root,alice.clone()) + verify { + assert_eq!(Admin::::get(), Some(alice)); + } set_service_fee_point { let alice: T::AccountId = account("alice", 0, 0); assert_ok!(Marketplace::::set_admin(RawOrigin::Root.into(),alice.clone())); }: _(RawOrigin::Signed(alice),10u8) + verify { + assert_eq!(Point::::get(), 10); + } create_order { let alice: T::AccountId = account("alice", 0, 0); create_non_fungible_token::(); - }: _(RawOrigin::Signed(alice),Asset::NonFungibleToken(1, 2),BalanceOf::::unique_saturated_from(100 * W3G),T::BlockNumber::from(100 * BLOCK)) + }: _(RawOrigin::Signed(alice.clone()),Asset::NonFungibleToken(1, 2),BalanceOf::::unique_saturated_from(100 * W3G),T::BlockNumber::from(100 * BLOCK)) + verify { + assert!(Orders::::contains_key(Asset::NonFungibleToken(1, 2))); + assert_last_event::(Event::::OrderCreated(alice.clone(), Asset::NonFungibleToken(1, 2), Order { creater: alice, price: BalanceOf::::unique_saturated_from(100 * W3G), start: T::BlockNumber::from(1_u32), duration: T::BlockNumber::from(100 * BLOCK) }).into()); + } cancel_order { let alice: T::AccountId = account("alice", 0, 0); create_non_fungible_token::(); assert_ok!(Marketplace::::create_order(RawOrigin::Signed(alice.clone()).into(),Asset::NonFungibleToken(1, 2),BalanceOf::::unique_saturated_from(100 * W3G),T::BlockNumber::from(100 * BLOCK))); - }: _(RawOrigin::Signed(alice),Asset::NonFungibleToken(1, 2)) + }: _(RawOrigin::Signed(alice.clone()),Asset::NonFungibleToken(1, 2)) + verify { + assert!(!Orders::::contains_key(Asset::NonFungibleToken(1, 2))); + assert!(!Bids::::contains_key(Asset::NonFungibleToken(1, 2))); + assert_last_event::(Event::::OrderCancelled(alice, Asset::NonFungibleToken(1, 2)).into()); + } execute_order { let alice: T::AccountId = account("alice", 0, 0); @@ -78,7 +98,13 @@ benchmarks! { 1000 * W3G, 0, )); - }: _(RawOrigin::Signed(bob),Asset::NonFungibleToken(1, 2)) + }: _(RawOrigin::Signed(bob.clone()),Asset::NonFungibleToken(1, 2)) + verify { + assert!(!Bids::::contains_key(Asset::NonFungibleToken(1, 2))); + assert!(!Orders::::contains_key(Asset::NonFungibleToken(1, 2))); + assert_last_event::(Event::::OrderExecuted(bob, Asset::NonFungibleToken(1, 2), Order { creater: alice, price: BalanceOf::::unique_saturated_from(100 * W3G), start: T::BlockNumber::from(0_u32), duration: T::BlockNumber::from(100 * BLOCK) }).into()); + } + place_bid { let alice: T::AccountId = account("alice", 0, 0); @@ -91,7 +117,11 @@ benchmarks! { 1000 * W3G, 0, )); - }: _(RawOrigin::Signed(bob),Asset::NonFungibleToken(1, 2),BalanceOf::::unique_saturated_from(100 * W3G),T::BlockNumber::from(100 * BLOCK)) + }: _(RawOrigin::Signed(bob.clone()),Asset::NonFungibleToken(1, 2),BalanceOf::::unique_saturated_from(100 * W3G),T::BlockNumber::from(100 * BLOCK)) + verify { + assert_eq!(Bids::::get(Asset::NonFungibleToken(1, 2)), Some(Order { creater: bob.clone(), price: BalanceOf::::unique_saturated_from(100 * W3G), start: T::BlockNumber::from(1_u32), duration: T::BlockNumber::from(100 * BLOCK) })); + assert_last_event::(Event::::BidCreated(bob.clone(), Asset::NonFungibleToken(1, 2), Order { creater: bob, price: BalanceOf::::unique_saturated_from(100 * W3G), start: T::BlockNumber::from(1_u32), duration: T::BlockNumber::from(100 * BLOCK) }).into()); + } cancel_bid { let alice: T::AccountId = account("alice", 0, 0); @@ -105,7 +135,11 @@ benchmarks! { 0, )); assert_ok!(Marketplace::::place_bid(RawOrigin::Signed(bob.clone()).into(),Asset::NonFungibleToken(1, 2),BalanceOf::::unique_saturated_from(100 * W3G),T::BlockNumber::from(100 * BLOCK))); - }: _(RawOrigin::Signed(bob),Asset::NonFungibleToken(1, 2)) + }: _(RawOrigin::Signed(bob.clone()),Asset::NonFungibleToken(1, 2)) + verify { + assert!(!Bids::::contains_key(Asset::NonFungibleToken(1, 2))); + assert_last_event::(Event::::BidCancelled(bob, Asset::NonFungibleToken(1, 2)).into()); + } accept_bid { let alice: T::AccountId = account("alice", 0, 0); @@ -119,7 +153,12 @@ benchmarks! { 0, )); assert_ok!(Marketplace::::place_bid(RawOrigin::Signed(bob.clone()).into(),Asset::NonFungibleToken(1, 2),BalanceOf::::unique_saturated_from(100 * W3G),T::BlockNumber::from(100 * BLOCK))); - }: _(RawOrigin::Signed(alice),Asset::NonFungibleToken(1, 2)) + }: _(RawOrigin::Signed(alice.clone()),Asset::NonFungibleToken(1, 2)) + verify { + assert!(!Bids::::contains_key(Asset::NonFungibleToken(1, 2))); + assert!(!Orders::::contains_key(Asset::NonFungibleToken(1, 2))); + assert_last_event::(Event::::BidAccepted(alice, Asset::NonFungibleToken(1, 2), Order { creater: bob, price: BalanceOf::::unique_saturated_from(100 * W3G), start: T::BlockNumber::from(0_u32), duration: T::BlockNumber::from(100 * BLOCK) }).into()); + } impl_benchmark_test_suite!(Marketplace, crate::mock::new_test_ext(), crate::mock::Test); }