Просмотр исходного кода

darkfid: Simplify consensus proposal_task.

parazyd 4 лет назад
Родитель
Сommit
b02933dcb9
2 измененных файлов с 44 добавлено и 42 удалено
  1. 40 39
      bin/darkfid2/src/consensus.rs
  2. 4 3
      bin/darkfid2/src/main.rs

+ 40 - 39
bin/darkfid2/src/consensus.rs

@@ -16,15 +16,13 @@ pub async fn proposal_task(p2p: P2pPtr, state: ValidatorStatePtr) {
         Err(e) => error!("Failed broadcasting consensus participation: {}", e),
     }
 
-    // After initialization, node should wait for next epoch
-    let seconds_until_next_epoch = state.read().await.next_epoch_start().as_secs();
-    info!("Waiting for next epoch({} sec)...", seconds_until_next_epoch);
-    sleep(seconds_until_next_epoch).await;
-
     loop {
+        let seconds_until_next_epoch = state.read().await.next_epoch_start().as_secs();
+        info!(target: "consensus", "Waiting for next epoch ({}) sec)...", seconds_until_next_epoch);
+        sleep(seconds_until_next_epoch).await;
+
         // Node refreshes participants records
         state.write().await.refresh_participants();
-        log::warn!("Participants: {:#?}", state.read().await.consensus.participants);
 
         // Node checks if it's the epoch leader to generate a new proposal
         // for that epoch.
@@ -37,49 +35,52 @@ pub async fn proposal_task(p2p: P2pPtr, state: ValidatorStatePtr) {
         match result {
             Ok(proposal) => {
                 if proposal.is_none() {
-                    info!("Node is not the epoch leader. Sleeping till next epoch...");
-                } else {
-                    // Leader creates a vote for the proposal and broadcasts them both
-                    let proposal = proposal.unwrap();
-                    info!("Node is the epoch leader: Proposed block: {:?}", proposal);
-                    let vote = state.write().await.receive_proposal(&proposal);
-                    match vote {
-                        Ok(v) => {
-                            if v.is_none() {
-                                debug!("Node did not vote for the proposed block");
-                            } else {
-                                let vote = v.unwrap();
-                                let result = state.write().await.receive_vote(&vote);
-                                match result {
-                                    Ok(_) => info!("Vote saved successfully."),
-                                    Err(e) => error!("Vote save failed: {}", e),
-                                }
+                    info!(target: "consensus", "Node is not the epoch leader. Sleeping till next epoch...");
+                    continue
+                }
+                // Leader creates a vote for the proposal and broadcasts them both
+                let proposal = proposal.unwrap();
+                info!(target: "consensus", "Node is the epoch leader: Proposed block: {:?}", proposal);
+                let vote = state.write().await.receive_proposal(&proposal);
+                match vote {
+                    Ok(v) => {
+                        if v.is_none() {
+                            debug!("proposal_task(): Node did not vote for the proposed block");
+                        } else {
+                            let vote = v.unwrap();
+                            let result = state.write().await.receive_vote(&vote);
+                            match result {
+                                Ok(_) => info!(target: "consensus", "Vote saved successfully."),
+                                Err(e) => error!(target: "consensus", "Vote save failed: {}", e),
+                            }
 
-                                // Broadcast block
-                                let result = p2p.broadcast(proposal).await;
-                                match result {
-                                    Ok(()) => info!("Proposal broadcasted successfully."),
-                                    Err(e) => error!("Failed broadcasting proposal: {}", e),
+                            // Broadcast block
+                            let result = p2p.broadcast(proposal).await;
+                            match result {
+                                Ok(()) => {
+                                    info!(target: "consensus", "Proposal broadcasted successfully.")
                                 }
+                                Err(e) => {
+                                    error!(target: "consensus", "Failed broadcasting proposal: {}", e)
+                                }
+                            }
 
-                                // Broadcast leader vote
-                                let result = p2p.broadcast(vote).await;
-                                match result {
-                                    Ok(()) => info!("Leader vote broadcasted successfully."),
-                                    Err(e) => error!("Failed broadcasting leader vote: {}", e),
+                            // Broadcast leader vote
+                            let result = p2p.broadcast(vote).await;
+                            match result {
+                                Ok(()) => {
+                                    info!(target: "consensus", "Leader vote broadcasted successfully.")
+                                }
+                                Err(e) => {
+                                    error!(target: "consensus", "Failed broadcasting leader vote: {}", e)
                                 }
                             }
                         }
-                        Err(e) => error!("Failed processing proposal: {}", e),
                     }
+                    Err(e) => error!(target: "consensus", "Failed processing proposal: {}", e),
                 }
             }
             Err(e) => error!("Block proposal failed: {}", e),
         }
-
-        // Node waits until next epoch
-        let seconds_until_next_epoch = state.read().await.next_epoch_start().as_secs();
-        info!("Waiting for next epoch ({} sec)...", seconds_until_next_epoch);
-        sleep(seconds_until_next_epoch).await;
     }
 }

+ 4 - 3
bin/darkfid2/src/main.rs

@@ -5,7 +5,7 @@ use async_std::sync::{Arc, Mutex};
 use async_trait::async_trait;
 use easy_parallel::Parallel;
 use futures_lite::future;
-use log::{debug, error, info};
+use log::{error, info};
 use rand::Rng;
 use serde_derive::Deserialize;
 use serde_json::{json, Value};
@@ -341,8 +341,8 @@ impl Darkfid {
 
         let result = self.p2p.broadcast(tx).await;
         match result {
-            Ok(()) => return jsonrpc::response(json!(true), id).into(),
-            Err(_) => return jsonrpc::error(InternalError, None, id).into(),
+            Ok(()) => jsonrpc::response(json!(true), id).into(),
+            Err(_) => jsonrpc::error(InternalError, None, id).into(),
         }
     }
 }
@@ -401,6 +401,7 @@ async fn realmain(args: Args, ex: Arc<Executor<'_>>) -> Result<()> {
 
     // Activate these protocols only if we're participating in consensus.
     if args.consensus {
+        info!("Registering consensus P2P protocols...");
         let registry = p2p.protocol_registry();
 
         let _state = state.clone();