diff --git a/pallets/disputes/src/benchmarking.rs b/pallets/disputes/src/benchmarking.rs index 83f80b4a..84e8dd61 100644 --- a/pallets/disputes/src/benchmarking.rs +++ b/pallets/disputes/src/benchmarking.rs @@ -1,35 +1,52 @@ -//! Benchmarking setup for pallet-disputes #![cfg(feature = "runtime-benchmarks")] use super::*; -#[allow(unused)] -use crate::Pallet as Disputes; -use frame_benchmarking::v2::*; -use frame_system::RawOrigin; +use crate::Pallet as PalletDisputes; +use crate::{traits::DisputeRaiser}; +use frame_benchmarking::{v2::*}; +use frame_support::assert_ok; +use sp_runtime::SaturatedConversion; +use orml_traits::{MultiCurrency}; +use common_types::CurrencyId; + +#[benchmarks( where ::AccountId: AsRef<[u8]>, crate::Event::: Into<::RuntimeEvent>)] #[benchmarks] mod benchmarks { use super::*; - #[benchmark] - fn do_something() { - let value = 100u32.into(); - let caller: T::AccountId = whitelisted_caller(); - #[extrinsic_call] - do_something(RawOrigin::Signed(caller), value); - - assert_eq!(Something::::get(), Some(value)); + fn raise_a_dispute() { + let alice: T::AccountId = create_funded_user::("alice", 1, 1_000_000_000_000_000_000u128); + let bob: T::AccountId = create_funded_user::("bob", 1, 1_000_000_000_000_000_000u128); + let charlie: T::AccountId = create_funded_user::("charlie", 1, 1_000_000_000_000_000_000u128); + let dispute_key = 10; + let jury = get_jury::(vec![alice, bob]); + let specifics = get_specifics::(vec![0, 1]); + assert_ok!(>::raise_dispute( + dispute_key, + alice, + jury, + specifics, + )); + assert!(PalletDisputes::disputes(dispute_key).is_some()); + assert_eq!(1, PalletDisputes::disputes(dispute_key).iter().count()); } - #[benchmark] - fn cause_error() { - Something::::put(100u32); - let caller: T::AccountId = whitelisted_caller(); - #[extrinsic_call] - cause_error(RawOrigin::Signed(caller)); - assert_eq!(Something::::get(), Some(101u32)); - } + impl_benchmark_test_suite!(PalletDisputes, crate::mock::new_test_ext(), crate::mock::Test); +} - impl_benchmark_test_suite!(Disputes, crate::mock::new_test_ext(), crate::mock::Test); +pub fn create_funded_user( + seed: &'static str, + n: u32, + balance_factor: u128, +) -> T::AccountId { + let user = account(seed, n, 0); + assert_ok!(::AccountId, + >>::deposit( + CurrencyId::Native, &user, balance_factor.saturated_into() + )); + user } + diff --git a/pallets/disputes/src/tests.rs b/pallets/disputes/src/tests.rs index d0aeddfb..a86c408f 100644 --- a/pallets/disputes/src/tests.rs +++ b/pallets/disputes/src/tests.rs @@ -8,9 +8,10 @@ use test_utils::*; mod test_utils { use super::*; + pub fn run_to_block(n: T::BlockNumber) - where - T::BlockNumber: Into, + where + T::BlockNumber: Into, { loop { let mut block: T::BlockNumber = frame_system::Pallet::::block_number(); @@ -56,7 +57,7 @@ fn raise_dispute_assert_state() { #[test] fn raise_dispute_assert_event() { - new_test_ext().execute_with(||{ + new_test_ext().execute_with(|| { let dispute_key = 10; let jury = get_jury::(vec![*CHARLIE, *BOB]); let specifics = get_specifics::(vec![0, 1]); @@ -67,7 +68,7 @@ fn raise_dispute_assert_event() { specifics, )); System::assert_last_event(RuntimeEvent::PalletDisputes( - Event::::DisputeRaised{ dispute_key:dispute_key}, + Event::::DisputeRaised { dispute_key: dispute_key }, )); }); } @@ -109,7 +110,7 @@ fn vote_on_dispute_assert_state() { specifics, )); let dispute_before_vote = Disputes::::get(dispute_key).expect("dispute should exist"); - assert_eq!(0,dispute_before_vote.votes.len()); + assert_eq!(0, dispute_before_vote.votes.len()); assert_ok!(PalletDisputes::vote_on_dispute( RuntimeOrigin::signed(*BOB), dispute_key, @@ -117,8 +118,8 @@ fn vote_on_dispute_assert_state() { )); let dispute_after_vote = Disputes::::get(dispute_key).expect("dispute should exist"); let vote = dispute_after_vote.votes.get(&BOB).unwrap(); - assert_eq!(true,*vote); - assert_eq!(1,dispute_after_vote.votes.len()); + assert_eq!(true, *vote); + assert_eq!(1, dispute_after_vote.votes.len()); }); } @@ -172,12 +173,40 @@ fn vote_on_dispute_autofinalises_on_unanimous_yes() { true )); //verify that the dispute has been removed once auto_finalization is done in case of unanimous yes - assert_eq!(0,PalletDisputes::disputes(dispute_key).iter().count()); + assert_eq!(0, PalletDisputes::disputes(dispute_key).iter().count()); }); } #[test] fn vote_on_dispute_autofinalises_on_unanimous_no() { + new_test_ext().execute_with(|| { + let dispute_key = 10; + let jury = get_jury::(vec![*CHARLIE, *BOB]); + let specifics = get_specifics::(vec![0, 1]); + assert_ok!(>::raise_dispute( + dispute_key, + *ALICE, + jury, + specifics, + )); + assert_ok!(PalletDisputes::vote_on_dispute( + RuntimeOrigin::signed(*BOB), + dispute_key, + false + )); + assert_ok!(PalletDisputes::vote_on_dispute( + RuntimeOrigin::signed(*CHARLIE), + dispute_key, + false + )); + //verify that the dispute has been removed once auto_finalization is done in case of unanimous no + assert_eq!(0, PalletDisputes::disputes(dispute_key).iter().count()); + }); +} + +///SHANKAR: What does this mean? +#[test] +fn try_auto_finalise_removes_autofinalise() { new_test_ext().execute_with(|| { new_test_ext().execute_with(|| { let dispute_key = 10; @@ -200,19 +229,14 @@ fn vote_on_dispute_autofinalises_on_unanimous_no() { false )); //verify that the dispute has been removed once auto_finalization is done in case of unanimous no - assert_eq!(0,PalletDisputes::disputes(dispute_key).iter().count()); + assert_eq!(0, PalletDisputes::disputes(dispute_key).iter().count()); + //After the dispute has been autofinalized and the we again tru to autofinalize it throws an error saying that + // the dispute doesnt exists as it has been removed + assert_noop!(Dispute::::try_finalise_with_result(dispute_key,DisputeResult::Success),Error::::DisputeDoesNotExist); }); }); } -///SHANKAR: What does this mean? -#[test] -fn try_auto_finalise_removes_autofinalise() { - new_test_ext().execute_with(|| { - todo!() - }); -} - ///testing if the non jury account tries to vote it should throw the error saying its not a jury account #[test] fn vote_on_dispute_not_jury_account() { @@ -321,7 +345,31 @@ fn extend_dispute_already_extended() { /// testing trying to extend the voting time and it successfully extend by setting the flag to true #[test] -fn extend_dispute_works() { +fn extend_dispute_works_assert_last_event() { + new_test_ext().execute_with(|| { + let dispute_key = 10; + let jury = get_jury::(vec![*BOB]); + let specific_ids = get_specifics::(vec![0]); + assert_ok!(>::raise_dispute( + dispute_key, + *ALICE, + jury, + specific_ids + )); + let d = Disputes::::get(dispute_key).expect("dispute should exist"); + assert!(!d.is_extended); + assert_ok!(PalletDisputes::extend_dispute( + RuntimeOrigin::signed(*BOB), + 10 + )); + System::assert_last_event(RuntimeEvent::PalletDisputes( + Event::::DisputeExtended { dispute_key: dispute_key }, + )); + }); +} + +#[test] +fn extend_dispute_works_assert_state() { new_test_ext().execute_with(|| { let dispute_key = 10; let jury = get_jury::(vec![*BOB]); @@ -334,17 +382,48 @@ fn extend_dispute_works() { )); let d = Disputes::::get(dispute_key).expect("dispute should exist"); assert!(!d.is_extended); + assert_eq!(11,d.expiration); assert_ok!(PalletDisputes::extend_dispute( RuntimeOrigin::signed(*BOB), 10 )); let d = Disputes::::get(dispute_key).expect("dispute should exist"); assert!(d.is_extended); + assert_eq!(21,d.expiration); }); } #[test] fn calculate_winner_works() { + new_test_ext().execute_with(|| {}); +} + + +///e2e +#[test] +fn e2e() { new_test_ext().execute_with(|| { + let dispute_key = 10; + let jury = get_jury::(vec![*CHARLIE, *BOB]); + let specifics = get_specifics::(vec![0, 1]); + assert_ok!(>::raise_dispute( + dispute_key, + *ALICE, + jury, + specifics, + )); + assert_ok!(PalletDisputes::vote_on_dispute( + RuntimeOrigin::signed(*BOB), + dispute_key, + true + )); + assert_ok!(PalletDisputes::vote_on_dispute( + RuntimeOrigin::signed(*CHARLIE), + dispute_key, + true + )); + //verify that the dispute has been removed once auto_finalization is done in case of unanimous yes + assert_eq!(0, PalletDisputes::disputes(dispute_key).iter().count()); }); } +