Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/delete datastore entries tu 2 #4806

Merged
merged 3 commits into from
Jan 17, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 61 additions & 12 deletions massa-ledger-worker/src/ledger_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,6 @@ fn delete_datastore_entries(
) {
// datastore
let key_prefix = datastore_prefix_from_address(addr, &[]);

for (serialized_key, _) in db
.iterator_cf(
STATE_CF,
Expand Down Expand Up @@ -524,6 +523,26 @@ impl LedgerDB {
addresses
}

/// Get the entire datastore for a given address (no deserialization + no end prefix)
///
/// IMPORTANT: This should only be used for debug purposes.
#[allow(dead_code)]
#[cfg(any(test, feature = "test-exports"))]
pub fn get_entire_datastore_raw(
&self,
addr: &Address,
) -> std::collections::BTreeMap<Vec<u8>, Vec<u8>> {
let db = self.db.read();

let key_prefix = datastore_prefix_from_address(addr, &[]);

db.iterator_cf(
STATE_CF,
MassaIteratorMode::From(&key_prefix, MassaDirection::Forward),
)
Leo-Besancon marked this conversation as resolved.
Show resolved Hide resolved
.collect()
}

/// Get the entire datastore for a given address.
///
/// IMPORTANT: This should only be used for debug purposes.
Expand Down Expand Up @@ -591,13 +610,13 @@ mod tests {
use std::sync::Arc;
use tempfile::TempDir;

fn init_test_ledger(addr: Address) -> (LedgerDB, BTreeMap<Vec<u8>, Vec<u8>>) {
fn setup_test_ledger(addr: Address) -> (LedgerDB, DBBatch, BTreeMap<Vec<u8>, Vec<u8>>) {
// init data

let mut data = BTreeMap::new();
data.insert(b"1".to_vec(), b"a".to_vec());
data.insert(b"2".to_vec(), b"b".to_vec());
data.insert(b"3".to_vec(), b"c".to_vec());

let entry = LedgerEntry {
balance: Amount::from_str("42").unwrap(),
datastore: data.clone(),
Expand Down Expand Up @@ -630,20 +649,25 @@ mod tests {

ledger_db.put_entry(&addr, entry, &mut batch);
ledger_db.update_entry(&addr, entry_update, &mut batch);
ledger_db
.db
.write()
.write_batch(batch, Default::default(), None);
// ledger_db
// .db
// .write()
// .write_batch(batch, Default::default(), None);

// return db and initial data
(ledger_db, data)
// (ledger_db, data)
(ledger_db, batch, data)
}

/// Functional test of `LedgerDB`
#[test]
fn test_ledger_db() {
let addr = Address::from_public_key(&KeyPair::generate(0).unwrap().get_public_key());
let (ledger_db, data) = init_test_ledger(addr);
let (ledger_db, batch, data) = setup_test_ledger(addr);
ledger_db
.db
.write()
.write_batch(batch, Default::default(), None);

let amount_deserializer =
AmountDeserializer::new(Included(Amount::MIN), Included(Amount::MAX));
Expand Down Expand Up @@ -702,7 +726,31 @@ mod tests {
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 (ledger_db, mut batch, _data) = setup_test_ledger(addr);

// Add a key, value into db with a prefix right after datastore (datastore indent = 3, here we use indent = 4)
// This is to test if delete_datastore_entries only delete datastore entries
let mut datastore_key = vec![];
ledger_db
.key_serializer_db
.serialize(
&Key::new(&addr, KeyType::DATASTORE(b"a".to_vec())),
&mut datastore_key,
)
.expect(KEY_SER_ERROR);
let datastore_key_len = datastore_key.len();
// We modify the datastore key indent (from 3 to 4) so this is not a 'datastore' key anymore
datastore_key[datastore_key_len - 2] = DATASTORE_IDENT + 1;
// Value is not important here
let datastore_value = b"a".to_vec();
batch.insert(datastore_key.clone(), Some(datastore_value.clone()));
//

// Write batch into db
ledger_db
.db
.write()
.write_batch(batch, Default::default(), None);

let datastore = ledger_db.get_entire_datastore(&addr);
// println!("datastore: {:?}", datastore);
Expand All @@ -717,8 +765,9 @@ mod tests {
guard.write_batch(batch, Default::default(), None);
drop(guard);

let datastore = ledger_db.get_entire_datastore(&addr);
let datastore = ledger_db.get_entire_datastore_raw(&addr);
// println!("datastore: {:?}", datastore);
assert!(datastore.is_empty());
assert_eq!(datastore.len(), 1);
assert!(datastore.contains_key(&datastore_key));
}
}
Loading