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:
Shawn Tabrizi
2021-05-28 14:35:15 -04:00
committed by GitHub
parent 61859bbdb1
commit 444d4e204c
59 changed files with 619 additions and 189 deletions
+2
View File
@@ -22,6 +22,7 @@ frame-support = { version = "3.0.0", default-features = false, path = "../suppor
# `system` module provides us with all sorts of useful stuff and macros depend on it being around.
frame-system = { version = "3.0.0", default-features = false, path = "../system" }
frame-benchmarking = { version = "3.1.0", default-features = false, path = "../benchmarking", optional = true }
max-encoded-len = { version = "3.0.0", default-features = false, path = "../../max-encoded-len", features = [ "derive" ] }
[dev-dependencies]
sp-core = { version = "3.0.0", path = "../../primitives/core" }
@@ -38,6 +39,7 @@ std = [
"frame-support/std",
"frame-system/std",
"frame-benchmarking/std",
"max-encoded-len/std",
]
runtime-benchmarks = [
"frame-benchmarking",
+36 -15
View File
@@ -138,14 +138,15 @@ mod functions;
mod types;
pub use types::*;
use sp_std::{prelude::*, borrow::Borrow};
use sp_std::{prelude::*, borrow::Borrow, convert::TryInto};
use sp_runtime::{
RuntimeDebug, TokenError, ArithmeticError, traits::{
TokenError, ArithmeticError,
traits::{
AtLeast32BitUnsigned, Zero, StaticLookup, Saturating, CheckedSub, CheckedAdd, Bounded,
StoredMapError,
}
};
use codec::{Encode, Decode, HasCompact};
use codec::HasCompact;
use frame_support::{ensure, dispatch::{DispatchError, DispatchResult}};
use frame_support::traits::{Currency, ReservableCurrency, BalanceStatus::Reserved, StoredMap};
use frame_support::traits::tokens::{WithdrawConsequence, DepositConsequence, fungibles};
@@ -165,6 +166,7 @@ pub mod pallet {
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::generate_storage_info]
pub struct Pallet<T, I = ()>(_);
#[pallet::config]
@@ -174,10 +176,10 @@ pub mod pallet {
type Event: From<Event<Self, I>> + IsType<<Self as frame_system::Config>::Event>;
/// The units in which we record balances.
type Balance: Member + Parameter + AtLeast32BitUnsigned + Default + Copy;
type Balance: Member + Parameter + AtLeast32BitUnsigned + Default + Copy + MaxEncodedLen;
/// Identifier for the class of asset.
type AssetId: Member + Parameter + Default + Copy + HasCompact;
type AssetId: Member + Parameter + Default + Copy + HasCompact + MaxEncodedLen;
/// The currency mechanism.
type Currency: ReservableCurrency<Self::AccountId>;
@@ -207,7 +209,7 @@ pub mod pallet {
type Freezer: FrozenBalance<Self::AssetId, Self::AccountId, Self::Balance>;
/// Additional data to be stored with an account's asset balance.
type Extra: Member + Parameter + Default;
type Extra: Member + Parameter + Default + MaxEncodedLen;
/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
@@ -232,6 +234,8 @@ pub mod pallet {
T::AccountId,
AssetBalance<T::Balance, T::Extra>,
ValueQuery,
GetDefault,
ConstU32<300_000>,
>;
#[pallet::storage]
@@ -247,6 +251,8 @@ pub mod pallet {
),
Approval<T::Balance, DepositBalanceOf<T, I>>,
OptionQuery,
GetDefault,
ConstU32<300_000>,
>;
#[pallet::storage]
@@ -255,8 +261,10 @@ pub mod pallet {
_,
Blake2_128Concat,
T::AssetId,
AssetMetadata<DepositBalanceOf<T, I>>,
AssetMetadata<DepositBalanceOf<T, I>, BoundedVec<u8, T::StringLimit>>,
ValueQuery,
GetDefault,
ConstU32<300_000>,
>;
#[pallet::event]
@@ -899,8 +907,14 @@ pub mod pallet {
) -> DispatchResult {
let origin = ensure_signed(origin)?;
ensure!(name.len() <= T::StringLimit::get() as usize, Error::<T, I>::BadMetadata);
ensure!(symbol.len() <= T::StringLimit::get() as usize, Error::<T, I>::BadMetadata);
let bounded_name: BoundedVec<u8, T::StringLimit> = name
.clone()
.try_into()
.map_err(|_| Error::<T, I>::BadMetadata)?;
let bounded_symbol: BoundedVec<u8, T::StringLimit> = symbol
.clone()
.try_into()
.map_err(|_| Error::<T, I>::BadMetadata)?;
let d = Asset::<T, I>::get(id).ok_or(Error::<T, I>::Unknown)?;
ensure!(&origin == &d.owner, Error::<T, I>::NoPermission);
@@ -924,8 +938,8 @@ pub mod pallet {
*metadata = Some(AssetMetadata {
deposit: new_deposit,
name: name.clone(),
symbol: symbol.clone(),
name: bounded_name,
symbol: bounded_symbol,
decimals,
is_frozen: false,
});
@@ -989,16 +1003,23 @@ pub mod pallet {
) -> DispatchResult {
T::ForceOrigin::ensure_origin(origin)?;
ensure!(name.len() <= T::StringLimit::get() as usize, Error::<T, I>::BadMetadata);
ensure!(symbol.len() <= T::StringLimit::get() as usize, Error::<T, I>::BadMetadata);
let bounded_name: BoundedVec<u8, T::StringLimit> = name
.clone()
.try_into()
.map_err(|_| Error::<T, I>::BadMetadata)?;
let bounded_symbol: BoundedVec<u8, T::StringLimit> = symbol
.clone()
.try_into()
.map_err(|_| Error::<T, I>::BadMetadata)?;
ensure!(Asset::<T, I>::contains_key(id), Error::<T, I>::Unknown);
Metadata::<T, I>::try_mutate_exists(id, |metadata| {
let deposit = metadata.take().map_or(Zero::zero(), |m| m.deposit);
*metadata = Some(AssetMetadata {
deposit,
name: name.clone(),
symbol: symbol.clone(),
name: bounded_name,
symbol: bounded_symbol,
decimals,
is_frozen,
});
+9 -8
View File
@@ -18,11 +18,12 @@
//! Various basic types for use in the assets pallet.
use super::*;
use frame_support::pallet_prelude::*;
pub(super) type DepositBalanceOf<T, I = ()> =
<<T as Config<I>>::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance;
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug)]
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, MaxEncodedLen)]
pub struct AssetDetails<
Balance,
AccountId,
@@ -66,7 +67,7 @@ impl<Balance, AccountId, DepositBalance> AssetDetails<Balance, AccountId, Deposi
}
/// Data concerning an approval.
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, Default)]
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, Default, MaxEncodedLen)]
pub struct Approval<Balance, DepositBalance> {
/// The amount of funds approved for the balance transfer from the owner to some delegated
/// target.
@@ -75,7 +76,7 @@ pub struct Approval<Balance, DepositBalance> {
pub(super) deposit: DepositBalance,
}
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, Default)]
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, Default, MaxEncodedLen)]
pub struct AssetBalance<Balance, Extra> {
/// The balance.
pub(super) balance: Balance,
@@ -87,16 +88,16 @@ pub struct AssetBalance<Balance, Extra> {
pub(super) extra: Extra,
}
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, Default)]
pub struct AssetMetadata<DepositBalance> {
#[derive(Clone, Encode, Decode, Eq, PartialEq, Default, RuntimeDebug, MaxEncodedLen)]
pub struct AssetMetadata<DepositBalance, BoundedString> {
/// The balance deposited for this metadata.
///
/// This pays for the data stored in this struct.
pub(super) deposit: DepositBalance,
/// The user friendly name of this asset. Limited in length by `StringLimit`.
pub(super) name: Vec<u8>,
pub(super) name: BoundedString,
/// The ticker symbol for this asset. Limited in length by `StringLimit`.
pub(super) symbol: Vec<u8>,
pub(super) symbol: BoundedString,
/// The number of decimals this asset uses to represent one unit.
pub(super) decimals: u8,
/// Whether the asset metadata may be changed by a non Force origin.
@@ -104,7 +105,7 @@ pub struct AssetMetadata<DepositBalance> {
}
/// Witness data for the destroy transactions.
#[derive(Copy, Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug)]
#[derive(Copy, Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, MaxEncodedLen)]
pub struct DestroyWitness {
/// The number of accounts holding the asset.
#[codec(compact)]
+2
View File
@@ -20,6 +20,7 @@ frame-benchmarking = { version = "3.1.0", default-features = false, path = "../b
frame-support = { version = "3.0.0", default-features = false, path = "../support" }
frame-system = { version = "3.0.0", default-features = false, path = "../system" }
log = { version = "0.4.14", default-features = false }
max-encoded-len = { version = "3.0.0", default-features = false, path = "../../max-encoded-len", features = [ "derive" ] }
[dev-dependencies]
sp-io = { version = "3.0.0", path = "../../primitives/io" }
@@ -36,6 +37,7 @@ std = [
"frame-support/std",
"frame-system/std",
"log/std",
"max-encoded-len/std",
]
runtime-benchmarks = ["frame-benchmarking"]
try-runtime = ["frame-support/try-runtime"]
+21 -11
View File
@@ -159,9 +159,9 @@ use sp_std::prelude::*;
use sp_std::{cmp, result, mem, fmt::Debug, ops::BitOr};
use codec::{Codec, Encode, Decode};
use frame_support::{
ensure,
ensure, WeakBoundedVec,
traits::{
Currency, OnUnbalanced, TryDrop, StoredMap,
Currency, OnUnbalanced, TryDrop, StoredMap, MaxEncodedLen,
WithdrawReasons, LockIdentifier, LockableCurrency, ExistenceRequirement,
Imbalance, SignedImbalance, ReservableCurrency, Get, ExistenceRequirement::KeepAlive,
ExistenceRequirement::AllowDeath,
@@ -193,7 +193,7 @@ pub mod pallet {
pub trait Config<I: 'static = ()>: frame_system::Config {
/// The balance of an account.
type Balance: Parameter + Member + AtLeast32BitUnsigned + Codec + Default + Copy +
MaybeSerializeDeserialize + Debug;
MaybeSerializeDeserialize + Debug + MaxEncodedLen;
/// Handler for the unbalanced reduction when removing a dust account.
type DustRemoval: OnUnbalanced<NegativeImbalance<Self, I>>;
@@ -218,6 +218,7 @@ pub mod pallet {
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::generate_storage_info]
pub struct Pallet<T, I=()>(PhantomData<(T, I)>);
#[pallet::call]
@@ -424,7 +425,9 @@ pub mod pallet {
Blake2_128Concat,
T::AccountId,
AccountData<T::Balance>,
ValueQuery
ValueQuery,
GetDefault,
ConstU32<300_000>,
>;
/// Any liquidity locks on some account balances.
@@ -435,8 +438,10 @@ pub mod pallet {
_,
Blake2_128Concat,
T::AccountId,
Vec<BalanceLock<T::Balance>>,
ValueQuery
WeakBoundedVec<BalanceLock<T::Balance>, T::MaxLocks>,
ValueQuery,
GetDefault,
ConstU32<300_000>,
>;
/// Storage version of the pallet.
@@ -513,7 +518,7 @@ impl<T: Config<I>, I: 'static> GenesisConfig<T, I> {
}
/// Simplified reasons for withdrawing balance.
#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug)]
#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, MaxEncodedLen)]
pub enum Reasons {
/// Paying system transaction fees.
Fee = 0,
@@ -545,7 +550,7 @@ impl BitOr for Reasons {
/// A single lock on a balance. There can be many of these on an account and they "overlap", so the
/// same balance is frozen by multiple locks.
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, MaxEncodedLen)]
pub struct BalanceLock<Balance> {
/// An identifier for this lock. Only one lock may be in existence for each identifier.
pub id: LockIdentifier,
@@ -556,7 +561,7 @@ pub struct BalanceLock<Balance> {
}
/// All balance information for an account.
#[derive(Encode, Decode, Clone, PartialEq, Eq, Default, RuntimeDebug)]
#[derive(Encode, Decode, Clone, PartialEq, Eq, Default, RuntimeDebug, MaxEncodedLen)]
pub struct AccountData<Balance> {
/// Non-reserved part of the balance. There may still be restrictions on this, but it is the
/// total pool what may in principle be transferred, reserved and used for tipping.
@@ -602,7 +607,7 @@ impl<Balance: Saturating + Copy + Ord> AccountData<Balance> {
// A value placed in storage that represents the current version of the Balances storage.
// This value is used by the `on_runtime_upgrade` logic to determine whether we run
// storage migration logic. This should match directly with the semantic versions of the Rust crate.
#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug)]
#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, MaxEncodedLen)]
enum Releases {
V1_0_0,
V2_0_0,
@@ -822,6 +827,11 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Update the account entry for `who`, given the locks.
fn update_locks(who: &T::AccountId, locks: &[BalanceLock<T::Balance>]) {
let bounded_locks = WeakBoundedVec::<_, T::MaxLocks>::force_from(
locks.to_vec(),
Some("Balances Update Locks"),
);
if locks.len() as u32 > T::MaxLocks::get() {
log::warn!(
target: "runtime::balances",
@@ -853,7 +863,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
system::Pallet::<T>::dec_consumers(who);
}
} else {
Locks::<T, I>::insert(who, locks);
Locks::<T, I>::insert(who, bounded_locks);
if !existed {
if system::Pallet::<T>::inc_consumers(who).is_err() {
// No providers for the locks. This is impossible under normal circumstances
+3 -1
View File
@@ -20,6 +20,7 @@ sp-core = { version = "3.0.0", default-features = false, path = "../../primitive
sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" }
sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" }
max-encoded-len = { version = "3.0.0", default-features = false, path = "../../max-encoded-len", features = [ "derive" ] }
frame-benchmarking = { version = "3.1.0", default-features = false, path = "../benchmarking", optional = true }
@@ -36,7 +37,8 @@ std = [
"frame-support/std",
"frame-system/std",
"sp-std/std",
"sp-io/std"
"sp-io/std",
"max-encoded-len/std",
]
runtime-benchmarks = [
"frame-benchmarking",
+36 -45
View File
@@ -33,7 +33,7 @@ mod tests;
mod benchmarking;
pub mod weights;
use sp_std::prelude::*;
use sp_std::{prelude::*, convert::TryInto};
use codec::{Encode, Decode};
use sp_io::hashing::blake2_256;
use sp_runtime::{
@@ -43,8 +43,11 @@ use sp_runtime::{
use frame_support::{
RuntimeDebug, ensure,
dispatch::{DispatchResultWithPostInfo, PostDispatchInfo},
traits::{Get, ReservableCurrency, Currency, InstanceFilter, OriginTrait, IsType, IsSubType},
weights::{Weight, GetDispatchInfo}
traits::{
Get, ReservableCurrency, Currency, InstanceFilter, OriginTrait,
IsType, IsSubType, MaxEncodedLen,
},
weights::GetDispatchInfo,
};
use frame_system::{self as system};
use frame_support::dispatch::DispatchError;
@@ -58,7 +61,7 @@ type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Con
/// The parameters under which a particular account has a proxy relationship with some other
/// account.
#[derive(Encode, Decode, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, RuntimeDebug)]
#[derive(Encode, Decode, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, RuntimeDebug, MaxEncodedLen)]
pub struct ProxyDefinition<AccountId, ProxyType, BlockNumber> {
/// The account which may act on behalf of another.
delegate: AccountId,
@@ -70,7 +73,7 @@ pub struct ProxyDefinition<AccountId, ProxyType, BlockNumber> {
}
/// Details surrounding a specific instance of an announcement to make a call.
#[derive(Encode, Decode, Clone, Copy, Eq, PartialEq, RuntimeDebug)]
#[derive(Encode, Decode, Clone, Copy, Eq, PartialEq, RuntimeDebug, MaxEncodedLen)]
pub struct Announcement<AccountId, Hash, BlockNumber> {
/// The account which made the announcement.
real: AccountId,
@@ -88,6 +91,7 @@ pub mod pallet {
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::generate_storage_info]
pub struct Pallet<T>(_);
/// Configuration trait.
@@ -109,7 +113,7 @@ pub mod pallet {
///
/// IMPORTANT: `Default` must be provided and MUST BE the the *most permissive* value.
type ProxyType: Parameter + Member + Ord + PartialOrd + InstanceFilter<<Self as Config>::Call>
+ Default;
+ Default + MaxEncodedLen;
/// The base amount of currency needed to reserve for creating a proxy.
///
@@ -128,7 +132,7 @@ pub mod pallet {
/// The maximum amount of proxies allowed for a single account.
#[pallet::constant]
type MaxProxies: Get<u16>;
type MaxProxies: Get<u32>;
/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
@@ -293,14 +297,20 @@ pub mod pallet {
let anonymous = Self::anonymous_account(&who, &proxy_type, index, None);
ensure!(!Proxies::<T>::contains_key(&anonymous), Error::<T>::Duplicate);
let deposit = T::ProxyDepositBase::get() + T::ProxyDepositFactor::get();
T::Currency::reserve(&who, deposit)?;
let proxy_def = ProxyDefinition {
delegate: who.clone(),
proxy_type: proxy_type.clone(),
delay,
};
Proxies::<T>::insert(&anonymous, (vec![proxy_def], deposit));
let bounded_proxies: BoundedVec<_, T::MaxProxies> = vec![proxy_def]
.try_into()
.map_err(|_| Error::<T>::TooMany)?;
let deposit = T::ProxyDepositBase::get() + T::ProxyDepositFactor::get();
T::Currency::reserve(&who, deposit)?;
Proxies::<T>::insert(&anonymous, (bounded_proxies, deposit));
Self::deposit_event(Event::AnonymousCreated(anonymous, who, proxy_type, index));
Ok(().into())
@@ -386,8 +396,7 @@ pub mod pallet {
};
Announcements::<T>::try_mutate(&who, |(ref mut pending, ref mut deposit)| {
ensure!(pending.len() < T::MaxPending::get() as usize, Error::<T>::TooMany);
pending.push(announcement);
pending.try_push(announcement).map_err(|_| Error::<T>::TooMany)?;
Self::rejig_deposit(
&who,
*deposit,
@@ -555,7 +564,13 @@ pub mod pallet {
_,
Twox64Concat,
T::AccountId,
(Vec<ProxyDefinition<T::AccountId, T::ProxyType, T::BlockNumber>>, BalanceOf<T>),
(
BoundedVec<
ProxyDefinition<T::AccountId, T::ProxyType, T::BlockNumber>,
T::MaxProxies,
>,
BalanceOf<T>
),
ValueQuery
>;
@@ -566,7 +581,13 @@ pub mod pallet {
_,
Twox64Concat,
T::AccountId,
(Vec<Announcement<T::AccountId, CallHashOf<T>, T::BlockNumber>>, BalanceOf<T>),
(
BoundedVec<
Announcement<T::AccountId, CallHashOf<T>, T::BlockNumber>,
T::MaxPending,
>,
BalanceOf<T>,
),
ValueQuery
>;
@@ -616,10 +637,9 @@ impl<T: Config> Pallet<T> {
) -> DispatchResultWithPostInfo {
ensure!(delegator != &delegatee, Error::<T>::NoSelfProxy);
Proxies::<T>::try_mutate(delegator, |(ref mut proxies, ref mut deposit)| {
ensure!(proxies.len() < T::MaxProxies::get() as usize, Error::<T>::TooMany);
let proxy_def = ProxyDefinition { delegate: delegatee, proxy_type, delay };
let i = proxies.binary_search(&proxy_def).err().ok_or(Error::<T>::Duplicate)?;
proxies.insert(i, proxy_def);
proxies.try_insert(i, proxy_def).map_err(|_| Error::<T>::TooMany)?;
let new_deposit = Self::deposit(proxies.len() as u32);
if new_deposit > *deposit {
T::Currency::reserve(delegator, new_deposit - *deposit)?;
@@ -749,32 +769,3 @@ impl<T: Config> Pallet<T> {
Self::deposit_event(Event::ProxyExecuted(e.map(|_| ()).map_err(|e| e.error)));
}
}
/// Migration utilities for upgrading the Proxy pallet between its different versions.
pub mod migration {
use super::*;
/// Migration code for <https://github.com/paritytech/substrate/pull/6770>
///
/// Details: This migration was introduced between Substrate 2.0-RC6 and Substrate 2.0 releases.
/// Before this migration, the `Proxies` storage item used a tuple of `AccountId` and
/// `ProxyType` to represent the proxy definition. After #6770, we switched to use a struct
/// `ProxyDefinition` which additionally included a `BlockNumber` delay value. This function,
/// simply takes any existing proxies using the old tuple format, and migrates it to the new
/// struct by setting the delay to zero.
pub fn migrate_to_time_delayed_proxies<T: Config>() -> Weight {
Proxies::<T>::translate::<(Vec<(T::AccountId, T::ProxyType)>, BalanceOf<T>), _>(
|_, (targets, deposit)| Some((
targets.into_iter()
.map(|(a, t)| ProxyDefinition {
delegate: a,
proxy_type: t,
delay: Zero::zero(),
})
.collect::<Vec<_>>(),
deposit,
))
);
T::BlockWeights::get().max_block
}
}
+24 -16
View File
@@ -100,7 +100,10 @@ parameter_types! {
pub const AnnouncementDepositBase: u64 = 1;
pub const AnnouncementDepositFactor: u64 = 1;
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, RuntimeDebug)]
#[derive(
Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, RuntimeDebug,
max_encoded_len::MaxEncodedLen,
)]
pub enum ProxyType {
Any,
JustTransfer,
@@ -180,15 +183,17 @@ fn announcement_works() {
assert_eq!(Balances::reserved_balance(3), 0);
assert_ok!(Proxy::announce(Origin::signed(3), 1, [1; 32].into()));
assert_eq!(Announcements::<Test>::get(3), (vec![Announcement {
let announcements = Announcements::<Test>::get(3);
assert_eq!(announcements.0, vec![Announcement {
real: 1,
call_hash: [1; 32].into(),
height: 1,
}], 2));
assert_eq!(Balances::reserved_balance(3), 2);
}]);
assert_eq!(Balances::reserved_balance(3), announcements.1);
assert_ok!(Proxy::announce(Origin::signed(3), 2, [2; 32].into()));
assert_eq!(Announcements::<Test>::get(3), (vec![
let announcements = Announcements::<Test>::get(3);
assert_eq!(announcements.0, vec![
Announcement {
real: 1,
call_hash: [1; 32].into(),
@@ -199,8 +204,8 @@ fn announcement_works() {
call_hash: [2; 32].into(),
height: 1,
},
], 3));
assert_eq!(Balances::reserved_balance(3), 3);
]);
assert_eq!(Balances::reserved_balance(3), announcements.1);
assert_noop!(Proxy::announce(Origin::signed(3), 2, [3; 32].into()), Error::<Test>::TooMany);
});
@@ -216,12 +221,13 @@ fn remove_announcement_works() {
let e = Error::<Test>::NotFound;
assert_noop!(Proxy::remove_announcement(Origin::signed(3), 1, [0; 32].into()), e);
assert_ok!(Proxy::remove_announcement(Origin::signed(3), 1, [1; 32].into()));
assert_eq!(Announcements::<Test>::get(3), (vec![Announcement {
let announcements = Announcements::<Test>::get(3);
assert_eq!(announcements.0, vec![Announcement {
real: 2,
call_hash: [2; 32].into(),
height: 1,
}], 2));
assert_eq!(Balances::reserved_balance(3), 2);
}]);
assert_eq!(Balances::reserved_balance(3), announcements.1);
});
}
@@ -237,12 +243,13 @@ fn reject_announcement_works() {
let e = Error::<Test>::NotFound;
assert_noop!(Proxy::reject_announcement(Origin::signed(4), 3, [1; 32].into()), e);
assert_ok!(Proxy::reject_announcement(Origin::signed(1), 3, [1; 32].into()));
assert_eq!(Announcements::<Test>::get(3), (vec![Announcement {
let announcements = Announcements::<Test>::get(3);
assert_eq!(announcements.0, vec![Announcement {
real: 2,
call_hash: [2; 32].into(),
height: 1,
}], 2));
assert_eq!(Balances::reserved_balance(3), 2);
}]);
assert_eq!(Balances::reserved_balance(3), announcements.1);
});
}
@@ -284,12 +291,13 @@ fn proxy_announced_removes_announcement_and_returns_deposit() {
system::Pallet::<Test>::set_block_number(2);
assert_ok!(Proxy::proxy_announced(Origin::signed(0), 3, 1, None, call.clone()));
assert_eq!(Announcements::<Test>::get(3), (vec![Announcement {
let announcements = Announcements::<Test>::get(3);
assert_eq!(announcements.0, vec![Announcement {
real: 2,
call_hash,
height: 1,
}], 2));
assert_eq!(Balances::reserved_balance(3), 2);
}]);
assert_eq!(Balances::reserved_balance(3), announcements.1);
});
}
+1
View File
@@ -125,6 +125,7 @@ pub mod pallet {
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::generate_storage_info]
pub struct Pallet<T>(PhantomData<T>);
#[pallet::call]
+2
View File
@@ -16,6 +16,7 @@ targets = ["x86_64-unknown-linux-gnu"]
serde = { version = "1.0.101", optional = true, features = ["derive"] }
codec = { package = "parity-scale-codec", version = "2.1.0", default-features = false, features = ["derive"] }
frame-metadata = { version = "13.0.0", default-features = false, path = "../metadata" }
max-encoded-len = { version = "3.0.0", default-features = false, path = "../../max-encoded-len", features = [ "derive" ] }
sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" }
sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" }
@@ -55,6 +56,7 @@ std = [
"sp-state-machine",
"frame-support-procedural/std",
"log/std",
"max-encoded-len/std",
]
runtime-benchmarks = []
try-runtime = []
@@ -28,7 +28,6 @@ mod debug_no_bound;
mod clone_no_bound;
mod partial_eq_no_bound;
mod default_no_bound;
mod max_encoded_len;
mod key_prefix;
pub(crate) use storage::INHERENT_INSTANCE_NAME;
@@ -447,12 +446,6 @@ pub fn crate_to_pallet_version(input: TokenStream) -> TokenStream {
/// and up to `NUMBER_OF_INSTANCE`.
pub(crate) const NUMBER_OF_INSTANCE: u8 = 16;
/// Derive `MaxEncodedLen`.
#[proc_macro_derive(MaxEncodedLen)]
pub fn derive_max_encoded_len(input: TokenStream) -> TokenStream {
max_encoded_len::derive_max_encoded_len(input)
}
/// This macro is meant to be used by frame-support only.
/// It implements the trait `HasKeyPrefix` and `HasReversibleKeyPrefix` for tuple of `Key`.
#[proc_macro]
@@ -1,133 +0,0 @@
// This file is part of Substrate.
// Copyright (C) 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 frame_support_procedural_tools::generate_crate_access_2018;
use quote::{quote, quote_spanned};
use syn::{
Data, DeriveInput, Fields, GenericParam, Generics, TraitBound, Type, TypeParamBound,
parse_quote, spanned::Spanned,
};
/// impl for `#[derive(MaxEncodedLen)]`
pub fn derive_max_encoded_len(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input: DeriveInput = match syn::parse(input) {
Ok(input) => input,
Err(e) => return e.to_compile_error().into(),
};
let mel_trait = match max_encoded_len_trait() {
Ok(mel_trait) => mel_trait,
Err(e) => return e.to_compile_error().into(),
};
let name = &input.ident;
let generics = add_trait_bounds(input.generics, mel_trait.clone());
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
let data_expr = data_length_expr(&input.data);
quote::quote!(
const _: () = {
impl #impl_generics #mel_trait for #name #ty_generics #where_clause {
fn max_encoded_len() -> usize {
#data_expr
}
}
};
)
.into()
}
fn max_encoded_len_trait() -> syn::Result<TraitBound> {
let frame_support = generate_crate_access_2018("frame-support")?;
Ok(parse_quote!(#frame_support::traits::MaxEncodedLen))
}
// Add a bound `T: MaxEncodedLen` to every type parameter T.
fn add_trait_bounds(mut generics: Generics, mel_trait: TraitBound) -> Generics {
for param in &mut generics.params {
if let GenericParam::Type(ref mut type_param) = *param {
type_param.bounds.push(TypeParamBound::Trait(mel_trait.clone()));
}
}
generics
}
/// generate an expression to sum up the max encoded length from several fields
fn fields_length_expr(fields: &Fields) -> proc_macro2::TokenStream {
let type_iter: Box<dyn Iterator<Item = &Type>> = match fields {
Fields::Named(ref fields) => Box::new(fields.named.iter().map(|field| &field.ty)),
Fields::Unnamed(ref fields) => Box::new(fields.unnamed.iter().map(|field| &field.ty)),
Fields::Unit => Box::new(std::iter::empty()),
};
// expands to an expression like
//
// 0
// .saturating_add(<type of first field>::max_encoded_len())
// .saturating_add(<type of second field>::max_encoded_len())
//
// We match the span of each field to the span of the corresponding
// `max_encoded_len` call. This way, if one field's type doesn't implement
// `MaxEncodedLen`, the compiler's error message will underline which field
// caused the issue.
let expansion = type_iter.map(|ty| {
quote_spanned! {
ty.span() => .saturating_add(<#ty>::max_encoded_len())
}
});
quote! {
0_usize #( #expansion )*
}
}
// generate an expression to sum up the max encoded length of each field
fn data_length_expr(data: &Data) -> proc_macro2::TokenStream {
match *data {
Data::Struct(ref data) => fields_length_expr(&data.fields),
Data::Enum(ref data) => {
// We need an expression expanded for each variant like
//
// 0
// .max(<variant expression>)
// .max(<variant expression>)
// .saturating_add(1)
//
// The 1 derives from the discriminant; see
// https://github.com/paritytech/parity-scale-codec/
// blob/f0341dabb01aa9ff0548558abb6dcc5c31c669a1/derive/src/encode.rs#L211-L216
//
// Each variant expression's sum is computed the way an equivalent struct's would be.
let expansion = data.variants.iter().map(|variant| {
let variant_expression = fields_length_expr(&variant.fields);
quote! {
.max(#variant_expression)
}
});
quote! {
0_usize #( #expansion )* .saturating_add(1)
}
}
Data::Union(ref data) => {
// https://github.com/paritytech/parity-scale-codec/
// blob/f0341dabb01aa9ff0548558abb6dcc5c31c669a1/derive/src/encode.rs#L290-L293
syn::Error::new(data.union_token.span(), "Union types are not supported")
.to_compile_error()
}
}
}
@@ -117,7 +117,7 @@ impl From<DeclStorageDef> for DeclStorageDefExt {
fn from(mut def: DeclStorageDef) -> Self {
let hidden_crate_name = def.hidden_crate.as_ref().map(|i| i.to_string())
.unwrap_or_else(|| "decl_storage".to_string());
let hidden_crate = generate_crate_access(&hidden_crate_name, "frame-support");
let hidden_imports = generate_hidden_includes(&hidden_crate_name, "frame-support");
+6 -2
View File
@@ -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;
+1 -29
View File
@@ -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.
@@ -1,149 +0,0 @@
// This file is part of Substrate.
// Copyright (C) 2020-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.
//! Tests for MaxEncodedLen derive macro
use frame_support::traits::MaxEncodedLen;
use codec::{Compact, Encode};
// These structs won't even compile if the macro isn't working right.
#[derive(Encode, MaxEncodedLen)]
struct Primitives {
bool: bool,
eight: u8,
}
#[test]
fn primitives_max_length() {
assert_eq!(Primitives::max_encoded_len(), 2);
}
#[derive(Encode, MaxEncodedLen)]
struct Composites {
fixed_size_array: [u8; 128],
tuple: (u128, u128),
}
#[test]
fn composites_max_length() {
assert_eq!(Composites::max_encoded_len(), 128 + 16 + 16);
}
#[derive(Encode, MaxEncodedLen)]
struct Generic<T> {
one: T,
two: T,
}
#[test]
fn generic_max_length() {
assert_eq!(Generic::<u8>::max_encoded_len(), u8::max_encoded_len() * 2);
assert_eq!(Generic::<u32>::max_encoded_len(), u32::max_encoded_len() * 2);
}
#[derive(Encode, MaxEncodedLen)]
struct TwoGenerics<T, U> {
t: T,
u: U,
}
#[test]
fn two_generics_max_length() {
assert_eq!(
TwoGenerics::<u8, u16>::max_encoded_len(),
u8::max_encoded_len() + u16::max_encoded_len()
);
assert_eq!(
TwoGenerics::<Compact<u64>, [u16; 8]>::max_encoded_len(),
Compact::<u64>::max_encoded_len() + <[u16; 8]>::max_encoded_len()
);
}
#[derive(Encode, MaxEncodedLen)]
struct UnitStruct;
#[test]
fn unit_struct_max_length() {
assert_eq!(UnitStruct::max_encoded_len(), 0);
}
#[derive(Encode, MaxEncodedLen)]
struct TupleStruct(u8, u32);
#[test]
fn tuple_struct_max_length() {
assert_eq!(TupleStruct::max_encoded_len(), u8::max_encoded_len() + u32::max_encoded_len());
}
#[derive(Encode, MaxEncodedLen)]
struct TupleGeneric<T>(T, T);
#[test]
fn tuple_generic_max_length() {
assert_eq!(TupleGeneric::<u8>::max_encoded_len(), u8::max_encoded_len() * 2);
assert_eq!(TupleGeneric::<u32>::max_encoded_len(), u32::max_encoded_len() * 2);
}
#[derive(Encode, MaxEncodedLen)]
#[allow(unused)]
enum UnitEnum {
A,
B,
}
#[test]
fn unit_enum_max_length() {
assert_eq!(UnitEnum::max_encoded_len(), 1);
}
#[derive(Encode, MaxEncodedLen)]
#[allow(unused)]
enum TupleEnum {
A(u32),
B,
}
#[test]
fn tuple_enum_max_length() {
assert_eq!(TupleEnum::max_encoded_len(), 1 + u32::max_encoded_len());
}
#[derive(Encode, MaxEncodedLen)]
#[allow(unused)]
enum StructEnum {
A { sixty_four: u64, one_twenty_eight: u128 },
B,
}
#[test]
fn struct_enum_max_length() {
assert_eq!(StructEnum::max_encoded_len(), 1 + u64::max_encoded_len() + u128::max_encoded_len());
}
// ensure that enums take the max of variant length, not the sum
#[derive(Encode, MaxEncodedLen)]
#[allow(unused)]
enum EnumMaxNotSum {
A(u32),
B(u32),
}
#[test]
fn enum_max_not_sum_max_length() {
assert_eq!(EnumMaxNotSum::max_encoded_len(), 1 + u32::max_encoded_len());
}
@@ -1,26 +0,0 @@
// This file is part of Substrate.
// Copyright (C) 2020-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.
#[rustversion::attr(not(stable), ignore)]
#[test]
fn derive_no_bound_ui() {
// As trybuild is using `cargo check`, we don't need the real WASM binaries.
std::env::set_var("SKIP_WASM_BUILD", "1");
let t = trybuild::TestCases::new();
t.compile_fail("tests/max_encoded_len_ui/*.rs");
}
@@ -1,6 +0,0 @@
use frame_support::traits::MaxEncodedLen;
#[derive(MaxEncodedLen)]
struct NotEncode;
fn main() {}
@@ -1,13 +0,0 @@
error[E0277]: the trait bound `NotEncode: WrapperTypeEncode` is not satisfied
--> $DIR/not_encode.rs:3:10
|
3 | #[derive(MaxEncodedLen)]
| ^^^^^^^^^^^^^ the trait `WrapperTypeEncode` is not implemented for `NotEncode`
|
::: $WORKSPACE/frame/support/src/traits/max_encoded_len.rs
|
| pub trait MaxEncodedLen: Encode {
| ------ required by this bound in `MaxEncodedLen`
|
= note: required because of the requirements on the impl of `frame_support::dispatch::Encode` for `NotEncode`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -1,14 +0,0 @@
use codec::Encode;
use frame_support::traits::MaxEncodedLen;
#[derive(Encode)]
struct NotMel;
#[derive(Encode, MaxEncodedLen)]
struct Generic<T> {
t: T,
}
fn main() {
let _ = Generic::<NotMel>::max_encoded_len();
}
@@ -1,21 +0,0 @@
error[E0599]: the function or associated item `max_encoded_len` exists for struct `Generic<NotMel>`, but its trait bounds were not satisfied
--> $DIR/not_mel.rs:13:29
|
5 | struct NotMel;
| -------------- doesn't satisfy `NotMel: MaxEncodedLen`
...
8 | struct Generic<T> {
| -----------------
| |
| function or associated item `max_encoded_len` not found for this
| doesn't satisfy `Generic<NotMel>: MaxEncodedLen`
...
13 | let _ = Generic::<NotMel>::max_encoded_len();
| ^^^^^^^^^^^^^^^ function or associated item cannot be called on `Generic<NotMel>` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`NotMel: MaxEncodedLen`
which is required by `Generic<NotMel>: MaxEncodedLen`
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `max_encoded_len`, perhaps you need to implement it:
candidate #1: `MaxEncodedLen`
@@ -1,10 +0,0 @@
use codec::Encode;
use frame_support::traits::MaxEncodedLen;
#[derive(Encode, MaxEncodedLen)]
union Union {
a: u8,
b: u16,
}
fn main() {}
@@ -1,11 +0,0 @@
error: Union types are not supported
--> $DIR/union.rs:5:1
|
5 | union Union {
| ^^^^^
error: Union types are not supported.
--> $DIR/union.rs:5:1
|
5 | union Union {
| ^^^^^
@@ -1,12 +0,0 @@
use codec::Encode;
use frame_support::traits::MaxEncodedLen;
#[derive(Encode)]
struct NotMel;
#[derive(Encode, MaxEncodedLen)]
enum UnsupportedVariant {
NotMel(NotMel),
}
fn main() {}
@@ -1,12 +0,0 @@
error[E0599]: no function or associated item named `max_encoded_len` found for struct `NotMel` in the current scope
--> $DIR/unsupported_variant.rs:9:9
|
5 | struct NotMel;
| -------------- function or associated item `max_encoded_len` not found for this
...
9 | NotMel(NotMel),
| ^^^^^^ function or associated item not found in `NotMel`
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `max_encoded_len`, perhaps you need to implement it:
candidate #1: `MaxEncodedLen`
@@ -4,7 +4,7 @@ error[E0277]: the trait bound `<T as pallet::Config>::Bar: WrapperTypeDecode` is
20 | fn foo(origin: OriginFor<T>, bar: T::Bar) -> DispatchResultWithPostInfo {
| ^ the trait `WrapperTypeDecode` is not implemented for `<T as pallet::Config>::Bar`
|
::: /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/parity-scale-codec-2.1.0/src/codec.rs:277:18
::: /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/parity-scale-codec-2.1.1/src/codec.rs:277:18
|
277 | fn decode<I: Input>(input: &mut I) -> Result<Self, Error>;
| ----- required by this bound in `pallet::_::_parity_scale_codec::Decode::decode`
@@ -17,7 +17,7 @@ error[E0277]: the trait bound `<T as pallet::Config>::Bar: WrapperTypeEncode` is
20 | fn foo(origin: OriginFor<T>, bar: T::Bar) -> DispatchResultWithPostInfo {
| ^ the trait `WrapperTypeEncode` is not implemented for `<T as pallet::Config>::Bar`
|
::: /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/parity-scale-codec-2.1.0/src/codec.rs:216:21
::: /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/parity-scale-codec-2.1.1/src/codec.rs:216:21
|
216 | fn encode_to<T: Output + ?Sized>(&self, dest: &mut T) {
| ------ required by this bound in `encode_to`
+5 -4
View File
@@ -88,7 +88,7 @@ use frame_support::{
Parameter, storage,
traits::{
SortedMembers, Get, PalletInfo, OnNewAccount, OnKilledAccount, HandleLifetime,
StoredMap, EnsureOrigin, OriginTrait, Filter,
StoredMap, EnsureOrigin, OriginTrait, Filter, MaxEncodedLen,
},
weights::{
Weight, RuntimeDbWeight, DispatchInfo, DispatchClass,
@@ -194,19 +194,20 @@ pub mod pallet {
type BlockNumber:
Parameter + Member + MaybeSerializeDeserialize + Debug + MaybeDisplay +
AtLeast32BitUnsigned + Default + Bounded + Copy + sp_std::hash::Hash +
sp_std::str::FromStr + MaybeMallocSizeOf;
sp_std::str::FromStr + MaybeMallocSizeOf + MaxEncodedLen;
/// The output of the `Hashing` function.
type Hash:
Parameter + Member + MaybeSerializeDeserialize + Debug + MaybeDisplay + SimpleBitOps + Ord
+ Default + Copy + CheckEqual + sp_std::hash::Hash + AsRef<[u8]> + AsMut<[u8]> + MaybeMallocSizeOf;
+ Default + Copy + CheckEqual + sp_std::hash::Hash + AsRef<[u8]> + AsMut<[u8]>
+ MaybeMallocSizeOf + MaxEncodedLen;
/// The hashing system (algorithm) being used in the runtime (e.g. Blake2).
type Hashing: Hash<Output=Self::Hash>;
/// The user account identifier type for the runtime.
type AccountId: Parameter + Member + MaybeSerializeDeserialize + Debug + MaybeDisplay + Ord
+ Default;
+ Default + MaxEncodedLen;
/// Converting trait to take a source type and convert to `AccountId`.
///
+2 -1
View File
@@ -116,7 +116,7 @@ pub mod pallet {
pub trait Config: frame_system::Config {
/// Type used for expressing timestamp.
type Moment: Parameter + Default + AtLeast32Bit
+ Scale<Self::BlockNumber, Output = Self::Moment> + Copy;
+ Scale<Self::BlockNumber, Output = Self::Moment> + Copy + MaxEncodedLen;
/// Something which can be notified when the timestamp is set. Set this to `()` if not needed.
type OnTimestampSet: OnTimestampSet<Self::Moment>;
@@ -134,6 +134,7 @@ pub mod pallet {
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::generate_storage_info]
pub struct Pallet<T>(PhantomData<T>);
/// Current time for the current block.