Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

renounce privileges #662

Merged
merged 7 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pallets/grants/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ benchmarks! {
let origin = T::CancelOrigin::successful_origin();
}: { call.dispatch_bypass_filter(origin)? }

renounce {
let config = create_shared_config::<T>(1);
let call = Call::<T>::renounce{
who: config.grantee_lookup,
};
let origin = T::CancelOrigin::successful_origin();
}: { call.dispatch_bypass_filter(origin)? }

impl_benchmark_test_suite!(
Grants,
crate::mock::ExtBuilder::default()
Expand Down
48 changes: 25 additions & 23 deletions pallets/grants/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ mod mock;
#[cfg(test)]
mod tests;

mod migrations;

use codec::{Decode, Encode};
use frame_support::{
ensure,
Expand All @@ -52,7 +50,7 @@ pub use weights::WeightInfo;

pub use pallet::*;

// A value placed in storage that represents the current version of the POA storage.
// A value placed in storage that represents the current version of the Grants storage.
// This value is used by the `on_runtime_upgrade` logic to determine whether we run storage
// migration logic. This should match directly with the semantic versions of the Rust crate.
#[derive(Encode, MaxEncodedLen, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo)]
Expand Down Expand Up @@ -122,15 +120,14 @@ impl<BlockNumber: AtLeast32Bit + Copy, Balance: AtLeast32Bit + Copy> VestingSche
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_support::pallet_prelude::{DispatchResultWithPostInfo, *};
use frame_system::pallet_prelude::*;

#[pallet::config]
pub trait Config: frame_system::Config {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
type Currency: LockableCurrency<Self::AccountId, Moment = Self::BlockNumber>;
type CancelOrigin: EnsureOrigin<Self::Origin>;
type ForceOrigin: EnsureOrigin<Self::Origin>;
/// The maximum number of vesting schedule.
#[pallet::constant]
type MaxSchedule: Get<u32>;
Expand All @@ -144,23 +141,6 @@ pub mod pallet {
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(PhantomData<T>);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<(), &'static str> {
migrations::v1::pre_upgrade::<T>()
}

fn on_runtime_upgrade() -> frame_support::weights::Weight {
migrations::v1::on_runtime_upgrade::<T>()
}

#[cfg(feature = "try-runtime")]
fn post_upgrade() -> Result<(), &'static str> {
migrations::v1::post_upgrade::<T>()
}
}

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Claim funds that have been vested so far
Expand Down Expand Up @@ -206,8 +186,9 @@ pub mod pallet {
T::CancelOrigin::try_origin(origin).map(|_| ()).or_else(ensure_root)?;

let account_with_schedule = T::Lookup::lookup(who)?;
let account_collector = T::Lookup::lookup(funds_collector)?;
ensure!(!Self::renounced(account_with_schedule.clone()), Error::<T>::Renounced);

let account_collector = T::Lookup::lookup(funds_collector)?;
let locked_amount_left = Self::do_claim(&account_with_schedule);
let free_balance = T::Currency::free_balance(&account_with_schedule);
let collectable_funds = locked_amount_left.min(free_balance);
Expand All @@ -227,6 +208,20 @@ pub mod pallet {

Ok(().into())
}

/// Allows the `CancelOrigin` to renounce to its privileges of being able to cancel
/// `who`'s vesting schedules.
#[pallet::weight(T::WeightInfo::renounce())]
pub fn renounce(origin: OriginFor<T>, who: <T::Lookup as StaticLookup>::Source) -> DispatchResultWithPostInfo {
T::CancelOrigin::try_origin(origin).map(|_| ()).or_else(ensure_root)?;

let target = T::Lookup::lookup(who)?;
Renounced::<T>::insert(target.clone(), true);

Self::deposit_event(Event::Renounced(target));

Ok(().into())
}
}

#[pallet::event]
Expand All @@ -238,6 +233,8 @@ pub mod pallet {
Claimed(T::AccountId, BalanceOf<T>),
/// Canceled all vesting schedules \[who\]
VestingSchedulesCanceled(T::AccountId),
/// Renounced rights to cancel grant for the given account id \[who\]
Renounced(T::AccountId),
}

#[pallet::error]
Expand All @@ -249,6 +246,7 @@ pub mod pallet {
EmptySchedules,
VestingToSelf,
MaxScheduleOverflow,
Renounced,
}

#[pallet::storage]
Expand All @@ -261,6 +259,10 @@ pub mod pallet {
ValueQuery,
>;

#[pallet::storage]
#[pallet::getter(fn renounced)]
pub type Renounced<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, bool, ValueQuery>;

#[pallet::storage]
pub(crate) type StorageVersion<T: Config> = StorageValue<_, Releases, ValueQuery>;

Expand Down
165 changes: 0 additions & 165 deletions pallets/grants/src/migrations.rs

This file was deleted.

2 changes: 0 additions & 2 deletions pallets/grants/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ ord_parameter_types! {
pub const ALICE: AccountId = 1;
pub const BOB: AccountId = 2;
pub const CancelOrigin: AccountId = 42;
pub const ForceOrigin: AccountId = 43;
}

parameter_types! {
Expand All @@ -109,7 +108,6 @@ impl Config for Test {
type Event = Event;
type Currency = PalletBalances;
type CancelOrigin = EnsureSignedBy<CancelOrigin, AccountId>;
type ForceOrigin = EnsureSignedBy<ForceOrigin, AccountId>;
type MaxSchedule = MaxSchedule;
type WeightInfo = ();
type BlockNumberProvider = frame_system::Pallet<Test>;
Expand Down
32 changes: 32 additions & 0 deletions pallets/grants/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,3 +571,35 @@ fn add_vesting_schedule_overflow_cfg_max_check() {
assert_eq!(context_events().len(), schedule_max as usize);
});
}

#[test]
fn renounce_only_works_for_cancel_origin() {
ExtBuilder::default().build().execute_with(|| {
assert_noop!(Vesting::renounce(Origin::signed(ALICE::get()), BOB::get()), BadOrigin);
})
}

#[test]
fn renounce_privileges() {
ExtBuilder::default().one_hundred_for_alice().build().execute_with(|| {
let schedule = VestingSchedule {
start: 0u64,
period: 10u64,
period_count: 2u32,
per_period: 10u64,
};
assert_ok!(Vesting::add_vesting_schedule(
Origin::signed(ALICE::get()),
BOB::get(),
schedule
));

assert_eq!(Vesting::renounced(BOB::get()), false);
assert_ok!(Vesting::renounce(Origin::signed(CancelOrigin::get()), BOB::get()));
assert_eq!(Vesting::renounced(BOB::get()), true);
assert_noop!(
Vesting::cancel_all_vesting_schedules(Origin::signed(CancelOrigin::get()), BOB::get(), CancelOrigin::get()),
Error::<Runtime>::Renounced
);
});
}
Loading