cuprate_types/
types.rs

1//! Various shared data types in Cuprate.
2
3use std::num::NonZero;
4
5use curve25519_dalek::edwards::CompressedEdwardsY;
6use monero_serai::{
7    block::Block,
8    transaction::{Timelock, Transaction},
9};
10
11use crate::HardFork;
12
13/// Extended header data of a block.
14///
15/// This contains various metadata of a block, but not the block blob itself.
16#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
17pub struct ExtendedBlockHeader {
18    /// The block's major version.
19    ///
20    /// This is the same value as [`monero_serai::block::BlockHeader::hardfork_version`].
21    pub version: HardFork,
22    /// The block's hard-fork vote.
23    ///
24    /// This can't be represented with [`HardFork`] as raw-votes can be out of the range of [`HardFork`]s.
25    ///
26    /// This is the same value as [`monero_serai::block::BlockHeader::hardfork_signal`].
27    pub vote: u8,
28    /// The UNIX time at which the block was mined.
29    pub timestamp: u64,
30    /// The total amount of coins mined in all blocks so far, including this block's.
31    pub cumulative_difficulty: u128,
32    /// The adjusted block size, in bytes.
33    pub block_weight: usize,
34    /// The long term block weight, based on the median weight of the preceding `100_000` blocks.
35    pub long_term_weight: usize,
36}
37
38/// Verified information of a transaction.
39///
40/// This represents a valid transaction
41#[derive(Clone, Debug, PartialEq, Eq)]
42pub struct VerifiedTransactionInformation {
43    /// The transaction itself.
44    pub tx: Transaction,
45    /// The serialized byte form of [`Self::tx`].
46    ///
47    /// [`Transaction::serialize`].
48    pub tx_blob: Vec<u8>,
49    /// The transaction's weight.
50    ///
51    /// [`Transaction::weight`].
52    pub tx_weight: usize,
53    /// The transaction's total fees.
54    pub fee: u64,
55    /// The transaction's hash.
56    ///
57    /// [`Transaction::hash`].
58    pub tx_hash: [u8; 32],
59}
60
61/// Verified information of a block.
62///
63/// This represents a block that has already been verified to be correct.
64#[derive(Clone, Debug, PartialEq, Eq)]
65pub struct VerifiedBlockInformation {
66    /// The block itself.
67    pub block: Block,
68    /// The serialized byte form of [`Self::block`].
69    ///
70    /// [`Block::serialize`].
71    pub block_blob: Vec<u8>,
72    /// All the transactions in the block, excluding the [`Block::miner_transaction`].
73    pub txs: Vec<VerifiedTransactionInformation>,
74    /// The block's hash.
75    ///
76    /// [`Block::hash`].
77    pub block_hash: [u8; 32],
78    /// The block's proof-of-work hash.
79    // TODO: make this an option.
80    pub pow_hash: [u8; 32],
81    /// The block's height.
82    pub height: usize,
83    /// The amount of generated coins (atomic units) in this block.
84    pub generated_coins: u64,
85    /// The adjusted block size, in bytes.
86    pub weight: usize,
87    /// The long term block weight, which is the weight factored in with previous block weights.
88    pub long_term_weight: usize,
89    /// The cumulative difficulty of all blocks up until and including this block.
90    pub cumulative_difficulty: u128,
91}
92
93/// A unique ID for an alt chain.
94///
95/// The inner value is meaningless.
96#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
97pub struct ChainId(pub NonZero<u64>);
98
99/// An identifier for a chain.
100#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
101pub enum Chain {
102    /// The main chain.
103    Main,
104    /// An alt chain.
105    Alt(ChainId),
106}
107
108/// A block on an alternative chain.
109#[derive(Clone, Debug, PartialEq, Eq)]
110pub struct AltBlockInformation {
111    /// The block itself.
112    pub block: Block,
113    /// The serialized byte form of [`Self::block`].
114    ///
115    /// [`Block::serialize`].
116    pub block_blob: Vec<u8>,
117    /// All the transactions in the block, excluding the [`Block::miner_transaction`].
118    pub txs: Vec<VerifiedTransactionInformation>,
119    /// The block's hash.
120    ///
121    /// [`Block::hash`].
122    pub block_hash: [u8; 32],
123    /// The block's proof-of-work hash.
124    pub pow_hash: [u8; 32],
125    /// The block's height.
126    pub height: usize,
127    /// The adjusted block size, in bytes.
128    pub weight: usize,
129    /// The long term block weight, which is the weight factored in with previous block weights.
130    pub long_term_weight: usize,
131    /// The cumulative difficulty of all blocks up until and including this block.
132    pub cumulative_difficulty: u128,
133    /// The [`ChainId`] of the chain this alt block is on.
134    pub chain_id: ChainId,
135}
136
137/// An already existing transaction output.
138#[derive(Clone, Copy, Debug, PartialEq, Eq)]
139pub struct OutputOnChain {
140    /// The block height this output belongs to.
141    pub height: usize,
142    /// The timelock of this output, if any.
143    pub time_lock: Timelock,
144    /// The public key of this output, if any.
145    pub key: CompressedEdwardsY,
146    /// The output's commitment.
147    pub commitment: CompressedEdwardsY,
148    /// The hash of the transaction hash this output belongs to.
149    /// This is [`None`] when this field is not requested for in relevant functions.
150    pub txid: Option<[u8; 32]>,
151}
152
153/// The inner response for a request for txs in a block.
154#[derive(Clone, Debug, PartialEq, Eq)]
155pub struct TxsInBlock {
156    pub block: Vec<u8>,
157    pub txs: Vec<Vec<u8>>,
158}
159
160/// A block template, used in RPC's `get_block_template`.
161#[derive(Clone, Debug, PartialEq, Eq)]
162pub struct BlockTemplate {
163    pub block: Block,
164    pub reserved_offset: u64,
165    pub difficulty: u128,
166    pub height: u64,
167    pub expected_reward: u64,
168    pub seed_height: u64,
169    pub seed_hash: [u8; 32],
170    pub next_seed_hash: [u8; 32],
171}
172
173/// TODO
174#[derive(Clone, Debug, PartialEq, Eq)]
175pub struct TxInBlockchain {
176    pub block_height: u64,
177    pub block_timestamp: u64,
178    pub confirmations: u64,
179    pub output_indices: Vec<u64>,
180    pub tx_hash: [u8; 32],
181    pub tx_blob: Vec<u8>,
182    pub pruned_blob: Vec<u8>,
183    pub prunable_blob: Vec<u8>,
184    pub prunable_hash: [u8; 32],
185}
186
187/// TODO
188#[derive(Clone, Debug, PartialEq, Eq)]
189pub struct TxInPool {
190    pub tx_blob: Vec<u8>,
191    pub tx_hash: [u8; 32],
192    pub double_spend_seen: bool,
193    pub received_timestamp: u64,
194    pub relayed: bool,
195}
196
197bitflags::bitflags! {
198    pub struct TxRelayChecks: u16 {
199        const DOUBLE_SPEND        = 1;
200        const FEE_TOO_LOW         = 1 << 1;
201        const INVALID_INPUT       = 1 << 2;
202        const INVALID_OUTPUT      = 1 << 3;
203        const LOW_MIXIN           = 1 << 4;
204        const NONZERO_UNLOCK_TIME = 1 << 5;
205        const OVERSPEND           = 1 << 6;
206        const TOO_BIG             = 1 << 7;
207        const TOO_FEW_OUTPUTS     = 1 << 8;
208        const TX_EXTRA_TOO_BIG    = 1 << 9;
209    }
210}
211
212/// Used in RPC's `get_output_distribution`.
213#[derive(Clone, Debug, PartialEq, Eq)]
214pub struct OutputDistributionInput {
215    pub amounts: Vec<u64>,
216    pub cumulative: bool,
217    pub from_height: u64,
218
219    /// [`None`] means the entire blockchain.
220    pub to_height: Option<NonZero<u64>>,
221}
222
223//---------------------------------------------------------------------------------------------------- Tests
224#[cfg(test)]
225mod test {
226    // use super::*;
227}