|
| 1 | +//! Benchmarking setup for pallet-events |
| 2 | +use super::*; |
| 3 | + |
| 4 | +// use crate::Pallet; |
| 5 | +use frame_benchmarking::{account, benchmarks, whitelisted_caller}; |
| 6 | +use frame_support::BoundedVec; |
| 7 | +use frame_system::RawOrigin; |
| 8 | +use pallet_community::types::{ |
| 9 | + Category, CommunityMetaData, CommunityType, Customs, Languages, Religions, Territories, |
| 10 | + Traditions, Values, |
| 11 | +}; |
| 12 | +use pallet_passport::types::BadgesType; |
| 13 | +use sp_std::vec; |
| 14 | +use sp_std::vec::Vec; |
| 15 | +const SEED: u32 = 0; |
| 16 | + |
| 17 | +fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) { |
| 18 | + frame_system::Pallet::<T>::assert_last_event(generic_event.into()); |
| 19 | +} |
| 20 | + |
| 21 | +fn get_community_metadata<T: Config>() -> CommunityMetaData<T::StringLimit> { |
| 22 | + let custom_one: Vec<u8> = |
| 23 | + "in public transport young people should leave the seat to elderly or pregnant women" |
| 24 | + .into(); |
| 25 | + let custom_two: Vec<u8> = "name newborns with a name that starts with the letter A".into(); |
| 26 | + |
| 27 | + let languages_1: Vec<u8> = "English".into(); |
| 28 | + let languages_2: Vec<u8> = "German".into(); |
| 29 | + |
| 30 | + let religions_1: Vec<u8> = "Christianity".into(); |
| 31 | + let religions_2: Vec<u8> = "Buddhism".into(); |
| 32 | + |
| 33 | + let territories: Vec<u8> = "Mars".into(); |
| 34 | + |
| 35 | + let traditions_1: Vec<u8> = "Exchange gifts for Christmas".into(); |
| 36 | + let traditions_2: Vec<u8> = "Organize one charity event every 100 blocks".into(); |
| 37 | + |
| 38 | + let values_1: Vec<u8> = "Peace".into(); |
| 39 | + let values_2: Vec<u8> = "No gender discrimination".into(); |
| 40 | + |
| 41 | + let community_metadata = CommunityMetaData { |
| 42 | + customs: Some(vec![ |
| 43 | + Customs(custom_one.try_into().unwrap()), |
| 44 | + Customs(custom_two.try_into().unwrap()), |
| 45 | + ]), |
| 46 | + languages: Some(vec![ |
| 47 | + Languages(languages_1.try_into().unwrap()), |
| 48 | + Languages(languages_2.try_into().unwrap()), |
| 49 | + ]), |
| 50 | + norms: Some(vec![]), |
| 51 | + religions: Some(vec![ |
| 52 | + Religions(religions_1.try_into().unwrap()), |
| 53 | + Religions(religions_2.try_into().unwrap()), |
| 54 | + ]), |
| 55 | + territories: Some(vec![Territories(territories.try_into().unwrap())]), |
| 56 | + traditions: Some(vec![ |
| 57 | + Traditions(traditions_1.try_into().unwrap()), |
| 58 | + Traditions(traditions_2.try_into().unwrap()), |
| 59 | + ]), |
| 60 | + values: Some(vec![ |
| 61 | + Values(values_1.try_into().unwrap()), |
| 62 | + Values(values_2.try_into().unwrap()), |
| 63 | + ]), |
| 64 | + }; |
| 65 | + |
| 66 | + community_metadata |
| 67 | +} |
| 68 | + |
| 69 | +fn create_community<T: Config>(caller: T::AccountId) -> T::CommunityId { |
| 70 | + let community_id = |
| 71 | + pallet_community::NextCommunityId::<T>::get().unwrap_or(T::CommunityId::initial_value()); |
| 72 | + |
| 73 | + let members = vec![account("sub", 1, SEED), account("sub", 2, SEED), account("sub", 3, SEED)]; |
| 74 | + |
| 75 | + pallet_community::Pallet::<T>::create_community( |
| 76 | + RawOrigin::Signed(caller).into(), |
| 77 | + // hash of IPFS path of dummy logo |
| 78 | + Some("bafkreifec54rzopwm6mvqm3fknmdlsw2yefpdr7xrgtsron62on2nynegq".into()), |
| 79 | + "Jur".as_bytes().to_vec(), |
| 80 | + Some( |
| 81 | + "Jur is the core community of the Jur ecosystem, which includes all the contributors." |
| 82 | + .into(), |
| 83 | + ), |
| 84 | + Some(members), |
| 85 | + Some(get_community_metadata::<T>()), |
| 86 | + Category::Public, |
| 87 | + Some("tag".into()), |
| 88 | + Some("#222307".into()), |
| 89 | + Some("#E76080".into()), |
| 90 | + Some(CommunityType::Nation), |
| 91 | + ) |
| 92 | + .unwrap(); |
| 93 | + |
| 94 | + community_id |
| 95 | +} |
| 96 | + |
| 97 | +pub fn add_founder<T: Config>(caller: T::AccountId) { |
| 98 | + pallet_whitelist::Pallet::<T>::add_founder(RawOrigin::Root.into(), caller).unwrap(); |
| 99 | +} |
| 100 | + |
| 101 | +fn add_badge<T: Config>(caller: T::AccountId, community_id: T::CommunityId) { |
| 102 | + let badge_name: Vec<u8> = "DEV MEGA SPOT".into(); |
| 103 | + let bounded_badge_name: BoundedVec<u8, <T as pallet_passport::Config>::BadgeNameLimit> = |
| 104 | + badge_name.try_into().unwrap(); |
| 105 | + |
| 106 | + let badge_description: Vec<u8> = "World largest hackathon to built on JUR".into(); |
| 107 | + let bounded_badge_description: BoundedVec< |
| 108 | + u8, |
| 109 | + <T as pallet_passport::Config>::DescriptionLimit, |
| 110 | + > = badge_description.try_into().unwrap(); |
| 111 | + |
| 112 | + let badge_address: Vec<u8> = |
| 113 | + "abcdreifec54rzopwm6mvqm3fknmdlsw2yefpdr7xrgtsron62on2nynegq".into(); |
| 114 | + let bounded_badge_address: BoundedVec<u8, <T as pallet_passport::Config>::AddressLimit> = |
| 115 | + badge_address.try_into().unwrap(); |
| 116 | + |
| 117 | + pallet_passport::Pallet::<T>::add_badge( |
| 118 | + RawOrigin::Signed(caller).into(), |
| 119 | + community_id, |
| 120 | + bounded_badge_name, |
| 121 | + BadgesType::Participation, |
| 122 | + bounded_badge_description, |
| 123 | + bounded_badge_address, |
| 124 | + ) |
| 125 | + .unwrap(); |
| 126 | +} |
| 127 | + |
| 128 | +benchmarks! { |
| 129 | + create_event { |
| 130 | + let caller: T::AccountId = whitelisted_caller(); |
| 131 | + let member = account("sub", 1, SEED); |
| 132 | + add_founder::<T>(caller.clone()); |
| 133 | + let community_id = create_community::<T>(caller.clone()); |
| 134 | + pallet_passport::Pallet::<T>::mint(RawOrigin::Signed(member).into(), community_id.clone()).unwrap(); |
| 135 | + add_badge::<T>(caller.clone(), community_id.clone()); |
| 136 | + |
| 137 | + let event_name: Vec<u8> = "Event for developers to built on JUR".into(); |
| 138 | + let bounded_event_name: BoundedVec<u8, <T as pallet::Config>::NameLimit> = event_name.try_into().unwrap(); |
| 139 | + |
| 140 | + let event_description: Vec<u8> = "World largest Event for developers to built on JUR".into(); |
| 141 | + let bounded_event_description: BoundedVec<u8, <T as pallet::Config>::DescriptionLimit> = event_description.try_into().unwrap(); |
| 142 | + |
| 143 | + let badge_name: Vec<u8> = "DEV MEGA SPOT".into(); |
| 144 | + let bounded_badge_name: BoundedVec<u8, <T as pallet_passport::Config>::BadgeNameLimit> = badge_name.try_into().unwrap(); |
| 145 | + |
| 146 | + }: _( |
| 147 | + RawOrigin::Signed(caller.clone()), |
| 148 | + community_id, |
| 149 | + bounded_event_name, |
| 150 | + bounded_event_description, |
| 151 | + 1703745212, |
| 152 | + 1703831612, |
| 153 | + EventType::Virtual, |
| 154 | + None, |
| 155 | + bounded_badge_name |
| 156 | + ) |
| 157 | + verify { |
| 158 | + assert_last_event::<T>(Event::<T>::CreatedEvent(community_id, <T as pallet::Config>::Helper::event(1)).into()); |
| 159 | + } |
| 160 | + |
| 161 | + impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test); |
| 162 | +} |
0 commit comments