derive serialize on more types (#1797)

* derive serialize on more types

---------

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
This commit is contained in:
Pavlo KhrystenkoandNiklas Adolfsson authored and GitHub committed 2024-10-03 11:15:09 +02:00
1 parent cbf05d1999
commit 7f9a1a17c0
3 files changed
+46 -82

No files matched your search

+1 -2
View File
@@ -452,9 +452,8 @@ pub type EncodedJustification = Vec<u8>;
/// This contains the runtime version information necessary to make transactions, as obtained from /// This contains the runtime version information necessary to make transactions, as obtained from
/// the RPC call `state_getRuntimeVersion`, /// the RPC call `state_getRuntimeVersion`,
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(serde::Serialize))]
pub struct RuntimeVersion { pub struct RuntimeVersion {
/// Version of the runtime specification. A full-node will not attempt to use its native /// 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`, /// runtime in substitute for the on-chain Wasm runtime unless all of `spec_name`,
+1 -1
View File
@@ -325,7 +325,7 @@ pub enum TransactionStatus<Hash> {
/// A response from calls like [`Backend::storage_fetch_values`] or /// A response from calls like [`Backend::storage_fetch_values`] or
/// [`Backend::storage_fetch_descendant_values`]. /// [`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 { pub struct StorageResponse {
/// The key. /// The key.
pub key: Vec<u8>, pub key: Vec<u8>,
+44 -79
View File
@@ -318,10 +318,9 @@ impl<T: Config> UnstableRpcMethods<T> {
/// ///
/// The stop event indicates that the JSON-RPC server was unable to provide a consistent list of /// 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. /// 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(rename_all = "camelCase")]
#[serde(tag = "event")] #[serde(tag = "event")]
#[cfg_attr(test, derive(Serialize))]
pub enum FollowEvent<Hash> { pub enum FollowEvent<Hash> {
/// The latest finalized block. /// The latest finalized block.
/// ///
@@ -363,9 +362,8 @@ pub enum FollowEvent<Hash> {
/// ///
/// This is the first event generated by the `follow` subscription /// This is the first event generated by the `follow` subscription
/// and is submitted only once. /// and is submitted only once.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[cfg_attr(test, derive(Serialize))] #[serde(rename_all = "camelCase")]
#[cfg_attr(test, serde(rename_all = "camelCase"))]
pub struct Initialized<Hash> { pub struct Initialized<Hash> {
/// The hashes of the last finalized blocks. /// The hashes of the last finalized blocks.
pub finalized_block_hashes: Vec<Hash>, pub finalized_block_hashes: Vec<Hash>,
@@ -404,10 +402,9 @@ impl<'de, Hash: Deserialize<'de>> Deserialize<'de> for Initialized<Hash> {
/// The runtime event generated if the `follow` subscription /// The runtime event generated if the `follow` subscription
/// has set the `with_runtime` flag. /// has set the `with_runtime` flag.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
#[serde(tag = "type")] #[serde(tag = "type")]
#[cfg_attr(test, derive(Serialize))]
pub enum RuntimeEvent { pub enum RuntimeEvent {
/// The runtime version of this block. /// The runtime version of this block.
Valid(RuntimeVersionEvent), Valid(RuntimeVersionEvent),
@@ -420,9 +417,8 @@ pub enum RuntimeEvent {
/// This event is generated for: /// This event is generated for:
/// - the first announced block by the follow subscription /// - the first announced block by the follow subscription
/// - blocks that suffered a change in runtime compared with their parents /// - 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")] #[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct RuntimeVersionEvent { pub struct RuntimeVersionEvent {
/// Details about this runtime. /// Details about this runtime.
pub spec: RuntimeSpec, pub spec: RuntimeSpec,
@@ -430,9 +426,8 @@ pub struct RuntimeVersionEvent {
/// This contains the runtime version information necessary to make transactions, and is obtained from /// 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. /// 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")] #[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct RuntimeSpec { pub struct RuntimeSpec {
/// Opaque string indicating the name of the chain. /// Opaque string indicating the name of the chain.
pub spec_name: String, 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 /// **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. /// hash of the name of the API. For example, the `TaggedTransactionQueue` API is 0xd2bc9897eed08f15.
#[serde(deserialize_with = "hashmap_as_tuple_list::deserialize")] #[serde(with = "hashmap_as_tuple_list")]
#[cfg_attr(
test,
serde(serialize_with = "hashmap_as_tuple_list::for_test::serialize")
)]
pub apis: HashMap<String, u32>, pub apis: HashMap<String, u32>,
} }
/// The operation could not be processed due to an error. /// 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")] #[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct ErrorEvent { pub struct ErrorEvent {
/// Reason of the error. /// Reason of the error.
pub error: String, pub error: String,
} }
/// Indicate a new non-finalized block. /// Indicate a new non-finalized block.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct NewBlock<Hash> { pub struct NewBlock<Hash> {
/// The hash of the new block. /// The hash of the new block.
pub block_hash: Hash, pub block_hash: Hash,
@@ -495,18 +484,16 @@ pub struct NewBlock<Hash> {
} }
/// Indicate the block hash of the new best block. /// 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")] #[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct BestBlockChanged<Hash> { pub struct BestBlockChanged<Hash> {
/// The block hash of the new best block. /// The block hash of the new best block.
pub best_block_hash: Hash, pub best_block_hash: Hash,
} }
/// Indicate the finalized and pruned block hashes. /// Indicate the finalized and pruned block hashes.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct Finalized<Hash> { pub struct Finalized<Hash> {
/// Block hashes that are finalized. /// Block hashes that are finalized.
pub finalized_block_hashes: Vec<Hash>, pub finalized_block_hashes: Vec<Hash>,
@@ -515,18 +502,16 @@ pub struct Finalized<Hash> {
} }
/// Indicate the operation id of the event. /// Indicate the operation id of the event.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct OperationId { pub struct OperationId {
/// The operation id of the event. /// The operation id of the event.
pub operation_id: String, pub operation_id: String,
} }
/// The response of the `chainHead_body` method. /// The response of the `chainHead_body` method.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct OperationBodyDone { pub struct OperationBodyDone {
/// The operation id of the event. /// The operation id of the event.
pub operation_id: String, pub operation_id: String,
@@ -535,9 +520,8 @@ pub struct OperationBodyDone {
} }
/// The response of the `chainHead_call` method. /// The response of the `chainHead_call` method.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct OperationCallDone { pub struct OperationCallDone {
/// The operation id of the event. /// The operation id of the event.
pub operation_id: String, pub operation_id: String,
@@ -546,9 +530,8 @@ pub struct OperationCallDone {
} }
/// The response of the `chainHead_call` method. /// The response of the `chainHead_call` method.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct OperationStorageItems { pub struct OperationStorageItems {
/// The operation id of the event. /// The operation id of the event.
pub operation_id: String, pub operation_id: String,
@@ -557,9 +540,8 @@ pub struct OperationStorageItems {
} }
/// Indicate a problem during the operation. /// Indicate a problem during the operation.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct OperationError { pub struct OperationError {
/// The operation id of the event. /// The operation id of the event.
pub operation_id: String, pub operation_id: String,
@@ -568,9 +550,8 @@ pub struct OperationError {
} }
/// The storage result. /// The storage result.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct StorageResult { pub struct StorageResult {
/// The hex-encoded key of the result. /// The hex-encoded key of the result.
pub key: Bytes, pub key: Bytes,
@@ -580,9 +561,8 @@ pub struct StorageResult {
} }
/// The type of the storage query. /// The type of the storage query.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub enum StorageResultType { pub enum StorageResultType {
/// Fetch the value of the provided key. /// Fetch the value of the provided key.
Value(Bytes), Value(Bytes),
@@ -593,10 +573,9 @@ pub enum StorageResultType {
} }
/// The method response of `chainHead_body`, `chainHead_call` and `chainHead_storage`. /// 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(rename_all = "camelCase")]
#[serde(tag = "result")] #[serde(tag = "result")]
#[cfg_attr(test, derive(Serialize))]
pub enum MethodResponse { pub enum MethodResponse {
/// The method has started. /// The method has started.
Started(MethodResponseStarted), Started(MethodResponseStarted),
@@ -605,9 +584,8 @@ pub enum MethodResponse {
} }
/// The `started` result of a method. /// The `started` result of a method.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct MethodResponseStarted { pub struct MethodResponseStarted {
/// The operation id of the response. /// The operation id of the response.
pub operation_id: String, pub operation_id: String,
@@ -724,10 +702,9 @@ impl<Hash: BlockHash> Stream for TransactionSubscription<Hash> {
} }
/// Transaction progress events /// Transaction progress events
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
#[serde(tag = "event")] #[serde(tag = "event")]
#[cfg_attr(test, derive(Serialize))]
pub enum TransactionStatus<Hash> { pub enum TransactionStatus<Hash> {
/// Transaction is part of the future queue. /// Transaction is part of the future queue.
Validated, Validated,
@@ -768,17 +745,12 @@ pub enum TransactionStatus<Hash> {
} }
/// Details of a block that a transaction is seen in. /// Details of a block that a transaction is seen in.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[cfg_attr(test, derive(Serialize))]
pub struct TransactionBlockDetails<Hash> { pub struct TransactionBlockDetails<Hash> {
/// The block hash. /// The block hash.
pub hash: Hash, pub hash: Hash,
/// The index of the transaction in the block. /// The index of the transaction in the block.
#[serde(deserialize_with = "unsigned_number_as_string::deserialize")] #[serde(with = "unsigned_number_as_string")]
#[cfg_attr(
test,
serde(serialize_with = "unsigned_number_as_string::for_test::serialize")
)]
pub index: u64, pub index: u64,
} }
@@ -833,17 +805,15 @@ pub(crate) mod unsigned_number_as_string {
Ok(v.into()) Ok(v.into())
} }
} }
#[cfg(test)]
pub mod for_test {
use serde::ser::Serializer;
/// Serialize a number as string use serde::ser::Serializer;
pub fn serialize<S>(item: &u64, serializer: S) -> Result<S::Ok, S::Error>
where /// Serialize a number as string
S: Serializer, pub fn serialize<S>(item: &u64, serializer: S) -> Result<S::Ok, S::Error>
{ where
serializer.serialize_str(&item.to_string()) S: Serializer,
} {
serializer.serialize_str(&item.to_string())
} }
} }
@@ -921,27 +891,22 @@ pub(crate) mod hashmap_as_tuple_list {
Ok(map) Ok(map)
} }
} }
#[cfg(test)]
pub mod for_test {
use std::collections::HashMap;
use std::hash::Hash;
use serde::ser::{Serialize, SerializeSeq, Serializer}; use serde::ser::{Serialize, SerializeSeq, Serializer};
/// Serialize hashmap as list of tuples /// Serialize hashmap as list of tuples
pub fn serialize<S, K: Eq + Hash + Serialize, V: Serialize>( pub fn serialize<S, K: Eq + Hash + Serialize, V: Serialize>(
item: &HashMap<K, V>, item: &HashMap<K, V>,
serializer: S, serializer: S,
) -> Result<S::Ok, S::Error> ) -> Result<S::Ok, S::Error>
where where
S: Serializer, S: Serializer,
{ {
let mut seq = serializer.serialize_seq(None)?; let mut seq = serializer.serialize_seq(None)?;
for i in item { for i in item {
seq.serialize_element(&i)?; seq.serialize_element(&i)?;
}
seq.end()
} }
seq.end()
} }
} }