Skip to content

Commit 48a03bb

Browse files
committed
chore: test get_by_hash() by loading both the value in get_by_key() and the same value by get_by_hash(hash(key))
1 parent 4a7f9e9 commit 48a03bb

File tree

1 file changed

+23
-0
lines changed
  • stackslib/src/chainstate/stacks/index

1 file changed

+23
-0
lines changed

stackslib/src/chainstate/stacks/index/marf.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,31 @@ pub trait MarfConnection<T: MarfTrieId> {
122122

123123
fn sqlite_conn(&self) -> &Connection;
124124

125+
/// Get and check a value against get_from_hash
126+
/// (test only)
127+
#[cfg(test)]
128+
fn get_and_check_with_hash(&mut self, block_hash: &T, key: &str) {
129+
let res = self.with_conn(|c| MARF::get_by_key(c, block_hash, key));
130+
let res_with_hash =
131+
self.with_conn(|c| MARF::get_by_hash(c, block_hash, &TrieHash::from_key(key)));
132+
match (res, res_with_hash) {
133+
(Ok(Some(x)), Ok(Some(y))) => {
134+
assert_eq!(x, y);
135+
}
136+
(Ok(None), Ok(None)) => {}
137+
(Err(_), Err(_)) => {}
138+
(x, y) => {
139+
panic!("Inconsistency: {x:?} != {y:?}");
140+
}
141+
}
142+
}
143+
144+
#[cfg(not(test))]
145+
fn get_and_check_with_hash(&mut self, _block_hash: &T, _key: &str) {}
146+
125147
/// Resolve a key from the MARF to a MARFValue with respect to the given block height.
126148
fn get(&mut self, block_hash: &T, key: &str) -> Result<Option<MARFValue>, Error> {
149+
self.get_and_check_with_hash(block_hash, key);
127150
self.with_conn(|c| MARF::get_by_key(c, block_hash, key))
128151
}
129152

0 commit comments

Comments
 (0)