Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 9235309

Browse files
authored
Ensure data size of identity pallet is bounded (#9168)
* Ensure data size of identity pallet is bounded * Fix unit tests for identity pallet * Move identity pallet custom types into its own module * Make use of NoBound family traits * Fix identity pallet benchmarks * Enumerate type imports * Properly convert to BoundedVec in benchmarks * Re-export types * Use BoundedVec when storing sub identities * Add generate_storage_info * Manually implement MaxEncodedLen on select types * Use ConstU32 instead of parameter_type * Leverage DefaultNoBound and add some comments * Use max_encoded_len() instead of hardcoded constant * Use MaxEncodedLen in parity-scal-codec * Add get_mut method for WeakBoundedVec * Use expect on an infallible operation * Rewrite as for loop
1 parent 65ac8a5 commit 9235309

File tree

7 files changed

+406
-325
lines changed

7 files changed

+406
-325
lines changed

frame/identity/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ readme = "README.md"
1313
targets = ["x86_64-unknown-linux-gnu"]
1414

1515
[dependencies]
16-
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
16+
codec = { package = "parity-scale-codec", version = "2.2.0", default-features = false, features = ["derive", "max-encoded-len"] }
1717
enumflags2 = { version = "0.6.2" }
1818
sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" }
1919
sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" }

frame/identity/src/benchmarking.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ fn add_registrars<T: Config>(r: u32) -> Result<(), &'static str> {
5656
fn create_sub_accounts<T: Config>(who: &T::AccountId, s: u32) -> Result<Vec<(T::AccountId, Data)>, &'static str> {
5757
let mut subs = Vec::new();
5858
let who_origin = RawOrigin::Signed(who.clone());
59-
let data = Data::Raw(vec![0; 32]);
59+
let data = Data::Raw(vec![0; 32].try_into().unwrap());
6060

6161
for i in 0..s {
6262
let sub_account = account("sub", i, SEED);
@@ -84,11 +84,11 @@ fn add_sub_accounts<T: Config>(who: &T::AccountId, s: u32) -> Result<Vec<(T::Acc
8484

8585
// This creates an `IdentityInfo` object with `num_fields` extra fields.
8686
// All data is pre-populated with some arbitrary bytes.
87-
fn create_identity_info<T: Config>(num_fields: u32) -> IdentityInfo {
88-
let data = Data::Raw(vec![0; 32]);
87+
fn create_identity_info<T: Config>(num_fields: u32) -> IdentityInfo<T::MaxAdditionalFields> {
88+
let data = Data::Raw(vec![0; 32].try_into().unwrap());
8989

9090
let info = IdentityInfo {
91-
additional: vec![(data.clone(), data.clone()); num_fields as usize],
91+
additional: vec![(data.clone(), data.clone()); num_fields as usize].try_into().unwrap(),
9292
display: data.clone(),
9393
legal: data.clone(),
9494
web: data.clone(),
@@ -353,7 +353,7 @@ benchmarks! {
353353
let caller: T::AccountId = whitelisted_caller();
354354
let _ = add_sub_accounts::<T>(&caller, s)?;
355355
let sub = account("new_sub", 0, SEED);
356-
let data = Data::Raw(vec![0; 32]);
356+
let data = Data::Raw(vec![0; 32].try_into().unwrap());
357357
ensure!(SubsOf::<T>::get(&caller).1.len() as u32 == s, "Subs not set.");
358358
}: _(RawOrigin::Signed(caller.clone()), T::Lookup::unlookup(sub), data)
359359
verify {
@@ -365,7 +365,7 @@ benchmarks! {
365365

366366
let caller: T::AccountId = whitelisted_caller();
367367
let (sub, _) = add_sub_accounts::<T>(&caller, s)?.remove(0);
368-
let data = Data::Raw(vec![1; 32]);
368+
let data = Data::Raw(vec![1; 32].try_into().unwrap());
369369
ensure!(SuperOf::<T>::get(&sub).unwrap().1 != data, "data already set");
370370
}: _(RawOrigin::Signed(caller), T::Lookup::unlookup(sub.clone()), data.clone())
371371
verify {
@@ -390,7 +390,7 @@ benchmarks! {
390390
let sup = account("super", 0, SEED);
391391
let _ = add_sub_accounts::<T>(&sup, s)?;
392392
let sup_origin = RawOrigin::Signed(sup).into();
393-
Identity::<T>::add_sub(sup_origin, T::Lookup::unlookup(caller.clone()), Data::Raw(vec![0; 32]))?;
393+
Identity::<T>::add_sub(sup_origin, T::Lookup::unlookup(caller.clone()), Data::Raw(vec![0; 32].try_into().unwrap()))?;
394394
ensure!(SuperOf::<T>::contains_key(&caller), "Sub doesn't exists");
395395
}: _(RawOrigin::Signed(caller.clone()))
396396
verify {

0 commit comments

Comments
 (0)