ops: replace all table len asserts with AssertTableLen

This commit is contained in:
hinto.janai 2024-04-24 16:17:28 -04:00
parent 1e260a4e9c
commit c485b951f3
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
6 changed files with 113 additions and 96 deletions

View file

@ -262,7 +262,7 @@ mod test {
use super::*; use super::*;
use crate::{ use crate::{
ops::tx::{get_tx, tx_exists}, 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, Env,
}; };
@ -315,20 +315,23 @@ mod test {
let tables = env_inner.open_tables(&tx_ro).unwrap(); let tables = env_inner.open_tables(&tx_ro).unwrap();
// Assert only the proper tables were added to. // Assert only the proper tables were added to.
assert_eq!(tables.block_infos().len().unwrap(), 3); AssertTableLen {
assert_eq!(tables.block_blobs().len().unwrap(), 3); block_infos: 3,
assert_eq!(tables.block_heights().len().unwrap(), 3); block_blobs: 3,
assert_eq!(tables.key_images().len().unwrap(), 69); block_heights: 3,
assert_eq!(tables.num_outputs().len().unwrap(), 38); key_images: 69,
assert_eq!(tables.pruned_tx_blobs().len().unwrap(), 0); num_outputs: 38,
assert_eq!(tables.prunable_hashes().len().unwrap(), 0); pruned_tx_blobs: 0,
assert_eq!(tables.outputs().len().unwrap(), 107); prunable_hashes: 0,
assert_eq!(tables.prunable_tx_blobs().len().unwrap(), 0); outputs: 107,
assert_eq!(tables.rct_outputs().len().unwrap(), 6); prunable_tx_blobs: 0,
assert_eq!(tables.tx_blobs().len().unwrap(), 5); rct_outputs: 6,
assert_eq!(tables.tx_ids().len().unwrap(), 5); tx_blobs: 5,
assert_eq!(tables.tx_heights().len().unwrap(), 5); tx_ids: 5,
assert_eq!(tables.tx_unlock_time().len().unwrap(), 0); tx_heights: 5,
tx_unlock_time: 0,
}
.assert(&tables);
// Check `cumulative` functions work. // Check `cumulative` functions work.
assert_eq!( assert_eq!(

View file

@ -101,7 +101,7 @@ mod test {
block::add_block, block::add_block,
tx::{get_tx, tx_exists}, 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, Env,
}; };
@ -149,6 +149,24 @@ mod test {
} }
// Assert reads are correct. // 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, chain_height(tables.block_heights()).unwrap());
assert_eq!( assert_eq!(
blocks_len - 1, blocks_len - 1,

View file

@ -64,7 +64,7 @@ mod test {
use super::*; use super::*;
use crate::{ use crate::{
ops::tx::{get_tx, tx_exists}, 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, Env,
}; };
@ -108,23 +108,11 @@ mod test {
let tables = env_inner.open_tables(&tx_ro).unwrap(); let tables = env_inner.open_tables(&tx_ro).unwrap();
// Assert only the proper tables were added to. // Assert only the proper tables were added to.
assert_eq!( AssertTableLen {
tables.key_images().len().unwrap(), key_images: tables.key_images().len().unwrap(),
u64::try_from(key_images.len()).unwrap() ..Default::default()
); }
assert_eq!(tables.block_infos().len().unwrap(), 0); .assert(&tables);
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);
for key_image in &key_images { for key_image in &key_images {
println!("key_image_exists(): {}", hex::encode(key_image)); println!("key_image_exists(): {}", hex::encode(key_image));

View file

@ -159,7 +159,7 @@ pub fn get_rct_num_outputs(
mod test { mod test {
use super::*; use super::*;
use crate::{ use crate::{
tests::{assert_all_tables_are_empty, tmp_concrete_env}, tests::{assert_all_tables_are_empty, tmp_concrete_env, AssertTableLen},
types::OutputFlags, types::OutputFlags,
Env, Env,
}; };
@ -221,20 +221,23 @@ mod test {
// Assert all reads of the outputs are OK. // Assert all reads of the outputs are OK.
{ {
// Assert proper tables were added to. // Assert proper tables were added to.
assert_eq!(tables.block_infos().len().unwrap(), 0); AssertTableLen {
assert_eq!(tables.block_blobs().len().unwrap(), 0); block_infos: 0,
assert_eq!(tables.block_heights().len().unwrap(), 0); block_blobs: 0,
assert_eq!(tables.key_images().len().unwrap(), 0); block_heights: 0,
assert_eq!(tables.num_outputs().len().unwrap(), 1); key_images: 0,
assert_eq!(tables.pruned_tx_blobs().len().unwrap(), 0); num_outputs: 1,
assert_eq!(tables.prunable_hashes().len().unwrap(), 0); pruned_tx_blobs: 0,
assert_eq!(tables.outputs().len().unwrap(), 1); prunable_hashes: 0,
assert_eq!(tables.prunable_tx_blobs().len().unwrap(), 0); outputs: 1,
assert_eq!(tables.rct_outputs().len().unwrap(), 1); prunable_tx_blobs: 0,
assert_eq!(tables.tx_blobs().len().unwrap(), 0); rct_outputs: 1,
assert_eq!(tables.tx_ids().len().unwrap(), 0); tx_blobs: 0,
assert_eq!(tables.tx_heights().len().unwrap(), 0); tx_ids: 0,
assert_eq!(tables.tx_unlock_time().len().unwrap(), 0); tx_heights: 0,
tx_unlock_time: 0,
}
.assert(&tables);
// Assert length is correct. // Assert length is correct.
assert_eq!(get_num_outputs(tables.outputs()).unwrap(), 1); assert_eq!(get_num_outputs(tables.outputs()).unwrap(), 1);

View file

@ -343,7 +343,7 @@ pub fn tx_exists(
mod test { mod test {
use super::*; use super::*;
use crate::{ use crate::{
tests::{assert_all_tables_are_empty, tmp_concrete_env}, tests::{assert_all_tables_are_empty, tmp_concrete_env, AssertTableLen},
Env, Env,
}; };
use cuprate_test_utils::data::{tx_v1_sig0, tx_v1_sig2, tx_v2_rct3}; 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(); let tables = env_inner.open_tables(&tx_ro).unwrap();
// Assert only the proper tables were added to. // Assert only the proper tables were added to.
assert_eq!(tables.block_infos().len().unwrap(), 0); AssertTableLen {
assert_eq!(tables.block_blobs().len().unwrap(), 0); block_infos: 0,
assert_eq!(tables.block_heights().len().unwrap(), 0); block_blobs: 0,
assert_eq!(tables.key_images().len().unwrap(), 4); // added to key images block_heights: 0,
assert_eq!(tables.pruned_tx_blobs().len().unwrap(), 0); key_images: 4, // added to key images
assert_eq!(tables.prunable_hashes().len().unwrap(), 0); pruned_tx_blobs: 0,
assert_eq!(tables.num_outputs().len().unwrap(), 9); prunable_hashes: 0,
assert_eq!(tables.outputs().len().unwrap(), 10); // added to outputs num_outputs: 9,
assert_eq!(tables.prunable_tx_blobs().len().unwrap(), 0); outputs: 10, // added to outputs
assert_eq!(tables.rct_outputs().len().unwrap(), 2); prunable_tx_blobs: 0,
assert_eq!(tables.tx_blobs().len().unwrap(), 3); rct_outputs: 2,
assert_eq!(tables.tx_ids().len().unwrap(), 3); tx_blobs: 3,
assert_eq!(tables.tx_heights().len().unwrap(), 3); tx_ids: 3,
assert_eq!(tables.tx_unlock_time().len().unwrap(), 1); // only 1 has a timelock 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. // Both from ID and hash should result in getting the same transaction.
let mut tx_hashes = vec![]; let mut tx_hashes = vec![];

View file

@ -16,6 +16,7 @@ use monero_serai::{
ringct::{RctPrunable, RctSignatures}, ringct::{RctPrunable, RctSignatures},
transaction::{Timelock, Transaction, TransactionPrefix}, transaction::{Timelock, Transaction, TransactionPrefix},
}; };
use pretty_assertions::assert_eq;
use crate::{ use crate::{
config::Config, key::Key, storable::Storable, tables::Tables, transaction::TxRo, ConcreteEnv, 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 /// This is a struct with fields instead of a function
/// so that callers can name arguments, otherwise the call-site /// so that callers can name arguments, otherwise the call-site
/// is a little confusing, i.e. `assert_table_len(0, 25, 1, 123)`. /// 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 { pub(crate) struct AssertTableLen {
block_infos: u64, pub(crate) block_infos: u64,
block_blobs: u64, pub(crate) block_blobs: u64,
block_heights: u64, pub(crate) block_heights: u64,
key_images: u64, pub(crate) key_images: u64,
num_outputs: u64, pub(crate) num_outputs: u64,
pruned_tx_blobs: u64, pub(crate) pruned_tx_blobs: u64,
prunable_hashes: u64, pub(crate) prunable_hashes: u64,
outputs: u64, pub(crate) outputs: u64,
prunable_tx_blobs: u64, pub(crate) prunable_tx_blobs: u64,
rct_outputs: u64, pub(crate) rct_outputs: u64,
tx_blobs: u64, pub(crate) tx_blobs: u64,
tx_ids: u64, pub(crate) tx_ids: u64,
tx_heights: u64, pub(crate) tx_heights: u64,
tx_unlock_time: u64, pub(crate) tx_unlock_time: u64,
} }
impl AssertTableLen { impl AssertTableLen {
/// Assert the length of all tables. /// Assert the length of all tables.
pub(crate) fn assert(self, tables: &impl Tables) { pub(crate) fn assert(self, tables: &impl Tables) {
for (table_len, self_len) in [ let other = Self {
(tables.block_infos().len(), self.block_infos), block_infos: tables.block_infos().len().unwrap(),
(tables.block_blobs().len(), self.block_blobs), block_blobs: tables.block_blobs().len().unwrap(),
(tables.block_heights().len(), self.block_heights), block_heights: tables.block_heights().len().unwrap(),
(tables.key_images().len(), self.key_images), key_images: tables.key_images().len().unwrap(),
(tables.num_outputs().len(), self.num_outputs), num_outputs: tables.num_outputs().len().unwrap(),
(tables.pruned_tx_blobs().len(), self.pruned_tx_blobs), pruned_tx_blobs: tables.pruned_tx_blobs().len().unwrap(),
(tables.prunable_hashes().len(), self.prunable_hashes), prunable_hashes: tables.prunable_hashes().len().unwrap(),
(tables.outputs().len(), self.outputs), outputs: tables.outputs().len().unwrap(),
(tables.prunable_tx_blobs().len(), self.prunable_tx_blobs), prunable_tx_blobs: tables.prunable_tx_blobs().len().unwrap(),
(tables.rct_outputs().len(), self.rct_outputs), rct_outputs: tables.rct_outputs().len().unwrap(),
(tables.tx_blobs().len(), self.tx_blobs), tx_blobs: tables.tx_blobs().len().unwrap(),
(tables.tx_ids().len(), self.tx_ids), tx_ids: tables.tx_ids().len().unwrap(),
(tables.tx_heights().len(), self.tx_heights), tx_heights: tables.tx_heights().len().unwrap(),
(tables.tx_unlock_time().len(), self.tx_unlock_time), tx_unlock_time: tables.tx_unlock_time().len().unwrap(),
] { };
assert_eq!(table_len.unwrap(), self_len);
} assert_eq!(self, other);
} }
} }