Option -> flattened enums + prefix structs

This commit is contained in:
hinto.janai 2024-09-25 21:21:51 -04:00
parent 1b29b7d92f
commit 2643902ecc
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
3 changed files with 53 additions and 28 deletions

View file

@ -20,7 +20,6 @@ json = []
cuprate-epee-encoding = { path = "../net/epee-encoding", optional = true } cuprate-epee-encoding = { path = "../net/epee-encoding", optional = true }
cuprate-fixed-bytes = { path = "../net/fixed-bytes" } cuprate-fixed-bytes = { path = "../net/fixed-bytes" }
# cfg-if = { workspace = true }
bytes = { workspace = true } bytes = { workspace = true }
curve25519-dalek = { workspace = true } curve25519-dalek = { workspace = true }
monero-serai = { workspace = true } monero-serai = { workspace = true }

View file

@ -12,8 +12,8 @@ use crate::json::output::Output;
#[derive(Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Block { pub struct Block {
pub major_version: u64, pub major_version: u8,
pub minor_version: u64, pub minor_version: u8,
pub timestamp: u64, pub timestamp: u64,
pub prev_id: String, pub prev_id: String,
pub nonce: u32, pub nonce: u32,
@ -21,25 +21,41 @@ pub struct Block {
pub tx_hashes: Vec<String>, pub tx_hashes: Vec<String>,
} }
#[derive(Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MinerTransaction { pub struct MinerTransactionPrefix {
pub version: u8, pub version: u8,
pub unlock_time: u64, pub unlock_time: u64,
pub vin: Vec<Input>, pub vin: Vec<Input>,
pub vout: Vec<Output>, pub vout: Vec<Output>,
pub extra: Vec<u8>, pub extra: Vec<u8>,
}
/// Should be [`None`] if [`Self::rct_signatures`] is [`Some`] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// This field always (de)serializes to/from an empty array #[serde(untagged)]
/// as coinbase transactions do not have signatures. pub enum MinerTransaction {
#[serde(default, skip_serializing_if = "Option::is_none")] V1 {
pub signatures: Option<[(); 0]>, /// This field is flattened.
#[serde(flatten)]
prefix: MinerTransactionPrefix,
signatures: [(); 0],
},
V2 {
/// This field is flattened.
#[serde(flatten)]
prefix: MinerTransactionPrefix,
rct_signatures: MinerTransactionRctSignature,
},
}
/// Should be [`None`] if [`Self::signatures`] is [`Some`] impl Default for MinerTransaction {
#[serde(default, skip_serializing_if = "Option::is_none")] fn default() -> Self {
pub rct_signatures: Option<MinerTransactionRctSignature>, Self::V1 {
prefix: Default::default(),
signatures: Default::default(),
}
}
} }
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]

View file

@ -1,6 +1,9 @@
//! JSON transaction types. //! JSON transaction types.
#![expect(non_snake_case, reason = "TODO")] #![expect(
non_snake_case,
reason = "JSON serialization requires non snake-case casing"
)]
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -14,24 +17,31 @@ use crate::json::output::Output;
/// - [`/get_transaction_pool` -> `tx_json`](https://www.getmonero.org/resources/developer-guides/daemon-rpc.html#get_transaction_pool) /// - [`/get_transaction_pool` -> `tx_json`](https://www.getmonero.org/resources/developer-guides/daemon-rpc.html#get_transaction_pool)
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Transaction { #[serde(untagged)]
pub enum Transaction {
V1 {
/// This field is flattened.
#[serde(flatten)]
prefix: TransactionPrefix,
signatures: Vec<String>,
},
V2 {
/// This field is flattened.
#[serde(flatten)]
prefix: TransactionPrefix,
rct_signatures: RctSignatures,
rctsig_prunable: RctSigPrunable,
},
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct TransactionPrefix {
pub version: u8, pub version: u8,
pub unlock_time: u64, pub unlock_time: u64,
pub vin: Vec<Input>, pub vin: Vec<Input>,
pub vout: Vec<Output>, pub vout: Vec<Output>,
pub extra: Vec<u8>, pub extra: Vec<u8>,
/// Should be [`None`] if [`Self::rct_signatures`] is [`Some`]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub signatures: Option<Vec<String>>,
/// Should be [`None`] if [`Self::signatures`] is [`Some`]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub rct_signatures: Option<RctSignatures>,
/// Should be [`None`] if [`Self::signatures`] is [`Some`]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub rctsig_prunable: Option<RctSigPrunable>,
} }
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]