Skip to content

Commit

Permalink
Add from_mel for Footprint with a Test (#5132)
Browse files Browse the repository at this point in the history
We may want to construct a `Footprint` by taking the `max_encoded_len`
of a type, without having to construct the type.

This impl allows easy access to that.

---------

Co-authored-by: Bastian Köcher <[email protected]>
Co-authored-by: command-bot <>
  • Loading branch information
shawntabrizi and bkchr authored Jul 26, 2024
1 parent 6efa1ee commit 90b5533
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
15 changes: 15 additions & 0 deletions prdoc/pr_5132.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: Add `from_mel` for `Footprint`

doc:
- audience: Runtime Dev
description: |
This introduces a new way to generate the `Footprint` type by calculating the max encoded
length of some generic type. This allows you to generate a `Footprint` of a type without
actually constructing that type.

crates:
- name: frame-support
bump: patch
23 changes: 22 additions & 1 deletion substrate/frame/support/src/traits/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,20 @@ pub struct Footprint {
}

impl Footprint {
/// Construct a footprint directly from `items` and `len`.
pub fn from_parts(items: usize, len: usize) -> Self {
Self { count: items as u64, size: len as u64 }
}

/// Construct a footprint with one item, and size equal to the encoded size of `e`.
pub fn from_encodable(e: impl Encode) -> Self {
Self::from_parts(1, e.encoded_size())
}

/// Construct a footprint with one item, and size equal to the max encoded length of `E`.
pub fn from_mel<E: MaxEncodedLen>() -> Self {
Self::from_parts(1, E::max_encoded_len())
}
}

/// A storage price that increases linearly with the number of elements and their size.
Expand Down Expand Up @@ -288,7 +295,8 @@ impl_incrementable!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128);
#[cfg(test)]
mod tests {
use super::*;
use sp_core::ConstU64;
use crate::BoundedVec;
use sp_core::{ConstU32, ConstU64};

#[test]
fn linear_storage_price_works() {
Expand All @@ -305,4 +313,17 @@ mod tests {

assert_eq!(p(u64::MAX, u64::MAX), u64::MAX);
}

#[test]
fn footprint_from_mel_works() {
let footprint = Footprint::from_mel::<(u8, BoundedVec<u8, ConstU32<9>>)>();
let expected_size = BoundedVec::<u8, ConstU32<9>>::max_encoded_len() as u64;
assert_eq!(expected_size, 10);
assert_eq!(footprint, Footprint { count: 1, size: expected_size + 1 });

let footprint = Footprint::from_mel::<(u8, BoundedVec<u8, ConstU32<999>>)>();
let expected_size = BoundedVec::<u8, ConstU32<999>>::max_encoded_len() as u64;
assert_eq!(expected_size, 1001);
assert_eq!(footprint, Footprint { count: 1, size: expected_size + 1 });
}
}

0 comments on commit 90b5533

Please sign in to comment.