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

Commit

Permalink
Simple MaxBoundedLen Implementations (#8793)
Browse files Browse the repository at this point in the history
* 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 73623e9.

* 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>
  • Loading branch information
4 people authored May 28, 2021
1 parent c2ec5bc commit 050e4ac
Show file tree
Hide file tree
Showing 59 changed files with 619 additions and 189 deletions.
41 changes: 35 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ members = [
"utils/frame/rpc/system",
"utils/prometheus",
"utils/wasm-builder",
# temp deps
"max-encoded-len",
"max-encoded-len/derive",
]

# The list of dependencies below (which can be both direct and indirect dependencies) are crates
Expand Down
3 changes: 3 additions & 0 deletions bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ pallet-transaction-payment = { version = "3.0.0", default-features = false, path
pallet-transaction-payment-rpc-runtime-api = { version = "3.0.0", default-features = false, path = "../../../frame/transaction-payment/rpc/runtime-api/" }
pallet-vesting = { version = "3.0.0", default-features = false, path = "../../../frame/vesting" }

max-encoded-len = { version = "3.0.0", default-features = false, path = "../../../max-encoded-len", features = [ "derive" ] }

[build-dependencies]
substrate-wasm-builder = { version = "4.0.0", path = "../../../utils/wasm-builder" }

Expand Down Expand Up @@ -159,6 +161,7 @@ std = [
"log/std",
"frame-try-runtime/std",
"sp-npos-elections/std",
"max-encoded-len/std",
]
runtime-benchmarks = [
"frame-benchmarking",
Expand Down
8 changes: 4 additions & 4 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use frame_support::{
},
traits::{
Currency, Imbalance, KeyOwnerProofSystem, OnUnbalanced, LockIdentifier,
U128CurrencyToVote,
U128CurrencyToVote, MaxEncodedLen,
},
};
use frame_system::{
Expand Down Expand Up @@ -114,8 +114,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 265,
impl_version: 1,
spec_version: 266,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 2,
};
Expand Down Expand Up @@ -253,7 +253,7 @@ parameter_types! {
}

/// The type used to represent the kinds of proxying allowed.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, RuntimeDebug)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, RuntimeDebug, MaxEncodedLen)]
pub enum ProxyType {
Any,
NonTransfer,
Expand Down
2 changes: 2 additions & 0 deletions frame/assets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -38,6 +39,7 @@ std = [
"frame-support/std",
"frame-system/std",
"frame-benchmarking/std",
"max-encoded-len/std",
]
runtime-benchmarks = [
"frame-benchmarking",
Expand Down
51 changes: 36 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,7 @@ pub mod pallet {

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

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

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

#[pallet::storage]
Expand All @@ -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]
Expand Down Expand Up @@ -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);
Expand All @@ -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,
});
Expand Down Expand Up @@ -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,
});
Expand Down
17 changes: 9 additions & 8 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
}

/// 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 @@ -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,
Expand All @@ -87,24 +88,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
Loading

0 comments on commit 050e4ac

Please sign in to comment.