diff --git a/crates/storage/db/src/implementation/mdbx/mod.rs b/crates/storage/db/src/implementation/mdbx/mod.rs index 2f727b5d73..936e1128f2 100644 --- a/crates/storage/db/src/implementation/mdbx/mod.rs +++ b/crates/storage/db/src/implementation/mdbx/mod.rs @@ -468,6 +468,52 @@ mod tests { tx.commit().expect(ERROR_COMMIT); } + #[test] + fn db_cursor_insert_dup() { + let db: Arc> = test_utils::create_test_db(EnvKind::RW); + let tx = db.tx_mut().expect(ERROR_INIT_TX); + + let mut dup_cursor = tx.cursor_dup_write::().unwrap(); + let key = Address::random(); + let subkey1 = H256::random(); + let subkey2 = H256::random(); + + let entry1 = StorageEntry { key: subkey1, value: U256::ZERO }; + assert!(dup_cursor.insert(key, entry1).is_ok()); + + // Can't insert + let entry2 = StorageEntry { key: subkey2, value: U256::ZERO }; + assert!(dup_cursor.insert(key, entry2).is_err()); + } + + #[test] + fn db_cursor_delete_current_non_existent() { + let db: Arc> = test_utils::create_test_db(EnvKind::RW); + let tx = db.tx_mut().expect(ERROR_INIT_TX); + + let key1 = Address::from_low_u64_be(1); + let key2 = Address::from_low_u64_be(2); + let key3 = Address::from_low_u64_be(3); + let mut cursor = tx.cursor_write::().unwrap(); + + assert!(cursor.insert(key1, Account::default()).is_ok()); + assert!(cursor.insert(key2, Account::default()).is_ok()); + assert!(cursor.insert(key3, Account::default()).is_ok()); + + // Seek & delete key2 + cursor.seek_exact(key2).unwrap(); + assert_eq!(cursor.delete_current(), Ok(())); + assert_eq!(cursor.seek_exact(key2), Ok(None)); + + // Seek & delete key2 again + assert_eq!(cursor.seek_exact(key2), Ok(None)); + assert_eq!(cursor.delete_current(), Ok(())); + // Assert that key1 is still there + assert_eq!(cursor.seek_exact(key1), Ok(Some((key1, Account::default())))); + // Assert that key3 was deleted + assert_eq!(cursor.seek_exact(key3), Ok(None)); + } + #[test] fn db_cursor_insert_wherever_cursor_is() { let db: Arc> = test_utils::create_test_db(EnvKind::RW);