This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
implement BoundedEncodedLen #8720
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
dff9c4a
implement BoundedEncodedLen
coriolinus 10d65a7
update header
coriolinus 5a8f30a
update imports
coriolinus 0204e2f
use impl_for_tuples instead of a custom macro
coriolinus 3703843
remove redundant where clause
coriolinus d249f57
impl for Compact<T>
coriolinus 91c31c5
Merge branch 'prgn-boundedencodedlen' of github.com:paritytech/substr…
coriolinus 13f22c0
impl BoundedEncodedLen for BoundedVec (#8727)
coriolinus 7d472bd
impl BoundedEncodedLen for bool
coriolinus f18183a
explicitly implement BoundedEncodedLen for each Compact form
coriolinus af0b814
rename BoundedEncodedLen -> MaxEncodedLen
coriolinus 8b8576d
add tests of compact encoded length
coriolinus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| // This file is part of Substrate. | ||
|
|
||
| // Copyright (C) 2019-2021 Parity Technologies (UK) Ltd. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use codec::{Compact, Encode}; | ||
| use impl_trait_for_tuples::impl_for_tuples; | ||
| use sp_std::{mem, marker::PhantomData}; | ||
|
|
||
| /// Items implementing `MaxEncodedLen` have a statically known maximum encoded size. | ||
| /// | ||
| /// Some containers, such as `BoundedVec`, have enforced size limits and this trait | ||
| /// can be implemented accurately. Other containers, such as `StorageMap`, do not have enforced size | ||
| /// limits. For those containers, it is necessary to make a documented assumption about the maximum | ||
| /// usage, and compute the max encoded length based on that assumption. | ||
| pub trait MaxEncodedLen: Encode { | ||
| /// Upper bound, in bytes, of the maximum encoded size of this item. | ||
| fn max_encoded_len() -> usize; | ||
| } | ||
|
|
||
| macro_rules! impl_primitives { | ||
| ( $($t:ty),+ ) => { | ||
| $( | ||
| impl MaxEncodedLen for $t { | ||
| fn max_encoded_len() -> usize { | ||
| mem::size_of::<$t>() | ||
| } | ||
| } | ||
| )+ | ||
| }; | ||
| } | ||
|
|
||
| impl_primitives!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, bool); | ||
|
|
||
| macro_rules! impl_compact { | ||
| ($( $t:ty => $e:expr; )*) => { | ||
| $( | ||
| impl MaxEncodedLen for Compact<$t> { | ||
| fn max_encoded_len() -> usize { | ||
| $e | ||
| } | ||
| } | ||
| )* | ||
| }; | ||
| } | ||
|
|
||
| impl_compact!( | ||
| // https://github.com/paritytech/parity-scale-codec/blob/f0341dabb01aa9ff0548558abb6dcc5c31c669a1/src/compact.rs#L261 | ||
| u8 => 2; | ||
| // https://github.com/paritytech/parity-scale-codec/blob/f0341dabb01aa9ff0548558abb6dcc5c31c669a1/src/compact.rs#L291 | ||
| u16 => 4; | ||
| // https://github.com/paritytech/parity-scale-codec/blob/f0341dabb01aa9ff0548558abb6dcc5c31c669a1/src/compact.rs#L326 | ||
| u32 => 5; | ||
| // https://github.com/paritytech/parity-scale-codec/blob/f0341dabb01aa9ff0548558abb6dcc5c31c669a1/src/compact.rs#L369 | ||
| u64 => 9; | ||
| // https://github.com/paritytech/parity-scale-codec/blob/f0341dabb01aa9ff0548558abb6dcc5c31c669a1/src/compact.rs#L413 | ||
| u128 => 17; | ||
| ); | ||
|
|
||
| // impl_for_tuples for values 19 and higher fails because that's where the WrapperTypeEncode impl stops. | ||
| #[impl_for_tuples(18)] | ||
| impl MaxEncodedLen for Tuple { | ||
| fn max_encoded_len() -> usize { | ||
| let mut len: usize = 0; | ||
| for_tuples!( #( len = len.saturating_add(Tuple::max_encoded_len()); )* ); | ||
| len | ||
| } | ||
| } | ||
|
|
||
| impl<T: MaxEncodedLen, const N: usize> MaxEncodedLen for [T; N] { | ||
| fn max_encoded_len() -> usize { | ||
| T::max_encoded_len().saturating_mul(N) | ||
| } | ||
| } | ||
|
|
||
| impl<T: MaxEncodedLen> MaxEncodedLen for Option<T> { | ||
| fn max_encoded_len() -> usize { | ||
| T::max_encoded_len().saturating_add(1) | ||
| } | ||
| } | ||
|
|
||
| impl<T, E> MaxEncodedLen for Result<T, E> | ||
| where | ||
| T: MaxEncodedLen, | ||
| E: MaxEncodedLen, | ||
| { | ||
| fn max_encoded_len() -> usize { | ||
| T::max_encoded_len().max(E::max_encoded_len()).saturating_add(1) | ||
| } | ||
| } | ||
|
|
||
| impl<T> MaxEncodedLen for PhantomData<T> { | ||
| fn max_encoded_len() -> usize { | ||
| 0 | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| macro_rules! test_compact_length { | ||
| ($(fn $name:ident($t:ty);)*) => { | ||
| $( | ||
| #[test] | ||
| fn $name() { | ||
| assert_eq!(Compact(<$t>::MAX).encode().len(), Compact::<$t>::max_encoded_len()); | ||
| } | ||
| )* | ||
| }; | ||
| } | ||
|
|
||
| test_compact_length!( | ||
| fn compact_u8(u8); | ||
| fn compact_u16(u16); | ||
| fn compact_u32(u32); | ||
| fn compact_u64(u64); | ||
| fn compact_u128(u128); | ||
| ); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the least significant comment: pity not to see
SomeTrait { fn some_trait() }, why nottrait MaxEncodedLen?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No reason really; I like
MaxEncodedLenbecause it's shorter and agrees with the trait's function. af0b814