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}
149
150/// Input required to generate an output histogram.
151///
152/// Used in RPC's `get_output_histogram`.
153#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
154pub struct OutputHistogramInput {
155    pub amounts: Vec<u64>,
156    pub min_count: u64,
157    pub max_count: u64,
158    pub unlocked: bool,
159    pub recent_cutoff: u64,
160}
161
162/// A single entry in an output histogram.
163///
164/// Used in RPC's `get_output_histogram`.
165#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
166pub struct OutputHistogramEntry {
167    pub amount: u64,
168    pub total_instances: u64,
169    pub unlocked_instances: u64,
170    pub recent_instances: u64,
171}
172
173/// Data of summed coinbase transactions.
174///
175/// Used in RPC's `get_coinbase_tx_sum`.
176#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
177pub struct CoinbaseTxSum {
178    pub emission_amount: u128,
179    pub fee_amount: u128,
180}
181
182/// Data to create a custom block template.
183///
184/// Used in RPC's `get_miner_data`.
185#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
186pub struct MinerData {
187    pub major_version: u8,
188    pub height: u64,
189    pub prev_id: [u8; 32],
190    pub seed_hash: [u8; 32],
191    pub difficulty: u128,
192    pub median_weight: u64,
193    pub already_generated_coins: u64,
194    pub tx_backlog: Vec<MinerDataTxBacklogEntry>,
195}
196
197/// A transaction in the txpool.
198///
199/// Used in [`MinerData::tx_backlog`].
200#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
201pub struct MinerDataTxBacklogEntry {
202    pub id: [u8; 32],
203    pub weight: u64,
204    pub fee: u64,
205}
206
207/// Information on a [`HardFork`].
208///
209/// Used in RPC's `hard_fork_info`.
210#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
211pub struct HardForkInfo {
212    pub earliest_height: u64,
213    pub enabled: bool,
214    pub state: u32,
215    pub threshold: u32,
216    pub version: u8,
217    pub votes: u32,
218    pub voting: u8,
219    pub window: u32,
220}
221
222/// Estimated fee data.
223///
224/// Used in RPC's `get_fee_estimate`.
225#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
226pub struct FeeEstimate {
227    pub fee: u64,
228    pub fees: Vec<u64>,
229    pub quantization_mask: u64,
230}
231
232/// Information on a (maybe alternate) chain.
233///
234/// Used in RPC's `get_alternate_chains`.
235#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
236pub struct ChainInfo {
237    pub block_hash: [u8; 32],
238    pub block_hashes: Vec<[u8; 32]>,
239    pub difficulty: u128,
240    pub height: u64,
241    pub length: u64,
242    pub main_chain_parent_block: [u8; 32],
243}
244
245/// Used in RPC's `add_aux_pow`.
246#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
247pub struct AuxPow {
248    pub id: [u8; 32],
249    pub hash: [u8; 32],
250}
251
252/// Used in RPC's `add_aux_pow`.
253#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
254pub struct AddAuxPow {
255    pub blocktemplate_blob: Vec<u8>,
256    pub blockhashing_blob: Vec<u8>,
257    pub merkle_root: [u8; 32],
258    pub merkle_tree_depth: u64,
259    pub aux_pow: Vec<AuxPow>,
260}
261
262/// The inner response for a request for txs in a block.
263#[derive(Clone, Debug, PartialEq, Eq)]
264pub struct TxsInBlock {
265    pub block: Vec<u8>,
266    pub txs: Vec<Vec<u8>>,
267}
268
269//---------------------------------------------------------------------------------------------------- Tests
270#[cfg(test)]
271mod test {
272    // use super::*;
273}