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

Commit

Permalink
Ensure data size of identity pallet is bounded (#9168)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
KiChjang authored Jul 8, 2021
1 parent 65ac8a5 commit 9235309
Show file tree
Hide file tree
Showing 7 changed files with 406 additions and 325 deletions.
2 changes: 1 addition & 1 deletion frame/identity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ readme = "README.md"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
codec = { package = "parity-scale-codec", version = "2.2.0", default-features = false, features = ["derive", "max-encoded-len"] }
enumflags2 = { version = "0.6.2" }
sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" }
sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" }
Expand Down
14 changes: 7 additions & 7 deletions frame/identity/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn add_registrars<T: Config>(r: u32) -> Result<(), &'static str> {
fn create_sub_accounts<T: Config>(who: &T::AccountId, s: u32) -> Result<Vec<(T::AccountId, Data)>, &'static str> {
let mut subs = Vec::new();
let who_origin = RawOrigin::Signed(who.clone());
let data = Data::Raw(vec![0; 32]);
let data = Data::Raw(vec![0; 32].try_into().unwrap());

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

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

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

let caller: T::AccountId = whitelisted_caller();
let (sub, _) = add_sub_accounts::<T>(&caller, s)?.remove(0);
let data = Data::Raw(vec![1; 32]);
let data = Data::Raw(vec![1; 32].try_into().unwrap());
ensure!(SuperOf::<T>::get(&sub).unwrap().1 != data, "data already set");
}: _(RawOrigin::Signed(caller), T::Lookup::unlookup(sub.clone()), data.clone())
verify {
Expand All @@ -390,7 +390,7 @@ benchmarks! {
let sup = account("super", 0, SEED);
let _ = add_sub_accounts::<T>(&sup, s)?;
let sup_origin = RawOrigin::Signed(sup).into();
Identity::<T>::add_sub(sup_origin, T::Lookup::unlookup(caller.clone()), Data::Raw(vec![0; 32]))?;
Identity::<T>::add_sub(sup_origin, T::Lookup::unlookup(caller.clone()), Data::Raw(vec![0; 32].try_into().unwrap()))?;
ensure!(SuperOf::<T>::contains_key(&caller), "Sub doesn't exists");
}: _(RawOrigin::Signed(caller.clone()))
verify {
Expand Down
Loading

0 comments on commit 9235309

Please sign in to comment.