Эх сурвалжийг харах

blockchain: updated to sled-overlay 0.1.2

skoupidi 2 жил өмнө
parent
commit
490084b26d

+ 2 - 2
Cargo.lock

@@ -6405,9 +6405,9 @@ dependencies = [
 
 [[package]]
 name = "sled-overlay"
-version = "0.1.1"
+version = "0.1.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "add8d336179fb379f1e4a1afac52b2fc4e0e594df3ee257d52bdb3febc3cf4c7"
+checksum = "1cb251f5f64d4d645399c8cdf53b7bc3134051b541a5662b9435366a433a50f7"
 dependencies = [
  "sled",
 ]

+ 1 - 1
Cargo.toml

@@ -119,7 +119,7 @@ libsqlite3-sys = {version = "0.28.0", features = ["sqlcipher"], optional = true}
 
 # Blockchain store
 sled = {version = "0.34.7", optional = true}
-sled-overlay = {version = "0.1.1", optional = true}
+sled-overlay = {version = "0.1.2", optional = true}
 
 # Miner
 randomx = {git = "https://github.com/darkrenaissance/RandomX", optional = true}

+ 6 - 6
bin/darkfid/src/task/garbage_collect.rs

@@ -65,7 +65,7 @@ pub async fn garbage_collect_task(node: Arc<Darkfid>) -> Result<()> {
                             target: "darkfid::task::garbage_collect_task",
                             "Overlay full clone creation failed: {e}"
                         );
-                        break
+                        return Err(e)
                     }
                 };
 
@@ -78,7 +78,7 @@ pub async fn garbage_collect_task(node: Arc<Darkfid>) -> Result<()> {
                                 target: "darkfid::task::garbage_collect_task",
                                 "Proposal transactions retrieval failed: {e}"
                             );
-                            break
+                            return Err(e)
                         }
                     };
 
@@ -95,7 +95,7 @@ pub async fn garbage_collect_task(node: Arc<Darkfid>) -> Result<()> {
                             target: "darkfid::task::garbage_collect_task",
                             "Next fork block height retrieval failed: {e}"
                         );
-                        break
+                        return Err(e)
                     }
                 };
 
@@ -124,6 +124,9 @@ pub async fn garbage_collect_task(node: Arc<Darkfid>) -> Result<()> {
                 }
             }
 
+            // Drop forks lock
+            drop(forks);
+
             // Remove transaction if its invalid for all the forks
             if !valid {
                 debug!(target: "darkfid::task::garbage_collect_task", "Removing invalid transaction: {tx_hash}");
@@ -134,9 +137,6 @@ pub async fn garbage_collect_task(node: Arc<Darkfid>) -> Result<()> {
                     );
                 };
             }
-
-            // Drop forks lock
-            drop(forks);
         }
         (last_checked, txs) =
             match node.validator.blockchain.transactions.get_after_pending(last_checked, TXS_CAP) {

+ 6 - 6
src/blockchain/block_store.rs

@@ -284,9 +284,9 @@ impl darkfi_serial::Decodable for BlockDifficulty {
     }
 }
 
-const SLED_BLOCK_TREE: &[u8] = b"_blocks";
-const SLED_BLOCK_ORDER_TREE: &[u8] = b"_block_order";
-const SLED_BLOCK_DIFFICULTY_TREE: &[u8] = b"_block_difficulty";
+pub const SLED_BLOCK_TREE: &[u8] = b"_blocks";
+pub const SLED_BLOCK_ORDER_TREE: &[u8] = b"_block_order";
+pub const SLED_BLOCK_DIFFICULTY_TREE: &[u8] = b"_block_difficulty";
 
 /// The `BlockStore` is a structure representing all `sled` trees related
 /// to storing the blockchain's blocks information.
@@ -584,9 +584,9 @@ pub struct BlockStoreOverlay(SledDbOverlayPtr);
 
 impl BlockStoreOverlay {
     pub fn new(overlay: &SledDbOverlayPtr) -> Result<Self> {
-        overlay.lock().unwrap().open_tree(SLED_BLOCK_TREE)?;
-        overlay.lock().unwrap().open_tree(SLED_BLOCK_ORDER_TREE)?;
-        overlay.lock().unwrap().open_tree(SLED_BLOCK_DIFFICULTY_TREE)?;
+        overlay.lock().unwrap().open_tree(SLED_BLOCK_TREE, true)?;
+        overlay.lock().unwrap().open_tree(SLED_BLOCK_ORDER_TREE, true)?;
+        overlay.lock().unwrap().open_tree(SLED_BLOCK_DIFFICULTY_TREE, true)?;
         Ok(Self(overlay.clone()))
     }
 

+ 6 - 6
src/blockchain/contract_store.rs

@@ -30,8 +30,8 @@ use crate::{
 
 use super::{parse_record, SledDbOverlayPtr};
 
-const SLED_CONTRACTS_TREE: &[u8] = b"_contracts";
-const SLED_BINCODE_TREE: &[u8] = b"_wasm_bincode";
+pub const SLED_CONTRACTS_TREE: &[u8] = b"_contracts";
+pub const SLED_BINCODE_TREE: &[u8] = b"_wasm_bincode";
 
 /// The hardcoded db name for the zkas circuits database tree
 pub const SMART_CONTRACT_ZKAS_DB_NAME: &str = "_zkas";
@@ -215,8 +215,8 @@ pub struct ContractStoreOverlay(SledDbOverlayPtr);
 
 impl ContractStoreOverlay {
     pub fn new(overlay: &SledDbOverlayPtr) -> Result<Self> {
-        overlay.lock().unwrap().open_tree(SLED_BINCODE_TREE)?;
-        overlay.lock().unwrap().open_tree(SLED_CONTRACTS_TREE)?;
+        overlay.lock().unwrap().open_tree(SLED_BINCODE_TREE, true)?;
+        overlay.lock().unwrap().open_tree(SLED_CONTRACTS_TREE, true)?;
         Ok(Self(overlay.clone()))
     }
 
@@ -280,7 +280,7 @@ impl ContractStoreOverlay {
         // Now we add it so it's marked as initialized and create its tree.
         state_pointers.push(ptr);
         lock.insert(SLED_CONTRACTS_TREE, &contract_id_bytes, &serialize(&state_pointers))?;
-        lock.open_tree(&ptr)?;
+        lock.open_tree(&ptr, false)?;
 
         Ok(ptr)
     }
@@ -311,7 +311,7 @@ impl ContractStoreOverlay {
         }
 
         // We open the tree and return its handle
-        lock.open_tree(&ptr)?;
+        lock.open_tree(&ptr, false)?;
         Ok(ptr)
     }
 

+ 3 - 3
src/blockchain/header_store.rs

@@ -115,8 +115,8 @@ impl Default for Header {
     }
 }
 
-const SLED_HEADER_TREE: &[u8] = b"_headers";
-const SLED_SYNC_HEADER_TREE: &[u8] = b"_sync_headers";
+pub const SLED_HEADER_TREE: &[u8] = b"_headers";
+pub const SLED_SYNC_HEADER_TREE: &[u8] = b"_sync_headers";
 
 /// The `HeaderStore` is a structure representing all `sled` trees related
 /// to storing the blockchain's blocks's header information.
@@ -324,7 +324,7 @@ pub struct HeaderStoreOverlay(SledDbOverlayPtr);
 
 impl HeaderStoreOverlay {
     pub fn new(overlay: &SledDbOverlayPtr) -> Result<Self> {
-        overlay.lock().unwrap().open_tree(SLED_HEADER_TREE)?;
+        overlay.lock().unwrap().open_tree(SLED_HEADER_TREE, true)?;
         Ok(Self(overlay.clone()))
     }
 

+ 32 - 5
src/blockchain/mod.rs

@@ -28,19 +28,29 @@ use crate::{tx::Transaction, util::time::Timestamp, Error, Result};
 
 /// Block related definitions and storage implementations
 pub mod block_store;
-pub use block_store::{Block, BlockDifficulty, BlockInfo, BlockStore, BlockStoreOverlay};
+pub use block_store::{
+    Block, BlockDifficulty, BlockInfo, BlockStore, BlockStoreOverlay, SLED_BLOCK_DIFFICULTY_TREE,
+    SLED_BLOCK_ORDER_TREE, SLED_BLOCK_TREE,
+};
 
 /// Header definition and storage implementation
 pub mod header_store;
-pub use header_store::{Header, HeaderHash, HeaderStore, HeaderStoreOverlay};
+pub use header_store::{
+    Header, HeaderHash, HeaderStore, HeaderStoreOverlay, SLED_HEADER_TREE, SLED_SYNC_HEADER_TREE,
+};
 
 /// Transactions related storage implementations
 pub mod tx_store;
-pub use tx_store::{TxStore, TxStoreOverlay};
+pub use tx_store::{
+    TxStore, TxStoreOverlay, SLED_PENDING_TX_ORDER_TREE, SLED_PENDING_TX_TREE,
+    SLED_TX_LOCATION_TREE, SLED_TX_TREE,
+};
 
 /// Contracts and Wasm storage implementations
 pub mod contract_store;
-pub use contract_store::{ContractStore, ContractStoreOverlay};
+pub use contract_store::{
+    ContractStore, ContractStoreOverlay, SLED_BINCODE_TREE, SLED_CONTRACTS_TREE,
+};
 
 /// Structure holding all sled trees that define the concept of Blockchain.
 #[derive(Clone)]
@@ -348,7 +358,24 @@ pub struct BlockchainOverlay {
 impl BlockchainOverlay {
     /// Instantiate a new `BlockchainOverlay` over the given [`Blockchain`] instance.
     pub fn new(blockchain: &Blockchain) -> Result<BlockchainOverlayPtr> {
-        let overlay = Arc::new(Mutex::new(sled_overlay::SledDbOverlay::new(&blockchain.sled_db)));
+        // Here we configure all our blockchain sled trees to be protected in the overlay
+        let protected_trees = vec![
+            SLED_BLOCK_TREE,
+            SLED_BLOCK_ORDER_TREE,
+            SLED_BLOCK_DIFFICULTY_TREE,
+            SLED_HEADER_TREE,
+            SLED_SYNC_HEADER_TREE,
+            SLED_TX_TREE,
+            SLED_TX_LOCATION_TREE,
+            SLED_PENDING_TX_TREE,
+            SLED_PENDING_TX_ORDER_TREE,
+            SLED_CONTRACTS_TREE,
+            SLED_BINCODE_TREE,
+        ];
+        let overlay = Arc::new(Mutex::new(sled_overlay::SledDbOverlay::new(
+            &blockchain.sled_db,
+            protected_trees,
+        )));
         let headers = HeaderStoreOverlay::new(&overlay)?;
         let blocks = BlockStoreOverlay::new(&overlay)?;
         let transactions = TxStoreOverlay::new(&overlay)?;

+ 16 - 9
src/blockchain/tx_store.rs

@@ -25,10 +25,10 @@ use crate::{tx::Transaction, Error, Result};
 
 use super::{parse_record, parse_u64_key_record, SledDbOverlayPtr};
 
-const SLED_TX_TREE: &[u8] = b"_transactions";
-const SLED_TX_LOCATION_TREE: &[u8] = b"_transaction_location";
-const SLED_PENDING_TX_TREE: &[u8] = b"_pending_transactions";
-const SLED_PENDING_TX_ORDER_TREE: &[u8] = b"_pending_transactions_order";
+pub const SLED_TX_TREE: &[u8] = b"_transactions";
+pub const SLED_TX_LOCATION_TREE: &[u8] = b"_transaction_location";
+pub const SLED_PENDING_TX_TREE: &[u8] = b"_pending_transactions";
+pub const SLED_PENDING_TX_ORDER_TREE: &[u8] = b"_pending_transactions_order";
 
 /// The `TxStore` is a structure representing all `sled` trees related
 /// to storing the blockchain's transactions information.
@@ -323,12 +323,19 @@ impl TxStore {
         Ok(txs)
     }
 
-    /// Fetch n transactions after given order. In the iteration, if a transaction
-    /// order is not found, the iteration stops and the function returns what
-    /// it has found so far in the store's pending order tree.
+    /// Fetch n transactions after given order([order..order+n)). In the iteration,
+    /// if a transaction order is not found, the iteration stops and the function
+    /// returns what it has found so far in the store's pending order tree.
     pub fn get_after_pending(&self, order: u64, n: usize) -> Result<(u64, Vec<Transaction>)> {
         let mut hashes = vec![];
 
+        // First we grab the order itself
+        if let Some(found) = self.pending_order.get(order.to_be_bytes())? {
+            let hash = deserialize(&found)?;
+            hashes.push(hash);
+        }
+
+        // Then whatever comes after it
         let mut key = order;
         let mut counter = 0;
         while counter < n {
@@ -405,8 +412,8 @@ pub struct TxStoreOverlay(SledDbOverlayPtr);
 
 impl TxStoreOverlay {
     pub fn new(overlay: &SledDbOverlayPtr) -> Result<Self> {
-        overlay.lock().unwrap().open_tree(SLED_TX_TREE)?;
-        overlay.lock().unwrap().open_tree(SLED_TX_LOCATION_TREE)?;
+        overlay.lock().unwrap().open_tree(SLED_TX_TREE, true)?;
+        overlay.lock().unwrap().open_tree(SLED_TX_LOCATION_TREE, true)?;
         Ok(Self(overlay.clone()))
     }