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//! The `Access*` structs (e.g. [`AccessResponseBase`]
8//! are pseudo-deprecated structs for the RPC payment system, 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//! Note that this library doesn't use [`AccessRequestBase`](https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server_commands_defs.h#L114-L122) found in `monerod`
15//! as the type is practically deprecated.
16//!
17//! Although, [`AccessResponseBase`] still exists as to allow
18//! outputting the same JSON fields as `monerod` (even if deprecated).
19
20//---------------------------------------------------------------------------------------------------- Import
21#[cfg(feature = "serde")]
22use serde::{Deserialize, Serialize};
23
24#[cfg(feature = "epee")]
25use cuprate_epee_encoding::epee_object;
26
27use crate::{macros::monero_definition_link, misc::Status};
28
29//---------------------------------------------------------------------------------------------------- Requests
30/// A base for RPC request types that support RPC payment.
31///
32#[doc = monero_definition_link!(cc73fe71162d564ffda8e549b79a350bca53c454, "rpc/core_rpc_server_commands_defs.h", 114..=122)]
33#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
34#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
35pub struct AccessRequestBase {
36 /// The RPC payment client.
37 pub client: String,
38}
39
40#[cfg(feature = "epee")]
41epee_object! {
42 AccessRequestBase,
43 client: String,
44}
45
46//---------------------------------------------------------------------------------------------------- Responses
47#[doc = monero_definition_link!(cc73fe71162d564ffda8e549b79a350bca53c454, "rpc/core_rpc_server_commands_defs.h", 101..=112)]
48/// The most common base for responses.
49#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
50#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
51pub struct ResponseBase {
52 /// General RPC error code. [`Status::Ok`] means everything looks good.
53 pub status: Status,
54 /// States if the result is obtained using the bootstrap mode,
55 /// and is therefore not trusted (`true`), or when the daemon
56 /// is fully synced and thus handles the RPC locally (`false`).
57 pub untrusted: bool,
58}
59
60impl ResponseBase {
61 /// [`Status::Ok`] and trusted [`Self`].
62 ///
63 /// This is the most common version of [`Self`].
64 ///
65 /// ```rust
66 /// use cuprate_rpc_types::{misc::*, base::*};
67 ///
68 /// assert_eq!(ResponseBase::OK, ResponseBase {
69 /// status: Status::Ok,
70 /// untrusted: false,
71 /// });
72 /// ```
73 pub const OK: Self = Self {
74 status: Status::Ok,
75 untrusted: false,
76 };
77
78 /// Same as [`Self::OK`] but with [`Self::untrusted`] set to `true`.
79 ///
80 /// ```rust
81 /// use cuprate_rpc_types::{misc::*, base::*};
82 ///
83 /// assert_eq!(ResponseBase::OK_UNTRUSTED, ResponseBase {
84 /// status: Status::Ok,
85 /// untrusted: true,
86 /// });
87 /// ```
88 pub const OK_UNTRUSTED: Self = Self {
89 status: Status::Ok,
90 untrusted: true,
91 };
92}
93
94#[cfg(feature = "epee")]
95epee_object! {
96 ResponseBase,
97 status: Status,
98 untrusted: bool,
99}
100
101#[doc = monero_definition_link!(cc73fe71162d564ffda8e549b79a350bca53c454, "rpc/core_rpc_server_commands_defs.h", 124..=136)]
102/// A base for RPC response types that support RPC payment.
103#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
104#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
105pub struct AccessResponseBase {
106 /// A flattened [`ResponseBase`].
107 #[cfg_attr(feature = "serde", serde(flatten))]
108 pub response_base: ResponseBase,
109 /// If payment for RPC is enabled, the number of credits
110 /// available to the requesting client. Otherwise, `0`.
111 pub credits: u64,
112 /// If payment for RPC is enabled, the hash of the
113 /// highest block in the chain. Otherwise, empty.
114 pub top_hash: String,
115}
116
117impl AccessResponseBase {
118 /// Creates a new [`Self`] with default values.
119 ///
120 /// Since RPC payment is semi-deprecated, [`Self::credits`]
121 /// and [`Self::top_hash`] will always be set to the default
122 /// values.
123 ///
124 /// ```rust
125 /// use cuprate_rpc_types::{misc::*, base::*};
126 ///
127 /// let new = AccessResponseBase::new(ResponseBase::OK);
128 /// assert_eq!(new, AccessResponseBase {
129 /// response_base: ResponseBase::OK,
130 /// credits: 0,
131 /// top_hash: "".into(),
132 /// });
133 /// ```
134 pub const fn new(response_base: ResponseBase) -> Self {
135 Self {
136 response_base,
137 credits: 0,
138 top_hash: String::new(),
139 }
140 }
141
142 /// [`Status::Ok`] and trusted [`Self`].
143 ///
144 /// This is the most common version of [`Self`].
145 ///
146 /// ```rust
147 /// use cuprate_rpc_types::{misc::*, base::*};
148 ///
149 /// assert_eq!(AccessResponseBase::OK, AccessResponseBase {
150 /// response_base: ResponseBase::OK,
151 /// credits: 0,
152 /// top_hash: "".into(),
153 /// });
154 /// ```
155 pub const OK: Self = Self {
156 response_base: ResponseBase::OK,
157 credits: 0,
158 top_hash: String::new(),
159 };
160
161 /// Same as [`Self::OK`] but with `untrusted` set to `true`.
162 ///
163 /// ```rust
164 /// use cuprate_rpc_types::{misc::*, base::*};
165 ///
166 /// assert_eq!(AccessResponseBase::OK_UNTRUSTED, AccessResponseBase {
167 /// response_base: ResponseBase::OK_UNTRUSTED,
168 /// credits: 0,
169 /// top_hash: "".into(),
170 /// });
171 /// ```
172 pub const OK_UNTRUSTED: Self = Self {
173 response_base: ResponseBase::OK_UNTRUSTED,
174 credits: 0,
175 top_hash: String::new(),
176 };
177}
178
179#[cfg(feature = "epee")]
180epee_object! {
181 AccessResponseBase,
182 credits: u64,
183 top_hash: String,
184 !flatten: response_base: ResponseBase,
185}
186
187//---------------------------------------------------------------------------------------------------- Tests
188#[cfg(test)]
189mod test {
190 // use super::*;
191}