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

blockchain: nfstore and rootstore stubs.

parazyd 4 лет назад
Родитель
Сommit
dfc9bd24c4
3 измененных файлов с 37 добавлено и 6 удалено
  1. 7 4
      src/blockchain2/mod.rs
  2. 15 1
      src/blockchain2/nfstore.rs
  3. 15 1
      src/blockchain2/rootstore.rs

+ 7 - 4
src/blockchain2/mod.rs

@@ -29,9 +29,10 @@ pub struct Blockchain {
     pub transactions: TxStore,
     /// Streamlet metadata sled tree
     pub streamlet_metadata: StreamletMetadataStore,
-    // TODO:
-    //pub nullifiers: NullifierStore,
-    //pub merkle_roots: RootStore,
+    /// Nullifiers sled tree
+    pub nullifiers: NullifierStore,
+    /// Merkle roots sled tree
+    pub merkle_roots: RootStore,
 }
 
 impl Blockchain {
@@ -39,8 +40,10 @@ impl Blockchain {
         let blocks = BlockStore::new(db, genesis_ts, genesis_data)?;
         let transactions = TxStore::new(db)?;
         let streamlet_metadata = StreamletMetadataStore::new(db)?;
+        let nullifiers = NullifierStore::new(db)?;
+        let merkle_roots = RootStore::new(db)?;
 
-        Ok(Self { blocks, transactions, streamlet_metadata })
+        Ok(Self { blocks, transactions, streamlet_metadata, nullifiers, merkle_roots })
     }
 
     /// Batch insert [`BlockProposal`]s.

+ 15 - 1
src/blockchain2/nfstore.rs

@@ -1,12 +1,26 @@
-use crate::Result;
+use sled::Batch;
+
+use crate::{crypto::nullifier::Nullifier, util::serial::serialize, Result};
 
 const SLED_NULLIFIER_TREE: &[u8] = b"_nullifiers";
 
 pub struct NullifierStore(sled::Tree);
 
 impl NullifierStore {
+    /// Opens a new or existing `NullifierStore` on the given sled database.
     pub fn new(db: &sled::Db) -> Result<Self> {
         let tree = db.open_tree(SLED_NULLIFIER_TREE)?;
         Ok(Self(tree))
     }
+
+    /// Insert a slice of [`Nullifier`] into the nullifier store.
+    pub fn insert(&self, nullifiers: &[Nullifier]) -> Result<()> {
+        let mut batch = Batch::default();
+        for i in nullifiers {
+            batch.insert(serialize(i), vec![] as Vec<u8>);
+        }
+
+        self.0.apply_batch(batch)?;
+        Ok(())
+    }
 }

+ 15 - 1
src/blockchain2/rootstore.rs

@@ -1,12 +1,26 @@
-use crate::Result;
+use sled::Batch;
+
+use crate::{crypto::merkle_node::MerkleNode, util::serial::serialize, Result};
 
 const SLED_ROOTS_TREE: &[u8] = b"_merkleroots";
 
 pub struct RootStore(sled::Tree);
 
 impl RootStore {
+    /// Opens a new or existing `RootStore` on the given sled database.
     pub fn new(db: &sled::Db) -> Result<Self> {
         let tree = db.open_tree(SLED_ROOTS_TREE)?;
         Ok(Self(tree))
     }
+
+    /// Insert a slice of [`MerkleNode`] on the given sled database.
+    pub fn insert(&self, roots: &[MerkleNode]) -> Result<()> {
+        let mut batch = Batch::default();
+        for i in roots {
+            batch.insert(serialize(i), vec![] as Vec<u8>);
+        }
+
+        self.0.apply_batch(batch)?;
+        Ok(())
+    }
 }