Skip to content
Snippets Groups Projects
Commit 81d984eb authored by Éloïs's avatar Éloïs
Browse files

fix(runtime): first_eligible_ud not initialised for post-genesis identities (!98)

* feat(identity): add call force_set_first_eligible_ud

* fix(runtime): first_eligible_ud not init for post-genesis identities

* tests(gdev): add test test_validate_new_idty_after_few_uds
parent 9713e547
No related branches found
No related tags found
1 merge request!98fix(runtime): first_eligible_ud not initialised for post-genesis identities
...@@ -398,6 +398,22 @@ pub mod pallet { ...@@ -398,6 +398,22 @@ pub mod pallet {
) -> DispatchResultWithPostInfo { ) -> DispatchResultWithPostInfo {
Self::do_transfer_ud(origin, dest, value, ExistenceRequirement::KeepAlive) Self::do_transfer_ud(origin, dest, value, ExistenceRequirement::KeepAlive)
} }
#[pallet::weight(T::WeightInfo::force_set_first_eligible_ud())]
pub fn force_set_first_eligible_ud(
origin: OriginFor<T>,
who: T::AccountId,
first_eligible_ud: FirstEligibleUd,
) -> DispatchResultWithPostInfo {
ensure_root(origin)?;
T::MembersStorage::try_mutate_exists(&who, |maybe_first_eligible_ud| {
if let Some(ref mut first_eligible_ud_) = maybe_first_eligible_ud {
*first_eligible_ud_ = first_eligible_ud;
}
Ok(().into())
})
}
} }
// PUBLIC FUNCTIONS // PUBLIC FUNCTIONS
......
...@@ -26,6 +26,7 @@ pub trait WeightInfo { ...@@ -26,6 +26,7 @@ pub trait WeightInfo {
fn claim_uds(n: u32) -> Weight; fn claim_uds(n: u32) -> Weight;
fn transfer_ud() -> Weight; fn transfer_ud() -> Weight;
fn transfer_ud_keep_alive() -> Weight; fn transfer_ud_keep_alive() -> Weight;
fn force_set_first_eligible_ud() -> Weight;
} }
// Insecure weights implementation, use it for tests only! // Insecure weights implementation, use it for tests only!
...@@ -82,4 +83,11 @@ impl WeightInfo for () { ...@@ -82,4 +83,11 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().writes(2 as Weight)) .saturating_add(RocksDbWeight::get().writes(2 as Weight))
} }
// Storage: Identity IdentityIndexOf (r:1 w:0)
// Storage: Identity Identities (r:1 w:1)
fn force_set_first_eligible_ud() -> Weight {
(0 as Weight)
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
} }
...@@ -41,10 +41,21 @@ impl<T> pallet_identity::traits::OnIdtyChange<T> for OnIdtyChangeHandler<T> ...@@ -41,10 +41,21 @@ impl<T> pallet_identity::traits::OnIdtyChange<T> for OnIdtyChangeHandler<T>
where where
T: frame_system::Config<AccountId = AccountId>, T: frame_system::Config<AccountId = AccountId>,
T: pallet_authority_members::Config<MemberId = IdtyIndex>, T: pallet_authority_members::Config<MemberId = IdtyIndex>,
T: pallet_identity::Config<IdtyIndex = IdtyIndex>, T: pallet_identity::Config<IdtyIndex = IdtyIndex, IdtyData = IdtyData>,
T: pallet_universal_dividend::Config,
{ {
fn on_idty_change(idty_index: IdtyIndex, idty_event: &IdtyEvent<T>) -> Weight { fn on_idty_change(idty_index: IdtyIndex, idty_event: &IdtyEvent<T>) -> Weight {
match idty_event { match idty_event {
IdtyEvent::Validated => {
pallet_identity::Identities::<T>::mutate_exists(idty_index, |idty_val_opt| {
if let Some(ref mut idty_val) = idty_val_opt {
idty_val.data = IdtyData {
first_eligible_ud:
pallet_universal_dividend::Pallet::<T>::init_first_eligible_ud(),
}
}
});
}
IdtyEvent::ChangedOwnerKey { new_owner_key } => { IdtyEvent::ChangedOwnerKey { new_owner_key } => {
if let Err(e) = pallet_authority_members::Pallet::<T>::change_owner_key( if let Err(e) = pallet_authority_members::Pallet::<T>::change_owner_key(
idty_index, idty_index,
...@@ -56,10 +67,7 @@ where ...@@ -56,10 +67,7 @@ where
); );
} }
} }
IdtyEvent::Created { .. } IdtyEvent::Created { .. } | IdtyEvent::Confirmed | IdtyEvent::Removed { .. } => {}
| IdtyEvent::Confirmed
| IdtyEvent::Validated
| IdtyEvent::Removed { .. } => {}
} }
0 0
} }
...@@ -80,18 +88,6 @@ impl< ...@@ -80,18 +88,6 @@ impl<
{ {
fn on_event(membership_event: &sp_membership::Event<IdtyIndex, MembershipMetaData>) -> Weight { fn on_event(membership_event: &sp_membership::Event<IdtyIndex, MembershipMetaData>) -> Weight {
(match membership_event { (match membership_event {
sp_membership::Event::MembershipAcquired(idty_index, _owner_key) => {
pallet_identity::Identities::<Runtime>::mutate_exists(idty_index, |idty_val_opt| {
if let Some(ref mut idty_val) = idty_val_opt {
idty_val.data = IdtyData {
first_eligible_ud:
pallet_universal_dividend::Pallet::<Runtime>::init_first_eligible_ud(
),
}
}
});
Runtime::DbWeight::get().reads_writes(1, 1)
}
sp_membership::Event::MembershipRevoked(idty_index) => { sp_membership::Event::MembershipRevoked(idty_index) => {
if let Some(idty_value) = pallet_identity::Identities::<Runtime>::get(idty_index) { if let Some(idty_value) = pallet_identity::Identities::<Runtime>::get(idty_index) {
if let Some(first_ud_index) = idty_value.data.first_eligible_ud.into() { if let Some(first_ud_index) = idty_value.data.first_eligible_ud.into() {
......
...@@ -96,4 +96,11 @@ impl<T: frame_system::Config> pallet_universal_dividend::WeightInfo for WeightIn ...@@ -96,4 +96,11 @@ impl<T: frame_system::Config> pallet_universal_dividend::WeightInfo for WeightIn
.saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight)) .saturating_add(T::DbWeight::get().writes(2 as Weight))
} }
// Storage: Identity IdentityIndexOf (r:1 w:0)
// Storage: Identity Identities (r:1 w:1)
fn force_set_first_eligible_ud() -> Weight {
(57_000_000 as Weight)
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
} }
...@@ -430,3 +430,57 @@ fn test_create_new_idty_without_founds() { ...@@ -430,3 +430,57 @@ fn test_create_new_idty_without_founds() {
); );
}); });
} }
#[test]
fn test_validate_new_idty_after_few_uds() {
ExtBuilder::new(1, 3, 4)
.with_initial_balances(vec![
(AccountKeyring::Alice.to_account_id(), 1_000),
(AccountKeyring::Bob.to_account_id(), 1_000),
(AccountKeyring::Charlie.to_account_id(), 1_000),
(AccountKeyring::Eve.to_account_id(), 1_000),
])
.build()
.execute_with(|| {
run_to_block(21);
// Should be able to create an identity
assert_ok!(Balances::transfer(
frame_system::RawOrigin::Signed(AccountKeyring::Alice.to_account_id()).into(),
MultiAddress::Id(AccountKeyring::Eve.to_account_id()),
200
));
assert_ok!(Identity::create_identity(
frame_system::RawOrigin::Signed(AccountKeyring::Alice.to_account_id()).into(),
AccountKeyring::Eve.to_account_id(),
));
// At next block, the created identity should be confirmed by its owner
run_to_block(22);
assert_ok!(Identity::confirm_identity(
frame_system::RawOrigin::Signed(AccountKeyring::Eve.to_account_id()).into(),
pallet_identity::IdtyName::from("Eve"),
));
// At next block, Bob should be able to certify and validate the new identity
run_to_block(23);
assert_ok!(Cert::add_cert(
frame_system::RawOrigin::Signed(AccountKeyring::Bob.to_account_id()).into(),
2,
5,
));
assert_ok!(Identity::validate_identity(
frame_system::RawOrigin::Signed(AccountKeyring::Bob.to_account_id()).into(),
5,
));
// The new member should have first_eligible_ud equal to one
assert!(Identity::identity(5).is_some());
assert_eq!(
Identity::identity(5).unwrap().data,
IdtyData {
first_eligible_ud: pallet_universal_dividend::FirstEligibleUd::from(3),
}
);
});
}
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