Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
0de4775
implement max_values + storages info
gui1117 May 6, 2021
e7c14fb
Merge remote-tracking branch 'origin/master' into gui-max_values-macro
gui1117 May 12, 2021
42c4f48
some formatting + doc
gui1117 May 12, 2021
d1032a3
sudo sanity check
shawntabrizi May 13, 2021
f2582d5
timestamp
shawntabrizi May 13, 2021
bf354b0
assets (not working)
shawntabrizi May 13, 2021
38cecd6
fix assets
shawntabrizi May 13, 2021
06b872a
impl for proxy
shawntabrizi May 14, 2021
43edcac
update balances
shawntabrizi May 14, 2021
0ca09ee
Merge remote-tracking branch 'origin/master' into gui-max_values-macro
gui1117 May 14, 2021
ada719b
rename StoragesInfo -> PalletStorageInfo
gui1117 May 14, 2021
341d3f5
merge both StorageInfoTrait and PalletStorageInfo
gui1117 May 14, 2021
5521953
Update frame/support/procedural/src/storage/parse.rs
gui1117 May 14, 2021
02b0cbb
Update frame/support/procedural/src/storage/storage_struct.rs
gui1117 May 14, 2021
999640b
Fix max_size using hasher information
gui1117 May 14, 2021
a7909d7
fix tests
gui1117 May 14, 2021
e5964c0
fix ui tests
gui1117 May 14, 2021
2001d32
Move `MaxBoundedLen` into its own crate (#8814)
coriolinus May 15, 2021
566425d
nits
shawntabrizi May 15, 2021
bb1d521
Merge branch 'master' into shawntabrizi-maxboundedlen-start
shawntabrizi May 15, 2021
1a398cb
fix compile
shawntabrizi May 15, 2021
3685d3c
line width
shawntabrizi May 15, 2021
15918ff
Merge commit '8d02bb0bfc6136f6a3c805db19f51e43090a7cd4' into gui-max_…
gui1117 May 17, 2021
4fc7829
Merge remote-tracking branch 'origin/master' into gui-max_values-macro
gui1117 May 17, 2021
9c42372
Merge remote-tracking branch 'origin/gui-max_values-macro' into shawn…
gui1117 May 17, 2021
e8841f5
fix max-values-macro merge
gui1117 May 17, 2021
c12be40
Merge remote-tracking branch 'origin/master' into shawntabrizi-maxbou…
gui1117 May 17, 2021
1a3e03e
Add some derive, needed for test and other purpose
gui1117 May 19, 2021
9a45be6
Merge remote-tracking branch 'origin/master' into shawntabrizi-maxbou…
gui1117 May 21, 2021
5af7c4c
use weak bounded vec in some cases
gui1117 May 21, 2021
0e50fe0
Merge branch 'master' into shawntabrizi-maxboundedlen-start
shawntabrizi May 22, 2021
7683f73
Update lib.rs
shawntabrizi May 22, 2021
0c09750
move max-encoded-len crate
shawntabrizi May 22, 2021
d93b503
fix
shawntabrizi May 22, 2021
73623e9
remove app crypto for now
shawntabrizi May 25, 2021
db766a8
Merge branch 'master' into shawntabrizi-maxboundedlen-start
shawntabrizi May 25, 2021
1bb36fd
width
shawntabrizi May 25, 2021
f1e3ced
Revert "remove app crypto for now"
shawntabrizi May 25, 2021
562f6b0
Merge branch 'master' into shawntabrizi-maxboundedlen-start
shawntabrizi May 26, 2021
747e613
unused variable
shawntabrizi May 26, 2021
7af3dcc
more unused variables
shawntabrizi May 26, 2021
0dd6403
more fixes
shawntabrizi May 26, 2021
b7620ef
Add #[max_encoded_len_crate(...)] helper attribute
coriolinus May 26, 2021
8a812bb
fix a ui test
coriolinus May 26, 2021
04375ac
use #[max_encoded_len_crate(...)] helper in app_crypto
coriolinus May 26, 2021
d5f3ee6
remove max_encoded_len import where not necessary
coriolinus May 26, 2021
8971223
Merge remote-tracking branch 'origin/master' into shawntabrizi-maxbou…
coriolinus May 26, 2021
016cc16
update lockfile
coriolinus May 26, 2021
478c742
Merge branch 'master' into shawntabrizi-maxboundedlen-start
shawntabrizi May 26, 2021
e2a16cb
fix ui test
shawntabrizi May 26, 2021
b71b2a7
ui
shawntabrizi May 26, 2021
3acfa75
newline
shawntabrizi May 26, 2021
62d6410
Merge branch 'master' into shawntabrizi-maxboundedlen-start
shawntabrizi May 27, 2021
cf92608
fix merge
shawntabrizi May 27, 2021
2f739cd
try fix ui again
shawntabrizi May 27, 2021
54839b7
Update max-encoded-len/derive/src/lib.rs
shawntabrizi May 27, 2021
1fd92a4
extract generate_crate_access_2018
shawntabrizi May 27, 2021
f41c90a
Merge branch 'shawntabrizi-maxboundedlen-start' of https://github.com…
shawntabrizi May 27, 2021
f6d5db3
Update lib.rs
shawntabrizi May 27, 2021
c329b06
compiler isnt smart enough
shawntabrizi May 27, 2021
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
39 changes: 24 additions & 15 deletions frame/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -165,6 +166,8 @@ pub mod pallet {

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

#[pallet::config]
Expand All @@ -174,10 +177,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>;
Expand Down Expand Up @@ -207,7 +210,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;
Expand All @@ -232,6 +235,8 @@ pub mod pallet {
T::AccountId,
AssetBalance<T::Balance, T::Extra>,
ValueQuery,
GetDefault,
ConstU32<300_000>,
>;

#[pallet::storage]
Expand All @@ -245,6 +250,8 @@ pub mod pallet {
ApprovalKey<T::AccountId>,
Approval<T::Balance, DepositBalanceOf<T, I>>,
OptionQuery,
GetDefault,
ConstU32<300_000>,
>;

#[pallet::storage]
Expand All @@ -253,8 +260,10 @@ pub mod pallet {
_,
Blake2_128Concat,
T::AssetId,
AssetMetadata<DepositBalanceOf<T, I>>,
AssetMetadata<DepositBalanceOf<T, I>, BoundedVec<u8, T::StringLimit>>,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in the future (or now) we should say how the associated types can be modified.
For instance here "T::StringLimit" is used for stored item, thus changing the limit requires a migration of those items.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only when the value decreases. Increases should be backwards compatible.

ValueQuery,
GetDefault,
ConstU32<300_000>,
>;

#[pallet::event]
Expand Down Expand Up @@ -900,8 +909,8 @@ 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);
Expand All @@ -925,8 +934,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,
});
Expand Down Expand Up @@ -990,16 +999,16 @@ 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,
});
Expand Down
19 changes: 10 additions & 9 deletions frame/assets/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -66,7 +67,7 @@ impl<Balance, AccountId, DepositBalance> AssetDetails<Balance, AccountId, Deposi
}

/// A pair to act as a key for the approval storage map.
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug)]
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, MaxEncodedLen)]
pub struct ApprovalKey<AccountId> {
/// The owner of the funds that are being approved.
pub(super) owner: AccountId,
Expand All @@ -75,7 +76,7 @@ pub struct ApprovalKey<AccountId> {
}

/// 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.
Expand All @@ -84,7 +85,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,
Expand All @@ -96,24 +97,24 @@ 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.
pub(super) is_frozen: bool,
}

/// 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)]
Expand Down
109 changes: 59 additions & 50 deletions frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, BoundedVec,
traits::{
Currency, OnUnbalanced, TryDrop, StoredMap,
Currency, OnUnbalanced, TryDrop, StoredMap, MaxEncodedLen,
WithdrawReasons, LockIdentifier, LockableCurrency, ExistenceRequirement,
Imbalance, SignedImbalance, ReservableCurrency, Get, ExistenceRequirement::KeepAlive,
ExistenceRequirement::AllowDeath,
Expand Down Expand Up @@ -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>>;
Expand All @@ -218,6 +218,7 @@ pub mod pallet {

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

#[pallet::hooks]
Expand Down Expand Up @@ -428,7 +429,9 @@ pub mod pallet {
Blake2_128Concat,
T::AccountId,
AccountData<T::Balance>,
ValueQuery
ValueQuery,
GetDefault,
ConstU32<300_000>,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this will need to be configurable in the future, because chains can have very different number of account

>;

/// Any liquidity locks on some account balances.
Expand All @@ -439,8 +442,10 @@ pub mod pallet {
_,
Blake2_128Concat,
T::AccountId,
Vec<BalanceLock<T::Balance>>,
ValueQuery
BoundedVec<BalanceLock<T::Balance>, T::MaxLocks>,
ValueQuery,
GetDefault,
ConstU32<300_000>,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same, it will need to be configurable

>;

/// Storage version of the pallet.
Expand Down Expand Up @@ -517,7 +522,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,
Expand Down Expand Up @@ -549,7 +554,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,
Expand All @@ -560,7 +565,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.
Expand Down Expand Up @@ -606,7 +611,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,
Expand Down Expand Up @@ -826,48 +831,52 @@ 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>]) {
if locks.len() as u32 > T::MaxLocks::get() {
log::warn!(
target: "runtime::balances",
"Warning: A user has more currency locks than expected. \
A runtime configuration adjustment may be needed."
);
}
// No way this can fail since we do not alter the existential balances.
let res = Self::mutate_account(who, |b| {
b.misc_frozen = Zero::zero();
b.fee_frozen = Zero::zero();
for l in locks.iter() {
if l.reasons == Reasons::All || l.reasons == Reasons::Misc {
b.misc_frozen = b.misc_frozen.max(l.amount);
unsafe {
let bounded_locks = BoundedVec::<_, T::MaxLocks>::force_from(locks.to_vec(), Some("Balances Update Locks"));

if locks.len() as u32 > T::MaxLocks::get() {
log::warn!(
target: "runtime::balances",
"Warning: A user has more currency locks than expected. \
A runtime configuration adjustment may be needed."
);
}
// No way this can fail since we do not alter the existential balances.
let res = Self::mutate_account(who, |b| {
b.misc_frozen = Zero::zero();
b.fee_frozen = Zero::zero();
for l in locks.iter() {
if l.reasons == Reasons::All || l.reasons == Reasons::Misc {
b.misc_frozen = b.misc_frozen.max(l.amount);
}
if l.reasons == Reasons::All || l.reasons == Reasons::Fee {
b.fee_frozen = b.fee_frozen.max(l.amount);
}
}
if l.reasons == Reasons::All || l.reasons == Reasons::Fee {
b.fee_frozen = b.fee_frozen.max(l.amount);
});
debug_assert!(res.is_ok());

let existed = Locks::<T, I>::contains_key(who);
if locks.is_empty() {
Locks::<T, I>::remove(who);
if existed {
// TODO: use Locks::<T, I>::hashed_key
// https://github.com/paritytech/substrate/issues/4969
system::Pallet::<T>::dec_consumers(who);
}
}
});
debug_assert!(res.is_ok());

let existed = Locks::<T, I>::contains_key(who);
if locks.is_empty() {
Locks::<T, I>::remove(who);
if existed {
// TODO: use Locks::<T, I>::hashed_key
// https://github.com/paritytech/substrate/issues/4969
system::Pallet::<T>::dec_consumers(who);
}
} else {
Locks::<T, I>::insert(who, locks);
if !existed {
if system::Pallet::<T>::inc_consumers(who).is_err() {
// No providers for the locks. This is impossible under normal circumstances
// since the funds that are under the lock will themselves be stored in the
// account and therefore will need a reference.
log::warn!(
target: "runtime::balances",
"Warning: Attempt to introduce lock consumer reference, yet no providers. \
This is unexpected but should be safe."
);
} else {
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
// since the funds that are under the lock will themselves be stored in the
// account and therefore will need a reference.
log::warn!(
target: "runtime::balances",
"Warning: Attempt to introduce lock consumer reference, yet no providers. \
This is unexpected but should be safe."
);
}
}
}
}
Expand Down
Loading