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

Add serde feature flag to primitives #13027

Merged
merged 27 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
720f23a
add serde_full feature flag
haerdib Dec 27, 2022
15ff74e
rename serde_full to serde
haerdib May 4, 2023
701a432
move #[doc(hidden)] back
haerdib May 4, 2023
ed20ff3
remove feature = full crypto require frm MultiSigner
haerdib May 4, 2023
7f05271
reorder serde and scale_info import
haerdib May 4, 2023
3b30d52
fix bs58 missing alloc import in serde feature
haerdib May 4, 2023
0e00dd6
add `from_string` to serde feature and add unimplemented
haerdib May 5, 2023
09f6049
remove serde feature from fixed_point display
haerdib May 9, 2023
1ce5dc3
Remove serde/alloc
haerdib May 9, 2023
66872b0
Update primitives/consensus/babe/Cargo.toml
haerdib May 10, 2023
ab077aa
Update primitives/arithmetic/src/fixed_point.rs
haerdib May 10, 2023
1c6b590
revert `from_string`fixed impl back to std only
haerdib May 10, 2023
3d4c295
remove duplicate runtime string impl
haerdib May 10, 2023
52ddef0
use sp_std::alloc
haerdib May 10, 2023
25dce46
remove no_std compatible rpc
haerdib May 10, 2023
69d65cb
remove no_std compatibility from serializer
haerdib May 10, 2023
040126e
rename mpl_maybe_marker_serde to std_or_serde
haerdib May 10, 2023
cc853c9
Merge branch 'master' into add-serde-feature-to-primitives
haerdib May 11, 2023
8d47924
Merge branch 'master' into add-serde-feature-to-primitives
haerdib May 16, 2023
4571213
update .lock
haerdib May 16, 2023
f2b66d4
add sp-std to executor
haerdib May 16, 2023
677bb8e
fix sp-std import
haerdib May 16, 2023
eaefbdc
fix sp_std::format import
haerdib May 16, 2023
4850bba
use crate import
haerdib May 16, 2023
0e29f09
add serde feature
haerdib May 16, 2023
e0095f7
Merge branch 'master' into add-serde-feature-to-primitives
haerdib May 16, 2023
9e38e69
Update primitives/core/src/lib.rs
bkchr May 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions bin/node/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 9 additions & 2 deletions primitives/application-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }

Expand All @@ -29,11 +29,18 @@ std = [
"sp-core/std",
"codec/std",
"scale-info/std",
"serde",
"serde/std",
haerdib marked this conversation as resolved.
Show resolved Hide resolved
"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.
Expand Down
29 changes: 17 additions & 12 deletions primitives/application-crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
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};
#[cfg(any(feature = "full_crypto", feature = "serde"))]
pub use sp_core::crypto::{DeriveJunction, Ss58Codec};
#[doc(hidden)]
pub use sp_core::{
self,
Expand All @@ -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};
Expand Down Expand Up @@ -282,7 +284,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] {
Expand Down Expand Up @@ -323,11 +325,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 {
michalkucharczyk marked this conversation as resolved.
Show resolved Hide resolved
fn derive<Iter: Iterator<Item = $crate::DeriveJunction>>(
Expand All @@ -338,15 +340,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<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
where
S: $crate::serde::Serializer,
{
Expand All @@ -356,22 +358,25 @@ macro_rules! app_crypto_public_common_if_std {
}

impl<'de> $crate::serde::Deserialize<'de> for Public {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
where
D: $crate::serde::Deserializer<'de>,
{
#[cfg(not(feature = "std"))]
use sp_std::{alloc::string::String, format};
use $crate::Ss58Codec;

Public::from_ss58check(&String::deserialize(deserializer)?)
.map_err(|e| $crate::serde::de::Error::custom(format!("{:?}", e)))
}
}
};
}

#[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 {}
};
Expand Down
9 changes: 7 additions & 2 deletions primitives/arithmetic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }

Expand All @@ -37,9 +37,14 @@ 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",
]

[[bench]]
name = "bench"
Expand Down
11 changes: 6 additions & 5 deletions primitives/arithmetic/src/fixed_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, ToString};

/// Integer types that can be used to interact with `FixedPointNumber` implementations.
pub trait FixedPointOperand:
Copy
Expand Down Expand Up @@ -928,14 +931,12 @@ macro_rules! implement_fixed {
}
}

#[cfg(feature = "std")]
impl sp_std::fmt::Display for $name {
haerdib marked this conversation as resolved.
Show resolved Hide resolved
fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
write!(f, "{}", self.0)
}
}

#[cfg(feature = "std")]
impl sp_std::str::FromStr for $name {
type Err = &'static str;

Expand All @@ -948,7 +949,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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -960,7 +961,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<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down
4 changes: 2 additions & 2 deletions primitives/arithmetic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions primitives/arithmetic/src/per_things.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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);

Expand Down
14 changes: 12 additions & 2 deletions primitives/consensus/babe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -34,7 +34,7 @@ std = [
"async-trait",
"codec/std",
"scale-info/std",
"serde",
"serde/std",
"sp-api/std",
"sp-application-crypto/std",
"sp-consensus",
Expand All @@ -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",
]
6 changes: 3 additions & 3 deletions primitives/consensus/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -217,7 +217,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,
Expand All @@ -241,7 +241,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
Expand Down
13 changes: 11 additions & 2 deletions primitives/consensus/beefy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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", optional = true, features = ["derive"] }
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" }
Expand All @@ -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",
Expand All @@ -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",
]
2 changes: 1 addition & 1 deletion primitives/consensus/beefy/src/mmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MerkleRoot> {
/// Id of the set.
///
Expand Down
13 changes: 11 additions & 2 deletions primitives/consensus/grandpa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -33,11 +33,20 @@ std = [
"grandpa/std",
"log/std",
"scale-info/std",
"serde",
"serde/std",
"sp-api/std",
"sp-application-crypto/std",
"sp-core/std",
"sp-keystore",
"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",
]
Loading