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

consensus: cleaned derived coin blind handling

aggstam 3 лет назад
Родитель
Сommit
f86a554ff8

+ 1 - 6
src/consensus/proto/protocol_proposal.rs

@@ -31,10 +31,6 @@ use crate::{
     Result,
 };
 
-use darkfi_sdk::pasta::pallas;
-use halo2_proofs::arithmetic::Field;
-use rand::rngs::OsRng;
-
 pub struct ProtocolProposal {
     proposal_sub: MessageSubscription<BlockProposal>,
     jobsman: ProtocolJobsManagerPtr,
@@ -71,7 +67,6 @@ impl ProtocolProposal {
 
         let exclude_list = vec![self.channel_address.clone()];
         loop {
-            let derived_blind = pallas::Scalar::random(&mut OsRng);
             let proposal = match self.proposal_sub.receive().await {
                 Ok(v) => v,
                 Err(e) => {
@@ -105,7 +100,7 @@ impl ProtocolProposal {
                 continue
             }
 
-            match lock.receive_proposal(&proposal_copy, None, derived_blind).await {
+            match lock.receive_proposal(&proposal_copy, None).await {
                 Ok(broadcast) => {
                     if broadcast {
                         // Broadcast proposal to rest of nodes

+ 5 - 21
src/consensus/task/proposal.rs

@@ -28,10 +28,6 @@ use crate::{
     util::{async_util::sleep, time::Timestamp},
 };
 
-use darkfi_sdk::pasta::pallas;
-use halo2_proofs::arithmetic::Field;
-use rand::rngs::OsRng;
-
 /// async task used for participating in the consensus protocol
 pub async fn proposal_task(
     consensus_p2p: P2pPtr,
@@ -141,7 +137,6 @@ async fn consensus_loop(
     let mut listened_slots = 0;
     let mut changed_status = false;
     loop {
-        let derived_blind = pallas::Scalar::random(&mut OsRng);
         // Check if node can start proposing.
         // This code ensures that we only change the status once
         // and listened_slots doesn't increment further.
@@ -156,7 +151,7 @@ async fn consensus_loop(
         }
 
         // Node waits and execute consensus protocol propose period.
-        if propose_period(consensus_p2p.clone(), state.clone(), derived_blind).await {
+        if propose_period(consensus_p2p.clone(), state.clone()).await {
             // Node needs to resync
             warn!(
                 target: "consensus::proposal",
@@ -184,11 +179,7 @@ async fn consensus_loop(
 ///     - Generate slot sigmas and checkpoint
 ///     - Check if slot leader to generate and broadcast proposal
 /// Returns flag in case node needs to resync.
-async fn propose_period(
-    consensus_p2p: P2pPtr,
-    state: ValidatorStatePtr,
-    derived_blind: pallas::Scalar,
-) -> bool {
+async fn propose_period(consensus_p2p: P2pPtr, state: ValidatorStatePtr) -> bool {
     // Node sleeps until next slot
     let seconds_next_slot = state.read().await.consensus.next_n_slot_start(1).as_secs();
     info!(target: "consensus::proposal", "consensus: Waiting for next slot ({} sec)", seconds_next_slot);
@@ -218,18 +209,11 @@ async fn propose_period(
     let (won, fork_index, coin_index) =
         state.write().await.consensus.is_slot_leader(sigma1, sigma2);
     let result = if won {
-        state.write().await.propose(
-            processing_slot,
-            fork_index,
-            coin_index,
-            sigma1,
-            sigma2,
-            derived_blind,
-        )
+        state.write().await.propose(processing_slot, fork_index, coin_index, sigma1, sigma2)
     } else {
         Ok(None)
     };
-    let (proposal, coin) = match result {
+    let (proposal, coin, derived_blind) = match result {
         Ok(pair) => {
             if pair.is_none() {
                 info!(target: "consensus::proposal", "consensus: Node is not the slot lead");
@@ -261,7 +245,7 @@ async fn propose_period(
     match state
         .write()
         .await
-        .receive_proposal(&proposal, Some((coin_index, coin)), derived_blind)
+        .receive_proposal(&proposal, Some((coin_index, coin, derived_blind)))
         .await
     {
         Ok(_) => {

+ 8 - 6
src/consensus/validator.rs

@@ -31,6 +31,7 @@ use darkfi_sdk::{
     pasta::{group::ff::PrimeField, pallas},
 };
 use darkfi_serial::{deserialize, serialize, Decodable, Encodable, WriteExt};
+use halo2_proofs::arithmetic::Field;
 use log::{debug, error, info, warn};
 use rand::rngs::OsRng;
 use serde_json::json;
@@ -264,8 +265,7 @@ impl ValidatorState {
         coin_index: usize,
         sigma1: pallas::Base,
         sigma2: pallas::Base,
-        derived_blind: pallas::Scalar,
-    ) -> Result<Option<(BlockProposal, LeadCoin)>> {
+    ) -> Result<Option<(BlockProposal, LeadCoin, pallas::Scalar)>> {
         let eta = self.consensus.get_eta();
         // Check if node can produce proposals
         if !self.consensus.proposing {
@@ -291,6 +291,9 @@ impl ValidatorState {
             (checkpoint.proposal.hash, checkpoint.coins[coin_index])
         };
 
+        // Generate derived coin blind
+        let derived_blind = pallas::Scalar::random(&mut OsRng);
+
         // Generating leader proof
         let (proof, public_inputs) = coin.create_lead_proof(
             sigma1,
@@ -324,7 +327,7 @@ impl ValidatorState {
             *self.consensus.leaders_history.last().unwrap(),
         );
 
-        Ok(Some((BlockProposal::new(header, unproposed_txs, lead_info), coin)))
+        Ok(Some((BlockProposal::new(header, unproposed_txs, lead_info), coin, derived_blind)))
     }
 
     /// Retrieve all unconfirmed transactions not proposed in previous blocks
@@ -365,8 +368,7 @@ impl ValidatorState {
     pub async fn receive_proposal(
         &mut self,
         proposal: &BlockProposal,
-        coin: Option<(usize, LeadCoin)>,
-        derived_blind: pallas::Scalar,
+        coin: Option<(usize, LeadCoin, pallas::Scalar)>,
     ) -> Result<bool> {
         let current = self.consensus.current_slot();
         // Node hasn't started participating
@@ -554,7 +556,7 @@ impl ValidatorState {
         // TODO: [PLACEHOLDER] Add rewards validation
 
         // If proposal came fromself, we derive new coin
-        if let Some((idx, c)) = coin {
+        if let Some((idx, c, derived_blind)) = coin {
             state_checkpoint.coins[idx] =
                 c.derive_coin(&mut state_checkpoint.coins_tree, derived_blind);
         }