Make BoundedVec
Size Configurable
Severity: Medium
Description
In Substrate, using a fixed size for BoundedVec
restricts flexibility and makes it harder to adapt to changing requirements. A hardcoded size is often not sufficient as the blockchain evolves, requiring updates that may lead to redeployments and governance interventions. By making the size configurable through the Config
trait, developers can enhance maintainability and adaptability, enabling runtime-specific adjustments without altering the core pallet logic.
What should be avoided
Using a fixed size for BondedVec
without a configurable option restricts adaptability:
#![allow(unused)] fn main() { // Hardcoded size #[pallet::storage] pub type Domain = BondedVec<u8, ConstU32<256>>; }
Best practice
Define the size as a configurable parameter within the Config
trait, which provides flexibility for future changes:
#![allow(unused)] fn main() { // --- In pallet/lib.rs file ---- #[pallet::config] pub trait Config: frame_system::Config { type MaxDomainSize: Get<u32>; } #[pallet::storage] pub type Domain<T> = BoundedVec<u8, T::MaxDomainSize>; // --- In runtime/lib.rs file --- impl some_pallet::Config for Runtime { ... type MaxDomainSize = ConstU32<256>; } }
This approach allows the MaxDomainSize
to be defined in the runtime configuration, making the code adaptable and easier to maintain.