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

Commit

Permalink
Move MaxBoundedLen into its own crate (#8814)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
coriolinus and shawntabrizi authored May 15, 2021
1 parent 43edcac commit 2001d32
Show file tree
Hide file tree
Showing 28 changed files with 143 additions and 60 deletions.
35 changes: 31 additions & 4 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ members = [
"frame/im-online",
"frame/indices",
"frame/lottery",
"frame/max-encoded-len",
"frame/max-encoded-len/derive",
"frame/membership",
"frame/merkle-mountain-range",
"frame/merkle-mountain-range/primitives",
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 = "../../../frame/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
4 changes: 2 additions & 2 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 @@ -252,7 +252,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
2 changes: 2 additions & 0 deletions frame/balances/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ frame-benchmarking = { version = "3.1.0", default-features = false, path = "../b
frame-support = { version = "3.0.0", default-features = false, path = "../support" }
frame-system = { version = "3.0.0", default-features = false, path = "../system" }
log = { version = "0.4.14", default-features = false }
max-encoded-len = { version = "3.0.0", default-features = false, path = "../max-encoded-len", features = [ "derive" ] }

[dev-dependencies]
sp-io = { version = "3.0.0", path = "../../primitives/io" }
Expand All @@ -36,6 +37,7 @@ std = [
"frame-support/std",
"frame-system/std",
"log/std",
"max-encoded-len/std",
]
runtime-benchmarks = ["frame-benchmarking"]
try-runtime = ["frame-support/try-runtime"]
35 changes: 35 additions & 0 deletions frame/max-encoded-len/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[package]
name = "max-encoded-len"
version = "3.0.0"
authors = ["Parity Technologies <[email protected]>"]
edition = "2018"
license = "Apache-2.0"
homepage = "https://substrate.dev"
repository = "https://github.com/paritytech/substrate/"
description = "Trait MaxEncodedLen bounds the max encoded length of an item."


[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
impl-trait-for-tuples = "0.2.1"
max-encoded-len-derive = { package = "max-encoded-len-derive", version = "3.0.0", path = "derive", default-features = false, optional = true }
primitive-types = { version = "0.9.0", default-features = false, features = ["codec"] }

[dev-dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = [ "derive" ] }
rustversion = "1.0.4"
trybuild = "1.0.42"

[features]
default = [
"derive",
"std",
]
derive = [
"max-encoded-len-derive",
]
std = [
"codec/std",
"max-encoded-len-derive/std",
"primitive-types/std",
]
25 changes: 25 additions & 0 deletions frame/max-encoded-len/derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "max-encoded-len-derive"
version = "3.0.0"
authors = ["Parity Technologies <[email protected]>"]
edition = "2018"
license = "Apache-2.0"
homepage = "https://substrate.dev"
repository = "https://github.com/paritytech/substrate/"
description = "Derive support for MaxEncodedLen"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[lib]
proc-macro = true

[dependencies]
frame-support-procedural-tools = { version = "3.0.0", path = "../../support/procedural/tools" }
proc-macro2 = "1.0.6"
quote = "1.0.3"
syn = { version = "1.0.58", features = ["full"] }

[features]
default = ["std"]
std = []
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ use syn::{
parse_quote, spanned::Spanned,
};

/// impl for `#[derive(MaxEncodedLen)]`

/// Derive `MaxEncodedLen`.
#[proc_macro_derive(MaxEncodedLen)]
pub fn derive_max_encoded_len(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input: DeriveInput = match syn::parse(input) {
Ok(input) => input,
Expand Down Expand Up @@ -53,8 +55,8 @@ pub fn derive_max_encoded_len(input: proc_macro::TokenStream) -> proc_macro::Tok
}

fn max_encoded_len_trait() -> syn::Result<TraitBound> {
let frame_support = generate_crate_access_2018("frame-support")?;
Ok(parse_quote!(#frame_support::traits::MaxEncodedLen))
let mel = generate_crate_access_2018("max-encoded-len")?;
Ok(parse_quote!(#mel::MaxEncodedLen))
}

// Add a bound `T: MaxEncodedLen` to every type parameter T.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of Substrate.

// Copyright (C) 2019-2021 Parity Technologies (UK) Ltd.
// Copyright (C) 2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -15,10 +15,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! `trait MaxEncodedLen` bounds the max encoded length of items.

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

use codec::{Compact, Encode};
use impl_trait_for_tuples::impl_for_tuples;
use sp_std::{mem, marker::PhantomData};
use sp_core::{H160, H256, H512};
use core::{mem, marker::PhantomData};
use primitive_types::{H160, H256, H512};

#[cfg(feature = "derive")]
pub use max_encoded_len_derive::MaxEncodedLen;

/// Items implementing `MaxEncodedLen` have a statically known maximum encoded size.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

//! Tests for MaxEncodedLen derive macro

use frame_support::traits::MaxEncodedLen;
#![cfg(feature = "derive")]

use max_encoded_len::MaxEncodedLen;
use codec::{Compact, Encode};

// These structs won't even compile if the macro isn't working right.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#[cfg(feature = "derive")]
#[rustversion::attr(not(stable), ignore)]
#[test]
fn derive_no_bound_ui() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use frame_support::traits::MaxEncodedLen;
use max_encoded_len::MaxEncodedLen;

#[derive(MaxEncodedLen)]
struct NotEncode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
error[E0277]: the trait bound `NotEncode: WrapperTypeEncode` is not satisfied
error[E0277]: the trait bound `NotEncode: parity_scale_codec::codec::WrapperTypeEncode` is not satisfied
--> $DIR/not_encode.rs:3:10
|
3 | #[derive(MaxEncodedLen)]
| ^^^^^^^^^^^^^ the trait `WrapperTypeEncode` is not implemented for `NotEncode`
| ^^^^^^^^^^^^^ the trait `parity_scale_codec::codec::WrapperTypeEncode` is not implemented for `NotEncode`
|
::: $WORKSPACE/frame/support/src/traits/max_encoded_len.rs
::: $WORKSPACE/frame/max-encoded-len/src/lib.rs
|
| pub trait MaxEncodedLen: Encode {
| ------ required by this bound in `MaxEncodedLen`
|
= note: required because of the requirements on the impl of `frame_support::dispatch::Encode` for `NotEncode`
= note: required because of the requirements on the impl of `parity_scale_codec::codec::Encode` for `NotEncode`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use codec::Encode;
use frame_support::traits::MaxEncodedLen;
use max_encoded_len::MaxEncodedLen;

#[derive(Encode)]
struct NotMel;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use codec::Encode;
use frame_support::traits::MaxEncodedLen;
use max_encoded_len::MaxEncodedLen;

#[derive(Encode, MaxEncodedLen)]
union Union {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use codec::Encode;
use frame_support::traits::MaxEncodedLen;
use max_encoded_len::MaxEncodedLen;

#[derive(Encode)]
struct NotMel;
Expand Down
4 changes: 3 additions & 1 deletion frame/proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ sp-core = { version = "3.0.0", default-features = false, path = "../../primitive
sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" }
sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" }
max-encoded-len = { version = "3.0.0", default-features = false, path = "../max-encoded-len", features = [ "derive" ] }

frame-benchmarking = { version = "3.1.0", default-features = false, path = "../benchmarking", optional = true }

Expand All @@ -36,7 +37,8 @@ std = [
"frame-support/std",
"frame-system/std",
"sp-std/std",
"sp-io/std"
"sp-io/std",
"max-encoded-len/std",
]
runtime-benchmarks = [
"frame-benchmarking",
Expand Down
2 changes: 2 additions & 0 deletions frame/support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ targets = ["x86_64-unknown-linux-gnu"]
serde = { version = "1.0.101", optional = true, features = ["derive"] }
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
frame-metadata = { version = "13.0.0", default-features = false, path = "../metadata" }
max-encoded-len = { version = "3.0.0", default-features = false, path = "../max-encoded-len", features = [ "derive" ] }
sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" }
sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" }
Expand Down Expand Up @@ -55,6 +56,7 @@ std = [
"sp-state-machine",
"frame-support-procedural/std",
"log/std",
"max-encoded-len/std",
]
runtime-benchmarks = []
try-runtime = []
Loading

0 comments on commit 2001d32

Please sign in to comment.