Skip to content

Commit

Permalink
fix type defaults, use Hex
Browse files Browse the repository at this point in the history
  • Loading branch information
hinto-janai committed Dec 11, 2024
1 parent 89191ce commit 3a79921
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 144 deletions.
30 changes: 15 additions & 15 deletions rpc/types/src/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
};

#[cfg(any(feature = "epee", feature = "serde"))]
use crate::defaults::{default_false, default_zero};
use crate::defaults::default;

//---------------------------------------------------------------------------------------------------- Definitions
define_request_and_response! {
Expand All @@ -36,7 +36,7 @@ define_request_and_response! {
heights: Vec<u64>,
},
AccessResponseBase {
blocks: Vec<BlockCompleteEntry>,
blocks: Vec<BlockCompleteEntry> = default::<Vec<BlockCompleteEntry>>(), "default",
}
}

Expand All @@ -46,11 +46,11 @@ define_request_and_response! {
core_rpc_server_commands_defs.h => 309..=338,
GetHashes,
Request {
block_ids: ByteArrayVec<32>,
block_ids: ByteArrayVec<32> = default::<ByteArrayVec<32>>(), "default",
start_height: u64,
},
AccessResponseBase {
m_blocks_ids: ByteArrayVec<32>,
m_blocks_ids: ByteArrayVec<32> = default::<ByteArrayVec<32>>(), "default",
start_height: u64,
current_height: u64,
}
Expand All @@ -67,7 +67,7 @@ define_request_and_response! {
txid: [u8; 32],
},
AccessResponseBase {
o_indexes: Vec<u64>,
o_indexes: Vec<u64> = default::<Vec<u64>>(), "default",
}
}

Expand All @@ -92,11 +92,11 @@ define_request_and_response! {
core_rpc_server_commands_defs.h => 512..=565,
GetOuts,
Request {
outputs: Vec<GetOutputsOut>,
get_txid: bool = default_false(), "default_false",
outputs: Vec<GetOutputsOut> = default::<Vec<GetOutputsOut>>(), "default",
get_txid: bool,
},
AccessResponseBase {
outs: Vec<OutKeyBin>,
outs: Vec<OutKeyBin> = default::<Vec<OutKeyBin>>(), "default",
}
}

Expand All @@ -107,7 +107,7 @@ define_request_and_response! {
GetTransactionPoolHashes,
Request {},
AccessResponseBase {
tx_hashes: ByteArrayVec<32>,
tx_hashes: ByteArrayVec<32> = default::<ByteArrayVec<32>>(), "default",
}
}

Expand All @@ -119,22 +119,22 @@ define_request_and_response! {
GetBlocks,

Request {
requested_info: u8 = default_zero::<u8>(), "default_zero",
requested_info: u8 = default::<u8>(), "default",
// FIXME: This is a `std::list` in `monerod` because...?
block_ids: ByteArrayVec<32>,
block_ids: ByteArrayVec<32> = default::<ByteArrayVec<32>>(), "default",
start_height: u64,
prune: bool,
no_miner_tx: bool = default_false(), "default_false",
pool_info_since: u64 = default_zero::<u64>(), "default_zero",
no_miner_tx: bool,
pool_info_since: u64 = default::<u64>(), "default",
},

// TODO: add `top_block_hash` field
// <https://github.com/monero-project/monero/blame/893916ad091a92e765ce3241b94e706ad012b62a/src/rpc/core_rpc_server_commands_defs.h#L263>
AccessResponseBase {
blocks: Vec<BlockCompleteEntry>,
blocks: Vec<BlockCompleteEntry> = default::<Vec<BlockCompleteEntry>>(), "default",
start_height: u64,
current_height: u64,
output_indices: Vec<BlockOutputIndices>,
output_indices: Vec<BlockOutputIndices> = default::<Vec<BlockOutputIndices>>(), "default",
daemon_time: u64,
pool_info: PoolInfo,
}
Expand Down
30 changes: 6 additions & 24 deletions rpc/types/src/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,12 @@
//---------------------------------------------------------------------------------------------------- Import

//---------------------------------------------------------------------------------------------------- TODO
/// Default [`bool`] type used in request/response types, `false`.
#[inline]
pub(crate) const fn default_false() -> bool {
false
}

/// Default [`bool`] type used in _some_ request/response types, `true`.
#[inline]
pub(crate) const fn default_true() -> bool {
true
}

/// Default [`String`] type used in request/response types.
#[inline]
pub(crate) const fn default_string() -> String {
String::new()
}

/// Default block height used in request/response types.
#[inline]
pub(crate) const fn default_height() -> u64 {
0
}

/// Default [`Vec`] used in request/response types.
#[inline]
pub(crate) const fn default_vec<T>() -> Vec<T> {
Vec::new()
}

/// Default `0` value used in request/response types.
#[inline]
pub(crate) fn default_zero<T: From<u8>>() -> T {
Expand All @@ -52,6 +28,12 @@ pub(crate) fn default_one<T: From<u8>>() -> T {
T::from(1)
}

/// Generate a default `T` to be used in request/response types.
#[inline]
pub(crate) fn default<T: Default>() -> T {
T::default()
}

//---------------------------------------------------------------------------------------------------- Tests
#[cfg(test)]
mod test {
Expand Down
74 changes: 34 additions & 40 deletions rpc/types/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ use crate::{
};

#[cfg(any(feature = "epee", feature = "serde"))]
use crate::defaults::{
default_false, default_height, default_one, default_string, default_true, default_vec,
default_zero,
};
use crate::defaults::{default, default_one, default_true};

//---------------------------------------------------------------------------------------------------- Definitions
// This generates 2 structs:
Expand Down Expand Up @@ -81,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 = default_string(), "default_string",
prev_block: String = default_string(), "default_string",
extra_nonce: String,
prev_block: String,

// 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 = default_zero::<u64>(), "default_zero" /* as Type */,
reserve_size: u64,

wallet_address: String,
},
Expand Down Expand Up @@ -125,7 +122,7 @@ define_request_and_response! {
difficulty: u64,
expected_reward: u64,
height: u64,
next_seed_hash: String = default_string(), "default_string",
next_seed_hash: String,
prev_hash: Hex<32>,
reserved_offset: u64,
seed_hash: Hex<32>,
Expand Down Expand Up @@ -200,7 +197,7 @@ define_request_and_response! {

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

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

AccessResponseBase {
Expand All @@ -236,13 +233,13 @@ define_request_and_response! {

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

AccessResponseBase {
block_header: BlockHeader,
block_headers: Vec<BlockHeader> = default_vec::<BlockHeader>(), "default_vec",
block_headers: Vec<BlockHeader>,
}
}

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

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

AccessResponseBase {
Expand All @@ -293,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 = default_string(), "default_string",
height: u64 = default_height(), "default_height",
fill_pow_hash: bool = default_false(), "default_false",
hash: String,
height: u64,
fill_pow_hash: bool,
},

AccessResponseBase {
Expand All @@ -305,7 +302,7 @@ define_request_and_response! {
/// to create this JSON string in a type-safe manner.
json: String,
miner_tx_hash: Hex<32>,
tx_hashes: Vec<Hex<32>> = default_vec::<Hex<32>>(), "default_vec",
tx_hashes: Vec<Hex<32>>,
}
}

Expand All @@ -319,7 +316,6 @@ define_request_and_response! {
Request {},

ResponseBase {
// FIXME: This is a `std::list` in `monerod` because...?
connections: Vec<ConnectionInfo>,
}
}
Expand Down Expand Up @@ -449,7 +445,7 @@ define_request_and_response! {
FlushTransactionPool (restricted),

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

#[repr(transparent)]
Expand All @@ -466,10 +462,10 @@ define_request_and_response! {

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

AccessResponseBase {
Expand Down Expand Up @@ -510,9 +506,9 @@ define_request_and_response! {
ResponseBase {
version: u32,
release: bool,
current_height: u64 = default_zero::<u64>(), "default_zero",
target_height: u64 = default_zero::<u64>(), "default_zero",
hard_forks: Vec<HardForkEntry> = default_vec(), "default_vec",
current_height: u64,
target_height: u64,
hard_forks: Vec<HardForkEntry>,
}
}

Expand All @@ -524,7 +520,7 @@ define_request_and_response! {
GetFeeEstimate,

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

AccessResponseBase {
Expand Down Expand Up @@ -576,10 +572,8 @@ define_request_and_response! {
height: u64,
next_needed_pruning_seed: u32,
overview: String,
// FIXME: This is a `std::list` in `monerod` because...?
peers: Vec<SyncInfoPeer> = default_vec::<SyncInfoPeer>(), "default_vec",
// FIXME: This is a `std::list` in `monerod` because...?
spans: Vec<Span> = default_vec::<Span>(), "default_vec",
peers: Vec<SyncInfoPeer>,
spans: Vec<Span>,
target_height: u64,
}
}
Expand Down Expand Up @@ -610,10 +604,10 @@ define_request_and_response! {
Request {
amounts: Vec<u64>,
binary: bool = default_true(), "default_true",
compress: bool = default_false(), "default_false",
cumulative: bool = default_false(), "default_false",
from_height: u64 = default_zero::<u64>(), "default_zero",
to_height: u64 = default_zero::<u64>(), "default_zero",
compress: bool,
cumulative: bool,
from_height: u64,
to_height: u64,
},

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

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

ResponseBase {
Expand Down Expand Up @@ -688,8 +682,8 @@ define_request_and_response! {

#[derive(Copy)]
Request {
bad_txs: bool = default_false(), "default_false",
bad_blocks: bool = default_false(), "default_false",
bad_txs: bool,
bad_blocks: bool,
},

ResponseBase {}
Expand Down
1 change: 1 addition & 0 deletions rpc/types/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ 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
14 changes: 14 additions & 0 deletions rpc/types/src/misc/key_image_spent_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use cuprate_epee_encoding::{
/// Used in [`crate::other::IsKeyImageSpentResponse`].
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(try_from = "u8", into = "u8"))]
#[repr(u8)]
pub enum KeyImageSpentStatus {
Unspent = 0,
Expand Down Expand Up @@ -68,6 +69,19 @@ impl KeyImageSpentStatus {
}
}

impl From<KeyImageSpentStatus> for u8 {
fn from(value: KeyImageSpentStatus) -> Self {
value.to_u8()
}
}

impl TryFrom<u8> for KeyImageSpentStatus {
type Error = u8;
fn try_from(value: u8) -> Result<Self, Self::Error> {
Self::from_u8(value).ok_or(value)
}
}

#[cfg(feature = "epee")]
impl EpeeValue for KeyImageSpentStatus {
const MARKER: Marker = u8::MARKER;
Expand Down
Loading

0 comments on commit 3a79921

Please sign in to comment.