diff --git a/src/blockchain2/mod.rs b/src/blockchain2/mod.rs index fdd66a825..e3a43134e 100644 --- a/src/blockchain2/mod.rs +++ b/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. diff --git a/src/blockchain2/nfstore.rs b/src/blockchain2/nfstore.rs index 8dd73cc7c..92b7d3b57 100644 --- a/src/blockchain2/nfstore.rs +++ b/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 { 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); + } + + self.0.apply_batch(batch)?; + Ok(()) + } } diff --git a/src/blockchain2/rootstore.rs b/src/blockchain2/rootstore.rs index 9441c0ccc..ac9618bf1 100644 --- a/src/blockchain2/rootstore.rs +++ b/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 { 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); + } + + self.0.apply_batch(batch)?; + Ok(()) + } }