From d0981da31184e9742a3b7835b35f91df47b89c00 Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Wed, 3 Jul 2024 16:40:21 -0400 Subject: [PATCH] support serde/epee default values --- rpc/types/src/defaults.rs | 16 +++++++ rpc/types/src/json.rs | 96 +++++++++++++++++++++++++++++++++++++-- rpc/types/src/lib.rs | 1 + rpc/types/src/macros.rs | 20 ++++---- 4 files changed, 119 insertions(+), 14 deletions(-) create mode 100644 rpc/types/src/defaults.rs diff --git a/rpc/types/src/defaults.rs b/rpc/types/src/defaults.rs new file mode 100644 index 0000000..98b23cc --- /dev/null +++ b/rpc/types/src/defaults.rs @@ -0,0 +1,16 @@ +//! TODO + +//---------------------------------------------------------------------------------------------------- Import + +//---------------------------------------------------------------------------------------------------- TODO +/// TODO +#[inline] +pub(crate) const fn bool() -> bool { + false +} + +//---------------------------------------------------------------------------------------------------- Tests +#[cfg(test)] +mod test { + // use super::*; +} diff --git a/rpc/types/src/json.rs b/rpc/types/src/json.rs index a5b58b2..dec7e0f 100644 --- a/rpc/types/src/json.rs +++ b/rpc/types/src/json.rs @@ -3,7 +3,12 @@ //! . //---------------------------------------------------------------------------------------------------- Import -use crate::{base::ResponseBase, macros::define_request_and_response}; +use crate::{ + base::{AccessResponseBase, ResponseBase}, + defaults::bool, + macros::define_request_and_response, + misc::BlockHeader, +}; //---------------------------------------------------------------------------------------------------- Struct definitions // This generates 2 structs: @@ -109,12 +114,32 @@ define_request_and_response! { core_rpc_server_commands_defs.h => 935..=939, OnGetBlockHash, #[derive(Copy)] + /// ```rust + /// use serde_json::*; + /// use cuprate_rpc_types::json::*; + /// + /// let x = OnGetBlockHashRequest { block_height: [3] }; + /// let x = to_string(&x).unwrap(); + /// assert_eq!(x, "[3]"); + /// ``` + #[cfg_attr(feature = "serde", serde(transparent))] + #[repr(transparent)] Request { - #[cfg_attr(feature = "serde", serde(flatten))] - block_height: u64, + // This is `std::vector` in `monerod` but + // it must be a 1 length array or else it will error. + block_height: [u64; 1], }, + /// ```rust + /// use serde_json::*; + /// use cuprate_rpc_types::json::*; + /// + /// let x = OnGetBlockHashResponse { block_hash: String::from("asdf") }; + /// let x = to_string(&x).unwrap(); + /// assert_eq!(x, "\"asdf\""); + /// ``` + #[cfg_attr(feature = "serde", serde(transparent))] + #[repr(transparent)] Response { - #[cfg_attr(feature = "serde", serde(flatten))] block_hash: String, } } @@ -152,6 +177,69 @@ define_request_and_response! { } } +define_request_and_response! { + generateblocks, + cc73fe71162d564ffda8e549b79a350bca53c454 => + core_rpc_server_commands_defs.h => 1130..=1161, + GenerateBlocks, + Request { + amount_of_blocks: u64, + wallet_address: String, + prev_block: String, + starting_nonce: u32, + }, + ResponseBase { + height: u64, + blocks: Vec, + } +} + +define_request_and_response! { + get_last_block_header, + cc73fe71162d564ffda8e549b79a350bca53c454 => + core_rpc_server_commands_defs.h => 1214..=1238, + GetLastBlockHeader, + Request { + #[cfg_attr(feature = "serde", serde(default = "bool"))] + fill_pow_hash: bool = bool(), + }, + AccessResponseBase { + block_header: BlockHeader, + } +} + +define_request_and_response! { + get_block_header_by_hash, + cc73fe71162d564ffda8e549b79a350bca53c454 => + core_rpc_server_commands_defs.h => 1240..=1269, + GetBlockHeaderByHash, + Request { + hash: String, + hashes: Vec, + #[cfg_attr(feature = "serde", serde(default = "bool"))] + fill_pow_hash: bool = bool(), + }, + AccessResponseBase { + block_header: BlockHeader, + block_headers: Vec, + } +} + +define_request_and_response! { + get_block_header_by_height, + cc73fe71162d564ffda8e549b79a350bca53c454 => + core_rpc_server_commands_defs.h => 1271..=1296, + GetBlockHeaderByHeight, + Request { + height: u64, + #[cfg_attr(feature = "serde", serde(default = "bool"))] + fill_pow_hash: bool = bool(), + }, + AccessResponseBase { + block_header: BlockHeader, + } +} + //---------------------------------------------------------------------------------------------------- Tests #[cfg(test)] mod test { diff --git a/rpc/types/src/lib.rs b/rpc/types/src/lib.rs index f4edb6a..8d614ac 100644 --- a/rpc/types/src/lib.rs +++ b/rpc/types/src/lib.rs @@ -99,6 +99,7 @@ //---------------------------------------------------------------------------------------------------- Use mod binary_string; mod constants; +mod defaults; mod macros; mod status; diff --git a/rpc/types/src/macros.rs b/rpc/types/src/macros.rs index 5de207e..36f8caf 100644 --- a/rpc/types/src/macros.rs +++ b/rpc/types/src/macros.rs @@ -63,7 +63,7 @@ macro_rules! define_request_and_response { // And any fields. $( $( #[$request_field_attr:meta] )* - $request_field:ident: $request_field_type:ty, + $request_field:ident: $request_field_type:ty $(= $request_field_type_default:expr)?, )* }, @@ -73,7 +73,7 @@ macro_rules! define_request_and_response { // And any fields. $( $( #[$response_field_attr:meta] )* - $response_field:ident: $response_field_type:ty, + $response_field:ident: $response_field_type:ty $(= $response_field_type_default:expr)?, )* } ) => { paste::paste! { @@ -91,7 +91,7 @@ macro_rules! define_request_and_response { [<$type_name Request>] { $( $( #[$request_field_attr] )* - $request_field: $request_field_type, + $request_field: $request_field_type $(= $request_field_type_default)?, )* } } @@ -114,7 +114,7 @@ macro_rules! define_request_and_response { $response_base_type => [<$type_name Response>] { $( $( #[$response_field_attr] )* - $response_field: $response_field_type, + $response_field: $response_field_type $(= $response_field_type_default)?, )* } } @@ -145,7 +145,7 @@ macro_rules! define_request { // And any fields. $( $( #[$request_field_attr:meta] )* - $request_field:ident: $request_field_type:ty, + $request_field:ident: $request_field_type:ty $(= $request_field_type_default:expr)?, )* } ) => { @@ -164,7 +164,7 @@ macro_rules! define_request { ::cuprate_epee_encoding::epee_object! { $request_type_name, $( - $request_field: $request_field_type, + $request_field: $request_field_type $(= $request_field_type_default)?, )* } }; @@ -181,7 +181,7 @@ macro_rules! define_response { // And any fields. $( $( #[$response_field_attr:meta] )* - $response_field:ident: $response_field_type:ty, + $response_field:ident: $response_field_type:ty $(= $response_field_type_default:expr)?, )* } ) => { @@ -197,7 +197,7 @@ macro_rules! define_response { ::cuprate_epee_encoding::epee_object! { $response_type_name, $( - $response_field: $response_field_type, + $response_field: $response_field_type $($response_field_type_default)?, )* } }; @@ -211,7 +211,7 @@ macro_rules! define_response { // And any fields. $( $( #[$response_field_attr:meta] )* - $response_field:ident: $response_field_type:ty, + $response_field:ident: $response_field_type:ty $(= $response_field_type_default:expr)?, )* } ) => { @@ -230,7 +230,7 @@ macro_rules! define_response { ::cuprate_epee_encoding::epee_object! { $response_type_name, $( - $response_field: $response_field_type, + $response_field: $response_field_type $(= $response_field_type_default)?, )* !flatten: base: $response_base_type, }