cuprate_rpc_types/misc/
tx_entry.rs

1//! TODO
2
3//---------------------------------------------------------------------------------------------------- Use
4#[cfg(feature = "serde")]
5use serde::{Deserialize, Serialize};
6
7#[cfg(feature = "epee")]
8use cuprate_epee_encoding::{
9    error,
10    macros::bytes::{Buf, BufMut},
11    EpeeObject, EpeeObjectBuilder,
12};
13
14#[cfg(feature = "serde")]
15use crate::serde::{serde_false, serde_true};
16
17//---------------------------------------------------------------------------------------------------- TxEntry
18#[doc = crate::macros::monero_definition_link!(
19    cc73fe71162d564ffda8e549b79a350bca53c454,
20    "rpc/core_rpc_server_commands_defs.h",
21    389..=428
22)]
23/// Used in [`crate::other::GetTransactionsResponse`].
24///
25/// # Epee
26/// This type is only used in a JSON endpoint, so the
27/// epee implementation on this type only panics.
28///
29/// It is only implemented to satisfy the RPC type generator
30/// macro, which requires all objects to be serde + epee.
31///
32/// # Example
33/// ```rust
34/// use cuprate_rpc_types::misc::*;
35/// use serde_json::{json, from_value};
36///
37/// let json = json!({
38///     "as_hex": String::default(),
39///     "as_json": String::default(),
40///     "block_height": u64::default(),
41///     "block_timestamp": u64::default(),
42///     "confirmations": u64::default(),
43///     "double_spend_seen": bool::default(),
44///     "output_indices": Vec::<u64>::default(),
45///     "prunable_as_hex": String::default(),
46///     "prunable_hash": String::default(),
47///     "pruned_as_hex": String::default(),
48///     "tx_hash": String::default(),
49///     "in_pool": bool::default(),
50/// });
51/// let tx_entry = from_value::<TxEntry>(json).unwrap();
52/// assert!(matches!(tx_entry, TxEntry::InPool { .. }));
53///
54/// let json = json!({
55///     "as_hex": String::default(),
56///     "as_json": String::default(),
57///     "double_spend_seen": bool::default(),
58///     "prunable_as_hex": String::default(),
59///     "prunable_hash": String::default(),
60///     "pruned_as_hex": String::default(),
61///     "received_timestamp": u64::default(),
62///     "relayed": bool::default(),
63///     "tx_hash": String::default(),
64///     "in_pool": bool::default(),
65/// });
66/// let tx_entry = from_value::<TxEntry>(json).unwrap();
67/// assert!(matches!(tx_entry, TxEntry::NotInPool { .. }));
68/// ```
69#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
70#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
71#[cfg_attr(feature = "serde", serde(untagged))]
72pub enum TxEntry {
73    /// This entry exists in the transaction pool.
74    InPool {
75        /// This field is [flattened](https://serde.rs/field-attrs.html#flatten).
76        #[cfg_attr(feature = "serde", serde(flatten))]
77        prefix: TxEntryPrefix,
78        block_height: u64,
79        block_timestamp: u64,
80        confirmations: u64,
81        output_indices: Vec<u64>,
82        #[cfg_attr(feature = "serde", serde(serialize_with = "serde_true"))]
83        /// Will always be serialized as `true`.
84        in_pool: bool,
85    },
86    /// This entry _does not_ exist in the transaction pool.
87    NotInPool {
88        /// This field is [flattened](https://serde.rs/field-attrs.html#flatten).
89        #[cfg_attr(feature = "serde", serde(flatten))]
90        prefix: TxEntryPrefix,
91        received_timestamp: u64,
92        relayed: bool,
93        #[cfg_attr(feature = "serde", serde(serialize_with = "serde_false"))]
94        /// Will always be serialized as `false`.
95        in_pool: bool,
96    },
97}
98
99impl Default for TxEntry {
100    fn default() -> Self {
101        Self::NotInPool {
102            prefix: Default::default(),
103            received_timestamp: u64::default(),
104            relayed: bool::default(),
105            in_pool: false,
106        }
107    }
108}
109
110/// Common fields in all [`TxEntry`] variants.
111#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
112#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
113pub struct TxEntryPrefix {
114    as_hex: String,
115    /// `cuprate_rpc_types::json::tx::Transaction` should be used
116    /// to create this JSON string in a type-safe manner.
117    as_json: String,
118    double_spend_seen: bool,
119    tx_hash: String,
120    prunable_as_hex: String,
121    prunable_hash: String,
122    pruned_as_hex: String,
123}
124
125//---------------------------------------------------------------------------------------------------- Epee
126#[cfg(feature = "epee")]
127impl EpeeObjectBuilder<TxEntry> for () {
128    fn add_field<B: Buf>(&mut self, _: &str, _: &mut B) -> error::Result<bool> {
129        unreachable!()
130    }
131
132    fn finish(self) -> error::Result<TxEntry> {
133        unreachable!()
134    }
135}
136
137#[cfg(feature = "epee")]
138impl EpeeObject for TxEntry {
139    type Builder = ();
140
141    fn number_of_fields(&self) -> u64 {
142        unreachable!()
143    }
144
145    fn write_fields<B: BufMut>(self, _: &mut B) -> error::Result<()> {
146        unreachable!()
147    }
148}