From d6488719661f82461a24351ab41bfa2fcf3e2eb9 Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Thu, 29 Aug 2024 18:44:34 +0100 Subject: [PATCH] add new tables & types --- storage/blockchain/src/lib.rs | 2 +- storage/blockchain/src/tables.rs | 25 ++++++-- storage/blockchain/src/types.rs | 101 ++++++++++++++++++++++++++++++- types/src/types.rs | 3 +- 4 files changed, 123 insertions(+), 8 deletions(-) diff --git a/storage/blockchain/src/lib.rs b/storage/blockchain/src/lib.rs index e544a69e..0dea345b 100644 --- a/storage/blockchain/src/lib.rs +++ b/storage/blockchain/src/lib.rs @@ -52,7 +52,7 @@ unused_crate_dependencies, unused_doc_comments, unused_mut, - missing_docs, + //missing_docs, deprecated, unused_comparisons, nonstandard_style diff --git a/storage/blockchain/src/tables.rs b/storage/blockchain/src/tables.rs index 122ac31b..6db76816 100644 --- a/storage/blockchain/src/tables.rs +++ b/storage/blockchain/src/tables.rs @@ -16,11 +16,7 @@ //! accessing _all_ tables defined here at once. //---------------------------------------------------------------------------------------------------- Import -use crate::types::{ - Amount, AmountIndex, AmountIndices, BlockBlob, BlockHash, BlockHeight, BlockInfo, KeyImage, - Output, PreRctOutputId, PrunableBlob, PrunableHash, PrunedBlob, RctOutput, TxBlob, TxHash, - TxId, UnlockTime, -}; +use crate::types::{Amount, AmountIndex, AmountIndices, BlockBlob, BlockHash, BlockHeight, BlockInfo, KeyImage, Output, PreRctOutputId, PrunableBlob, PrunableHash, PrunedBlob, RctOutput, TxBlob, TxHash, TxId, UnlockTime, RawChainId, AltChainInfo, AltBlockHeight, CompactAltBlockInfo, AltTransactionInfo}; //---------------------------------------------------------------------------------------------------- Tables // Notes: @@ -129,6 +125,25 @@ cuprate_database::define_tables! { /// Transactions without unlock times will not exist in this table. 14 => TxUnlockTime, TxId => UnlockTime, + + 15 => AltChainInfos, + RawChainId => AltChainInfo, + + 16 => AltBlockHeights, + BlockHash => AltBlockHeight, + + 17 => AltBlocksInfo, + AltBlockHeight => CompactAltBlockInfo, + + 18 => AltBlockBlobs, + AltBlockHeight => BlockBlob, + + 19 => AltTransactionBlobs, + TxHash => TxBlob, + + 20 => AltTransactionInfos, + TxHash => AltTransactionInfo, + } //---------------------------------------------------------------------------------------------------- Tests diff --git a/storage/blockchain/src/types.rs b/storage/blockchain/src/types.rs index eb1dc647..73c7614f 100644 --- a/storage/blockchain/src/types.rs +++ b/storage/blockchain/src/types.rs @@ -41,13 +41,15 @@ #![forbid(unsafe_code)] // if you remove this line i will steal your monero //---------------------------------------------------------------------------------------------------- Import +use std::num::NonZero; + use bytemuck::{Pod, Zeroable}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; use cuprate_database::{Key, StorableVec}; - +use cuprate_types::{Chain, ChainId}; //---------------------------------------------------------------------------------------------------- Aliases // These type aliases exist as many Monero-related types are the exact same. // For clarity, they're given type aliases as to not confuse them. @@ -324,6 +326,103 @@ pub struct RctOutput { } // TODO: local_index? +//---------------------------------------------------------------------------------------------------- RawChain +#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)] +#[repr(transparent)] +pub struct RawChain(u64); + +impl From for RawChain { + fn from(value: Chain) -> Self { + match value { + Chain::Main => RawChain(0), + Chain::Alt(chain_id) => RawChain(chain_id.0.get()), + } + } +} + +impl From for Chain { + fn from(value: RawChain) -> Self { + NonZero::new(value.0) + .map(|id| Chain::Alt(ChainId(id))) + .unwrap_or(Chain::Main) + } +} + +//---------------------------------------------------------------------------------------------------- RawChainId +#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)] +#[repr(transparent)] +pub struct RawChainId(u64); + +impl From for RawChainId { + fn from(value: ChainId) -> Self { + RawChainId(value.0.get()) + } +} + +impl From for ChainId { + fn from(value: RawChainId) -> Self { + ChainId(NonZero::new(value.0).expect("RawChainId mut not have a value of `0`")) + } +} + +impl Key for RawChainId {} + +//---------------------------------------------------------------------------------------------------- AltChainInfo +#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)] +#[repr(C)] +pub struct AltChainInfo { + parent_chain: RawChain, + common_ancestor_height: u64 +} + +//---------------------------------------------------------------------------------------------------- AltBlockHeight +#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)] +#[repr(C)] +pub struct AltBlockHeight { + chain_id: u64, + height: u64, +} + +impl Key for AltBlockHeight {} + +//---------------------------------------------------------------------------------------------------- CompactAltBlockInfo +#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)] +#[repr(C)] +pub struct CompactAltBlockInfo { + /// The block's hash. + /// + /// [`Block::hash`]. + pub block_hash: [u8; 32], + /// The block's proof-of-work hash. + pub pow_hash: [u8; 32], + /// The block's height. + pub height: u64, + /// The adjusted block size, in bytes. + pub weight: usize, + /// The long term block weight, which is the weight factored in with previous block weights. + pub long_term_weight: usize, + /// The cumulative difficulty of all blocks up until and including this block. + pub cumulative_difficulty_low: u64, + pub cumulative_difficulty_high: u64, + +} + +//---------------------------------------------------------------------------------------------------- AltTransactionInfo +#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)] +#[repr(C)] +pub struct AltTransactionInfo { + /// The transaction's weight. + /// + /// [`Transaction::weight`]. + pub tx_weight: usize, + /// The transaction's total fees. + pub fee: u64, + /// The transaction's hash. + /// + /// [`Transaction::hash`]. + pub tx_hash: [u8; 32], +} + //---------------------------------------------------------------------------------------------------- Tests #[cfg(test)] mod test { diff --git a/types/src/types.rs b/types/src/types.rs index 4b6e2e12..da4422a1 100644 --- a/types/src/types.rs +++ b/types/src/types.rs @@ -1,5 +1,6 @@ //! Various shared data types in Cuprate. +use std::num::NonZero; //---------------------------------------------------------------------------------------------------- Import use curve25519_dalek::edwards::EdwardsPoint; use monero_serai::{ @@ -97,7 +98,7 @@ pub struct VerifiedBlockInformation { /// /// The inner value is meaningless. #[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)] -pub struct ChainId(pub u64); +pub struct ChainId(pub NonZero); //---------------------------------------------------------------------------------------------------- Chain /// An identifier for a chain.