Jelajahi Sumber

src/consensus/task/consensus_sync: modified state retrieval, script/consensus_simulation and script/research/node-tools updated, src/util/time: increased retries to 10

aggstam 4 tahun lalu
induk
melakukan
0f3b420122

+ 17 - 20
script/consensus_simulation.sh

@@ -7,9 +7,6 @@ nodes=4
 # moving one folder up
 cd ..
 
-# compiling bin
-make BINS=darkfid2
-
 # PIDs array
 pids=()
 
@@ -17,12 +14,12 @@ pids=()
 LOG_TARGETS="!sled,!net" ./darkfid2 \
     -v \
     --consensus \
-    --consensus-p2p-accept 127.0.0.1:6000 \
-    --consensus-p2p-external 127.0.0.1:6000 \
+    --consensus-p2p-accept tcp://127.0.0.1:6000 \
+    --consensus-p2p-external tcp://127.0.0.1:6000 \
     --database ./tmp/node0/blockchain \
     --rpc-listen tcp://127.0.0.1:6010 \
-    --sync-p2p-accept 127.0.0.1:6020 \
-    --sync-p2p-external 127.0.0.1:6020 \
+    --sync-p2p-accept tcp://127.0.0.1:6020 \
+    --sync-p2p-external tcp://127.0.0.1:6020 \
     --wallet-path ./tmp/node0/wallet.db &
     
 pids[${#pids[@]}]=$!
@@ -35,16 +32,16 @@ bound=$(($nodes-2))
 for i in $(eval echo "{1..$bound}")
 do
   LOG_TARGETS="!sled,!net" ./darkfid2 \
-    -v \
+      -v \
     --consensus \
-    --consensus-p2p-seed 127.0.0.1:6000 \
-    --sync-p2p-seed 127.0.0.1:6020 \
-    --consensus-p2p-accept 127.0.0.1:600$i \
-    --consensus-p2p-external 127.0.0.1:600$i \
+    --consensus-p2p-seed tcp://127.0.0.1:6000 \
+    --sync-p2p-seed tcp://127.0.0.1:6020 \
+    --consensus-p2p-accept tcp://127.0.0.1:600$i \
+    --consensus-p2p-external tcp://127.0.0.1:600$i \
     --database ./tmp/node$i/blockchain \
     --rpc-listen tcp://127.0.0.1:601$i \
-    --sync-p2p-accept 127.0.0.1:602$i \
-    --sync-p2p-external 127.0.0.1:602$i \
+    --sync-p2p-accept tcp://127.0.0.1:602$i \
+    --sync-p2p-external tcp://127.0.0.1:602$i \
     --wallet-path ./tmp/node$i/wallet.db &
   pids[${#pids[@]}]=$!
   # waiting for node to setup
@@ -67,12 +64,12 @@ bound=$(($nodes-1))
 LOG_TARGETS="!sled,!net" ./darkfid2 \
     -v \
     --consensus \
-    --consensus-p2p-seed 127.0.0.1:6000 \
-    --sync-p2p-seed 127.0.0.1:6020 \
-    --consensus-p2p-accept 127.0.0.1:600$bound \
-    --consensus-p2p-external 127.0.0.1:600$bound \
+    --consensus-p2p-seed tcp://127.0.0.1:6000 \
+    --sync-p2p-seed tcp://127.0.0.1:6020 \
+    --consensus-p2p-accept tcp://127.0.0.1:600$bound \
+    --consensus-p2p-external tcp://127.0.0.1:600$bound \
     --database ./tmp/node$bound/blockchain \
     --rpc-listen tcp://127.0.0.1:601$bound \
-    --sync-p2p-accept 127.0.0.1:602$bound \
-    --sync-p2p-external 127.0.0.1:602$bound \
+    --sync-p2p-accept tcp://127.0.0.1:602$bound \
+    --sync-p2p-external tcp://127.0.0.1:602$bound \
     --wallet-path ./tmp/node$bound/wallet.db

+ 1 - 2
script/research/nodes-tool/src/main.rs

@@ -13,14 +13,13 @@ use darkfi::{
         metadata::{Metadata, OuroborosMetadata, StreamletMetadata},
         participant::Participant,
         state::{ConsensusState, ValidatorState},
-        util::Timestamp,
         vote::Vote,
         TESTNET_GENESIS_HASH_BYTES,
     },
     crypto::token_list::DrkTokenList,
     node::Client,
     tx::Transaction,
-    util::expand_path,
+    util::{expand_path, time::Timestamp},
     wallet::walletdb::init_wallet,
     Result,
 };

+ 22 - 15
src/consensus/task/consensus_sync.rs

@@ -12,25 +12,32 @@ use crate::{
 /// async task used for consensus state syncing.
 pub async fn consensus_sync_task(p2p: P2pPtr, state: ValidatorStatePtr) -> Result<()> {
     info!("Starting consensus state sync...");
-
+    let channels_map = p2p.channels().lock().await;
+    let values = channels_map.values();
     // Using len here beacuse is_empty() uses unstable library feature
     // called 'exact_size_is_empty'.
-    if p2p.channels().lock().await.values().len() != 0 {
-        // Nodes ask for the consensus state of the last channel peer
-        let channel = p2p.channels().lock().await.values().last().unwrap().clone();
-
-        // Communication setup
-        let msg_subsystem = channel.get_message_subsystem();
-        msg_subsystem.add_dispatch::<ConsensusResponse>().await;
-        let response_sub = channel.subscribe_msg::<ConsensusResponse>().await?;
+    if values.len() != 0 {
+        // Node iterates the channel peers to ask for their consensus state
+        for channel in values {
+            // Communication setup
+            let msg_subsystem = channel.get_message_subsystem();
+            msg_subsystem.add_dispatch::<ConsensusResponse>().await;
+            let response_sub = channel.subscribe_msg::<ConsensusResponse>().await?;
 
-        // Node creates a `ConsensusRequest` and sends it
-        let request = ConsensusRequest { address: state.read().await.address };
-        channel.send(request).await?;
+            // Node creates a `ConsensusRequest` and sends it
+            let request = ConsensusRequest { address: state.read().await.address };
+            channel.send(request).await?;
 
-        // Node stores response data. Extra validations can be added here.
-        let response = response_sub.receive().await?;
-        state.write().await.consensus = response.consensus.clone();
+            // Node verifies response came from a participating node.
+            // Extra validations can be added here.
+            let response = response_sub.receive().await?;
+            if response.consensus.participants.is_empty() {
+                warn!("Retrieved consensus state from a new node, retrying...");
+                continue
+            }
+            // Node stores response data.
+            state.write().await.consensus = response.consensus.clone();
+        }
     } else {
         warn!("Node is not connected to other nodes, resetting consensus state.");
         state.write().await.reset_consensus_state()?;

+ 1 - 1
src/consensus/task/proposal.rs

@@ -38,7 +38,7 @@ pub async fn proposal_task(consensus_p2p: P2pPtr, sync_p2p: P2pPtr, state: Valid
         return
     };
 
-    // Node signals the network that iw till start participating
+    // Node signals the network that it will start participating
     let address = state.read().await.address;
     let cur_epoch = state.read().await.current_epoch();
     let participant = Participant::new(address, cur_epoch);

+ 1 - 1
src/util/time.rs

@@ -56,7 +56,7 @@ impl std::fmt::Display for Timestamp {
 }
 
 // Clock sync parameters
-const RETRIES: u8 = 5;
+const RETRIES: u8 = 10;
 const WORLDTIMEAPI_ADDRESS: &str = "worldtimeapi.org";
 const WORLDTIMEAPI_ADDRESS_WITH_PORT: &str = "worldtimeapi.org:443";
 const WORLDTIMEAPI_PAYLOAD: &[u8; 88] = b"GET /api/timezone/Etc/UTC HTTP/1.1\r\nHost: worldtimeapi.org\r\nAccept: application/json\r\n\r\n";