Răsfoiți Sursa

blockchain: Add get_all functions.

parazyd 4 ani în urmă
părinte
comite
67adc758f4

+ 11 - 61
src/blockchain2/blockstore.rs

@@ -65,68 +65,18 @@ impl BlockStore {
         Ok(self.0.contains_key(blockhash.as_bytes())?)
     }
 
-    /*
-    /// Fetch the first block in the tree, based on the Ord implementation for Vec<u8>.
-    pub fn get_first(&self) -> Result<Option<(blake3::Hash, Block)>> {
-        if let Some(found) = self.0.first()? {
-            let hash_bytes: [u8; 32] = found.0.as_ref().try_into().unwrap();
-            let block = deserialize(&found.1)?;
-            return Ok(Some((hash_bytes.into(), block)))
+    /// Retrieve all blocks.
+    /// Be careful as this will try to load everything in memory.
+    pub fn get_all(&self) -> Result<Vec<Option<(blake3::Hash, Block)>>> {
+        let mut blocks = vec![];
+        let mut iterator = self.0.into_iter().enumerate();
+        while let Some((_, r)) = iterator.next() {
+            let (k, v) = r.unwrap();
+            let hash_bytes: [u8; 32] = k.as_ref().try_into().unwrap();
+            let block = deserialize(&v)?;
+            blocks.push(Some((hash_bytes.into(), block)));
         }
 
-        Ok(None)
+        Ok(blocks)
     }
-
-    /// Fetch the last block in the tree, based on the Ord implementation for Vec<u8>.
-    pub fn get_last(&self) -> Result<Option<(blake3::Hash, Block)>> {
-        if let Some(found) = self.0.last()? {
-            let hash_bytes: [u8; 32] = found.0.as_ref().try_into().unwrap();
-            let block = deserialize(&found.1)?;
-            return Ok(Some((hash_bytes.into(), block)))
-        }
-
-        Ok(None)
-    }
-
-    /// Fetch the block and its hash before the provided blockhash, if one exists.
-    pub fn get_lt(&self, blockhash: blake3::Hash) -> Result<Option<(blake3::Hash, Block)>> {
-        if let Some(found) = self.0.get_lt(blockhash.as_bytes())? {
-            let hash_bytes: [u8; 32] = found.0.as_ref().try_into().unwrap();
-            let block = deserialize(&found.1)?;
-            return Ok(Some((hash_bytes.into(), block)))
-        }
-
-        Ok(None)
-    }
-
-    /// Fetch the block and its hash after the provided blockhash, if one exists.
-    pub fn get_gt(&self, blockhash: blake3::Hash) -> Result<Option<(blake3::Hash, Block)>> {
-        if let Some(found) = self.0.get_gt(blockhash.as_bytes())? {
-            let hash_bytes: [u8; 32] = found.0.as_ref().try_into().unwrap();
-            let block = deserialize(&found.1)?;
-            return Ok(Some((hash_bytes.into(), block)))
-        }
-
-        Ok(None)
-    }
-
-    /// Retrieve an iterator over a range of blockhashes.
-    /// When iterating, take care of potential memory limitations if you're
-    /// storing results in memory. For blockchain sync, it should probably
-    /// be done in chunks.
-    // Usage:
-    // ```
-    // let mut r = get_range(foo, bar);
-    // while let Some((k, v)) = r.next() {
-    //     let hash_bytes: [u8; 32] = k.as_ref().try_into().unwrap();
-    //     let block = deserialize(&v)?;
-    // }
-    // ```
-    pub fn get_range(&self, start: blake3::Hash, end: blake3::Hash) -> sled::Iter {
-        let start: &[u8] = start.as_bytes().as_ref();
-        let end: &[u8] = end.as_bytes().as_ref();
-
-        self.0.range(start..end)
-    }
-    */
 }

+ 20 - 1
src/blockchain2/metadatastore.rs

@@ -1,6 +1,10 @@
 use sled::Batch;
 
-use crate::{consensus2::StreamletMetadata, util::serial::serialize, Result};
+use crate::{
+    consensus2::StreamletMetadata,
+    util::serial::{deserialize, serialize},
+    Result,
+};
 
 const SLED_STREAMLET_METADATA_TREE: &[u8] = b"_streamlet_metadata";
 
@@ -26,4 +30,19 @@ impl StreamletMetadataStore {
         self.0.apply_batch(batch)?;
         Ok(())
     }
+
+    /// Retrieve all `StreamletMetadata`.
+    /// Be careful as this will try to lead everything in memory.
+    pub fn get_all(&self) -> Result<Vec<Option<(blake3::Hash, StreamletMetadata)>>> {
+        let mut metadata = vec![];
+        let mut iterator = self.0.into_iter().enumerate();
+        while let Some((_, r)) = iterator.next() {
+            let (k, v) = r.unwrap();
+            let hash_bytes: [u8; 32] = k.as_ref().try_into().unwrap();
+            let m = deserialize(&v)?;
+            metadata.push(Some((hash_bytes.into(), m)));
+        }
+
+        Ok(metadata)
+    }
 }

+ 19 - 1
src/blockchain2/nfstore.rs

@@ -1,6 +1,10 @@
 use sled::Batch;
 
-use crate::{crypto::nullifier::Nullifier, util::serial::serialize, Result};
+use crate::{
+    crypto::nullifier::Nullifier,
+    util::serial::{deserialize, serialize},
+    Result,
+};
 
 const SLED_NULLIFIER_TREE: &[u8] = b"_nullifiers";
 
@@ -23,4 +27,18 @@ impl NullifierStore {
         self.0.apply_batch(batch)?;
         Ok(())
     }
+
+    /// Retrieve all nullifiers.
+    /// Be careful as this will try to load everything im memory.
+    pub fn get_all(&self) -> Result<Vec<Option<Nullifier>>> {
+        let mut nfs = vec![];
+        let mut iterator = self.0.into_iter().enumerate();
+        while let Some((_, r)) = iterator.next() {
+            let (k, _) = r.unwrap();
+            let nullifier = deserialize(&k)?;
+            nfs.push(Some(nullifier))
+        }
+
+        Ok(nfs)
+    }
 }

+ 19 - 1
src/blockchain2/rootstore.rs

@@ -1,6 +1,10 @@
 use sled::Batch;
 
-use crate::{crypto::merkle_node::MerkleNode, util::serial::serialize, Result};
+use crate::{
+    crypto::merkle_node::MerkleNode,
+    util::serial::{deserialize, serialize},
+    Result,
+};
 
 const SLED_ROOTS_TREE: &[u8] = b"_merkleroots";
 
@@ -23,4 +27,18 @@ impl RootStore {
         self.0.apply_batch(batch)?;
         Ok(())
     }
+
+    /// Retrieve all merkle roots.
+    /// Be careful as this will try to load everything in memory.
+    pub fn get_all(&self) -> Result<Vec<Option<MerkleNode>>> {
+        let mut roots = vec![];
+        let mut iterator = self.0.into_iter().enumerate();
+        while let Some((_, r)) = iterator.next() {
+            let (k, _) = r.unwrap();
+            let root = deserialize(&k)?;
+            roots.push(Some(root));
+        }
+
+        Ok(roots)
+    }
 }

+ 20 - 1
src/blockchain2/txstore.rs

@@ -1,6 +1,10 @@
 use sled::Batch;
 
-use crate::{consensus2::Tx, util::serial::serialize, Result};
+use crate::{
+    consensus2::Tx,
+    util::serial::{deserialize, serialize},
+    Result,
+};
 
 const SLED_TX_TREE: &[u8] = b"_transactions";
 
@@ -30,4 +34,19 @@ impl TxStore {
         self.0.apply_batch(batch)?;
         Ok(ret)
     }
+
+    /// Retrieve all transactions.
+    /// Be careful as this will try to load everything in memory.
+    pub fn get_all(&self) -> Result<Vec<Option<(blake3::Hash, Tx)>>> {
+        let mut txs = vec![];
+        let mut iterator = self.0.into_iter().enumerate();
+        while let Some((_, r)) = iterator.next() {
+            let (k, v) = r.unwrap();
+            let hash_bytes: [u8; 32] = k.as_ref().try_into().unwrap();
+            let tx = deserialize(&v)?;
+            txs.push(Some((hash_bytes.into(), tx)));
+        }
+
+        Ok(txs)
+    }
 }