Skip to content

Commit

Permalink
chore: fix serde bounds (#390)
Browse files Browse the repository at this point in the history
* reduce bounds for serialization and deserialization

* update dependencies
  • Loading branch information
ntn-x2 authored Aug 8, 2022
1 parent 886152d commit bebf506
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 83 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions nodes/parachain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ hex-literal = "0.3.4"
jsonrpsee = {version = "0.14.0", features = ["server"]}
log = "0.4.17"
parking_lot = "0.12.1"
serde = {version = "1.0.137", features = ["derive"]}
serde_json = "1.0.79"
serde = {version = "1.0.140", features = ["derive"]}
serde_json = "1.0.82"

# Substrate dependencies
frame-rpc-system = {package = "substrate-frame-rpc-system", git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.26"}
Expand Down
4 changes: 2 additions & 2 deletions nodes/standalone/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ runtime-common = {path = "../../runtimes/common"}
clap = {version = "3.2.10", features = ["derive"]}
futures = "0.3.21"
log = "0.4.17"
serde = {version = "1.0.137", features = ["derive"]}
serde_json = "1.0.79"
serde = {version = "1.0.140", features = ["derive"]}
serde_json = "1.0.82"

# Substrate dependencies
frame-system = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.26"}
Expand Down
4 changes: 2 additions & 2 deletions pallets/attestation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ctype = {features = ["mock"], path = "../ctype"}
kilt-support = {features = ["mock"], path = "../../support"}

pallet-balances = {branch = "polkadot-v0.9.26", git = "https://github.com/paritytech/substrate"}
serde = "1.0.137"
serde = "1.0.140"
sp-core = {branch = "polkadot-v0.9.26", git = "https://github.com/paritytech/substrate"}
sp-io = {branch = "polkadot-v0.9.26", git = "https://github.com/paritytech/substrate"}
sp-keystore = {branch = "polkadot-v0.9.26", git = "https://github.com/paritytech/substrate"}
Expand All @@ -26,7 +26,7 @@ sp-keystore = {branch = "polkadot-v0.9.26", git = "https://github.com/paritytech
codec = {package = "parity-scale-codec", version = "3.1.5", default-features = false, features = ["derive"]}
log = "0.4.17"
scale-info = {version = "2.1.1", default-features = false, features = ["derive"]}
serde = {version = "1.0.137", optional = true}
serde = {version = "1.0.140", optional = true}

# Internal dependencies
ctype = {default-features = false, path = "../ctype"}
Expand Down
4 changes: 2 additions & 2 deletions pallets/ctype/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ substrate-wasm-builder = {git = "https://github.com/paritytech/substrate", branc
[dev-dependencies]
kilt-support = {features = ["mock"], path = "../../support"}
pallet-balances = {branch = "polkadot-v0.9.26", git = "https://github.com/paritytech/substrate"}
serde = "1.0.137"
serde = "1.0.140"
sp-core = {branch = "polkadot-v0.9.26", git = "https://github.com/paritytech/substrate"}
sp-keystore = {branch = "polkadot-v0.9.26", git = "https://github.com/paritytech/substrate"}

[dependencies]
codec = {package = "parity-scale-codec", version = "3.1.5", default-features = false, features = ["derive"]}
log = "0.4.17"
scale-info = {version = "2.1.1", default-features = false, features = ["derive"]}
serde = {version = "1.0.137", optional = true}
serde = {version = "1.0.140", optional = true}

# Internal dependencies
kilt-support = {default-features = false, path = "../../support"}
Expand Down
4 changes: 2 additions & 2 deletions pallets/delegation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ kilt-support = {features = ["mock"], path = "../../support"}

# External dependencies
env_logger = "0.9.0"
serde = "1.0.137"
serde = "1.0.140"

# Substrate dependencies
pallet-balances = {branch = "polkadot-v0.9.26", git = "https://github.com/paritytech/substrate"}
Expand All @@ -37,7 +37,7 @@ bitflags = {default-features = false, version = "1.3.2"}
codec = {package = "parity-scale-codec", version = "3.1.5", default-features = false, features = ["derive"]}
log = "0.4.17"
scale-info = {version = "2.1.1", default-features = false, features = ["derive"]}
serde = {version = "1.0.137", optional = true}
serde = {version = "1.0.140", optional = true}

# Substrate dependencies
frame-benchmarking = {branch = "polkadot-v0.9.26", default-features = false, git = "https://github.com/paritytech/substrate", optional = true}
Expand Down
2 changes: 1 addition & 1 deletion pallets/did/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ substrate-wasm-builder = {git = "https://github.com/paritytech/substrate", branc

[dev-dependencies]
env_logger = "0.9.0"
serde = "1.0.137"
serde = "1.0.140"

ctype = {features = ["mock"], path = "../ctype"}

Expand Down
4 changes: 2 additions & 2 deletions pallets/pallet-inflation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ version = "1.7.1"
targets = ["x86_64-unknown-linux-gnu"]

[dev-dependencies]
serde = "1.0.137"
serde = "1.0.140"

pallet-balances = {branch = "polkadot-v0.9.26", git = "https://github.com/paritytech/substrate"}
sp-core = {branch = "polkadot-v0.9.26", git = "https://github.com/paritytech/substrate"}
Expand All @@ -19,7 +19,7 @@ sp-io = {branch = "polkadot-v0.9.26", git = "https://github.com/paritytech/subst
[dependencies]
codec = {package = "parity-scale-codec", version = "3.1.5", default-features = false, features = ["derive"]}
scale-info = {version = "2.1.1", default-features = false, features = ["derive"]}
serde = {version = "1.0.137", optional = true}
serde = {version = "1.0.140", optional = true}

# benchmarking
frame-benchmarking = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.26", default-features = false, optional = true}
Expand Down
2 changes: 1 addition & 1 deletion pallets/parachain-staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ sp-io = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.
log = "0.4.17"
parity-scale-codec = {version = "3.1.5", default-features = false, features = ["derive"]}
scale-info = {version = "2.1.1", default-features = false, features = ["derive"]}
serde = {version = "1.0.137", optional = true}
serde = {version = "1.0.140", optional = true}

frame-support = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.26", default-features = false}
frame-system = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.26", default-features = false}
Expand Down
22 changes: 7 additions & 15 deletions rpc/did/runtime-api/src/did_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,20 @@ use kilt_support::deposit::Deposit;

#[derive(Encode, Decode, TypeInfo, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "std",
serde(bound(
serialize = "
Balance: std::fmt::Display,
AccountId: Serialize,
Key: Serialize,
BlockNumber: Serialize",
deserialize = "
Balance: std::str::FromStr,
AccountId: Deserialize<'de>,
Key: Deserialize<'de>,
BlockNumber: Deserialize<'de>"
))
)]
pub struct DidDetails<Key: Ord, BlockNumber: MaxEncodedLen, AccountId, Balance> {
pub authentication_key: Key,
pub key_agreement_keys: BTreeSet<Key>,
pub delegation_key: Option<Key>,
pub attestation_key: Option<Key>,
pub public_keys: BTreeMap<Key, DidPublicKeyDetails<BlockNumber>>,
pub last_tx_counter: u64,
#[cfg_attr(
feature = "std",
serde(bound(
serialize = "Deposit<AccountId, Balance>: Serialize",
deserialize = "Deposit<AccountId, Balance>: Deserialize<'de>"
))
)]
pub deposit: Deposit<AccountId, Balance>,
}

Expand Down
34 changes: 7 additions & 27 deletions rpc/did/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,6 @@ pub use service_endpoint::*;

#[derive(Encode, Decode, TypeInfo, PartialEq)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "std",
serde(bound(
serialize = "
Balance: std::fmt::Display,
AccountId: Serialize,
LinkableAccountId: Serialize,
Key: Serialize,
BlockNumber: Serialize,
DidIdentifier: Serialize,
Type: Serialize,
Url: Serialize,
Id: Serialize,
Web3Name: Serialize,",
deserialize = "
Balance: std::str::FromStr,
AccountId: Deserialize<'de>,
LinkableAccountId: Deserialize<'de>,
Key: Deserialize<'de>,
BlockNumber: Deserialize<'de>,
DidIdentifier: Deserialize<'de>,
Type: Deserialize<'de>,
Url: Deserialize<'de>,
Id: Deserialize<'de>,
Web3Name: Deserialize<'de>,"
))
)]
pub struct DidLinkedInfo<
DidIdentifier,
AccountId,
Expand All @@ -75,6 +48,13 @@ pub struct DidLinkedInfo<
pub accounts: Vec<LinkableAccountId>,
pub w3n: Option<Web3Name>,
pub service_endpoints: Vec<ServiceEndpoint<Id, Type, Url>>,
#[cfg_attr(
feature = "std",
serde(bound(
serialize = "DidDetails<Key, BlockNumber, AccountId, Balance>: Serialize",
deserialize = "DidDetails<Key, BlockNumber, AccountId, Balance>: Deserialize<'de>"
))
)]
pub details: DidDetails<Key, BlockNumber, AccountId, Balance>,
}

Expand Down
2 changes: 1 addition & 1 deletion runtimes/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ sp-io = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.
codec = {package = "parity-scale-codec", version = "3.1.5", default-features = false, features = ["derive"]}
log = "0.4.17"
scale-info = {version = "2.1.1", default-features = false, features = ["derive"]}
serde = {version = "1.0.137", optional = true, features = ["derive"]}
serde = {version = "1.0.140", optional = true, features = ["derive"]}
smallvec = "1.8.0"

attestation = {default-features = false, path = "../../pallets/attestation"}
Expand Down
2 changes: 1 addition & 1 deletion runtimes/peregrine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ smallvec = "1.8.0"
static_assertions = "1.1.0"

hex-literal = {version = "0.3.4", optional = true}
serde = {version = "1.0.137", optional = true, features = ["derive"]}
serde = {version = "1.0.140", optional = true, features = ["derive"]}

# RPC
did-rpc-runtime-api = {path = "../../rpc/did/runtime-api", default-features = false}
Expand Down
2 changes: 1 addition & 1 deletion runtimes/spiritnet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ smallvec = "1.8.0"
static_assertions = "1.1.0"

hex-literal = {version = "0.3.4", optional = true}
serde = {version = "1.0.137", optional = true, features = ["derive"]}
serde = {version = "1.0.140", optional = true, features = ["derive"]}

# RPC
did-rpc-runtime-api = {path = "../../rpc/did/runtime-api", default-features = false}
Expand Down
2 changes: 1 addition & 1 deletion runtimes/standalone/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ log = "0.4.17"
scale-info = {version = "2.1.1", default-features = false, features = ["derive"]}

hex-literal = {version = "0.3.4", optional = true}
serde = {version = "1.0.137", optional = true, features = ["derive"]}
serde = {version = "1.0.140", optional = true, features = ["derive"]}

# kilt functionality
attestation = {default-features = false, path = "../../pallets/attestation"}
Expand Down
2 changes: 1 addition & 1 deletion support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ sp-runtime = {branch = "polkadot-v0.9.26", default-features = false, git = "http
sp-std = {branch = "polkadot-v0.9.26", default-features = false, git = "https://github.com/paritytech/substrate"}

[dev-dependencies]
serde_json = "1.0.79"
serde_json = "1.0.82"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
Expand Down
25 changes: 9 additions & 16 deletions support/src/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,18 @@ use serde::{Deserialize, Serialize};
/// An amount of balance reserved by the specified address.
#[derive(Clone, Debug, Encode, Decode, PartialEq, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "std",
serde(bound(
serialize = "
Balance: std::fmt::Display,
Account: Serialize",
deserialize = "
Balance: std::str::FromStr,
Account: Deserialize<'de>"
))
)]
pub struct Deposit<Account, Balance> {
pub owner: Account,
#[cfg_attr(
feature = "std",
serde(bound(serialize = "Balance: std::fmt::Display", deserialize = "Balance: std::str::FromStr"))
)]
#[cfg_attr(feature = "std", serde(with = "serde_balance"))]
pub amount: Balance,
}

// This code was copied from https://github.com/paritytech/substrate/blob/ded44948e2d5a398abcb4e342b0513cb690961bb/frame/transaction-payment/src/types.rs#L113
// It is needed because u128 cannot be serialized by serde out of the box.
// It is needed because u128 cannot be serialized by serde_json out of the box.
#[cfg(feature = "std")]
mod serde_balance {
use serde::{Deserialize, Deserializer, Serializer};
Expand All @@ -66,10 +59,10 @@ mod tests {
fn should_serialize_and_deserialize_properly_with_string() {
let deposit = Deposit {
owner: 0_u8,
amount: 1_000_000_u128,
amount: 0_u128,
};

let json_str = r#"{"owner":0,"amount":"1000000"}"#;
let json_str = r#"{"owner":0,"amount":"0"}"#;

assert_eq!(serde_json::to_string(&deposit).unwrap(), json_str);
assert_eq!(serde_json::from_str::<Deposit<u8, u128>>(json_str).unwrap(), deposit);
Expand All @@ -82,10 +75,10 @@ mod tests {
fn should_serialize_and_deserialize_properly_large_value() {
let deposit = Deposit {
owner: 0_u8,
amount: 1_000_000_u128,
amount: u128::MAX,
};

let json_str = r#"{"owner":0,"amount":"1000000"}"#;
let json_str = r#"{"owner":0,"amount":"340282366920938463463374607431768211455"}"#;

assert_eq!(serde_json::to_string(&deposit).unwrap(), json_str);
assert_eq!(serde_json::from_str::<Deposit<u8, u128>>(json_str).unwrap(), deposit);
Expand Down

0 comments on commit bebf506

Please sign in to comment.