diff --git a/Cargo.toml b/Cargo.toml index 1c9b5b6..23e4cc6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,8 +27,8 @@ members = [ "p2p/p2p-core", "p2p/bucket", "p2p/dandelion-tower", - "p2p/address-book", "p2p/async-buffer", + "p2p/address-book", # Storage "storage/blockchain", diff --git a/rpc/types/src/defaults.rs b/rpc/types/src/defaults.rs index 6bf1aa4..9457793 100644 --- a/rpc/types/src/defaults.rs +++ b/rpc/types/src/defaults.rs @@ -5,7 +5,7 @@ //! has a [`crate::json::GetBlockRequest::height`] //! field and a [`crate::json::GetBlockRequest::hash`] //! field, when the RPC interface reads JSON without -//! `height`, it will use [`default_height`] to fill that in. +//! `height`, it will use [`default`] to fill that in. //---------------------------------------------------------------------------------------------------- Import @@ -16,12 +16,6 @@ pub(crate) const fn default_true() -> bool { true } -/// Default `0` value used in request/response types. -#[inline] -pub(crate) fn default_zero>() -> T { - T::from(0) -} - /// Default `1` value used in request/response types. #[inline] pub(crate) fn default_one>() -> T { @@ -36,16 +30,4 @@ pub(crate) fn default() -> T { //---------------------------------------------------------------------------------------------------- Tests #[cfg(test)] -mod test { - use super::*; - - /// Tests that [`default_zero`] returns `0` on all unsigned numbers. - #[test] - fn zero() { - assert_eq!(default_zero::(), 0); - assert_eq!(default_zero::(), 0); - assert_eq!(default_zero::(), 0); - assert_eq!(default_zero::(), 0); - assert_eq!(default_zero::(), 0); - } -} +mod test {} diff --git a/rpc/types/src/json.rs b/rpc/types/src/json.rs index 39165b1..e304a67 100644 --- a/rpc/types/src/json.rs +++ b/rpc/types/src/json.rs @@ -78,15 +78,15 @@ define_request_and_response! { // // This is a HACK since `serde`'s default attribute only takes in // string literals and macros (stringify) within attributes do not work. - extra_nonce: String, - prev_block: String, + extra_nonce: String = default::(), "default", + prev_block: String = default::(), "default", // Another optional expression: // This indicates to the macro to (de)serialize // this field as another type in epee. // // See `cuprate_epee_encoding::epee_object` for info. - reserve_size: u64, + reserve_size: u64 /* as Type */, wallet_address: String, }, @@ -197,7 +197,7 @@ define_request_and_response! { Request { amount_of_blocks: u64, - prev_block: String, + prev_block: String = default::(), "default", starting_nonce: u32, wallet_address: String, }, @@ -217,7 +217,7 @@ define_request_and_response! { #[derive(Copy)] Request { - fill_pow_hash: bool, + fill_pow_hash: bool = default::(), "default", }, AccessResponseBase { @@ -232,9 +232,9 @@ define_request_and_response! { GetBlockHeaderByHash, Request { - hash: Hex<32>, - hashes: Vec>, - fill_pow_hash: bool, + hash: Hex<32> = default::>(), "default", + hashes: Vec> = default::>>(), "default", + fill_pow_hash: bool = default::(), "default", }, AccessResponseBase { @@ -253,7 +253,7 @@ define_request_and_response! { #[derive(Copy)] Request { height: u64, - fill_pow_hash: bool, + fill_pow_hash: bool = default::(), "default", }, AccessResponseBase { @@ -272,7 +272,7 @@ define_request_and_response! { Request { start_height: u64, end_height: u64, - fill_pow_hash: bool, + fill_pow_hash: bool = default::(), "default", }, AccessResponseBase { @@ -290,9 +290,9 @@ define_request_and_response! { // `monerod` has both `hash` and `height` fields. // In the RPC handler, if `hash.is_empty()`, it will use it, else, it uses `height`. // - hash: String, - height: u64, - fill_pow_hash: bool, + hash: String = default::(), "default", + height: u64 = default::(), "default", + fill_pow_hash: bool = default::(), "default", }, AccessResponseBase { @@ -445,7 +445,7 @@ define_request_and_response! { FlushTransactionPool (restricted), Request { - txids: Vec>, + txids: Vec> = default::>>(), "default", }, #[repr(transparent)] @@ -461,11 +461,11 @@ define_request_and_response! { GetOutputHistogram, Request { - amounts: Vec, - min_count: u64, - max_count: u64, - unlocked: bool, - recent_cutoff: u64, + amounts: Vec = default::>(), "default", + min_count: u64 = default::(), "default", + max_count: u64 = default::(), "default", + unlocked: bool = default::(), "default", + recent_cutoff: u64 = default::(), "default", }, AccessResponseBase { @@ -520,7 +520,7 @@ define_request_and_response! { GetFeeEstimate, Request { - grace_blocks: u64, + grace_blocks: u64 = default::(), "default", }, AccessResponseBase { @@ -550,7 +550,7 @@ define_request_and_response! { RelayTx (restricted), Request { - txids: Vec>, + txids: Vec> = default::>>(), "default", }, #[repr(transparent)] @@ -604,10 +604,10 @@ define_request_and_response! { Request { amounts: Vec, binary: bool = default_true(), "default_true", - compress: bool, - cumulative: bool, - from_height: u64, - to_height: u64, + compress: bool = default::(), "default", + cumulative: bool = default::(), "default", + from_height: u64 = default::(), "default", + to_height: u64 = default::(), "default", }, AccessResponseBase { @@ -643,7 +643,7 @@ define_request_and_response! { #[derive(Copy)] Request { - check: bool, + check: bool = default::(), "default", }, ResponseBase { diff --git a/rpc/types/src/macros.rs b/rpc/types/src/macros.rs index 77ffe87..c815844 100644 --- a/rpc/types/src/macros.rs +++ b/rpc/types/src/macros.rs @@ -238,7 +238,6 @@ macro_rules! define_request { ) => { #[allow(dead_code, missing_docs, reason = "inside a macro")] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] - #[cfg_attr(feature = "serde", serde(default))] // TODO: link epee field not serializing oddity #[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] $( #[$attr] )* pub struct $t { @@ -292,6 +291,7 @@ macro_rules! define_response { } ) => { $( #[$attr] )* + #[cfg_attr(feature = "serde", serde(default))] // TODO: link epee field not serializing oddity pub struct $t { $( $( #[$field_attr] )* @@ -329,6 +329,7 @@ macro_rules! define_response { } ) => { $( #[$attr] )* + #[cfg_attr(feature = "serde", serde(default))] // TODO: link epee field not serializing oddity pub struct $t { #[cfg_attr(feature = "serde", serde(flatten))] pub base: $base, diff --git a/rpc/types/src/other.rs b/rpc/types/src/other.rs index 17e88c0..045665f 100644 --- a/rpc/types/src/other.rs +++ b/rpc/types/src/other.rs @@ -12,12 +12,12 @@ use cuprate_types::rpc::{OutKey, Peer, PublicNode, TxpoolStats}; use crate::{ base::{AccessResponseBase, ResponseBase}, macros::define_request_and_response, - misc::{GetOutputsOut, KeyImageSpentStatus, SpentKeyImageInfo, Status, TxEntry, TxInfo}, + misc::{GetOutputsOut, SpentKeyImageInfo, Status, TxEntry, TxInfo}, RpcCallValue, }; #[cfg(any(feature = "serde", feature = "epee"))] -use crate::defaults::default_true; +use crate::defaults::{default, default_true}; //---------------------------------------------------------------------------------------------------- Definitions define_request_and_response! { @@ -44,9 +44,9 @@ define_request_and_response! { // FIXME: this is documented as optional but it isn't serialized as an optional // but it is set _somewhere_ to false in `monerod` // - decode_as_json: bool, - prune: bool, - split: bool, + decode_as_json: bool = default::(), "default", + prune: bool = default::(), "default", + split: bool = default::(), "default", }, AccessResponseBase { @@ -83,7 +83,8 @@ define_request_and_response! { }, AccessResponseBase { - spent_status: Vec, + /// These [`u8`]s are [`crate::misc::KeyImageSpentStatus`]. + spent_status: Vec, } } @@ -96,7 +97,7 @@ define_request_and_response! { Request { tx_as_hex: String, - do_not_relay: bool, + do_not_relay: bool = default::(), "default", do_sanity_checks: bool = default_true(), "default_true", }, @@ -189,7 +190,7 @@ define_request_and_response! { Request { public_only: bool = default_true(), "default_true", - include_blocked: bool, + include_blocked: bool = default::(), "default", }, ResponseBase { @@ -236,7 +237,7 @@ define_request_and_response! { SetLogCategories (restricted), Request { - categories: String, + categories: String = default::(), "default", }, ResponseBase { @@ -253,9 +254,9 @@ define_request_and_response! { Request { address: String, - username: String, - password: String, - proxy: String, + username: String = default::(), "default", + password: String = default::(), "default", + proxy: String = default::(), "default", }, Response { @@ -325,8 +326,8 @@ define_request_and_response! { SetLimit (restricted), Request { - limit_down: i64, - limit_up: i64, + limit_down: i64 = default::(), "default", + limit_up: i64 = default::(), "default", }, ResponseBase { @@ -408,7 +409,7 @@ define_request_and_response! { Request { command: String, - path: String, + path: String = default::(), "default", }, ResponseBase { auto_uri: String, @@ -458,9 +459,9 @@ define_request_and_response! { GetPublicNodes (restricted), Request { - gray: bool, + gray: bool = default::(), "default", white: bool = default_true(), "default_true", - include_blocked: bool, + include_blocked: bool = default::(), "default", }, ResponseBase { @@ -698,7 +699,7 @@ mod test { #[test] fn get_transactions_response() { - test_json::(other::GET_TRANSACTIONS_RESPONSE, None); + test_json::(other::GET_TRANSACTIONS_RESPONSE, None); } #[test] @@ -737,7 +738,7 @@ mod test { other::IS_KEY_IMAGE_SPENT_RESPONSE, Some(IsKeyImageSpentResponse { base: AccessResponseBase::OK, - spent_status: vec![KeyImageSpentStatus::SpentInBlockchain; 2], + spent_status: vec![1, 1], }), ); } diff --git a/types/hex/src/hex.rs b/types/hex/src/hex.rs index f395f66..33bb416 100644 --- a/types/hex/src/hex.rs +++ b/types/hex/src/hex.rs @@ -19,6 +19,17 @@ use serde::{Deserialize, Deserializer, Serialize}; /// /// let from_str = serde_json::from_str::>(expected_json).unwrap(); /// assert_eq!(hex_bytes, from_str); +/// +/// //------ +/// +/// let vec = vec![hex_bytes; 2]; +/// let expected_json = r#"["0101010101010101010101010101010101010101010101010101010101010101","0101010101010101010101010101010101010101010101010101010101010101"]"#; +/// +/// let to_string = serde_json::to_string(&vec).unwrap(); +/// assert_eq!(to_string, expected_json); +/// +/// let from_str = serde_json::from_str::>>(expected_json).unwrap(); +/// assert_eq!(vec, from_str); /// ``` /// /// # Deserialization @@ -30,6 +41,19 @@ use serde::{Deserialize, Deserializer, Serialize}; #[repr(transparent)] pub struct Hex(#[serde(with = "hex::serde")] pub [u8; N]); +impl Hex { + /// Returns `true` if the inner array is zeroed. + /// + /// ```rust + /// # use cuprate_hex::Hex; + /// assert!(Hex([0; 32]).is_zeroed()); + /// assert!(!Hex([1; 32]).is_zeroed()); + /// ``` + pub fn is_zeroed(&self) -> bool { + *self == Self([0; N]) + } +} + impl<'de, const N: usize> Deserialize<'de> for Hex where [u8; N]: FromHex,