document types

This commit is contained in:
Boog900 2024-09-07 02:45:45 +01:00
parent f92375f6a6
commit a864f934be
No known key found for this signature in database
GPG key ID: 42AB1287CB0041C2
2 changed files with 157 additions and 11 deletions

View file

@ -327,6 +327,29 @@ pub struct RctOutput {
// TODO: local_index? // TODO: local_index?
//---------------------------------------------------------------------------------------------------- RawChain //---------------------------------------------------------------------------------------------------- RawChain
/// [`Chain`] in a format which can be stored in the DB.
///
/// Implements [`Into`] and [`From`] for [`Chain`].
///
/// ```rust
/// # use std::borrow::*;
/// # use cuprate_blockchain::{*, types::*};
/// use cuprate_database::Storable;
/// use cuprate_types::Chain;
///
/// // Assert Storable is correct.
/// let a: RawChain = Chain::Main.into();
/// let b = Storable::as_bytes(&a);
/// let c: RawChain = Storable::from_bytes(b);
/// assert_eq!(a, c);
/// ```
///
/// # Size & Alignment
/// ```rust
/// # use cuprate_blockchain::types::*;
/// assert_eq!(size_of::<RawChain>(), 8);
/// assert_eq!(align_of::<RawChain>(), 8);
/// ```
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)] #[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)]
#[repr(transparent)] #[repr(transparent)]
pub struct RawChain(u64); pub struct RawChain(u64);
@ -348,6 +371,7 @@ impl From<RawChain> for Chain {
impl From<RawChainId> for RawChain { impl From<RawChainId> for RawChain {
fn from(value: RawChainId) -> Self { fn from(value: RawChainId) -> Self {
// A [`ChainID`] with an inner value of `0` is invalid.
assert_ne!(value.0, 0); assert_ne!(value.0, 0);
Self(value.0) Self(value.0)
@ -355,6 +379,29 @@ impl From<RawChainId> for RawChain {
} }
//---------------------------------------------------------------------------------------------------- RawChainId //---------------------------------------------------------------------------------------------------- RawChainId
/// [`ChainId`] in a format which can be stored in the DB.
///
/// Implements [`Into`] and [`From`] for [`ChainId`].
///
/// ```rust
/// # use std::borrow::*;
/// # use cuprate_blockchain::{*, types::*};
/// use cuprate_database::Storable;
/// use cuprate_types::ChainId;
///
/// // Assert Storable is correct.
/// let a: RawChainId = ChainId(10.try_into().unwrap()).into();
/// let b = Storable::as_bytes(&a);
/// let c: RawChainId = Storable::from_bytes(b);
/// assert_eq!(a, c);
/// ```
///
/// # Size & Alignment
/// ```rust
/// # use cuprate_blockchain::types::*;
/// assert_eq!(size_of::<RawChainId>(), 8);
/// assert_eq!(align_of::<RawChainId>(), 8);
/// ```
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)] #[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)]
#[repr(transparent)] #[repr(transparent)]
pub struct RawChainId(u64); pub struct RawChainId(u64);
@ -374,31 +421,112 @@ impl From<RawChainId> for ChainId {
impl Key for RawChainId {} impl Key for RawChainId {}
//---------------------------------------------------------------------------------------------------- AltChainInfo //---------------------------------------------------------------------------------------------------- AltChainInfo
/// Information on an alternative chain.
///
/// ```rust
/// # use std::borrow::*;
/// # use cuprate_blockchain::{*, types::*};
/// use cuprate_database::Storable;
/// use cuprate_types::Chain;
///
/// // Assert Storable is correct.
/// let a: AltChainInfo = AltChainInfo {
/// parent_chain: Chain::Main.into(),
/// common_ancestor_height: 0,
/// chain_height: 1,
/// };
/// let b = Storable::as_bytes(&a);
/// let c: AltChainInfo = Storable::from_bytes(b);
/// assert_eq!(a, c);
/// ```
///
/// # Size & Alignment
/// ```rust
/// # use cuprate_blockchain::types::*;
/// assert_eq!(size_of::<AltChainInfo>(), 24);
/// assert_eq!(align_of::<AltChainInfo>(), 8);
/// ```
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)] #[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)]
#[repr(C)] #[repr(C)]
pub struct AltChainInfo { pub struct AltChainInfo {
/// The chain this alt chain forks from.
pub parent_chain: RawChain, pub parent_chain: RawChain,
/// The height of the first block we share with the parent chain.
pub common_ancestor_height: usize, pub common_ancestor_height: usize,
/// The chain height of the blocks in this alt chain.
pub chain_height: usize, pub chain_height: usize,
} }
//---------------------------------------------------------------------------------------------------- AltBlockHeight //---------------------------------------------------------------------------------------------------- AltBlockHeight
/// Represents the height of a block on an alt-chain.
///
/// ```rust
/// # use std::borrow::*;
/// # use cuprate_blockchain::{*, types::*};
/// use cuprate_database::Storable;
/// use cuprate_types::ChainId;
///
/// // Assert Storable is correct.
/// let a: AltBlockHeight = AltBlockHeight {
/// chain_id: ChainId(1.try_into().unwrap()).into(),
/// height: 1,
/// };
/// let b = Storable::as_bytes(&a);
/// let c: AltBlockHeight = Storable::from_bytes(b);
/// assert_eq!(a, c);
/// ```
///
/// # Size & Alignment
/// ```rust
/// # use cuprate_blockchain::types::*;
/// assert_eq!(size_of::<AltBlockHeight>(), 16);
/// assert_eq!(align_of::<AltBlockHeight>(), 8);
/// ```
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)] #[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)]
#[repr(C)] #[repr(C)]
pub struct AltBlockHeight { pub struct AltBlockHeight {
/// The [`ChainId`] of the chain this alt block is on, in raw form.
pub chain_id: RawChainId, pub chain_id: RawChainId,
/// The height of this alt-block.
pub height: usize, pub height: usize,
} }
impl Key for AltBlockHeight {} impl Key for AltBlockHeight {}
//---------------------------------------------------------------------------------------------------- CompactAltBlockInfo //---------------------------------------------------------------------------------------------------- CompactAltBlockInfo
/// Represents information on an alt-chain.
///
/// ```rust
/// # use std::borrow::*;
/// # use cuprate_blockchain::{*, types::*};
/// use cuprate_database::Storable;
///
/// // Assert Storable is correct.
/// let a: CompactAltBlockInfo = CompactAltBlockInfo {
/// block_hash: [1; 32],
/// pow_hash: [2; 32],
/// height: 10,
/// weight: 20,
/// long_term_weight: 30,
/// cumulative_difficulty_low: 40,
/// cumulative_difficulty_high: 50,
/// };
///
/// let b = Storable::as_bytes(&a);
/// let c: CompactAltBlockInfo = Storable::from_bytes(b);
/// assert_eq!(a, c);
/// ```
///
/// # Size & Alignment
/// ```rust
/// # use cuprate_blockchain::types::*;
/// assert_eq!(size_of::<CompactAltBlockInfo>(), 104);
/// assert_eq!(align_of::<CompactAltBlockInfo>(), 8);
/// ```
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)] #[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)]
#[repr(C)] #[repr(C)]
pub struct CompactAltBlockInfo { pub struct CompactAltBlockInfo {
/// The block's hash. /// The block's hash.
///
/// [`Block::hash`].
pub block_hash: [u8; 32], pub block_hash: [u8; 32],
/// The block's proof-of-work hash. /// The block's proof-of-work hash.
pub pow_hash: [u8; 32], pub pow_hash: [u8; 32],
@ -408,24 +536,46 @@ pub struct CompactAltBlockInfo {
pub weight: usize, pub weight: usize,
/// The long term block weight, which is the weight factored in with previous block weights. /// The long term block weight, which is the weight factored in with previous block weights.
pub long_term_weight: usize, pub long_term_weight: usize,
/// The cumulative difficulty of all blocks up until and including this block. /// The low 64 bits of the cumulative difficulty.
pub cumulative_difficulty_low: u64, pub cumulative_difficulty_low: u64,
/// The high 64 bits of the cumulative difficulty.
pub cumulative_difficulty_high: u64, pub cumulative_difficulty_high: u64,
} }
//---------------------------------------------------------------------------------------------------- AltTransactionInfo //---------------------------------------------------------------------------------------------------- AltTransactionInfo
/// Represents information on an alt transaction.
///
/// ```rust
/// # use std::borrow::*;
/// # use cuprate_blockchain::{*, types::*};
/// use cuprate_database::Storable;
///
/// // Assert Storable is correct.
/// let a: AltTransactionInfo = AltTransactionInfo {
/// tx_weight: 1,
/// fee: 6,
/// tx_hash: [6; 32],
/// };
///
/// let b = Storable::as_bytes(&a);
/// let c: AltTransactionInfo = Storable::from_bytes(b);
/// assert_eq!(a, c);
/// ```
///
/// # Size & Alignment
/// ```rust
/// # use cuprate_blockchain::types::*;
/// assert_eq!(size_of::<AltTransactionInfo>(), 48);
/// assert_eq!(align_of::<AltTransactionInfo>(), 8);
/// ```
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)] #[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)]
#[repr(C)] #[repr(C)]
pub struct AltTransactionInfo { pub struct AltTransactionInfo {
/// The transaction's weight. /// The transaction's weight.
///
/// [`Transaction::weight`].
pub tx_weight: usize, pub tx_weight: usize,
/// The transaction's total fees. /// The transaction's total fees.
pub fee: u64, pub fee: u64,
/// The transaction's hash. /// The transaction's hash.
///
/// [`Transaction::hash`].
pub tx_hash: [u8; 32], pub tx_hash: [u8; 32],
} }

View file

@ -105,10 +105,6 @@ pub enum BlockchainReadRequest {
//---------------------------------------------------------------------------------------------------- WriteRequest //---------------------------------------------------------------------------------------------------- WriteRequest
/// A write request to the blockchain database. /// A write request to the blockchain database.
///
/// There is currently only 1 write request to the database,
/// as such, the only valid [`BlockchainResponse`] to this request is
/// the proper response for a [`BlockchainResponse::WriteBlockOk`].
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum BlockchainWriteRequest { pub enum BlockchainWriteRequest {
/// Request that a block be written to the database. /// Request that a block be written to the database.