Skip to content

Commit

Permalink
Add TU for delete_datastore_entries function (#4802)
Browse files Browse the repository at this point in the history
* Add TU for delete_datastore_entries function

* Cargo clippy fix
  • Loading branch information
sydhds authored Jan 3, 2025
1 parent f8608ab commit 90a84b7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
4 changes: 2 additions & 2 deletions massa-db-worker/src/massa_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ where
change_id: Option<ChangeID>,
reset_history: bool,
) -> Result<(), MassaDBError> {
if let Some(change_id) = change_id.clone() {
if change_id < self.get_change_id().expect(CHANGE_ID_DESER_ERROR) {
if let Some(change_id) = change_id.as_ref() {
if *change_id < self.get_change_id().expect(CHANGE_ID_DESER_ERROR) {
return Err(MassaDBError::InvalidChangeID(String::from(
"change_id should monotonically increase after every write",
)));
Expand Down
4 changes: 2 additions & 2 deletions massa-execution-worker/src/active_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,11 @@ impl ActiveHistory {
return SlotIndexPosition::Past; // too old
}
let index: usize = match slot.slots_since(first_slot, thread_count) {
Err(_) => return SlotIndexPosition::Past, // overflow
Err(_) => return SlotIndexPosition::Past, // first_slot > slot - see slots_since docstring
Ok(d) => {
match d.try_into() {
Ok(d) => d,
Err(_) => return SlotIndexPosition::Future, // usize overflow
Err(_) => return SlotIndexPosition::Future, // conversion from u64 to usize fail
}
}
};
Expand Down
24 changes: 24 additions & 0 deletions massa-ledger-worker/src/ledger_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,4 +697,28 @@ mod tests {
assert_eq!(end_prefix(&[5, 6, 7]), Some(vec![5, 6, 8]));
assert_eq!(end_prefix(&[5, 6, 255]), Some(vec![5, 7]));
}

#[test]
fn test_ledger_delete() {
let keypair = KeyPair::generate(0).unwrap();
let addr = Address::from_public_key(&keypair.get_public_key());
let (ledger_db, _data) = init_test_ledger(addr);

let datastore = ledger_db.get_entire_datastore(&addr);
// println!("datastore: {:?}", datastore);
assert!(!datastore.is_empty());

let mut batch = DBBatch::new();
let guard = ledger_db.db.read();
delete_datastore_entries(&addr, &guard, &mut batch);
drop(guard);

let mut guard = ledger_db.db.write();
guard.write_batch(batch, Default::default(), None);
drop(guard);

let datastore = ledger_db.get_entire_datastore(&addr);
// println!("datastore: {:?}", datastore);
assert!(datastore.is_empty());
}
}

0 comments on commit 90a84b7

Please sign in to comment.