From da594f1d928fc9a83679f36a4d2a58ec1d270bab Mon Sep 17 00:00:00 2001 From: Joseph Livesey Date: Sun, 17 Sep 2023 10:51:25 -0400 Subject: [PATCH 1/3] Add tests for commit_store get_batch_by_transaction Signed-off-by: Joseph Livesey --- validator/src/journal/commit_store.rs | 71 +++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/validator/src/journal/commit_store.rs b/validator/src/journal/commit_store.rs index 00699fa614..b2a5d5c772 100644 --- a/validator/src/journal/commit_store.rs +++ b/validator/src/journal/commit_store.rs @@ -562,3 +562,74 @@ impl ChainReader for CommitStore { .map_err(|err| ChainReadError::GeneralReadError(format!("{err:?}"))) } } + +#[cfg(test)] +mod tests { + use crate::database::error::DatabaseError; + + #[derive(Clone, Debug, PartialEq)] + pub struct TestBatch { + // pub header_signature: String, + // pub transactions: Vec, + // pub signer_public_key: String, + pub transaction_ids: Vec, + // pub trace: bool, + + // pub header_bytes: Vec, + } + + pub struct TestBlock { + // pub header_signature: String, + pub batches: Vec, + // pub state_root_hash: String, + // pub consensus: Vec, + // pub batch_ids: Vec, + // pub signer_public_key: String, + // pub previous_block_id: String, + // pub block_num: u64, + + // pub header_bytes: Vec, + } + + // Copies logic of `sawtooth_validator::journal::commit_store::CommitStore::get_batch_by_transaction` + fn mock_get_batch_by_transaction( + transaction_id: &str, + block: TestBlock, + ) -> Result { + // self.get_by_transaction_id(transaction_id) + // .and_then(|block| { + block + .batches + .into_iter() + .find(|batch| { + !batch + .transaction_ids + .iter() + .any(|txn_id| txn_id == transaction_id) + }) + .ok_or_else(|| DatabaseError::CorruptionError("Transaction index corrupted".into())) + // }) + } + + #[test] + fn test_get_batch_by_transaction_ok() { + let block = TestBlock { + batches: vec![TestBatch { + transaction_ids: vec!["1".to_string()], + }], + }; + let transaction_id = "1"; + assert!(mock_get_batch_by_transaction(transaction_id, block).is_ok()); + } + + #[test] + fn test_get_batch_by_transaction_should_err() { + let block = TestBlock { + batches: vec![TestBatch { + transaction_ids: vec!["1".to_string()], + }], + }; + let transaction_id = "nope"; + assert!(mock_get_batch_by_transaction(transaction_id, block).is_err()); + } +} From ea13a2c84dbffdb9f2cbbf51b082e19a824a8947 Mon Sep 17 00:00:00 2001 From: Joseph Livesey Date: Tue, 10 Oct 2023 11:06:01 -0400 Subject: [PATCH 2/3] Fix bug in commit_store get_batch_by_transaction Signed-off-by: Joseph Livesey --- validator/src/journal/commit_store.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/validator/src/journal/commit_store.rs b/validator/src/journal/commit_store.rs index b2a5d5c772..ab3ccfe091 100644 --- a/validator/src/journal/commit_store.rs +++ b/validator/src/journal/commit_store.rs @@ -319,7 +319,7 @@ impl CommitStore { .batches .into_iter() .find(|batch| { - !batch + batch .transaction_ids .iter() .any(|txn_id| txn_id == transaction_id) @@ -605,7 +605,7 @@ mod tests { !batch .transaction_ids .iter() - .any(|txn_id| txn_id == transaction_id) + .all(|txn_id| txn_id != transaction_id) }) .ok_or_else(|| DatabaseError::CorruptionError("Transaction index corrupted".into())) // }) From 7e3088cc57012fa87fee7fa3f0d9819520376e3a Mon Sep 17 00:00:00 2001 From: Joseph Livesey Date: Tue, 10 Oct 2023 11:17:46 -0400 Subject: [PATCH 3/3] Fix test logic after fixing bug in get_batch_by_transaction Signed-off-by: Joseph Livesey --- validator/src/journal/commit_store.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/validator/src/journal/commit_store.rs b/validator/src/journal/commit_store.rs index ab3ccfe091..15bdba7d62 100644 --- a/validator/src/journal/commit_store.rs +++ b/validator/src/journal/commit_store.rs @@ -602,10 +602,10 @@ mod tests { .batches .into_iter() .find(|batch| { - !batch + batch .transaction_ids .iter() - .all(|txn_id| txn_id != transaction_id) + .any(|txn_id| txn_id == transaction_id) }) .ok_or_else(|| DatabaseError::CorruptionError("Transaction index corrupted".into())) // })