Procházet zdrojové kódy

blockchain: renamed stores

aggstam před 3 roky
rodič
revize
1645f3f27d

+ 0 - 3
bin/darkfid/src/main.rs

@@ -214,9 +214,6 @@ impl RequestHandler for Darkfid {
             Some("blockchain.last_known_slot") => {
                 return self.blockchain_last_known_slot(req.id, params).await
             }
-            Some("blockchain.merkle_roots") => {
-                return self.blockchain_merkle_roots(req.id, params).await
-            }
             Some("blockchain.subscribe_blocks") => {
                 return self.blockchain_subscribe_blocks(req.id, params).await
             }

+ 1 - 32
bin/darkfid/src/rpc_blockchain.rs

@@ -16,10 +16,7 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-use darkfi_sdk::{
-    crypto::{ContractId, MerkleNode},
-    db::SMART_CONTRACT_ZKAS_DB_NAME,
-};
+use darkfi_sdk::{crypto::ContractId, db::SMART_CONTRACT_ZKAS_DB_NAME};
 use darkfi_serial::{deserialize, serialize};
 use log::{debug, error};
 use serde_json::{json, Value};
@@ -83,34 +80,6 @@ impl Darkfid {
         JsonResponse::new(json!(last_slot.0), id).into()
     }
 
-    // RPCAPI:
-    // Queries the blockchain database for all available merkle roots.
-    //
-    // --> {"jsonrpc": "2.0", "method": "blockchain.merkle_roots", "params": [], "id": 1}
-    // <-- {"jsonrpc": "2.0", "result": [..., ..., ...], "id": 1}
-    pub async fn blockchain_merkle_roots(&self, id: Value, params: &[Value]) -> JsonResult {
-        if !params.is_empty() {
-            return JsonError::new(InvalidParams, None, id).into()
-        }
-
-        let validator_state = self.validator_state.read().await;
-
-        let roots: Vec<MerkleNode> = match validator_state.blockchain.merkle_roots.get_all() {
-            Ok(v) => {
-                drop(validator_state);
-                v
-            }
-            Err(e) => {
-                error!("[RPC] blockchain.merkle_roots: Failed fetching merkle roots from rootstore: {}", e);
-                return JsonError::new(InternalError, None, id).into()
-            }
-        };
-
-        let roots: Vec<String> = roots.iter().map(|x| x.to_string()).collect();
-
-        JsonResponse::new(json!(roots), id).into()
-    }
-
     // RPCAPI:
     // Initializes a subscription to new incoming blocks.
     // Once a subscription is established, `darkfid` will send JSON-RPC notifications of

+ 3 - 3
script/research/nodes-tool/src/main.rs

@@ -20,9 +20,9 @@ use std::{fs::File, io::Write};
 
 use darkfi::{
     blockchain::{
-        blockstore::{BlockOrderStore, BlockStore, HeaderStore},
-        slotcheckpointstore::SlotCheckpointStore,
-        txstore::TxStore,
+        block_store::{BlockOrderStore, BlockStore, HeaderStore},
+        slot_checkpoint_store::SlotCheckpointStore,
+        tx_store::TxStore,
         Blockchain,
     },
     consensus::{

+ 0 - 0
src/blockchain/blockstore.rs → src/blockchain/block_store.rs


+ 0 - 0
src/blockchain/contractstore.rs → src/blockchain/contract_store.rs


+ 8 - 22
src/blockchain/mod.rs

@@ -25,23 +25,17 @@ use crate::{
     Error, Result,
 };
 
-pub mod blockstore;
-pub use blockstore::{BlockOrderStore, BlockStore, HeaderStore};
+pub mod block_store;
+pub use block_store::{BlockOrderStore, BlockStore, HeaderStore};
 
-pub mod slotcheckpointstore;
-pub use slotcheckpointstore::SlotCheckpointStore;
+pub mod slot_checkpoint_store;
+pub use slot_checkpoint_store::SlotCheckpointStore;
 
-pub mod nfstore;
-pub use nfstore::NullifierStore;
+pub mod tx_store;
+pub use tx_store::TxStore;
 
-pub mod rootstore;
-pub use rootstore::RootStore;
-
-pub mod txstore;
-pub use txstore::TxStore;
-
-pub mod contractstore;
-pub use contractstore::{ContractStateStore, WasmStore};
+pub mod contract_store;
+pub use contract_store::{ContractStateStore, WasmStore};
 
 /// Structure holding all sled trees that define the concept of Blockchain.
 #[derive(Clone)]
@@ -58,10 +52,6 @@ pub struct Blockchain {
     pub slot_checkpoints: SlotCheckpointStore,
     /// Transactions sled tree
     pub transactions: TxStore,
-    /// Nullifiers sled tree
-    pub nullifiers: NullifierStore,
-    /// Merkle roots sled tree
-    pub merkle_roots: RootStore,
     /// Contract states
     pub contracts: ContractStateStore,
     /// Wasm bincodes
@@ -76,8 +66,6 @@ impl Blockchain {
         let order = BlockOrderStore::new(db, genesis_ts, genesis_data)?;
         let slot_checkpoints = SlotCheckpointStore::new(db)?;
         let transactions = TxStore::new(db)?;
-        let nullifiers = NullifierStore::new(db)?;
-        let merkle_roots = RootStore::new(db)?;
         let contracts = ContractStateStore::new(db)?;
         let wasm_bincode = WasmStore::new(db)?;
 
@@ -88,8 +76,6 @@ impl Blockchain {
             order,
             slot_checkpoints,
             transactions,
-            nullifiers,
-            merkle_roots,
             contracts,
             wasm_bincode,
         })

+ 0 - 72
src/blockchain/nfstore.rs

@@ -1,72 +0,0 @@
-/* This file is part of DarkFi (https://dark.fi)
- *
- * Copyright (C) 2020-2023 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_sdk::crypto::Nullifier;
-use darkfi_serial::{deserialize, serialize};
-
-use crate::Result;
-
-const SLED_NULLIFIER_TREE: &[u8] = b"_nullifiers";
-
-/// The `NullifierStore` is a `sled` tree storing all the nullifiers seen
-/// in existing blocks. The key is the nullifier itself, while the value
-/// is an empty vector that's not used. As a sidenote, perhaps we could
-/// hold the transaction hash where the nullifier was seen in the value.
-#[derive(Clone)]
-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 store. With sled, the
-    /// operation is done as a batch. The nullifier is used as a key,
-    /// while the value is an empty vector.
-    pub fn insert(&self, nfs: &[Nullifier]) -> Result<()> {
-        let mut batch = sled::Batch::default();
-
-        for nf in nfs {
-            batch.insert(serialize(nf), vec![] as Vec<u8>);
-        }
-
-        self.0.apply_batch(batch)?;
-        Ok(())
-    }
-
-    /// Check if the nullifierstore contains a given nullifier.
-    pub fn contains(&self, nullifier: &Nullifier) -> Result<bool> {
-        Ok(self.0.contains_key(serialize(nullifier))?)
-    }
-
-    /// Retrieve all nullifiers from the store.
-    /// Be careful as this will try to load everything in memory.
-    pub fn get_all(&self) -> Result<Vec<Nullifier>> {
-        let mut nullifiers = vec![];
-
-        for nullifier in self.0.iter() {
-            let (key, _) = nullifier.unwrap();
-            let nullifier = deserialize(&key)?;
-            nullifiers.push(nullifier);
-        }
-
-        Ok(nullifiers)
-    }
-}

+ 0 - 71
src/blockchain/rootstore.rs

@@ -1,71 +0,0 @@
-/* This file is part of DarkFi (https://dark.fi)
- *
- * Copyright (C) 2020-2023 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_sdk::crypto::MerkleNode;
-use darkfi_serial::{deserialize, serialize};
-
-use crate::Result;
-
-const SLED_ROOTS_TREE: &[u8] = b"_merkleroots";
-
-/// The `RootStore` is a `sled` tree storing all the Merkle roots seen
-/// in existing blocks. The key is the Merkle root itself, while the value
-/// is an empty vector that's not used.
-#[derive(Clone)]
-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`] into the store. With sled, the
-    /// operation is done as a batch. The Merkle root is used as a key,
-    /// while the value is an empty vector.
-    pub fn insert(&self, roots: &[MerkleNode]) -> Result<()> {
-        let mut batch = sled::Batch::default();
-
-        for root in roots {
-            batch.insert(serialize(root), vec![] as Vec<u8>);
-        }
-
-        self.0.apply_batch(batch)?;
-        Ok(())
-    }
-
-    /// Check if the rootstore contains a given Merkle root.
-    pub fn contains(&self, root: &MerkleNode) -> Result<bool> {
-        Ok(self.0.contains_key(serialize(root))?)
-    }
-
-    /// Retrieve all Merkle roots from the store.
-    /// Be careful as this will try to load everything in memory.
-    pub fn get_all(&self) -> Result<Vec<MerkleNode>> {
-        let mut roots = vec![];
-
-        for root in self.0.iter() {
-            let (key, _) = root.unwrap();
-            let root = deserialize(&key)?;
-            roots.push(root);
-        }
-
-        Ok(roots)
-    }
-}

+ 0 - 0
src/blockchain/slotcheckpointstore.rs → src/blockchain/slot_checkpoint_store.rs


+ 0 - 0
src/blockchain/txstore.rs → src/blockchain/tx_store.rs


+ 5 - 5
src/consensus/leadcoin.rs → src/consensus/lead_coin.rs

@@ -47,7 +47,7 @@ use std::{
     io::{prelude::*, BufWriter},
 };
 
-pub const MERKLE_DEPTH_LEADCOIN: usize = 32;
+pub const MERKLE_DEPTH_LEAD_COIN: usize = 32;
 pub const MERKLE_DEPTH: u8 = 32;
 pub const ZERO: pallas::Base = pallas::Base::zero();
 pub const ONE: pallas::Base = pallas::Base::one();
@@ -74,7 +74,7 @@ pub struct LeadCoin {
     /// Coin commitment position
     pub coin1_commitment_pos: u32,
     /// Merkle path to the coin1's commitment
-    pub coin1_commitment_merkle_path: [MerkleNode; MERKLE_DEPTH_LEADCOIN],
+    pub coin1_commitment_merkle_path: [MerkleNode; MERKLE_DEPTH_LEAD_COIN],
     /// coin1 sk
     pub coin1_sk: pallas::Base,
     /// Merkle root of the `coin1` secret key
@@ -82,7 +82,7 @@ pub struct LeadCoin {
     /// coin1 sk position in merkle tree
     pub coin1_sk_pos: u32,
     /// Merkle path to the secret key of `coin1`
-    pub coin1_sk_merkle_path: [MerkleNode; MERKLE_DEPTH_LEADCOIN],
+    pub coin1_sk_merkle_path: [MerkleNode; MERKLE_DEPTH_LEAD_COIN],
     /// coin1 commitment blinding factor
     pub coin1_blind: pallas::Scalar,
 }
@@ -104,7 +104,7 @@ impl LeadCoin {
         // sk pos
         coin1_sk_pos: usize,
         // Merkle path to the secret key of `coin_1` in the Merkle tree of secret keys
-        coin1_sk_merkle_path: [MerkleNode; MERKLE_DEPTH_LEADCOIN],
+        coin1_sk_merkle_path: [MerkleNode; MERKLE_DEPTH_LEAD_COIN],
         // coin1 nonce
         seed: pallas::Base,
         // Merkle tree of coin commitments
@@ -488,7 +488,7 @@ impl LeadCoin {
 pub struct LeadCoinSecrets {
     pub secret_keys: Vec<SecretKey>,
     pub merkle_roots: Vec<MerkleNode>,
-    pub merkle_paths: Vec<[MerkleNode; MERKLE_DEPTH_LEADCOIN]>,
+    pub merkle_paths: Vec<[MerkleNode; MERKLE_DEPTH_LEAD_COIN]>,
 }
 
 impl LeadCoinSecrets {

+ 2 - 2
src/consensus/mod.rs

@@ -50,8 +50,8 @@ pub mod clock;
 pub use clock::{Clock, Ticks};
 
 /// Consensus participation coin functions and definitions
-pub mod leadcoin;
-pub use leadcoin::LeadCoin;
+pub mod lead_coin;
+pub use lead_coin::LeadCoin;
 
 /// Utility types
 pub mod types;

+ 1 - 1
src/consensus/rcpt.rs

@@ -30,7 +30,7 @@ use rand::rngs::OsRng;
 
 use crate::Error;
 
-/// transfered leadcoin is rcpt into two coins,
+/// transfered lead coin is rcpt into two coins,
 /// first coin is transfered rcpt coin.
 /// second coin is the change returning to sender, or different address.
 #[derive(Debug, Clone, Copy, Eq, PartialEq, SerialEncodable, SerialDecodable)]

+ 1 - 1
src/consensus/state.rs

@@ -30,7 +30,7 @@ use rand::{thread_rng, Rng};
 
 use super::{
     constants,
-    leadcoin::{LeadCoin, LeadCoinSecrets},
+    lead_coin::{LeadCoin, LeadCoinSecrets},
     utils::fbig2base,
     Block, BlockProposal, Float10,
 };

+ 1 - 1
src/consensus/validator.rs

@@ -37,7 +37,7 @@ use serde_json::json;
 
 use super::{
     constants,
-    leadcoin::LeadCoin,
+    lead_coin::LeadCoin,
     state::{ConsensusState, Fork, SlotCheckpoint, StateCheckpoint},
     BlockInfo, BlockProposal, Header, LeadInfo, LeadProof,
 };