Skip to content
Snippets Groups Projects
Commit d17dcd65 authored by Pascal Engélibert's avatar Pascal Engélibert :bicyclist: Committed by Hugo Trentesaux
Browse files

fix: disable UD when membership removed (!299)

* fix: disable UD when membership removed
parent c4b3f8c5
No related branches found
No related tags found
1 merge request!299fix: disable UD when membership removed
Pipeline #39471 waiting for manual action
...@@ -22,9 +22,7 @@ use sp_runtime::RuntimeDebug; ...@@ -22,9 +22,7 @@ use sp_runtime::RuntimeDebug;
pub type UdIndex = u16; pub type UdIndex = u16;
/// Represents the first eligible Universal Dividend. /// Represents the first eligible Universal Dividend.
#[derive( #[derive(Clone, Default, Eq, PartialEq, RuntimeDebug, serde::Deserialize, serde::Serialize)]
Clone, Copy, Default, Eq, PartialEq, RuntimeDebug, serde::Deserialize, serde::Serialize,
)]
pub struct FirstEligibleUd(pub Option<NonZeroU16>); pub struct FirstEligibleUd(pub Option<NonZeroU16>);
impl FirstEligibleUd { impl FirstEligibleUd {
......
...@@ -111,21 +111,20 @@ impl< ...@@ -111,21 +111,20 @@ impl<
// duniter-wot related actions // duniter-wot related actions
let mut weight = pallet_duniter_wot::Pallet::<Runtime>::on_removed(idty_index); let mut weight = pallet_duniter_wot::Pallet::<Runtime>::on_removed(idty_index);
let mut add_db_reads_writes = |reads, writes| { // When membership is removed:
weight += Runtime::DbWeight::get().reads_writes(reads, writes); // - call on_removed_member handler which auto claims UD;
}; // - set the first_eligible_ud to None so the identity cannot claim UD anymore.
pallet_identity::Identities::<Runtime>::mutate(idty_index, |maybe_idty_value| {
// When membership is removed, call on_removed_member handler which auto claims UD. if let Some(idty_value) = maybe_idty_value {
if let Some(idty_value) = pallet_identity::Identities::<Runtime>::get(idty_index) { if let Some(first_ud_index) = idty_value.data.first_eligible_ud.0.take() {
add_db_reads_writes(1, 0); weight += pallet_universal_dividend::Pallet::<Runtime>::on_removed_member(
if let Some(first_ud_index) = idty_value.data.first_eligible_ud.into() { first_ud_index.into(),
add_db_reads_writes(1, 0); &idty_value.owner_key,
weight += pallet_universal_dividend::Pallet::<Runtime>::on_removed_member( );
first_ud_index, }
&idty_value.owner_key,
);
} }
} });
weight += Runtime::DbWeight::get().reads_writes(1, 1);
// When membership is removed, also remove from smith member. // When membership is removed, also remove from smith member.
weight.saturating_add( weight.saturating_add(
......
...@@ -59,7 +59,7 @@ where ...@@ -59,7 +59,7 @@ where
) -> Result<R, E> { ) -> Result<R, E> {
pallet_identity::Pallet::<T>::try_mutate_exists(key, |maybe_idty_data| { pallet_identity::Pallet::<T>::try_mutate_exists(key, |maybe_idty_data| {
if let Some(ref mut idty_data) = maybe_idty_data { if let Some(ref mut idty_data) = maybe_idty_data {
let mut maybe_first_eligible_ud = Some(idty_data.first_eligible_ud); let mut maybe_first_eligible_ud = Some(idty_data.first_eligible_ud.clone());
let result = f(&mut maybe_first_eligible_ud)?; let result = f(&mut maybe_first_eligible_ud)?;
if let Some(first_eligible_ud) = maybe_first_eligible_ud { if let Some(first_eligible_ud) = maybe_first_eligible_ud {
idty_data.first_eligible_ud = first_eligible_ud; idty_data.first_eligible_ud = first_eligible_ud;
......
...@@ -731,6 +731,57 @@ fn test_revoke_identity_after_one_ud() { ...@@ -731,6 +731,57 @@ fn test_revoke_identity_after_one_ud() {
}); });
} }
// test that UD cannot be claimed after revocation
#[test]
fn test_claim_ud_after_revoke() {
ExtBuilder::new(1, 3, 4).build().execute_with(|| {
run_to_block(
(<Runtime as pallet_universal_dividend::Config>::UdCreationPeriod::get()
/ <Runtime as pallet_babe::Config>::ExpectedBlockTime::get()
+ 1) as u32,
);
// before UD, bob has 0 (initial amount)
run_to_block(1);
assert_eq!(
Balances::free_balance(AccountKeyring::Bob.to_account_id()),
0
);
// revoke identity
Identity::do_revoke_identity(2, pallet_identity::RevocationReason::Root);
assert_eq!(
Balances::free_balance(AccountKeyring::Bob.to_account_id()),
1_000
);
// go after UD creation block
run_to_block(
(<Runtime as pallet_universal_dividend::Config>::UdCreationPeriod::get()
/ <Runtime as pallet_babe::Config>::ExpectedBlockTime::get()
+ 1) as u32,
);
assert_eq!(
Balances::free_balance(AccountKeyring::Bob.to_account_id()),
1_000
);
assert_err!(
UniversalDividend::claim_uds(
frame_system::RawOrigin::Signed(AccountKeyring::Bob.to_account_id()).into()
),
pallet_universal_dividend::Error::<Runtime>::AccountNotAllowedToClaimUds,
);
assert_eq!(
Balances::free_balance(AccountKeyring::Bob.to_account_id()),
1_000
);
});
}
/// test that UD are auto claimed when membership expires /// test that UD are auto claimed when membership expires
/// and that claimed UD matches expectations /// and that claimed UD matches expectations
#[test] #[test]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment