فهرست منبع

[consensus] kp, ki as a function of #leads

mohab metwally 3 سال پیش
والد
کامیت
d903c71cd9
2فایلهای تغییر یافته به همراه29 افزوده شده و 1 حذف شده
  1. 1 0
      src/consensus/constants.rs
  2. 28 1
      src/consensus/state.rs

+ 1 - 0
src/consensus/constants.rs

@@ -38,6 +38,7 @@ lazy_static! {
     pub static ref  FLOAT10_ONE: Float10 = Float10::from_str_native("1").unwrap().with_precision(RADIX_BITS).value();
     pub static ref  FLOAT10_TWO: Float10 = Float10::from_str_native("2").unwrap().with_precision(RADIX_BITS).value();
     pub static ref  FLOAT10_THREE: Float10 = Float10::from_str_native("3").unwrap().with_precision(RADIX_BITS).value();
+    pub static ref  FLOAT10_FIVE: Float10 = Float10::from_str_native("5").unwrap().with_precision(RADIX_BITS).value();
     pub static ref  FLOAT10_NINE: Float10 = Float10::from_str_native("9").unwrap().with_precision(RADIX_BITS).value();
     pub static ref  FLOAT10_TEN: Float10 = Float10::from_str_native("10").unwrap().with_precision(RADIX_BITS).value();
 

+ 28 - 1
src/consensus/state.rs

@@ -354,8 +354,30 @@ impl ConsensusState {
         Self::pid_error(self.extend_leaders_history())
     }
 
+    fn max_windowed_forks(&self) -> Float10 {
+        let mut max : u64= 5;
+        let window_size = 10;
+        let len = self.leaders_history.len();
+        let window_begining = if len <= (window_size+1) {
+            0
+        } else {
+            len - (window_size +1)
+        };
+        for item in &self.leaders_history[window_begining..] {
+            if *item>max {
+                max = *item;
+            }
+        }
+
+        Float10::try_from(max as i64).unwrap().with_precision(constants::RADIX_BITS).value()
+    }
+
+    fn tuned_kp(&self) -> Float10 {
+        (constants::KP.clone() * constants::FLOAT10_FIVE.clone())/self.max_windowed_forks()
+    }
+
     fn weighted_f_dif(&mut self) -> Float10 {
-        constants::KP.clone() * self.f_dif()
+        self.tuned_kp() * self.f_dif()
     }
 
     fn f_der(&self) -> Float10 {
@@ -375,6 +397,7 @@ impl ConsensusState {
         der
     }
 
+
     fn weighted_f_der(&self) -> Float10 {
         constants::KD.clone() * self.f_der()
     }
@@ -390,6 +413,10 @@ impl ConsensusState {
         sum
     }
 
+    fn tuned_ki(&self) -> Float10 {
+        (constants::KI.clone() * constants::FLOAT10_FIVE.clone())/self.max_windowed_forks()
+    }
+
     fn weighted_f_int(&self) -> Float10 {
         constants::KI.clone() * self.f_int()
     }