Skip to content

Commit

Permalink
frame: support for serde added (paritytech#14261)
Browse files Browse the repository at this point in the history
* frame: support for serde added

- enabled `serde` features in dependent crates, no gate feature introduced, linker should do the job and strip unused code.

- frame::staking: added impl of `serde::Serialize, serde::Deserialize` for `enum Forcing`

- primitives::runtime: impl_opaque_keys macro provides `Serialize/Deserialize` impl if `serde` is enabled

- primitives::staking: added impl of `serde::Serialize`, `serde::Deserialize` for `enum StakerStatus`

* frame::support: serde for pallets' GenesisConfig enabled in no-std

* Cargo.lock updated

* Update primitives/staking/Cargo.toml

Co-authored-by: Bastian Köcher <[email protected]>

* fix

* Cargo.lock update + missed serde/std in beefy

---------

Co-authored-by: Bastian Köcher <[email protected]>
  • Loading branch information
michalkucharczyk and bkchr authored Jun 1, 2023
1 parent 230876c commit 2577379
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 71 deletions.
12 changes: 6 additions & 6 deletions frame/babe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] }
log = { version = "0.4.17", default-features = false }
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.5.0", default-features = false, features = ["derive", "serde"] }
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, path = "../benchmarking" }
frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
pallet-authorship = { version = "4.0.0-dev", default-features = false, path = "../authorship" }
pallet-session = { version = "4.0.0-dev", default-features = false, path = "../session" }
pallet-timestamp = { version = "4.0.0-dev", default-features = false, path = "../timestamp" }
sp-application-crypto = { version = "23.0.0", default-features = false, path = "../../primitives/application-crypto" }
sp-consensus-babe = { version = "0.10.0-dev", default-features = false, path = "../../primitives/consensus/babe" }
sp-core = { version = "21.0.0", default-features = false, path = "../../primitives/core" }
sp-application-crypto = { version = "23.0.0", default-features = false, path = "../../primitives/application-crypto", features = ["serde"] }
sp-consensus-babe = { version = "0.10.0-dev", default-features = false, path = "../../primitives/consensus/babe", features = ["serde"] }
sp-core = { version = "21.0.0", default-features = false, path = "../../primitives/core", features = ["serde"] }
sp-io = { version = "23.0.0", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "24.0.0", default-features = false, path = "../../primitives/runtime" }
sp-runtime = { version = "24.0.0", default-features = false, path = "../../primitives/runtime", features = ["serde"] }
sp-session = { version = "4.0.0-dev", default-features = false, path = "../../primitives/session" }
sp-staking = { version = "4.0.0-dev", default-features = false, path = "../../primitives/staking" }
sp-staking = { version = "4.0.0-dev", default-features = false, path = "../../primitives/staking", features = ["serde"] }
sp-std = { version = "8.0.0", default-features = false, path = "../../primitives/std" }

[dev-dependencies]
Expand Down
10 changes: 5 additions & 5 deletions frame/beefy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ homepage = "https://substrate.io"

[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"] }
scale-info = { version = "2.5.0", default-features = false, features = ["derive", "serde"] }
serde = { version = "1.0.163", optional = true }
frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
pallet-authorship = { version = "4.0.0-dev", default-features = false, path = "../authorship" }
pallet-session = { version = "4.0.0-dev", default-features = false, path = "../session" }
sp-consensus-beefy = { version = "4.0.0-dev", default-features = false, path = "../../primitives/consensus/beefy" }
sp-runtime = { version = "24.0.0", default-features = false, path = "../../primitives/runtime" }
sp-consensus-beefy = { version = "4.0.0-dev", default-features = false, path = "../../primitives/consensus/beefy", features = ["serde"] }
sp-runtime = { version = "24.0.0", default-features = false, path = "../../primitives/runtime", features = ["serde"] }
sp-session = { version = "4.0.0-dev", default-features = false, path = "../../primitives/session" }
sp-staking = { version = "4.0.0-dev", default-features = false, path = "../../primitives/staking" }
sp-staking = { version = "4.0.0-dev", default-features = false, path = "../../primitives/staking", features = ["serde"] }
sp-std = { version = "8.0.0", default-features = false, path = "../../primitives/std" }

[dev-dependencies]
Expand All @@ -42,7 +42,7 @@ std = [
"pallet-authorship/std",
"pallet-session/std",
"scale-info/std",
"serde",
"serde/std",
"sp-consensus-beefy/std",
"sp-runtime/std",
"sp-session/std",
Expand Down
12 changes: 6 additions & 6 deletions frame/grandpa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] }
log = { version = "0.4.17", default-features = false }
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.5.0", default-features = false, features = ["derive", "serde"] }
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, path = "../benchmarking" }
frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
pallet-authorship = { version = "4.0.0-dev", default-features = false, path = "../authorship" }
pallet-session = { version = "4.0.0-dev", default-features = false, path = "../session" }
sp-application-crypto = { version = "23.0.0", default-features = false, path = "../../primitives/application-crypto" }
sp-core = { version = "21.0.0", default-features = false, path = "../../primitives/core" }
sp-consensus-grandpa = { version = "4.0.0-dev", default-features = false, path = "../../primitives/consensus/grandpa" }
sp-application-crypto = { version = "23.0.0", default-features = false, path = "../../primitives/application-crypto", features = ["serde"] }
sp-consensus-grandpa = { version = "4.0.0-dev", default-features = false, path = "../../primitives/consensus/grandpa", features = ["serde"] }
sp-core = { version = "21.0.0", default-features = false, path = "../../primitives/core", features = ["serde"] }
sp-io = { version = "23.0.0", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "24.0.0", default-features = false, path = "../../primitives/runtime" }
sp-runtime = { version = "24.0.0", default-features = false, path = "../../primitives/runtime", features = ["serde"] }
sp-session = { version = "4.0.0-dev", default-features = false, path = "../../primitives/session" }
sp-staking = { version = "4.0.0-dev", default-features = false, path = "../../primitives/staking" }
sp-staking = { version = "4.0.0-dev", default-features = false, path = "../../primitives/staking", features = ["serde"] }
sp-std = { version = "8.0.0", default-features = false, path = "../../primitives/std" }

[dev-dependencies]
Expand Down
10 changes: 5 additions & 5 deletions frame/im-online/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] }
log = { version = "0.4.17", default-features = false }
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.5.0", default-features = false, features = ["derive", "serde"] }
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, path = "../benchmarking" }
frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
pallet-authorship = { version = "4.0.0-dev", default-features = false, path = "../authorship" }
sp-application-crypto = { version = "23.0.0", default-features = false, path = "../../primitives/application-crypto" }
sp-core = { version = "21.0.0", default-features = false, path = "../../primitives/core" }
sp-application-crypto = { version = "23.0.0", default-features = false, path = "../../primitives/application-crypto", features = ["serde"] }
sp-core = { version = "21.0.0", default-features = false, path = "../../primitives/core", features = ["serde"] }
sp-io = { version = "23.0.0", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "24.0.0", default-features = false, path = "../../primitives/runtime" }
sp-staking = { version = "4.0.0-dev", default-features = false, path = "../../primitives/staking" }
sp-runtime = { version = "24.0.0", default-features = false, path = "../../primitives/runtime", features = ["serde"] }
sp-staking = { version = "4.0.0-dev", default-features = false, path = "../../primitives/staking", features = ["serde"] }
sp-std = { version = "8.0.0", default-features = false, path = "../../primitives/std" }

[dev-dependencies]
Expand Down
6 changes: 3 additions & 3 deletions frame/membership/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false }
log = { version = "0.4.17", default-features = false }
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.5.0", default-features = false, features = ["derive", "serde"] }
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, path = "../benchmarking" }
frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
sp-core = { version = "21.0.0", default-features = false, path = "../../primitives/core" }
sp-core = { version = "21.0.0", default-features = false, path = "../../primitives/core", features = ["serde"] }
sp-io = { version = "23.0.0", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "24.0.0", default-features = false, path = "../../primitives/runtime" }
sp-runtime = { version = "24.0.0", default-features = false, path = "../../primitives/runtime", features = ["serde"] }
sp-std = { version = "8.0.0", default-features = false, path = "../../primitives/std" }

[features]
Expand Down
8 changes: 4 additions & 4 deletions frame/session/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ targets = ["x86_64-unknown-linux-gnu"]
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] }
impl-trait-for-tuples = "0.2.2"
log = { version = "0.4.17", default-features = false }
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.5.0", default-features = false, features = ["derive", "serde"] }
frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
pallet-timestamp = { version = "4.0.0-dev", default-features = false, path = "../timestamp" }
sp-core = { version = "21.0.0", default-features = false, path = "../../primitives/core" }
sp-core = { version = "21.0.0", default-features = false, path = "../../primitives/core", features = ["serde"] }
sp-io = { version = "23.0.0", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "24.0.0", default-features = false, path = "../../primitives/runtime" }
sp-runtime = { version = "24.0.0", default-features = false, path = "../../primitives/runtime", features = ["serde"] }
sp-session = { version = "4.0.0-dev", default-features = false, path = "../../primitives/session" }
sp-staking = { version = "4.0.0-dev", default-features = false, path = "../../primitives/staking" }
sp-staking = { version = "4.0.0-dev", default-features = false, path = "../../primitives/staking", features = ["serde"] }
sp-std = { version = "8.0.0", default-features = false, path = "../../primitives/std" }
sp-trie = { version = "22.0.0", default-features = false, optional = true, path = "../../primitives/trie" }

Expand Down
14 changes: 7 additions & 7 deletions frame/staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ readme = "README.md"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
serde = { version = "1.0.163", optional = true }
serde = { version = "1.0.163", default-features = false, features = ["alloc", "derive"]}
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"] }
sp-std = { version = "8.0.0", default-features = false, path = "../../primitives/std" }
scale-info = { version = "2.5.0", default-features = false, features = ["derive", "serde"] }
sp-io = { version = "23.0.0", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "24.0.0", default-features = false, path = "../../primitives/runtime" }
sp-staking = { version = "4.0.0-dev", default-features = false, path = "../../primitives/staking" }
sp-runtime = { version = "24.0.0", default-features = false, path = "../../primitives/runtime", features = ["serde"] }
sp-staking = { version = "4.0.0-dev", default-features = false, path = "../../primitives/staking", features = ["serde"] }
sp-std = { version = "8.0.0", default-features = false, path = "../../primitives/std" }
frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
pallet-session = { version = "4.0.0-dev", default-features = false, features = [
"historical",
], path = "../session" }
pallet-authorship = { version = "4.0.0-dev", default-features = false, path = "../authorship" }
sp-application-crypto = { version = "23.0.0", default-features = false, path = "../../primitives/application-crypto" }
sp-application-crypto = { version = "23.0.0", default-features = false, path = "../../primitives/application-crypto", features = ["serde"] }
frame-election-provider-support = { version = "4.0.0-dev", default-features = false, path = "../election-provider-support" }
log = { version = "0.4.17", default-features = false }

Expand All @@ -53,7 +53,7 @@ rand_chacha = { version = "0.2" }
default = ["std"]
std = [
"frame-benchmarking?/std",
"serde",
"serde/std",
"codec/std",
"scale-info/std",
"sp-std/std",
Expand Down
15 changes: 13 additions & 2 deletions frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,8 +849,19 @@ impl<Balance: AtLeast32BitUnsigned + Clone, T: Get<&'static PiecewiseLinear<'sta
}

/// Mode of era-forcing.
#[derive(Copy, Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(
Copy,
Clone,
PartialEq,
Eq,
Encode,
Decode,
RuntimeDebug,
TypeInfo,
MaxEncodedLen,
serde::Serialize,
serde::Deserialize,
)]
pub enum Forcing {
/// Not forcing anything - just let whatever happen.
NotForcing,
Expand Down
4 changes: 2 additions & 2 deletions frame/support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ readme = "README.md"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
serde = { version = "1.0.163", optional = true, features = ["derive"] }
serde = { version = "1.0.163", default-features = false, features = ["alloc", "derive"] }
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"] }
frame-metadata = { version = "15.1.0", default-features = false, features = ["v14", "v15-unstable"] }
Expand Down Expand Up @@ -54,7 +54,7 @@ std = [
"sp-core/std",
"k256/std",
"once_cell",
"serde",
"serde/std",
"sp-api/std",
"sp-io/std",
"codec/std",
Expand Down
22 changes: 6 additions & 16 deletions frame/support/procedural/src/pallet/expand/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,13 @@ pub fn expand_genesis_config(def: &mut Def) -> proc_macro2::TokenStream {
));
}
attrs.push(syn::parse_quote!(
#[cfg_attr(feature = "std", derive(#frame_support::Serialize, #frame_support::Deserialize))]
#[derive(#frame_support::Serialize, #frame_support::Deserialize)]
));
attrs.push(
syn::parse_quote!( #[cfg_attr(feature = "std", serde(rename_all = "camelCase"))] ),
);
attrs.push(
syn::parse_quote!( #[cfg_attr(feature = "std", serde(deny_unknown_fields))] ),
);
attrs.push(
syn::parse_quote!( #[cfg_attr(feature = "std", serde(bound(serialize = "")))] ),
);
attrs.push(
syn::parse_quote!( #[cfg_attr(feature = "std", serde(bound(deserialize = "")))] ),
);
attrs.push(
syn::parse_quote!( #[cfg_attr(feature = "std", serde(crate = #serde_crate))] ),
);
attrs.push(syn::parse_quote!( #[serde(rename_all = "camelCase")] ));
attrs.push(syn::parse_quote!( #[serde(deny_unknown_fields)] ));
attrs.push(syn::parse_quote!( #[serde(bound(serialize = ""))] ));
attrs.push(syn::parse_quote!( #[serde(bound(deserialize = ""))] ));
attrs.push(syn::parse_quote!( #[serde(crate = #serde_crate)] ));
},
_ => unreachable!("Checked by genesis_config parser"),
}
Expand Down
2 changes: 0 additions & 2 deletions frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ pub use once_cell;
pub use paste;
#[doc(hidden)]
pub use scale_info;
#[cfg(feature = "std")]
pub use serde;
pub use sp_api::metadata_ir;
pub use sp_core::{OpaqueMetadata, Void};
Expand Down Expand Up @@ -821,7 +820,6 @@ macro_rules! assert_error_encoded_size {
} => {};
}

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

Expand Down
14 changes: 7 additions & 7 deletions frame/system/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ targets = ["x86_64-unknown-linux-gnu"]
cfg-if = "1.0"
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] }
log = { version = "0.4.17", default-features = false }
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
serde = { version = "1.0.163", features = ["derive"], optional = true }
scale-info = { version = "2.5.0", default-features = false, features = ["derive", "serde"] }
serde = { version = "1.0.163", default-features = false, features = ["derive", "alloc"] }
frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
sp-core = { version = "21.0.0", default-features = false, path = "../../primitives/core" }
sp-core = { version = "21.0.0", default-features = false, path = "../../primitives/core", features = ["serde"] }
sp-io = { version = "23.0.0", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "24.0.0", default-features = false, path = "../../primitives/runtime" }
sp-runtime = { version = "24.0.0", default-features = false, path = "../../primitives/runtime", features = ["serde"] }
sp-std = { version = "8.0.0", default-features = false, path = "../../primitives/std" }
sp-version = { version = "22.0.0", default-features = false, path = "../../primitives/version" }
sp-weights = { version = "20.0.0", default-features = false, path = "../../primitives/weights" }
sp-version = { version = "22.0.0", default-features = false, path = "../../primitives/version", features = ["serde"] }
sp-weights = { version = "20.0.0", default-features = false, path = "../../primitives/weights", features = ["serde"] }

[dev-dependencies]
criterion = "0.4.0"
Expand All @@ -38,7 +38,7 @@ std = [
"frame-support/std",
"log/std",
"scale-info/std",
"serde",
"serde/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
Expand Down
2 changes: 1 addition & 1 deletion frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ pub mod pallet {
#[derive(Default)]
#[pallet::genesis_config]
pub struct GenesisConfig {
#[cfg_attr(feature = "std", serde(with = "sp_core::bytes"))]
#[serde(with = "sp_core::bytes")]
pub code: Vec<u8>,
}

Expand Down
4 changes: 2 additions & 2 deletions primitives/runtime/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1772,7 +1772,7 @@ macro_rules! impl_opaque_keys_inner {
/// }
/// ```
#[macro_export]
#[cfg(feature = "std")]
#[cfg(any(feature = "serde", feature = "std"))]
macro_rules! impl_opaque_keys {
{
$( #[ $attr:meta ] )*
Expand Down Expand Up @@ -1802,7 +1802,7 @@ macro_rules! impl_opaque_keys {
}

#[macro_export]
#[cfg(not(feature = "std"))]
#[cfg(all(not(feature = "std"), not(feature = "serde")))]
#[doc(hidden)]
macro_rules! impl_opaque_keys {
{
Expand Down
4 changes: 2 additions & 2 deletions primitives/staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ readme = "README.md"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
serde = { version = "1.0.163", optional = true }
serde = { version = "1.0.163", default-features = false, features = ["derive", "alloc"], optional = true }
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"] }
sp-core = { version = "21.0.0", default-features = false, path = "../core" }
Expand All @@ -23,7 +23,7 @@ sp-std = { version = "8.0.0", default-features = false, path = "../std" }
[features]
default = ["std"]
std = [
"serde",
"serde/std",
"codec/std",
"scale-info/std",
"sp-core/std",
Expand Down
Loading

0 comments on commit 2577379

Please sign in to comment.