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

Serialize/Deserialize trait implemented in no-std for numerous types #7312

Merged
merged 9 commits into from
Jun 1, 2023
2 changes: 1 addition & 1 deletion parachain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ derive_more = "0.99.11"
bounded-collections = { version = "0.1.7", default-features = false }

# all optional crates.
serde = { version = "1.0.137", default-features = false, features = [ "derive" ], optional = true }
serde = { version = "1.0.137", default-features = false, features = ["derive", "alloc"] }

[features]
default = ["std"]
Expand Down
47 changes: 33 additions & 14 deletions parachain/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,9 @@ use bounded_collections::{BoundedVec, ConstU32};
use frame_support::weights::Weight;
use parity_scale_codec::{CompactAs, Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_core::{RuntimeDebug, TypeId};
use sp_runtime::traits::Hash as _;

#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};

#[cfg(feature = "std")]
use sp_core::bytes;
use sp_core::{bytes, RuntimeDebug, TypeId};
use sp_runtime::traits::Hash as _;

use polkadot_core_primitives::{Hash, OutboundHrmpMessage};

Expand All @@ -39,10 +34,21 @@ pub use polkadot_core_primitives::BlockNumber as RelayChainBlockNumber;

/// Parachain head data included in the chain.
#[derive(
PartialEq, Eq, Clone, PartialOrd, Ord, Encode, Decode, RuntimeDebug, derive_more::From, TypeInfo,
PartialEq,
Eq,
Clone,
PartialOrd,
Ord,
Encode,
Decode,
RuntimeDebug,
derive_more::From,
TypeInfo,
Serialize,
Deserialize,
)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Hash, Default))]
pub struct HeadData(#[cfg_attr(feature = "std", serde(with = "bytes"))] pub Vec<u8>);
#[cfg_attr(feature = "std", derive(Hash, Default))]
pub struct HeadData(#[serde(with = "bytes")] pub Vec<u8>);

impl HeadData {
/// Returns the hash of this head data.
Expand All @@ -52,9 +58,20 @@ impl HeadData {
}

/// Parachain validation code.
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, derive_more::From, TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Hash))]
pub struct ValidationCode(#[cfg_attr(feature = "std", serde(with = "bytes"))] pub Vec<u8>);
#[derive(
PartialEq,
Eq,
Clone,
Encode,
Decode,
RuntimeDebug,
derive_more::From,
TypeInfo,
Serialize,
Deserialize,
)]
#[cfg_attr(feature = "std", derive(Hash))]
pub struct ValidationCode(#[serde(with = "bytes")] pub Vec<u8>);

impl ValidationCode {
/// Get the blake2-256 hash of the validation code bytes.
Expand Down Expand Up @@ -129,9 +146,11 @@ pub struct BlockData(#[cfg_attr(feature = "std", serde(with = "bytes"))] pub Vec
PartialEq,
PartialOrd,
RuntimeDebug,
serde::Serialize,
serde::Deserialize,
TypeInfo,
)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize, derive_more::Display))]
#[cfg_attr(feature = "std", derive(derive_more::Display))]
pub struct Id(u32);

impl TypeId for Id {
Expand Down
4 changes: 2 additions & 2 deletions primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ bitvec = { version = "1.0.0", default-features = false, features = ["alloc"] }
hex-literal = "0.4.1"
parity-scale-codec = { version = "3.4.0", default-features = false, features = ["bit-vec", "derive"] }
scale-info = { version = "2.5.0", default-features = false, features = ["bit-vec", "derive"] }
serde = { version = "1.0.137", optional = true, features = ["derive"] }
serde = { version = "1.0.137", default-features = false, features = ["derive", "alloc"] }

application-crypto = { package = "sp-application-crypto", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
inherents = { package = "sp-inherents", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
Expand Down Expand Up @@ -44,7 +44,7 @@ std = [
"sp-staking/std",
"sp-arithmetic/std",
"runtime_primitives/std",
"serde",
"serde/std",
"polkadot-parachain/std",
"polkadot-core-primitives/std",
"bitvec/std",
Expand Down
7 changes: 3 additions & 4 deletions primitives/src/v4/executor_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ use crate::{BlakeTwo256, HashT as _, PvfExecTimeoutKind, PvfPrepTimeoutKind};
use parity_scale_codec::{Decode, Encode};
use polkadot_core_primitives::Hash;
use scale_info::TypeInfo;
use serde::{Deserialize, Serialize};
use sp_std::{ops::Deref, time::Duration, vec, vec::Vec};

/// The different executor parameters for changing the execution environment semantics.
#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, TypeInfo)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, TypeInfo, Serialize, Deserialize)]
pub enum ExecutorParam {
/// Maximum number of memory pages (64KiB bytes per page) the executor can allocate.
#[codec(index = 1)]
Expand Down Expand Up @@ -93,8 +93,7 @@ impl sp_std::fmt::LowerHex for ExecutorParamsHash {
// into individual fields of the structure. Thus, complex migrations shall be avoided when adding
// new entries and removing old ones. At the moment, there's no mandatory parameters defined. If
// they show up, they must be clearly documented as mandatory ones.
#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, TypeInfo)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, TypeInfo, Serialize, Deserialize)]
pub struct ExecutorParams(Vec<ExecutorParam>);

impl ExecutorParams {
Expand Down
7 changes: 2 additions & 5 deletions primitives/src/v4/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ pub use polkadot_parachain::primitives::{
ValidationCodeHash, LOWEST_PUBLIC_ID, LOWEST_USER_ID,
};

#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};

pub use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId;
Expand Down Expand Up @@ -1762,8 +1761,7 @@ impl<T: Encode> WellKnownKey<T> {
}

/// Type discriminator for PVF preparation timeouts
#[derive(Encode, Decode, TypeInfo, Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(Encode, Decode, TypeInfo, Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum PvfPrepTimeoutKind {
/// For prechecking requests, the time period after which the preparation worker is considered
/// unresponsive and will be killed.
Expand All @@ -1776,8 +1774,7 @@ pub enum PvfPrepTimeoutKind {
}

/// Type discriminator for PVF execution timeouts
#[derive(Encode, Decode, TypeInfo, Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(Encode, Decode, TypeInfo, Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum PvfExecTimeoutKind {
/// The amount of time to spend on execution during backing.
Backing,
Expand Down
13 changes: 11 additions & 2 deletions primitives/src/vstaging/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,17 @@ use primitives::RuntimeDebug;
use scale_info::TypeInfo;

/// Candidate's acceptance limitations for asynchronous backing per relay parent.
#[derive(RuntimeDebug, Copy, Clone, PartialEq, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(
RuntimeDebug,
Copy,
Clone,
PartialEq,
Encode,
Decode,
TypeInfo,
serde::Serialize,
serde::Deserialize,
)]
pub struct AsyncBackingParams {
/// The maximum number of para blocks between the para head in a relay parent
/// and a new candidate. Restricts nodes from building arbitrary long chains
Expand Down
5 changes: 2 additions & 3 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ parity-scale-codec = { version = "3.4.0", default-features = false, features = [
log = { version = "0.4.17", default-features = false }
rustc-hex = { version = "2.1.0", default-features = false }
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
serde = { version = "1.0.139", default-features = false }
serde_derive = { version = "1.0.117", optional = true }
serde = { version = "1.0.139", default-features = false, features = ["alloc"] }
serde_derive = { version = "1.0.117" }
static_assertions = "1.1.0"

sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
Expand Down Expand Up @@ -70,7 +70,6 @@ std = [
"scale-info/std",
"log/std",
"rustc-hex/std",
"serde_derive",
"serde/std",
"primitives/std",
"inherents/std",
Expand Down
10 changes: 5 additions & 5 deletions runtime/common/src/claims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ pub use pallet::*;
use parity_scale_codec::{Decode, Encode};
use primitives::ValidityError;
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{self, Deserialize, Deserializer, Serialize, Serializer};
use sp_io::{crypto::secp256k1_ecdsa_recover, hashing::keccak_256};
use sp_runtime::{
Expand All @@ -36,6 +35,8 @@ use sp_runtime::{
},
RuntimeDebug,
};
#[cfg(not(feature = "std"))]
use sp_std::alloc::{format, string::String};
use sp_std::{fmt::Debug, prelude::*};

type CurrencyOf<T> = <<T as Config>::VestingSchedule as VestingSchedule<
Expand Down Expand Up @@ -71,8 +72,9 @@ impl WeightInfo for TestWeightInfo {
}

/// The kind of statement an account needs to make for a claim to be valid.
#[derive(Encode, Decode, Clone, Copy, Eq, PartialEq, RuntimeDebug, TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(
Encode, Decode, Clone, Copy, Eq, PartialEq, RuntimeDebug, TypeInfo, Serialize, Deserialize,
)]
pub enum StatementKind {
/// Statement required to be made by non-SAFT holders.
Regular,
Expand Down Expand Up @@ -108,7 +110,6 @@ impl Default for StatementKind {
#[derive(Clone, Copy, PartialEq, Eq, Encode, Decode, Default, RuntimeDebug, TypeInfo)]
pub struct EthereumAddress([u8; 20]);

#[cfg(feature = "std")]
impl Serialize for EthereumAddress {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -119,7 +120,6 @@ impl Serialize for EthereumAddress {
}
}

#[cfg(feature = "std")]
impl<'de> Deserialize<'de> for EthereumAddress {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down
4 changes: 2 additions & 2 deletions runtime/parachains/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ parity-scale-codec = { version = "3.4.0", default-features = false, features = [
log = { version = "0.4.17", default-features = false }
rustc-hex = { version = "2.1.0", default-features = false }
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
serde = { version = "1.0.139", features = [ "derive" ], optional = true }
serde = { version = "1.0.139", default-features = false, features = ["derive", "alloc"] }
derive_more = "0.99.17"
bitflags = "1.3.2"

Expand Down Expand Up @@ -69,7 +69,7 @@ std = [
"parity-scale-codec/std",
"rustc-hex/std",
"scale-info/std",
"serde",
"serde/std",
"primitives/std",
"inherents/std",
"sp-core/std",
Expand Down
12 changes: 10 additions & 2 deletions runtime/parachains/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,16 @@ pub use pallet::*;
const LOG_TARGET: &str = "runtime::configuration";

/// All configuration of the runtime with respect to parachains and parathreads.
#[derive(Clone, Encode, Decode, PartialEq, sp_core::RuntimeDebug, scale_info::TypeInfo)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(
Clone,
Encode,
Decode,
PartialEq,
sp_core::RuntimeDebug,
scale_info::TypeInfo,
serde::Serialize,
serde::Deserialize,
)]
pub struct HostConfiguration<BlockNumber> {
// NOTE: This structure is used by parachains via merkle proofs. Therefore, this struct requires
// special treatment.
Expand Down
8 changes: 2 additions & 6 deletions runtime/parachains/src/paras/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ use sp_runtime::{
};
use sp_std::{cmp, collections::btree_set::BTreeSet, mem, prelude::*};

#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};

pub use crate::Origin as ParachainOrigin;
Expand Down Expand Up @@ -290,15 +289,14 @@ impl<N: Ord + Copy + PartialEq> ParaPastCodeMeta<N> {
}

/// Arguments for initializing a para.
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo, Serialize, Deserialize)]
pub struct ParaGenesisArgs {
/// The initial head data to use.
pub genesis_head: HeadData,
/// The initial validation code to use.
pub validation_code: ValidationCode,
/// Parachain or Parathread.
#[cfg_attr(feature = "std", serde(rename = "parachain"))]
#[serde(rename = "parachain")]
pub para_kind: ParaKind,
}

Expand All @@ -309,7 +307,6 @@ pub enum ParaKind {
Parachain,
}

#[cfg(feature = "std")]
impl Serialize for ParaKind {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -322,7 +319,6 @@ impl Serialize for ParaKind {
}
}

#[cfg(feature = "std")]
impl<'de> Deserialize<'de> for ParaKind {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down