substrate-docs icon indicating copy to clipboard operation
substrate-docs copied to clipboard

Storage requirements using `BoundedVec` by default

Open sacha-l opened this issue 3 years ago • 0 comments

We need to document the change in Substrate that's just been merged: https://github.com/paritytech/substrate/pull/10662

Pallets require that all vectors in runtime storage are bounded, meaning that they must use the BoundedVec type and specify a max value constant. For example:

	/// The description of each child-bounty.
	#[pallet::storage]
	#[pallet::getter(fn child_bounty_descriptions)]
	pub type ChildBountyDescriptions<T: Config> =
		StorageMap<_, Twox64Concat, BountyIndex, BoundedVec<u8, T::MaximumReasonLength>>;

To override this default behaviour, developers must add the without_storage_info attribute to their pallets:

	#[pallet::pallet]
	#[pallet::generate_store(pub(super) trait Store)]
	#[pallet::without_storage_info] 
	pub struct Pallet<T>(_);

Bounded storage items is important because if any Vec gets too big it can lead to:

  • Running out of wasm memory loading that vec for a solo chain / relay chain (which happened with staking on Polkadot a while back)
  • Running out of PoV space for a parachain validation block (only 5mb available per block)

Just like computation, memory / storage space is also a bottleneck for blockchains. For relay chains, bounded vectors are used to compute the PoV proof size and to ensure that the size of a proof-of-membership set in a vector doesn't grow unbounded. Users can still opt out of writing things bounded using the without_storage_info attribute. However they are encouraged by the compiler to ensure their storage is bounded.

sacha-l avatar Jan 20 '22 12:01 sacha-l