diff --git a/Cargo.lock b/Cargo.lock index f11953213d01e..a3c3f3b65c0e5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,7 +1,5 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 - [[package]] name = "Inflector" version = "0.11.4" @@ -1810,6 +1808,7 @@ dependencies = [ "frame-system", "impl-trait-for-tuples", "log", + "max-encoded-len", "once_cell", "parity-scale-codec", "parity-util-mem", @@ -3719,6 +3718,28 @@ dependencies = [ "rawpointer", ] +[[package]] +name = "max-encoded-len" +version = "3.0.0" +dependencies = [ + "impl-trait-for-tuples", + "max-encoded-len-derive", + "parity-scale-codec", + "primitive-types", + "rustversion", + "trybuild", +] + +[[package]] +name = "max-encoded-len-derive" +version = "3.0.0" +dependencies = [ + "frame-support-procedural-tools", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "maybe-uninit" version = "2.0.0" @@ -4291,6 +4312,7 @@ dependencies = [ "frame-try-runtime", "hex-literal", "log", + "max-encoded-len", "node-primitives", "pallet-assets", "pallet-authority-discovery", @@ -4624,6 +4646,7 @@ dependencies = [ "frame-benchmarking", "frame-support", "frame-system", + "max-encoded-len", "pallet-balances", "parity-scale-codec", "sp-core", @@ -4734,6 +4757,7 @@ dependencies = [ "frame-support", "frame-system", "log", + "max-encoded-len", "pallet-transaction-payment", "parity-scale-codec", "sp-core", @@ -5260,6 +5284,7 @@ dependencies = [ "frame-benchmarking", "frame-support", "frame-system", + "max-encoded-len", "pallet-balances", "pallet-utility", "parity-scale-codec", @@ -8783,6 +8808,7 @@ dependencies = [ "lazy_static", "libsecp256k1", "log", + "max-encoded-len", "merlin", "num-traits", "parity-scale-codec", @@ -9008,6 +9034,7 @@ dependencies = [ "hash256-std-hasher", "impl-trait-for-tuples", "log", + "max-encoded-len", "parity-scale-codec", "parity-util-mem", "paste 1.0.4", @@ -10433,9 +10460,9 @@ dependencies = [ [[package]] name = "trybuild" -version = "1.0.41" +version = "1.0.42" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99471a206425fba51842a9186315f32d91c56eadc21ea4c21f847b59cf778f8b" +checksum = "1768998d9a3b179411618e377dbb134c58a88cda284b0aa71c42c40660127d46" dependencies = [ "dissimilar", "glob", diff --git a/Cargo.toml b/Cargo.toml index 1b35c7181d17d..76c9d9a7b2169 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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", diff --git a/bin/node/runtime/Cargo.toml b/bin/node/runtime/Cargo.toml index 512f32d66a66c..a0659db84ea11 100644 --- a/bin/node/runtime/Cargo.toml +++ b/bin/node/runtime/Cargo.toml @@ -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" } @@ -159,6 +161,7 @@ std = [ "log/std", "frame-try-runtime/std", "sp-npos-elections/std", + "max-encoded-len/std", ] runtime-benchmarks = [ "frame-benchmarking", diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 05f75b14b9603..eb544337e3795 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -33,7 +33,7 @@ use frame_support::{ }, traits::{ Currency, Imbalance, KeyOwnerProofSystem, OnUnbalanced, LockIdentifier, - U128CurrencyToVote, + U128CurrencyToVote, MaxEncodedLen, }, }; use frame_system::{ @@ -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, diff --git a/frame/assets/Cargo.toml b/frame/assets/Cargo.toml index 7137cf1d789a2..db03959616475 100644 --- a/frame/assets/Cargo.toml +++ b/frame/assets/Cargo.toml @@ -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" } @@ -38,6 +39,7 @@ std = [ "frame-support/std", "frame-system/std", "frame-benchmarking/std", + "max-encoded-len/std", ] runtime-benchmarks = [ "frame-benchmarking", diff --git a/frame/balances/Cargo.toml b/frame/balances/Cargo.toml index 116a52151583a..667724ae3d0cc 100644 --- a/frame/balances/Cargo.toml +++ b/frame/balances/Cargo.toml @@ -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" } @@ -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"] diff --git a/frame/max-encoded-len/Cargo.toml b/frame/max-encoded-len/Cargo.toml new file mode 100644 index 0000000000000..b5f97e54ad0f0 --- /dev/null +++ b/frame/max-encoded-len/Cargo.toml @@ -0,0 +1,35 @@ +[package] +name = "max-encoded-len" +version = "3.0.0" +authors = ["Parity Technologies "] +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", +] diff --git a/frame/max-encoded-len/derive/Cargo.toml b/frame/max-encoded-len/derive/Cargo.toml new file mode 100644 index 0000000000000..c8d33bef1c6a5 --- /dev/null +++ b/frame/max-encoded-len/derive/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "max-encoded-len-derive" +version = "3.0.0" +authors = ["Parity Technologies "] +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 = [] diff --git a/frame/support/procedural/src/max_encoded_len.rs b/frame/max-encoded-len/derive/src/lib.rs similarity index 96% rename from frame/support/procedural/src/max_encoded_len.rs rename to frame/max-encoded-len/derive/src/lib.rs index 72efa446b3f4d..75484ad963204 100644 --- a/frame/support/procedural/src/max_encoded_len.rs +++ b/frame/max-encoded-len/derive/src/lib.rs @@ -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, @@ -53,8 +55,8 @@ pub fn derive_max_encoded_len(input: proc_macro::TokenStream) -> proc_macro::Tok } fn max_encoded_len_trait() -> syn::Result { - 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. diff --git a/frame/support/src/traits/max_encoded_len.rs b/frame/max-encoded-len/src/lib.rs similarity index 92% rename from frame/support/src/traits/max_encoded_len.rs rename to frame/max-encoded-len/src/lib.rs index 2f206ddcbe11d..13c99f4e5b0ce 100644 --- a/frame/support/src/traits/max_encoded_len.rs +++ b/frame/max-encoded-len/src/lib.rs @@ -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"); @@ -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. /// diff --git a/frame/support/test/tests/max_encoded_len.rs b/frame/max-encoded-len/tests/max_encoded_len.rs similarity index 98% rename from frame/support/test/tests/max_encoded_len.rs rename to frame/max-encoded-len/tests/max_encoded_len.rs index e9e74929108d4..665ac8fa98a4f 100644 --- a/frame/support/test/tests/max_encoded_len.rs +++ b/frame/max-encoded-len/tests/max_encoded_len.rs @@ -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. diff --git a/frame/support/test/tests/max_encoded_len_ui.rs b/frame/max-encoded-len/tests/max_encoded_len_ui.rs similarity index 97% rename from frame/support/test/tests/max_encoded_len_ui.rs rename to frame/max-encoded-len/tests/max_encoded_len_ui.rs index c5c0489da924f..79d6d49234ff2 100644 --- a/frame/support/test/tests/max_encoded_len_ui.rs +++ b/frame/max-encoded-len/tests/max_encoded_len_ui.rs @@ -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() { diff --git a/frame/support/test/tests/max_encoded_len_ui/not_encode.rs b/frame/max-encoded-len/tests/max_encoded_len_ui/not_encode.rs similarity index 58% rename from frame/support/test/tests/max_encoded_len_ui/not_encode.rs rename to frame/max-encoded-len/tests/max_encoded_len_ui/not_encode.rs index ed6fe94471e58..5e8eb6035547a 100644 --- a/frame/support/test/tests/max_encoded_len_ui/not_encode.rs +++ b/frame/max-encoded-len/tests/max_encoded_len_ui/not_encode.rs @@ -1,4 +1,4 @@ -use frame_support::traits::MaxEncodedLen; +use max_encoded_len::MaxEncodedLen; #[derive(MaxEncodedLen)] struct NotEncode; diff --git a/frame/support/test/tests/max_encoded_len_ui/not_encode.stderr b/frame/max-encoded-len/tests/max_encoded_len_ui/not_encode.stderr similarity index 53% rename from frame/support/test/tests/max_encoded_len_ui/not_encode.stderr rename to frame/max-encoded-len/tests/max_encoded_len_ui/not_encode.stderr index f4dbeac040843..111e63e3f1e30 100644 --- a/frame/support/test/tests/max_encoded_len_ui/not_encode.stderr +++ b/frame/max-encoded-len/tests/max_encoded_len_ui/not_encode.stderr @@ -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) diff --git a/frame/support/test/tests/max_encoded_len_ui/not_mel.rs b/frame/max-encoded-len/tests/max_encoded_len_ui/not_mel.rs similarity index 80% rename from frame/support/test/tests/max_encoded_len_ui/not_mel.rs rename to frame/max-encoded-len/tests/max_encoded_len_ui/not_mel.rs index 6116f30e5272b..cbaf820ff58e3 100644 --- a/frame/support/test/tests/max_encoded_len_ui/not_mel.rs +++ b/frame/max-encoded-len/tests/max_encoded_len_ui/not_mel.rs @@ -1,5 +1,5 @@ use codec::Encode; -use frame_support::traits::MaxEncodedLen; +use max_encoded_len::MaxEncodedLen; #[derive(Encode)] struct NotMel; diff --git a/frame/support/test/tests/max_encoded_len_ui/not_mel.stderr b/frame/max-encoded-len/tests/max_encoded_len_ui/not_mel.stderr similarity index 100% rename from frame/support/test/tests/max_encoded_len_ui/not_mel.stderr rename to frame/max-encoded-len/tests/max_encoded_len_ui/not_mel.stderr diff --git a/frame/support/test/tests/max_encoded_len_ui/union.rs b/frame/max-encoded-len/tests/max_encoded_len_ui/union.rs similarity index 70% rename from frame/support/test/tests/max_encoded_len_ui/union.rs rename to frame/max-encoded-len/tests/max_encoded_len_ui/union.rs index c685b6939e9b8..932c484b9e670 100644 --- a/frame/support/test/tests/max_encoded_len_ui/union.rs +++ b/frame/max-encoded-len/tests/max_encoded_len_ui/union.rs @@ -1,5 +1,5 @@ use codec::Encode; -use frame_support::traits::MaxEncodedLen; +use max_encoded_len::MaxEncodedLen; #[derive(Encode, MaxEncodedLen)] union Union { diff --git a/frame/support/test/tests/max_encoded_len_ui/union.stderr b/frame/max-encoded-len/tests/max_encoded_len_ui/union.stderr similarity index 100% rename from frame/support/test/tests/max_encoded_len_ui/union.stderr rename to frame/max-encoded-len/tests/max_encoded_len_ui/union.stderr diff --git a/frame/support/test/tests/max_encoded_len_ui/unsupported_variant.rs b/frame/max-encoded-len/tests/max_encoded_len_ui/unsupported_variant.rs similarity index 77% rename from frame/support/test/tests/max_encoded_len_ui/unsupported_variant.rs rename to frame/max-encoded-len/tests/max_encoded_len_ui/unsupported_variant.rs index 675f62c168a69..2fa94867471b1 100644 --- a/frame/support/test/tests/max_encoded_len_ui/unsupported_variant.rs +++ b/frame/max-encoded-len/tests/max_encoded_len_ui/unsupported_variant.rs @@ -1,5 +1,5 @@ use codec::Encode; -use frame_support::traits::MaxEncodedLen; +use max_encoded_len::MaxEncodedLen; #[derive(Encode)] struct NotMel; diff --git a/frame/support/test/tests/max_encoded_len_ui/unsupported_variant.stderr b/frame/max-encoded-len/tests/max_encoded_len_ui/unsupported_variant.stderr similarity index 100% rename from frame/support/test/tests/max_encoded_len_ui/unsupported_variant.stderr rename to frame/max-encoded-len/tests/max_encoded_len_ui/unsupported_variant.stderr diff --git a/frame/proxy/Cargo.toml b/frame/proxy/Cargo.toml index d8f7afe433cb3..992d18fcc3f86 100644 --- a/frame/proxy/Cargo.toml +++ b/frame/proxy/Cargo.toml @@ -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 } @@ -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", diff --git a/frame/support/Cargo.toml b/frame/support/Cargo.toml index 7b1179122b973..9c2831af83423 100644 --- a/frame/support/Cargo.toml +++ b/frame/support/Cargo.toml @@ -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" } @@ -55,6 +56,7 @@ std = [ "sp-state-machine", "frame-support-procedural/std", "log/std", + "max-encoded-len/std", ] runtime-benchmarks = [] try-runtime = [] diff --git a/frame/support/procedural/src/lib.rs b/frame/support/procedural/src/lib.rs index 5146b8e78380e..f6a2176d777c4 100644 --- a/frame/support/procedural/src/lib.rs +++ b/frame/support/procedural/src/lib.rs @@ -28,7 +28,6 @@ mod debug_no_bound; mod clone_no_bound; mod partial_eq_no_bound; mod default_no_bound; -mod max_encoded_len; pub(crate) use storage::INHERENT_INSTANCE_NAME; use proc_macro::TokenStream; @@ -445,9 +444,3 @@ pub fn crate_to_pallet_version(input: TokenStream) -> TokenStream { /// The number of module instances supported by the runtime, starting at index 1, /// and up to `NUMBER_OF_INSTANCE`. pub(crate) const NUMBER_OF_INSTANCE: u8 = 16; - -/// Derive `MaxEncodedLen`. -#[proc_macro_derive(MaxEncodedLen)] -pub fn derive_max_encoded_len(input: TokenStream) -> TokenStream { - max_encoded_len::derive_max_encoded_len(input) -} diff --git a/frame/support/src/traits.rs b/frame/support/src/traits.rs index cfac84aea865e..59ee29193e145 100644 --- a/frame/support/src/traits.rs +++ b/frame/support/src/traits.rs @@ -81,33 +81,5 @@ pub use dispatch::{EnsureOrigin, OriginTrait, UnfilteredDispatchable}; mod voting; pub use voting::{CurrencyToVote, SaturatingCurrencyToVote, U128CurrencyToVote}; -mod max_encoded_len; -// This looks like an overlapping import/export, but it isn't: -// macros and traits live in distinct namespaces. +// for backwards-compatibility with existing imports pub use max_encoded_len::MaxEncodedLen; -/// Derive [`MaxEncodedLen`][max_encoded_len::MaxEncodedLen]. -/// -/// # Examples -/// -/// ``` -/// # use codec::Encode; -/// # use frame_support::traits::MaxEncodedLen; -/// #[derive(Encode, MaxEncodedLen)] -/// struct TupleStruct(u8, u32); -/// -/// assert_eq!(TupleStruct::max_encoded_len(), u8::max_encoded_len() + u32::max_encoded_len()); -/// ``` -/// -/// ``` -/// # use codec::Encode; -/// # use frame_support::traits::MaxEncodedLen; -/// #[derive(Encode, MaxEncodedLen)] -/// enum GenericEnum { -/// A, -/// B(T), -/// } -/// -/// assert_eq!(GenericEnum::::max_encoded_len(), u8::max_encoded_len() + u8::max_encoded_len()); -/// assert_eq!(GenericEnum::::max_encoded_len(), u8::max_encoded_len() + u128::max_encoded_len()); -/// ``` -pub use frame_support_procedural::MaxEncodedLen; diff --git a/primitives/core/Cargo.toml b/primitives/core/Cargo.toml index 3d9cf1287e051..071f6821f2930 100644 --- a/primitives/core/Cargo.toml +++ b/primitives/core/Cargo.toml @@ -40,6 +40,7 @@ parity-util-mem = { version = "0.9.0", default-features = false, features = ["pr futures = { version = "0.3.1", optional = true } dyn-clonable = { version = "0.9.0", optional = true } thiserror = { version = "1.0.21", optional = true } +max-encoded-len = { version = "3.0.0", default-features = false, path = "../../frame/max-encoded-len", features = [ "derive" ] } # full crypto ed25519-dalek = { version = "1.0.1", default-features = false, features = ["u64_backend", "alloc"], optional = true } @@ -114,6 +115,7 @@ std = [ "futures/thread-pool", "libsecp256k1/std", "dyn-clonable", + "max-encoded-len/std", ] # This feature enables all crypto primitives for `no_std` builds like microcontrollers diff --git a/primitives/core/src/crypto.rs b/primitives/core/src/crypto.rs index 7446ab25ce4be..8d8b7b967ac63 100644 --- a/primitives/core/src/crypto.rs +++ b/primitives/core/src/crypto.rs @@ -20,6 +20,7 @@ // end::description[] use crate::{sr25519, ed25519}; +use max_encoded_len::MaxEncodedLen; use sp_std::hash::Hash; use sp_std::vec::Vec; use sp_std::str; @@ -692,7 +693,7 @@ pub trait Public: } /// An opaque 32-byte cryptographic identifier. -#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Default, Encode, Decode)] +#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Default, Encode, Decode, MaxEncodedLen)] #[cfg_attr(feature = "std", derive(Hash))] pub struct AccountId32([u8; 32]); diff --git a/primitives/runtime/Cargo.toml b/primitives/runtime/Cargo.toml index 7d33e7fa62d2d..646c7e4d10abe 100644 --- a/primitives/runtime/Cargo.toml +++ b/primitives/runtime/Cargo.toml @@ -29,6 +29,7 @@ impl-trait-for-tuples = "0.2.1" parity-util-mem = { version = "0.9.0", default-features = false, features = ["primitive-types"] } hash256-std-hasher = { version = "0.15.2", default-features = false } either = { version = "1.5", default-features = false } +max-encoded-len = { version = "3.0.0", default-features = false, path = "../../frame/max-encoded-len", features = [ "derive" ] } [dev-dependencies] serde_json = "1.0.41" @@ -55,4 +56,5 @@ std = [ "parity-util-mem/std", "hash256-std-hasher/std", "either/use_std", + "max-encoded-len/std", ] diff --git a/primitives/runtime/src/traits.rs b/primitives/runtime/src/traits.rs index 41820d8cb4a1c..968f475f02252 100644 --- a/primitives/runtime/src/traits.rs +++ b/primitives/runtime/src/traits.rs @@ -40,6 +40,7 @@ pub use sp_arithmetic::traits::{ use sp_application_crypto::AppKey; use impl_trait_for_tuples::impl_for_tuples; use crate::DispatchResult; +use max_encoded_len::MaxEncodedLen; /// A lazy value. pub trait Lazy { @@ -386,7 +387,7 @@ impl::Output> { /// The hash type produced. type Output: Member + MaybeSerializeDeserialize + Debug + sp_std::hash::Hash - + AsRef<[u8]> + AsMut<[u8]> + Copy + Default + Encode + Decode; + + AsRef<[u8]> + AsMut<[u8]> + Copy + Default + Encode + Decode + MaxEncodedLen; /// Produce the hash of some byte-slice. fn hash(s: &[u8]) -> Self::Output {