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

consensus: moved epoch configuration under mod.rs

aggstam 3 лет назад
Родитель
Сommit
fae2a7a169
3 измененных файлов с 15 добавлено и 15 удалено
  1. 7 0
      src/consensus/mod.rs
  2. 6 13
      src/consensus/state.rs
  3. 2 2
      src/consensus/task/keep_alive.rs

+ 7 - 0
src/consensus/mod.rs

@@ -64,8 +64,15 @@ lazy_static! {
     pub static ref BLOCK_INFO_MAGIC_BYTES: [u8; 4] = [0x90, 0x44, 0xf1, 0xf6];
     pub static ref BLOCK_INFO_MAGIC_BYTES: [u8; 4] = [0x90, 0x44, 0xf1, 0xf6];
 
 
     // Epoch configuration
     // Epoch configuration
+    /// Slots in an epoch
     pub static ref EPOCH_LENGTH: u64 = 10;
     pub static ref EPOCH_LENGTH: u64 = 10;
+    /// Block leader reward
     pub static ref REWARD: u64 = 420;
     pub static ref REWARD: u64 = 420;
+    /// `2 * DELTA` represents slot time
+    pub static ref DELTA: u64 = 20;
+    
+    /// Quarantine duration, in slots
+    pub static ref QUARANTINE_DURATION: u64 = 5;
 
 
     // TODO: Describe constants meaning in comment
     // TODO: Describe constants meaning in comment
     pub static ref RADIX_BITS: usize = 76;
     pub static ref RADIX_BITS: usize = 76;

+ 6 - 13
src/consensus/state.rs

@@ -16,7 +16,7 @@ use rand::rngs::OsRng;
 
 
 use super::{
 use super::{
     Block, BlockInfo, BlockProposal, Header, KeepAlive, LeadProof, Metadata, Participant,
     Block, BlockInfo, BlockProposal, Header, KeepAlive, LeadProof, Metadata, Participant,
-    ProposalChain,
+    ProposalChain, EPOCH_LENGTH, DELTA, QUARANTINE_DURATION,
 };
 };
 
 
 use crate::{
 use crate::{
@@ -36,13 +36,6 @@ use crate::{
     Result,
     Result,
 };
 };
 
 
-/// `2 * DELTA` represents slot time
-pub const DELTA: u64 = 20;
-/// Slots in an epoch
-pub const EPOCH_SLOTS: u64 = 10;
-/// Quarantine duration, in slots
-pub const QUARANTINE_DURATION: u64 = 5;
-
 /// This struct represents the information required by the consensus algorithm
 /// This struct represents the information required by the consensus algorithm
 #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
 #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
 pub struct ConsensusState {
 pub struct ConsensusState {
@@ -207,15 +200,15 @@ impl ValidatorState {
     }
     }
 
 
     /// Calculates the epoch of the provided slot.
     /// Calculates the epoch of the provided slot.
-    /// Epoch duration is configured using the `EPOCH_SLOTS` value.
+    /// Epoch duration is configured using the `EPOCH_LENGTH` value.
     pub fn slot_epoch(&self, slot: u64) -> u64 {
     pub fn slot_epoch(&self, slot: u64) -> u64 {
-        slot / EPOCH_SLOTS
+        slot / *EPOCH_LENGTH
     }
     }
 
 
     /// Calculates current slot, based on elapsed time from the genesis block.
     /// Calculates current slot, based on elapsed time from the genesis block.
     /// Slot duration is configured using the `DELTA` value.
     /// Slot duration is configured using the `DELTA` value.
     pub fn current_slot(&self) -> u64 {
     pub fn current_slot(&self) -> u64 {
-        self.consensus.genesis_ts.elapsed() / (2 * DELTA)
+        self.consensus.genesis_ts.elapsed() / (2 * *DELTA)
     }
     }
 
 
     /// Finds the last slot a proposal or block was generated.
     /// Finds the last slot a proposal or block was generated.
@@ -244,7 +237,7 @@ impl ValidatorState {
     pub fn next_n_slot_start(&self, n: u64) -> Duration {
     pub fn next_n_slot_start(&self, n: u64) -> Duration {
         let start_time = NaiveDateTime::from_timestamp(self.consensus.genesis_ts.0, 0);
         let start_time = NaiveDateTime::from_timestamp(self.consensus.genesis_ts.0, 0);
         let current_slot = self.current_slot() + n;
         let current_slot = self.current_slot() + n;
-        let next_slot_start = (current_slot * (2 * DELTA)) + (start_time.timestamp() as u64);
+        let next_slot_start = (current_slot * (2 * *DELTA)) + (start_time.timestamp() as u64);
         let next_slot_start = NaiveDateTime::from_timestamp(next_slot_start as i64, 0);
         let next_slot_start = NaiveDateTime::from_timestamp(next_slot_start as i64, 0);
         let current_time = NaiveDateTime::from_timestamp(Utc::now().timestamp(), 0);
         let current_time = NaiveDateTime::from_timestamp(Utc::now().timestamp(), 0);
         let diff = next_slot_start - current_time;
         let diff = next_slot_start - current_time;
@@ -674,7 +667,7 @@ impl ValidatorState {
         self.consensus.pending_participants = vec![];
         self.consensus.pending_participants = vec![];
 
 
         let mut inactive = Vec::new();
         let mut inactive = Vec::new();
-        let low_bound = current - QUARANTINE_DURATION;
+        let low_bound = current - *QUARANTINE_DURATION;
 
 
         debug!(
         debug!(
             "refresh_participants(): Node {} checking slots range: {} -> {}",
             "refresh_participants(): Node {} checking slots range: {} -> {}",

+ 2 - 2
src/consensus/task/keep_alive.rs

@@ -5,7 +5,7 @@ use rand::Rng;
 use smol::Executor;
 use smol::Executor;
 
 
 use crate::{
 use crate::{
-    consensus::{state::QUARANTINE_DURATION, KeepAlive, ValidatorStatePtr},
+    consensus::{QUARANTINE_DURATION, KeepAlive, ValidatorStatePtr},
     crypto::schnorr::SchnorrSecret,
     crypto::schnorr::SchnorrSecret,
     net::P2pPtr,
     net::P2pPtr,
     util::async_util::sleep,
     util::async_util::sleep,
@@ -21,7 +21,7 @@ pub async fn keep_alive_task(
     ex.spawn(async move {
     ex.spawn(async move {
         loop {
         loop {
             // Pick a random slot in range: next slot + QUARANTINE_DURATION, exluding first and last
             // Pick a random slot in range: next slot + QUARANTINE_DURATION, exluding first and last
-            let slot = rand::thread_rng().gen_range(2..QUARANTINE_DURATION);
+            let slot = rand::thread_rng().gen_range(2..*QUARANTINE_DURATION);
             let seconds = state.read().await.next_n_slot_start(slot).as_secs();
             let seconds = state.read().await.next_n_slot_start(slot).as_secs();
             debug!("keep_alive_task: Waiting for next {} slots ({} sec)", slot, seconds);
             debug!("keep_alive_task: Waiting for next {} slots ({} sec)", slot, seconds);