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

blockchain: removed redundant metadatastore

aggstam 3 лет назад
Родитель
Сommit
2707d7f306
2 измененных файлов с 6 добавлено и 128 удалено
  1. 0 117
      src/blockchain/metadatastore.rs
  2. 6 11
      src/blockchain/mod.rs

+ 0 - 117
src/blockchain/metadatastore.rs

@@ -1,117 +0,0 @@
-/* This file is part of DarkFi (https://dark.fi)
- *
- * Copyright (C) 2020-2022 Dyne.org foundation
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <https://www.gnu.org/licenses/>.
- */
-
-use darkfi_serial::{deserialize, serialize};
-
-use crate::{
-    consensus::{Block, Metadata},
-    util::time::Timestamp,
-    Error, Result,
-};
-
-const SLED_METADATA_TREE: &[u8] = b"_metadata";
-
-/// The `MetadataStore` is a `sled` tree storing all the blockchain's
-/// blocks' metadata used by the Streamlet consensus protocol, where the key
-/// is the block's headers' hash, and the value is the serialized metadata.
-#[derive(Clone)]
-pub struct MetadataStore(sled::Tree);
-
-impl MetadataStore {
-    /// Opens a new or existing `OuroborosMetadataStore` on the given sled database.
-    pub fn new(db: &sled::Db, genesis_ts: Timestamp, genesis_data: blake3::Hash) -> Result<Self> {
-        let tree = db.open_tree(SLED_METADATA_TREE)?;
-        let store = Self(tree);
-
-        // In case the store is empty, initialize it with the genesis block.
-        if store.0.is_empty() {
-            let genesis_block = Block::genesis_block(genesis_ts, genesis_data);
-            let genesis_hash = blake3::hash(&serialize(&genesis_block));
-
-            store.insert(&[genesis_hash], &[genesis_block.metadata])?;
-        }
-
-        Ok(store)
-    }
-
-    /// Insert a slice of blockhashes and respective metadata into the store.
-    /// With sled, the operation is done as a batch.
-    /// The block hash is used as the key, and the metadata is used as value.
-    pub fn insert(&self, hashes: &[blake3::Hash], metadatas: &[Metadata]) -> Result<()> {
-        assert_eq!(hashes.len(), metadatas.len());
-        let mut batch = sled::Batch::default();
-
-        for (i, hash) in hashes.iter().enumerate() {
-            batch.insert(hash.as_bytes(), serialize(&metadatas[i]));
-        }
-
-        self.0.apply_batch(batch)?;
-        Ok(())
-    }
-
-    /// Check if the metadata store contains a given block hash
-    pub fn contains(&self, hash: &blake3::Hash) -> Result<bool> {
-        Ok(self.0.contains_key(hash.as_bytes())?)
-    }
-
-    /// Fetch given blockhashes metadata from the store. The resulting vector contains
-    /// `Option`, which is `Some` if the metadata was found in the metadatastore, and
-    /// otherwise it is `None`, if it has not. The second parameter is a boolean
-    /// which tells the function to fail in case at least one blocks' metadata was not
-    /// found.
-    pub fn get(&self, hashes: &[blake3::Hash], strict: bool) -> Result<Vec<Option<Metadata>>> {
-        let mut ret = Vec::with_capacity(hashes.len());
-
-        for hash in hashes {
-            if let Some(found) = self.0.get(hash.as_bytes())? {
-                let sm = deserialize(&found)?;
-                ret.push(Some(sm));
-            } else {
-                if strict {
-                    let s = hash.to_hex().as_str().to_string();
-                    return Err(Error::BlockMetadataNotFound(s))
-                }
-                ret.push(None);
-            }
-        }
-
-        Ok(ret)
-    }
-
-    /// Retrieve all metadata from the store in the form of a tuple
-    /// (`hash`, `metadata`).
-    /// Be careful as this will try to load everything in memory.
-    pub fn get_all(&self) -> Result<Vec<(blake3::Hash, Metadata)>> {
-        let mut hashes = vec![];
-
-        for hash in self.0.iter() {
-            let (key, value) = hash.unwrap();
-            let hash_bytes: [u8; 32] = key.as_ref().try_into().unwrap();
-            let m = deserialize(&value)?;
-            hashes.push((hash_bytes.into(), m));
-        }
-
-        Ok(hashes)
-    }
-
-    /// Retrive last key/val
-    pub fn get_last(&self) -> Result<(blake3::Hash, Metadata)> {
-        let all = self.get_all().unwrap();
-        Ok(all[all.len() - 1].clone())
-    }
-}

+ 6 - 11
src/blockchain/mod.rs

@@ -16,6 +16,7 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+use darkfi_serial::serialize;
 use log::debug;
 
 use crate::{
@@ -27,9 +28,6 @@ use crate::{
 pub mod blockstore;
 pub use blockstore::{BlockOrderStore, BlockStore, HeaderStore};
 
-pub mod metadatastore;
-pub use metadatastore::MetadataStore;
-
 pub mod nfstore;
 pub use nfstore::NullifierStore;
 
@@ -52,8 +50,6 @@ pub struct Blockchain {
     pub order: BlockOrderStore,
     /// Transactions sled tree
     pub transactions: TxStore,
-    /// Metadata sled tree
-    pub metadata: MetadataStore,
     /// Nullifiers sled tree
     pub nullifiers: NullifierStore,
     /// Merkle roots sled tree
@@ -68,12 +64,11 @@ impl Blockchain {
         let headers = HeaderStore::new(db, genesis_ts, genesis_data)?;
         let blocks = BlockStore::new(db, genesis_ts, genesis_data)?;
         let order = BlockOrderStore::new(db, genesis_ts, genesis_data)?;
-        let metadata = MetadataStore::new(db, genesis_ts, genesis_data)?;
         let transactions = TxStore::new(db)?;
         let nullifiers = NullifierStore::new(db)?;
         let merkle_roots = RootStore::new(db)?;
 
-        Ok(Self { headers, blocks, order, transactions, metadata, nullifiers, merkle_roots })
+        Ok(Self { headers, blocks, order, transactions, nullifiers, merkle_roots })
     }
 
     /// Insert a given slice of [`BlockInfo`] into the blockchain database.
@@ -101,9 +96,6 @@ impl Blockchain {
             // Store block order
             self.order.insert(&[block.header.slot], &[headerhash[0]])?;
 
-            // Store ouroboros metadata
-            self.metadata.insert(&[headerhash[0]], &[block.metadata.clone()])?;
-
             // NOTE: The nullifiers and Merkle roots are applied in the state
             // transition apply function.
         }
@@ -170,8 +162,11 @@ impl Blockchain {
         self.order.get_last()
     }
 
+    /// Retrieve last finalized block leader proof hash.
     pub fn get_last_proof_hash(&self) -> Result<blake3::Hash> {
-        let (hash, _) = self.metadata.get_last().unwrap();
+        let (slot, _) = self.last().unwrap();
+        let block = &self.get_blocks_by_slot(&vec![slot]).unwrap()[0];
+        let hash = blake3::hash(&serialize(&block.metadata.proof));
         Ok(hash)
     }
 }