Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Add Storage Info to Various Pallets #10810

Merged
merged 28 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion frame/beefy-mmr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ pub mod pallet {
/// BEEFY-MMR pallet.
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);

/// The module's configuration trait.
Expand Down
13 changes: 6 additions & 7 deletions frame/examples/offchain-worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ pub mod crypto {
}
}

const MAX_PRICES_LEN: u32 = 64;
ggwpez marked this conversation as resolved.
Show resolved Hide resolved

pub use pallet::*;

#[frame_support::pallet]
Expand Down Expand Up @@ -330,7 +332,8 @@ pub mod pallet {
/// This is used to calculate average price, should have bounded size.
#[pallet::storage]
#[pallet::getter(fn prices)]
pub(super) type Prices<T: Config> = StorageValue<_, Vec<u32>, ValueQuery>;
pub(super) type Prices<T: Config> =
StorageValue<_, BoundedVec<u32, ConstU32<MAX_PRICES_LEN>>, ValueQuery>;

/// Defines the block when next unsigned transaction will be accepted.
///
Expand Down Expand Up @@ -645,12 +648,8 @@ impl<T: Config> Pallet<T> {
fn add_price(maybe_who: Option<T::AccountId>, price: u32) {
log::info!("Adding to the average: {}", price);
<Prices<T>>::mutate(|prices| {
const MAX_LEN: usize = 64;

if prices.len() < MAX_LEN {
prices.push(price);
} else {
prices[price as usize % MAX_LEN] = price;
if prices.try_push(price).is_err() {
prices[(price % MAX_PRICES_LEN) as usize] = price;
}
});

Expand Down
2 changes: 1 addition & 1 deletion frame/gilt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ pub mod pallet {
_,
Blake2_128Concat,
u32,
BoundedVec<GiltBid<BalanceOf<T>, T::AccountId>, T::QueueCount>,
BoundedVec<GiltBid<BalanceOf<T>, T::AccountId>, T::MaxQueueLen>,
ValueQuery,
>;

Expand Down
4 changes: 2 additions & 2 deletions frame/merkle-mountain-range/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ pub mod pallet {

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::without_storage_info]
pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);

/// This pallet's configuration trait
Expand Down Expand Up @@ -126,7 +125,8 @@ pub mod pallet {
+ Default
+ codec::Codec
+ codec::EncodeLike
+ scale_info::TypeInfo;
+ scale_info::TypeInfo
+ MaxEncodedLen;

/// Data stored in the leaf nodes.
///
Expand Down
1 change: 0 additions & 1 deletion frame/randomness-collective-flip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ pub mod pallet {

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
//#[pallet::without_storage_info]
pub struct Pallet<T>(_);

#[pallet::config]
Expand Down
29 changes: 16 additions & 13 deletions frame/recovery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -179,7 +179,7 @@ type BalanceOf<T> =
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)]
#[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,
Expand All @@ -191,16 +191,16 @@ pub struct ActiveRecovery<BlockNumber, Balance, 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,
/// The amount held in reserve of the `depositor`,
/// 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,
}
Expand All @@ -214,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.
Expand Down Expand Up @@ -331,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.
Expand Down Expand Up @@ -465,21 +464,25 @@ 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: BoundedVec<_, _> =
shawntabrizi marked this conversation as resolved.
Show resolved Hide resolved
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)
.ok_or(ArithmeticError::Overflow)?;
// 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);

Expand Down
12 changes: 8 additions & 4 deletions frame/recovery/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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));
});
}
Expand Down Expand Up @@ -316,7 +320,7 @@ fn vouch_recovery_works() {
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].try_into().unwrap() };
ActiveRecovery { created: 0, deposit: 10, friends: bounded_vec![2, 3, 4] };
assert_eq!(<ActiveRecoveries<Test>>::get(&5, &1), Some(recovery_status));
});
}
Expand Down
5 changes: 2 additions & 3 deletions frame/transaction-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Decode, Encode};
use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;

use sp_runtime::{
Expand Down Expand Up @@ -226,7 +226,7 @@ where
}

/// Storage releases of the pallet.
#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo)]
#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
enum Releases {
/// Original version of the pallet.
V1Ancient,
Expand All @@ -248,7 +248,6 @@ pub mod pallet {

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);

#[pallet::config]
Expand Down
15 changes: 10 additions & 5 deletions frame/uniques/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ pub mod pallet {

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::without_storage_info]
pub struct Pallet<T, I = ()>(_);

#[pallet::config]
Expand All @@ -72,10 +71,16 @@ pub mod pallet {
type Event: From<Event<Self, I>> + IsType<<Self as frame_system::Config>::Event>;

/// Identifier for the class of asset.
type ClassId: Member + Parameter + Default + Copy + HasCompact;
type ClassId: Member + Parameter + Default + Copy + HasCompact + MaxEncodedLen;

/// The type used to identify a unique asset within an asset class.
type InstanceId: Member + Parameter + Default + Copy + HasCompact + From<u16>;
type InstanceId: Member
+ Parameter
+ Default
+ Copy
+ HasCompact
+ From<u16>
+ MaxEncodedLen;

/// The currency mechanism, used for paying for reserves.
type Currency: ReservableCurrency<Self::AccountId>;
Expand Down Expand Up @@ -175,7 +180,7 @@ pub mod pallet {
_,
Blake2_128Concat,
T::ClassId,
ClassMetadata<DepositBalanceOf<T, I>, T::StringLimit>,
ClassMetadata<DepositBalanceOf<T, I>, BoundedVec<u8, T::StringLimit>>,
OptionQuery,
>;

Expand All @@ -187,7 +192,7 @@ pub mod pallet {
T::ClassId,
Blake2_128Concat,
T::InstanceId,
InstanceMetadata<DepositBalanceOf<T, I>, T::StringLimit>,
InstanceMetadata<DepositBalanceOf<T, I>, BoundedVec<u8, T::StringLimit>>,
OptionQuery,
>;

Expand Down
20 changes: 10 additions & 10 deletions frame/uniques/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//! Various basic types for use in the assets pallet.

use super::*;
use frame_support::{traits::Get, BoundedVec};
use frame_support::pallet_prelude::MaxEncodedLen;
use scale_info::TypeInfo;

pub(super) type DepositBalanceOf<T, I = ()> =
Expand All @@ -28,7 +28,7 @@ pub(super) type ClassDetailsFor<T, I> =
pub(super) type InstanceDetailsFor<T, I> =
InstanceDetails<<T as SystemConfig>::AccountId, DepositBalanceOf<T, I>>;

#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo)]
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct ClassDetails<AccountId, DepositBalance> {
/// Can change `owner`, `issuer`, `freezer` and `admin` accounts.
pub(super) owner: AccountId,
Expand All @@ -54,7 +54,7 @@ pub struct ClassDetails<AccountId, DepositBalance> {
}

/// Witness data for the destroy transactions.
#[derive(Copy, Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo)]
#[derive(Copy, Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct DestroyWitness {
/// The total number of outstanding instances of this asset class.
#[codec(compact)]
Expand All @@ -78,7 +78,7 @@ impl<AccountId, DepositBalance> ClassDetails<AccountId, DepositBalance> {
}

/// Information concerning the ownership of a single unique asset.
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, Default, TypeInfo)]
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, Default, TypeInfo, MaxEncodedLen)]
pub struct InstanceDetails<AccountId, DepositBalance> {
/// The owner of this asset.
pub(super) owner: AccountId,
Expand All @@ -91,32 +91,32 @@ pub struct InstanceDetails<AccountId, DepositBalance> {
pub(super) deposit: DepositBalance,
}

#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, Default, TypeInfo)]
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, Default, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(StringLimit))]
pub struct ClassMetadata<DepositBalance, StringLimit: Get<u32>> {
pub struct ClassMetadata<DepositBalance, BoundedString> {
/// The balance deposited for this metadata.
///
/// This pays for the data stored in this struct.
pub(super) deposit: DepositBalance,
/// General information concerning this asset. Limited in length by `StringLimit`. This will
/// generally be either a JSON dump or the hash of some JSON which can be found on a
/// hash-addressable global publication system such as IPFS.
pub(super) data: BoundedVec<u8, StringLimit>,
pub(super) data: BoundedString,
shawntabrizi marked this conversation as resolved.
Show resolved Hide resolved
/// Whether the asset metadata may be changed by a non Force origin.
pub(super) is_frozen: bool,
}

#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, Default, TypeInfo)]
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, Default, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(StringLimit))]
pub struct InstanceMetadata<DepositBalance, StringLimit: Get<u32>> {
pub struct InstanceMetadata<DepositBalance, BoundedString> {
/// The balance deposited for this metadata.
///
/// This pays for the data stored in this struct.
pub(super) deposit: DepositBalance,
/// General information concerning this asset. Limited in length by `StringLimit`. This will
/// generally be either a JSON dump or the hash of some JSON which can be found on a
/// hash-addressable global publication system such as IPFS.
pub(super) data: BoundedVec<u8, StringLimit>,
pub(super) data: BoundedString,
/// Whether the asset metadata may be changed by a non Force origin.
pub(super) is_frozen: bool,
}
4 changes: 2 additions & 2 deletions primitives/beefy/src/mmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
//! but we imagine they will be useful for other chains that either want to bridge with Polkadot
//! or are completely standalone, but heavily inspired by Polkadot.

use codec::{Decode, Encode};
use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;

/// A standard leaf that gets added every block to the MMR constructed by Substrate's `pallet_mmr`.
Expand Down Expand Up @@ -81,7 +81,7 @@ impl MmrLeafVersion {
}

/// Details of the next BEEFY authority set.
#[derive(Debug, Default, PartialEq, Eq, Clone, Encode, Decode, TypeInfo)]
#[derive(Debug, Default, PartialEq, Eq, Clone, Encode, Decode, TypeInfo, MaxEncodedLen)]
pub struct BeefyNextAuthoritySet<MerkleRoot> {
/// Id of the next set.
///
Expand Down