Skip to content

Commit

Permalink
Merge remote-tracking branch 'haerdib/add-serde-feature-to-primitives…
Browse files Browse the repository at this point in the history
…' into gc-dev
  • Loading branch information
michalkucharczyk committed Apr 27, 2023
2 parents 58be496 + ee4ce97 commit 17a7981
Show file tree
Hide file tree
Showing 53 changed files with 455 additions and 210 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

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",
"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
31 changes: 19 additions & 12 deletions primitives/application-crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

pub use sp_core::crypto::{key_types, CryptoTypeId, KeyTypeId};
#[doc(hidden)]
#[cfg(any(feature = "full_crypto", feature = "serde"))]
pub use sp_core::crypto::{DeriveJunction, Ss58Codec};
#[cfg(feature = "full_crypto")]
pub use sp_core::crypto::{DeriveError, DeriveJunction, Pair, SecretStringError, Ss58Codec};
pub use sp_core::crypto::{DeriveError, Pair, SecretStringError};
#[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 @@ -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] {
Expand Down Expand Up @@ -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<Iter: Iterator<Item = $crate::DeriveJunction>>(
Expand All @@ -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<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 @@ -345,22 +347,27 @@ 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"))]
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)))
}
}
};
}

#[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
10 changes: 8 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,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"
Expand Down
13 changes: 8 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, string::ToString};

/// Integer types that can be used to interact with `FixedPointNumber` implementations.
pub trait FixedPointOperand:
Copy
Expand Down Expand Up @@ -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;

Expand All @@ -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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -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<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", 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-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 @@ -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,
Expand All @@ -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
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", optional = true, default-features = false, 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

0 comments on commit 17a7981

Please sign in to comment.