Преглед изворни кода

consensus: explicit field names in structures

aggstam пре 4 година
родитељ
комит
d02ee72177

+ 20 - 20
script/research/nodes-tool/src/main.rs

@@ -43,16 +43,16 @@ impl ParticipantInfo {
 #[derive(Debug)]
 struct VoteInfo {
     _proposal: blake3::Hash,
-    _sl: u64,
+    _slot: u64,
     _address: String,
 }
 
 impl VoteInfo {
     pub fn new(vote: &Vote) -> VoteInfo {
         let _proposal = vote.proposal;
-        let _sl = vote.sl;
+        let _slot = vote.slot;
         let _address = vote.address.to_string();
-        VoteInfo { _proposal, _sl, _address }
+        VoteInfo { _proposal, _slot, _address }
     }
 }
 
@@ -83,16 +83,16 @@ impl StreamletMetadataInfo {
 #[derive(Debug)]
 struct MetadataInfo {
     _proof: String,
-    _r: String,
-    _s: String,
+    _rand_seed: String,
+    _signature: String,
 }
 
 impl MetadataInfo {
     pub fn new(metadata: &Metadata) -> MetadataInfo {
         let _proof = metadata.proof.clone();
-        let _r = metadata.r.clone();
-        let _s = metadata.s.clone();
-        MetadataInfo { _proof, _r, _s }
+        let _rand_seed = metadata.rand_seed.clone();
+        let _signature = metadata.signature.clone();
+        MetadataInfo { _proof, _rand_seed, _signature }
     }
 }
 
@@ -155,23 +155,23 @@ impl ConsensusInfo {
 #[derive(Debug)]
 struct HeaderInfo {
     _hash: blake3::Hash,
-    _v: u8,
-    _st: blake3::Hash,
-    _e: u64,
-    _sl: u64,
+    _version: u8,
+    _state: blake3::Hash,
+    _epoch: u64,
+    _slot: u64,
     _timestamp: Timestamp,
     _root: MerkleNode,
 }
 
 impl HeaderInfo {
     pub fn new(_hash: blake3::Hash, header: &Header) -> HeaderInfo {
-        let _v = header.v;
-        let _st = header.st;
-        let _e = header.e;
-        let _sl = header.sl;
+        let _version = header.version;
+        let _state = header.state;
+        let _epoch = header.epoch;
+        let _slot = header.slot;
         let _timestamp = header.timestamp;
         let _root = header.root;
-        HeaderInfo { _hash, _v, _st, _e, _sl, _timestamp, _root }
+        HeaderInfo { _hash, _version, _state, _epoch, _slot, _timestamp, _root }
     }
 }
 
@@ -238,13 +238,13 @@ impl BlockInfoChain {
 
 #[derive(Debug)]
 struct OrderInfo {
-    _sl: u64,
+    _slot: u64,
     _hash: blake3::Hash,
 }
 
 impl OrderInfo {
-    pub fn new(_sl: u64, _hash: blake3::Hash) -> OrderInfo {
-        OrderInfo { _sl, _hash }
+    pub fn new(_slot: u64, _hash: blake3::Hash) -> OrderInfo {
+        OrderInfo { _slot, _hash }
     }
 }
 

+ 2 - 2
src/blockchain/mod.rs

@@ -88,7 +88,7 @@ impl Blockchain {
             self.blocks.insert(&[_block])?;
 
             // Store block order
-            self.order.insert(&[block.header.sl], &[headerhash[0]])?;
+            self.order.insert(&[block.header.slot], &[headerhash[0]])?;
 
             // Store streamlet metadata
             self.streamlet_metadata.insert(&[headerhash[0]], &[block.sm.clone()])?;
@@ -102,7 +102,7 @@ impl Blockchain {
 
     /// Check if the given [`BlockInfo`] is in the database and all trees.
     pub fn has_block(&self, block: &BlockInfo) -> Result<bool> {
-        let blockhash = match self.order.get(&[block.header.sl], true) {
+        let blockhash = match self.order.get(&[block.header.slot], true) {
             Ok(v) => v[0].unwrap(),
             Err(_) => return Ok(false),
         };

+ 17 - 11
src/consensus/block.rs

@@ -24,13 +24,13 @@ use crate::{
 #[derive(Debug, Clone, PartialEq, SerialEncodable, SerialDecodable)]
 pub struct Header {
     /// Block version
-    pub v: u8,
+    pub version: u8,
     /// Previous block hash
-    pub st: blake3::Hash,
+    pub state: blake3::Hash,
     /// Epoch
-    pub e: u64,
+    pub epoch: u64,
     /// Slot UID
-    pub sl: u64,
+    pub slot: u64,
     /// Block creation timestamp
     pub timestamp: Timestamp,
     /// Root of the transaction hashes merkle tree
@@ -38,9 +38,15 @@ pub struct Header {
 }
 
 impl Header {
-    pub fn new(st: blake3::Hash, e: u64, sl: u64, timestamp: Timestamp, root: MerkleNode) -> Self {
-        let v = *BLOCK_VERSION;
-        Self { v, st, e, sl, timestamp, root }
+    pub fn new(
+        state: blake3::Hash,
+        epoch: u64,
+        slot: u64,
+        timestamp: Timestamp,
+        root: MerkleNode,
+    ) -> Self {
+        let version = *BLOCK_VERSION;
+        Self { version, state, epoch, slot, timestamp, root }
     }
 
     /// Generate the genesis block.
@@ -91,7 +97,7 @@ impl Block {
 #[derive(Debug, SerialEncodable, SerialDecodable)]
 pub struct BlockOrder {
     /// Slot UID
-    pub sl: u64,
+    pub slot: u64,
     /// Block headerhash of that slot
     pub block: blake3::Hash,
 }
@@ -221,14 +227,14 @@ impl ProposalChain {
     /// excluding the genesis block proposal.
     /// Additional validity rules can be applied.
     pub fn check_proposal(&self, proposal: &BlockProposal, previous: &BlockProposal) -> bool {
-        if proposal.block.header.st == self.genesis_block {
+        if proposal.block.header.state == self.genesis_block {
             debug!("check_proposal(): Genesis block proposal provided.");
             return false
         }
 
         let prev_hash = previous.block.header.headerhash();
-        if proposal.block.header.st != prev_hash ||
-            proposal.block.header.sl <= previous.block.header.sl
+        if proposal.block.header.state != prev_hash ||
+            proposal.block.header.slot <= previous.block.header.slot
         {
             debug!("check_proposal(): Provided proposal is invalid.");
             return false

+ 4 - 4
src/consensus/metadata.rs

@@ -8,14 +8,14 @@ pub struct Metadata {
     /// Proof that the stakeholder is the block owner
     pub proof: String,
     /// Random seed for VRF
-    pub r: String,
+    pub rand_seed: String,
     /// Block owner signature
-    pub s: String,
+    pub signature: String,
 }
 
 impl Metadata {
-    pub fn new(proof: String, r: String, s: String) -> Self {
-        Self { proof, r, s }
+    pub fn new(proof: String, rand_seed: String, signature: String) -> Self {
+        Self { proof, rand_seed, signature }
     }
 }
 

+ 1 - 1
src/consensus/proto/protocol_sync.rs

@@ -70,7 +70,7 @@ impl ProtocolSync {
             debug!("ProtocolSync::handle_receive_request() received {:?}", order);
 
             // Extra validations can be added here
-            let key = order.sl;
+            let key = order.slot;
             let blocks = match self.state.read().await.blockchain.get_blocks_after(key, BATCH) {
                 Ok(v) => v,
                 Err(e) => {

+ 20 - 18
src/consensus/state.rs

@@ -210,8 +210,8 @@ impl ValidatorState {
         let mut slot = 0;
         for chain in &self.consensus.proposals {
             for proposal in &chain.proposals {
-                if proposal.block.header.sl > slot {
-                    slot = proposal.block.header.sl;
+                if proposal.block.header.slot > slot {
+                    slot = proposal.block.header.slot;
                 }
             }
         }
@@ -222,8 +222,8 @@ impl ValidatorState {
             return Ok(slot)
         }
 
-        let (last_sl, _) = self.blockchain.last()?;
-        Ok(last_sl)
+        let (last_slot, _) = self.blockchain.last()?;
+        Ok(last_slot)
     }
 
     /// Calculates seconds until next slot starting time.
@@ -442,7 +442,7 @@ impl ValidatorState {
             self.public,
             signed_hash,
             proposal_hash,
-            proposal.block.header.sl,
+            proposal.block.header.slot,
             self.address,
         )))
     }
@@ -464,19 +464,21 @@ impl ValidatorState {
         for (index, chain) in self.consensus.proposals.iter().enumerate() {
             let last = chain.proposals.last().unwrap();
             let hash = last.block.header.headerhash();
-            if proposal.block.header.st == hash && proposal.block.header.sl > last.block.header.sl {
+            if proposal.block.header.state == hash &&
+                proposal.block.header.slot > last.block.header.slot
+            {
                 return Ok(index as i64)
             }
 
-            if proposal.block.header.st == last.block.header.st &&
-                proposal.block.header.sl == last.block.header.sl
+            if proposal.block.header.state == last.block.header.state &&
+                proposal.block.header.slot == last.block.header.slot
             {
                 debug!("find_extended_chain_index(): Proposal already received");
                 return Ok(-2)
             }
 
-            if proposal.block.header.st == last.block.header.st &&
-                proposal.block.header.sl > last.block.header.sl
+            if proposal.block.header.state == last.block.header.state &&
+                proposal.block.header.slot > last.block.header.slot
             {
                 fork = Some(chain.clone());
             }
@@ -495,8 +497,8 @@ impl ValidatorState {
             None => (),
         }
 
-        let (last_sl, last_block) = self.blockchain.last()?;
-        if proposal.block.header.st != last_block || proposal.block.header.sl <= last_sl {
+        let (last_slot, last_block) = self.blockchain.last()?;
+        if proposal.block.header.state != last_block || proposal.block.header.slot <= last_slot {
             debug!("find_extended_chain_index(): Proposal doesn't extend any known chain");
             return Ok(-2)
         }
@@ -555,11 +557,11 @@ impl ValidatorState {
                 // Updating participant vote
                 match participant.voted {
                     Some(voted) => {
-                        if vote.sl > voted {
-                            participant.voted = Some(vote.sl);
+                        if vote.slot > voted {
+                            participant.voted = Some(vote.slot);
                         }
                     }
-                    None => participant.voted = Some(vote.sl),
+                    None => participant.voted = Some(vote.slot),
                 }
 
                 // Invalidating quarantine
@@ -709,12 +711,12 @@ impl ValidatorState {
         }
 
         let last_block = *blockhashes.last().unwrap();
-        let last_sl = finalized.last().unwrap().header.sl;
+        let last_slot = finalized.last().unwrap().header.slot;
 
         let mut dropped = vec![];
         for chain in self.consensus.proposals.iter() {
             let first = chain.proposals.first().unwrap();
-            if first.block.header.st != last_block || first.block.header.sl <= last_sl {
+            if first.block.header.state != last_block || first.block.header.slot <= last_slot {
                 dropped.push(chain.clone());
             }
         }
@@ -726,7 +728,7 @@ impl ValidatorState {
         // Remove orphan votes
         let mut orphans = vec![];
         for vote in self.consensus.orphan_votes.iter() {
-            if vote.sl <= last_sl {
+            if vote.slot <= last_slot {
                 orphans.push(vote.clone());
             }
         }

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

@@ -34,7 +34,7 @@ pub async fn block_sync_task(p2p: net::P2pPtr, state: ValidatorStatePtr) -> Resu
 
         loop {
             // Node creates a `BlockOrder` and sends it
-            let order = BlockOrder { sl: last.0, block: last.1 };
+            let order = BlockOrder { slot: last.0, block: last.1 };
             channel.send(order).await?;
 
             // Node stores response data.

+ 3 - 3
src/consensus/vote.rs

@@ -17,7 +17,7 @@ pub struct Vote {
     /// Block proposal hash to vote on
     pub proposal: blake3::Hash,
     /// Slot uid, generated by the beacon
-    pub sl: u64,
+    pub slot: u64,
     /// Node wallet address
     pub address: Address,
 }
@@ -27,10 +27,10 @@ impl Vote {
         public_key: PublicKey,
         vote: Signature,
         proposal: blake3::Hash,
-        sl: u64,
+        slot: u64,
         address: Address,
     ) -> Self {
-        Self { public_key, vote, proposal, sl, address }
+        Self { public_key, vote, proposal, slot, address }
     }
 }