Browse Source

validator: use sled-overlay add diff functionality to rebuild forks

skoupidi 2 năm trước cách đây
mục cha
commit
23d49cd158
3 tập tin đã thay đổi với 17 bổ sung19 xóa
  1. 2 2
      Cargo.lock
  2. 1 1
      Cargo.toml
  3. 14 16
      src/validator/consensus.rs

+ 2 - 2
Cargo.lock

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

+ 1 - 1
Cargo.toml

@@ -118,7 +118,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.0", optional = true}
+sled-overlay = {version = "0.1.1", optional = true}
 
 # Miner
 randomx = {git = "https://github.com/darkrenaissance/RandomX", optional = true}

+ 14 - 16
src/validator/consensus.rs

@@ -20,7 +20,7 @@ use std::collections::BTreeSet;
 
 use darkfi_sdk::crypto::{MerkleTree, SecretKey};
 use darkfi_serial::{async_trait, serialize, SerialDecodable, SerialEncodable};
-use log::{debug, error, info};
+use log::{debug, info};
 use num_bigint::BigUint;
 use sled_overlay::database::SledDbOverlayState;
 use smol::lock::RwLock;
@@ -32,7 +32,7 @@ use crate::{
     validator::{
         pow::PoWModule,
         utils::{best_fork_index, block_rank, find_extended_fork_index},
-        verify_block, verify_proposal, verify_transactions, TxVerifyFailed,
+        verify_proposal, verify_transactions, TxVerifyFailed,
     },
     Error, Result,
 };
@@ -92,6 +92,7 @@ impl Consensus {
             for p in fork.proposals.iter().rev() {
                 if p == &proposal.hash {
                     drop(lock);
+                    debug!(target: "validator::consensus::append_proposal", "Proposal {} already exists", proposal.hash);
                     return Err(Error::ProposalAlreadyExists)
                 }
             }
@@ -173,7 +174,6 @@ impl Consensus {
             return Ok((original_fork.full_clone()?, Some(f_index)))
         }
 
-        // TODO: use new sled overlay diffs logic to make this not having rebuild the whole fork
         // Rebuild fork
         let mut fork = Fork::new(self.blockchain.clone(), self.module.read().await.clone()).await?;
         fork.proposals = original_fork.proposals[..p_index + 1].to_vec();
@@ -181,24 +181,22 @@ impl Consensus {
 
         // Retrieve proposals blocks from original fork
         let blocks = &original_fork.overlay.lock().unwrap().get_blocks_by_hash(&fork.proposals)?;
+        for (index, block) in blocks.iter().enumerate() {
+            // Apply block diffs
+            fork.overlay.lock().unwrap().overlay.lock().unwrap().add_diff(&fork.diffs[index]);
 
-        // Retrieve last block
-        let mut previous = &fork.overlay.lock().unwrap().last_block()?;
+            // Grab next mine target and difficulty
+            let (next_target, next_difficulty) = fork.module.next_mine_target_and_difficulty()?;
 
-        // Validate and insert each block
-        for block in blocks {
-            // Verify block
-            if verify_block(&fork.overlay, &fork.module, block, previous).await.is_err() {
-                error!(target: "validator::consensus::find_extended_fork_overlay", "Erroneous block found in set");
-                fork.overlay.lock().unwrap().overlay.lock().unwrap().purge_new_trees()?;
-                return Err(Error::BlockIsInvalid(block.hash()?.to_string()))
-            };
+            // Calculate block rank
+            let (target_distance_sq, hash_distance_sq) = block_rank(block, &next_target)?;
 
             // Update PoW module
-            fork.module.append(block.header.timestamp, &fork.module.next_difficulty()?);
+            fork.module.append(block.header.timestamp, &next_difficulty);
 
-            // Use last inserted block as next iteration previous
-            previous = block;
+            // Update fork ranks
+            fork.targets_rank += target_distance_sq;
+            fork.hashes_rank += hash_distance_sq;
         }
 
         // Drop forks lock