Repository: dusk-network/rusk (language: rust)

Issue:
node: add multi get tx feature to Ledger db
#### Summary

To get a list of `SpentTransaction` more easily, we need a function that can get many transactions. This function can assume that all given transaction hashes must be present and return an error otherwise.

This is also required for the Archive node, to be able aggregate larger amounts of historical events and their corresponding transaction hashes with data from the Ledger DB.

#### Possible solution design or implementation

Implement RocksDB multi get for SpentTransactions based on a list of Tx hashes.



Discussion / hints:

Fix patch:
diff --git a/node/CHANGELOG.md b/node/CHANGELOG.md
index e9d9f531e..f2cdb4c3e 100644
--- a/node/CHANGELOG.md
+++ b/node/CHANGELOG.md
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ## [Unreleased]
 
+### Added
+
+- Add `ledger_txs` to `Ledger` trait and Backend implementation [#3491]
+
 ### Fixed
 
 - Change the way the archive synchronizes with the node Acceptor [#3359]
@@ -30,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - First `dusk-node` release
 
 <!-- Issues -->
+[#3491]: https://github.com/dusk-network/rusk/issues/3491
 [#3359]: https://github.com/dusk-network/rusk/issues/3359
 [#3407]: https://github.com/dusk-network/rusk/issues/3407
 [#3405]: https://github.com/dusk-network/rusk/issues/3405
diff --git a/node/src/database.rs b/node/src/database.rs
index 08bc8e1a0..668c24036 100644
--- a/node/src/database.rs
+++ b/node/src/database.rs
@@ -80,6 +80,10 @@ pub trait Ledger {
     fn block_exists(&self, hash: &[u8]) -> Result<bool>;
 
     fn ledger_tx(&self, tx_id: &[u8]) -> Result<Option<SpentTransaction>>;
+    fn ledger_txs(
+        &self,
+        tx_ids: Vec<&[u8; 32]>,
+    ) -> Result<Vec<SpentTransaction>>;
 
     fn ledger_tx_exists(&self, tx_id: &[u8]) -> Result<bool>;
 
diff --git a/node/src/database/rocksdb.rs b/node/src/database/rocksdb.rs
index d8b462ac2..8ea6cc677 100644
--- a/node/src/database/rocksdb.rs
+++ b/node/src/database/rocksdb.rs
@@ -531,6 +531,43 @@ impl<'db, DB: DBAccess> Ledger for DBTransaction<'db, DB> {
         Ok(tx)
     }
 
+    /// Returns a list of transactions from the ledger
+    ///
+    /// This function expects a list of transaction IDs that are in the ledger.
+    ///
+    /// It will return an error if any of the transaction IDs are not found in
+    /// the ledger.
+    fn ledger_txs(
+        &self,
+        tx_ids: Vec<&[u8; 32]>,
+    ) -> Result<Vec<SpentTransaction>> {
+        let cf = self.ledger_txs_cf;
+
+        let ids = tx_ids.into_iter().map(|id| (cf, id)).collect::<Vec<_>>();
+
+        let multi_get_results = self.inner.multi_get_cf(ids);
+
+        let mut spent_transactions =
+            Vec::with_capacity(multi_get_results.len());
+        for result in multi_get_results.into_iter() {
+            let opt_blob = result.map_err(|e| {
+                std::io::Error::new(std::io::ErrorKind::Other, e)
+            })?;
+
+            let Some(blob) = opt_blob else {
+                return Err(anyhow::anyhow!(
+                    "At least one Transaction ID was not found"
+                ));
+            };
+
+            let stx = SpentTransaction::read(&mut &blob[..])?;
+
+            spent_transactions.push(stx);
+        }
+
+        Ok(spent_transactions)
+    }
+
     /// Returns true if the transaction exists in the
     /// ledger
     ///


Test patch: