Skip to content
Snippets Groups Projects

Replace bool by Result<(), dispatchError> for checks

Merged Hugo Trentesaux requested to merge hugo-issue-90 into master
Files
4
@@ -92,6 +92,18 @@ pub mod pallet {
@@ -92,6 +92,18 @@ pub mod pallet {
true
true
}
}
}
}
 
 
// ERRORS //
 
 
#[pallet::error]
 
pub enum Error<T, I = ()> {
 
/// Identity creation period not respected
 
IdtyCreationPeriodNotRespected,
 
/// Not enough received certifications to create identity
 
NotEnoughReceivedCertsToCreateIdty,
 
/// Max number of emited certs reached
 
MaxEmitedCertsReached,
 
}
}
}
impl<AccountId, T: Config<I>, I: 'static> pallet_identity::traits::EnsureIdtyCallAllowed<T>
impl<AccountId, T: Config<I>, I: 'static> pallet_identity::traits::EnsureIdtyCallAllowed<T>
@@ -99,40 +111,59 @@ impl<AccountId, T: Config<I>, I: 'static> pallet_identity::traits::EnsureIdtyCal
@@ -99,40 +111,59 @@ impl<AccountId, T: Config<I>, I: 'static> pallet_identity::traits::EnsureIdtyCal
where
where
T: frame_system::Config<AccountId = AccountId> + pallet_membership::Config<I>,
T: frame_system::Config<AccountId = AccountId> + pallet_membership::Config<I>,
{
{
fn can_create_identity(creator: IdtyIndex) -> bool {
fn check_create_identity(creator: IdtyIndex) -> Result<(), DispatchError> {
if !T::IsSubWot::get() {
if !T::IsSubWot::get() {
let cert_meta = pallet_certification::Pallet::<T, I>::idty_cert_meta(creator);
let cert_meta = pallet_certification::Pallet::<T, I>::idty_cert_meta(creator);
cert_meta.received_count >= T::MinCertForCreateIdtyRight::get()
&& cert_meta.next_issuable_on <= frame_system::pallet::Pallet::<T>::block_number()
// perform all checks
&& cert_meta.issued_count < T::MaxByIssuer::get()
ensure!(
 
cert_meta.received_count >= T::MinCertForCreateIdtyRight::get(),
 
Error::<T, I>::NotEnoughReceivedCertsToCreateIdty
 
);
 
ensure!(
 
cert_meta.issued_count < T::MaxByIssuer::get(),
 
Error::<T, I>::MaxEmitedCertsReached
 
);
 
ensure!(
 
cert_meta.next_issuable_on <= frame_system::pallet::Pallet::<T>::block_number(),
 
Error::<T, I>::IdtyCreationPeriodNotRespected
 
);
 
 
// all checks passed
 
Ok(())
} else {
} else {
true
// sub wot can always create identity (why?)
 
Ok(())
}
}
}
}
fn can_confirm_identity(idty_index: IdtyIndex) -> bool {
fn check_confirm_identity(idty_index: IdtyIndex) -> Result<(), DispatchError> {
if !T::IsSubWot::get() {
if !T::IsSubWot::get() {
pallet_membership::Pallet::<T, I>::force_request_membership(
match pallet_membership::Pallet::<T, I>::force_request_membership(
RawOrigin::Root.into(),
RawOrigin::Root.into(),
idty_index,
idty_index,
Default::default(),
Default::default(),
)
) {
.is_ok()
Ok(_) => Ok(()),
 
Err(e) => Err(e.error), // return error from inner function
 
}
} else {
} else {
true
Ok(())
}
}
}
}
fn can_validate_identity(idty_index: IdtyIndex) -> bool {
fn check_validate_identity(idty_index: IdtyIndex) -> Result<(), DispatchError> {
if !T::IsSubWot::get() {
if !T::IsSubWot::get() {
// TODO replace this code by the commented one for distance feature
// TODO replace this code by the commented one for distance feature
/*let idty_cert_meta = pallet_certification::Pallet::<T, I>::idty_cert_meta(idty_index);
/*let idty_cert_meta = pallet_certification::Pallet::<T, I>::idty_cert_meta(idty_index);
idty_cert_meta.received_count >= T::MinCertForMembership::get() as u32*/
idty_cert_meta.received_count >= T::MinCertForMembership::get() as u32*/
pallet_membership::Pallet::<T, I>::claim_membership(
match pallet_membership::Pallet::<T, I>::claim_membership(
RawOrigin::Root.into(),
RawOrigin::Root.into(),
Some(idty_index),
Some(idty_index),
)
) {
.is_ok()
Ok(_) => Ok(()),
 
Err(e) => Err(e.error),
 
}
} else {
} else {
true
Ok(())
}
}
}
}
fn can_change_identity_address(idty_index: IdtyIndex) -> bool {
fn can_change_identity_address(idty_index: IdtyIndex) -> bool {
Loading