mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-27 01:07:57 +00:00
Simple MaxBoundedLen Implementations (#8793)
* implement max_values + storages info * some formatting + doc * sudo sanity check * timestamp * assets (not working) * fix assets * impl for proxy * update balances * rename StoragesInfo -> PalletStorageInfo * merge both StorageInfoTrait and PalletStorageInfo I think it is more future proof. In the future some storage could make use of multiple prefix. Like one to store how much value has been inserted, etc... * Update frame/support/procedural/src/storage/parse.rs Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com> * Update frame/support/procedural/src/storage/storage_struct.rs Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com> * Fix max_size using hasher information hasher now expose `max_len` which allows to computes their maximum len. For hasher without concatenation, it is the size of the hash part, for hasher with concatenation, it is the size of the hash part + max encoded len of the key. * fix tests * fix ui tests * Move `MaxBoundedLen` into its own crate (#8814) * move MaxEncodedLen into its own crate * remove MaxEncodedLen impl from frame-support * add to assets and balances * try more fixes * fix compile Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com> * nits * fix compile * line width * fix max-values-macro merge * Add some derive, needed for test and other purpose * use weak bounded vec in some cases * Update lib.rs * move max-encoded-len crate * fix * remove app crypto for now * width * Revert "remove app crypto for now" This reverts commit 73623e9933d50648e0e7fe90b6171a8e45d7f5a2. * unused variable * more unused variables * more fixes * Add #[max_encoded_len_crate(...)] helper attribute The purpose of this attribute is to reduce the surface area of max_encoded_len changes. Crates deriving `MaxEncodedLen` do not need to add it to `Cargo.toml`; they can instead just do ```rust \#[derive(Encode, MaxEncodedLen)] \#[max_encoded_len_crate(frame_support::max_encoded_len)] struct Example; ``` * fix a ui test * use #[max_encoded_len_crate(...)] helper in app_crypto * remove max_encoded_len import where not necessary * update lockfile * fix ui test * ui * newline * fix merge * try fix ui again * Update max-encoded-len/derive/src/lib.rs Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com> * extract generate_crate_access_2018 * Update lib.rs * compiler isnt smart enough Co-authored-by: thiolliere <gui.thiolliere@gmail.com> Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com> Co-authored-by: Peter Goodspeed-Niklaus <peter.r.goodspeedniklaus@gmail.com>
This commit is contained in:
@@ -76,7 +76,7 @@ pub use self::hash::{
|
||||
pub use self::storage::{
|
||||
StorageValue, StorageMap, StorageDoubleMap, StorageNMap, StoragePrefixedMap,
|
||||
IterableStorageMap, IterableStorageDoubleMap, IterableStorageNMap, migration,
|
||||
bounded_vec::{self, BoundedVec},
|
||||
bounded_vec::BoundedVec, weak_bounded_vec::WeakBoundedVec,
|
||||
};
|
||||
pub use self::dispatch::{Parameter, Callable};
|
||||
pub use sp_runtime::{self, ConsensusEngineId, print, traits::Printable};
|
||||
@@ -1239,7 +1239,7 @@ pub mod pallet_prelude {
|
||||
RuntimeDebug, storage,
|
||||
traits::{
|
||||
Get, Hooks, IsType, GetPalletVersion, EnsureOrigin, PalletInfoAccess, StorageInfoTrait,
|
||||
ConstU32, GetDefault,
|
||||
ConstU32, GetDefault, MaxEncodedLen,
|
||||
},
|
||||
dispatch::{DispatchResultWithPostInfo, Parameter, DispatchError, DispatchResult},
|
||||
weights::{DispatchClass, Pays, Weight},
|
||||
@@ -2339,3 +2339,7 @@ pub mod pallet_prelude {
|
||||
/// * use the newest nightly possible.
|
||||
///
|
||||
pub use frame_support_procedural::pallet;
|
||||
|
||||
/// The `max_encoded_len` module contains the `MaxEncodedLen` trait and derive macro, which is
|
||||
/// useful for computing upper bounds on storage size.
|
||||
pub use max_encoded_len;
|
||||
|
||||
@@ -81,33 +81,5 @@ pub use dispatch::{EnsureOrigin, OriginTrait, UnfilteredDispatchable};
|
||||
mod voting;
|
||||
pub use voting::{CurrencyToVote, SaturatingCurrencyToVote, U128CurrencyToVote};
|
||||
|
||||
mod max_encoded_len;
|
||||
// This looks like an overlapping import/export, but it isn't:
|
||||
// macros and traits live in distinct namespaces.
|
||||
// for backwards-compatibility with existing imports
|
||||
pub use max_encoded_len::MaxEncodedLen;
|
||||
/// Derive [`MaxEncodedLen`][max_encoded_len::MaxEncodedLen].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # use codec::Encode;
|
||||
/// # use frame_support::traits::MaxEncodedLen;
|
||||
/// #[derive(Encode, MaxEncodedLen)]
|
||||
/// struct TupleStruct(u8, u32);
|
||||
///
|
||||
/// assert_eq!(TupleStruct::max_encoded_len(), u8::max_encoded_len() + u32::max_encoded_len());
|
||||
/// ```
|
||||
///
|
||||
/// ```
|
||||
/// # use codec::Encode;
|
||||
/// # use frame_support::traits::MaxEncodedLen;
|
||||
/// #[derive(Encode, MaxEncodedLen)]
|
||||
/// enum GenericEnum<T> {
|
||||
/// A,
|
||||
/// B(T),
|
||||
/// }
|
||||
///
|
||||
/// assert_eq!(GenericEnum::<u8>::max_encoded_len(), u8::max_encoded_len() + u8::max_encoded_len());
|
||||
/// assert_eq!(GenericEnum::<u128>::max_encoded_len(), u8::max_encoded_len() + u128::max_encoded_len());
|
||||
/// ```
|
||||
pub use frame_support_procedural::MaxEncodedLen;
|
||||
|
||||
@@ -1,132 +0,0 @@
|
||||
// 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);
|
||||
);
|
||||
}
|
||||
@@ -22,7 +22,7 @@ use sp_runtime::traits::MaybeSerializeDeserialize;
|
||||
use crate::dispatch::{DispatchResult, DispatchError};
|
||||
use super::misc::{Balance, WithdrawReasons, ExistenceRequirement};
|
||||
use super::imbalance::{Imbalance, SignedImbalance};
|
||||
|
||||
use frame_support::traits::MaxEncodedLen;
|
||||
|
||||
mod reservable;
|
||||
pub use reservable::ReservableCurrency;
|
||||
@@ -32,7 +32,7 @@ pub use lockable::{LockableCurrency, VestingSchedule, LockIdentifier};
|
||||
/// Abstraction over a fungible assets system.
|
||||
pub trait Currency<AccountId> {
|
||||
/// The balance of an account.
|
||||
type Balance: Balance + MaybeSerializeDeserialize + Debug;
|
||||
type Balance: Balance + MaybeSerializeDeserialize + Debug + MaxEncodedLen;
|
||||
|
||||
/// The opaque token type for an imbalance. This is returned by unbalanced operations
|
||||
/// and must be dealt with. It may be dropped but cannot be cloned.
|
||||
|
||||
Reference in New Issue
Block a user