Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

storage: add unit tests for StorageRevertsIter #11999

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions crates/storage/provider/src/bundle_state/state_reverts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,105 @@ where
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_storage_reverts_iter_empty() {
// Create empty sample data for reverts and wiped entries.
let reverts: Vec<(B256, RevertToSlot)> = vec![];
let wiped: Vec<(B256, U256)> = vec![];

// Create the iterator with the empty data.
let iter = StorageRevertsIter::new(reverts, wiped);

// Iterate and collect results into a vector for verification.
let results: Vec<_> = iter.collect();

// Verify that the results are empty.
assert_eq!(results, vec![]);
}

#[test]
fn test_storage_reverts_iter_reverts_only() {
// Create sample data for only reverts.
let reverts = vec![
(B256::from_slice(&[4; 32]), RevertToSlot::Destroyed),
(B256::from_slice(&[5; 32]), RevertToSlot::Some(U256::from(40))),
];

// Create the iterator with only reverts and no wiped entries.
let iter = StorageRevertsIter::new(reverts, vec![]);

// Iterate and collect results into a vector for verification.
let results: Vec<_> = iter.collect();

// Verify the output order and values.
assert_eq!(
results,
vec![
(B256::from_slice(&[4; 32]), U256::ZERO), // Revert slot previous value
(B256::from_slice(&[5; 32]), U256::from(40)), // Only revert present.
]
);
}

#[test]
fn test_storage_reverts_iter_wiped_only() {
// Create sample data for only wiped entries.
let wiped = vec![
(B256::from_slice(&[6; 32]), U256::from(50)),
(B256::from_slice(&[7; 32]), U256::from(60)),
];

// Create the iterator with only wiped entries and no reverts.
let iter = StorageRevertsIter::new(vec![], wiped);

// Iterate and collect results into a vector for verification.
let results: Vec<_> = iter.collect();

// Verify the output order and values.
assert_eq!(
results,
vec![
(B256::from_slice(&[6; 32]), U256::from(50)), // Only wiped present.
(B256::from_slice(&[7; 32]), U256::from(60)), // Only wiped present.
]
);
}

#[test]
fn test_storage_reverts_iter_interleaved() {
// Create sample data for interleaved reverts and wiped entries.
let reverts = vec![
(B256::from_slice(&[8; 32]), RevertToSlot::Some(U256::from(70))),
(B256::from_slice(&[9; 32]), RevertToSlot::Some(U256::from(80))),
// Some higher key than wiped
(B256::from_slice(&[15; 32]), RevertToSlot::Some(U256::from(90))),
];

let wiped = vec![
(B256::from_slice(&[8; 32]), U256::from(75)), // Same key as revert
(B256::from_slice(&[10; 32]), U256::from(85)), // Wiped with new key
];

// Create the iterator with the sample data.
let iter = StorageRevertsIter::new(reverts, wiped);

// Iterate and collect results into a vector for verification.
let results: Vec<_> = iter.collect();

// Verify the output order and values.
assert_eq!(
results,
vec![
(B256::from_slice(&[8; 32]), U256::from(70)), // Revert takes priority.
(B256::from_slice(&[9; 32]), U256::from(80)), // Only revert present.
(B256::from_slice(&[10; 32]), U256::from(85)), // Wiped entry.
(B256::from_slice(&[15; 32]), U256::from(90)), // WGreater revert entry
]
);
}
}
Loading