Skip to content

Commit 2d151fc

Browse files
committed
Merge branch 'origin/tomas/iter-prefix-full-match' (#1642)
* origin/tomas/iter-prefix-full-match: changelog: add #1642 test/storage/rocksdb: check that prefix_iter matches only full segments ledger/db: ensure that prefix iter only matches full key segments
2 parents 27eec1b + f5ed324 commit 2d151fc

File tree

3 files changed

+40
-19
lines changed

3 files changed

+40
-19
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- Storage: Ensure that prefix iterator only returns key-
2+
vals in which the prefix key segments are matched fully.
3+
([\#1642](https://github.com/anoma/namada/pull/1642))

apps/src/lib/node/ledger/storage/rocksdb.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,8 +1312,18 @@ fn iter_subspace_prefix<'iter>(
13121312
.get_column_family(SUBSPACE_CF)
13131313
.expect("{SUBSPACE_CF} column family should exist");
13141314
let db_prefix = "".to_owned();
1315-
let prefix = prefix.map(|k| k.to_string()).unwrap_or_default();
1316-
iter_prefix(db, subspace_cf, db_prefix, Some(prefix))
1315+
iter_prefix(
1316+
db,
1317+
subspace_cf,
1318+
db_prefix,
1319+
prefix.map(|k| {
1320+
if k == &Key::default() {
1321+
k.to_string()
1322+
} else {
1323+
format!("{k}/")
1324+
}
1325+
}),
1326+
)
13171327
}
13181328

13191329
fn iter_diffs_prefix(
@@ -1609,28 +1619,24 @@ mod test {
16091619
let key_1_a = prefix_1.push(&"a".to_string()).unwrap();
16101620
let key_1_b = prefix_1.push(&"b".to_string()).unwrap();
16111621
let key_1_c = prefix_1.push(&"c".to_string()).unwrap();
1622+
let prefix_01 = Key::parse("01").unwrap();
1623+
let key_01_a = prefix_01.push(&"a".to_string()).unwrap();
16121624

1613-
let keys_0 = vec![key_0_a.clone(), key_0_b.clone(), key_0_c.clone()];
1614-
let keys_1 = vec![key_1_a.clone(), key_1_b.clone(), key_1_c.clone()];
1615-
let all_keys = vec![keys_0.clone(), keys_1.clone()].concat();
1625+
let keys_0 = vec![key_0_a, key_0_b, key_0_c];
1626+
let keys_1 = vec![key_1_a, key_1_b, key_1_c];
1627+
let keys_01 = vec![key_01_a];
1628+
let all_keys = vec![keys_0.clone(), keys_01, keys_1.clone()].concat();
16161629

16171630
// Write the keys
16181631
let mut batch = RocksDB::batch();
16191632
let height = BlockHeight(1);
1620-
db.batch_write_subspace_val(&mut batch, height, &key_0_a, [0_u8])
1621-
.unwrap();
1622-
db.batch_write_subspace_val(&mut batch, height, &key_0_b, [0_u8])
1623-
.unwrap();
1624-
db.batch_write_subspace_val(&mut batch, height, &key_0_c, [0_u8])
1625-
.unwrap();
1626-
db.batch_write_subspace_val(&mut batch, height, &key_1_a, [0_u8])
1627-
.unwrap();
1628-
db.batch_write_subspace_val(&mut batch, height, &key_1_b, [0_u8])
1629-
.unwrap();
1630-
db.batch_write_subspace_val(&mut batch, height, &key_1_c, [0_u8])
1631-
.unwrap();
1633+
for key in &all_keys {
1634+
db.batch_write_subspace_val(&mut batch, height, key, [0_u8])
1635+
.unwrap();
1636+
}
16321637
db.exec_batch(batch.0).unwrap();
16331638

1639+
// Prefix "0" shouldn't match prefix "01"
16341640
let itered_keys: Vec<Key> = db
16351641
.iter_prefix(Some(&prefix_0))
16361642
.map(|(key, _val, _)| Key::parse(key).unwrap())

core/src/ledger/storage/mockdb.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,20 @@ impl<'iter> DBIter<'iter> for MockDB {
516516

517517
fn iter_prefix(&'iter self, prefix: Option<&Key>) -> MockPrefixIterator {
518518
let db_prefix = "subspace/".to_owned();
519-
let prefix_str = prefix.map(|k| k.to_string()).unwrap_or_default();
520-
let prefix = format!("{}{}", db_prefix, prefix_str);
519+
let prefix = format!(
520+
"{}{}",
521+
db_prefix,
522+
match prefix {
523+
Some(prefix) => {
524+
if prefix == &Key::default() {
525+
prefix.to_string()
526+
} else {
527+
format!("{prefix}/")
528+
}
529+
}
530+
None => "".to_string(),
531+
}
532+
);
521533
let iter = self.0.borrow().clone().into_iter();
522534
MockPrefixIterator::new(MockIterator { prefix, iter }, db_prefix)
523535
}

0 commit comments

Comments
 (0)