Skip to content

Commit

Permalink
return defaults, hex test
Browse files Browse the repository at this point in the history
  • Loading branch information
hinto-janai committed Dec 11, 2024
1 parent 3a79921 commit 1719d7d
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 67 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 2 additions & 20 deletions rpc/types/src/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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: From<u8>>() -> T {
T::from(0)
}

/// Default `1` value used in request/response types.
#[inline]
pub(crate) fn default_one<T: From<u8>>() -> T {
Expand All @@ -36,16 +30,4 @@ pub(crate) fn default<T: 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::<usize>(), 0);
assert_eq!(default_zero::<u64>(), 0);
assert_eq!(default_zero::<u32>(), 0);
assert_eq!(default_zero::<u16>(), 0);
assert_eq!(default_zero::<u8>(), 0);
}
}
mod test {}
52 changes: 26 additions & 26 deletions rpc/types/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>(), "default",
prev_block: String = default::<String>(), "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,
},
Expand Down Expand Up @@ -197,7 +197,7 @@ define_request_and_response! {

Request {
amount_of_blocks: u64,
prev_block: String,
prev_block: String = default::<String>(), "default",
starting_nonce: u32,
wallet_address: String,
},
Expand All @@ -217,7 +217,7 @@ define_request_and_response! {

#[derive(Copy)]
Request {
fill_pow_hash: bool,
fill_pow_hash: bool = default::<bool>(), "default",
},

AccessResponseBase {
Expand All @@ -232,9 +232,9 @@ define_request_and_response! {
GetBlockHeaderByHash,

Request {
hash: Hex<32>,
hashes: Vec<Hex<32>>,
fill_pow_hash: bool,
hash: Hex<32> = default::<Hex<32>>(), "default",
hashes: Vec<Hex<32>> = default::<Vec<Hex<32>>>(), "default",
fill_pow_hash: bool = default::<bool>(), "default",
},

AccessResponseBase {
Expand All @@ -253,7 +253,7 @@ define_request_and_response! {
#[derive(Copy)]
Request {
height: u64,
fill_pow_hash: bool,
fill_pow_hash: bool = default::<bool>(), "default",
},

AccessResponseBase {
Expand All @@ -272,7 +272,7 @@ define_request_and_response! {
Request {
start_height: u64,
end_height: u64,
fill_pow_hash: bool,
fill_pow_hash: bool = default::<bool>(), "default",
},

AccessResponseBase {
Expand All @@ -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`.
// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L2674>
hash: String,
height: u64,
fill_pow_hash: bool,
hash: String = default::<String>(), "default",
height: u64 = default::<u64>(), "default",
fill_pow_hash: bool = default::<bool>(), "default",
},

AccessResponseBase {
Expand Down Expand Up @@ -445,7 +445,7 @@ define_request_and_response! {
FlushTransactionPool (restricted),

Request {
txids: Vec<Hex<32>>,
txids: Vec<Hex<32>> = default::<Vec<Hex<32>>>(), "default",
},

#[repr(transparent)]
Expand All @@ -461,11 +461,11 @@ define_request_and_response! {
GetOutputHistogram,

Request {
amounts: Vec<u64>,
min_count: u64,
max_count: u64,
unlocked: bool,
recent_cutoff: u64,
amounts: Vec<u64> = default::<Vec<u64>>(), "default",
min_count: u64 = default::<u64>(), "default",
max_count: u64 = default::<u64>(), "default",
unlocked: bool = default::<bool>(), "default",
recent_cutoff: u64 = default::<u64>(), "default",
},

AccessResponseBase {
Expand Down Expand Up @@ -520,7 +520,7 @@ define_request_and_response! {
GetFeeEstimate,

Request {
grace_blocks: u64,
grace_blocks: u64 = default::<u64>(), "default",
},

AccessResponseBase {
Expand Down Expand Up @@ -550,7 +550,7 @@ define_request_and_response! {
RelayTx (restricted),

Request {
txids: Vec<Hex<32>>,
txids: Vec<Hex<32>> = default::<Vec<Hex<32>>>(), "default",
},

#[repr(transparent)]
Expand Down Expand Up @@ -604,10 +604,10 @@ define_request_and_response! {
Request {
amounts: Vec<u64>,
binary: bool = default_true(), "default_true",
compress: bool,
cumulative: bool,
from_height: u64,
to_height: u64,
compress: bool = default::<bool>(), "default",
cumulative: bool = default::<bool>(), "default",
from_height: u64 = default::<u64>(), "default",
to_height: u64 = default::<u64>(), "default",
},

AccessResponseBase {
Expand Down Expand Up @@ -643,7 +643,7 @@ define_request_and_response! {

#[derive(Copy)]
Request {
check: bool,
check: bool = default::<bool>(), "default",
},

ResponseBase {
Expand Down
3 changes: 2 additions & 1 deletion rpc/types/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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] )*
Expand Down Expand Up @@ -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,
Expand Down
39 changes: 20 additions & 19 deletions rpc/types/src/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand All @@ -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`
// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server_commands_defs.h#L382>
decode_as_json: bool,
prune: bool,
split: bool,
decode_as_json: bool = default::<bool>(), "default",
prune: bool = default::<bool>(), "default",
split: bool = default::<bool>(), "default",
},

AccessResponseBase {
Expand Down Expand Up @@ -83,7 +83,8 @@ define_request_and_response! {
},

AccessResponseBase {
spent_status: Vec<KeyImageSpentStatus>,
/// These [`u8`]s are [`crate::misc::KeyImageSpentStatus`].
spent_status: Vec<u8>,
}
}

Expand All @@ -96,7 +97,7 @@ define_request_and_response! {

Request {
tx_as_hex: String,
do_not_relay: bool,
do_not_relay: bool = default::<bool>(), "default",
do_sanity_checks: bool = default_true(), "default_true",
},

Expand Down Expand Up @@ -189,7 +190,7 @@ define_request_and_response! {

Request {
public_only: bool = default_true(), "default_true",
include_blocked: bool,
include_blocked: bool = default::<bool>(), "default",
},

ResponseBase {
Expand Down Expand Up @@ -236,7 +237,7 @@ define_request_and_response! {
SetLogCategories (restricted),

Request {
categories: String,
categories: String = default::<String>(), "default",
},

ResponseBase {
Expand All @@ -253,9 +254,9 @@ define_request_and_response! {

Request {
address: String,
username: String,
password: String,
proxy: String,
username: String = default::<String>(), "default",
password: String = default::<String>(), "default",
proxy: String = default::<String>(), "default",
},

Response {
Expand Down Expand Up @@ -325,8 +326,8 @@ define_request_and_response! {
SetLimit (restricted),

Request {
limit_down: i64,
limit_up: i64,
limit_down: i64 = default::<i64>(), "default",
limit_up: i64 = default::<i64>(), "default",
},

ResponseBase {
Expand Down Expand Up @@ -408,7 +409,7 @@ define_request_and_response! {

Request {
command: String,
path: String,
path: String = default::<String>(), "default",
},
ResponseBase {
auto_uri: String,
Expand Down Expand Up @@ -458,9 +459,9 @@ define_request_and_response! {
GetPublicNodes (restricted),

Request {
gray: bool,
gray: bool = default::<bool>(), "default",
white: bool = default_true(), "default_true",
include_blocked: bool,
include_blocked: bool = default::<bool>(), "default",
},

ResponseBase {
Expand Down Expand Up @@ -698,7 +699,7 @@ mod test {

#[test]
fn get_transactions_response() {
test_json::<GetTransactionsRequest>(other::GET_TRANSACTIONS_RESPONSE, None);
test_json::<GetTransactionsResponse>(other::GET_TRANSACTIONS_RESPONSE, None);
}

#[test]
Expand Down Expand Up @@ -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],
}),
);
}
Expand Down
24 changes: 24 additions & 0 deletions types/hex/src/hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ use serde::{Deserialize, Deserializer, Serialize};
///
/// let from_str = serde_json::from_str::<Hex<32>>(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::<Vec<Hex<32>>>(expected_json).unwrap();
/// assert_eq!(vec, from_str);
/// ```
///
/// # Deserialization
Expand All @@ -30,6 +41,19 @@ use serde::{Deserialize, Deserializer, Serialize};
#[repr(transparent)]
pub struct Hex<const N: usize>(#[serde(with = "hex::serde")] pub [u8; N]);

impl<const N: usize> Hex<N> {
/// 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<N>
where
[u8; N]: FromHex,
Expand Down

0 comments on commit 1719d7d

Please sign in to comment.