From 720f23a641463b7ef7136748a9f38e88075d4827 Mon Sep 17 00:00:00 2001 From: haerdib Date: Tue, 27 Dec 2022 10:38:50 +0100 Subject: [PATCH 01/24] add serde_full feature flag add serde_full to sp_runtime add space to toml add serde_full to application-crypto add serde_full to arithmetic fix arithmetic add serde full to beefy add serde full to consensus add serde_full to core add serdefull to finality grandpa add serde_full to several primitives crates make rpc no_std compatible add scale info to runtime make serializer no_std compatible add serde full to storage add full serde to version add serde full to weights add all serde_full features add . to comment add missing impl-serde fix no-std build fix build add full_crypto to serde_full serde_full also implements crypto full_serde does not work with full_crytpo. needs std no no_std serde impl possible also for crypto std is necessary no serde full for application crypto fix arithmetic fix tomls fix some things impl fmt for Signature add serialize to Public add impl_maybe_marker_serde_full fix sp-application-crypto toml add serde feature flag fix clippy fix toml grandpa fix grandpa rename if_std to if_serde keystore is not no_std compatible make keystore vrf no_std compatible fix nopos-elections fix rpc fix serializer fix test-primitives fix version add comment add serde full only import for format string remove all(serde_full and full_crypot) as serde_full enforces full_crypto make comment better readable even better comment clean up rpc toml clean up toml clean up serializer toml clean up storage toml fix std build update .lock fix sp-version move sp_std import test extern crate alloc replace sp_std with core add missing core sp_core: serde feature do not enforce full crypto application-crypto: serde feature do not enforce full crypto rename serde_full to serde add dep:serde and alloc to default feature add full_crypto and remove unnecessary debu/fmt impls for serde update comment remove obolsete change in display AccountId32 remove extra changes minimize diff revert keystore changes remove std from keystore remove full-crypto feature fix serde import fix comment fix feature = serde --- Cargo.lock | 3 + primitives/application-crypto/Cargo.toml | 11 +++- primitives/application-crypto/src/lib.rs | 33 ++++++---- primitives/arithmetic/Cargo.toml | 10 ++- primitives/arithmetic/src/fixed_point.rs | 13 ++-- primitives/arithmetic/src/lib.rs | 4 +- primitives/arithmetic/src/per_things.rs | 4 +- primitives/consensus/babe/Cargo.toml | 14 ++++- primitives/consensus/babe/src/lib.rs | 6 +- primitives/consensus/beefy/Cargo.toml | 13 +++- primitives/consensus/beefy/src/mmr.rs | 2 +- primitives/consensus/grandpa/Cargo.toml | 13 +++- primitives/consensus/grandpa/src/lib.rs | 6 +- primitives/consensus/slots/Cargo.toml | 10 ++- primitives/consensus/slots/src/lib.rs | 2 +- primitives/core/Cargo.toml | 24 +++++-- primitives/core/src/crypto.rs | 62 ++++++++++--------- primitives/core/src/ecdsa.rs | 14 +++-- primitives/core/src/ed25519.rs | 14 +++-- primitives/core/src/lib.rs | 51 +++++++++++++-- primitives/core/src/offchain/mod.rs | 6 +- primitives/core/src/sr25519.rs | 29 +++++---- primitives/merkle-mountain-range/Cargo.toml | 12 +++- primitives/merkle-mountain-range/src/lib.rs | 4 +- primitives/npos-elections/Cargo.toml | 13 +++- primitives/npos-elections/src/assignments.rs | 6 +- primitives/npos-elections/src/lib.rs | 6 +- primitives/rpc/Cargo.toml | 16 ++++- primitives/rpc/src/lib.rs | 2 + primitives/rpc/src/list.rs | 1 + primitives/rpc/src/number.rs | 2 +- primitives/runtime/Cargo.toml | 14 ++++- primitives/runtime/src/generic/block.rs | 14 ++--- primitives/runtime/src/generic/digest.rs | 10 +-- primitives/runtime/src/generic/era.rs | 4 +- primitives/runtime/src/generic/header.rs | 14 ++--- .../src/generic/unchecked_extrinsic.rs | 6 +- .../runtime/src/legacy/byte_sized_error.rs | 10 +-- primitives/runtime/src/lib.rs | 30 ++++----- primitives/runtime/src/runtime_string.rs | 9 ++- primitives/runtime/src/traits.rs | 12 ++-- .../runtime/src/transaction_validity.rs | 6 +- primitives/serializer/Cargo.toml | 13 +++- primitives/serializer/src/lib.rs | 10 ++- primitives/storage/Cargo.toml | 18 +++++- primitives/storage/src/lib.rs | 22 +++---- primitives/test-primitives/Cargo.toml | 14 ++++- primitives/test-primitives/src/lib.rs | 3 +- primitives/version/Cargo.toml | 15 +++-- primitives/version/src/lib.rs | 13 ++-- primitives/weights/Cargo.toml | 12 +++- primitives/weights/src/lib.rs | 6 +- primitives/weights/src/weight_v2.rs | 2 +- 53 files changed, 452 insertions(+), 211 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 42ad1855248a0..44f3d6d785c52 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10821,6 +10821,7 @@ dependencies = [ "serde", "serde_json", "sp-core", + "sp-std", ] [[package]] @@ -10929,6 +10930,7 @@ version = "4.0.0-dev" dependencies = [ "serde", "serde_json", + "sp-std", ] [[package]] @@ -11006,6 +11008,7 @@ dependencies = [ "sp-application-crypto", "sp-core", "sp-runtime", + "sp-std", ] [[package]] diff --git a/primitives/application-crypto/Cargo.toml b/primitives/application-crypto/Cargo.toml index 37b58bc2bf649..dc97948fdf5c8 100644 --- a/primitives/application-crypto/Cargo.toml +++ b/primitives/application-crypto/Cargo.toml @@ -18,7 +18,7 @@ targets = ["x86_64-unknown-linux-gnu"] sp-core = { version = "7.0.0", default-features = false, path = "../core" } codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] } scale-info = { version = "2.5.0", default-features = false, features = ["derive"] } -serde = { version = "1.0.136", optional = true, features = ["derive"] } +serde = { version = "1.0.136", default-features = false, optional = true, features = ["derive", "alloc"] } sp-std = { version = "5.0.0", default-features = false, path = "../std" } sp-io = { version = "7.0.0", default-features = false, path = "../io" } @@ -29,11 +29,18 @@ std = [ "sp-core/std", "codec/std", "scale-info/std", - "serde", + "serde/std", "sp-std/std", "sp-io/std", ] +# Serde support without relying on std features. +serde = [ + "dep:serde", + "sp-core/serde", + "scale-info/serde", +] + # This feature enables all crypto primitives for `no_std` builds like microcontrollers # or Intel SGX. # For the regular wasm runtime builds this should not be used. diff --git a/primitives/application-crypto/src/lib.rs b/primitives/application-crypto/src/lib.rs index 5e77795199dd9..95645a172f6e4 100644 --- a/primitives/application-crypto/src/lib.rs +++ b/primitives/application-crypto/src/lib.rs @@ -21,9 +21,11 @@ #![cfg_attr(not(feature = "std"), no_std)] pub use sp_core::crypto::{key_types, CryptoTypeId, KeyTypeId}; -#[doc(hidden)] #[cfg(feature = "full_crypto")] -pub use sp_core::crypto::{DeriveError, DeriveJunction, Pair, SecretStringError, Ss58Codec}; +pub use sp_core::crypto::{DeriveError, Pair, SecretStringError}; +#[doc(hidden)] +#[cfg(any(feature = "full_crypto", feature = "serde"))] +pub use sp_core::crypto::{DeriveJunction, Ss58Codec}; #[doc(hidden)] pub use sp_core::{ self, @@ -36,7 +38,7 @@ pub use codec; #[doc(hidden)] pub use scale_info; #[doc(hidden)] -#[cfg(feature = "std")] +#[cfg(feature = "serde")] pub use serde; #[doc(hidden)] pub use sp_std::{ops::Deref, vec::Vec}; @@ -278,7 +280,7 @@ macro_rules! app_crypto_public_not_full_crypto { #[macro_export] macro_rules! app_crypto_public_common { ($public:ty, $sig:ty, $key_type:expr, $crypto_type:expr) => { - $crate::app_crypto_public_common_if_std!(); + $crate::app_crypto_public_common_if_serde!(); impl AsRef<[u8]> for Public { fn as_ref(&self) -> &[u8] { @@ -312,11 +314,11 @@ macro_rules! app_crypto_public_common { }; } -/// Implements traits for the public key type if `feature = "std"` is enabled. -#[cfg(feature = "std")] +/// Implements traits for the public key type if `feature = "serde"` is enabled. +#[cfg(feature = "serde")] #[doc(hidden)] #[macro_export] -macro_rules! app_crypto_public_common_if_std { +macro_rules! app_crypto_public_common_if_serde { () => { impl $crate::Derive for Public { fn derive>( @@ -327,15 +329,15 @@ macro_rules! app_crypto_public_common_if_std { } } - impl std::fmt::Display for Public { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + impl core::fmt::Display for Public { + fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { use $crate::Ss58Codec; write!(f, "{}", self.0.to_ss58check()) } } impl $crate::serde::Serialize for Public { - fn serialize(&self, serializer: S) -> std::result::Result + fn serialize(&self, serializer: S) -> core::result::Result where S: $crate::serde::Serializer, { @@ -345,11 +347,16 @@ macro_rules! app_crypto_public_common_if_std { } impl<'de> $crate::serde::Deserialize<'de> for Public { - fn deserialize(deserializer: D) -> std::result::Result + fn deserialize(deserializer: D) -> core::result::Result where D: $crate::serde::Deserializer<'de>, { + #[cfg(not(feature = "std"))] + extern crate alloc; + #[cfg(not(feature = "std"))] + use alloc::{format, string::String}; use $crate::Ss58Codec; + Public::from_ss58check(&String::deserialize(deserializer)?) .map_err(|e| $crate::serde::de::Error::custom(format!("{:?}", e))) } @@ -357,10 +364,10 @@ macro_rules! app_crypto_public_common_if_std { }; } -#[cfg(not(feature = "std"))] +#[cfg(not(feature = "serde"))] #[doc(hidden)] #[macro_export] -macro_rules! app_crypto_public_common_if_std { +macro_rules! app_crypto_public_common_if_serde { () => { impl $crate::Derive for Public {} }; diff --git a/primitives/arithmetic/Cargo.toml b/primitives/arithmetic/Cargo.toml index 92eebd4a6d8d6..66b58a5dab827 100644 --- a/primitives/arithmetic/Cargo.toml +++ b/primitives/arithmetic/Cargo.toml @@ -21,7 +21,7 @@ codec = { package = "parity-scale-codec", version = "3.2.2", default-features = integer-sqrt = "0.1.2" num-traits = { version = "0.2.8", default-features = false } scale-info = { version = "2.5.0", default-features = false, features = ["derive"] } -serde = { version = "1.0.136", features = ["derive"], optional = true } +serde = { version = "1.0.136", default-features = false, features = ["derive", "alloc"], optional = true } static_assertions = "1.1.0" sp-std = { version = "5.0.0", default-features = false, path = "../std" } @@ -37,9 +37,15 @@ std = [ "codec/std", "num-traits/std", "scale-info/std", - "serde", + "serde/std", "sp-std/std", ] +# Serde support without relying on std features. +serde = [ + "dep:serde", + "scale-info/serde", + "serde/alloc", +] [[bench]] name = "bench" diff --git a/primitives/arithmetic/src/fixed_point.rs b/primitives/arithmetic/src/fixed_point.rs index 67fbd5bc79c88..4440b1864bde9 100644 --- a/primitives/arithmetic/src/fixed_point.rs +++ b/primitives/arithmetic/src/fixed_point.rs @@ -32,9 +32,12 @@ use sp_std::{ prelude::*, }; -#[cfg(feature = "std")] +#[cfg(feature = "serde")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; +#[cfg(all(not(feature = "std"), feature = "serde"))] +use sp_std::alloc::{string::String, string::ToString}; + /// Integer types that can be used to interact with `FixedPointNumber` implementations. pub trait FixedPointOperand: Copy @@ -928,14 +931,14 @@ macro_rules! implement_fixed { } } - #[cfg(feature = "std")] + #[cfg(feature = "serde")] impl sp_std::fmt::Display for $name { fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result { write!(f, "{}", self.0) } } - #[cfg(feature = "std")] + #[cfg(feature = "serde")] impl sp_std::str::FromStr for $name { type Err = &'static str; @@ -948,7 +951,7 @@ macro_rules! implement_fixed { // Manual impl `Serialize` as serde_json does not support i128. // TODO: remove impl if issue https://github.com/serde-rs/json/issues/548 fixed. - #[cfg(feature = "std")] + #[cfg(feature = "serde")] impl Serialize for $name { fn serialize(&self, serializer: S) -> Result where @@ -960,7 +963,7 @@ macro_rules! implement_fixed { // Manual impl `Deserialize` as serde_json does not support i128. // TODO: remove impl if issue https://github.com/serde-rs/json/issues/548 fixed. - #[cfg(feature = "std")] + #[cfg(feature = "serde")] impl<'de> Deserialize<'de> for $name { fn deserialize(deserializer: D) -> Result where diff --git a/primitives/arithmetic/src/lib.rs b/primitives/arithmetic/src/lib.rs index e7ba08a532909..d2eceafab5ed0 100644 --- a/primitives/arithmetic/src/lib.rs +++ b/primitives/arithmetic/src/lib.rs @@ -53,12 +53,12 @@ use traits::{BaseArithmetic, One, SaturatedConversion, Unsigned, Zero}; use codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; -#[cfg(feature = "std")] +#[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; /// Arithmetic errors. #[derive(Eq, PartialEq, Clone, Copy, Encode, Decode, Debug, TypeInfo, MaxEncodedLen)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum ArithmeticError { /// Underflow. Underflow, diff --git a/primitives/arithmetic/src/per_things.rs b/primitives/arithmetic/src/per_things.rs index 611a4e05e1027..fe88b72e24c22 100644 --- a/primitives/arithmetic/src/per_things.rs +++ b/primitives/arithmetic/src/per_things.rs @@ -15,7 +15,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#[cfg(feature = "std")] +#[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; use crate::traits::{ @@ -556,7 +556,7 @@ macro_rules! implement_per_thing { /// A fixed point representation of a number in the range [0, 1]. /// #[doc = $title] - #[cfg_attr(feature = "std", derive(Serialize, Deserialize))] + #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Encode, Copy, Clone, PartialEq, Eq, codec::MaxEncodedLen, PartialOrd, Ord, scale_info::TypeInfo)] pub struct $name($type); diff --git a/primitives/consensus/babe/Cargo.toml b/primitives/consensus/babe/Cargo.toml index 7e171811a7fe2..f2dd2a78d1386 100644 --- a/primitives/consensus/babe/Cargo.toml +++ b/primitives/consensus/babe/Cargo.toml @@ -16,7 +16,7 @@ targets = ["x86_64-unknown-linux-gnu"] async-trait = { version = "0.1.57", optional = true } codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false } scale-info = { version = "2.5.0", default-features = false, features = ["derive"] } -serde = { version = "1.0.136", features = ["derive"], optional = true } +serde = { version = "1.0.136", default-features = false, features = ["derive", "alloc"], optional = true } sp-api = { version = "4.0.0-dev", default-features = false, path = "../../api" } sp-application-crypto = { version = "7.0.0", default-features = false, path = "../../application-crypto" } sp-consensus = { version = "0.10.0-dev", optional = true, path = "../common" } @@ -34,7 +34,7 @@ std = [ "async-trait", "codec/std", "scale-info/std", - "serde", + "serde/std", "sp-api/std", "sp-application-crypto/std", "sp-consensus", @@ -46,3 +46,13 @@ std = [ "sp-std/std", "sp-timestamp", ] + +# Serde support without relying on std features. +serde = [ + "dep:serde", + "scale-info/serde", + "sp-application-crypto/serde", + "sp-consensus-slots/serde", + "sp-core/serde", + "sp-runtime/serde", +] diff --git a/primitives/consensus/babe/src/lib.rs b/primitives/consensus/babe/src/lib.rs index 37cf0c3f0b677..01fc75e3e7605 100644 --- a/primitives/consensus/babe/src/lib.rs +++ b/primitives/consensus/babe/src/lib.rs @@ -25,7 +25,7 @@ pub mod inherents; use codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; -#[cfg(feature = "std")] +#[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; use sp_runtime::{traits::Header, ConsensusEngineId, RuntimeDebug}; use sp_std::vec::Vec; @@ -210,7 +210,7 @@ impl BabeConfiguration { /// Types of allowed slots. #[derive(Clone, Copy, PartialEq, Eq, Encode, Decode, RuntimeDebug, MaxEncodedLen, TypeInfo)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum AllowedSlots { /// Only allow primary slots. PrimarySlots, @@ -234,7 +234,7 @@ impl AllowedSlots { /// Configuration data used by the BABE consensus engine that may change with epochs. #[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, MaxEncodedLen, TypeInfo)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct BabeEpochConfiguration { /// A constant value that is used in the threshold calculation formula. /// Expressed as a rational where the first member of the tuple is the diff --git a/primitives/consensus/beefy/Cargo.toml b/primitives/consensus/beefy/Cargo.toml index 144c8452d71b0..d811563c02086 100644 --- a/primitives/consensus/beefy/Cargo.toml +++ b/primitives/consensus/beefy/Cargo.toml @@ -13,7 +13,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] } -serde = { version = "1.0.136", optional = true, features = ["derive"] } +serde = { version = "1.0.136", default-features = false, optional = true, features = ["derive", "alloc"] } scale-info = { version = "2.5.0", default-features = false, features = ["derive"] } sp-api = { version = "4.0.0-dev", default-features = false, path = "../../api" } sp-application-crypto = { version = "7.0.0", default-features = false, path = "../../application-crypto" } @@ -34,7 +34,7 @@ default = ["std"] std = [ "codec/std", "scale-info/std", - "serde", + "serde/std", "sp-api/std", "sp-application-crypto/std", "sp-core/std", @@ -43,3 +43,12 @@ std = [ "sp-runtime/std", "sp-std/std", ] + +# Serde support without relying on std features. +serde = [ + "dep:serde", + "scale-info/serde", + "sp-application-crypto/serde", + "sp-core/serde", + "sp-runtime/serde", +] diff --git a/primitives/consensus/beefy/src/mmr.rs b/primitives/consensus/beefy/src/mmr.rs index 465008dc22499..c303cae2fdcc7 100644 --- a/primitives/consensus/beefy/src/mmr.rs +++ b/primitives/consensus/beefy/src/mmr.rs @@ -101,7 +101,7 @@ impl MmrLeafVersion { /// Details of a BEEFY authority set. #[derive(Debug, Default, PartialEq, Eq, Clone, Encode, Decode, TypeInfo, MaxEncodedLen)] -#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct BeefyAuthoritySet { /// Id of the set. /// diff --git a/primitives/consensus/grandpa/Cargo.toml b/primitives/consensus/grandpa/Cargo.toml index 446471da8361a..a1846aa76ac1f 100644 --- a/primitives/consensus/grandpa/Cargo.toml +++ b/primitives/consensus/grandpa/Cargo.toml @@ -18,7 +18,7 @@ codec = { package = "parity-scale-codec", version = "3.2.2", default-features = grandpa = { package = "finality-grandpa", version = "0.16.2", default-features = false, features = ["derive-codec"] } log = { version = "0.4.17", default-features = false } scale-info = { version = "2.5.0", default-features = false, features = ["derive"] } -serde = { version = "1.0.136", features = ["derive"], optional = true } +serde = { version = "1.0.136", features = ["derive", "alloc"], default-features = false, optional = true } sp-api = { version = "4.0.0-dev", default-features = false, path = "../../api" } sp-application-crypto = { version = "7.0.0", default-features = false, path = "../../application-crypto" } sp-core = { version = "7.0.0", default-features = false, path = "../../core" } @@ -33,7 +33,7 @@ std = [ "grandpa/std", "log/std", "scale-info/std", - "serde", + "serde/std", "sp-api/std", "sp-application-crypto/std", "sp-core/std", @@ -41,3 +41,12 @@ std = [ "sp-runtime/std", "sp-std/std", ] + +# Serde support without relying on std features. +serde = [ + "dep:serde", + "scale-info/serde", + "sp-application-crypto/serde", + "sp-core/serde", + "sp-runtime/serde", +] diff --git a/primitives/consensus/grandpa/src/lib.rs b/primitives/consensus/grandpa/src/lib.rs index 728ce3092888b..baeaee4738e48 100644 --- a/primitives/consensus/grandpa/src/lib.rs +++ b/primitives/consensus/grandpa/src/lib.rs @@ -22,7 +22,7 @@ #[cfg(not(feature = "std"))] extern crate alloc; -#[cfg(feature = "std")] +#[cfg(feature = "serde")] use serde::Serialize; use codec::{Codec, Decode, Encode, Input}; @@ -140,8 +140,8 @@ pub struct GrandpaJustification { } /// A scheduled change of authority set. -#[cfg_attr(feature = "std", derive(Serialize))] #[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)] +#[cfg_attr(feature = "serde", derive(Serialize))] pub struct ScheduledChange { /// The new authorities after the change, along with their respective weights. pub next_authorities: AuthorityList, @@ -150,8 +150,8 @@ pub struct ScheduledChange { } /// An consensus log item for GRANDPA. -#[cfg_attr(feature = "std", derive(Serialize))] #[derive(Decode, Encode, PartialEq, Eq, Clone, RuntimeDebug)] +#[cfg_attr(feature = "serde", derive(Serialize))] pub enum ConsensusLog { /// Schedule an authority set change. /// diff --git a/primitives/consensus/slots/Cargo.toml b/primitives/consensus/slots/Cargo.toml index a4d85dff6ea85..29aa3c10c2f55 100644 --- a/primitives/consensus/slots/Cargo.toml +++ b/primitives/consensus/slots/Cargo.toml @@ -15,7 +15,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive", "max-encoded-len"] } scale-info = { version = "2.0.0", default-features = false, features = ["derive"] } -serde = { version = "1.0", features = ["derive"], optional = true } +serde = { version = "1.0", default-features = false, features = ["derive", "alloc"], optional = true } sp-std = { version = "5.0.0", default-features = false, path = "../../std" } sp-timestamp = { version = "4.0.0-dev", default-features = false, path = "../../timestamp" } @@ -24,7 +24,13 @@ default = ["std"] std = [ "codec/std", "scale-info/std", - "serde", + "serde/std", "sp-std/std", "sp-timestamp/std", ] + +# Serde support without relying on std features. +serde = [ + "dep:serde", + "scale-info/serde", +] diff --git a/primitives/consensus/slots/src/lib.rs b/primitives/consensus/slots/src/lib.rs index 1f2fa585df7f2..30bb42e2c7589 100644 --- a/primitives/consensus/slots/src/lib.rs +++ b/primitives/consensus/slots/src/lib.rs @@ -25,7 +25,7 @@ use sp_timestamp::Timestamp; /// Unit type wrapper that represents a slot. #[derive(Debug, Encode, MaxEncodedLen, Decode, Eq, Clone, Copy, Default, Ord, TypeInfo)] -#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Slot(u64); impl core::ops::Deref for Slot { diff --git a/primitives/core/Cargo.toml b/primitives/core/Cargo.toml index 8eac0996760e3..b748da63b0d9f 100644 --- a/primitives/core/Cargo.toml +++ b/primitives/core/Cargo.toml @@ -16,10 +16,10 @@ targets = ["x86_64-unknown-linux-gnu"] codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive","max-encoded-len"] } scale-info = { version = "2.5.0", default-features = false, features = ["derive"] } log = { version = "0.4.17", default-features = false } -serde = { version = "1.0.136", optional = true, features = ["derive"] } +serde = { version = "1.0.136", optional = true, default-features = false, features = ["derive", "alloc"] } bounded-collections = { version = "0.1.4", default-features = false } primitive-types = { version = "0.12.0", default-features = false, features = ["codec", "scale-info"] } -impl-serde = { version = "0.4.0", optional = true } +impl-serde = { version = "0.4.0", default-features = false, optional = true } hash-db = { version = "0.16.0", default-features = false } hash256-std-hasher = { version = "0.15.2", default-features = false } bs58 = { version = "0.4.0", default-features = false, optional = true } @@ -31,6 +31,7 @@ zeroize = { version = "1.4.3", default-features = false } secrecy = { version = "0.8.0", default-features = false } lazy_static = { version = "1.4.0", default-features = false, optional = true } parking_lot = { version = "0.12.1", optional = true } +ss58-registry = { version = "1.34.0", default-features = false } sp-std = { version = "5.0.0", default-features = false, path = "../std" } sp-debug-derive = { version = "5.0.0", default-features = false, path = "../debug-derive" } sp-storage = { version = "7.0.0", default-features = false, path = "../storage" } @@ -49,7 +50,6 @@ libsecp256k1 = { version = "0.7", default-features = false, features = ["static- schnorrkel = { version = "0.9.1", features = ["preaudit_deprecated", "u64_backend"], default-features = false } merlin = { version = "2.0", default-features = false } secp256k1 = { version = "0.24.0", default-features = false, features = ["recovery", "alloc"], optional = true } -ss58-registry = { version = "1.34.0", default-features = false } sp-core-hashing = { version = "5.0.0", path = "./hashing", default-features = false, optional = true } sp-runtime-interface = { version = "7.0.0", default-features = false, path = "../runtime-interface" } @@ -81,13 +81,13 @@ std = [ "primitive-types/serde", "primitive-types/byteorder", "primitive-types/rustc-hex", - "impl-serde", + "impl-serde/std", "codec/std", "scale-info/std", "hash256-std-hasher/std", "hash-db/std", "sp-std/std", - "serde", + "serde/std", "blake2/std", "array-bytes", "ed25519-zebra/std", @@ -113,6 +113,20 @@ std = [ "dyn-clonable", ] +# Serde support without relying on std features. +serde = [ + "dep:serde", + "array-bytes", + "blake2", + "bs58", + "scale-info/serde", + "secrecy/alloc", + "impl-serde", + "primitive-types/serde_no_std", + "sp-storage/serde", + "sp-core-hashing", +] + # This feature enables all crypto primitives for `no_std` builds like microcontrollers # or Intel SGX. # For the regular wasm runtime builds this should not be used. diff --git a/primitives/core/src/crypto.rs b/primitives/core/src/crypto.rs index dd0d9e60529f2..ec534e2c15801 100644 --- a/primitives/core/src/crypto.rs +++ b/primitives/core/src/crypto.rs @@ -33,13 +33,16 @@ pub use secrecy::{ExposeSecret, SecretString}; use sp_runtime_interface::pass_by::PassByInner; #[doc(hidden)] pub use sp_std::ops::Deref; +#[cfg(all(not(feature = "std"), feature = "serde"))] +use sp_std::{ + alloc::{format, string::String}, + vec, +}; use sp_std::{hash::Hash, str, vec::Vec}; +pub use ss58_registry::{from_known_address_format, Ss58AddressFormat, Ss58AddressFormatRegistry}; /// Trait to zeroize a memory buffer. pub use zeroize::Zeroize; -#[cfg(feature = "full_crypto")] -pub use ss58_registry::{from_known_address_format, Ss58AddressFormat, Ss58AddressFormatRegistry}; - /// The root phrase for our publicly known keys. pub const DEV_PHRASE: &str = "bottom drive obey lake curtain smoke basket hold race lonely fit walk"; @@ -49,7 +52,6 @@ pub const DEV_ADDRESS: &str = "5DfhGyQdFobKM8NsWvEeAKk5EQQgYe9AydgJ7rMB6E1EqRzV" /// The length of the junction identifier. Note that this is also referred to as the /// `CHAIN_CODE_LENGTH` in the context of Schnorrkel. -#[cfg(feature = "full_crypto")] pub const JUNCTION_ID_LEN: usize = 32; /// Similar to `From`, except that the onus is on the part of the caller to ensure @@ -113,7 +115,7 @@ pub enum DeriveError { /// a new secret key from an existing secret key and, in the case of `SoftRaw` and `SoftIndex` /// a new public key from an existing public key. #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Encode, Decode)] -#[cfg(feature = "full_crypto")] +#[cfg(any(feature = "full_crypto", feature = "serde"))] pub enum DeriveJunction { /// Soft (vanilla) derivation. Public keys have a correspondent derivation. Soft([u8; JUNCTION_ID_LEN]), @@ -121,7 +123,7 @@ pub enum DeriveJunction { Hard([u8; JUNCTION_ID_LEN]), } -#[cfg(feature = "full_crypto")] +#[cfg(any(feature = "full_crypto", feature = "serde"))] impl DeriveJunction { /// Consume self to return a soft derive junction with the same chain code. pub fn soften(self) -> Self { @@ -180,7 +182,7 @@ impl DeriveJunction { } } -#[cfg(feature = "full_crypto")] +#[cfg(any(feature = "full_crypto", feature = "serde"))] impl> From for DeriveJunction { fn from(j: T) -> DeriveJunction { let j = j.as_ref(); @@ -208,7 +210,7 @@ impl> From for DeriveJunction { #[cfg_attr(not(feature = "std"), derive(Debug))] #[derive(Clone, Copy, Eq, PartialEq)] #[allow(missing_docs)] -#[cfg(feature = "full_crypto")] +#[cfg(any(feature = "full_crypto", feature = "serde"))] pub enum PublicError { #[cfg_attr(feature = "std", error("Base 58 requirement is violated"))] BadBase58, @@ -247,7 +249,6 @@ impl sp_std::fmt::Debug for PublicError { /// /// See /// for information on the codec. -#[cfg(feature = "full_crypto")] pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + ByteArray { /// A format filterer, can be used to ensure that `from_ss58check` family only decode for /// allowed identifiers. By default just refuses the two reserved identifiers. @@ -256,7 +257,7 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + ByteArray { } /// Some if the string is a properly encoded SS58Check address. - #[cfg(feature = "std")] + #[cfg(feature = "serde")] fn from_ss58check(s: &str) -> Result { Self::from_ss58check_with_version(s).and_then(|(r, v)| match v { v if !v.is_custom() => Ok(r), @@ -266,7 +267,7 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + ByteArray { } /// Some if the string is a properly encoded SS58Check address. - #[cfg(feature = "std")] + #[cfg(feature = "serde")] fn from_ss58check_with_version(s: &str) -> Result<(Self, Ss58AddressFormat), PublicError> { const CHECKSUM_LEN: usize = 2; let body_len = Self::LEN; @@ -321,7 +322,7 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + ByteArray { } /// Return the ss58-check string for this key. - #[cfg(feature = "std")] + #[cfg(feature = "serde")] fn to_ss58check_with_version(&self, version: Ss58AddressFormat) -> String { // We mask out the upper two bits of the ident - SS58 Prefix currently only supports 14-bits let ident: u16 = u16::from(version) & 0b0011_1111_1111_1111; @@ -344,7 +345,7 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + ByteArray { } /// Return the ss58-check string for this key. - #[cfg(feature = "std")] + #[cfg(feature = "serde")] fn to_ss58check(&self) -> String { self.to_ss58check_with_version(default_ss58_version()) } @@ -362,16 +363,16 @@ pub trait Derive: Sized { /// Derive a child key from a series of given junctions. /// /// Will be `None` for public keys if there are any hard junctions in there. - #[cfg(feature = "std")] + #[cfg(feature = "serde")] fn derive>(&self, _path: Iter) -> Option { None } } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] const PREFIX: &[u8] = b"SS58PRE"; -#[cfg(feature = "std")] +#[cfg(feature = "serde")] fn ss58hash(data: &[u8]) -> Vec { use blake2::{Blake2b512, Digest}; @@ -382,19 +383,19 @@ fn ss58hash(data: &[u8]) -> Vec { } /// Default prefix number -#[cfg(feature = "std")] -static DEFAULT_VERSION: core::sync::atomic::AtomicU16 = std::sync::atomic::AtomicU16::new( +#[cfg(feature = "serde")] +static DEFAULT_VERSION: core::sync::atomic::AtomicU16 = core::sync::atomic::AtomicU16::new( from_known_address_format(Ss58AddressFormatRegistry::SubstrateAccount), ); /// Returns default SS58 format used by the current active process. -#[cfg(feature = "std")] +#[cfg(feature = "serde")] pub fn default_ss58_version() -> Ss58AddressFormat { - DEFAULT_VERSION.load(std::sync::atomic::Ordering::Relaxed).into() + DEFAULT_VERSION.load(core::sync::atomic::Ordering::Relaxed).into() } /// Returns either the input address format or the default. -#[cfg(feature = "std")] +#[cfg(feature = "serde")] pub fn unwrap_or_default_ss58_version(network: Option) -> Ss58AddressFormat { network.unwrap_or_else(default_ss58_version) } @@ -408,9 +409,9 @@ pub fn unwrap_or_default_ss58_version(network: Option) -> Ss5 /// This will enable the node to decode ss58 addresses with this prefix. /// /// This SS58 version/format is also only used by the node and not by the runtime. -#[cfg(feature = "std")] +#[cfg(feature = "serde")] pub fn set_default_ss58_version(new_default: Ss58AddressFormat) { - DEFAULT_VERSION.store(new_default.into(), std::sync::atomic::Ordering::Relaxed); + DEFAULT_VERSION.store(new_default.into(), core::sync::atomic::Ordering::Relaxed); } #[cfg(feature = "std")] @@ -458,6 +459,11 @@ impl + AsRef<[u8]> + Public + Derive> Ss58Codec for T { } } +// Use the default implementations of the trait in serde feature. +// The std implementation is not available because of std only crate Regex. +#[cfg(all(not(feature = "std"), feature = "serde"))] +impl + AsRef<[u8]> + Public + Derive> Ss58Codec for T {} + /// Trait used for types that are really just a fixed-length array. pub trait ByteArray: AsRef<[u8]> + AsMut<[u8]> + for<'a> TryFrom<&'a [u8], Error = ()> { /// The "length" of the values of this type, which is always the same. @@ -507,7 +513,7 @@ impl ByteArray for AccountId32 { const LEN: usize = 32; } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] impl Ss58Codec for AccountId32 {} impl AsRef<[u8]> for AccountId32 { @@ -591,7 +597,7 @@ impl sp_std::fmt::Debug for AccountId32 { } } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] impl serde::Serialize for AccountId32 { fn serialize(&self, serializer: S) -> Result where @@ -601,7 +607,7 @@ impl serde::Serialize for AccountId32 { } } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] impl<'de> serde::Deserialize<'de> for AccountId32 { fn deserialize(deserializer: D) -> Result where @@ -1063,7 +1069,7 @@ pub trait CryptoType { crate::RuntimeDebug, TypeInfo, )] -#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct KeyTypeId(pub [u8; 4]); impl From for KeyTypeId { @@ -1115,7 +1121,7 @@ pub trait VrfVerifier: VrfCrypto { /// An identifier for a specific cryptographic algorithm used by a key pair #[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Encode, Decode)] -#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct CryptoTypeId(pub [u8; 4]); /// Known key types; this also functions as a global registry of key types for projects wishing to diff --git a/primitives/core/src/ecdsa.rs b/primitives/core/src/ecdsa.rs index 2474fa7736b78..0c997bc290008 100644 --- a/primitives/core/src/ecdsa.rs +++ b/primitives/core/src/ecdsa.rs @@ -21,7 +21,7 @@ use codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; use sp_runtime_interface::pass_by::PassByInner; -#[cfg(feature = "std")] +#[cfg(feature = "serde")] use crate::crypto::Ss58Codec; use crate::crypto::{ ByteArray, CryptoType, CryptoTypeId, Derive, Public as TraitPublic, UncheckedFrom, @@ -40,8 +40,10 @@ use secp256k1::{ ecdsa::{RecoverableSignature, RecoveryId}, Message, PublicKey, SecretKey, }; -#[cfg(feature = "std")] +#[cfg(feature = "serde")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; +#[cfg(all(not(feature = "std"), feature = "serde"))] +use sp_std::alloc::{format, string::String}; #[cfg(feature = "full_crypto")] use sp_std::vec::Vec; @@ -164,7 +166,7 @@ impl sp_std::fmt::Debug for Public { } } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] impl Serialize for Public { fn serialize(&self, serializer: S) -> Result where @@ -174,7 +176,7 @@ impl Serialize for Public { } } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] impl<'de> Deserialize<'de> for Public { fn deserialize(deserializer: D) -> Result where @@ -204,7 +206,7 @@ impl TryFrom<&[u8]> for Signature { } } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] impl Serialize for Signature { fn serialize(&self, serializer: S) -> Result where @@ -214,7 +216,7 @@ impl Serialize for Signature { } } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] impl<'de> Deserialize<'de> for Signature { fn deserialize(deserializer: D) -> Result where diff --git a/primitives/core/src/ed25519.rs b/primitives/core/src/ed25519.rs index d0e6bef97a7d9..29da78626f850 100644 --- a/primitives/core/src/ed25519.rs +++ b/primitives/core/src/ed25519.rs @@ -29,7 +29,7 @@ use crate::{ use codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; -#[cfg(feature = "std")] +#[cfg(feature = "serde")] use crate::crypto::Ss58Codec; use crate::crypto::{CryptoType, CryptoTypeId, Derive, Public as TraitPublic, UncheckedFrom}; #[cfg(feature = "full_crypto")] @@ -38,9 +38,11 @@ use crate::crypto::{DeriveError, DeriveJunction, Pair as TraitPair, SecretString use core::convert::TryFrom; #[cfg(feature = "full_crypto")] use ed25519_zebra::{SigningKey, VerificationKey}; -#[cfg(feature = "std")] +#[cfg(feature = "serde")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; use sp_runtime_interface::pass_by::PassByInner; +#[cfg(all(not(feature = "std"), feature = "serde"))] +use sp_std::alloc::{format, string::String}; use sp_std::ops::Deref; /// An identifier used to match public keys against ed25519 keys @@ -176,7 +178,7 @@ impl sp_std::fmt::Debug for Public { } } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] impl Serialize for Public { fn serialize(&self, serializer: S) -> Result where @@ -186,7 +188,7 @@ impl Serialize for Public { } } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] impl<'de> Deserialize<'de> for Public { fn deserialize(deserializer: D) -> Result where @@ -216,7 +218,7 @@ impl TryFrom<&[u8]> for Signature { } } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] impl Serialize for Signature { fn serialize(&self, serializer: S) -> Result where @@ -226,7 +228,7 @@ impl Serialize for Signature { } } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] impl<'de> Deserialize<'de> for Signature { fn deserialize(deserializer: D) -> Result where diff --git a/primitives/core/src/lib.rs b/primitives/core/src/lib.rs index 04f44631cb21c..c6c933835c936 100644 --- a/primitives/core/src/lib.rs +++ b/primitives/core/src/lib.rs @@ -34,16 +34,16 @@ macro_rules! map { #[doc(hidden)] pub use codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; -#[cfg(feature = "std")] +#[cfg(feature = "serde")] pub use serde; -#[cfg(feature = "std")] +#[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; use sp_runtime_interface::pass_by::{PassByEnum, PassByInner}; use sp_std::{ops::Deref, prelude::*}; pub use sp_debug_derive::RuntimeDebug; -#[cfg(feature = "std")] +#[cfg(feature = "serde")] pub use impl_serde::serialize as bytes; #[cfg(feature = "full_crypto")] @@ -134,8 +134,8 @@ impl ExecutionContext { /// Hex-serialized shim for `Vec`. #[derive(PartialEq, Eq, Clone, RuntimeDebug)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Hash, PartialOrd, Ord))] -pub struct Bytes(#[cfg_attr(feature = "std", serde(with = "bytes"))] pub Vec); +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize, Hash, PartialOrd, Ord))] +pub struct Bytes(#[cfg_attr(feature = "serde", serde(with = "bytes"))] pub Vec); impl From> for Bytes { fn from(s: Vec) -> Self { @@ -204,7 +204,7 @@ impl sp_std::ops::Deref for OpaqueMetadata { PassByInner, TypeInfo, )] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct OpaquePeerId(pub Vec); impl OpaquePeerId { @@ -389,6 +389,45 @@ macro_rules! impl_maybe_marker { } } +/// Macro for creating `Maybe*` marker traits. +/// +/// Such a maybe-marker trait requires the given bound when `feature = std` or `feature = +/// serde` but does not require the bound in `no_std` without `serde` activated. +/// +/// # Example +/// +/// ``` +/// sp_core::impl_maybe_marker_serde_full! { +/// /// A marker for a type that implements `Debug` when `feature = serde` or `feature = std`. +/// trait MaybeDebug: std::fmt::Debug; +/// /// A marker for a type that implements `Debug + Display` when `feature = serde` or `feature = std`. +/// trait MaybeDebugDisplay: std::fmt::Debug, std::fmt::Display; +/// } +/// ``` +#[macro_export] +macro_rules! impl_maybe_marker_serde_full { + ( + $( + $(#[$doc:meta] )+ + trait $trait_name:ident: $( $trait_bound:path ),+; + )+ + ) => { + $( + $(#[$doc])+ + #[cfg(any(feature = "serde", feature = "std"))] + pub trait $trait_name: $( $trait_bound + )+ {} + #[cfg(any(feature = "serde", feature = "std"))] + impl $trait_name for T {} + + $(#[$doc])+ + #[cfg(not(any(feature = "serde", feature = "std")))] + pub trait $trait_name {} + #[cfg(not(any(feature = "serde", feature = "std")))] + impl $trait_name for T {} + )+ + } +} + /// The maximum number of bytes that can be allocated at one time. // The maximum possible allocation size was chosen rather arbitrary, 32 MiB should be enough for // everybody. diff --git a/primitives/core/src/offchain/mod.rs b/primitives/core/src/offchain/mod.rs index 5a77e19a3e522..2d23db7c9f439 100644 --- a/primitives/core/src/offchain/mod.rs +++ b/primitives/core/src/offchain/mod.rs @@ -58,7 +58,7 @@ pub trait OffchainStorage: Clone + Send + Sync { /// A type of supported crypto. #[derive(Clone, Copy, PartialEq, Eq, Encode, Decode, RuntimeDebug, PassByEnum)] -#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[repr(C)] pub enum StorageKind { /// Persistent storage is non-revertible and not fork-aware. It means that any value @@ -208,14 +208,14 @@ impl OpaqueMultiaddr { #[derive( Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Default, RuntimeDebug, PassByInner, Encode, Decode, )] -#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Timestamp(u64); /// Duration type #[derive( Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Default, RuntimeDebug, PassByInner, Encode, Decode, )] -#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Duration(u64); impl Duration { diff --git a/primitives/core/src/sr25519.rs b/primitives/core/src/sr25519.rs index 9b1c82205de10..4a714bafb8586 100644 --- a/primitives/core/src/sr25519.rs +++ b/primitives/core/src/sr25519.rs @@ -19,15 +19,20 @@ //! //! Note: `CHAIN_CODE_LENGTH` must be equal to `crate::crypto::JUNCTION_ID_LEN` //! for this to work. - -#[cfg(feature = "std")] +#[cfg(any(feature = "full_crypto", feature = "serde"))] +use crate::crypto::DeriveJunction; +#[cfg(feature = "serde")] use crate::crypto::Ss58Codec; #[cfg(feature = "full_crypto")] -use crate::crypto::{DeriveError, DeriveJunction, Pair as TraitPair, SecretStringError}; +use crate::crypto::{DeriveError, Pair as TraitPair, SecretStringError}; #[cfg(feature = "full_crypto")] use schnorrkel::{ - derive::{ChainCode, Derivation, CHAIN_CODE_LENGTH}, - signing_context, ExpansionMode, Keypair, MiniSecretKey, PublicKey, SecretKey, + derive::CHAIN_CODE_LENGTH, signing_context, ExpansionMode, Keypair, MiniSecretKey, SecretKey, +}; +#[cfg(any(feature = "full_crypto", feature = "serde"))] +use schnorrkel::{ + derive::{ChainCode, Derivation}, + PublicKey, }; use sp_std::vec::Vec; @@ -41,9 +46,11 @@ use sp_std::ops::Deref; #[cfg(feature = "full_crypto")] use schnorrkel::keys::{MINI_SECRET_KEY_LENGTH, SECRET_KEY_LENGTH}; -#[cfg(feature = "std")] +#[cfg(feature = "serde")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; use sp_runtime_interface::pass_by::PassByInner; +#[cfg(all(not(feature = "std"), feature = "serde"))] +use sp_std::alloc::{format, string::String}; // signing context #[cfg(feature = "full_crypto")] @@ -176,7 +183,7 @@ impl sp_std::fmt::Debug for Public { } } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] impl Serialize for Public { fn serialize(&self, serializer: S) -> Result where @@ -186,7 +193,7 @@ impl Serialize for Public { } } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] impl<'de> Deserialize<'de> for Public { fn deserialize(deserializer: D) -> Result where @@ -216,7 +223,7 @@ impl TryFrom<&[u8]> for Signature { } } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] impl Serialize for Signature { fn serialize(&self, serializer: S) -> Result where @@ -226,7 +233,7 @@ impl Serialize for Signature { } } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] impl<'de> Deserialize<'de> for Signature { fn deserialize(deserializer: D) -> Result where @@ -339,7 +346,7 @@ impl Derive for Public { /// Derive a child key from a series of given junctions. /// /// `None` if there are any hard junctions in there. - #[cfg(feature = "std")] + #[cfg(feature = "serde")] fn derive>(&self, path: Iter) -> Option { let mut acc = PublicKey::from_bytes(self.as_ref()).ok()?; for j in path { diff --git a/primitives/merkle-mountain-range/Cargo.toml b/primitives/merkle-mountain-range/Cargo.toml index cb3d6ddd51ad2..4d2c050899493 100644 --- a/primitives/merkle-mountain-range/Cargo.toml +++ b/primitives/merkle-mountain-range/Cargo.toml @@ -16,7 +16,7 @@ codec = { package = "parity-scale-codec", version = "3.2.2", default-features = scale-info = { version = "2.5.0", default-features = false, features = ["derive"] } log = { version = "0.4.17", default-features = false } mmr-lib = { package = "ckb-merkle-mountain-range", version = "0.5.2", default-features = false } -serde = { version = "1.0.136", features = ["derive"], optional = true } +serde = { version = "1.0.136", features = ["derive", "alloc"], default-features = false, optional = true } sp-api = { version = "4.0.0-dev", default-features = false, path = "../api" } sp-core = { version = "7.0.0", default-features = false, path = "../core" } sp-debug-derive = { version = "5.0.0", default-features = false, path = "../debug-derive" } @@ -33,10 +33,18 @@ std = [ "codec/std", "log/std", "mmr-lib/std", - "serde", + "serde/std", "sp-api/std", "sp-core/std", "sp-debug-derive/std", "sp-runtime/std", "sp-std/std", ] + +# Serde support without relying on std features. +serde = [ + "dep:serde", + "scale-info/serde", + "sp-core/serde", + "sp-runtime/serde", +] diff --git a/primitives/merkle-mountain-range/src/lib.rs b/primitives/merkle-mountain-range/src/lib.rs index 436755c032908..6c0e75005ead8 100644 --- a/primitives/merkle-mountain-range/src/lib.rs +++ b/primitives/merkle-mountain-range/src/lib.rs @@ -101,13 +101,13 @@ impl FullLeaf /// This type does not implement SCALE encoding/decoding on purpose to avoid confusion, /// it would have to be SCALE-compatible with the concrete leaf type, but due to SCALE limitations /// it's not possible to know how many bytes the encoding of concrete leaf type uses. -#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(RuntimeDebug, Clone, PartialEq)] pub struct OpaqueLeaf( /// Raw bytes of the leaf type encoded in its compact form. /// /// NOTE it DOES NOT include length prefix (like `Vec` encoding would). - #[cfg_attr(feature = "std", serde(with = "sp_core::bytes"))] + #[cfg_attr(feature = "serde", serde(with = "sp_core::bytes"))] pub Vec, ); diff --git a/primitives/npos-elections/Cargo.toml b/primitives/npos-elections/Cargo.toml index 50a7a5bcd6088..9006a4d199189 100644 --- a/primitives/npos-elections/Cargo.toml +++ b/primitives/npos-elections/Cargo.toml @@ -15,7 +15,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] } scale-info = { version = "2.5.0", default-features = false, features = ["derive"] } -serde = { version = "1.0.136", features = ["derive"], optional = true } +serde = { version = "1.0.136", default-features = false, features = ["derive", "alloc"], optional = true } sp-arithmetic = { version = "6.0.0", default-features = false, path = "../arithmetic" } sp-core = { version = "7.0.0", default-features = false, path = "../core" } sp-runtime = { version = "7.0.0", default-features = false, path = "../runtime" } @@ -31,9 +31,18 @@ bench = [] std = [ "codec/std", "scale-info/std", - "serde", + "serde/std", "sp-arithmetic/std", "sp-core/std", "sp-runtime/std", "sp-std/std", ] + +# Serde support without relying on std features. +serde = [ + "dep:serde", + "scale-info/serde", + "sp-arithmetic/serde", + "sp-core/serde", + "sp-runtime/serde", +] diff --git a/primitives/npos-elections/src/assignments.rs b/primitives/npos-elections/src/assignments.rs index 9390cd1f4f9fc..2ac2b9bebd771 100644 --- a/primitives/npos-elections/src/assignments.rs +++ b/primitives/npos-elections/src/assignments.rs @@ -18,7 +18,7 @@ //! Structs and helpers for distributing a voter's stake among various winners. use crate::{ExtendedBalance, IdentifierT, PerThing128}; -#[cfg(feature = "std")] +#[cfg(feature = "serde")] use codec::{Decode, Encode}; use sp_arithmetic::{ traits::{Bounded, Zero}, @@ -29,7 +29,7 @@ use sp_std::vec::Vec; /// A voter's stake assignment among a set of targets, represented as ratios. #[derive(RuntimeDebug, Clone, Default)] -#[cfg_attr(feature = "std", derive(PartialEq, Eq, Encode, Decode))] +#[cfg_attr(feature = "serde", derive(PartialEq, Eq, Encode, Decode))] pub struct Assignment { /// Voter's identifier. pub who: AccountId, @@ -97,7 +97,7 @@ impl Assignment { /// A voter's stake assignment among a set of targets, represented as absolute values in the scale /// of [`ExtendedBalance`]. #[derive(RuntimeDebug, Clone, Default)] -#[cfg_attr(feature = "std", derive(PartialEq, Eq, Encode, Decode))] +#[cfg_attr(feature = "serde", derive(PartialEq, Eq, Encode, Decode))] pub struct StakedAssignment { /// Voter's identifier pub who: AccountId, diff --git a/primitives/npos-elections/src/lib.rs b/primitives/npos-elections/src/lib.rs index 716c4b283c68e..253a231602f77 100644 --- a/primitives/npos-elections/src/lib.rs +++ b/primitives/npos-elections/src/lib.rs @@ -77,7 +77,7 @@ use codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; -#[cfg(feature = "std")] +#[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; use sp_arithmetic::{traits::Zero, Normalizable, PerThing, Rational128, ThresholdOrd}; use sp_core::{bounded::BoundedVec, RuntimeDebug}; @@ -144,7 +144,7 @@ pub type ExtendedBalance = u128; /// 2. `sum_stake`. /// 3. `sum_stake_squared`. #[derive(Clone, Copy, PartialEq, Eq, Encode, Decode, MaxEncodedLen, TypeInfo, Debug, Default)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ElectionScore { /// The minimal winner, in terms of total backing stake. /// @@ -430,7 +430,7 @@ pub struct ElectionResult { /// This, at the current version, resembles the `Exposure` defined in the Staking pallet, yet they /// do not necessarily have to be the same. #[derive(RuntimeDebug, Encode, Decode, Clone, Eq, PartialEq, TypeInfo)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Support { /// Total support. pub total: ExtendedBalance, diff --git a/primitives/rpc/Cargo.toml b/primitives/rpc/Cargo.toml index ef9fdc544301d..e639fa1ff5f9f 100644 --- a/primitives/rpc/Cargo.toml +++ b/primitives/rpc/Cargo.toml @@ -13,9 +13,19 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -rustc-hash = "1.1.0" -serde = { version = "1.0.136", features = ["derive"] } -sp-core = { version = "7.0.0", path = "../core" } +rustc-hash = { version = "1.1.0", default-features = false } +serde = { version = "1.0.136", default-features = false, features = ["derive", "alloc"] } +sp-core = { version = "7.0.0", default-features = false, path = "../core", features = ["serde"] } +sp-std = { version = "5.0.0", default-features = false, path = "../std" } [dev-dependencies] serde_json = "1.0.85" + +[features] +default = ["std"] +std = [ + "rustc-hash/std", + "serde/std", + "sp-core/std", + "sp-std/std", +] diff --git a/primitives/rpc/src/lib.rs b/primitives/rpc/src/lib.rs index 4dbc629bb967a..bae85409474cc 100644 --- a/primitives/rpc/src/lib.rs +++ b/primitives/rpc/src/lib.rs @@ -17,10 +17,12 @@ //! Substrate RPC primitives and utilities. +#![cfg_attr(not(feature = "std"), no_std)] #![warn(missing_docs)] pub mod list; pub mod number; +#[cfg(feature = "std")] pub mod tracing; /// A util function to assert the result of serialization and deserialization is the same. diff --git a/primitives/rpc/src/list.rs b/primitives/rpc/src/list.rs index 860e5161b97c1..0a8331982f6b3 100644 --- a/primitives/rpc/src/list.rs +++ b/primitives/rpc/src/list.rs @@ -18,6 +18,7 @@ //! RPC a lenient list or value type. use serde::{Deserialize, Serialize}; +use sp_std::vec::Vec; /// RPC list or value wrapper. /// diff --git a/primitives/rpc/src/number.rs b/primitives/rpc/src/number.rs index 28caa243eb700..165882a3411d1 100644 --- a/primitives/rpc/src/number.rs +++ b/primitives/rpc/src/number.rs @@ -20,7 +20,7 @@ use serde::{Deserialize, Serialize}; use sp_core::U256; -use std::fmt::Debug; +use sp_std::fmt::Debug; /// A number type that can be serialized both as a number or a string that encodes a number in a /// string. diff --git a/primitives/runtime/Cargo.toml b/primitives/runtime/Cargo.toml index 4ec55162e9774..0d67a89c38c03 100644 --- a/primitives/runtime/Cargo.toml +++ b/primitives/runtime/Cargo.toml @@ -22,7 +22,7 @@ log = { version = "0.4.17", default-features = false } paste = "1.0" rand = { version = "0.8.5", optional = true } scale-info = { version = "2.5.0", default-features = false, features = ["derive"] } -serde = { version = "1.0.136", features = ["derive"], optional = true } +serde = { version = "1.0.136", default-features = false, features = ["derive", "alloc"], optional = true } sp-application-crypto = { version = "7.0.0", default-features = false, path = "../application-crypto" } sp-arithmetic = { version = "6.0.0", default-features = false, path = "../arithmetic" } sp-core = { version = "7.0.0", default-features = false, path = "../core" } @@ -50,7 +50,7 @@ std = [ "log/std", "rand", "scale-info/std", - "serde", + "serde/std", "sp-application-crypto/std", "sp-arithmetic/std", "sp-core/std", @@ -58,3 +58,13 @@ std = [ "sp-std/std", "sp-weights/std", ] + +# Serde support without relying on std features. +serde = [ + "dep:serde", + "scale-info/serde", + "sp-application-crypto/serde", + "sp-arithmetic/serde", + "sp-core/serde", + "sp-weights/serde", +] diff --git a/primitives/runtime/src/generic/block.rs b/primitives/runtime/src/generic/block.rs index 1df747a16c839..6261e412eb8ad 100644 --- a/primitives/runtime/src/generic/block.rs +++ b/primitives/runtime/src/generic/block.rs @@ -20,7 +20,7 @@ #[cfg(feature = "std")] use std::fmt; -#[cfg(feature = "std")] +#[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; use crate::{ @@ -76,9 +76,9 @@ impl fmt::Display for BlockId { /// Abstraction over a substrate block. #[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, scale_info::TypeInfo)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))] -#[cfg_attr(feature = "std", serde(deny_unknown_fields))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] +#[cfg_attr(feature = "serde", serde(deny_unknown_fields))] pub struct Block { /// The block header. pub header: Header, @@ -114,9 +114,9 @@ where /// Abstraction over a substrate block and justification. #[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))] -#[cfg_attr(feature = "std", serde(deny_unknown_fields))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] +#[cfg_attr(feature = "serde", serde(deny_unknown_fields))] pub struct SignedBlock { /// Full block. pub block: Block, diff --git a/primitives/runtime/src/generic/digest.rs b/primitives/runtime/src/generic/digest.rs index 143fc2ce58d90..d7db0f91a4821 100644 --- a/primitives/runtime/src/generic/digest.rs +++ b/primitives/runtime/src/generic/digest.rs @@ -17,8 +17,10 @@ //! Generic implementation of a digest. -#[cfg(feature = "std")] +#[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; +#[cfg(all(not(feature = "std"), feature = "serde"))] +use sp_std::alloc::format; use sp_std::prelude::*; @@ -34,7 +36,7 @@ use sp_core::RuntimeDebug; /// Generic header digest. #[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo, Default)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Digest { /// A list of logs in the digest. pub logs: Vec, @@ -106,7 +108,7 @@ pub enum DigestItem { RuntimeEnvironmentUpdated, } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] impl serde::Serialize for DigestItem { fn serialize(&self, seq: S) -> Result where @@ -116,7 +118,7 @@ impl serde::Serialize for DigestItem { } } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] impl<'a> serde::Deserialize<'a> for DigestItem { fn deserialize(de: D) -> Result where diff --git a/primitives/runtime/src/generic/era.rs b/primitives/runtime/src/generic/era.rs index 79dea7258c9e1..bdc7e9930fb1d 100644 --- a/primitives/runtime/src/generic/era.rs +++ b/primitives/runtime/src/generic/era.rs @@ -17,7 +17,7 @@ //! Generic implementation of an unchecked (pre-verification) extrinsic. -#[cfg(feature = "std")] +#[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; use crate::codec::{Decode, Encode, Error, Input, Output}; @@ -30,7 +30,7 @@ pub type Phase = u64; /// An era to describe the longevity of a transaction. #[derive(PartialEq, Eq, Clone, Copy, sp_core::RuntimeDebug)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum Era { /// The transaction is valid forever. The genesis hash must be present in the signed content. Immortal, diff --git a/primitives/runtime/src/generic/header.rs b/primitives/runtime/src/generic/header.rs index e8b99efd42c75..7c1faaefb419a 100644 --- a/primitives/runtime/src/generic/header.rs +++ b/primitives/runtime/src/generic/header.rs @@ -26,22 +26,22 @@ use crate::{ MaybeSerializeDeserialize, Member, SimpleBitOps, }, }; -#[cfg(feature = "std")] +#[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; use sp_core::U256; use sp_std::fmt::Debug; /// Abstraction over a block header for a substrate chain. #[derive(Encode, Decode, PartialEq, Eq, Clone, sp_core::RuntimeDebug, TypeInfo)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))] -#[cfg_attr(feature = "std", serde(deny_unknown_fields))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] +#[cfg_attr(feature = "serde", serde(deny_unknown_fields))] pub struct Header + TryFrom, Hash: HashT> { /// The parent hash. pub parent_hash: Hash::Output, /// The block number. #[cfg_attr( - feature = "std", + feature = "serde", serde(serialize_with = "serialize_number", deserialize_with = "deserialize_number") )] #[codec(compact)] @@ -54,7 +54,7 @@ pub struct Header + TryFrom, Hash: HashT> { pub digest: Digest, } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] pub fn serialize_number + TryFrom>( val: &T, s: S, @@ -66,7 +66,7 @@ where serde::Serialize::serialize(&u256, s) } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] pub fn deserialize_number<'a, D, T: Copy + Into + TryFrom>(d: D) -> Result where D: serde::Deserializer<'a>, diff --git a/primitives/runtime/src/generic/unchecked_extrinsic.rs b/primitives/runtime/src/generic/unchecked_extrinsic.rs index d147e7b6c1505..b9d7b9eb1b6c8 100644 --- a/primitives/runtime/src/generic/unchecked_extrinsic.rs +++ b/primitives/runtime/src/generic/unchecked_extrinsic.rs @@ -29,6 +29,8 @@ use crate::{ use codec::{Compact, Decode, Encode, EncodeLike, Error, Input}; use scale_info::{build::Fields, meta_type, Path, StaticTypeInfo, Type, TypeInfo, TypeParameter}; use sp_io::hashing::blake2_256; +#[cfg(all(not(feature = "std"), feature = "serde"))] +use sp_std::alloc::format; use sp_std::{fmt, prelude::*}; /// Current version of the [`UncheckedExtrinsic`] encoded format. @@ -317,7 +319,7 @@ where { } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] impl serde::Serialize for UncheckedExtrinsic { @@ -329,7 +331,7 @@ impl s } } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] impl<'a, Address: Decode, Signature: Decode, Call: Decode, Extra: SignedExtension> serde::Deserialize<'a> for UncheckedExtrinsic { diff --git a/primitives/runtime/src/legacy/byte_sized_error.rs b/primitives/runtime/src/legacy/byte_sized_error.rs index b552d6af3066e..a592c751231dc 100644 --- a/primitives/runtime/src/legacy/byte_sized_error.rs +++ b/primitives/runtime/src/legacy/byte_sized_error.rs @@ -20,12 +20,12 @@ use crate::{ArithmeticError, TokenError}; use codec::{Decode, Encode}; use scale_info::TypeInfo; -#[cfg(feature = "std")] +#[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; /// [`ModuleError`] type definition before BlockBuilder API version 6. #[derive(Eq, Clone, Copy, Encode, Decode, Debug, TypeInfo)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ModuleError { /// Module index, matching the metadata module index. pub index: u8, @@ -33,7 +33,7 @@ pub struct ModuleError { pub error: u8, /// Optional error message. #[codec(skip)] - #[cfg_attr(feature = "std", serde(skip_deserializing))] + #[cfg_attr(feature = "serde", serde(skip_deserializing))] pub message: Option<&'static str>, } @@ -45,12 +45,12 @@ impl PartialEq for ModuleError { /// [`DispatchError`] type definition before BlockBuilder API version 6. #[derive(Eq, Clone, Copy, Encode, Decode, Debug, TypeInfo, PartialEq)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum DispatchError { /// Some error occurred. Other( #[codec(skip)] - #[cfg_attr(feature = "std", serde(skip_deserializing))] + #[cfg_attr(feature = "serde", serde(skip_deserializing))] &'static str, ), /// Failed to lookup some data. diff --git a/primitives/runtime/src/lib.rs b/primitives/runtime/src/lib.rs index e30146918a4a9..c03a752226860 100644 --- a/primitives/runtime/src/lib.rs +++ b/primitives/runtime/src/lib.rs @@ -24,7 +24,7 @@ pub use codec; #[doc(hidden)] pub use scale_info; -#[cfg(feature = "std")] +#[cfg(feature = "serde")] #[doc(hidden)] pub use serde; #[doc(hidden)] @@ -52,6 +52,8 @@ use sp_std::prelude::*; use codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; +#[cfg(all(not(feature = "std"), feature = "serde"))] +use sp_std::alloc::format; pub mod curve; pub mod generic; @@ -122,7 +124,7 @@ pub type EncodedJustification = Vec; /// Collection of justifications for a given block, multiple justifications may /// be provided by different consensus engines for the same block. -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct Justifications(Vec); @@ -179,7 +181,7 @@ impl From for Justifications { use traits::{Lazy, Verify}; use crate::traits::IdentifyAccount; -#[cfg(feature = "std")] +#[cfg(feature = "serde")] pub use serde::{de::DeserializeOwned, Deserialize, Serialize}; /// Complex storage builder stuff. @@ -235,7 +237,7 @@ impl BuildStorage for () { pub type ConsensusEngineId = [u8; 4]; /// Signature verify that can work with any known signature types. -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Eq, PartialEq, Clone, Encode, Decode, MaxEncodedLen, RuntimeDebug, TypeInfo)] pub enum MultiSignature { /// An Ed25519 signature. @@ -299,7 +301,7 @@ impl TryFrom for ecdsa::Signature { /// Public key for any known crypto algorithm. #[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Encode, Decode, RuntimeDebug, TypeInfo)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[cfg_attr(all(feature = "serde", feature = "full_crypto"), derive(Serialize, Deserialize))] pub enum MultiSigner { /// An Ed25519 identity. Ed25519(ed25519::Public), @@ -427,7 +429,7 @@ impl Verify for MultiSignature { /// Signature verify that can work with any known signature types.. #[derive(Eq, PartialEq, Clone, Default, Encode, Decode, RuntimeDebug)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct AnySignature(H512); impl Verify for AnySignature { @@ -475,7 +477,7 @@ pub type DispatchResultWithInfo = sp_std::result::Result, } @@ -495,7 +497,7 @@ impl PartialEq for ModuleError { /// Errors related to transactional storage layers. #[derive(Eq, PartialEq, Clone, Copy, Encode, Decode, Debug, TypeInfo, MaxEncodedLen)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum TransactionalError { /// Too many transactional layers have been spawned. LimitReached, @@ -520,12 +522,12 @@ impl From for DispatchError { /// Reason why a dispatch call failed. #[derive(Eq, Clone, Copy, Encode, Decode, Debug, TypeInfo, PartialEq, MaxEncodedLen)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum DispatchError { /// Some error occurred. Other( #[codec(skip)] - #[cfg_attr(feature = "std", serde(skip_deserializing))] + #[cfg_attr(feature = "serde", serde(skip_deserializing))] &'static str, ), /// Failed to lookup some data. @@ -605,7 +607,7 @@ impl From for DispatchError { /// Description of what went wrong when trying to complete an operation on a token. #[derive(Eq, PartialEq, Clone, Copy, Encode, Decode, Debug, TypeInfo, MaxEncodedLen)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum TokenError { /// Funds are unavailable. FundsUnavailable, @@ -876,7 +878,7 @@ impl sp_std::fmt::Debug for OpaqueExtrinsic { } } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] impl ::serde::Serialize for OpaqueExtrinsic { fn serialize(&self, seq: S) -> Result where @@ -886,7 +888,7 @@ impl ::serde::Serialize for OpaqueExtrinsic { } } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] impl<'a> ::serde::Deserialize<'a> for OpaqueExtrinsic { fn deserialize(de: D) -> Result where diff --git a/primitives/runtime/src/runtime_string.rs b/primitives/runtime/src/runtime_string.rs index f8f183ec785c4..24477fa11b572 100644 --- a/primitives/runtime/src/runtime_string.rs +++ b/primitives/runtime/src/runtime_string.rs @@ -129,7 +129,7 @@ impl std::fmt::Display for RuntimeString { } } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] impl serde::Serialize for RuntimeString { fn serialize(&self, serializer: S) -> Result { match self { @@ -146,6 +146,13 @@ impl<'de> serde::Deserialize<'de> for RuntimeString { } } +#[cfg(all(not(feature = "std"), feature = "serde"))] +impl<'de> serde::Deserialize<'de> for RuntimeString { + fn deserialize>(de: D) -> Result { + Vec::::deserialize(de).map(Self::Owned) + } +} + /// Create a const [`RuntimeString`]. #[macro_export] macro_rules! create_runtime_str { diff --git a/primitives/runtime/src/traits.rs b/primitives/runtime/src/traits.rs index 95f977077e704..da04776849257 100644 --- a/primitives/runtime/src/traits.rs +++ b/primitives/runtime/src/traits.rs @@ -28,7 +28,7 @@ use crate::{ DispatchResult, }; use impl_trait_for_tuples::impl_for_tuples; -#[cfg(feature = "std")] +#[cfg(feature = "serde")] use serde::{de::DeserializeOwned, Deserialize, Serialize}; use sp_application_crypto::AppCrypto; pub use sp_arithmetic::traits::{ @@ -716,7 +716,7 @@ pub trait Hash: /// Blake2-256 Hash implementation. #[derive(PartialEq, Eq, Clone, RuntimeDebug, TypeInfo)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct BlakeTwo256; impl Hasher for BlakeTwo256 { @@ -743,7 +743,7 @@ impl Hash for BlakeTwo256 { /// Keccak-256 Hash implementation. #[derive(PartialEq, Eq, Clone, RuntimeDebug, TypeInfo)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Keccak256; impl Hasher for Keccak256 { @@ -824,11 +824,13 @@ sp_core::impl_maybe_marker!( /// A type that implements Hash when in std environment. trait MaybeHash: sp_std::hash::Hash; +); - /// A type that implements Serialize when in std environment. +sp_core::impl_maybe_marker_serde_full!( + /// A type that implements Serialize when in std environment or serde feature is activated. trait MaybeSerialize: Serialize; - /// A type that implements Serialize, DeserializeOwned and Debug when in std environment. + /// A type that implements Serialize, DeserializeOwned and Debug when in std environment or serde feature is activated. trait MaybeSerializeDeserialize: DeserializeOwned, Serialize; ); diff --git a/primitives/runtime/src/transaction_validity.rs b/primitives/runtime/src/transaction_validity.rs index 072609c667b41..836948493823c 100644 --- a/primitives/runtime/src/transaction_validity.rs +++ b/primitives/runtime/src/transaction_validity.rs @@ -36,7 +36,7 @@ pub type TransactionTag = Vec; /// An invalid transaction validity. #[derive(Clone, PartialEq, Eq, Encode, Decode, Copy, RuntimeDebug, TypeInfo)] -#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum InvalidTransaction { /// The call of the transaction is not expected. Call, @@ -119,7 +119,7 @@ impl From for &'static str { /// An unknown transaction validity. #[derive(Clone, PartialEq, Eq, Encode, Decode, Copy, RuntimeDebug, TypeInfo)] -#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum UnknownTransaction { /// Could not lookup some information that is required to validate the transaction. CannotLookup, @@ -143,7 +143,7 @@ impl From for &'static str { /// Errors that can occur while checking the validity of a transaction. #[derive(Clone, PartialEq, Eq, Encode, Decode, Copy, RuntimeDebug, TypeInfo)] -#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum TransactionValidityError { /// The transaction is invalid. Invalid(InvalidTransaction), diff --git a/primitives/serializer/Cargo.toml b/primitives/serializer/Cargo.toml index 585e4b4e0dc20..74e744c1b6ba9 100644 --- a/primitives/serializer/Cargo.toml +++ b/primitives/serializer/Cargo.toml @@ -14,5 +14,14 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -serde = "1.0.136" -serde_json = "1.0.85" +serde = { version = "1.0.136", default-features = false, features = ["derive", "alloc"] } +serde_json = { version = "1.0.85", default-features = false, features = ["alloc"] } +sp-std = { version = "5.0.0", default-features = false, path = "../std" } + +[features] +default = ["std"] +std = [ + "serde/std", + "serde_json/std", + "sp-std/std", +] diff --git a/primitives/serializer/src/lib.rs b/primitives/serializer/src/lib.rs index 3d42707143d5e..073bbe1e528d4 100644 --- a/primitives/serializer/src/lib.rs +++ b/primitives/serializer/src/lib.rs @@ -20,9 +20,16 @@ //! The idea is that we can later change the implementation //! to something more compact, but for now we're using JSON. +#![cfg_attr(not(feature = "std"), no_std)] #![warn(missing_docs)] -pub use serde_json::{from_reader, from_slice, from_str, Error, Result}; +#[cfg(feature = "std")] +pub use serde_json::from_reader; + +pub use serde_json::{from_slice, from_str, Error, Result}; + +#[cfg(not(feature = "std"))] +use sp_std::{alloc::string::String, vec::Vec}; const PROOF: &str = "Serializers are infallible; qed"; @@ -37,6 +44,7 @@ pub fn encode(value: &T) -> Vec { } /// Serialize the given data structure as JSON into the IO stream. +#[cfg(feature = "std")] pub fn to_writer( writer: W, value: &T, diff --git a/primitives/storage/Cargo.toml b/primitives/storage/Cargo.toml index 72ebc40fc1135..c8271669f6721 100644 --- a/primitives/storage/Cargo.toml +++ b/primitives/storage/Cargo.toml @@ -15,12 +15,24 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] } -impl-serde = { version = "0.4.0", optional = true } +impl-serde = { version = "0.4.0", optional = true, default-features = false } ref-cast = "1.0.0" -serde = { version = "1.0.136", features = ["derive"], optional = true } +serde = { version = "1.0.136", default-features = false, features = ["derive", "alloc"], optional = true } sp-debug-derive = { version = "5.0.0", default-features = false, path = "../debug-derive" } sp-std = { version = "5.0.0", default-features = false, path = "../std" } [features] default = [ "std" ] -std = [ "codec/std", "impl-serde", "serde", "sp-debug-derive/std", "sp-std/std" ] +std = [ + "codec/std", + "impl-serde/std", + "serde/std", + "sp-debug-derive/std", + "sp-std/std", +] + +# Serde support without relying on std features. +serde = [ + "dep:serde", + "impl-serde", +] diff --git a/primitives/storage/src/lib.rs b/primitives/storage/src/lib.rs index 06995005d9f56..3a916a71b9c44 100644 --- a/primitives/storage/src/lib.rs +++ b/primitives/storage/src/lib.rs @@ -21,7 +21,7 @@ use core::fmt::Display; -#[cfg(feature = "std")] +#[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; use sp_debug_derive::RuntimeDebug; @@ -35,11 +35,11 @@ use sp_std::{ /// Storage key. #[derive(PartialEq, Eq, RuntimeDebug)] #[cfg_attr( - feature = "std", + feature = "serde", derive(Serialize, Deserialize, Hash, PartialOrd, Ord, Clone, Encode, Decode) )] pub struct StorageKey( - #[cfg_attr(feature = "std", serde(with = "impl_serde::serialize"))] pub Vec, + #[cfg_attr(feature = "serde", serde(with = "impl_serde::serialize"))] pub Vec, ); impl AsRef<[u8]> for StorageKey { @@ -100,11 +100,11 @@ impl From> for TrackedStorageKey { /// Storage key of a child trie, it contains the prefix to the key. #[derive(PartialEq, Eq, RuntimeDebug)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Hash, PartialOrd, Ord, Clone))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize, Hash, PartialOrd, Ord, Clone))] #[repr(transparent)] #[derive(RefCast)] pub struct PrefixedStorageKey( - #[cfg_attr(feature = "std", serde(with = "impl_serde::serialize"))] Vec, + #[cfg_attr(feature = "serde", serde(with = "impl_serde::serialize"))] Vec, ); impl Deref for PrefixedStorageKey { @@ -142,11 +142,11 @@ impl PrefixedStorageKey { /// Storage data associated to a [`StorageKey`]. #[derive(PartialEq, Eq, RuntimeDebug)] #[cfg_attr( - feature = "std", + feature = "serde", derive(Serialize, Deserialize, Hash, PartialOrd, Ord, Clone, Encode, Decode, Default) )] pub struct StorageData( - #[cfg_attr(feature = "std", serde(with = "impl_serde::serialize"))] pub Vec, + #[cfg_attr(feature = "serde", serde(with = "impl_serde::serialize"))] pub Vec, ); /// Map of data to use in a storage, it is a collection of @@ -178,8 +178,8 @@ pub struct Storage { /// Storage change set #[derive(RuntimeDebug)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize, PartialEq, Eq))] -#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize, PartialEq, Eq))] +#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] pub struct StorageChangeSet { /// Block hash pub block: Hash, @@ -242,7 +242,7 @@ pub const TRIE_VALUE_NODE_THRESHOLD: u32 = 33; /// Information related to a child state. #[derive(Debug, Clone)] -#[cfg_attr(feature = "std", derive(PartialEq, Eq, Hash, PartialOrd, Ord, Encode, Decode))] +#[cfg_attr(feature = "serde", derive(PartialEq, Eq, Hash, PartialOrd, Ord, Encode, Decode))] pub enum ChildInfo { /// This is the one used by default. ParentKeyId(ChildTrieParentKeyId), @@ -389,7 +389,7 @@ impl ChildType { /// to be a unique id that will be use only once. Those unique id also required to be long enough to /// avoid any unique id to be prefixed by an other unique id. #[derive(Debug, Clone)] -#[cfg_attr(feature = "std", derive(PartialEq, Eq, Hash, PartialOrd, Ord, Encode, Decode))] +#[cfg_attr(feature = "serde", derive(PartialEq, Eq, Hash, PartialOrd, Ord, Encode, Decode))] pub struct ChildTrieParentKeyId { /// Data is the storage key without prefix. data: Vec, diff --git a/primitives/test-primitives/Cargo.toml b/primitives/test-primitives/Cargo.toml index f6d57ce70fc05..77df69b9d1455 100644 --- a/primitives/test-primitives/Cargo.toml +++ b/primitives/test-primitives/Cargo.toml @@ -14,10 +14,11 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] } scale-info = { version = "2.1.1", default-features = false, features = ["derive"] } -serde = { version = "1.0.136", features = ["derive"], optional = true } +serde = { version = "1.0.136", default-features = false, features = ["derive"], optional = true } sp-application-crypto = { version = "7.0.0", default-features = false, path = "../application-crypto" } sp-core = { version = "7.0.0", default-features = false, path = "../core" } sp-runtime = { version = "7.0.0", default-features = false, path = "../runtime" } +sp-std = { version = "5.0.0", default-features = false, path = "../std" } [features] default = [ @@ -25,9 +26,18 @@ default = [ ] std = [ "codec/std", - "serde", + "serde/std", "sp-application-crypto/std", "sp-core/std", "sp-runtime/std", + "sp-std/std", "scale-info/std", ] + +# Serde support without relying on std features. +serde = [ + "dep:serde", + "sp-application-crypto/serde", + "sp-core/serde", + "sp-runtime/serde", +] diff --git a/primitives/test-primitives/src/lib.rs b/primitives/test-primitives/src/lib.rs index 035acc7a35ef6..913cb762d92a5 100644 --- a/primitives/test-primitives/src/lib.rs +++ b/primitives/test-primitives/src/lib.rs @@ -26,6 +26,7 @@ use sp_application_crypto::sr25519; pub use sp_core::{hash::H256, RuntimeDebug}; use sp_runtime::traits::{BlakeTwo256, Extrinsic as ExtrinsicT, Verify}; +use sp_std::vec::Vec; /// Extrinsic for test-runtime. #[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, scale_info::TypeInfo)] @@ -34,7 +35,7 @@ pub enum Extrinsic { StorageChange(Vec, Option>), } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] impl serde::Serialize for Extrinsic { fn serialize(&self, seq: S) -> Result where diff --git a/primitives/version/Cargo.toml b/primitives/version/Cargo.toml index 6b425aa7b9d27..9cebb8f01d82e 100644 --- a/primitives/version/Cargo.toml +++ b/primitives/version/Cargo.toml @@ -15,10 +15,10 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] } -impl-serde = { version = "0.4.0", optional = true } +impl-serde = { version = "0.4.0", default-features = false, optional = true } parity-wasm = { version = "0.45", optional = true } scale-info = { version = "2.5.0", default-features = false, features = ["derive"] } -serde = { version = "1.0.136", features = ["derive"], optional = true } +serde = { version = "1.0.136", default-features = false, features = ["derive", "alloc"], optional = true } thiserror = { version = "1.0.30", optional = true } sp-core-hashing-proc-macro = { version = "5.0.0", path = "../core/hashing/proc-macro" } sp-runtime = { version = "7.0.0", default-features = false, path = "../runtime" } @@ -29,11 +29,18 @@ sp-version-proc-macro = { version = "4.0.0-dev", default-features = false, path default = ["std"] std = [ "codec/std", - "impl-serde", + "impl-serde/std", "parity-wasm", "scale-info/std", - "serde", + "serde/std", "sp-runtime/std", "sp-std/std", "thiserror", ] + +# Serde support without relying on std features. +serde = [ + "dep:serde", + "impl-serde", + "sp-runtime/serde", +] diff --git a/primitives/version/src/lib.rs b/primitives/version/src/lib.rs index 214606acc534a..bd8408bb4a48a 100644 --- a/primitives/version/src/lib.rs +++ b/primitives/version/src/lib.rs @@ -33,7 +33,7 @@ #![cfg_attr(not(feature = "std"), no_std)] -#[cfg(feature = "std")] +#[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; #[cfg(feature = "std")] use std::collections::HashSet; @@ -156,8 +156,8 @@ macro_rules! create_apis_vec { /// `authoring_version`, absolutely not `impl_version` since they change the semantics of the /// runtime. #[derive(Clone, PartialEq, Eq, Encode, Default, sp_runtime::RuntimeDebug, TypeInfo)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] pub struct RuntimeVersion { /// Identifies the different Substrate runtimes. There'll be at least polkadot and node. /// A different on-chain spec_name to that of the native runtime would normally result @@ -190,7 +190,7 @@ pub struct RuntimeVersion { /// List of supported API "features" along with their versions. #[cfg_attr( - feature = "std", + feature = "serde", serde( serialize_with = "apis_serialize::serialize", deserialize_with = "apis_serialize::deserialize", @@ -389,11 +389,12 @@ impl GetNativeVersion for std::sync::Arc { } } -#[cfg(feature = "std")] +#[cfg(feature = "serde")] mod apis_serialize { use super::*; use impl_serde::serialize as bytes; use serde::{de, ser::SerializeTuple, Serializer}; + use sp_std::vec::Vec; #[derive(Serialize)] struct ApiId<'a>(#[serde(serialize_with = "serialize_bytesref")] &'a super::ApiId, &'a u32); @@ -428,7 +429,7 @@ mod apis_serialize { impl<'de> de::Visitor<'de> for Visitor { type Value = ApisVec; - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + fn expecting(&self, formatter: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result { formatter.write_str("a sequence of api id and version tuples") } diff --git a/primitives/weights/Cargo.toml b/primitives/weights/Cargo.toml index af1b5647e24d7..534d790884794 100644 --- a/primitives/weights/Cargo.toml +++ b/primitives/weights/Cargo.toml @@ -15,7 +15,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] } scale-info = { version = "2.5.0", default-features = false, features = ["derive"] } -serde = { version = "1.0.136", optional = true, features = ["derive"] } +serde = { version = "1.0.136", default-features = false, optional = true, features = ["derive", "alloc"] } smallvec = "1.8.0" sp-arithmetic = { version = "6.0.0", default-features = false, path = "../arithmetic" } sp-core = { version = "7.0.0", default-features = false, path = "../core" } @@ -27,7 +27,7 @@ default = [ "std" ] std = [ "codec/std", "scale-info/std", - "serde", + "serde/std", "sp-arithmetic/std", "sp-core/std", "sp-debug-derive/std", @@ -36,3 +36,11 @@ std = [ # By default some types have documentation, `full-metadata-docs` allows to add documentation to # more types in the metadata. full-metadata-docs = ["scale-info/docs"] + +# Serde support without relying on std features. +serde = [ + "dep:serde", + "scale-info/serde", + "sp-arithmetic/serde", + "sp-core/serde", +] diff --git a/primitives/weights/src/lib.rs b/primitives/weights/src/lib.rs index 55d9104b00b77..36cf864dd5389 100644 --- a/primitives/weights/src/lib.rs +++ b/primitives/weights/src/lib.rs @@ -29,7 +29,7 @@ mod weight_v2; use codec::{CompactAs, Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; -#[cfg(feature = "std")] +#[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; use smallvec::SmallVec; use sp_arithmetic::{ @@ -69,8 +69,8 @@ pub mod constants { MaxEncodedLen, TypeInfo, )] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -#[cfg_attr(feature = "std", serde(transparent))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", serde(transparent))] #[deprecated(note = "Will be removed soon; use `Weight` instead.")] pub struct OldWeight(pub u64); diff --git a/primitives/weights/src/weight_v2.rs b/primitives/weights/src/weight_v2.rs index cb1cedf18ce87..2aede666dd3da 100644 --- a/primitives/weights/src/weight_v2.rs +++ b/primitives/weights/src/weight_v2.rs @@ -25,7 +25,7 @@ use super::*; #[derive( Encode, Decode, MaxEncodedLen, TypeInfo, Eq, PartialEq, Copy, Clone, RuntimeDebug, Default, )] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Weight { #[codec(compact)] /// The weight of computational time used based on some reference hardware. From 15ff74e626bfd6e24a430894186844fd7afd9884 Mon Sep 17 00:00:00 2001 From: haerdib Date: Thu, 4 May 2023 10:22:45 +0200 Subject: [PATCH 02/24] rename serde_full to serde --- primitives/core/src/lib.rs | 6 +++--- primitives/runtime/src/traits.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/primitives/core/src/lib.rs b/primitives/core/src/lib.rs index c6c933835c936..13f79cba9dbde 100644 --- a/primitives/core/src/lib.rs +++ b/primitives/core/src/lib.rs @@ -397,7 +397,7 @@ macro_rules! impl_maybe_marker { /// # Example /// /// ``` -/// sp_core::impl_maybe_marker_serde_full! { +/// sp_core::impl_maybe_marker_serde! { /// /// A marker for a type that implements `Debug` when `feature = serde` or `feature = std`. /// trait MaybeDebug: std::fmt::Debug; /// /// A marker for a type that implements `Debug + Display` when `feature = serde` or `feature = std`. @@ -405,7 +405,7 @@ macro_rules! impl_maybe_marker { /// } /// ``` #[macro_export] -macro_rules! impl_maybe_marker_serde_full { +macro_rules! impl_maybe_marker_serde { ( $( $(#[$doc:meta] )+ @@ -479,7 +479,7 @@ macro_rules! generate_feature_enabled_macro { } // Work around for: - #[doc(hidden)] + #[doc(hidden)] pub use [<_ $macro_name>] as $macro_name; } }; diff --git a/primitives/runtime/src/traits.rs b/primitives/runtime/src/traits.rs index da04776849257..4efac19e01900 100644 --- a/primitives/runtime/src/traits.rs +++ b/primitives/runtime/src/traits.rs @@ -826,7 +826,7 @@ sp_core::impl_maybe_marker!( trait MaybeHash: sp_std::hash::Hash; ); -sp_core::impl_maybe_marker_serde_full!( +sp_core::impl_maybe_marker_serde!( /// A type that implements Serialize when in std environment or serde feature is activated. trait MaybeSerialize: Serialize; From 701a4325aa6bb1afbbe11d4b41946616c2423df1 Mon Sep 17 00:00:00 2001 From: haerdib Date: Thu, 4 May 2023 10:28:03 +0200 Subject: [PATCH 03/24] move #[doc(hidden)] back --- primitives/application-crypto/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/application-crypto/src/lib.rs b/primitives/application-crypto/src/lib.rs index 95645a172f6e4..b2392b5847899 100644 --- a/primitives/application-crypto/src/lib.rs +++ b/primitives/application-crypto/src/lib.rs @@ -21,9 +21,9 @@ #![cfg_attr(not(feature = "std"), no_std)] pub use sp_core::crypto::{key_types, CryptoTypeId, KeyTypeId}; +#[doc(hidden)] #[cfg(feature = "full_crypto")] pub use sp_core::crypto::{DeriveError, Pair, SecretStringError}; -#[doc(hidden)] #[cfg(any(feature = "full_crypto", feature = "serde"))] pub use sp_core::crypto::{DeriveJunction, Ss58Codec}; #[doc(hidden)] From ed20ff3768092b39743ad4d4ea2e5b217dd3ba47 Mon Sep 17 00:00:00 2001 From: haerdib Date: Thu, 4 May 2023 10:43:32 +0200 Subject: [PATCH 04/24] remove feature = full crypto require frm MultiSigner --- primitives/runtime/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/runtime/src/lib.rs b/primitives/runtime/src/lib.rs index c03a752226860..337cdecea46ca 100644 --- a/primitives/runtime/src/lib.rs +++ b/primitives/runtime/src/lib.rs @@ -301,7 +301,7 @@ impl TryFrom for ecdsa::Signature { /// Public key for any known crypto algorithm. #[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Encode, Decode, RuntimeDebug, TypeInfo)] -#[cfg_attr(all(feature = "serde", feature = "full_crypto"), derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum MultiSigner { /// An Ed25519 identity. Ed25519(ed25519::Public), From 7f0527187ac0068084b2728c81787a6ba49a2391 Mon Sep 17 00:00:00 2001 From: haerdib Date: Thu, 4 May 2023 10:44:16 +0200 Subject: [PATCH 05/24] reorder serde and scale_info import --- primitives/consensus/beefy/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/consensus/beefy/Cargo.toml b/primitives/consensus/beefy/Cargo.toml index d811563c02086..cf5f660b22007 100644 --- a/primitives/consensus/beefy/Cargo.toml +++ b/primitives/consensus/beefy/Cargo.toml @@ -13,8 +13,8 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] } -serde = { version = "1.0.136", default-features = false, optional = true, features = ["derive", "alloc"] } scale-info = { version = "2.5.0", default-features = false, features = ["derive"] } +serde = { version = "1.0.136", default-features = false, optional = true, features = ["derive", "alloc"] } sp-api = { version = "4.0.0-dev", default-features = false, path = "../../api" } sp-application-crypto = { version = "7.0.0", default-features = false, path = "../../application-crypto" } sp-core = { version = "7.0.0", default-features = false, path = "../../core" } From 3b30d526915727143544116ed044d2e5c43a6aaf Mon Sep 17 00:00:00 2001 From: haerdib Date: Thu, 4 May 2023 13:11:52 +0200 Subject: [PATCH 06/24] fix bs58 missing alloc import in serde feature --- primitives/core/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/core/Cargo.toml b/primitives/core/Cargo.toml index b748da63b0d9f..b131d46530b43 100644 --- a/primitives/core/Cargo.toml +++ b/primitives/core/Cargo.toml @@ -118,7 +118,7 @@ serde = [ "dep:serde", "array-bytes", "blake2", - "bs58", + "bs58/alloc", "scale-info/serde", "secrecy/alloc", "impl-serde", From 0e00dd62ea86813adb6eeff90581575abcb24f47 Mon Sep 17 00:00:00 2001 From: haerdib Date: Fri, 5 May 2023 08:47:01 +0200 Subject: [PATCH 07/24] add `from_string` to serde feature and add unimplemented --- primitives/core/src/crypto.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/primitives/core/src/crypto.rs b/primitives/core/src/crypto.rs index ec534e2c15801..cbc82097bd8ff 100644 --- a/primitives/core/src/crypto.rs +++ b/primitives/core/src/crypto.rs @@ -312,7 +312,7 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + ByteArray { /// Some if the string is a properly encoded SS58Check address, optionally with /// a derivation path following. - #[cfg(feature = "std")] + #[cfg(feature = "serde")] fn from_string(s: &str) -> Result { Self::from_string_with_version(s).and_then(|(r, v)| match v { v if !v.is_custom() => Ok(r), @@ -352,7 +352,7 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + ByteArray { /// Some if the string is a properly encoded SS58Check address, optionally with /// a derivation path following. - #[cfg(feature = "std")] + #[cfg(feature = "serde")] fn from_string_with_version(s: &str) -> Result<(Self, Ss58AddressFormat), PublicError> { Self::from_ss58check_with_version(s) } @@ -459,10 +459,20 @@ impl + AsRef<[u8]> + Public + Derive> Ss58Codec for T { } } -// Use the default implementations of the trait in serde feature. -// The std implementation is not available because of std only crate Regex. +// No_std implementation of `from_string` functions. +// The std version is not available due to std only crate Regex. #[cfg(all(not(feature = "std"), feature = "serde"))] -impl + AsRef<[u8]> + Public + Derive> Ss58Codec for T {} +impl + AsRef<[u8]> + Public + Derive> Ss58Codec for T { + fn from_string(s: &str) -> Result { + //FXIME: Needed for #13334, but it needs a regex alternative. + unimplemented!() + } + + fn from_string_with_version(s: &str) -> Result<(Self, Ss58AddressFormat), PublicError> { + //FXIME: Needed for #13334, but it needs a regex alternative. + unimplemented!() + } +} /// Trait used for types that are really just a fixed-length array. pub trait ByteArray: AsRef<[u8]> + AsMut<[u8]> + for<'a> TryFrom<&'a [u8], Error = ()> { From 09f6049e998fa76b9d1343b5294938b181872379 Mon Sep 17 00:00:00 2001 From: haerdib Date: Tue, 9 May 2023 08:24:47 +0200 Subject: [PATCH 08/24] remove serde feature from fixed_point display --- primitives/arithmetic/src/fixed_point.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/primitives/arithmetic/src/fixed_point.rs b/primitives/arithmetic/src/fixed_point.rs index 4440b1864bde9..06424e5c9f6e6 100644 --- a/primitives/arithmetic/src/fixed_point.rs +++ b/primitives/arithmetic/src/fixed_point.rs @@ -931,14 +931,12 @@ macro_rules! implement_fixed { } } - #[cfg(feature = "serde")] impl sp_std::fmt::Display for $name { fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result { write!(f, "{}", self.0) } } - #[cfg(feature = "serde")] impl sp_std::str::FromStr for $name { type Err = &'static str; From 1ce5dc367d6377a987df0bf1ffb538bb3ac52d97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bigna=20H=C3=A4rdi?= Date: Tue, 9 May 2023 13:15:27 +0200 Subject: [PATCH 09/24] Remove serde/alloc Co-authored-by: Davide Galassi --- primitives/arithmetic/Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/primitives/arithmetic/Cargo.toml b/primitives/arithmetic/Cargo.toml index 66b58a5dab827..122691623b8fd 100644 --- a/primitives/arithmetic/Cargo.toml +++ b/primitives/arithmetic/Cargo.toml @@ -44,7 +44,6 @@ std = [ serde = [ "dep:serde", "scale-info/serde", - "serde/alloc", ] [[bench]] From 66872b0fd3a15b9470f048da82f35a18b1c64c04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bigna=20H=C3=A4rdi?= Date: Wed, 10 May 2023 15:23:13 +0200 Subject: [PATCH 10/24] Update primitives/consensus/babe/Cargo.toml MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/consensus/babe/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/consensus/babe/Cargo.toml b/primitives/consensus/babe/Cargo.toml index f2dd2a78d1386..5e57f276aac8b 100644 --- a/primitives/consensus/babe/Cargo.toml +++ b/primitives/consensus/babe/Cargo.toml @@ -16,7 +16,7 @@ targets = ["x86_64-unknown-linux-gnu"] async-trait = { version = "0.1.57", optional = true } codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false } scale-info = { version = "2.5.0", default-features = false, features = ["derive"] } -serde = { version = "1.0.136", default-features = false, features = ["derive", "alloc"], optional = true } +serde = { version = "1.0.136", default-features = false, features = ["derive", "alloc"], optional = true } sp-api = { version = "4.0.0-dev", default-features = false, path = "../../api" } sp-application-crypto = { version = "7.0.0", default-features = false, path = "../../application-crypto" } sp-consensus = { version = "0.10.0-dev", optional = true, path = "../common" } From ab077aad956f379a8aefcd8d74aa795f0cd0b581 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bigna=20H=C3=A4rdi?= Date: Wed, 10 May 2023 15:23:28 +0200 Subject: [PATCH 11/24] Update primitives/arithmetic/src/fixed_point.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/fixed_point.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/fixed_point.rs b/primitives/arithmetic/src/fixed_point.rs index 06424e5c9f6e6..08b788e9abd76 100644 --- a/primitives/arithmetic/src/fixed_point.rs +++ b/primitives/arithmetic/src/fixed_point.rs @@ -36,7 +36,7 @@ use sp_std::{ use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; #[cfg(all(not(feature = "std"), feature = "serde"))] -use sp_std::alloc::{string::String, string::ToString}; +use sp_std::alloc::string::{String, ToString}; /// Integer types that can be used to interact with `FixedPointNumber` implementations. pub trait FixedPointOperand: From 1c6b59089d360b5127fcd0103f01fef7853fd65e Mon Sep 17 00:00:00 2001 From: haerdib Date: Wed, 10 May 2023 15:27:49 +0200 Subject: [PATCH 12/24] revert `from_string`fixed impl back to std only --- primitives/core/src/crypto.rs | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/primitives/core/src/crypto.rs b/primitives/core/src/crypto.rs index cbc82097bd8ff..ec534e2c15801 100644 --- a/primitives/core/src/crypto.rs +++ b/primitives/core/src/crypto.rs @@ -312,7 +312,7 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + ByteArray { /// Some if the string is a properly encoded SS58Check address, optionally with /// a derivation path following. - #[cfg(feature = "serde")] + #[cfg(feature = "std")] fn from_string(s: &str) -> Result { Self::from_string_with_version(s).and_then(|(r, v)| match v { v if !v.is_custom() => Ok(r), @@ -352,7 +352,7 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + ByteArray { /// Some if the string is a properly encoded SS58Check address, optionally with /// a derivation path following. - #[cfg(feature = "serde")] + #[cfg(feature = "std")] fn from_string_with_version(s: &str) -> Result<(Self, Ss58AddressFormat), PublicError> { Self::from_ss58check_with_version(s) } @@ -459,20 +459,10 @@ impl + AsRef<[u8]> + Public + Derive> Ss58Codec for T { } } -// No_std implementation of `from_string` functions. -// The std version is not available due to std only crate Regex. +// Use the default implementations of the trait in serde feature. +// The std implementation is not available because of std only crate Regex. #[cfg(all(not(feature = "std"), feature = "serde"))] -impl + AsRef<[u8]> + Public + Derive> Ss58Codec for T { - fn from_string(s: &str) -> Result { - //FXIME: Needed for #13334, but it needs a regex alternative. - unimplemented!() - } - - fn from_string_with_version(s: &str) -> Result<(Self, Ss58AddressFormat), PublicError> { - //FXIME: Needed for #13334, but it needs a regex alternative. - unimplemented!() - } -} +impl + AsRef<[u8]> + Public + Derive> Ss58Codec for T {} /// Trait used for types that are really just a fixed-length array. pub trait ByteArray: AsRef<[u8]> + AsMut<[u8]> + for<'a> TryFrom<&'a [u8], Error = ()> { From 3d4c29550e19db16b62133683bade8f387a5c7b9 Mon Sep 17 00:00:00 2001 From: haerdib Date: Wed, 10 May 2023 15:30:10 +0200 Subject: [PATCH 13/24] remove duplicate runtime string impl --- primitives/runtime/src/runtime_string.rs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/primitives/runtime/src/runtime_string.rs b/primitives/runtime/src/runtime_string.rs index 24477fa11b572..aa0bd52e56fe4 100644 --- a/primitives/runtime/src/runtime_string.rs +++ b/primitives/runtime/src/runtime_string.rs @@ -139,17 +139,10 @@ impl serde::Serialize for RuntimeString { } } -#[cfg(feature = "std")] -impl<'de> serde::Deserialize<'de> for RuntimeString { - fn deserialize>(de: D) -> Result { - String::deserialize(de).map(Self::Owned) - } -} - -#[cfg(all(not(feature = "std"), feature = "serde"))] +#[cfg(feature = "serde")] impl<'de> serde::Deserialize<'de> for RuntimeString { fn deserialize>(de: D) -> Result { - Vec::::deserialize(de).map(Self::Owned) + Ok(Self::Owned(serde::Deserialize::deserialize(de)?)) } } From 52ddef081d552b9e10da40a14bc58ee6fdeebd4e Mon Sep 17 00:00:00 2001 From: haerdib Date: Wed, 10 May 2023 15:32:06 +0200 Subject: [PATCH 14/24] use sp_std::alloc --- primitives/application-crypto/src/lib.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/primitives/application-crypto/src/lib.rs b/primitives/application-crypto/src/lib.rs index b2392b5847899..69837a05cb787 100644 --- a/primitives/application-crypto/src/lib.rs +++ b/primitives/application-crypto/src/lib.rs @@ -352,9 +352,7 @@ macro_rules! app_crypto_public_common_if_serde { D: $crate::serde::Deserializer<'de>, { #[cfg(not(feature = "std"))] - extern crate alloc; - #[cfg(not(feature = "std"))] - use alloc::{format, string::String}; + use sp_std::alloc::{format, string::String}; use $crate::Ss58Codec; Public::from_ss58check(&String::deserialize(deserializer)?) From 25dce4613d514ea784e05eb05edecc0c752213d7 Mon Sep 17 00:00:00 2001 From: haerdib Date: Wed, 10 May 2023 15:35:30 +0200 Subject: [PATCH 15/24] remove no_std compatible rpc --- primitives/rpc/Cargo.toml | 16 +++------------- primitives/rpc/src/lib.rs | 2 -- primitives/rpc/src/list.rs | 1 - primitives/rpc/src/number.rs | 2 +- 4 files changed, 4 insertions(+), 17 deletions(-) diff --git a/primitives/rpc/Cargo.toml b/primitives/rpc/Cargo.toml index e639fa1ff5f9f..ef9fdc544301d 100644 --- a/primitives/rpc/Cargo.toml +++ b/primitives/rpc/Cargo.toml @@ -13,19 +13,9 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -rustc-hash = { version = "1.1.0", default-features = false } -serde = { version = "1.0.136", default-features = false, features = ["derive", "alloc"] } -sp-core = { version = "7.0.0", default-features = false, path = "../core", features = ["serde"] } -sp-std = { version = "5.0.0", default-features = false, path = "../std" } +rustc-hash = "1.1.0" +serde = { version = "1.0.136", features = ["derive"] } +sp-core = { version = "7.0.0", path = "../core" } [dev-dependencies] serde_json = "1.0.85" - -[features] -default = ["std"] -std = [ - "rustc-hash/std", - "serde/std", - "sp-core/std", - "sp-std/std", -] diff --git a/primitives/rpc/src/lib.rs b/primitives/rpc/src/lib.rs index bae85409474cc..4dbc629bb967a 100644 --- a/primitives/rpc/src/lib.rs +++ b/primitives/rpc/src/lib.rs @@ -17,12 +17,10 @@ //! Substrate RPC primitives and utilities. -#![cfg_attr(not(feature = "std"), no_std)] #![warn(missing_docs)] pub mod list; pub mod number; -#[cfg(feature = "std")] pub mod tracing; /// A util function to assert the result of serialization and deserialization is the same. diff --git a/primitives/rpc/src/list.rs b/primitives/rpc/src/list.rs index 0a8331982f6b3..860e5161b97c1 100644 --- a/primitives/rpc/src/list.rs +++ b/primitives/rpc/src/list.rs @@ -18,7 +18,6 @@ //! RPC a lenient list or value type. use serde::{Deserialize, Serialize}; -use sp_std::vec::Vec; /// RPC list or value wrapper. /// diff --git a/primitives/rpc/src/number.rs b/primitives/rpc/src/number.rs index 165882a3411d1..28caa243eb700 100644 --- a/primitives/rpc/src/number.rs +++ b/primitives/rpc/src/number.rs @@ -20,7 +20,7 @@ use serde::{Deserialize, Serialize}; use sp_core::U256; -use sp_std::fmt::Debug; +use std::fmt::Debug; /// A number type that can be serialized both as a number or a string that encodes a number in a /// string. From 69d65cbddf98a8c3d20285e82ce56bf2f28e4cd8 Mon Sep 17 00:00:00 2001 From: haerdib Date: Wed, 10 May 2023 15:36:49 +0200 Subject: [PATCH 16/24] remove no_std compatibility from serializer --- primitives/serializer/Cargo.toml | 13 ++----------- primitives/serializer/src/lib.rs | 10 +--------- 2 files changed, 3 insertions(+), 20 deletions(-) diff --git a/primitives/serializer/Cargo.toml b/primitives/serializer/Cargo.toml index 74e744c1b6ba9..585e4b4e0dc20 100644 --- a/primitives/serializer/Cargo.toml +++ b/primitives/serializer/Cargo.toml @@ -14,14 +14,5 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -serde = { version = "1.0.136", default-features = false, features = ["derive", "alloc"] } -serde_json = { version = "1.0.85", default-features = false, features = ["alloc"] } -sp-std = { version = "5.0.0", default-features = false, path = "../std" } - -[features] -default = ["std"] -std = [ - "serde/std", - "serde_json/std", - "sp-std/std", -] +serde = "1.0.136" +serde_json = "1.0.85" diff --git a/primitives/serializer/src/lib.rs b/primitives/serializer/src/lib.rs index 073bbe1e528d4..3d42707143d5e 100644 --- a/primitives/serializer/src/lib.rs +++ b/primitives/serializer/src/lib.rs @@ -20,16 +20,9 @@ //! The idea is that we can later change the implementation //! to something more compact, but for now we're using JSON. -#![cfg_attr(not(feature = "std"), no_std)] #![warn(missing_docs)] -#[cfg(feature = "std")] -pub use serde_json::from_reader; - -pub use serde_json::{from_slice, from_str, Error, Result}; - -#[cfg(not(feature = "std"))] -use sp_std::{alloc::string::String, vec::Vec}; +pub use serde_json::{from_reader, from_slice, from_str, Error, Result}; const PROOF: &str = "Serializers are infallible; qed"; @@ -44,7 +37,6 @@ pub fn encode(value: &T) -> Vec { } /// Serialize the given data structure as JSON into the IO stream. -#[cfg(feature = "std")] pub fn to_writer( writer: W, value: &T, From 040126ee026b717da11248403cd76cc6f5f107a5 Mon Sep 17 00:00:00 2001 From: haerdib Date: Wed, 10 May 2023 15:39:51 +0200 Subject: [PATCH 17/24] rename mpl_maybe_marker_serde to std_or_serde --- primitives/core/src/lib.rs | 4 ++-- primitives/runtime/src/traits.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/primitives/core/src/lib.rs b/primitives/core/src/lib.rs index 13f79cba9dbde..c098dfb5a785c 100644 --- a/primitives/core/src/lib.rs +++ b/primitives/core/src/lib.rs @@ -397,7 +397,7 @@ macro_rules! impl_maybe_marker { /// # Example /// /// ``` -/// sp_core::impl_maybe_marker_serde! { +/// sp_core::impl_maybe_marker_std_or_serde! { /// /// A marker for a type that implements `Debug` when `feature = serde` or `feature = std`. /// trait MaybeDebug: std::fmt::Debug; /// /// A marker for a type that implements `Debug + Display` when `feature = serde` or `feature = std`. @@ -405,7 +405,7 @@ macro_rules! impl_maybe_marker { /// } /// ``` #[macro_export] -macro_rules! impl_maybe_marker_serde { +macro_rules! impl_maybe_marker_std_or_serde { ( $( $(#[$doc:meta] )+ diff --git a/primitives/runtime/src/traits.rs b/primitives/runtime/src/traits.rs index 4efac19e01900..d37db4802d78f 100644 --- a/primitives/runtime/src/traits.rs +++ b/primitives/runtime/src/traits.rs @@ -826,7 +826,7 @@ sp_core::impl_maybe_marker!( trait MaybeHash: sp_std::hash::Hash; ); -sp_core::impl_maybe_marker_serde!( +sp_core::impl_maybe_marker_std_or_serde!( /// A type that implements Serialize when in std environment or serde feature is activated. trait MaybeSerialize: Serialize; From 45712131371a2a3fc063265788d74e8bd53df9ea Mon Sep 17 00:00:00 2001 From: haerdib Date: Tue, 16 May 2023 10:03:06 +0200 Subject: [PATCH 18/24] update .lock --- Cargo.lock | 1 - 1 file changed, 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 60fe0e1db2d0a..babe13cfe9661 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11117,7 +11117,6 @@ dependencies = [ "serde", "serde_json", "sp-core", - "sp-std", ] [[package]] From f2b66d4487494d161954f020015ac551839d2003 Mon Sep 17 00:00:00 2001 From: haerdib Date: Tue, 16 May 2023 10:48:38 +0200 Subject: [PATCH 19/24] add sp-std to executor --- bin/node/executor/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/node/executor/Cargo.toml b/bin/node/executor/Cargo.toml index 5f11d513c434b..8d842631a3684 100644 --- a/bin/node/executor/Cargo.toml +++ b/bin/node/executor/Cargo.toml @@ -25,6 +25,7 @@ sp-state-machine = { version = "0.13.0", path = "../../../primitives/state-machi sp-tracing = { version = "6.0.0", path = "../../../primitives/tracing" } sp-trie = { version = "7.0.0", path = "../../../primitives/trie" } sp-statement-store = { version = "4.0.0-dev", path = "../../../primitives/statement-store" } +sp-std = { version = "5.0.0", path = "../../primitives/std" } [dev-dependencies] criterion = "0.4.0" From 677bb8e6c0b78667c62a79e707491411b8430ef1 Mon Sep 17 00:00:00 2001 From: haerdib Date: Tue, 16 May 2023 10:52:00 +0200 Subject: [PATCH 20/24] fix sp-std import --- Cargo.lock | 1 + bin/node/executor/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index babe13cfe9661..259c8f9ce758d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5357,6 +5357,7 @@ dependencies = [ "sp-runtime", "sp-state-machine", "sp-statement-store", + "sp-std", "sp-tracing", "sp-trie", "wat", diff --git a/bin/node/executor/Cargo.toml b/bin/node/executor/Cargo.toml index 8d842631a3684..2dfd18d9d74fd 100644 --- a/bin/node/executor/Cargo.toml +++ b/bin/node/executor/Cargo.toml @@ -25,7 +25,7 @@ sp-state-machine = { version = "0.13.0", path = "../../../primitives/state-machi sp-tracing = { version = "6.0.0", path = "../../../primitives/tracing" } sp-trie = { version = "7.0.0", path = "../../../primitives/trie" } sp-statement-store = { version = "4.0.0-dev", path = "../../../primitives/statement-store" } -sp-std = { version = "5.0.0", path = "../../primitives/std" } +sp-std = { version = "5.0.0", path = "../../../primitives/std" } [dev-dependencies] criterion = "0.4.0" From eaefbdcbd94a02c1ec35e865dcfe7dfada03a10d Mon Sep 17 00:00:00 2001 From: haerdib Date: Tue, 16 May 2023 13:08:05 +0200 Subject: [PATCH 21/24] fix sp_std::format import --- primitives/application-crypto/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/application-crypto/src/lib.rs b/primitives/application-crypto/src/lib.rs index d91619d47768b..9aa07008baa14 100644 --- a/primitives/application-crypto/src/lib.rs +++ b/primitives/application-crypto/src/lib.rs @@ -363,7 +363,7 @@ macro_rules! app_crypto_public_common_if_serde { D: $crate::serde::Deserializer<'de>, { #[cfg(not(feature = "std"))] - use sp_std::alloc::{format, string::String}; + use sp_std::{alloc::string::String, format}; use $crate::Ss58Codec; Public::from_ss58check(&String::deserialize(deserializer)?) From 4850bbacfb5b9e72f8af66b2b3b13cf292643e9e Mon Sep 17 00:00:00 2001 From: haerdib Date: Tue, 16 May 2023 14:30:27 +0200 Subject: [PATCH 22/24] use crate import --- Cargo.lock | 1 - bin/node/executor/Cargo.toml | 1 - primitives/application-crypto/src/lib.rs | 7 +++++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 259c8f9ce758d..babe13cfe9661 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5357,7 +5357,6 @@ dependencies = [ "sp-runtime", "sp-state-machine", "sp-statement-store", - "sp-std", "sp-tracing", "sp-trie", "wat", diff --git a/bin/node/executor/Cargo.toml b/bin/node/executor/Cargo.toml index 2dfd18d9d74fd..5f11d513c434b 100644 --- a/bin/node/executor/Cargo.toml +++ b/bin/node/executor/Cargo.toml @@ -25,7 +25,6 @@ sp-state-machine = { version = "0.13.0", path = "../../../primitives/state-machi sp-tracing = { version = "6.0.0", path = "../../../primitives/tracing" } sp-trie = { version = "7.0.0", path = "../../../primitives/trie" } sp-statement-store = { version = "4.0.0-dev", path = "../../../primitives/statement-store" } -sp-std = { version = "5.0.0", path = "../../../primitives/std" } [dev-dependencies] criterion = "0.4.0" diff --git a/primitives/application-crypto/src/lib.rs b/primitives/application-crypto/src/lib.rs index 9aa07008baa14..48d145fc35463 100644 --- a/primitives/application-crypto/src/lib.rs +++ b/primitives/application-crypto/src/lib.rs @@ -32,6 +32,9 @@ pub use sp_core::{ crypto::{ByteArray, CryptoType, Derive, IsWrappedBy, Public, UncheckedFrom, Wraps}, RuntimeDebug, }; +#[doc(hidden)] +#[cfg(all(not(feature = "std"), feature = "serde"))] +pub use sp_std::alloc::{format, string::String}; #[doc(hidden)] pub use codec; @@ -362,9 +365,9 @@ macro_rules! app_crypto_public_common_if_serde { where D: $crate::serde::Deserializer<'de>, { - #[cfg(not(feature = "std"))] - use sp_std::{alloc::string::String, format}; use $crate::Ss58Codec; + #[cfg(not(feature = "std"))] + use $crate::{format, String}; Public::from_ss58check(&String::deserialize(deserializer)?) .map_err(|e| $crate::serde::de::Error::custom(format!("{:?}", e))) From 0e29f09738e9c0ef15b5a0be9e5ea8cf94292272 Mon Sep 17 00:00:00 2001 From: haerdib Date: Tue, 16 May 2023 14:48:34 +0200 Subject: [PATCH 23/24] add serde feature --- primitives/application-crypto/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/application-crypto/src/lib.rs b/primitives/application-crypto/src/lib.rs index 48d145fc35463..11be54e29c702 100644 --- a/primitives/application-crypto/src/lib.rs +++ b/primitives/application-crypto/src/lib.rs @@ -366,7 +366,7 @@ macro_rules! app_crypto_public_common_if_serde { D: $crate::serde::Deserializer<'de>, { use $crate::Ss58Codec; - #[cfg(not(feature = "std"))] + #[cfg(all(not(feature = "std"), feature = "serde"))] use $crate::{format, String}; Public::from_ss58check(&String::deserialize(deserializer)?) From 9e38e693f89aad9ca3fec07c2e80f8217cd3c6ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Tue, 16 May 2023 23:27:29 +0200 Subject: [PATCH 24/24] Update primitives/core/src/lib.rs --- primitives/core/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/primitives/core/src/lib.rs b/primitives/core/src/lib.rs index e5f5076c67def..b61009bc640ee 100644 --- a/primitives/core/src/lib.rs +++ b/primitives/core/src/lib.rs @@ -396,8 +396,8 @@ macro_rules! impl_maybe_marker { /// Macro for creating `Maybe*` marker traits. /// -/// Such a maybe-marker trait requires the given bound when `feature = std` or `feature = -/// serde` but does not require the bound in `no_std` without `serde` activated. +/// Such a maybe-marker trait requires the given bound when either `feature = std` or `feature = +/// serde` is activated. /// /// # Example ///