cuprate_rpc_types/base.rs
1//! The base data that appear in many RPC request/responses.
2//!
3//! These are the common "headers" or "base" types that are
4//! [`flattened`](https://serde.rs/field-attrs.html#flatten)
5//! into many of Monero's RPC types.
6//!
7//! [`AccessResponseBase`] is retained for response compatibility with
8//! endpoints that still include `credits` and `top_hash` fields, see:
9//!
10//! - <https://github.com/monero-project/monero/commit/2899379791b7542e4eb920b5d9d58cf232806937>
11//! - <https://github.com/monero-project/monero/issues/8722>
12//! - <https://github.com/monero-project/monero/pull/8843>
13//!
14//---------------------------------------------------------------------------------------------------- Import
15#[cfg(feature = "serde")]
16use serde::{Deserialize, Serialize};
17
18#[cfg(feature = "epee")]
19use cuprate_epee_encoding::epee_object;
20
21use crate::{macros::monero_definition_link, misc::Status};
22
23//---------------------------------------------------------------------------------------------------- Responses
24#[doc = monero_definition_link!("cc73fe71162d564ffda8e549b79a350bca53c454", "rpc/core_rpc_server_commands_defs.h", 101..=112)]
25/// The most common base for responses.
26#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
27#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
28pub struct ResponseBase {
29 /// General RPC error code. [`Status::Ok`] means everything looks good.
30 pub status: Status,
31 /// States if the result is obtained using the bootstrap mode,
32 /// and is therefore not trusted (`true`), or when the daemon
33 /// is fully synced and thus handles the RPC locally (`false`).
34 pub untrusted: bool,
35}
36
37impl ResponseBase {
38 /// [`Status::Ok`] and trusted [`Self`].
39 ///
40 /// This is the most common version of [`Self`].
41 ///
42 /// ```rust
43 /// use cuprate_rpc_types::{misc::*, base::*};
44 ///
45 /// assert_eq!(ResponseBase::OK, ResponseBase {
46 /// status: Status::Ok,
47 /// untrusted: false,
48 /// });
49 /// ```
50 pub const OK: Self = Self {
51 status: Status::Ok,
52 untrusted: false,
53 };
54
55 /// Same as [`Self::OK`] but with [`Self::untrusted`] set to `true`.
56 ///
57 /// ```rust
58 /// use cuprate_rpc_types::{misc::*, base::*};
59 ///
60 /// assert_eq!(ResponseBase::OK_UNTRUSTED, ResponseBase {
61 /// status: Status::Ok,
62 /// untrusted: true,
63 /// });
64 /// ```
65 pub const OK_UNTRUSTED: Self = Self {
66 status: Status::Ok,
67 untrusted: true,
68 };
69}
70
71#[cfg(feature = "epee")]
72epee_object! {
73 ResponseBase,
74 status: Status,
75 untrusted: bool,
76}
77
78#[doc = monero_definition_link!("cc73fe71162d564ffda8e549b79a350bca53c454", "rpc/core_rpc_server_commands_defs.h", 124..=136)]
79/// A base for RPC response types that support RPC payment.
80#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
81#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
82pub struct AccessResponseBase {
83 /// A flattened [`ResponseBase`].
84 #[cfg_attr(feature = "serde", serde(flatten))]
85 pub response_base: ResponseBase,
86 /// If payment for RPC is enabled, the number of credits
87 /// available to the requesting client. Otherwise, `0`.
88 pub credits: u64,
89 /// If payment for RPC is enabled, the hash of the
90 /// highest block in the chain. Otherwise, empty.
91 pub top_hash: String,
92}
93
94impl AccessResponseBase {
95 /// Creates a new [`Self`] with default values.
96 ///
97 /// Since RPC payment is semi-deprecated, [`Self::credits`]
98 /// and [`Self::top_hash`] will always be set to the default
99 /// values.
100 ///
101 /// ```rust
102 /// use cuprate_rpc_types::{misc::*, base::*};
103 ///
104 /// let new = AccessResponseBase::new(ResponseBase::OK);
105 /// assert_eq!(new, AccessResponseBase {
106 /// response_base: ResponseBase::OK,
107 /// credits: 0,
108 /// top_hash: "".into(),
109 /// });
110 /// ```
111 pub const fn new(response_base: ResponseBase) -> Self {
112 Self {
113 response_base,
114 credits: 0,
115 top_hash: String::new(),
116 }
117 }
118
119 /// [`Status::Ok`] and trusted [`Self`].
120 ///
121 /// This is the most common version of [`Self`].
122 ///
123 /// ```rust
124 /// use cuprate_rpc_types::{misc::*, base::*};
125 ///
126 /// assert_eq!(AccessResponseBase::OK, AccessResponseBase {
127 /// response_base: ResponseBase::OK,
128 /// credits: 0,
129 /// top_hash: "".into(),
130 /// });
131 /// ```
132 pub const OK: Self = Self {
133 response_base: ResponseBase::OK,
134 credits: 0,
135 top_hash: String::new(),
136 };
137
138 /// Same as [`Self::OK`] but with `untrusted` set to `true`.
139 ///
140 /// ```rust
141 /// use cuprate_rpc_types::{misc::*, base::*};
142 ///
143 /// assert_eq!(AccessResponseBase::OK_UNTRUSTED, AccessResponseBase {
144 /// response_base: ResponseBase::OK_UNTRUSTED,
145 /// credits: 0,
146 /// top_hash: "".into(),
147 /// });
148 /// ```
149 pub const OK_UNTRUSTED: Self = Self {
150 response_base: ResponseBase::OK_UNTRUSTED,
151 credits: 0,
152 top_hash: String::new(),
153 };
154}
155
156#[cfg(feature = "epee")]
157epee_object! {
158 AccessResponseBase,
159 credits: u64,
160 top_hash: String,
161 !flatten: response_base: ResponseBase,
162}
163
164//---------------------------------------------------------------------------------------------------- Tests
165#[cfg(test)]
166mod test {
167 // use super::*;
168}