Skip to content

[Bad Example] Process up to a maximum number of elements #12

@shawntabrizi

Description

@shawntabrizi

https://libro.blockdeep.dev/critical/Avoid_Unbounded_Iteration.html

The example here isn't that great:

const MAX_ITEMS: usize = 20;

#[pallet::storage]
pub type UnboundedData<T: Config> = StorageValue<_, Vec<u32>;

let big_data = UnboundedData::<T>::get();
for item in big_data.iter().take(MAX_ITEMS) {
    // Process a limited number of items safely
}

In this example, you are reading an unbounded storage item which itself is bad practice.

The better example here would perhaps be an imput to the extrinsic from the user, which is a Vec, and ensuring the length of the vec is within some limit.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions