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

Simple MaxBoundedLen Implementations #8793

Merged
60 commits merged into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
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>>,
Copy link
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
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 @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

due to the usage of bounded vec here we should make sure that the limit is not exceeded before migration.
Also any modification of T::StringLimit should ensure the on chain items don't exceed the limit.

If these consideration are too strong, we should move to WeakBoundedVec.

Also we could use BoundedVec for the call argument directly instead of Vec + conversion here. But I think this can be done later, as UI needs to handle the new type.

.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