Skip to content

Commit

Permalink
Fix deletion reordering (#675)
Browse files Browse the repository at this point in the history
* Fix deletion reordering

* No more leaf_index ordering
  • Loading branch information
Dzejkop authored Jan 18, 2024
1 parent 3c729a2 commit 9a78643
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 39 deletions.
1 change: 1 addition & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ impl App {
let mut mined_items = database
.get_commitments_by_status(ProcessedStatus::Mined)
.await?;

mined_items.sort_by_key(|item| item.leaf_index);

if !force_cache_purge {
Expand Down
55 changes: 16 additions & 39 deletions src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ impl Database {
SELECT leaf_index, commitment
FROM identities
WHERE status = $1
ORDER BY leaf_index ASC, id ASC;
ORDER BY id ASC;
"#,
)
.bind(<&str>::from(status));
Expand Down Expand Up @@ -1658,7 +1658,7 @@ mod test {
}

#[tokio::test]
async fn get_commitments_by_status_results_are_not_deduplicated() -> anyhow::Result<()> {
async fn get_commitments_by_status_results_are_in_id_order() -> anyhow::Result<()> {
let (db, _db_container) = setup_db().await?;

let identities = mock_identities(5);
Expand Down Expand Up @@ -1687,53 +1687,30 @@ mod test {
pending_tree_updates[0].element, identities[0],
"First element is the original value"
);
assert_eq!(
pending_tree_updates[1].element,
Hash::ZERO,
"Second element is the updated (deleted) value"
);

// 3rd identity
for (idx, identity) in identities.iter().enumerate() {
assert_eq!(
pending_tree_updates[idx].element, *identity,
"Element {} is the original value",
idx
);
}

// Deletions
assert_eq!(
pending_tree_updates[4].element, identities[3],
"First element is the original value"
pending_tree_updates[5].element,
Hash::ZERO,
"First deletion is at the end"
);
assert_eq!(
pending_tree_updates[5].element,
pending_tree_updates[6].element,
Hash::ZERO,
"Second element is the updated (deleted) value"
"Second deletion is at the end"
);

Ok(())
}

#[tokio::test]
async fn get_commitments_by_status_results_are_sorted() -> anyhow::Result<()> {
let (db, _db_container) = setup_db().await?;

let identities = mock_identities(5);

let roots = mock_roots(5);

let unordered_indexes = vec![3, 1, 4, 2, 0];
for i in unordered_indexes {
db.insert_pending_identity(i, &identities[i], &roots[i])
.await
.context("Inserting identity")?;
}

let pending_tree_updates = db
.get_commitments_by_status(ProcessedStatus::Pending)
.await?;
assert_eq!(pending_tree_updates.len(), 5);
for i in 0..5 {
assert_eq!(pending_tree_updates[i].element, identities[i]);
assert_eq!(pending_tree_updates[i].leaf_index, i);
}

Ok(())
}

#[tokio::test]
async fn test_root_invalidation() -> anyhow::Result<()> {
let (db, _db_container) = setup_db().await?;
Expand Down

0 comments on commit 9a78643

Please sign in to comment.