From c485b951f339efe0af05353f217948932b261c8f Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Wed, 24 Apr 2024 16:17:28 -0400 Subject: [PATCH] ops: replace all table len asserts with `AssertTableLen` --- database/src/ops/block.rs | 33 +++++++++-------- database/src/ops/blockchain.rs | 20 ++++++++++- database/src/ops/key_image.rs | 24 ++++--------- database/src/ops/output.rs | 33 +++++++++-------- database/src/ops/tx.rs | 33 +++++++++-------- database/src/tests.rs | 66 +++++++++++++++++----------------- 6 files changed, 113 insertions(+), 96 deletions(-) diff --git a/database/src/ops/block.rs b/database/src/ops/block.rs index bd3763e8..d70939a6 100644 --- a/database/src/ops/block.rs +++ b/database/src/ops/block.rs @@ -262,7 +262,7 @@ mod test { use super::*; use crate::{ ops::tx::{get_tx, tx_exists}, - tests::{assert_all_tables_are_empty, tmp_concrete_env}, + tests::{assert_all_tables_are_empty, tmp_concrete_env, AssertTableLen}, Env, }; @@ -315,20 +315,23 @@ mod test { let tables = env_inner.open_tables(&tx_ro).unwrap(); // Assert only the proper tables were added to. - assert_eq!(tables.block_infos().len().unwrap(), 3); - assert_eq!(tables.block_blobs().len().unwrap(), 3); - assert_eq!(tables.block_heights().len().unwrap(), 3); - assert_eq!(tables.key_images().len().unwrap(), 69); - assert_eq!(tables.num_outputs().len().unwrap(), 38); - assert_eq!(tables.pruned_tx_blobs().len().unwrap(), 0); - assert_eq!(tables.prunable_hashes().len().unwrap(), 0); - assert_eq!(tables.outputs().len().unwrap(), 107); - assert_eq!(tables.prunable_tx_blobs().len().unwrap(), 0); - assert_eq!(tables.rct_outputs().len().unwrap(), 6); - assert_eq!(tables.tx_blobs().len().unwrap(), 5); - assert_eq!(tables.tx_ids().len().unwrap(), 5); - assert_eq!(tables.tx_heights().len().unwrap(), 5); - assert_eq!(tables.tx_unlock_time().len().unwrap(), 0); + AssertTableLen { + block_infos: 3, + block_blobs: 3, + block_heights: 3, + key_images: 69, + num_outputs: 38, + pruned_tx_blobs: 0, + prunable_hashes: 0, + outputs: 107, + prunable_tx_blobs: 0, + rct_outputs: 6, + tx_blobs: 5, + tx_ids: 5, + tx_heights: 5, + tx_unlock_time: 0, + } + .assert(&tables); // Check `cumulative` functions work. assert_eq!( diff --git a/database/src/ops/blockchain.rs b/database/src/ops/blockchain.rs index b94cea05..c3104ad1 100644 --- a/database/src/ops/blockchain.rs +++ b/database/src/ops/blockchain.rs @@ -101,7 +101,7 @@ mod test { block::add_block, tx::{get_tx, tx_exists}, }, - tests::{assert_all_tables_are_empty, tmp_concrete_env}, + tests::{assert_all_tables_are_empty, tmp_concrete_env, AssertTableLen}, Env, }; @@ -149,6 +149,24 @@ mod test { } // Assert reads are correct. + AssertTableLen { + block_infos: 3, + block_blobs: 3, + block_heights: 3, + key_images: 69, + num_outputs: 38, + pruned_tx_blobs: 0, + prunable_hashes: 0, + outputs: 107, + prunable_tx_blobs: 0, + rct_outputs: 6, + tx_blobs: 5, + tx_ids: 5, + tx_heights: 5, + tx_unlock_time: 0, + } + .assert(&tables); + assert_eq!(blocks_len, chain_height(tables.block_heights()).unwrap()); assert_eq!( blocks_len - 1, diff --git a/database/src/ops/key_image.rs b/database/src/ops/key_image.rs index 1fa00ea4..0938447f 100644 --- a/database/src/ops/key_image.rs +++ b/database/src/ops/key_image.rs @@ -64,7 +64,7 @@ mod test { use super::*; use crate::{ ops::tx::{get_tx, tx_exists}, - tests::{assert_all_tables_are_empty, tmp_concrete_env}, + tests::{assert_all_tables_are_empty, tmp_concrete_env, AssertTableLen}, Env, }; @@ -108,23 +108,11 @@ mod test { let tables = env_inner.open_tables(&tx_ro).unwrap(); // Assert only the proper tables were added to. - assert_eq!( - tables.key_images().len().unwrap(), - u64::try_from(key_images.len()).unwrap() - ); - assert_eq!(tables.block_infos().len().unwrap(), 0); - assert_eq!(tables.block_blobs().len().unwrap(), 0); - assert_eq!(tables.block_heights().len().unwrap(), 0); - assert_eq!(tables.num_outputs().len().unwrap(), 0); - assert_eq!(tables.pruned_tx_blobs().len().unwrap(), 0); - assert_eq!(tables.prunable_hashes().len().unwrap(), 0); - assert_eq!(tables.outputs().len().unwrap(), 0); - assert_eq!(tables.prunable_tx_blobs().len().unwrap(), 0); - assert_eq!(tables.rct_outputs().len().unwrap(), 0); - assert_eq!(tables.tx_blobs().len().unwrap(), 0); - assert_eq!(tables.tx_ids().len().unwrap(), 0); - assert_eq!(tables.tx_heights().len().unwrap(), 0); - assert_eq!(tables.tx_unlock_time().len().unwrap(), 0); + AssertTableLen { + key_images: tables.key_images().len().unwrap(), + ..Default::default() + } + .assert(&tables); for key_image in &key_images { println!("key_image_exists(): {}", hex::encode(key_image)); diff --git a/database/src/ops/output.rs b/database/src/ops/output.rs index b2c615f7..db7303cf 100644 --- a/database/src/ops/output.rs +++ b/database/src/ops/output.rs @@ -159,7 +159,7 @@ pub fn get_rct_num_outputs( mod test { use super::*; use crate::{ - tests::{assert_all_tables_are_empty, tmp_concrete_env}, + tests::{assert_all_tables_are_empty, tmp_concrete_env, AssertTableLen}, types::OutputFlags, Env, }; @@ -221,20 +221,23 @@ mod test { // Assert all reads of the outputs are OK. { // Assert proper tables were added to. - assert_eq!(tables.block_infos().len().unwrap(), 0); - assert_eq!(tables.block_blobs().len().unwrap(), 0); - assert_eq!(tables.block_heights().len().unwrap(), 0); - assert_eq!(tables.key_images().len().unwrap(), 0); - assert_eq!(tables.num_outputs().len().unwrap(), 1); - assert_eq!(tables.pruned_tx_blobs().len().unwrap(), 0); - assert_eq!(tables.prunable_hashes().len().unwrap(), 0); - assert_eq!(tables.outputs().len().unwrap(), 1); - assert_eq!(tables.prunable_tx_blobs().len().unwrap(), 0); - assert_eq!(tables.rct_outputs().len().unwrap(), 1); - assert_eq!(tables.tx_blobs().len().unwrap(), 0); - assert_eq!(tables.tx_ids().len().unwrap(), 0); - assert_eq!(tables.tx_heights().len().unwrap(), 0); - assert_eq!(tables.tx_unlock_time().len().unwrap(), 0); + AssertTableLen { + block_infos: 0, + block_blobs: 0, + block_heights: 0, + key_images: 0, + num_outputs: 1, + pruned_tx_blobs: 0, + prunable_hashes: 0, + outputs: 1, + prunable_tx_blobs: 0, + rct_outputs: 1, + tx_blobs: 0, + tx_ids: 0, + tx_heights: 0, + tx_unlock_time: 0, + } + .assert(&tables); // Assert length is correct. assert_eq!(get_num_outputs(tables.outputs()).unwrap(), 1); diff --git a/database/src/ops/tx.rs b/database/src/ops/tx.rs index a66750ec..1166c0b5 100644 --- a/database/src/ops/tx.rs +++ b/database/src/ops/tx.rs @@ -343,7 +343,7 @@ pub fn tx_exists( mod test { use super::*; use crate::{ - tests::{assert_all_tables_are_empty, tmp_concrete_env}, + tests::{assert_all_tables_are_empty, tmp_concrete_env, AssertTableLen}, Env, }; use cuprate_test_utils::data::{tx_v1_sig0, tx_v1_sig2, tx_v2_rct3}; @@ -384,20 +384,23 @@ mod test { let tables = env_inner.open_tables(&tx_ro).unwrap(); // Assert only the proper tables were added to. - assert_eq!(tables.block_infos().len().unwrap(), 0); - assert_eq!(tables.block_blobs().len().unwrap(), 0); - assert_eq!(tables.block_heights().len().unwrap(), 0); - assert_eq!(tables.key_images().len().unwrap(), 4); // added to key images - assert_eq!(tables.pruned_tx_blobs().len().unwrap(), 0); - assert_eq!(tables.prunable_hashes().len().unwrap(), 0); - assert_eq!(tables.num_outputs().len().unwrap(), 9); - assert_eq!(tables.outputs().len().unwrap(), 10); // added to outputs - assert_eq!(tables.prunable_tx_blobs().len().unwrap(), 0); - assert_eq!(tables.rct_outputs().len().unwrap(), 2); - assert_eq!(tables.tx_blobs().len().unwrap(), 3); - assert_eq!(tables.tx_ids().len().unwrap(), 3); - assert_eq!(tables.tx_heights().len().unwrap(), 3); - assert_eq!(tables.tx_unlock_time().len().unwrap(), 1); // only 1 has a timelock + AssertTableLen { + block_infos: 0, + block_blobs: 0, + block_heights: 0, + key_images: 4, // added to key images + pruned_tx_blobs: 0, + prunable_hashes: 0, + num_outputs: 9, + outputs: 10, // added to outputs + prunable_tx_blobs: 0, + rct_outputs: 2, + tx_blobs: 3, + tx_ids: 3, + tx_heights: 3, + tx_unlock_time: 1, // only 1 has a timelock + } + .assert(&tables); // Both from ID and hash should result in getting the same transaction. let mut tx_hashes = vec![]; diff --git a/database/src/tests.rs b/database/src/tests.rs index 625fe139..dbebbe93 100644 --- a/database/src/tests.rs +++ b/database/src/tests.rs @@ -16,6 +16,7 @@ use monero_serai::{ ringct::{RctPrunable, RctSignatures}, transaction::{Timelock, Transaction, TransactionPrefix}, }; +use pretty_assertions::assert_eq; use crate::{ config::Config, key::Key, storable::Storable, tables::Tables, transaction::TxRo, ConcreteEnv, @@ -28,44 +29,45 @@ use crate::{ /// This is a struct with fields instead of a function /// so that callers can name arguments, otherwise the call-site /// is a little confusing, i.e. `assert_table_len(0, 25, 1, 123)`. +#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] pub(crate) struct AssertTableLen { - block_infos: u64, - block_blobs: u64, - block_heights: u64, - key_images: u64, - num_outputs: u64, - pruned_tx_blobs: u64, - prunable_hashes: u64, - outputs: u64, - prunable_tx_blobs: u64, - rct_outputs: u64, - tx_blobs: u64, - tx_ids: u64, - tx_heights: u64, - tx_unlock_time: u64, + pub(crate) block_infos: u64, + pub(crate) block_blobs: u64, + pub(crate) block_heights: u64, + pub(crate) key_images: u64, + pub(crate) num_outputs: u64, + pub(crate) pruned_tx_blobs: u64, + pub(crate) prunable_hashes: u64, + pub(crate) outputs: u64, + pub(crate) prunable_tx_blobs: u64, + pub(crate) rct_outputs: u64, + pub(crate) tx_blobs: u64, + pub(crate) tx_ids: u64, + pub(crate) tx_heights: u64, + pub(crate) tx_unlock_time: u64, } impl AssertTableLen { /// Assert the length of all tables. pub(crate) fn assert(self, tables: &impl Tables) { - for (table_len, self_len) in [ - (tables.block_infos().len(), self.block_infos), - (tables.block_blobs().len(), self.block_blobs), - (tables.block_heights().len(), self.block_heights), - (tables.key_images().len(), self.key_images), - (tables.num_outputs().len(), self.num_outputs), - (tables.pruned_tx_blobs().len(), self.pruned_tx_blobs), - (tables.prunable_hashes().len(), self.prunable_hashes), - (tables.outputs().len(), self.outputs), - (tables.prunable_tx_blobs().len(), self.prunable_tx_blobs), - (tables.rct_outputs().len(), self.rct_outputs), - (tables.tx_blobs().len(), self.tx_blobs), - (tables.tx_ids().len(), self.tx_ids), - (tables.tx_heights().len(), self.tx_heights), - (tables.tx_unlock_time().len(), self.tx_unlock_time), - ] { - assert_eq!(table_len.unwrap(), self_len); - } + let other = Self { + block_infos: tables.block_infos().len().unwrap(), + block_blobs: tables.block_blobs().len().unwrap(), + block_heights: tables.block_heights().len().unwrap(), + key_images: tables.key_images().len().unwrap(), + num_outputs: tables.num_outputs().len().unwrap(), + pruned_tx_blobs: tables.pruned_tx_blobs().len().unwrap(), + prunable_hashes: tables.prunable_hashes().len().unwrap(), + outputs: tables.outputs().len().unwrap(), + prunable_tx_blobs: tables.prunable_tx_blobs().len().unwrap(), + rct_outputs: tables.rct_outputs().len().unwrap(), + tx_blobs: tables.tx_blobs().len().unwrap(), + tx_ids: tables.tx_ids().len().unwrap(), + tx_heights: tables.tx_heights().len().unwrap(), + tx_unlock_time: tables.tx_unlock_time().len().unwrap(), + }; + + assert_eq!(self, other); } }