skoupidi 2 лет назад
Родитель
Сommit
cff856971d

+ 21 - 8
bin/darkfid/src/task/miner.rs

@@ -109,12 +109,15 @@ pub async fn miner_task(node: &Darkfid, recipient: &PublicKey, skip_sync: bool)
 
 
     // Start miner loop
     // Start miner loop
     loop {
     loop {
-        // Grab best current fork index
-        let fork_index = best_forks_indexes(&node.validator.consensus.forks.read().await)?[0];
+        // Grab best current fork proposals sequence
+        let forks = node.validator.consensus.forks.read().await;
+        let fork_index = best_forks_indexes(&forks)?[0];
+        let fork_proposals = forks[fork_index].proposals.clone();
+        drop(forks);
 
 
         // Start listenning for network proposals and mining next block for best fork.
         // Start listenning for network proposals and mining next block for best fork.
         smol::future::or(
         smol::future::or(
-            listen_to_network(node, fork_index, &subscription),
+            listen_to_network(node, fork_proposals, &subscription),
             mine_next_block(node, fork_index, &mut secret, recipient, &zkbin, &pk),
             mine_next_block(node, fork_index, &mut secret, recipient, &zkbin, &pk),
         )
         )
         .await?;
         .await?;
@@ -134,19 +137,29 @@ pub async fn miner_task(node: &Darkfid, recipient: &PublicKey, skip_sync: bool)
 /// Auxiliary function to listen for incoming proposals and check if the best fork has changed
 /// Auxiliary function to listen for incoming proposals and check if the best fork has changed
 async fn listen_to_network(
 async fn listen_to_network(
     node: &Darkfid,
     node: &Darkfid,
-    fork_index: usize,
+    fork_proposals: Vec<blake3::Hash>,
     subscription: &Subscription<JsonNotification>,
     subscription: &Subscription<JsonNotification>,
 ) -> Result<()> {
 ) -> Result<()> {
-    loop {
+    'main_loop: loop {
         // Wait until a new proposal has been received
         // Wait until a new proposal has been received
         subscription.receive().await;
         subscription.receive().await;
 
 
+        // Grab a lock over node forks
+        let forks = node.validator.consensus.forks.read().await;
+
         // Grab best current fork indexes
         // Grab best current fork indexes
-        let fork_indexes = best_forks_indexes(&node.validator.consensus.forks.read().await)?;
+        let fork_indexes = best_forks_indexes(&forks)?;
 
 
-        if !fork_indexes.contains(&fork_index) {
-            return Ok(())
+        // Iterate to verify if proposals sequence has changed
+        for index in fork_indexes {
+            if forks[index].proposals == fork_proposals {
+                drop(forks);
+                continue 'main_loop
+            }
         }
         }
+
+        drop(forks);
+        return Ok(())
     }
     }
 }
 }
 
 

+ 17 - 0
contrib/localnet/darkfid-small/darkfid2.toml

@@ -20,6 +20,9 @@ database = "darkfid2"
 # Finalization threshold, denominated by number of blocks
 # Finalization threshold, denominated by number of blocks
 threshold = 3
 threshold = 3
 
 
+# minerd JSON-RPC endpoint
+minerd_endpoint = "tcp://127.0.0.1:28467"
+
 # PoW block production target, in seconds
 # PoW block production target, in seconds
 pow_target = 20
 pow_target = 20
 
 
@@ -42,3 +45,17 @@ peers = ["tcp+tls://0.0.0.0:48242", "tcp+tls://0.0.0.0:48342"]
 
 
 # Allow localnet hosts
 # Allow localnet hosts
 localnet = true
 localnet = true
+
+## Localnet miners P2P network settings
+[network_config."localnet".miners_net]
+# P2P accept addresses the instance listens on for inbound connections
+inbound = ["tcp+tls://0.0.0.0:48441"]
+
+# Whitelisted network transports for outbound connections
+allowed_transports = ["tcp+tls"]
+
+# Peer nodes to manually connect to
+peers = ["tcp+tls://0.0.0.0:48241", "tcp+tls://0.0.0.0:48341"]
+
+# Allow localnet hosts
+localnet = true

+ 13 - 11
contrib/localnet/darkfid-small/tmux_sessions.sh

@@ -1,6 +1,8 @@
 #!/bin/sh
 #!/bin/sh
 set -e
 set -e
 
 
+session=darkfid-small
+
 # Start a tmux session with two mining and a non-mining darkfid nodes.
 # Start a tmux session with two mining and a non-mining darkfid nodes.
 # Additionally, start two minerd daemons.
 # Additionally, start two minerd daemons.
 
 
@@ -11,18 +13,18 @@ else
 	verbose=""
 	verbose=""
 fi
 fi
 
 
-tmux new-session -d
-tmux send-keys "../../../minerd ${verbose} -c minerd0.toml" Enter
+tmux new-session -d -s $session
+tmux send-keys -t $session "../../../minerd ${verbose} -c minerd0.toml" Enter
 sleep 1
 sleep 1
-tmux split-window -v
-tmux send-keys "LOG_TARGETS='!sled' ../../../darkfid ${verbose} -c darkfid0.toml" Enter
+tmux split-window -t $session -v
+tmux send-keys -t $session "LOG_TARGETS='!sled' ../../../darkfid ${verbose} -c darkfid0.toml" Enter
 sleep 2
 sleep 2
-tmux new-window
-tmux send-keys "../../../minerd ${verbose} -c minerd1.toml" Enter
+tmux new-window -t $session
+tmux send-keys -t $session "../../../minerd ${verbose} -c minerd1.toml" Enter
 sleep 1
 sleep 1
-tmux split-window -v
-tmux send-keys "LOG_TARGETS='!sled' ../../../darkfid ${verbose} -c darkfid1.toml" Enter
+tmux split-window -t $session -v
+tmux send-keys -t $session "LOG_TARGETS='!sled' ../../../darkfid ${verbose} -c darkfid1.toml" Enter
 sleep 2
 sleep 2
-tmux new-window
-tmux send-keys "LOG_TARGETS='!sled' ../../../darkfid ${verbose} -c darkfid2.toml" Enter
-tmux attach
+tmux new-window -t $session
+tmux send-keys -t $session "LOG_TARGETS='!sled' ../../../darkfid ${verbose} -c darkfid2.toml" Enter
+tmux attach -t $session

+ 13 - 8
src/validator/consensus.rs

@@ -141,18 +141,23 @@ impl Consensus {
         // If a fork index was found, replace forks with the mutated one,
         // If a fork index was found, replace forks with the mutated one,
         // otherwise push the new fork.
         // otherwise push the new fork.
         let mut lock = self.forks.write().await;
         let mut lock = self.forks.write().await;
+        // Check if fork already exists
+        for f in lock.iter() {
+            if f.proposals == fork.proposals {
+                drop(lock);
+                return Err(Error::ProposalAlreadyExists)
+            }
+        }
         match index {
         match index {
             Some(i) => {
             Some(i) => {
-                lock[i] = fork;
+                if i < lock.len() && lock[i].proposals == fork.proposals[..fork.proposals.len() - 1]
+                {
+                    lock[i] = fork;
+                } else {
+                    lock.push(fork);
+                }
             }
             }
             None => {
             None => {
-                // Check if fork already exists
-                for f in lock.iter() {
-                    if f.proposals == fork.proposals {
-                        drop(lock);
-                        return Err(Error::ProposalAlreadyExists)
-                    }
-                }
                 lock.push(fork);
                 lock.push(fork);
             }
             }
         }
         }