diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index fb2011e9b6..4409ffcf9e 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -3000,6 +3000,7 @@ dependencies = [ name = "sr-version" version = "0.1.0" dependencies = [ + "impl-serde 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "parity-codec 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "parity-codec-derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/substrate/core/primitives/src/storage.rs b/substrate/core/primitives/src/storage.rs index f3b22294bd..0a14b14756 100644 --- a/substrate/core/primitives/src/storage.rs +++ b/substrate/core/primitives/src/storage.rs @@ -32,6 +32,7 @@ pub struct StorageData(#[cfg_attr(feature = "std", serde(with="bytes"))] pub Vec /// Storage change set #[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug, PartialEq, Eq))] +#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))] pub struct StorageChangeSet { /// Block hash pub block: Hash, diff --git a/substrate/core/rpc/src/state/tests.rs b/substrate/core/rpc/src/state/tests.rs index 8cea4d8a6c..9c860adef7 100644 --- a/substrate/core/rpc/src/state/tests.rs +++ b/substrate/core/rpc/src/state/tests.rs @@ -220,6 +220,11 @@ fn should_return_runtime_version() { api.runtime_version(None.into()), Ok(ref ver) if ver == &runtime::VERSION ); + + assert_eq!( + ::serde_json::to_string(&api.runtime_version(None.into()).unwrap()).unwrap(), + r#"{"specName":"test","implName":"parity-test","authoringVersion":1,"specVersion":1,"implVersion":1,"apis":[["0xdf6acb689907609b",1],["0x37e397fc7c91f5e4",1],["0xd2bc9897eed08f15",1],["0x40fe3ad401f8959a",1],["0xc6e9a76309f39b09",1],["0xdd718d5cc53262d4",1]]}"# + ); } #[test] diff --git a/substrate/core/rpc/src/system/helpers.rs b/substrate/core/rpc/src/system/helpers.rs index 3ab75fb50e..ba4d9c8f16 100644 --- a/substrate/core/rpc/src/system/helpers.rs +++ b/substrate/core/rpc/src/system/helpers.rs @@ -38,17 +38,21 @@ pub struct SystemInfo { /// Health struct returned by the RPC #[derive(Debug, PartialEq, Serialize)] +#[serde(rename_all = "camelCase")] pub struct Health { /// Number of connected peers pub peers: usize, /// Is the node syncing pub is_syncing: bool, /// Should this node have any peers + /// + /// Might be false for local chains or when running without discovery. pub should_have_peers: bool, } /// Network Peer information #[derive(Debug, PartialEq, Serialize)] +#[serde(rename_all = "camelCase")] pub struct PeerInfo { /// Peer Node Index pub index: usize, @@ -71,3 +75,35 @@ impl fmt::Display for Health { } else { "idle" }) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn should_serialize_health() { + assert_eq!( + ::serde_json::to_string(&Health { + peers: 1, + is_syncing: false, + should_have_peers: true, + }).unwrap(), + r#"{"peers":1,"isSyncing":false,"shouldHavePeers":true}"#, + ); + } + + #[test] + fn should_serialize_peer_info() { + assert_eq!( + ::serde_json::to_string(&PeerInfo { + index: 1, + peer_id: "2".into(), + roles: "a".into(), + protocol_version: 2, + best_hash: 5u32, + best_number: 6u32, + }).unwrap(), + r#"{"index":1,"peerId":"2","roles":"a","protocolVersion":2,"bestHash":5,"bestNumber":6}"#, + ); + } +} diff --git a/substrate/core/rpc/src/system/tests.rs b/substrate/core/rpc/src/system/tests.rs index 169d5b914b..1f13abd7ec 100644 --- a/substrate/core/rpc/src/system/tests.rs +++ b/substrate/core/rpc/src/system/tests.rs @@ -19,7 +19,6 @@ use super::*; use network::{self, SyncState, SyncStatus, ProtocolStatus, NodeIndex, PeerId, PeerInfo as NetworkPeerInfo, PublicKey}; use network::config::Roles; use test_client::runtime::Block; -use primitives::H256; #[derive(Default)] struct Status { diff --git a/substrate/core/sr-version/Cargo.toml b/substrate/core/sr-version/Cargo.toml index ef6b2dd205..a166ac475b 100644 --- a/substrate/core/sr-version/Cargo.toml +++ b/substrate/core/sr-version/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" authors = ["Parity Technologies "] [dependencies] +impl-serde = { version = "0.1", optional = true } serde = { version = "1.0", default-features = false } serde_derive = { version = "1.0", optional = true } parity-codec = { version = "2.2", default-features = false } @@ -14,6 +15,7 @@ sr-primitives = { path = "../sr-primitives", default-features = false } [features] default = ["std"] std = [ + "impl-serde", "serde/std", "serde_derive", "parity-codec/std", diff --git a/substrate/core/sr-version/src/lib.rs b/substrate/core/sr-version/src/lib.rs index b24347baa8..4a4c7174ba 100644 --- a/substrate/core/sr-version/src/lib.rs +++ b/substrate/core/sr-version/src/lib.rs @@ -71,7 +71,8 @@ macro_rules! create_apis_vec { /// In particular: bug fixes should result in an increment of `spec_version` and possibly `authoring_version`, /// absolutely not `impl_version` since they change the semantics of the runtime. #[derive(Clone, PartialEq, Eq, Encode)] -#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize, Decode))] +#[cfg_attr(feature = "std", derive(Debug, Serialize, Decode))] +#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))] pub struct RuntimeVersion { /// Identifies the different Substrate runtimes. There'll be at least polkadot and node. /// A different on-chain spec_name to that of the native runtime would normally result @@ -103,6 +104,7 @@ pub struct RuntimeVersion { pub impl_version: u32, /// List of supported API "features" along with their versions. + #[cfg_attr(feature = "std", serde(serialize_with = "apis_serialize::serialize"))] pub apis: ApisVec, } @@ -154,3 +156,36 @@ impl NativeVersion { self.can_author_with.contains(&other.authoring_version)) } } + +#[cfg(feature = "std")] +mod apis_serialize { + extern crate impl_serde; + extern crate serde; + + use super::*; + use self::impl_serde::serialize as bytes; + use self::serde::{Serializer, ser::SerializeTuple}; + + #[derive(Serialize)] + struct ApiId<'a>( + #[serde(serialize_with="serialize_bytesref")] &'a super::ApiId, + &'a u32, + ); + + pub fn serialize(apis: &ApisVec, ser: S) -> Result where + S: Serializer, + { + let len = apis.len(); + let mut seq = ser.serialize_tuple(len)?; + for (api, ver) in &**apis { + seq.serialize_element(&ApiId(api, ver))?; + } + seq.end() + } + + pub fn serialize_bytesref(apis: &&super::ApiId, ser: S) -> Result where + S: Serializer, + { + bytes::serialize(*apis, ser) + } +}