Просмотр исходного кода

drk: replaced sled-overlay with kvdb-overlay

x 1 день назад
Родитель
Сommit
2388b7ac3f
8 измененных файлов с 101 добавлено и 101 удалено
  1. 2 1
      Cargo.lock
  2. 4 1
      bin/drk/Cargo.toml
  3. 56 62
      bin/drk/src/cache.rs
  4. 10 10
      bin/drk/src/dao.rs
  5. 2 2
      bin/drk/src/lib.rs
  6. 4 4
      bin/drk/src/money.rs
  7. 9 9
      bin/drk/src/rpc.rs
  8. 14 12
      bin/drk/src/scanned_blocks.rs

+ 2 - 1
Cargo.lock

@@ -2781,9 +2781,11 @@ dependencies = [
  "darkfi_money_contract",
  "easy-parallel",
  "futures",
+ "kvdb-overlay",
  "lazy_static",
  "libc",
  "linenoise-rs",
+ "log",
  "num-bigint",
  "prettytable-rs",
  "rand 0.8.6",
@@ -2791,7 +2793,6 @@ dependencies = [
  "serde",
  "signal-hook",
  "signal-hook-async-std",
- "sled-overlay",
  "smol",
  "structopt",
  "structopt-toml",

+ 4 - 1
bin/drk/Cargo.toml

@@ -25,6 +25,7 @@ darkfi-serial = {path = "../../src/serial"}
 blake3 = "1.8.5"
 bs58 = "0.5.1"
 futures = "0.3.32"
+kvdb-overlay = {git = "https://git.dark.fi/darkrenaissance/kvdb-overlay", version = "0.1.0"}
 lazy_static = "1.5.0"
 libc = "0.2"
 linenoise-rs = "0.1.1"
@@ -32,12 +33,14 @@ num-bigint = "0.4.6"
 prettytable-rs = "0.10.0"
 rand = "0.8.6"
 rodio = { git = "https://github.com/RustAudio/rodio", default-features = false, features = ["playback", "mp3"] }
-sled-overlay = "0.1.20"
 toml = "0.9.8"
 tracing = "0.1.44"
 turso = "0.7.0-pre.14"
 url = "2.5.8"
 
+# Disable crates logging on release builds
+log = { version = "0.4", features = ["release_max_level_off"] }
+
 # Daemon
 easy-parallel = "3.3.1"
 signal-hook-async-std = "0.4.0"

+ 56 - 62
bin/drk/src/cache.rs

@@ -29,66 +29,60 @@ use darkfi_sdk::{
     pasta::pallas,
 };
 use darkfi_serial::{deserialize, serialize};
+use kvdb_overlay::{Batch, Database, DatabaseOverlay, DatabaseOverlayStateDiff, Tree};
 use num_bigint::BigUint;
-use sled_overlay::{sled, SledDbOverlay, SledDbOverlayStateDiff};
 use tracing::error;
 
-pub const SLED_SCANNED_BLOCKS_TREE: &[u8] = b"_scanned_blocks";
-pub const SLED_STATE_INVERSE_DIFF_TREE: &[u8] = b"_state_inverse_diff";
-pub const SLED_MERKLE_TREES_TREE: &[u8] = b"_merkle_trees";
-pub const SLED_MONEY_SMT_TREE: &[u8] = b"_money_smt";
+pub const KVDB_SCANNED_BLOCKS_TREE: &str = "_scanned_blocks";
+pub const KVDB_STATE_INVERSE_DIFF_TREE: &str = "_state_inverse_diff";
+pub const KVDB_MERKLE_TREES_TREE: &str = "_merkle_trees";
+pub const KVDB_MONEY_SMT_TREE: &str = "_money_smt";
 
-/// Structure holding all sled trees that define the blockchain cache.
+/// Structure holding all kvdb trees that define the blockchain cache.
 #[derive(Clone)]
 pub struct Cache {
-    /// Main pointer to the sled db connection
-    pub sled_db: sled::Db,
-    /// The `sled` tree storing the scanned blocks from the blockchain,
+    /// Main pointer to the kvdb connection
+    pub kvdb: Database,
+    /// The kvdb tree storing the scanned blocks from the blockchain,
     /// where the key is the height number, and the value is the blocks'
     /// hash.
-    pub scanned_blocks: sled::Tree,
-    /// The `sled` tree storing each blocks' full database state inverse
+    pub scanned_blocks: Tree,
+    /// The kvdb tree storing each blocks' full database state inverse
     /// changes, where the key is the block height number, and the value
     /// is the serialized database inverse diff.
-    pub state_inverse_diff: sled::Tree,
-    /// The `sled` tree storing the merkle trees of the blockchain,
+    pub state_inverse_diff: Tree,
+    /// The kvdb tree storing the merkle trees of the blockchain,
     /// where the key is the tree name, and the value is the serialized
     /// merkle tree itself.
-    pub merkle_trees: sled::Tree,
-    /// The `sled` tree storing the Sparse Merkle Tree of the Money
+    pub merkle_trees: Tree,
+    /// The kvdb tree storing the Sparse Merkle Tree of the Money
     /// contract.
     // TODO: this could be a map of trees so more contracts can open
     // SMTs if needed
-    pub money_smt: sled::Tree,
+    pub money_smt: Tree,
     // TODO: Perhaps we should also move transactions history here
 }
 
 impl Cache {
-    /// Instantiate a new `Cache` with the given `sled` database.
-    pub fn new(db: &sled::Db) -> Result<Self> {
-        let scanned_blocks = db.open_tree(SLED_SCANNED_BLOCKS_TREE)?;
-        let state_inverse_diff = db.open_tree(SLED_STATE_INVERSE_DIFF_TREE)?;
-        let merkle_trees = db.open_tree(SLED_MERKLE_TREES_TREE)?;
-        let money_smt = db.open_tree(SLED_MONEY_SMT_TREE)?;
-
-        Ok(Self {
-            sled_db: db.clone(),
-            scanned_blocks,
-            state_inverse_diff,
-            merkle_trees,
-            money_smt,
-        })
+    /// Instantiate a new `Cache` with the given key-value database.
+    pub fn new(kvdb: &Database) -> Result<Self> {
+        let scanned_blocks = kvdb.open_tree_default(KVDB_SCANNED_BLOCKS_TREE)?;
+        let state_inverse_diff = kvdb.open_tree_default(KVDB_STATE_INVERSE_DIFF_TREE)?;
+        let merkle_trees = kvdb.open_tree_default(KVDB_MERKLE_TREES_TREE)?;
+        let money_smt = kvdb.open_tree_default(KVDB_MONEY_SMT_TREE)?;
+
+        Ok(Self { kvdb: kvdb.clone(), scanned_blocks, state_inverse_diff, merkle_trees, money_smt })
     }
 
-    /// Execute an atomic sled batch corresponding to inserts to the
+    /// Execute an atomic kvdb batch corresponding to inserts to the
     /// merkle trees tree. For each record, the bytes slice is used as
     /// the key, and the serialized merkle tree is used as value.
     pub fn insert_merkle_trees(&self, trees: &[(&[u8], &MerkleTree)]) -> Result<()> {
-        let mut batch = sled::Batch::default();
+        let mut batch = Batch::new();
         for (key, tree) in trees {
-            batch.insert(*key, serialize(*tree));
+            batch.insert(key, &serialize(*tree));
         }
-        self.merkle_trees.apply_batch(batch)?;
+        self.kvdb.atomic_write(&[(&self.merkle_trees, &batch)])?;
         Ok(())
     }
 
@@ -98,17 +92,17 @@ impl Cache {
     pub fn insert_state_inverse_diff(
         &self,
         height: &u32,
-        diff: &SledDbOverlayStateDiff,
+        diff: &DatabaseOverlayStateDiff,
     ) -> Result<()> {
-        self.state_inverse_diff.insert(height.to_be_bytes(), serialize(diff))?;
+        self.state_inverse_diff.insert(&height.to_be_bytes(), &serialize(diff))?;
         Ok(())
     }
 
     /// Fetch given block height number from the store's state inverse
     /// diffs tree. The function will fail if the block height number
     /// was not found.
-    pub fn get_state_inverse_diff(&self, height: &u32) -> Result<SledDbOverlayStateDiff> {
-        match self.state_inverse_diff.get(height.to_be_bytes())? {
+    pub fn get_state_inverse_diff(&self, height: &u32) -> Result<DatabaseOverlayStateDiff> {
+        match self.state_inverse_diff.get(&height.to_be_bytes())? {
             Some(found) => Ok(deserialize(&found)?),
             None => Err(Error::BlockStateInverseDiffNotFound(*height)),
         }
@@ -116,25 +110,25 @@ impl Cache {
 }
 
 /// Overlay structure over a [`Cache`] instance.
-pub struct CacheOverlay(pub SledDbOverlay);
+pub struct CacheOverlay(pub DatabaseOverlay);
 
 impl CacheOverlay {
     /// Instantiate a new `CacheOverlay` over the given [`Cache`] instance.
     pub fn new(cache: &Cache) -> Result<CacheOverlay> {
-        // Here we configure all our cache sled trees to be protected in the overlay
+        // Here we configure all our cache kvdb trees to be protected in the overlay
         let protected_trees = vec![
-            SLED_SCANNED_BLOCKS_TREE,
-            SLED_STATE_INVERSE_DIFF_TREE,
-            SLED_MERKLE_TREES_TREE,
-            SLED_MONEY_SMT_TREE,
+            KVDB_SCANNED_BLOCKS_TREE.to_string(),
+            KVDB_STATE_INVERSE_DIFF_TREE.to_string(),
+            KVDB_MERKLE_TREES_TREE.to_string(),
+            KVDB_MONEY_SMT_TREE.to_string(),
         ];
-        let mut overlay = SledDbOverlay::new(&cache.sled_db, protected_trees);
+        let mut overlay = DatabaseOverlay::new(&cache.kvdb, protected_trees)?;
 
-        // Open all our cache sled trees in the overlay
-        overlay.open_tree(SLED_SCANNED_BLOCKS_TREE, true)?;
-        overlay.open_tree(SLED_STATE_INVERSE_DIFF_TREE, true)?;
-        overlay.open_tree(SLED_MERKLE_TREES_TREE, true)?;
-        overlay.open_tree(SLED_MONEY_SMT_TREE, true)?;
+        // Open all our cache kvdb trees in the overlay
+        overlay.open_tree_default(KVDB_SCANNED_BLOCKS_TREE, true)?;
+        overlay.open_tree_default(KVDB_STATE_INVERSE_DIFF_TREE, true)?;
+        overlay.open_tree_default(KVDB_MERKLE_TREES_TREE, true)?;
+        overlay.open_tree_default(KVDB_MONEY_SMT_TREE, true)?;
 
         Ok(Self(overlay))
     }
@@ -154,7 +148,7 @@ impl CacheOverlay {
             None => String::from("-"),
         };
         self.0.insert(
-            SLED_SCANNED_BLOCKS_TREE,
+            KVDB_SCANNED_BLOCKS_TREE,
             &height.to_be_bytes(),
             &serialize(&(hash.to_string(), block_signing_key)),
         )?;
@@ -173,12 +167,12 @@ pub type CacheSmt = SparseMerkleTree<
 
 pub struct CacheSmtStorage {
     pub overlay: CacheOverlay,
-    tree: Vec<u8>,
+    tree: String,
 }
 
 impl CacheSmtStorage {
-    pub fn new(overlay: CacheOverlay, tree: &[u8]) -> Self {
-        Self { overlay, tree: tree.to_vec() }
+    pub fn new(overlay: CacheOverlay, tree: &str) -> Self {
+        Self { overlay, tree: tree.to_string() }
     }
 
     pub fn snapshot(&self) -> Result<HashMap<BigUint, pallas::Base>> {
@@ -242,16 +236,16 @@ mod tests {
         crypto::smt::{gen_empty_nodes, util::FieldHasher, PoseidonFp, SparseMerkleTree},
         pasta::pallas,
     };
+    use kvdb_overlay::Database;
     use rand::rngs::OsRng;
-    use sled_overlay::sled;
 
-    use crate::cache::{Cache, CacheOverlay, CacheSmtStorage, SLED_MONEY_SMT_TREE};
+    use crate::cache::{Cache, CacheOverlay, CacheSmtStorage, KVDB_MONEY_SMT_TREE};
 
     #[test]
     fn test_cache_smt() -> Result<()> {
         // Setup cache and its overlay
-        let sled_db = sled::Config::new().temporary(true).open()?;
-        let cache = Cache::new(&sled_db)?;
+        let (kvdb, _kvdb_folder) = Database::open_temp()?;
+        let cache = Cache::new(&kvdb)?;
         let overlay = CacheOverlay::new(&cache)?;
 
         // Setup SMT
@@ -259,7 +253,7 @@ mod tests {
         let hasher = PoseidonFp::new();
         let empty_leaf = pallas::Base::ZERO;
         let empty_nodes = gen_empty_nodes::<{ HEIGHT + 1 }, _, _>(&hasher, empty_leaf);
-        let store = CacheSmtStorage::new(overlay, SLED_MONEY_SMT_TREE);
+        let store = CacheSmtStorage::new(overlay, KVDB_MONEY_SMT_TREE);
         let mut smt = SparseMerkleTree::<HEIGHT, { HEIGHT + 1 }, _, _, _>::new(
             store,
             hasher.clone(),
@@ -267,7 +261,7 @@ mod tests {
         );
 
         // Verify database is empty
-        assert!(cache.money_smt.is_empty());
+        assert!(cache.money_smt.is_empty()?);
 
         let leaves = vec![
             (pallas::Base::from(1), pallas::Base::random(&mut OsRng)),
@@ -309,13 +303,13 @@ mod tests {
         smt.store.overlay.0.apply_diff(&diff)?;
 
         // Verify database contains keys
-        assert!(!cache.money_smt.is_empty());
+        assert!(!cache.money_smt.is_empty()?);
 
         // We are now going to rollback the changes
         smt.store.overlay.0.apply_diff(&diff.inverse())?;
 
         // Verify database is empty again
-        assert!(cache.money_smt.is_empty());
+        assert!(cache.money_smt.is_empty()?);
 
         Ok(())
     }

+ 10 - 10
bin/drk/src/dao.rs

@@ -76,7 +76,7 @@ use darkfi_serial::{
 };
 
 use crate::{
-    cache::{CacheOverlay, CacheSmt, CacheSmtStorage, SLED_MONEY_SMT_TREE},
+    cache::{CacheOverlay, CacheSmt, CacheSmtStorage, KVDB_MONEY_SMT_TREE},
     convert_named_params,
     error::{WalletDbError, WalletDbResult},
     money::BALANCE_BASE10_DECIMALS,
@@ -86,9 +86,9 @@ use crate::{
     Drk,
 };
 
-// DAO Merkle trees Sled keys
-pub const SLED_MERKLE_TREES_DAO_DAOS: &[u8] = b"_dao_daos";
-pub const SLED_MERKLE_TREES_DAO_PROPOSALS: &[u8] = b"_dao_proposals";
+// DAO Merkle trees kvdb keys
+pub const KVDB_MERKLE_TREES_DAO_DAOS: &[u8] = b"_dao_daos";
+pub const KVDB_MERKLE_TREES_DAO_PROPOSALS: &[u8] = b"_dao_proposals";
 
 // Wallet SQL table constant names. These have to represent the `dao.sql`
 // SQL schema. Table names are prefixed with the contract ID to avoid collisions.
@@ -931,11 +931,11 @@ impl Drk {
     /// Fetch DAO Merkle trees from the wallet.
     /// If a tree doesn't exists a new Merkle Tree is returned.
     pub async fn get_dao_trees(&self) -> Result<(MerkleTree, MerkleTree)> {
-        let daos_tree = match self.cache.merkle_trees.get(SLED_MERKLE_TREES_DAO_DAOS)? {
+        let daos_tree = match self.cache.merkle_trees.get(KVDB_MERKLE_TREES_DAO_DAOS)? {
             Some(tree_bytes) => deserialize_async(&tree_bytes).await?,
             None => MerkleTree::new(u32::MAX as usize),
         };
-        let proposals_tree = match self.cache.merkle_trees.get(SLED_MERKLE_TREES_DAO_PROPOSALS)? {
+        let proposals_tree = match self.cache.merkle_trees.get(KVDB_MERKLE_TREES_DAO_PROPOSALS)? {
             Some(tree_bytes) => deserialize_async(&tree_bytes).await?,
             None => MerkleTree::new(u32::MAX as usize),
         };
@@ -1624,11 +1624,11 @@ impl Drk {
     /// Reset the DAO Merkle trees in the cache.
     pub fn reset_dao_trees(&self, output: &mut Vec<String>) -> WalletDbResult<()> {
         output.push(String::from("Resetting DAO Merkle trees"));
-        if let Err(e) = self.cache.merkle_trees.remove(SLED_MERKLE_TREES_DAO_DAOS) {
+        if let Err(e) = self.cache.merkle_trees.remove(KVDB_MERKLE_TREES_DAO_DAOS) {
             output.push(format!("[reset_dao_trees] Resetting DAO DAOs Merkle tree failed: {e}"));
             return Err(WalletDbError::GenericError)
         }
-        if let Err(e) = self.cache.merkle_trees.remove(SLED_MERKLE_TREES_DAO_PROPOSALS) {
+        if let Err(e) = self.cache.merkle_trees.remove(KVDB_MERKLE_TREES_DAO_PROPOSALS) {
             output
                 .push(format!("[reset_dao_trees] Resetting DAO Proposals Merkle tree failed: {e}"));
             return Err(WalletDbError::GenericError)
@@ -2551,7 +2551,7 @@ impl Drk {
         };
 
         // Generate the Money nullifiers Sparse Merkle Tree
-        let store = CacheSmtStorage::new(CacheOverlay::new(&self.cache)?, SLED_MONEY_SMT_TREE);
+        let store = CacheSmtStorage::new(CacheOverlay::new(&self.cache)?, KVDB_MONEY_SMT_TREE);
         let money_null_smt = CacheSmt::new(store, PoseidonFp::new(), &EMPTY_NODES_FP);
 
         // Create the proposal call
@@ -2732,7 +2732,7 @@ impl Drk {
         };
 
         // Generate the Money nullifiers Sparse Merkle Tree
-        let store = CacheSmtStorage::new(CacheOverlay::new(&self.cache)?, SLED_MONEY_SMT_TREE);
+        let store = CacheSmtStorage::new(CacheOverlay::new(&self.cache)?, KVDB_MONEY_SMT_TREE);
         let money_null_smt = CacheSmt::new(store, PoseidonFp::new(), &EMPTY_NODES_FP);
 
         // Create the proposal call

+ 2 - 2
bin/drk/src/lib.rs

@@ -102,8 +102,8 @@ impl Drk {
     ) -> Result<Self> {
         // Initialize blockchain cache database
         let db_path = expand_path(&cache_path)?;
-        let sled_db = sled_overlay::sled::open(&db_path)?;
-        let Ok(cache) = Cache::new(&sled_db) else {
+        let kvdb = kvdb_overlay::Database::open_default(&db_path)?;
+        let Ok(cache) = Cache::new(&kvdb) else {
             return Err(Error::DatabaseError(format!("{}", WalletDbError::InitializationFailed)));
         };
 

+ 4 - 4
bin/drk/src/money.rs

@@ -70,8 +70,8 @@ use crate::{
     Drk,
 };
 
-// Money Merkle tree Sled key
-pub const SLED_MERKLE_TREES_MONEY: &[u8] = b"_money_tree";
+// Money Merkle tree kvdb key
+pub const KVDB_MERKLE_TREES_MONEY: &[u8] = b"_money_tree";
 
 // Wallet SQL table constant names. These have to represent the `money.sql`
 // SQL schema. Table names are prefixed with the contract ID to avoid collisions.
@@ -743,7 +743,7 @@ impl Drk {
     /// Fetch the Money Merkle tree from the cache.
     /// If it doesn't exists a new Merkle Tree is returned.
     pub async fn get_money_tree(&self) -> Result<MerkleTree> {
-        match self.cache.merkle_trees.get(SLED_MERKLE_TREES_MONEY)? {
+        match self.cache.merkle_trees.get(KVDB_MERKLE_TREES_MONEY)? {
             Some(tree_bytes) => Ok(deserialize_async(&tree_bytes).await?),
             None => {
                 let mut tree = MerkleTree::new(u32::MAX as usize);
@@ -1182,7 +1182,7 @@ impl Drk {
     /// Reset the Money Merkle tree in the cache.
     pub fn reset_money_tree(&self, output: &mut Vec<String>) -> WalletDbResult<()> {
         output.push(String::from("Resetting Money Merkle tree"));
-        if let Err(e) = self.cache.merkle_trees.remove(SLED_MERKLE_TREES_MONEY) {
+        if let Err(e) = self.cache.merkle_trees.remove(KVDB_MERKLE_TREES_MONEY) {
             output.push(format!("[reset_money_tree] Resetting Money Merkle tree failed: {e}"));
             return Err(WalletDbError::GenericError)
         }

+ 9 - 9
bin/drk/src/rpc.rs

@@ -51,11 +51,11 @@ use darkfi_sdk::{
 use darkfi_serial::{deserialize_async, serialize_async};
 
 use crate::{
-    cache::{CacheOverlay, CacheSmt, CacheSmtStorage, SLED_MONEY_SMT_TREE},
+    cache::{CacheOverlay, CacheSmt, CacheSmtStorage, KVDB_MONEY_SMT_TREE},
     cli_util::append_or_print,
-    dao::{SLED_MERKLE_TREES_DAO_DAOS, SLED_MERKLE_TREES_DAO_PROPOSALS},
+    dao::{KVDB_MERKLE_TREES_DAO_DAOS, KVDB_MERKLE_TREES_DAO_PROPOSALS},
     error::{WalletDbError, WalletDbResult},
-    money::SLED_MERKLE_TREES_MONEY,
+    money::KVDB_MERKLE_TREES_MONEY,
     Drk, DrkPtr,
 };
 
@@ -124,7 +124,7 @@ impl Drk {
     /// wallet.
     pub async fn scan_cache(&self) -> Result<ScanCache> {
         let money_tree = self.get_money_tree().await?;
-        let smt_store = CacheSmtStorage::new(CacheOverlay::new(&self.cache)?, SLED_MONEY_SMT_TREE);
+        let smt_store = CacheSmtStorage::new(CacheOverlay::new(&self.cache)?, KVDB_MONEY_SMT_TREE);
         let money_smt = CacheSmt::new(smt_store, PoseidonFp::new(), &EMPTY_NODES_FP);
         let mut notes_secrets = self.get_money_secrets().await?;
         let mut owncoins_nullifiers = BTreeMap::new();
@@ -277,13 +277,13 @@ impl Drk {
 
         // Update the merkle trees
         self.cache.insert_merkle_trees(&[
-            (SLED_MERKLE_TREES_MONEY, &scan_cache.money_tree),
-            (SLED_MERKLE_TREES_DAO_DAOS, &scan_cache.dao_daos_tree),
-            (SLED_MERKLE_TREES_DAO_PROPOSALS, &scan_cache.dao_proposals_tree),
+            (KVDB_MERKLE_TREES_MONEY, &scan_cache.money_tree),
+            (KVDB_MERKLE_TREES_DAO_DAOS, &scan_cache.dao_daos_tree),
+            (KVDB_MERKLE_TREES_DAO_PROPOSALS, &scan_cache.dao_proposals_tree),
         ])?;
 
-        // Flush sled
-        self.cache.sled_db.flush()?;
+        // Flush kvdb
+        self.cache.kvdb.flush_default_mode()?;
 
         // Update wallet transactions records
         if let Err(e) =

+ 14 - 12
bin/drk/src/scanned_blocks.rs

@@ -20,16 +20,16 @@ use darkfi_serial::deserialize;
 
 use crate::{
     cache::CacheOverlay,
-    dao::{SLED_MERKLE_TREES_DAO_DAOS, SLED_MERKLE_TREES_DAO_PROPOSALS},
+    dao::{KVDB_MERKLE_TREES_DAO_DAOS, KVDB_MERKLE_TREES_DAO_PROPOSALS},
     error::{WalletDbError, WalletDbResult},
-    money::SLED_MERKLE_TREES_MONEY,
+    money::KVDB_MERKLE_TREES_MONEY,
     Drk,
 };
 
 impl Drk {
     /// Get a scanned block information record.
     pub fn get_scanned_block(&self, height: &u32) -> WalletDbResult<(String, String)> {
-        let Ok(query_result) = self.cache.scanned_blocks.get(height.to_be_bytes()) else {
+        let Ok(query_result) = self.cache.scanned_blocks.get(&height.to_be_bytes()) else {
             return Err(WalletDbError::QueryExecutionFailed);
         };
         let Some(value_bytes) = query_result else {
@@ -49,7 +49,7 @@ impl Drk {
             let Ok((key, value)) = record else {
                 return Err(WalletDbError::QueryExecutionFailed);
             };
-            let key: [u8; 4] = match key.as_ref().try_into() {
+            let key: [u8; 4] = match key.try_into() {
                 Ok(k) => k,
                 Err(_) => return Err(WalletDbError::ParseColumnValueError),
             };
@@ -70,7 +70,7 @@ impl Drk {
             return Err(WalletDbError::QueryExecutionFailed);
         };
         let Some((key, value)) = query_result else { return Ok((0, String::from("-"))) };
-        let key: [u8; 4] = match key.as_ref().try_into() {
+        let key: [u8; 4] = match key.try_into() {
             Ok(k) => k,
             Err(_) => return Err(WalletDbError::ParseColumnValueError),
         };
@@ -177,7 +177,7 @@ impl Drk {
             }
 
             // Remove it
-            if let Err(e) = self.cache.state_inverse_diff.remove(height.to_be_bytes()) {
+            if let Err(e) = self.cache.state_inverse_diff.remove(&height.to_be_bytes()) {
                 output.push(format!(
                     "[reset_to_height] Removing state inverse diff from the cache failed: {e}"
                 ));
@@ -189,17 +189,19 @@ impl Drk {
             dao_daos_tree.rewind();
             dao_proposals_tree.rewind();
             if let Err(e) = self.cache.insert_merkle_trees(&[
-                (SLED_MERKLE_TREES_MONEY, &money_tree),
-                (SLED_MERKLE_TREES_DAO_DAOS, &dao_daos_tree),
-                (SLED_MERKLE_TREES_DAO_PROPOSALS, &dao_proposals_tree),
+                (KVDB_MERKLE_TREES_MONEY, &money_tree),
+                (KVDB_MERKLE_TREES_DAO_DAOS, &dao_daos_tree),
+                (KVDB_MERKLE_TREES_DAO_PROPOSALS, &dao_proposals_tree),
             ]) {
                 output.push(format!("[reset_to_height] Updating merkle trees failed: {e}"));
                 return Err(WalletDbError::GenericError)
             };
 
-            // Flush sled
-            if let Err(e) = self.cache.sled_db.flush() {
-                output.push(format!("[reset_to_height] Flushing cache sled database failed: {e}"));
+            // Flush kvdb
+            if let Err(e) = self.cache.kvdb.flush_default_mode() {
+                output.push(format!(
+                    "[reset_to_height] Flushing cache key-value database failed: {e}"
+                ));
                 return Err(WalletDbError::GenericError)
             }
         }