diff --git a/Cargo.lock b/Cargo.lock index d420f9d4faa765634278ad7d0f07b5bffb30122a..b8f42cc068abe956a266a2166f141a36a1c77ed2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9547,6 +9547,7 @@ dependencies = [ "scale-info", "sp-core", "sp-io", + "sp-membership", "sp-runtime", ] diff --git a/end2end-tests/cucumber-features/distance_fail.feature b/end2end-tests/cucumber-features/distance_fail.feature index 1abb9df532bda6e0dd03da47b8885fe39af3d30e..e465b715a5321c2201a9f452e55d9c7760f1e079 100644 --- a/end2end-tests/cucumber-features/distance_fail.feature +++ b/end2end-tests/cucumber-features/distance_fail.feature @@ -31,12 +31,14 @@ Feature: Distance fail When bob certifies ferdie Then ferdie should be certified by bob Then ferdie should have 0 ÄžD reserved - Then ferdie should have 1449 cÄžD + # 700 + 750 - 2(one transaction fee, not a member yet) + Then ferdie should have 1448 cÄžD When ferdie requests distance evaluation Then ferdie should have 10 ÄžD reserved - Then ferdie should have 449 cÄžD + # 1448 - 1000 - 2(one transaction fee, not a member yet) + Then ferdie should have 446 cÄžD When 7 blocks later - Then treasury should contain 102 cÄžD + Then treasury should contain 105 cÄžD When alice runs distance oracle When 7 blocks later Then ferdie should be certified by alice @@ -45,6 +47,6 @@ Feature: Distance fail Then ferdie identity should be unvalidated # Ferdie got his reserve slashed Then ferdie should have 0 ÄžD reserved - Then ferdie should have 449 cÄžD + Then ferdie should have 446 cÄžD # Slashed amount is transfered to treasury - Then treasury should contain 1102 cÄžD + Then treasury should contain 1105 cÄžD diff --git a/end2end-tests/cucumber-features/identity_creation.feature b/end2end-tests/cucumber-features/identity_creation.feature index 97ae14b029732fc33643301a75cccdbb94fcdaf2..3bb8fee162300da4dbc73639a04a7698da1e2925 100644 --- a/end2end-tests/cucumber-features/identity_creation.feature +++ b/end2end-tests/cucumber-features/identity_creation.feature @@ -24,13 +24,15 @@ Feature: Identity creation Then dave should be certified by bob Then dave should be certified by charlie Then dave should have 0 ÄžD reserved - Then dave should have 1449 cÄžD + # 700 + 750 - 2(one transaction fee, not a member yet) + Then dave should have 1448 cÄžD When dave requests distance evaluation Then dave should have 10 ÄžD reserved - Then dave should have 449 cÄžD + # 1448 - 1000 - 2(one transaction fee, not a member yet) + Then dave should have 446 cÄžD When 7 blocks later When alice runs distance oracle When 7 blocks later Then dave identity should be member Then dave should have 0 ÄžD reserved - Then dave should have 1449 cÄžD + Then dave should have 1446 cÄžD diff --git a/pallets/quota/Cargo.toml b/pallets/quota/Cargo.toml index 9df99355f7735a97d06dc783a59d6852a444e01c..d23f77648be15c77e612c48a34450372fb2b16cc 100644 --- a/pallets/quota/Cargo.toml +++ b/pallets/quota/Cargo.toml @@ -16,6 +16,7 @@ runtime-benchmarks = [ "frame-system/runtime-benchmarks", "pallet-balances/runtime-benchmarks", "pallet-identity/runtime-benchmarks", + "sp-membership/runtime-benchmarks", "sp-runtime/runtime-benchmarks", ] try-runtime = [ @@ -23,8 +24,8 @@ try-runtime = [ "frame-system/try-runtime", "pallet-balances/runtime-benchmarks", "pallet-balances/try-runtime", - "pallet-identity/runtime-benchmarks", "pallet-identity/try-runtime", + "sp-membership/try-runtime", "sp-runtime/try-runtime", ] std = [ @@ -34,6 +35,7 @@ std = [ "frame-system/std", "pallet-balances/std", "pallet-identity/std", + "sp-membership/std", "scale-info/std", "sp-core/std", "sp-io/std", @@ -50,6 +52,7 @@ frame-support = { workspace = true } frame-system = { workspace = true } pallet-balances = { workspace = true } pallet-identity = { workspace = true } +sp-membership = { workspace = true } scale-info = { workspace = true, features = ["derive"] } sp-core = { workspace = true } sp-runtime = { workspace = true } diff --git a/pallets/quota/src/lib.rs b/pallets/quota/src/lib.rs index 9ea7aa21050c7cadcde0a1c40275b0a6dfa3bb03..25782264e959a6cd8d091d4fa0a3060449c59e75 100644 --- a/pallets/quota/src/lib.rs +++ b/pallets/quota/src/lib.rs @@ -360,17 +360,20 @@ impl<T: Config> RefundFee<T> for Pallet<T> { /// Checks if an identity is eligible for a refund. /// -/// This function returns `true` for all identities, regardless of their status. -/// If the identity has no quotas or has been deleted, the refund request is still queued, -/// but when handled, no refund will be issued (and `NoQuotaForIdty` may be raised). -fn is_eligible_for_refund<T: pallet_identity::Config>(_identity: IdtyId<T>) -> bool { - true +/// This function returns `true` only if the identity exists and has a status of `Member`. +/// If the identity does not exist or has a different status, it returns `false`, and the refund request will not be processed. +/// +fn is_eligible_for_refund<T: pallet_identity::Config>(idty_index: IdtyId<T>) -> bool { + pallet_identity::Identities::<T>::get(idty_index).map_or_else( + || false, + |id| id.status == pallet_identity::IdtyStatus::Member, + ) } -/// Implementing the on new identity event handler for the pallet. -impl<T: Config> pallet_identity::traits::OnNewIdty<T> for Pallet<T> { +/// Implementing the on new membership event handler for the pallet. +impl<T: Config> sp_membership::traits::OnNewMembership<IdtyId<T>> for Pallet<T> { /// This implementation initializes the identity quota for the newly created identity. - fn on_created(idty_index: &IdtyId<T>, _creator: &T::IdtyIndex) { + fn on_created(idty_index: &IdtyId<T>) { IdtyQuota::<T>::insert( idty_index, Quota { @@ -379,10 +382,12 @@ impl<T: Config> pallet_identity::traits::OnNewIdty<T> for Pallet<T> { }, ); } + + fn on_renewed(_idty_index: &IdtyId<T>) {} } /// Implementing the on remove identity event handler for the pallet. -impl<T: Config> pallet_identity::traits::OnRemoveIdty<T> for Pallet<T> { +impl<T: Config> sp_membership::traits::OnRemoveMembership<IdtyId<T>> for Pallet<T> { /// This implementation removes the identity quota associated with the removed identity. fn on_removed(idty_id: &IdtyId<T>) -> Weight { let mut weight = Weight::zero(); @@ -394,9 +399,4 @@ impl<T: Config> pallet_identity::traits::OnRemoveIdty<T> for Pallet<T> { add_db_reads_writes(1, 1); weight } - - /// This implementation removes the identity quota associated with the removed identity. - fn on_revoked(idty_id: &IdtyId<T>) -> Weight { - Self::on_removed(idty_id) - } } diff --git a/runtime/common/src/handlers.rs b/runtime/common/src/handlers.rs index 91b791345802eb128cc942ee0e2748495800f150..3fdd7b2d395730e3bb4f45cbb12ae1bc7f3f1817 100644 --- a/runtime/common/src/handlers.rs +++ b/runtime/common/src/handlers.rs @@ -38,33 +38,28 @@ where /// Runtime handler for OnNewIdty, calling all implementations of /// OnNewIdty and implementing logic at the runtime level. pub struct OnNewIdtyHandler<Runtime>(core::marker::PhantomData<Runtime>); -impl<Runtime: pallet_duniter_wot::Config + pallet_quota::Config> - pallet_identity::traits::OnNewIdty<Runtime> for OnNewIdtyHandler<Runtime> +impl<Runtime: pallet_duniter_wot::Config> pallet_identity::traits::OnNewIdty<Runtime> + for OnNewIdtyHandler<Runtime> { fn on_created(idty_index: &IdtyIndex, creator: &IdtyIndex) { pallet_duniter_wot::Pallet::<Runtime>::on_created(idty_index, creator); - pallet_quota::Pallet::<Runtime>::on_created(idty_index, creator); } } /// Runtime handler for OnRemoveIdty, calling all implementations of /// OnRemoveIdty and implementing logic at the runtime level. pub struct OnRemoveIdtyHandler<Runtime>(core::marker::PhantomData<Runtime>); -impl< - Runtime: pallet_duniter_wot::Config + pallet_quota::Config + pallet_duniter_account::Config, - > pallet_identity::traits::OnRemoveIdty<Runtime> for OnRemoveIdtyHandler<Runtime> +impl<Runtime: pallet_duniter_wot::Config + pallet_duniter_account::Config> + pallet_identity::traits::OnRemoveIdty<Runtime> for OnRemoveIdtyHandler<Runtime> { fn on_removed(idty_index: &IdtyIndex) -> Weight { pallet_duniter_wot::Pallet::<Runtime>::on_removed(idty_index) - .saturating_add(pallet_quota::Pallet::<Runtime>::on_removed(idty_index)) } fn on_revoked(idty_index: &IdtyIndex) -> Weight { - pallet_duniter_wot::Pallet::<Runtime>::on_revoked(idty_index) - .saturating_add(pallet_duniter_account::Pallet::<Runtime>::on_revoked( - idty_index, - )) - .saturating_add(pallet_quota::Pallet::<Runtime>::on_revoked(idty_index)) + pallet_duniter_wot::Pallet::<Runtime>::on_revoked(idty_index).saturating_add( + pallet_duniter_account::Pallet::<Runtime>::on_revoked(idty_index), + ) } } @@ -75,13 +70,16 @@ impl< Runtime: frame_system::Config<AccountId = AccountId> + pallet_identity::Config<IdtyData = IdtyData, IdtyIndex = IdtyIndex> + pallet_duniter_wot::Config - + pallet_universal_dividend::Config, + + pallet_universal_dividend::Config + + pallet_quota::Config, > sp_membership::traits::OnNewMembership<IdtyIndex> for OnNewMembershipHandler<Runtime> { fn on_created(idty_index: &IdtyIndex) { // duniter-wot related actions pallet_duniter_wot::Pallet::<Runtime>::on_created(idty_index); + pallet_quota::Pallet::<Runtime>::on_created(idty_index); + // When main membership is acquired, it starts getting right to UD. pallet_identity::Identities::<Runtime>::mutate_exists(idty_index, |idty_val_opt| { if let Some(ref mut idty_val) = idty_val_opt { @@ -107,6 +105,7 @@ impl< + pallet_identity::Config<IdtyData = IdtyData, IdtyIndex = IdtyIndex> + pallet_smith_members::Config<IdtyIndex = IdtyIndex> + pallet_duniter_wot::Config + + pallet_quota::Config + pallet_universal_dividend::Config, > sp_membership::traits::OnRemoveMembership<IdtyIndex> for OnRemoveMembershipHandler<Runtime> { @@ -127,7 +126,8 @@ impl< } } }); - weight += Runtime::DbWeight::get().reads_writes(1, 1); + weight.saturating_add(pallet_quota::Pallet::<Runtime>::on_removed(idty_index)); + weight.saturating_add(Runtime::DbWeight::get().reads_writes(1, 1)); // When membership is removed, also remove from smith member. weight.saturating_add(