Add Storage Info to Various Pallets (#10810)

* atomic swap

* bounties

* bounties fmt

* gilt

* indices

* nicks

* randomness-collective-flip

* recovery

* reuse maxapprovals

* Update tests.rs

* Update frame/randomness-collective-flip/src/lib.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* use the correct bound

* complete recovery

* use `bounded_vec` macro

* Update tests.rs

* transaction payment

* uniques

* mmr

* example offchain worker

* beefy-mmr

* Update frame/recovery/src/lib.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Use BoundedVec instead of a type-parameterized BoundedString

* cargo fmt

* Update frame/atomic-swap/src/lib.rs

* use config const

* Update lib.rs

* update mel_bound

* fmt

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>
This commit is contained in:
Shawn Tabrizi
2022-02-08 15:10:35 +01:00
committed by GitHub
parent d14e1c641e
commit f6f82d876b
25 changed files with 165 additions and 115 deletions
+33 -20
View File
@@ -154,7 +154,7 @@
// Ensure we're `no_std` when compiling for Wasm.
#![cfg_attr(not(feature = "std"), no_std)]
use codec::{Decode, Encode};
use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_runtime::traits::{CheckedAdd, CheckedMul, Dispatchable, SaturatedConversion};
use sp_std::prelude::*;
@@ -163,7 +163,7 @@ use frame_support::{
dispatch::PostDispatchInfo,
traits::{BalanceStatus, Currency, ReservableCurrency},
weights::GetDispatchInfo,
RuntimeDebug,
BoundedVec, RuntimeDebug,
};
pub use pallet::*;
@@ -176,21 +176,23 @@ mod tests;
type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
type FriendsOf<T> = BoundedVec<<T as frame_system::Config>::AccountId, <T as Config>::MaxFriends>;
/// An active recovery process.
#[derive(Clone, Eq, PartialEq, Encode, Decode, Default, RuntimeDebug, TypeInfo)]
pub struct ActiveRecovery<BlockNumber, Balance, AccountId> {
#[derive(Clone, Eq, PartialEq, Encode, Decode, Default, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct ActiveRecovery<BlockNumber, Balance, Friends> {
/// The block number when the recovery process started.
created: BlockNumber,
/// The amount held in reserve of the `depositor`,
/// To be returned once this recovery process is closed.
deposit: Balance,
/// The friends which have vouched so far. Always sorted.
friends: Vec<AccountId>,
friends: Friends,
}
/// Configuration for recovering an account.
#[derive(Clone, Eq, PartialEq, Encode, Decode, Default, RuntimeDebug, TypeInfo)]
pub struct RecoveryConfig<BlockNumber, Balance, AccountId> {
#[derive(Clone, Eq, PartialEq, Encode, Decode, Default, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct RecoveryConfig<BlockNumber, Balance, Friends> {
/// The minimum number of blocks since the start of the recovery process before the account
/// can be recovered.
delay_period: BlockNumber,
@@ -198,7 +200,7 @@ pub struct RecoveryConfig<BlockNumber, Balance, AccountId> {
/// to be returned once this configuration is removed.
deposit: Balance,
/// The list of friends which can help recover an account. Always sorted.
friends: Vec<AccountId>,
friends: Friends,
/// The number of approving friends needed to recover an account.
threshold: u16,
}
@@ -212,7 +214,6 @@ pub mod pallet {
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);
/// Configuration trait.
@@ -245,8 +246,13 @@ pub mod pallet {
type FriendDepositFactor: Get<BalanceOf<Self>>;
/// The maximum amount of friends allowed in a recovery configuration.
///
/// NOTE: The threshold programmed in this Pallet uses u16, so it does
/// not really make sense to have a limit here greater than u16::MAX.
/// But also, that is a lot more than you should probably set this value
/// to anyway...
#[pallet::constant]
type MaxFriends: Get<u16>;
type MaxFriends: Get<u32>;
/// The base amount of currency needed to reserve for starting a recovery.
///
@@ -324,7 +330,7 @@ pub mod pallet {
_,
Twox64Concat,
T::AccountId,
RecoveryConfig<T::BlockNumber, BalanceOf<T>, T::AccountId>,
RecoveryConfig<T::BlockNumber, BalanceOf<T>, FriendsOf<T>>,
>;
/// Active recovery attempts.
@@ -339,7 +345,7 @@ pub mod pallet {
T::AccountId,
Twox64Concat,
T::AccountId,
ActiveRecovery<T::BlockNumber, BalanceOf<T>, T::AccountId>,
ActiveRecovery<T::BlockNumber, BalanceOf<T>, FriendsOf<T>>,
>;
/// The list of allowed proxy accounts.
@@ -458,12 +464,12 @@ pub mod pallet {
ensure!(threshold >= 1, Error::<T>::ZeroThreshold);
ensure!(!friends.is_empty(), Error::<T>::NotEnoughFriends);
ensure!(threshold as usize <= friends.len(), Error::<T>::NotEnoughFriends);
let max_friends = T::MaxFriends::get() as usize;
ensure!(friends.len() <= max_friends, Error::<T>::MaxFriends);
ensure!(Self::is_sorted_and_unique(&friends), Error::<T>::NotSorted);
let bounded_friends: FriendsOf<T> =
friends.try_into().map_err(|()| Error::<T>::MaxFriends)?;
ensure!(Self::is_sorted_and_unique(&bounded_friends), Error::<T>::NotSorted);
// Total deposit is base fee + number of friends * factor fee
let friend_deposit = T::FriendDepositFactor::get()
.checked_mul(&friends.len().saturated_into())
.checked_mul(&bounded_friends.len().saturated_into())
.ok_or(ArithmeticError::Overflow)?;
let total_deposit = T::ConfigDepositBase::get()
.checked_add(&friend_deposit)
@@ -471,8 +477,12 @@ pub mod pallet {
// Reserve the deposit
T::Currency::reserve(&who, total_deposit)?;
// Create the recovery configuration
let recovery_config =
RecoveryConfig { delay_period, deposit: total_deposit, friends, threshold };
let recovery_config = RecoveryConfig {
delay_period,
deposit: total_deposit,
friends: bounded_friends,
threshold,
};
// Create the recovery configuration storage item
<Recoverable<T>>::insert(&who, recovery_config);
@@ -519,7 +529,7 @@ pub mod pallet {
let recovery_status = ActiveRecovery {
created: <frame_system::Pallet<T>>::block_number(),
deposit: recovery_deposit,
friends: vec![],
friends: Default::default(),
};
// Create the active recovery storage item
<ActiveRecoveries<T>>::insert(&account, &who, recovery_status);
@@ -571,7 +581,10 @@ pub mod pallet {
// Either insert the vouch, or return an error that the user already vouched.
match active_recovery.friends.binary_search(&who) {
Ok(_pos) => Err(Error::<T>::AlreadyVouched)?,
Err(pos) => active_recovery.friends.insert(pos, who.clone()),
Err(pos) => active_recovery
.friends
.try_insert(pos, who.clone())
.map_err(|()| Error::<T>::MaxFriends)?,
}
// Update storage with the latest details
<ActiveRecoveries<T>>::insert(&lost, &rescuer, active_recovery);
+2 -2
View File
@@ -22,7 +22,7 @@ use super::*;
use crate as recovery;
use frame_support::{
parameter_types,
traits::{ConstU16, ConstU32, ConstU64, OnFinalize, OnInitialize},
traits::{ConstU32, ConstU64, OnFinalize, OnInitialize},
};
use sp_core::H256;
use sp_runtime::{
@@ -105,7 +105,7 @@ impl Config for Test {
type Currency = Balances;
type ConfigDepositBase = ConfigDepositBase;
type FriendDepositFactor = FriendDepositFactor;
type MaxFriends = ConstU16<3>;
type MaxFriends = ConstU32<3>;
type RecoveryDeposit = RecoveryDeposit;
}
+11 -5
View File
@@ -18,7 +18,7 @@
//! Tests for the module.
use super::*;
use frame_support::{assert_noop, assert_ok, traits::Currency};
use frame_support::{assert_noop, assert_ok, bounded_vec, traits::Currency};
use mock::{
new_test_ext, run_to_block, Balances, BalancesCall, Call, Origin, Recovery, RecoveryCall, Test,
};
@@ -201,8 +201,12 @@ fn create_recovery_works() {
// Base 10 + 1 per friends = 13 total reserved
assert_eq!(Balances::reserved_balance(5), 13);
// Recovery configuration is correctly stored
let recovery_config =
RecoveryConfig { delay_period, deposit: 13, friends: friends.clone(), threshold };
let recovery_config = RecoveryConfig {
delay_period,
deposit: 13,
friends: friends.try_into().unwrap(),
threshold,
};
assert_eq!(Recovery::recovery_config(5), Some(recovery_config));
});
}
@@ -254,7 +258,8 @@ fn initiate_recovery_works() {
// Deposit is reserved
assert_eq!(Balances::reserved_balance(1), 10);
// Recovery status object is created correctly
let recovery_status = ActiveRecovery { created: 0, deposit: 10, friends: vec![] };
let recovery_status =
ActiveRecovery { created: 0, deposit: 10, friends: Default::default() };
assert_eq!(<ActiveRecoveries<Test>>::get(&5, &1), Some(recovery_status));
// Multiple users can attempt to recover the same account
assert_ok!(Recovery::initiate_recovery(Origin::signed(2), 5));
@@ -314,7 +319,8 @@ fn vouch_recovery_works() {
assert_ok!(Recovery::vouch_recovery(Origin::signed(4), 5, 1));
assert_ok!(Recovery::vouch_recovery(Origin::signed(3), 5, 1));
// Final recovery status object is updated correctly
let recovery_status = ActiveRecovery { created: 0, deposit: 10, friends: vec![2, 3, 4] };
let recovery_status =
ActiveRecovery { created: 0, deposit: 10, friends: bounded_vec![2, 3, 4] };
assert_eq!(<ActiveRecoveries<Test>>::get(&5, &1), Some(recovery_status));
});
}