diff --git a/database/src/backend/heed/env.rs b/database/src/backend/heed/env.rs index 402cc678..ce7980cb 100644 --- a/database/src/backend/heed/env.rs +++ b/database/src/backend/heed/env.rs @@ -171,20 +171,14 @@ impl Env for ConcreteEnv { // // `heed` creates the database if it didn't exist. // - use crate::tables::{TestTable, TestTable2}; - let mut tx_rw = env.write_txn()?; - - // FIXME: - // These wonderful fully qualified trait types are brought - // to you by `tower::discover::Discover>::Key` collisions. - - // TODO: Create all tables when schema is done. /// Function that creates the tables based off the passed `T: Table`. fn create_table( env: &heed::Env, tx_rw: &mut heed::RwTxn<'_>, ) -> Result<(), InitError> { + println!("create_table(): {}", T::NAME); // TODO: use tracing. + DatabaseOpenOptions::new(env) .name(::NAME) .types::::Key>, StorableHeed<::Value>>() @@ -192,8 +186,28 @@ impl Env for ConcreteEnv { Ok(()) } - create_table::(&env, &mut tx_rw)?; - create_table::(&env, &mut tx_rw)?; + use crate::tables::{ + BlockBlobs, BlockHeights, BlockInfoV1s, BlockInfoV2s, BlockInfoV3s, KeyImages, + NumOutputs, Outputs, PrunableHashes, PrunableTxBlobs, PrunedTxBlobs, RctOutputs, + TxHeights, TxIds, TxUnlockTime, + }; + + let mut tx_rw = env.write_txn()?; + create_table::(&env, &mut tx_rw)?; + create_table::(&env, &mut tx_rw)?; + create_table::(&env, &mut tx_rw)?; + create_table::(&env, &mut tx_rw)?; + create_table::(&env, &mut tx_rw)?; + create_table::(&env, &mut tx_rw)?; + create_table::(&env, &mut tx_rw)?; + create_table::(&env, &mut tx_rw)?; + create_table::(&env, &mut tx_rw)?; + create_table::(&env, &mut tx_rw)?; + create_table::(&env, &mut tx_rw)?; + create_table::(&env, &mut tx_rw)?; + create_table::(&env, &mut tx_rw)?; + create_table::(&env, &mut tx_rw)?; + create_table::(&env, &mut tx_rw)?; // TODO: Set dupsort and comparison functions for certain tables // diff --git a/database/src/backend/redb/env.rs b/database/src/backend/redb/env.rs index 11484981..39faeb53 100644 --- a/database/src/backend/redb/env.rs +++ b/database/src/backend/redb/env.rs @@ -83,17 +83,11 @@ impl Env for ConcreteEnv { // Create all database tables. // `redb` creates tables if they don't exist. // - use crate::tables::{TestTable, TestTable2}; - let tx_rw = env.begin_write()?; - - // FIXME: - // These wonderful fully qualified trait types are brought - // to you by `tower::discover::Discover>::Key` collisions. - - // TODO: Create all tables when schema is done. /// Function that creates the tables based off the passed `T: Table`. fn create_table(tx_rw: &redb::WriteTransaction<'_>) -> Result<(), InitError> { + println!("create_table(): {}", T::NAME); // TODO: use tracing. + let table: redb::TableDefinition< 'static, StorableRedb<::Key>, @@ -105,8 +99,28 @@ impl Env for ConcreteEnv { Ok(()) } - create_table::(&tx_rw)?; - create_table::(&tx_rw)?; + use crate::tables::{ + BlockBlobs, BlockHeights, BlockInfoV1s, BlockInfoV2s, BlockInfoV3s, KeyImages, + NumOutputs, Outputs, PrunableHashes, PrunableTxBlobs, PrunedTxBlobs, RctOutputs, + TxHeights, TxIds, TxUnlockTime, + }; + + let tx_rw = env.begin_write()?; + create_table::(&tx_rw)?; + create_table::(&tx_rw)?; + create_table::(&tx_rw)?; + create_table::(&tx_rw)?; + create_table::(&tx_rw)?; + create_table::(&tx_rw)?; + create_table::(&tx_rw)?; + create_table::(&tx_rw)?; + create_table::(&tx_rw)?; + create_table::(&tx_rw)?; + create_table::(&tx_rw)?; + create_table::(&tx_rw)?; + create_table::(&tx_rw)?; + create_table::(&tx_rw)?; + create_table::(&tx_rw)?; tx_rw.commit()?; // Check for file integrity. diff --git a/database/src/backend/redb/storable.rs b/database/src/backend/redb/storable.rs index 89b4bcac..077db25e 100644 --- a/database/src/backend/redb/storable.rs +++ b/database/src/backend/redb/storable.rs @@ -5,7 +5,7 @@ use std::{any::Any, borrow::Cow, cmp::Ordering, fmt::Debug, marker::PhantomData} use redb::{RedbKey, RedbValue, TypeName}; -use crate::{key::Key, storable::Storable}; +use crate::{key::Key, storable::Storable, value_guard::ValueGuard}; //---------------------------------------------------------------------------------------------------- StorableRedb /// The glue structs that implements `redb`'s (de)serialization @@ -17,14 +17,14 @@ pub(super) struct StorableRedb(PhantomData) where T: Storable + ?Sized; -impl crate::value_guard::ValueGuard for redb::AccessGuard<'_, StorableRedb> { +impl ValueGuard for redb::AccessGuard<'_, StorableRedb> { #[inline] fn unguard(&self) -> Cow<'_, T> { self.value() } } -impl crate::value_guard::ValueGuard for &redb::AccessGuard<'_, StorableRedb> { +impl ValueGuard for &redb::AccessGuard<'_, StorableRedb> { #[inline] fn unguard(&self) -> Cow<'_, T> { self.value() @@ -35,7 +35,7 @@ impl crate::value_guard::ValueGuard for &redb::AccessGuard<'_, S // If `Key` is also implemented, this can act as a `RedbKey`. impl RedbKey for StorableRedb where - T: Key, + T: Key + ?Sized, { #[inline] fn compare(left: &[u8], right: &[u8]) -> Ordering { diff --git a/database/src/backend/tests.rs b/database/src/backend/tests.rs index 1875c566..83ddaf98 100644 --- a/database/src/backend/tests.rs +++ b/database/src/backend/tests.rs @@ -13,6 +13,8 @@ //! //! `redb`, and it only must be enabled for it to be tested. +#![allow(clippy::items_after_statements, clippy::significant_drop_tightening)] + //---------------------------------------------------------------------------------------------------- Import use std::borrow::{Borrow, Cow}; @@ -23,9 +25,17 @@ use crate::{ error::{InitError, RuntimeError}, resize::ResizeAlgorithm, table::Table, - tables::{TestTable, TestTable2}, + tables::{ + BlockBlobs, BlockHeights, BlockInfoV1s, BlockInfoV2s, BlockInfoV3s, KeyImages, NumOutputs, + Outputs, PrunableHashes, PrunableTxBlobs, PrunedTxBlobs, RctOutputs, TxHeights, TxIds, + TxUnlockTime, + }, transaction::{TxRo, TxRw}, - types::TestType, + types::{ + Amount, AmountIndex, AmountIndices, BlockBlob, BlockHash, BlockHeight, BlockInfoV1, + BlockInfoV2, BlockInfoV3, KeyImage, Output, PreRctOutputId, PrunableBlob, PrunableHash, + PrunedBlob, RctOutput, TxHash, TxId, UnlockTime, + }, value_guard::ValueGuard, ConcreteEnv, }; @@ -63,7 +73,6 @@ fn tx() { /// Open (and verify) that all database tables /// exist already after calling [`Env::open`]. #[test] -#[allow(clippy::items_after_statements, clippy::significant_drop_tightening)] fn open_db() { let (env, _tempdir) = tmp_concrete_env(); let env_inner = env.env_inner(); @@ -72,13 +81,39 @@ fn open_db() { // Open all tables in read-only mode. // This should be updated when tables are modified. - env_inner.open_db_ro::(&tx_ro).unwrap(); - env_inner.open_db_ro::(&tx_ro).unwrap(); + env_inner.open_db_ro::(&tx_ro).unwrap(); + env_inner.open_db_ro::(&tx_ro).unwrap(); + env_inner.open_db_ro::(&tx_ro).unwrap(); + env_inner.open_db_ro::(&tx_ro).unwrap(); + env_inner.open_db_ro::(&tx_ro).unwrap(); + env_inner.open_db_ro::(&tx_ro).unwrap(); + env_inner.open_db_ro::(&tx_ro).unwrap(); + env_inner.open_db_ro::(&tx_ro).unwrap(); + env_inner.open_db_ro::(&tx_ro).unwrap(); + env_inner.open_db_ro::(&tx_ro).unwrap(); + env_inner.open_db_ro::(&tx_ro).unwrap(); + env_inner.open_db_ro::(&tx_ro).unwrap(); + env_inner.open_db_ro::(&tx_ro).unwrap(); + env_inner.open_db_ro::(&tx_ro).unwrap(); + env_inner.open_db_ro::(&tx_ro).unwrap(); TxRo::commit(tx_ro).unwrap(); // Open all tables in read/write mode. - env_inner.open_db_rw::(&mut tx_rw).unwrap(); - env_inner.open_db_rw::(&mut tx_rw).unwrap(); + env_inner.open_db_rw::(&mut tx_rw).unwrap(); + env_inner.open_db_rw::(&mut tx_rw).unwrap(); + env_inner.open_db_rw::(&mut tx_rw).unwrap(); + env_inner.open_db_rw::(&mut tx_rw).unwrap(); + env_inner.open_db_rw::(&mut tx_rw).unwrap(); + env_inner.open_db_rw::(&mut tx_rw).unwrap(); + env_inner.open_db_rw::(&mut tx_rw).unwrap(); + env_inner.open_db_rw::(&mut tx_rw).unwrap(); + env_inner.open_db_rw::(&mut tx_rw).unwrap(); + env_inner.open_db_rw::(&mut tx_rw).unwrap(); + env_inner.open_db_rw::(&mut tx_rw).unwrap(); + env_inner.open_db_rw::(&mut tx_rw).unwrap(); + env_inner.open_db_rw::(&mut tx_rw).unwrap(); + env_inner.open_db_rw::(&mut tx_rw).unwrap(); + env_inner.open_db_rw::(&mut tx_rw).unwrap(); TxRw::commit(tx_rw).unwrap(); } @@ -113,6 +148,7 @@ fn non_manual_resize_1() { env.resize_map(None); } } + #[test] #[should_panic = "unreachable"] fn non_manual_resize_2() { @@ -126,40 +162,48 @@ fn non_manual_resize_2() { /// Test all `DatabaseR{o,w}` operations. #[test] -#[allow( - clippy::items_after_statements, - clippy::significant_drop_tightening, - clippy::used_underscore_binding -)] fn db_read_write() { let (env, _tempdir) = tmp_concrete_env(); let env_inner = env.env_inner(); let mut tx_rw = env_inner.tx_rw().unwrap(); - let mut table = env_inner.open_db_rw::(&mut tx_rw).unwrap(); + let mut table = env_inner.open_db_rw::(&mut tx_rw).unwrap(); - const KEY: i64 = 0_i64; - const VALUE: TestType = TestType { - u: 1, - b: 255, - _pad: [0; 7], + /// The (1st) key. + const KEY: PreRctOutputId = PreRctOutputId { + amount: 1, + amount_index: 123, + }; + /// The expected value. + const VALUE: Output = Output { + key: [35; 32], + height: 45_761_798, + output_flags: 0, + tx_idx: 2_353_487, }; + /// Assert a passed `Output` is equal to the const value. + fn assert_eq(output: &Output) { + assert_eq!(output, &VALUE); + // Make sure all field accesses are aligned. + assert_eq!(output.key, VALUE.key); + assert_eq!(output.height, VALUE.height); + assert_eq!(output.output_flags, VALUE.output_flags); + assert_eq!(output.tx_idx, VALUE.tx_idx); + } + // Insert `0..100` keys. + let mut key = KEY; for i in 0..100 { - table.put(&(KEY + i), &VALUE).unwrap(); + table.put(&key, &VALUE).unwrap(); + key.amount += 1; } // Assert the 1st key is there. { let guard = table.get(&KEY).unwrap(); - let cow: Cow<'_, TestType> = guard.unguard(); - let value: &TestType = cow.as_ref(); - - // Make sure all field accesses are aligned. - assert_eq!(value, &VALUE); - assert_eq!(value.u, VALUE.u); - assert_eq!(value.b, VALUE.b); - assert_eq!(value._pad, VALUE._pad); + let cow: Cow<'_, Output> = guard.unguard(); + let value: &Output = cow.as_ref(); + assert_eq(value); } // Assert the whole range is there. @@ -168,21 +212,18 @@ fn db_read_write() { let mut i = 0; for result in range { let guard = result.unwrap(); - let cow: Cow<'_, TestType> = guard.unguard(); - let value: &TestType = cow.as_ref(); - - assert_eq!(value, &VALUE); - assert_eq!(value.u, VALUE.u); - assert_eq!(value.b, VALUE.b); - assert_eq!(value._pad, VALUE._pad); - + let cow: Cow<'_, Output> = guard.unguard(); + let value: &Output = cow.as_ref(); + assert_eq(value); i += 1; } assert_eq!(i, 100); } // Assert `get_range()` works. - let range = KEY..(KEY + 100); + let mut key = KEY; + key.amount += 100; + let range = KEY..key; assert_eq!(100, table.get_range(&range).unwrap().count()); // Assert deleting works. @@ -190,3 +231,169 @@ fn db_read_write() { let value = table.get(&KEY); assert!(matches!(value, Err(RuntimeError::KeyNotFound))); } + +//---------------------------------------------------------------------------------------------------- Table Tests +/// Test multiple tables and their key + values. +/// +/// Each one of these tests: +/// - Opens a specific table +/// - Inserts a key + value +/// - Retrieves the key + value +/// - Asserts it is the same +/// - Tests `get_range()` +/// - Tests `delete()` +macro_rules! test_tables { + ($( + $table:ident, // Table type + $key_type:ty => // Key (type) + $value_type:ty, // Value (type) + $key:expr => // Key (the value) + $value:expr, // Value (the value) + )* $(,)?) => { paste::paste! { $( + // Test function's name is the table type in `snake_case`. + #[test] + fn [<$table:snake>]() { + // Open the database env and table. + let (env, _tempdir) = tmp_concrete_env(); + let env_inner = env.env_inner(); + let mut tx_rw = env_inner.tx_rw().unwrap(); + let mut table = env_inner.open_db_rw::<$table>(&mut tx_rw).unwrap(); + + /// The expected key. + const KEY: $key_type = $key; + /// The expected value. + const VALUE: &$value_type = &$value; + + /// Assert a passed value is equal to the const value. + fn assert_eq(value: &$value_type) { + assert_eq!(value, VALUE); + } + + // Insert the key. + table.put(&KEY, VALUE).unwrap(); + // Assert key is there. + { + let guard = table.get(&KEY).unwrap(); + let cow: Cow<'_, $value_type> = guard.unguard(); + let value: &$value_type = cow.as_ref(); + assert_eq(value); + } + + // Assert `get_range()` works. + { + let range = KEY..; + assert_eq!(1, table.get_range(&range).unwrap().count()); + let mut iter = table.get_range(&range).unwrap(); + let guard = iter.next().unwrap().unwrap(); + let cow = guard.unguard(); + let value = cow.as_ref(); + assert_eq(value); + } + + // Assert deleting works. + table.delete(&KEY).unwrap(); + let value = table.get(&KEY); + assert!(matches!(value, Err(RuntimeError::KeyNotFound))); + } + )*}}; +} + +// Notes: +// - Keep this sorted A-Z (by table name) +test_tables! { + BlockBlobs, // Table type + BlockHeight => BlockBlob, // Key type => Value type + 123 => [1,2,3,4,5,6,7,8].as_slice(), // Actual key => Actual value + + BlockHeights, + BlockHash => BlockHeight, + [32; 32] => 123, + + BlockInfoV1s, + BlockHeight => BlockInfoV1, + 123 => BlockInfoV1 { + timestamp: 1, + total_generated_coins: 123, + weight: 321, + cumulative_difficulty: 111, + block_hash: [54; 32], + }, + + BlockInfoV2s, + BlockHeight => BlockInfoV2, + 123 => BlockInfoV2 { + timestamp: 1, + total_generated_coins: 123, + weight: 321, + cumulative_difficulty: 111, + cumulative_rct_outs: 2389, + block_hash: [54; 32], + }, + + BlockInfoV3s, + BlockHeight => BlockInfoV3, + 123 => BlockInfoV3 { + timestamp: 1, + total_generated_coins: 123, + weight: 321, + cumulative_difficulty_low: 111, + cumulative_difficulty_high: 112, + block_hash: [54; 32], + cumulative_rct_outs: 2389, + long_term_weight: 2389, + }, + + KeyImages, + KeyImage => (), + [32; 32] => (), + + NumOutputs, + Amount => AmountIndex, + 123 => 123, + + TxIds, + TxHash => TxId, + [32; 32] => 123, + + TxHeights, + TxId => BlockHeight, + 123 => 123, + + TxUnlockTime, + TxId => UnlockTime, + 123 => 123, + + Outputs, + PreRctOutputId => Output, + PreRctOutputId { + amount: 1, + amount_index: 2, + } => Output { + key: [1; 32], + height: 1, + output_flags: 0, + tx_idx: 3, + }, + + PrunedTxBlobs, + TxId => PrunedBlob, + 123 => [1,2,3,4,5,6,7,8].as_slice(), + + PrunableTxBlobs, + TxId => PrunableBlob, + 123 => [1,2,3,4,5,6,7,8].as_slice(), + + PrunableHashes, + TxId => PrunableHash, + 123 => [32; 32], + + RctOutputs, + AmountIndex => RctOutput, + 123 => RctOutput { + key: [1; 32], + height: 1, + output_flags: 0, + tx_idx: 3, + commitment: [3; 32], + }, +} diff --git a/database/src/key.rs b/database/src/key.rs index 1e87d711..2e4d616a 100644 --- a/database/src/key.rs +++ b/database/src/key.rs @@ -115,6 +115,13 @@ impl Key for [T; N] { type Primary = Self; } +// TODO: temporary for now for `Key` bound, remove later. +impl Key for crate::types::PreRctOutputId { + const DUPLICATE: bool = false; + const CUSTOM_COMPARE: bool = false; + type Primary = Self; +} + //---------------------------------------------------------------------------------------------------- Tests #[cfg(test)] mod test { diff --git a/database/src/table.rs b/database/src/table.rs index b94aea3c..723d8839 100644 --- a/database/src/table.rs +++ b/database/src/table.rs @@ -19,10 +19,10 @@ pub trait Table: crate::tables::private::Sealed + 'static { const NAME: &'static str; /// Primary key type. - type Key: Key + 'static; + type Key: Key + ?Sized + 'static; /// Value type. - type Value: Storable + 'static; + type Value: Storable + ?Sized + 'static; } //---------------------------------------------------------------------------------------------------- Tests diff --git a/database/src/tables.rs b/database/src/tables.rs index 0d61d7bb..7944b3ce 100644 --- a/database/src/tables.rs +++ b/database/src/tables.rs @@ -5,7 +5,11 @@ //---------------------------------------------------------------------------------------------------- Import use crate::{ table::Table, - types::{TestType, TestType2}, + types::{ + Amount, AmountIndex, AmountIndices, BlockBlob, BlockHash, BlockHeight, BlockInfoV1, + BlockInfoV2, BlockInfoV3, KeyImage, Output, PreRctOutputId, PrunableBlob, PrunableHash, + PrunedBlob, RctOutput, TxHash, TxId, UnlockTime, + }, }; //---------------------------------------------------------------------------------------------------- Tables @@ -35,7 +39,6 @@ macro_rules! tables { $( $(#[$attr:meta])* // Documentation and any `derive`'s. $table:ident, // The table name + doubles as the table struct name. - $size:literal, // Are the table's values all the same size? $key:ty => // Key type. $value:ty // Value type. ),* $(,)? @@ -56,7 +59,7 @@ macro_rules! tables { )] /// ``` #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] - #[derive(Copy,Clone,Debug,PartialEq,PartialOrd,Eq,Ord,Hash)] + #[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash)] pub struct [<$table:camel>]; // Implement the `Sealed` in this file. @@ -74,16 +77,71 @@ macro_rules! tables { } //---------------------------------------------------------------------------------------------------- Tables +// Notes: +// - Keep this sorted A-Z (by table name) +// - Tables are defined in plural to avoid name conflicts with types +// - If adding/changing a table, also edit the tests in `src/backend/tests.rs` +// and edit `Env::open` to make sure it creates the table tables! { - /// Test documentation. - TestTable, - true, - i64 => TestType, + /// TODO + BlockBlobs, + BlockHeight => BlockBlob, - /// Test documentation 2. - TestTable2, - true, - u8 => TestType2, + /// TODO + BlockHeights, + BlockHash => BlockHeight, + + /// TODO + BlockInfoV1s, + BlockHeight => BlockInfoV1, + + /// TODO + BlockInfoV2s, + BlockHeight => BlockInfoV2, + + /// TODO + BlockInfoV3s, + BlockHeight => BlockInfoV3, + + /// TODO + KeyImages, + KeyImage => (), + + /// TODO + NumOutputs, + Amount => AmountIndex, + + /// TODO + PrunedTxBlobs, + TxId => PrunedBlob, + + /// TODO + Outputs, + PreRctOutputId => Output, + + /// TODO + PrunableTxBlobs, + TxId => PrunableBlob, + + /// TODO + PrunableHashes, + TxId => PrunableHash, + + /// TODO + RctOutputs, + AmountIndex => RctOutput, + + /// TODO + TxIds, + TxHash => TxId, + + /// TODO + TxHeights, + TxId => BlockHeight, + + /// TODO + TxUnlockTime, + TxId => UnlockTime, } //---------------------------------------------------------------------------------------------------- Tests diff --git a/database/src/types.rs b/database/src/types.rs index 865b6e95..89ffba2a 100644 --- a/database/src/types.rs +++ b/database/src/types.rs @@ -3,10 +3,6 @@ //! This module contains all types used by the database tables. //! //! TODO: Add schema here or a link to it. -//! -//! ## `*Bits` -//! The non-documented items ending in `Bits` can be ignored, -//! they are helper structs generated by `bytemuck`. /* * <============================================> VERY BIG SCARY SAFETY MESSAGE <============================================> @@ -49,97 +45,320 @@ use bytemuck::{AnyBitPattern, NoUninit, Pod, Zeroable}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -//---------------------------------------------------------------------------------------------------- TestType -/// TEST +//---------------------------------------------------------------------------------------------------- Aliases +// TODO: document these, why they exist, and their purpose. +// +// Notes: +// - Keep this sorted A-Z + +/// TODO +pub type Amount = u64; + +/// TODO +pub type AmountIndex = u64; + +/// TODO +pub type AmountIndices = [AmountIndex]; + +/// TODO +pub type BlockBlob = [u8]; + +/// TODO +pub type BlockHash = [u8; 32]; + +/// TODO +pub type BlockHeight = u64; + +/// TODO +pub type KeyImage = [u8; 32]; + +/// TODO +pub type PrunedBlob = [u8]; + +/// TODO +pub type PrunableBlob = [u8]; + +/// TODO +pub type PrunableHash = [u8; 32]; + +/// TODO +pub type TxId = u64; + +/// TODO +pub type TxHash = [u8; 32]; + +/// TODO +pub type UnlockTime = u64; + +//---------------------------------------------------------------------------------------------------- BlockInfoV1 +/// TODO /// /// ```rust +/// # use std::borrow::*; /// # use cuprate_database::{*, types::*}; -/// // Assert bytemuck is correct. -/// let a = TestType { u: 1, b: 255, _pad: [0; 7] }; // original struct -/// let b = bytemuck::must_cast::(a); // cast into bytes -/// let c = bytemuck::checked::cast::<[u8; 16], TestType>(b); // cast back into struct -/// assert_eq!(a, c); -/// assert_eq!(c.u, 1); -/// assert_eq!(c.b, 255); -/// assert_eq!(c._pad, [0; 7]); -/// /// // Assert Storable is correct. -/// let b2 = Storable::as_bytes(&a); -/// let c2: &TestType = Storable::from_bytes(b2); -/// assert_eq!(a, *c2); -/// assert_eq!(b, b2); -/// assert_eq!(c, *c2); -/// assert_eq!(c2.u, 1); -/// assert_eq!(c2.b, 255); -/// assert_eq!(c2._pad, [0; 7]); +/// let a = PreRctOutputId { +/// amount: 1, +/// amount_index: 123, +/// }; +/// let b = Storable::as_bytes(&a); +/// let c: &PreRctOutputId = Storable::from_bytes(b); +/// let c2: Cow<'_, PreRctOutputId> = Storable::from_bytes_unaligned(b); +/// assert_eq!(&a, c); +/// assert_eq!(c, c2.as_ref()); /// ``` /// /// # Size & Alignment /// ```rust /// # use cuprate_database::types::*; /// # use std::mem::*; -/// assert_eq!(size_of::(), 16); -/// assert_eq!(align_of::(), 8); +/// assert_eq!(size_of::(), 16); +/// assert_eq!(align_of::(), 8); /// ``` #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)] #[repr(C)] -pub struct TestType { - /// TEST - pub u: usize, - /// TEST - pub b: u8, - /// TEST - /// - /// TODO: is there a cheaper way (CPU instruction wise) - /// to add padding to structs over 0 filled arrays? - /// - /// TODO: this is basically leeway to - /// add more things to our structs too, - /// because otherwise this space is wasted. - pub _pad: [u8; 7], +pub struct PreRctOutputId { + /// TODO + pub amount: Amount, + /// TODO + pub amount_index: AmountIndex, } -//---------------------------------------------------------------------------------------------------- TestType2 -/// TEST2 +//---------------------------------------------------------------------------------------------------- BlockInfoV1 +/// TODO /// /// ```rust +/// # use std::borrow::*; /// # use cuprate_database::{*, types::*}; -/// // Assert bytemuck is correct. -/// let a = TestType2 { u: 1, b: [1; 32] }; // original struct -/// let b = bytemuck::must_cast::(a); // cast into bytes -/// let c = bytemuck::must_cast::<[u8; 40], TestType2>(b); // cast back into struct -/// assert_eq!(a, c); -/// assert_eq!(c.u, 1); -/// assert_eq!(c.b, [1; 32]); -/// /// // Assert Storable is correct. -/// let b2 = Storable::as_bytes(&a); -/// let c2: &TestType2 = Storable::from_bytes(b2); -/// assert_eq!(a, *c2); -/// assert_eq!(b, b2); -/// assert_eq!(c, *c2); -/// assert_eq!(c.u, 1); -/// assert_eq!(c.b, [1; 32]); +/// let a = BlockInfoV1 { +/// timestamp: 1, +/// total_generated_coins: 123, +/// weight: 321, +/// cumulative_difficulty: 111, +/// block_hash: [54; 32], +/// }; +/// let b = Storable::as_bytes(&a); +/// let c: &BlockInfoV1 = Storable::from_bytes(b); +/// let c2: Cow<'_, BlockInfoV1> = Storable::from_bytes_unaligned(b); +/// assert_eq!(&a, c); +/// assert_eq!(c, c2.as_ref()); /// ``` /// /// # Size & Alignment /// ```rust /// # use cuprate_database::types::*; /// # use std::mem::*; -/// assert_eq!(size_of::(), 40); -/// assert_eq!(align_of::(), 8); +/// assert_eq!(size_of::(), 64); +/// assert_eq!(align_of::(), 8); /// ``` #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)] #[repr(C)] -pub struct TestType2 { - /// TEST - pub u: usize, - /// TEST - pub b: [u8; 32], +pub struct BlockInfoV1 { + /// TODO + pub timestamp: u64, + /// TODO + pub total_generated_coins: u64, + /// TODO + pub weight: u64, + /// TODO + pub cumulative_difficulty: u64, + /// TODO + pub block_hash: [u8; 32], } +//---------------------------------------------------------------------------------------------------- BlockInfoV2 +/// TODO +/// +/// ```rust +/// # use std::borrow::*; +/// # use cuprate_database::{*, types::*}; +/// // Assert Storable is correct. +/// let a = BlockInfoV2 { +/// timestamp: 1, +/// total_generated_coins: 123, +/// weight: 321, +/// block_hash: [54; 32], +/// cumulative_difficulty: 111, +/// cumulative_rct_outs: 2389, +/// }; +/// let b = Storable::as_bytes(&a); +/// let c: &BlockInfoV2 = Storable::from_bytes(b); +/// let c2: Cow<'_, BlockInfoV2> = Storable::from_bytes_unaligned(b); +/// assert_eq!(&a, c); +/// assert_eq!(c, c2.as_ref()); +/// ``` +/// +/// # Size & Alignment +/// ```rust +/// # use cuprate_database::types::*; +/// # use std::mem::*; +/// assert_eq!(size_of::(), 72); +/// assert_eq!(align_of::(), 8); +/// ``` +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)] +#[repr(C)] +pub struct BlockInfoV2 { + /// TODO + pub timestamp: u64, + /// TODO + pub total_generated_coins: u64, + /// TODO + pub weight: u64, + /// TODO + pub block_hash: [u8; 32], + /// TODO + pub cumulative_difficulty: u64, + /// TODO + /// + /// TODO: note that this is originally u32, + /// but is u64 here for padding reasons. + pub cumulative_rct_outs: u64, +} + +//---------------------------------------------------------------------------------------------------- BlockInfoV3 +/// TODO +/// +/// ```rust +/// # use std::borrow::*; +/// # use cuprate_database::{*, types::*}; +/// // Assert Storable is correct. +/// let a = BlockInfoV3 { +/// timestamp: 1, +/// total_generated_coins: 123, +/// weight: 321, +/// cumulative_difficulty_low: 111, +/// cumulative_difficulty_high: 112, +/// block_hash: [54; 32], +/// cumulative_rct_outs: 2389, +/// long_term_weight: 2389, +/// }; +/// let b = Storable::as_bytes(&a); +/// let c: &BlockInfoV3 = Storable::from_bytes(b); +/// let c2: Cow<'_, BlockInfoV3> = Storable::from_bytes_unaligned(b); +/// assert_eq!(&a, c); +/// assert_eq!(c, c2.as_ref()); +/// ``` +/// +/// # Size & Alignment +/// ```rust +/// # use cuprate_database::types::*; +/// # use std::mem::*; +/// assert_eq!(size_of::(), 88); +/// assert_eq!(align_of::(), 8); +/// ``` +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)] +#[repr(C)] +pub struct BlockInfoV3 { + /// TODO + pub timestamp: u64, + /// TODO + pub total_generated_coins: u64, + /// TODO + pub weight: u64, + // Maintain 8 byte alignment. + /// TODO + pub cumulative_difficulty_low: u64, + /// TODO + pub cumulative_difficulty_high: u64, + /// TODO + pub block_hash: [u8; 32], + /// TODO + pub cumulative_rct_outs: u64, + /// TODO + pub long_term_weight: u64, +} + +//---------------------------------------------------------------------------------------------------- Output +/// TODO +/// +/// ```rust +/// # use std::borrow::*; +/// # use cuprate_database::{*, types::*}; +/// // Assert Storable is correct. +/// let a = Output { +/// key: [1; 32], +/// height: 1, +/// output_flags: 0, +/// tx_idx: 3, +/// }; +/// let b = Storable::as_bytes(&a); +/// let c: &Output = Storable::from_bytes(b); +/// let c2: Cow<'_, Output> = Storable::from_bytes_unaligned(b); +/// assert_eq!(&a, c); +/// assert_eq!(c, c2.as_ref()); +/// ``` +/// +/// # Size & Alignment +/// ```rust +/// # use cuprate_database::types::*; +/// # use std::mem::*; +/// assert_eq!(size_of::(), 48); +/// assert_eq!(align_of::(), 8); +/// ``` +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)] +#[repr(C)] +pub struct Output { + /// TODO + pub key: [u8; 32], + /// We could get this from the tx_idx with the Tx Heights table but that would require another look up per out. + pub height: u32, + /// Bit flags for this output, currently only the first bit is used and, if set, it means this output has a non-zero unlock time. + pub output_flags: u32, + /// TODO + pub tx_idx: u64, +} + +//---------------------------------------------------------------------------------------------------- RctOutput +/// TODO +/// +/// ```rust +/// # use std::borrow::*; +/// # use cuprate_database::{*, types::*}; +/// // Assert Storable is correct. +/// let a = RctOutput { +/// key: [1; 32], +/// height: 1, +/// output_flags: 0, +/// tx_idx: 3, +/// commitment: [3; 32], +/// }; +/// let b = Storable::as_bytes(&a); +/// let c: &RctOutput = Storable::from_bytes(b); +/// let c2: Cow<'_, RctOutput> = Storable::from_bytes_unaligned(b); +/// assert_eq!(&a, c); +/// assert_eq!(c, c2.as_ref()); +/// ``` +/// +/// # Size & Alignment +/// ```rust +/// # use cuprate_database::types::*; +/// # use std::mem::*; +/// assert_eq!(size_of::(), 80); +/// assert_eq!(align_of::(), 8); +/// ``` +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)] +#[repr(C)] +pub struct RctOutput { + /// TODO + pub key: [u8; 32], + /// We could get this from the tx_idx with the Tx Heights table but that would require another look up per out. + pub height: u32, + /// Bit flags for this output, currently only the first bit is used and, if set, it means this output has a non-zero unlock time. + pub output_flags: u32, + /// TODO + pub tx_idx: u64, + /// The amount commitment of this output. + pub commitment: [u8; 32], +} +// TODO: local_index? + //---------------------------------------------------------------------------------------------------- Tests #[cfg(test)] mod test { diff --git a/database/src/value_guard.rs b/database/src/value_guard.rs index 48e5ec4c..8dda43bd 100644 --- a/database/src/value_guard.rs +++ b/database/src/value_guard.rs @@ -24,12 +24,12 @@ use crate::{table::Table, Storable, ToOwnedDebug}; /// - `redb` will always be `Cow::Borrowed` for `[u8]` /// or any type where `Storable::ALIGN == 1` /// - `redb` will always be `Cow::Owned` for everything else -pub trait ValueGuard { +pub trait ValueGuard { /// Retrieve the data from the guard. fn unguard(&self) -> Cow<'_, T>; } -impl ValueGuard for Cow<'_, T> { +impl ValueGuard for Cow<'_, T> { #[inline] fn unguard(&self) -> Cow<'_, T> { Cow::Borrowed(self.borrow())