cuprate_types/rpc/
types.rs

1//! Various types (in)directly used in RPC.
2
3use cuprate_fixed_bytes::ByteArrayVec;
4use cuprate_hex::Hex;
5
6use crate::{AddressType, ConnectionState, HardFork};
7
8const fn default_string() -> String {
9    String::new()
10}
11
12fn default_zero<T: From<u8>>() -> T {
13    T::from(0)
14}
15
16/// Output a string link to `monerod` source code.
17macro_rules! monero_definition_link {
18    (
19        $commit:literal, // Git commit hash
20        $file_path:literal, // File path within `monerod`'s `src/`, e.g. `rpc/core_rpc_server_commands_defs.h`
21        $start:literal$(..=$end:literal)? // File lines, e.g. `0..=123` or `0`
22    ) => {
23        concat!(
24            "[Definition](https://github.com/monero-project/monero/blob/",
25            $commit,
26            "/src/",
27            $file_path,
28            "#L",
29            stringify!($start),
30            $(
31                "-L",
32                stringify!($end),
33            )?
34            ")."
35        )
36    };
37}
38
39/// This macro (local to this file) defines all the misc types.
40///
41/// This macro:
42/// 1. Defines a `struct` with all `pub` fields
43/// 2. Implements `serde` on the struct
44/// 3. Implements `epee` on the struct
45///
46/// When using, consider documenting:
47/// - The original Monero definition site with [`monero_definition_link`]
48/// - The request/responses where the `struct` is used
49macro_rules! define_struct_and_impl_epee {
50    ($(
51        // Optional `struct` attributes.
52        $( #[$struct_attr:meta] )*
53        // The `struct`'s name.
54        $struct_name:ident {
55            // And any fields.
56            $(
57                $( #[$field_attr:meta] )* // Field attributes
58                // Field name => the type => optional `epee_object` default value.
59                $field_name:ident: $field_type:ty $(= $field_default:expr_2021)?,
60            )*
61        }
62    )*) => {
63        $(
64            #[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
65            #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
66            $( #[$struct_attr] )*
67            pub struct $struct_name {
68                $(
69                    $( #[$field_attr] )*
70                    pub $field_name: $field_type,
71                )*
72            }
73
74            #[cfg(feature = "epee")]
75            cuprate_epee_encoding::epee_object! {
76                $struct_name,
77                $(
78                    $field_name: $field_type $(= $field_default)?,
79                )*
80            }
81        )*
82    };
83}
84
85define_struct_and_impl_epee! {
86    #[doc = monero_definition_link!(
87        "cc73fe71162d564ffda8e549b79a350bca53c454",
88        "rpc/core_rpc_server_commands_defs.h",
89        1163..=1212
90    )]
91    BlockHeader {
92        block_weight: u64,
93        cumulative_difficulty_top64: u64,
94        cumulative_difficulty: u64,
95        depth: u64,
96        difficulty_top64: u64,
97        difficulty: u64,
98        hash: [u8; 32],
99        height: u64,
100        long_term_weight: u64,
101        major_version: HardFork,
102        miner_tx_hash: [u8; 32],
103        minor_version: u8,
104        nonce: u32,
105        num_txes: u64,
106        orphan_status: bool,
107        /// This is [`None`] if the `fill_pow_hash` param is `false`.
108        pow_hash: Option<[u8; 32]>,
109        prev_hash: [u8; 32],
110        reward: u64,
111        timestamp: u64,
112    }
113
114    #[doc = monero_definition_link!(
115        "cc73fe71162d564ffda8e549b79a350bca53c454",
116        "cryptonote_protocol/cryptonote_protocol_defs.h",
117        47..=116
118    )]
119    ConnectionInfo {
120        address: String,
121        address_type: AddressType,
122        avg_download: u64,
123        avg_upload: u64,
124        connection_id: String,
125        current_download: u64,
126        current_upload: u64,
127        height: u64,
128        host: String,
129        incoming: bool,
130        ip: String,
131        live_time: u64,
132        localhost: bool,
133        local_ip: bool,
134        peer_id: String,
135        port: String,
136        pruning_seed: u32,
137        recv_count: u64,
138        recv_idle_time: u64,
139        rpc_credits_per_hash: u32,
140        rpc_port: u16,
141        send_count: u64,
142        send_idle_time: u64,
143        // Exists in the original definition, but isn't
144        // used or (de)serialized for RPC purposes.
145        // ssl: bool,
146        state: ConnectionState,
147        support_flags: u32,
148    }
149
150    #[doc = monero_definition_link!(
151        "cc73fe71162d564ffda8e549b79a350bca53c454",
152        "rpc/core_rpc_server_commands_defs.h",
153        2034..=2047
154    )]
155    SetBan {
156        #[cfg_attr(feature = "serde", serde(default = "default_string"))]
157        host: String,
158        #[cfg_attr(feature = "serde", serde(default = "default_zero"))]
159        ip: u32,
160        ban: bool,
161        seconds: u32,
162    }
163
164    #[doc = monero_definition_link!(
165        "cc73fe71162d564ffda8e549b79a350bca53c454",
166        "rpc/core_rpc_server_commands_defs.h",
167        1999..=2010
168    )]
169    GetBan {
170        host: String,
171        ip: u32,
172        seconds: u32,
173    }
174
175    #[doc = monero_definition_link!(
176        "cc73fe71162d564ffda8e549b79a350bca53c454",
177        "rpc/core_rpc_server_commands_defs.h",
178        2139..=2156
179    )]
180    #[derive(Copy)]
181    HistogramEntry {
182        amount: u64,
183        total_instances: u64,
184        unlocked_instances: u64,
185        recent_instances: u64,
186    }
187
188    #[doc = monero_definition_link!(
189        "cc73fe71162d564ffda8e549b79a350bca53c454",
190        "rpc/core_rpc_server_commands_defs.h",
191        2180..=2191
192    )]
193    #[derive(Copy)]
194    HardForkEntry {
195        height: u64,
196        hf_version: HardFork,
197    }
198
199    #[doc = monero_definition_link!(
200        "cc73fe71162d564ffda8e549b79a350bca53c454",
201        "rpc/core_rpc_server_commands_defs.h",
202        2289..=2310
203    )]
204    ChainInfo {
205        block_hash: [u8; 32],
206        block_hashes: Vec<[u8; 32]>,
207        difficulty_top64: u64,
208        difficulty: u64,
209        height: u64,
210        length: u64,
211        main_chain_parent_block: [u8; 32],
212    }
213
214    #[doc = monero_definition_link!(
215        "cc73fe71162d564ffda8e549b79a350bca53c454",
216        "rpc/core_rpc_server_commands_defs.h",
217        2393..=2400
218    )]
219    SyncInfoPeer {
220        info: ConnectionInfo,
221    }
222
223    #[doc = monero_definition_link!(
224        "cc73fe71162d564ffda8e549b79a350bca53c454",
225        "rpc/core_rpc_server_commands_defs.h",
226        2402..=2421
227    )]
228    Span {
229        connection_id: String,
230        nblocks: u64,
231        rate: u32,
232        remote_address: String,
233        size: u64,
234        speed: u32,
235        start_block_height: u64,
236    }
237
238    #[doc = monero_definition_link!(
239        "cc73fe71162d564ffda8e549b79a350bca53c454",
240        "rpc/core_rpc_server_commands_defs.h",
241        1637..=1642
242    )]
243    #[derive(Copy)]
244    TxBacklogEntry {
245        weight: u64,
246        fee: u64,
247        time_in_pool: u64,
248    }
249
250    #[doc = monero_definition_link!(
251        "cc73fe71162d564ffda8e549b79a350bca53c454",
252        "rpc/rpc_handler.h",
253        45..=50
254    )]
255    OutputDistributionData {
256        amount: u64,
257        distribution: Vec<u64>,
258        start_height: u64,
259        base: u64,
260    }
261
262    #[doc = monero_definition_link!(
263        "cc73fe71162d564ffda8e549b79a350bca53c454",
264        "rpc/core_rpc_server_commands_defs.h",
265        1016..=1027
266    )]
267    GetMinerDataTxBacklogEntry {
268        id: Hex<32>,
269        weight: u64,
270        fee: u64,
271    }
272
273    #[doc = monero_definition_link!(
274        "cc73fe71162d564ffda8e549b79a350bca53c454",
275        "rpc/core_rpc_server_commands_defs.h",
276        1070..=1079
277    )]
278    AuxPow {
279        id: Hex<32>,
280        hash: Hex<32>,
281    }
282
283    #[doc = monero_definition_link!(
284        "cc73fe71162d564ffda8e549b79a350bca53c454",
285        "rpc/core_rpc_server_commands_defs.h",
286        192..=199
287    )]
288    TxOutputIndices {
289        indices: Vec<u64>,
290    }
291
292    #[doc = monero_definition_link!(
293        "cc73fe71162d564ffda8e549b79a350bca53c454",
294        "rpc/core_rpc_server_commands_defs.h",
295        201..=208
296    )]
297    BlockOutputIndices {
298        indices: Vec<TxOutputIndices>,
299    }
300
301    #[doc = monero_definition_link!(
302        "cc73fe71162d564ffda8e549b79a350bca53c454",
303        "rpc/core_rpc_server_commands_defs.h",
304        512..=521
305    )]
306    #[derive(Copy)]
307    GetOutputsOut {
308        amount: u64,
309        index: u64,
310    }
311
312    #[doc = monero_definition_link!(
313        "cc73fe71162d564ffda8e549b79a350bca53c454",
314        "rpc/core_rpc_server_commands_defs.h",
315        1335..=1367
316    )]
317    Peer {
318        id: u64,
319        host: String,
320        ip: u32,
321        port: u16,
322        #[cfg_attr(feature = "serde", serde(default = "default_zero"))]
323        rpc_port: u16 = default_zero::<u16>(),
324        #[cfg_attr(feature = "serde", serde(default = "default_zero"))]
325        rpc_credits_per_hash: u32 = default_zero::<u32>(),
326        last_seen: u64,
327        #[cfg_attr(feature = "serde", serde(default = "default_zero"))]
328        pruning_seed: u32 = default_zero::<u32>(),
329    }
330
331    #[doc = monero_definition_link!(
332        "cc73fe71162d564ffda8e549b79a350bca53c454",
333        "rpc/core_rpc_server_commands_defs.h",
334        1398..=1417
335    )]
336    PublicNode {
337        host: String,
338        last_seen: u64,
339        rpc_port: u16,
340        rpc_credits_per_hash: u32,
341    }
342
343    #[doc = monero_definition_link!(
344        "cc73fe71162d564ffda8e549b79a350bca53c454",
345        "rpc/core_rpc_server_commands_defs.h",
346        1519..=1556
347    )]
348    TxInfo {
349        blob_size: u64,
350        do_not_relay: bool,
351        double_spend_seen: bool,
352        fee: u64,
353        id_hash: [u8; 32],
354        kept_by_block: bool,
355        last_failed_height: u64,
356        last_failed_id_hash: [u8; 32],
357        last_relayed_time: u64,
358        max_used_block_height: u64,
359        max_used_block_id_hash: [u8; 32],
360        receive_time: u64,
361        relayed: bool,
362        tx_blob: Vec<u8>,
363        tx_json: crate::json::tx::Transaction,
364        #[cfg_attr(feature = "serde", serde(default = "default_zero"))]
365        weight: u64 = default_zero::<u64>(),
366    }
367
368    #[doc = monero_definition_link!(
369        "cc73fe71162d564ffda8e549b79a350bca53c454",
370        "rpc/core_rpc_server_commands_defs.h",
371        1558..=1567
372    )]
373    SpentKeyImageInfo {
374        id_hash: [u8; 32],
375        txs_hashes: Vec<[u8; 32]>,
376    }
377
378    #[doc = monero_definition_link!(
379        "cc73fe71162d564ffda8e549b79a350bca53c454",
380        "rpc/core_rpc_server_commands_defs.h",
381        1666..=1675
382    )]
383    #[derive(Copy)]
384    TxpoolHisto {
385        txs: u32,
386        bytes: u64,
387    }
388
389    #[doc = monero_definition_link!(
390        "cc73fe71162d564ffda8e549b79a350bca53c454",
391        "rpc/core_rpc_server_commands_defs.h",
392        1677..=1710
393    )]
394    TxpoolStats {
395        bytes_max: u32,
396        bytes_med: u32,
397        bytes_min: u32,
398        bytes_total: u64,
399        fee_total: u64,
400        histo_98pc: u64,
401        histo: Vec<TxpoolHisto>,
402        num_10m: u32,
403        num_double_spends: u32,
404        num_failing: u32,
405        num_not_relayed: u32,
406        oldest: u64,
407        txs_total: u32,
408    }
409
410    #[doc = monero_definition_link!(
411        "893916ad091a92e765ce3241b94e706ad012b62a",
412        "blockchain_db/lmdb/db_lmdb.cpp",
413        4222
414    )]
415    OutputHistogramInput {
416        amounts: Vec<u64>,
417        min_count: u64,
418        max_count: u64,
419        unlocked: bool,
420        recent_cutoff: u64,
421    }
422
423    #[doc = monero_definition_link!(
424        "893916ad091a92e765ce3241b94e706ad012b62a",
425        "rpc/core_rpc_server_commands_defs.h",
426        2139..=2156
427    )]
428    OutputHistogramEntry {
429        amount: u64,
430        total_instances: u64,
431        unlocked_instances: u64,
432        recent_instances: u64,
433    }
434
435    #[doc = monero_definition_link!(
436        "893916ad091a92e765ce3241b94e706ad012b62a",
437        "rpc/core_rpc_server_commands_defs.h",
438        2228..=2247
439    )]
440    CoinbaseTxSum {
441        emission_amount_top64: u64,
442        emission_amount: u64,
443        fee_amount_top64: u64,
444        fee_amount: u64,
445    }
446
447    #[doc = monero_definition_link!(
448        "893916ad091a92e765ce3241b94e706ad012b62a",
449        "rpc/core_rpc_server_commands_defs.h",
450        1027..=1033
451    )]
452    MinerData {
453        major_version: u8,
454        height: u64,
455        prev_id: [u8; 32],
456        seed_hash: [u8; 32],
457        difficulty_top64: u64,
458        difficulty: u64,
459        median_weight: u64,
460        already_generated_coins: u64,
461        tx_backlog: Vec<MinerDataTxBacklogEntry>,
462    }
463
464    #[doc = monero_definition_link!(
465        "893916ad091a92e765ce3241b94e706ad012b62a",
466        "rpc/core_rpc_server_commands_defs.h",
467        1037..=1039
468    )]
469    MinerDataTxBacklogEntry {
470        id: [u8; 32],
471        weight: u64,
472        fee: u64,
473    }
474
475    #[doc = monero_definition_link!(
476        "893916ad091a92e765ce3241b94e706ad012b62a",
477        "rpc/core_rpc_server_commands_defs.h",
478        1973..=1980
479    )]
480    HardForkInfo {
481        earliest_height: u64,
482        enabled: bool,
483        state: u32,
484        threshold: u32,
485        version: u8,
486        votes: u32,
487        voting: u8,
488        window: u32,
489    }
490
491    #[doc = monero_definition_link!(
492        "893916ad091a92e765ce3241b94e706ad012b62a",
493        "rpc/core_rpc_server_commands_defs.h",
494        2264..=2267
495    )]
496    FeeEstimate {
497        fee: u64,
498        fees: Vec<u64>,
499        quantization_mask: u64,
500    }
501
502    #[doc = monero_definition_link!(
503        "893916ad091a92e765ce3241b94e706ad012b62a",
504        "rpc/core_rpc_server_commands_defs.h",
505        1115..=1119
506    )]
507    AddAuxPow {
508        blocktemplate_blob: Vec<u8>,
509        blockhashing_blob: Vec<u8>,
510        merkle_root: [u8; 32],
511        merkle_tree_depth: u64,
512        aux_pow: Vec<AuxPow>,
513    }
514
515    #[doc = monero_definition_link!(
516        "893916ad091a92e765ce3241b94e706ad012b62a",
517        "rpc/core_rpc_server_commands_defs.h",
518        227..=229
519    )]
520    PoolTxInfo {
521        tx_hash: [u8; 32],
522        tx_blob: Vec<u8>,
523        double_spend_seen: bool,
524    }
525
526    #[doc = monero_definition_link!(
527        "893916ad091a92e765ce3241b94e706ad012b62a",
528        "rpc/core_rpc_server_commands_defs.h",
529        254..=256
530    )]
531    PoolInfoIncremental {
532        added_pool_txs: Vec<PoolTxInfo>,
533        remaining_added_pool_txids: ByteArrayVec<32>,
534        removed_pool_txids: ByteArrayVec<32>,
535    }
536
537    #[doc = monero_definition_link!(
538        "893916ad091a92e765ce3241b94e706ad012b62a",
539        "rpc/core_rpc_server_commands_defs.h",
540        254..=256
541    )]
542    PoolInfoFull {
543        added_pool_txs: Vec<PoolTxInfo>,
544        remaining_added_pool_txids: ByteArrayVec<32>,
545    }
546}
547
548//---------------------------------------------------------------------------------------------------- Tests
549#[cfg(test)]
550mod test {
551    // use super::*;
552}