-
Notifications
You must be signed in to change notification settings - Fork 78
add SMT integration tests for History mechanism #754
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
Open
Himess
wants to merge
6
commits into
0xMiden:next
Choose a base branch
from
Himess:feat/smt-history-tests
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
369c110
feat(smt): add integration tests for History mechanism
Himess 6034e4d
refactor: use real SMT mutations in History tests
Himess 98e0d3f
fix: unwrap Result from compute_mutations and apply_mutations
Himess 525729d
refactor: extract shared helper and add hash verification
Himess 321e6a7
Fix: use get_node_hash() as reviewer suggested
Himess 7c51eb2
test: add round-trip consistency check for history view vs SMT value
Himess File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -363,3 +363,156 @@ fn view_at() -> Result<()> { | |
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| // SMT INTEGRATION TESTS | ||
| // ================================================================================================ | ||
|
|
||
| use crate::merkle::smt::{MutationSet, NodeMutation, SMT_DEPTH, Smt, SparseMerkleTree}; | ||
|
|
||
| /// Converts a MutationSet into the format expected by History. | ||
| /// | ||
| /// This helper extracts node additions and leaf changes from an SMT mutation set, | ||
| /// transforming them into the format used by the History tracking mechanism. | ||
| fn mutation_set_to_history_changes( | ||
| mutations: &MutationSet<SMT_DEPTH, Word, Word>, | ||
| ) -> (NodeChanges, LeafChanges) { | ||
| let mut node_changes = NodeChanges::default(); | ||
| for (index, mutation) in mutations.node_mutations().iter() { | ||
| if let NodeMutation::Addition(inner_node) = mutation { | ||
| node_changes.insert(*index, inner_node.hash()); | ||
| } | ||
| } | ||
|
|
||
| let mut leaf_changes = LeafChanges::default(); | ||
| for (key, value) in mutations.new_pairs().iter() { | ||
| let leaf_index = LeafIndex::new(Smt::key_to_leaf_index(key).value()).unwrap(); | ||
| leaf_changes | ||
| .entry(leaf_index) | ||
| .or_insert_with(CompactLeaf::new) | ||
| .insert(*key, *value); | ||
| } | ||
|
|
||
| (node_changes, leaf_changes) | ||
| } | ||
|
|
||
| /// Tests History integration using real SMT mutations. | ||
| /// | ||
| /// This test creates an actual SMT, computes mutations via the SMT API, | ||
| /// and verifies that History correctly tracks the resulting node and leaf changes. | ||
| #[test] | ||
| fn smt_history_with_real_mutations() -> Result<()> { | ||
| // Create an empty SMT | ||
| let mut smt = Smt::new(); | ||
| let initial_root = smt.root(); | ||
|
|
||
| // Generate test key-value pairs | ||
| let key_1: Word = rand_value(); | ||
| let value_1: Word = rand_value(); | ||
| let key_2: Word = rand_value(); | ||
| let value_2: Word = rand_value(); | ||
|
|
||
| // Create history to track versions | ||
| let mut history = History::empty(3); | ||
|
|
||
| // Version 0: Insert first key-value pair using real SMT mutation | ||
| let mutations_v0 = smt.compute_mutations(vec![(key_1, value_1)]).unwrap(); | ||
| let (node_changes_v0, leaf_changes_v0) = mutation_set_to_history_changes(&mutations_v0); | ||
| smt.apply_mutations(mutations_v0).unwrap(); | ||
| let root_v0 = smt.root(); | ||
|
|
||
| // Verify stored node hashes match what the SMT computed | ||
| for (index, hash) in node_changes_v0.iter() { | ||
| assert_eq!(*hash, smt.get_node_hash(*index)); | ||
| } | ||
|
|
||
| history.add_version(root_v0, 0, node_changes_v0.clone(), leaf_changes_v0.clone())?; | ||
|
|
||
| // Version 1: Insert second key-value pair | ||
| let mutations_v1 = smt.compute_mutations(vec![(key_2, value_2)]).unwrap(); | ||
| let (node_changes_v1, leaf_changes_v1) = mutation_set_to_history_changes(&mutations_v1); | ||
| smt.apply_mutations(mutations_v1).unwrap(); | ||
| let root_v1 = smt.root(); | ||
|
|
||
| // Verify stored node hashes match what the SMT computed | ||
| for (index, hash) in node_changes_v1.iter() { | ||
| assert_eq!(*hash, smt.get_node_hash(*index)); | ||
| } | ||
|
|
||
| history.add_version(root_v1, 1, node_changes_v1, leaf_changes_v1)?; | ||
|
|
||
| // Verify roots are tracked correctly | ||
| assert!(history.is_known_root(root_v0)); | ||
| assert!(history.is_known_root(root_v1)); | ||
| assert!(!history.is_known_root(initial_root)); // Initial empty root not added | ||
|
|
||
| // Query version 0 and verify leaf data | ||
| let view_v0 = history.get_view_at(0)?; | ||
| let leaf_index_1 = LeafIndex::new(Smt::key_to_leaf_index(&key_1).value()).unwrap(); | ||
| assert!(view_v0.leaf_value(&leaf_index_1).is_some()); | ||
| assert_eq!(view_v0.value(&key_1), Some(Some(&value_1))); | ||
|
|
||
| // Query version 1 and verify both leaves accessible | ||
| let view_v1 = history.get_view_at(1)?; | ||
| let leaf_index_2 = LeafIndex::new(Smt::key_to_leaf_index(&key_2).value()).unwrap(); | ||
| assert!(view_v1.leaf_value(&leaf_index_2).is_some()); | ||
| assert_eq!(view_v1.value(&key_2), Some(Some(&value_2))); | ||
|
|
||
| // Verify node changes were captured (mutations produce inner node updates) | ||
| assert!(!node_changes_v0.is_empty(), "SMT insertion should produce node changes"); | ||
|
|
||
| // Verify querying a non-existent key returns None | ||
| let nonexistent_key: Word = rand_value(); | ||
| assert!(view_v1.value(&nonexistent_key).is_none()); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// Tests History with SMT value updates (replacing existing values). | ||
| #[test] | ||
| fn smt_history_value_updates() -> Result<()> { | ||
| let mut smt = Smt::new(); | ||
|
|
||
| let key: Word = rand_value(); | ||
| let value_v0: Word = rand_value(); | ||
| let value_v1: Word = rand_value(); | ||
|
|
||
| let mut history = History::empty(2); | ||
|
|
||
| // Version 0: Insert initial value | ||
| let mutations_v0 = smt.compute_mutations(vec![(key, value_v0)]).unwrap(); | ||
| let (node_changes_v0, leaf_changes_v0) = mutation_set_to_history_changes(&mutations_v0); | ||
| smt.apply_mutations(mutations_v0).unwrap(); | ||
|
|
||
| // Verify stored node hashes match what the SMT computed | ||
| for (index, hash) in node_changes_v0.iter() { | ||
| assert_eq!(*hash, smt.get_node_hash(*index)); | ||
| } | ||
|
|
||
| history.add_version(smt.root(), 0, node_changes_v0, leaf_changes_v0)?; | ||
|
|
||
| // Version 1: Update to new value | ||
| let mutations_v1 = smt.compute_mutations(vec![(key, value_v1)]).unwrap(); | ||
| let (node_changes_v1, leaf_changes_v1) = mutation_set_to_history_changes(&mutations_v1); | ||
| smt.apply_mutations(mutations_v1).unwrap(); | ||
|
|
||
| // Verify stored node hashes match what the SMT computed | ||
| for (index, hash) in node_changes_v1.iter() { | ||
| assert_eq!(*hash, smt.get_node_hash(*index)); | ||
| } | ||
|
|
||
| history.add_version(smt.root(), 1, node_changes_v1, leaf_changes_v1)?; | ||
|
|
||
| // Verify version 0 has original value | ||
| let view_v0 = history.get_view_at(0)?; | ||
| assert_eq!(view_v0.value(&key), Some(Some(&value_v0))); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: since we have the SMT available, could also verify |
||
|
|
||
| // Verify version 1 has updated value | ||
| let view_v1 = history.get_view_at(1)?; | ||
| assert_eq!(view_v1.value(&key), Some(Some(&value_v1))); | ||
|
|
||
| // Verify round-trip consistency: history view matches current SMT value | ||
| let current_smt_value = smt.get_value(&key); | ||
| assert_eq!(view_v1.value(&key), Some(Some(¤t_smt_value))); | ||
|
|
||
| Ok(()) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test verifies node changes were captured, but doesn't compare the stored hashes against the actual SMT. Could we add something like:
This would confirm the History node values match what the SMT actually computed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Added hash verification assertions after each mutation is applied.