Skip to content
Merged
Changes from 1 commit
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
20 changes: 19 additions & 1 deletion substrate/frame/support/src/traits/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ impl Footprint {
pub fn from_encodable(e: impl Encode) -> Self {
Self::from_parts(1, e.encoded_size())
}

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 +292,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 +310,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 });
}
}