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