From fc6b04e21bf001c34188bdd7253a6f6501c368ea Mon Sep 17 00:00:00 2001 From: Pavlo Khrystenko Date: Tue, 1 Oct 2024 11:38:52 +0200 Subject: [PATCH 1/2] derive serialize on more types --- subxt/src/backend/legacy/rpc_methods.rs | 3 +- subxt/src/backend/mod.rs | 2 +- subxt/src/backend/unstable/rpc_methods.rs | 127 ++++++++-------------- 3 files changed, 48 insertions(+), 84 deletions(-) diff --git a/subxt/src/backend/legacy/rpc_methods.rs b/subxt/src/backend/legacy/rpc_methods.rs index 72b328111a..1beae0694e 100644 --- a/subxt/src/backend/legacy/rpc_methods.rs +++ b/subxt/src/backend/legacy/rpc_methods.rs @@ -452,9 +452,8 @@ pub type EncodedJustification = Vec; /// This contains the runtime version information necessary to make transactions, as obtained from /// the RPC call `state_getRuntimeVersion`, -#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] -#[cfg_attr(test, derive(serde::Serialize))] pub struct RuntimeVersion { /// Version of the runtime specification. A full-node will not attempt to use its native /// runtime in substitute for the on-chain Wasm runtime unless all of `spec_name`, diff --git a/subxt/src/backend/mod.rs b/subxt/src/backend/mod.rs index 40779e593d..cc48e1a0a7 100644 --- a/subxt/src/backend/mod.rs +++ b/subxt/src/backend/mod.rs @@ -325,7 +325,7 @@ pub enum TransactionStatus { /// A response from calls like [`Backend::storage_fetch_values`] or /// [`Backend::storage_fetch_descendant_values`]. -#[cfg_attr(test, derive(serde::Serialize, Clone, PartialEq, Debug))] +#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Debug)] pub struct StorageResponse { /// The key. pub key: Vec, diff --git a/subxt/src/backend/unstable/rpc_methods.rs b/subxt/src/backend/unstable/rpc_methods.rs index 0e730c4fb1..13f0ac3399 100644 --- a/subxt/src/backend/unstable/rpc_methods.rs +++ b/subxt/src/backend/unstable/rpc_methods.rs @@ -318,10 +318,9 @@ impl UnstableRpcMethods { /// /// The stop event indicates that the JSON-RPC server was unable to provide a consistent list of /// the blocks at the head of the chain. -#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] #[serde(tag = "event")] -#[cfg_attr(test, derive(Serialize))] pub enum FollowEvent { /// The latest finalized block. /// @@ -363,9 +362,8 @@ pub enum FollowEvent { /// /// This is the first event generated by the `follow` subscription /// and is submitted only once. -#[derive(Debug, Clone, PartialEq, Eq)] -#[cfg_attr(test, derive(Serialize))] -#[cfg_attr(test, serde(rename_all = "camelCase"))] +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] pub struct Initialized { /// The hashes of the last finalized blocks. pub finalized_block_hashes: Vec, @@ -404,10 +402,9 @@ impl<'de, Hash: Deserialize<'de>> Deserialize<'de> for Initialized { /// The runtime event generated if the `follow` subscription /// has set the `with_runtime` flag. -#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] #[serde(tag = "type")] -#[cfg_attr(test, derive(Serialize))] pub enum RuntimeEvent { /// The runtime version of this block. Valid(RuntimeVersionEvent), @@ -420,9 +417,8 @@ pub enum RuntimeEvent { /// This event is generated for: /// - the first announced block by the follow subscription /// - blocks that suffered a change in runtime compared with their parents -#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] -#[cfg_attr(test, derive(Serialize))] pub struct RuntimeVersionEvent { /// Details about this runtime. pub spec: RuntimeSpec, @@ -430,9 +426,8 @@ pub struct RuntimeVersionEvent { /// This contains the runtime version information necessary to make transactions, and is obtained from /// the "initialized" event of `chainHead_follow` if the `withRuntime` flag is set. -#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] -#[cfg_attr(test, derive(Serialize))] pub struct RuntimeSpec { /// Opaque string indicating the name of the chain. pub spec_name: String, @@ -459,27 +454,21 @@ pub struct RuntimeSpec { /// /// **Note:** In Substrate, the keys in the apis field consists of the hexadecimal-encoded 8-bytes blake2 /// hash of the name of the API. For example, the `TaggedTransactionQueue` API is 0xd2bc9897eed08f15. - #[serde(deserialize_with = "hashmap_as_tuple_list::deserialize")] - #[cfg_attr( - test, - serde(serialize_with = "hashmap_as_tuple_list::for_test::serialize") - )] + #[serde(with = "hashmap_as_tuple_list")] pub apis: HashMap, } /// The operation could not be processed due to an error. -#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] -#[cfg_attr(test, derive(Serialize))] pub struct ErrorEvent { /// Reason of the error. pub error: String, } /// Indicate a new non-finalized block. -#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] -#[cfg_attr(test, derive(Serialize))] pub struct NewBlock { /// The hash of the new block. pub block_hash: Hash, @@ -495,18 +484,16 @@ pub struct NewBlock { } /// Indicate the block hash of the new best block. -#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] -#[cfg_attr(test, derive(Serialize))] pub struct BestBlockChanged { /// The block hash of the new best block. pub best_block_hash: Hash, } /// Indicate the finalized and pruned block hashes. -#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] -#[cfg_attr(test, derive(Serialize))] pub struct Finalized { /// Block hashes that are finalized. pub finalized_block_hashes: Vec, @@ -515,18 +502,16 @@ pub struct Finalized { } /// Indicate the operation id of the event. -#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] -#[cfg_attr(test, derive(Serialize))] pub struct OperationId { /// The operation id of the event. pub operation_id: String, } /// The response of the `chainHead_body` method. -#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] -#[cfg_attr(test, derive(Serialize))] pub struct OperationBodyDone { /// The operation id of the event. pub operation_id: String, @@ -535,9 +520,8 @@ pub struct OperationBodyDone { } /// The response of the `chainHead_call` method. -#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] -#[cfg_attr(test, derive(Serialize))] pub struct OperationCallDone { /// The operation id of the event. pub operation_id: String, @@ -546,9 +530,8 @@ pub struct OperationCallDone { } /// The response of the `chainHead_call` method. -#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] -#[cfg_attr(test, derive(Serialize))] pub struct OperationStorageItems { /// The operation id of the event. pub operation_id: String, @@ -557,9 +540,8 @@ pub struct OperationStorageItems { } /// Indicate a problem during the operation. -#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] -#[cfg_attr(test, derive(Serialize))] pub struct OperationError { /// The operation id of the event. pub operation_id: String, @@ -568,9 +550,8 @@ pub struct OperationError { } /// The storage result. -#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] -#[cfg_attr(test, derive(Serialize))] pub struct StorageResult { /// The hex-encoded key of the result. pub key: Bytes, @@ -580,9 +561,8 @@ pub struct StorageResult { } /// The type of the storage query. -#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] -#[cfg_attr(test, derive(Serialize))] pub enum StorageResultType { /// Fetch the value of the provided key. Value(Bytes), @@ -593,10 +573,9 @@ pub enum StorageResultType { } /// The method response of `chainHead_body`, `chainHead_call` and `chainHead_storage`. -#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] #[serde(tag = "result")] -#[cfg_attr(test, derive(Serialize))] pub enum MethodResponse { /// The method has started. Started(MethodResponseStarted), @@ -605,9 +584,8 @@ pub enum MethodResponse { } /// The `started` result of a method. -#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] -#[cfg_attr(test, derive(Serialize))] pub struct MethodResponseStarted { /// The operation id of the response. pub operation_id: String, @@ -724,10 +702,9 @@ impl Stream for TransactionSubscription { } /// Transaction progress events -#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] #[serde(tag = "event")] -#[cfg_attr(test, derive(Serialize))] pub enum TransactionStatus { /// Transaction is part of the future queue. Validated, @@ -768,17 +745,12 @@ pub enum TransactionStatus { } /// Details of a block that a transaction is seen in. -#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] -#[cfg_attr(test, derive(Serialize))] +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] pub struct TransactionBlockDetails { /// The block hash. pub hash: Hash, /// The index of the transaction in the block. - #[serde(deserialize_with = "unsigned_number_as_string::deserialize")] - #[cfg_attr( - test, - serde(serialize_with = "unsigned_number_as_string::for_test::serialize") - )] + #[serde(with = "unsigned_number_as_string")] pub index: u64, } @@ -833,17 +805,15 @@ pub(crate) mod unsigned_number_as_string { Ok(v.into()) } } - #[cfg(test)] - pub mod for_test { - use serde::ser::Serializer; - /// Serialize a number as string - pub fn serialize(item: &u64, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str(&item.to_string()) - } + use serde::ser::Serializer; + + /// Serialize a number as string + pub fn serialize(item: &u64, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_str(&item.to_string()) } } @@ -921,27 +891,22 @@ pub(crate) mod hashmap_as_tuple_list { Ok(map) } } - #[cfg(test)] - pub mod for_test { - use std::collections::HashMap; - use std::hash::Hash; - - use serde::ser::{Serialize, SerializeSeq, Serializer}; - - /// Serialize hashmap as list of tuples - pub fn serialize( - item: &HashMap, - serializer: S, - ) -> Result - where - S: Serializer, - { - let mut seq = serializer.serialize_seq(None)?; - for i in item { - seq.serialize_element(&i)?; - } - seq.end() + + use serde::ser::{Serialize, SerializeSeq, Serializer}; + + /// Serialize hashmap as list of tuples + pub fn serialize( + item: &HashMap, + serializer: S, + ) -> Result + where + S: Serializer, + { + let mut seq = serializer.serialize_seq(None)?; + for i in item { + seq.serialize_element(&i)?; } + seq.end() } } From 18bdaf98dc4dc9ea279e6b925985e04cd73e8315 Mon Sep 17 00:00:00 2001 From: Niklas Adolfsson Date: Tue, 1 Oct 2024 16:03:25 +0200 Subject: [PATCH 2/2] Update subxt/src/backend/unstable/rpc_methods.rs --- subxt/src/backend/unstable/rpc_methods.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/subxt/src/backend/unstable/rpc_methods.rs b/subxt/src/backend/unstable/rpc_methods.rs index 13f0ac3399..7acd55006e 100644 --- a/subxt/src/backend/unstable/rpc_methods.rs +++ b/subxt/src/backend/unstable/rpc_methods.rs @@ -362,7 +362,7 @@ pub enum FollowEvent { /// /// This is the first event generated by the `follow` subscription /// and is submitted only once. -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] #[serde(rename_all = "camelCase")] pub struct Initialized { /// The hashes of the last finalized blocks.