Skip to content

Commit

Permalink
Expose add_member_to_rank extrinsic
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg Plakida committed Jun 12, 2024
1 parent c4aa2ab commit 1d2a54c
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,10 @@ impl<T: frame_system::Config> pallet_ranked_collective::WeightInfo for WeightInf
.saturating_add(T::DbWeight::get().reads(6))
.saturating_add(T::DbWeight::get().writes(10))
}
fn add_member_to_rank(r: u32, ) -> Weight {
Weight::from_parts(15_000_000, 3507)
// Standard Error: 1_000
.saturating_add((Weight::from_parts(251_000, 0)).saturating_mul(r.into()))
.saturating_add(T::DbWeight::get().reads(1_u64))
}
}
18 changes: 16 additions & 2 deletions substrate/frame/ranked-collective/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
// limitations under the License.

//! Staking pallet benchmarking.

use super::*;
#[allow(unused_imports)]
use crate::Pallet as RankedCollective;
Expand All @@ -25,8 +24,8 @@ use frame_benchmarking::v1::{
account, benchmarks_instance_pallet, whitelisted_caller, BenchmarkError,
};
use frame_support::{assert_ok, traits::UnfilteredDispatchable};
use frame_system::RawOrigin as SystemOrigin;

use frame_system::RawOrigin as SystemOrigin;
const SEED: u32 = 0;

fn assert_last_event<T: Config<I>, I: 'static>(generic_event: <T as Config<I>>::RuntimeEvent) {
Expand Down Expand Up @@ -181,5 +180,20 @@ benchmarks_instance_pallet! {
assert_has_event::<T, I>(Event::MemberExchanged { who, new_who }.into());
}

add_member_to_rank {
let r in 0 .. 10;
let rank = r as u16;
let who = account::<T::AccountId>("member-without-rank", 0, SEED);
let who_lookup = T::Lookup::unlookup(who.clone());
let origin =
T::AddOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
let call = Call::<T, I>::add_member_to_rank { who: who_lookup, rank };
}: { call.dispatch_bypass_filter(origin)? }
verify {
assert_eq!(Members::<T, I>::get(&who).unwrap().rank, rank);
assert_eq!(MemberCount::<T, I>::get(0),1);
assert_eq!(MemberCount::<T, I>::get(rank),1);
}

impl_benchmark_test_suite!(RankedCollective, crate::tests::ExtBuilder::default().build(), crate::tests::Test);
}
12 changes: 12 additions & 0 deletions substrate/frame/ranked-collective/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,18 @@ pub mod pallet {

Ok(())
}

#[pallet::call_index(7)]
#[pallet::weight(T::WeightInfo::add_member_to_rank(*rank as u32))]
pub fn add_member_to_rank(
origin: OriginFor<T>,
who: AccountIdLookupOf<T>,
rank: Rank,
) -> DispatchResult {
T::AddOrigin::ensure_origin(origin)?;
let who = T::Lookup::lookup(who)?;
Self::do_add_member_to_rank(who, rank, true)
}
}

#[pallet::hooks]
Expand Down
14 changes: 14 additions & 0 deletions substrate/frame/ranked-collective/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,3 +645,17 @@ fn exchange_member_same_noops() {
);
});
}

#[test]
pub fn add_member_to_rank_work(){
ExtBuilder::default().build_and_execute(|| {
let max_rank = 4u16;
assert_ok!(Club::add_member_to_rank(RuntimeOrigin::root(), 2,max_rank));
for i in 0..=max_rank{
assert_eq!(member_count(i),1);
}

//-- Should fail ----------------------------------------
assert_noop!(Club::add_member_to_rank(RuntimeOrigin::signed(1),2,1), DispatchError::BadOrigin);
})
}
50 changes: 50 additions & 0 deletions substrate/frame/ranked-collective/src/weights.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1d2a54c

Please sign in to comment.