Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add from_mel for Footprint with a Test #5132

Merged
merged 8 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
ggwpez marked this conversation as resolved.
Show resolved Hide resolved
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 sp_core::{ConstU32, ConstU64};
use crate::BoundedVec;

#[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 });
}
}
Loading