mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-21 07:31:03 +00:00
Salary pallet (#13378)
* More drafting * Paymaster pallet * Fix build * More tests * Rename * Rename * Renaming * Revert old changes * Multi-phase payouts to avoid bank-runs * Tests * Tests * Allow payment to be targeted elsewhere * Proper ssync payment failure handling * Test for repayment * Docs * Impl RankedMembers for RankedCollective * Implement Pay for Pot (i.e. basic account). * Benchmarks * Weights * Introduce Salary benchmark into node * Fix warning * ".git/.scripts/commands/bench/bench.sh" pallet dev pallet_salary * Update primitives/arithmetic/src/traits.rs Co-authored-by: Jegor Sidorenko <5252494+jsidorenko@users.noreply.github.com> * Update frame/salary/src/lib.rs Co-authored-by: Jegor Sidorenko <5252494+jsidorenko@users.noreply.github.com> * Update lib.rs * Update frame/salary/src/lib.rs Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com> * Docs * Update frame/salary/src/lib.rs Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com> * Update frame/salary/src/lib.rs Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com> * Fix * Fixes * Fixes * Move some salary traits stuff to a shared location * Fix * Update frame/salary/src/lib.rs Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Update frame/salary/src/lib.rs Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Mul floor * Fix warnings * Fix test * Docs --------- Co-authored-by: command-bot <> Co-authored-by: Jegor Sidorenko <5252494+jsidorenko@users.noreply.github.com> Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com> Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
This commit is contained in:
@@ -0,0 +1,183 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2020-2022 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Salary pallet benchmarking.
|
||||
|
||||
#![cfg(feature = "runtime-benchmarks")]
|
||||
|
||||
use super::*;
|
||||
use crate::Pallet as Salary;
|
||||
|
||||
use frame_benchmarking::v2::*;
|
||||
use frame_system::{Pallet as System, RawOrigin};
|
||||
use sp_core::Get;
|
||||
|
||||
const SEED: u32 = 0;
|
||||
|
||||
fn ensure_member_with_salary<T: Config<I>, I: 'static>(who: &T::AccountId) {
|
||||
// induct if not a member.
|
||||
if T::Members::rank_of(who).is_none() {
|
||||
T::Members::induct(who).unwrap();
|
||||
}
|
||||
// promote until they have a salary.
|
||||
for _ in 0..255 {
|
||||
let r = T::Members::rank_of(who).expect("prior guard ensures `who` is a member; qed");
|
||||
if !T::Salary::get_salary(r, &who).is_zero() {
|
||||
break
|
||||
}
|
||||
T::Members::promote(who).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[instance_benchmarks]
|
||||
mod benchmarks {
|
||||
use super::*;
|
||||
|
||||
#[benchmark]
|
||||
fn init() {
|
||||
let caller: T::AccountId = whitelisted_caller();
|
||||
|
||||
#[extrinsic_call]
|
||||
_(RawOrigin::Signed(caller.clone()));
|
||||
|
||||
assert!(Salary::<T, I>::status().is_some());
|
||||
}
|
||||
|
||||
#[benchmark]
|
||||
fn bump() {
|
||||
let caller: T::AccountId = whitelisted_caller();
|
||||
Salary::<T, I>::init(RawOrigin::Signed(caller.clone()).into()).unwrap();
|
||||
System::<T>::set_block_number(System::<T>::block_number() + Salary::<T, I>::cycle_period());
|
||||
|
||||
#[extrinsic_call]
|
||||
_(RawOrigin::Signed(caller.clone()));
|
||||
|
||||
assert_eq!(Salary::<T, I>::status().unwrap().cycle_index, 1u32.into());
|
||||
}
|
||||
|
||||
#[benchmark]
|
||||
fn induct() {
|
||||
let caller = whitelisted_caller();
|
||||
ensure_member_with_salary::<T, I>(&caller);
|
||||
Salary::<T, I>::init(RawOrigin::Signed(caller.clone()).into()).unwrap();
|
||||
|
||||
#[extrinsic_call]
|
||||
_(RawOrigin::Signed(caller.clone()));
|
||||
|
||||
assert!(Salary::<T, I>::last_active(&caller).is_ok());
|
||||
}
|
||||
|
||||
#[benchmark]
|
||||
fn register() {
|
||||
let caller = whitelisted_caller();
|
||||
ensure_member_with_salary::<T, I>(&caller);
|
||||
Salary::<T, I>::init(RawOrigin::Signed(caller.clone()).into()).unwrap();
|
||||
Salary::<T, I>::induct(RawOrigin::Signed(caller.clone()).into()).unwrap();
|
||||
System::<T>::set_block_number(System::<T>::block_number() + Salary::<T, I>::cycle_period());
|
||||
Salary::<T, I>::bump(RawOrigin::Signed(caller.clone()).into()).unwrap();
|
||||
|
||||
#[extrinsic_call]
|
||||
_(RawOrigin::Signed(caller.clone()));
|
||||
|
||||
assert_eq!(Salary::<T, I>::last_active(&caller).unwrap(), 1u32.into());
|
||||
}
|
||||
|
||||
#[benchmark]
|
||||
fn payout() {
|
||||
let caller = whitelisted_caller();
|
||||
ensure_member_with_salary::<T, I>(&caller);
|
||||
Salary::<T, I>::init(RawOrigin::Signed(caller.clone()).into()).unwrap();
|
||||
Salary::<T, I>::induct(RawOrigin::Signed(caller.clone()).into()).unwrap();
|
||||
System::<T>::set_block_number(System::<T>::block_number() + Salary::<T, I>::cycle_period());
|
||||
Salary::<T, I>::bump(RawOrigin::Signed(caller.clone()).into()).unwrap();
|
||||
System::<T>::set_block_number(System::<T>::block_number() + T::RegistrationPeriod::get());
|
||||
|
||||
let salary = T::Salary::get_salary(T::Members::rank_of(&caller).unwrap(), &caller);
|
||||
T::Paymaster::ensure_successful(&caller, salary);
|
||||
|
||||
#[extrinsic_call]
|
||||
_(RawOrigin::Signed(caller.clone()));
|
||||
|
||||
match Claimant::<T, I>::get(&caller) {
|
||||
Some(ClaimantStatus { last_active, status: Attempted { id, .. } }) => {
|
||||
assert_eq!(last_active, 1u32.into());
|
||||
assert_ne!(T::Paymaster::check_payment(id), PaymentStatus::Failure);
|
||||
},
|
||||
_ => panic!("No claim made"),
|
||||
}
|
||||
assert!(Salary::<T, I>::payout(RawOrigin::Signed(caller.clone()).into()).is_err());
|
||||
}
|
||||
|
||||
#[benchmark]
|
||||
fn payout_other() {
|
||||
let caller = whitelisted_caller();
|
||||
ensure_member_with_salary::<T, I>(&caller);
|
||||
Salary::<T, I>::init(RawOrigin::Signed(caller.clone()).into()).unwrap();
|
||||
Salary::<T, I>::induct(RawOrigin::Signed(caller.clone()).into()).unwrap();
|
||||
System::<T>::set_block_number(System::<T>::block_number() + Salary::<T, I>::cycle_period());
|
||||
Salary::<T, I>::bump(RawOrigin::Signed(caller.clone()).into()).unwrap();
|
||||
System::<T>::set_block_number(System::<T>::block_number() + T::RegistrationPeriod::get());
|
||||
|
||||
let salary = T::Salary::get_salary(T::Members::rank_of(&caller).unwrap(), &caller);
|
||||
let recipient: T::AccountId = account("recipient", 0, SEED);
|
||||
T::Paymaster::ensure_successful(&recipient, salary);
|
||||
|
||||
#[extrinsic_call]
|
||||
_(RawOrigin::Signed(caller.clone()), recipient.clone());
|
||||
|
||||
match Claimant::<T, I>::get(&caller) {
|
||||
Some(ClaimantStatus { last_active, status: Attempted { id, .. } }) => {
|
||||
assert_eq!(last_active, 1u32.into());
|
||||
assert_ne!(T::Paymaster::check_payment(id), PaymentStatus::Failure);
|
||||
},
|
||||
_ => panic!("No claim made"),
|
||||
}
|
||||
assert!(Salary::<T, I>::payout(RawOrigin::Signed(caller.clone()).into()).is_err());
|
||||
}
|
||||
|
||||
#[benchmark]
|
||||
fn check_payment() {
|
||||
let caller = whitelisted_caller();
|
||||
ensure_member_with_salary::<T, I>(&caller);
|
||||
Salary::<T, I>::init(RawOrigin::Signed(caller.clone()).into()).unwrap();
|
||||
Salary::<T, I>::induct(RawOrigin::Signed(caller.clone()).into()).unwrap();
|
||||
System::<T>::set_block_number(System::<T>::block_number() + Salary::<T, I>::cycle_period());
|
||||
Salary::<T, I>::bump(RawOrigin::Signed(caller.clone()).into()).unwrap();
|
||||
System::<T>::set_block_number(System::<T>::block_number() + T::RegistrationPeriod::get());
|
||||
|
||||
let salary = T::Salary::get_salary(T::Members::rank_of(&caller).unwrap(), &caller);
|
||||
let recipient: T::AccountId = account("recipient", 0, SEED);
|
||||
T::Paymaster::ensure_successful(&recipient, salary);
|
||||
Salary::<T, I>::payout(RawOrigin::Signed(caller.clone()).into()).unwrap();
|
||||
let id = match Claimant::<T, I>::get(&caller).unwrap().status {
|
||||
Attempted { id, .. } => id,
|
||||
_ => panic!("No claim made"),
|
||||
};
|
||||
T::Paymaster::ensure_concluded(id);
|
||||
|
||||
#[extrinsic_call]
|
||||
_(RawOrigin::Signed(caller.clone()));
|
||||
|
||||
assert!(!matches!(Claimant::<T, I>::get(&caller).unwrap().status, Attempted { .. }));
|
||||
}
|
||||
|
||||
impl_benchmark_test_suite! {
|
||||
Salary,
|
||||
crate::tests::new_test_ext(),
|
||||
crate::tests::Test,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user