Skip to content

Commit 34bfd70

Browse files
committed
- Overhauled the set_up_application function in the Gated Marketplace pallet. Removed the separate validate_fields function and integrated its validation logic directly into set_up_application, providing field validation in the same context as application setup. This resulted in more streamlined validation, ensuring a non-empty fields list and correct matching of fields and custodian fields sizes directly in the application setup. Added error handling for the case that the number of application fields exceeds the maximum limit.
- Adjusted the Gated Marketplace pallet structure and error definitions. Added the `ExceedMaxFilesApplication` error to handle scenarios where the number of application files surpasses the allowable maximum. This new error is now part of the error returns in the `enroll_application` and `enroll_self_managed_application` functions, improving the robustness and user feedback of our enrollment operations.
1 parent e89fe52 commit 34bfd70

File tree

2 files changed

+17
-28
lines changed

2 files changed

+17
-28
lines changed

pallets/gated-marketplace/src/functions.rs

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ impl<T: Config> Pallet<T> {
135135
// ensure the origin is owner or admin
136136
Self::is_authorized(authority.clone(), &marketplace_id, Permission::Enroll)?;
137137

138-
Self::validate_fields(&fields, &custodian_fields)?;
139-
let (custodian, fields) = Self::set_up_application(fields, custodian_fields);
138+
let (custodian, fields) = Self::set_up_application(fields, custodian_fields)?;
140139

141140
let application = Application::<T> {
142141
status: ApplicationStatus::default(),
@@ -795,29 +794,12 @@ impl<T: Config> Pallet<T> {
795794
}
796795

797796
/* ---- Helper functions ---- */
798-
pub fn validate_fields(
799-
fields: &Fields<T>,
800-
custodian_fields: &Option<CustodianFields<T>>,
801-
) -> DispatchResult {
802-
match custodian_fields {
803-
Some(c_fields) => {
804-
if fields.len() != c_fields.1.len() {
805-
return Err(Error::<T>::InsufficientCustodianFields.into())
806-
}
807-
}
808-
None => {
809-
if fields.is_empty() {
810-
return Err(Error::<T>::FieldsNotProvided.into())
811-
}
812-
}
813-
}
814-
Ok(())
815-
}
816-
817797
pub fn set_up_application(
818798
fields: Fields<T>,
819799
custodian_fields: Option<CustodianFields<T>>,
820-
) -> (Option<T::AccountId>, BoundedVec<ApplicationField, T::MaxFiles>) {
800+
) -> Result<(Option<T::AccountId>, BoundedVec<ApplicationField, T::MaxFiles>), DispatchError> {
801+
ensure!(!fields.is_empty(), Error::<T>::FieldsNotProvided);
802+
821803
let mut f: Vec<ApplicationField> = fields
822804
.iter()
823805
.map(|tuple| ApplicationField {
@@ -826,16 +808,23 @@ impl<T: Config> Pallet<T> {
826808
custodian_cid: None,
827809
})
828810
.collect();
811+
829812
let custodian = match custodian_fields {
830813
Some(c_fields) => {
814+
if fields.len() != c_fields.1.len() {
815+
return Err(Error::<T>::InsufficientCustodianFields.into())
816+
}
831817
for (i, field) in f.iter_mut().enumerate() {
832818
field.custodian_cid = Some(c_fields.1[i].clone());
833819
}
834820
Some(c_fields.0)
835821
},
836822
_ => None,
837823
};
838-
(custodian, BoundedVec::<ApplicationField, T::MaxFiles>::try_from(f).unwrap_or_default())
824+
825+
let fields = BoundedVec::<ApplicationField, T::MaxFiles>::try_from(f).
826+
map_err(|_| Error::<T>::ExceedMaxFilesApplication)?;
827+
Ok((custodian, fields))
839828
}
840829

841830
fn insert_in_auth_market_lists(

pallets/gated-marketplace/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,9 @@ pub mod pallet {
341341
/// Not enough custodian fields provided
342342
InsufficientCustodianFields,
343343
/// Fields not provided for the application
344-
FieldsNotProvided
344+
FieldsNotProvided,
345+
/// Exceeds the maximum number of files
346+
ExceedMaxFilesApplication,
345347
}
346348

347349
#[pallet::call]
@@ -439,8 +441,7 @@ pub mod pallet {
439441
) -> DispatchResult {
440442
let who = ensure_signed(origin)?;
441443

442-
Self::validate_fields(&fields, &custodian_fields)?;
443-
let (custodian, fields) = Self::set_up_application(fields, custodian_fields);
444+
let (custodian, fields) = Self::set_up_application(fields, custodian_fields)?;
444445

445446
let application = Application::<T> {
446447
status: ApplicationStatus::default(),
@@ -478,8 +479,7 @@ pub mod pallet {
478479
) -> DispatchResult {
479480
let who = ensure_signed(origin)?;
480481

481-
Self::validate_fields(&fields, &custodian_fields)?;
482-
let (custodian, fields) = Self::set_up_application(fields, custodian_fields);
482+
let (custodian, fields) = Self::set_up_application(fields, custodian_fields)?;
483483

484484
let application = Application::<T> {
485485
status: ApplicationStatus::default(),

0 commit comments

Comments
 (0)