Ver código fonte

consensus: removed public key from Block Proposal, validation through Participant.public_key

aggstam 4 anos atrás
pai
commit
633fbc362a
2 arquivos alterados com 10 adições e 16 exclusões
  1. 3 8
      src/consensus/block.rs
  2. 7 8
      src/consensus/state.rs

+ 3 - 8
src/consensus/block.rs

@@ -8,8 +8,7 @@ use super::{
 };
 use crate::{
     crypto::{
-        address::Address, constants::MERKLE_DEPTH, keypair::PublicKey, merkle_node::MerkleNode,
-        schnorr::Signature,
+        address::Address, constants::MERKLE_DEPTH, merkle_node::MerkleNode, schnorr::Signature,
     },
     impl_vec, net,
     tx::Transaction,
@@ -159,8 +158,6 @@ impl net::Message for BlockResponse {
 /// This struct represents a block proposal, used for consensus.
 #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
 pub struct BlockProposal {
-    /// Leader public key
-    pub public_key: PublicKey,
     /// Block signature
     pub signature: Signature,
     /// Leader address
@@ -172,7 +169,6 @@ pub struct BlockProposal {
 impl BlockProposal {
     #[allow(clippy::too_many_arguments)]
     pub fn new(
-        public_key: PublicKey,
         signature: Signature,
         address: Address,
         header: Header,
@@ -181,14 +177,13 @@ impl BlockProposal {
         sm: StreamletMetadata,
     ) -> Self {
         let block = BlockInfo::new(header, txs, metadata, sm);
-        Self { public_key, signature, address, block }
+        Self { signature, address, block }
     }
 }
 
 impl PartialEq for BlockProposal {
     fn eq(&self, other: &Self) -> bool {
-        self.public_key == other.public_key &&
-            self.signature == other.signature &&
+        self.signature == other.signature &&
             self.address == other.address &&
             self.block.header == other.block.header &&
             self.block.txs == other.block.txs &&

+ 7 - 8
src/consensus/state.rs

@@ -248,7 +248,7 @@ impl ValidatorState {
     /// Find slot leader, using a simple hash method.
     /// Leader calculation is based on how many nodes are participating
     /// in the network.
-    pub fn slot_leader(&mut self) -> Address {
+    pub fn slot_leader(&mut self) -> Participant {
         let slot = self.current_slot();
         // DefaultHasher is used to hash the slot number
         // because it produces a number string which then can be modulated by the len.
@@ -258,13 +258,13 @@ impl ValidatorState {
         let pos = hasher.finish() % (self.consensus.participants.len() as u64);
         // Since BTreeMap orders by key in asceding order, each node will have
         // the same key in calculated position.
-        self.consensus.participants.iter().nth(pos as usize).unwrap().1.address
+        self.consensus.participants.iter().nth(pos as usize).unwrap().1.clone()
     }
 
     /// Check if we're the current slot leader
     pub fn is_slot_leader(&mut self) -> bool {
         let address = self.address;
-        address == self.slot_leader()
+        address == self.slot_leader().address
     }
 
     /// Generate a block proposal for the current slot, containing all
@@ -294,7 +294,6 @@ impl ValidatorState {
         let signed_proposal = self.secret.sign(&header.headerhash().as_bytes()[..]);
 
         Ok(Some(BlockProposal::new(
-            self.public,
             signed_proposal,
             self.address,
             header,
@@ -371,16 +370,16 @@ impl ValidatorState {
         self.refresh_participants()?;
 
         let leader = self.slot_leader();
-        if leader != proposal.address {
+        if leader.address != proposal.address {
             warn!(
                 "Received proposal not from slot leader ({}), but from ({})",
-                leader,
+                leader.address.to_string(),
                 proposal.address.to_string()
             );
             return Ok(None)
         }
 
-        if !proposal
+        if !leader
             .public_key
             .verify(proposal.block.header.headerhash().as_bytes(), &proposal.signature)
         {
@@ -819,7 +818,7 @@ impl ValidatorState {
                 None => {
                     // Slot leader is always quarantined, to cover the case they become inactive the slot before
                     // becoming the leader. This can be used for slashing in the future.
-                    if participant.address == leader {
+                    if participant.address == leader.address {
                         debug!(
                             "refresh_participants(): Quaranteening leader: {:?} (joined {:?}, voted {:?})",
                             participant.address.to_string(),