Skip to content

Commit e97d36e

Browse files
committed
- Introduced a new helper function validate_fields in the Gated Marketplace pallet to validate the fields and custodian fields inputs before setting up an application. It throws an InsufficientCustodianFields error if the lengths of fields and custodian_fields do not match, and a FieldsNotProvided error if custodian fields are absent but fields are present.
- Incorporated the newly-created `validate_fields` helper function into the `create_marketplace` and `enroll_marketplace` functions in the Gated Marketplace pallet's `#[pallet::call]` section. This addition incorporates additional checks and enhances data accuracy. Also introduced a new type of error, `FieldsNotProvided`, in the pallet to handle scenarios where fields are not provided for the application. - Performed a minor cleanup in the RBAC pallet configuration in mock.rs, reordering the fields for better clarity. Swapped the places of `RemoveOrigin` and `WeightInfo` fields to maintain consistent code order. No functionality was changed in this commit.
1 parent f90a8a9 commit e97d36e

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

pallets/gated-marketplace/src/functions.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +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)?;
138139
let (custodian, fields) = Self::set_up_application(fields, custodian_fields);
139140

140141
let application = Application::<T> {
@@ -794,6 +795,24 @@ impl<T: Config> Pallet<T> {
794795
}
795796

796797
/* ---- 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+
}
797816

798817
pub fn set_up_application(
799818
fields: Fields<T>,
@@ -812,7 +831,6 @@ impl<T: Config> Pallet<T> {
812831
for (i, field) in f.iter_mut().enumerate() {
813832
field.custodian_cid = Some(c_fields.1[i].clone());
814833
}
815-
816834
Some(c_fields.0)
817835
},
818836
_ => None,

pallets/gated-marketplace/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,10 @@ pub mod pallet {
338338
OwnerNotInMarketplace,
339339
/// MappedAssetId not found
340340
AssetNotFound,
341+
/// Not enough custodian fields provided
342+
InsufficientCustodianFields,
343+
/// Fields not provided for the application
344+
FieldsNotProvided
341345
}
342346

343347
#[pallet::call]
@@ -435,6 +439,7 @@ pub mod pallet {
435439
) -> DispatchResult {
436440
let who = ensure_signed(origin)?;
437441

442+
Self::validate_fields(&fields, &custodian_fields)?;
438443
let (custodian, fields) = Self::set_up_application(fields, custodian_fields);
439444

440445
let application = Application::<T> {
@@ -473,6 +478,7 @@ pub mod pallet {
473478
) -> DispatchResult {
474479
let who = ensure_signed(origin)?;
475480

481+
Self::validate_fields(&fields, &custodian_fields)?;
476482
let (custodian, fields) = Self::set_up_application(fields, custodian_fields);
477483

478484
let application = Application::<T> {

pallets/gated-marketplace/src/mock.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,24 +163,25 @@ impl pallet_balances::Config for Test {
163163
}
164164

165165
parameter_types! {
166-
pub const MaxScopesPerPallet: u32 = 2;
167-
pub const MaxRolesPerPallet: u32 = 6;
168-
pub const RoleMaxLen: u32 = 25;
169-
pub const PermissionMaxLen: u32 = 25;
170-
pub const MaxPermissionsPerRole: u32 = 30;
171-
pub const MaxRolesPerUser: u32 = 2;
172-
pub const MaxUsersPerRole: u32 = 2;
166+
pub const MaxScopesPerPallet: u32 = 2;
167+
pub const MaxRolesPerPallet: u32 = 6;
168+
pub const RoleMaxLen: u32 = 25;
169+
pub const PermissionMaxLen: u32 = 25;
170+
pub const MaxPermissionsPerRole: u32 = 30;
171+
pub const MaxRolesPerUser: u32 = 2;
172+
pub const MaxUsersPerRole: u32 = 2;
173173
}
174174
impl pallet_rbac::Config for Test {
175175
type RuntimeEvent = RuntimeEvent;
176+
type RemoveOrigin = EnsureRoot<Self::AccountId>;
176177
type MaxScopesPerPallet = MaxScopesPerPallet;
177178
type MaxRolesPerPallet = MaxRolesPerPallet;
178179
type RoleMaxLen = RoleMaxLen;
179180
type PermissionMaxLen = PermissionMaxLen;
180181
type MaxPermissionsPerRole = MaxPermissionsPerRole;
181182
type MaxRolesPerUser = MaxRolesPerUser;
182183
type MaxUsersPerRole = MaxUsersPerRole;
183-
type RemoveOrigin = EnsureRoot<Self::AccountId>;
184+
type WeightInfo = ();
184185
}
185186

186187
// Build genesis storage according to the mock runtime.

0 commit comments

Comments
 (0)