|
@@ -68,6 +68,10 @@ pub struct ConsensusState {
|
|
|
pub slot_checkpoints: Vec<SlotCheckpoint>,
|
|
pub slot_checkpoints: Vec<SlotCheckpoint>,
|
|
|
/// Leaders count history
|
|
/// Leaders count history
|
|
|
pub leaders_history: Vec<u64>,
|
|
pub leaders_history: Vec<u64>,
|
|
|
|
|
+ /// controller output history
|
|
|
|
|
+ pub f_history: Vec<Float10>,
|
|
|
|
|
+ /// controller proportional error history
|
|
|
|
|
+ pub err_history: Vec<Float10>,
|
|
|
// TODO: Aren't these already in db after finalization?
|
|
// TODO: Aren't these already in db after finalization?
|
|
|
/// Canonical competing coins
|
|
/// Canonical competing coins
|
|
|
pub coins: Vec<LeadCoin>,
|
|
pub coins: Vec<LeadCoin>,
|
|
@@ -102,6 +106,9 @@ impl ConsensusState {
|
|
|
epoch_eta: pallas::Base::zero(),
|
|
epoch_eta: pallas::Base::zero(),
|
|
|
slot_checkpoints: vec![],
|
|
slot_checkpoints: vec![],
|
|
|
leaders_history: vec![0],
|
|
leaders_history: vec![0],
|
|
|
|
|
+ f_history: vec![constants::FLOAT10_ZERO.clone()],
|
|
|
|
|
+ err_history: vec![constants::FLOAT10_ZERO.clone(),
|
|
|
|
|
+ constants::FLOAT10_ZERO.clone()],
|
|
|
coins: vec![],
|
|
coins: vec![],
|
|
|
coins_tree: BridgeTree::<MerkleNode, MERKLE_DEPTH>::new(constants::EPOCH_LENGTH * 100),
|
|
coins_tree: BridgeTree::<MerkleNode, MERKLE_DEPTH>::new(constants::EPOCH_LENGTH * 100),
|
|
|
nullifiers: vec![],
|
|
nullifiers: vec![],
|
|
@@ -189,7 +196,9 @@ impl ConsensusState {
|
|
|
/// Generate current slot checkpoint
|
|
/// Generate current slot checkpoint
|
|
|
fn generate_slot_checkpoint(&mut self, sigma1: pallas::Base, sigma2: pallas::Base) {
|
|
fn generate_slot_checkpoint(&mut self, sigma1: pallas::Base, sigma2: pallas::Base) {
|
|
|
let slot = self.current_slot();
|
|
let slot = self.current_slot();
|
|
|
- let checkpoint = SlotCheckpoint { slot, eta: self.epoch_eta, sigma1, sigma2 };
|
|
|
|
|
|
|
+ let eta = self.get_eta();
|
|
|
|
|
+ info!("generate_slot_checkpoint: slot: {:?}, eta: {:?}", slot, eta);
|
|
|
|
|
+ let checkpoint = SlotCheckpoint { slot, eta, sigma1, sigma2 };
|
|
|
self.slot_checkpoints.push(checkpoint);
|
|
self.slot_checkpoints.push(checkpoint);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -277,10 +286,10 @@ impl ConsensusState {
|
|
|
// Temporarily, we compete with fixed stake.
|
|
// Temporarily, we compete with fixed stake.
|
|
|
// This stake should be based on how many nodes we want to run, and they all
|
|
// This stake should be based on how many nodes we want to run, and they all
|
|
|
// must sum to initial distribution total coins.
|
|
// must sum to initial distribution total coins.
|
|
|
- let stake = self.initial_distribution;
|
|
|
|
|
|
|
+ //let stake = self.initial_distribution;
|
|
|
let coin = LeadCoin::new(
|
|
let coin = LeadCoin::new(
|
|
|
eta,
|
|
eta,
|
|
|
- stake,
|
|
|
|
|
|
|
+ 200,
|
|
|
slot,
|
|
slot,
|
|
|
epoch_secrets.secret_keys[0].inner(),
|
|
epoch_secrets.secret_keys[0].inner(),
|
|
|
epoch_secrets.merkle_roots[0],
|
|
epoch_secrets.merkle_roots[0],
|
|
@@ -375,7 +384,8 @@ impl ConsensusState {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
fn f_dif(&mut self) -> Float10 {
|
|
fn f_dif(&mut self) -> Float10 {
|
|
|
- Self::pid_error(self.extend_leaders_history())
|
|
|
|
|
|
|
+ let len = self.leaders_history.len();
|
|
|
|
|
+ Self::pid_error(Float10::try_from(self.leaders_history[len-1] as i64).unwrap().with_precision(constants::RADIX_BITS).value())
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
fn max_windowed_forks(&self) -> Float10 {
|
|
fn max_windowed_forks(&self) -> Float10 {
|
|
@@ -393,7 +403,8 @@ impl ConsensusState {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
fn tuned_kp(&self) -> Float10 {
|
|
fn tuned_kp(&self) -> Float10 {
|
|
|
- (constants::KP.clone() * constants::FLOAT10_FIVE.clone()) / self.max_windowed_forks()
|
|
|
|
|
|
|
+ //(constants::KP.clone() * constants::FLOAT10_FIVE.clone()) / self.max_windowed_forks()
|
|
|
|
|
+ constants::KP.clone()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
fn weighted_f_dif(&mut self) -> Float10 {
|
|
fn weighted_f_dif(&mut self) -> Float10 {
|
|
@@ -428,7 +439,8 @@ impl ConsensusState {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
fn tuned_ki(&self) -> Float10 {
|
|
fn tuned_ki(&self) -> Float10 {
|
|
|
- (constants::KI.clone() * constants::FLOAT10_FIVE.clone()) / self.max_windowed_forks()
|
|
|
|
|
|
|
+ //(constants::KI.clone() * constants::FLOAT10_FIVE.clone()) / self.max_windowed_forks()
|
|
|
|
|
+ constants::KI.clone()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
fn weighted_f_int(&self) -> Float10 {
|
|
fn weighted_f_int(&self) -> Float10 {
|
|
@@ -449,9 +461,7 @@ impl ConsensusState {
|
|
|
count
|
|
count
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /// the probability inverse of winnig lottery having all the stake
|
|
|
|
|
- /// returns f
|
|
|
|
|
- fn win_inv_prob_with_full_stake(&mut self) -> Float10 {
|
|
|
|
|
|
|
+ fn pid(&mut self) -> Float10 {
|
|
|
let p = self.weighted_f_dif();
|
|
let p = self.weighted_f_dif();
|
|
|
let i = self.weighted_f_int();
|
|
let i = self.weighted_f_int();
|
|
|
let d = self.weighted_f_der();
|
|
let d = self.weighted_f_der();
|
|
@@ -459,7 +469,35 @@ impl ConsensusState {
|
|
|
info!(target: "consensus::state", "win_inv_prob_with_full_stake(): PID I: {:?}", i);
|
|
info!(target: "consensus::state", "win_inv_prob_with_full_stake(): PID I: {:?}", i);
|
|
|
info!(target: "consensus::state", "win_inv_prob_with_full_stake(): PID D: {:?}", d);
|
|
info!(target: "consensus::state", "win_inv_prob_with_full_stake(): PID D: {:?}", d);
|
|
|
let f = p + i.clone() + d;
|
|
let f = p + i.clone() + d;
|
|
|
- info!(target: "consensus::state", "win_inv_prob_with_full_stake(): PID f: {}", f);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ info!("win_inv_prob_with_full_stake(): PID f: {}", f);
|
|
|
|
|
+ f
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fn discrete_pid(&mut self) -> Float10 {
|
|
|
|
|
+ let k1 = constants::KP.clone() +
|
|
|
|
|
+ constants::KI.clone() +
|
|
|
|
|
+ constants::KD.clone();
|
|
|
|
|
+ let k2 = constants::FLOAT10_NEG_ONE.clone() * constants::KP.clone() -
|
|
|
|
|
+ constants::FLOAT10_NEG_TWO.clone() * constants::KD.clone();
|
|
|
|
|
+ let k3 = constants::KD.clone();
|
|
|
|
|
+ let f_len = self.f_history.len();
|
|
|
|
|
+ let err = self.f_dif();
|
|
|
|
|
+ let err_len = self.err_history.len();
|
|
|
|
|
+ let ret = self.f_history[f_len-1].clone() +
|
|
|
|
|
+ k1 * err.clone() +
|
|
|
|
|
+ k2 * self.err_history[err_len-1].clone() +
|
|
|
|
|
+ k3 * self.err_history[err_len-2].clone();
|
|
|
|
|
+ self.f_history.push(ret.clone());
|
|
|
|
|
+ self.err_history.push(err);
|
|
|
|
|
+ ret
|
|
|
|
|
+ }
|
|
|
|
|
+ /// the probability inverse of winnig lottery having all the stake
|
|
|
|
|
+ /// returns f
|
|
|
|
|
+ fn win_inv_prob_with_full_stake(&mut self) -> Float10 {
|
|
|
|
|
+ self.extend_leaders_history();
|
|
|
|
|
+ //
|
|
|
|
|
+ let f = self.discrete_pid();
|
|
|
if f == constants::FLOAT10_ZERO.clone() {
|
|
if f == constants::FLOAT10_ZERO.clone() {
|
|
|
return constants::MIN_F.clone()
|
|
return constants::MIN_F.clone()
|
|
|
} else if f >= constants::FLOAT10_ONE.clone() {
|
|
} else if f >= constants::FLOAT10_ONE.clone() {
|
|
@@ -469,8 +507,8 @@ impl ConsensusState {
|
|
|
if hist_len > 3 &&
|
|
if hist_len > 3 &&
|
|
|
self.leaders_history[hist_len - 1] == 0 &&
|
|
self.leaders_history[hist_len - 1] == 0 &&
|
|
|
self.leaders_history[hist_len - 2] == 0 &&
|
|
self.leaders_history[hist_len - 2] == 0 &&
|
|
|
- self.leaders_history[hist_len - 3] == 0 &&
|
|
|
|
|
- i == constants::FLOAT10_ZERO.clone()
|
|
|
|
|
|
|
+ self.leaders_history[hist_len - 3] == 0
|
|
|
|
|
+ //&& i == constants::FLOAT10_ZERO.clone()
|
|
|
{
|
|
{
|
|
|
return f * constants::DEG_RATE.clone().powf(self.zero_leads_len())
|
|
return f * constants::DEG_RATE.clone().powf(self.zero_leads_len())
|
|
|
}
|
|
}
|
|
@@ -615,6 +653,7 @@ impl ConsensusState {
|
|
|
|
|
|
|
|
/// Auxillary function to set nodes leaders count history to the largest fork sequence
|
|
/// Auxillary function to set nodes leaders count history to the largest fork sequence
|
|
|
/// of leaders, by using provided index.
|
|
/// of leaders, by using provided index.
|
|
|
|
|
+
|
|
|
pub fn set_leader_history(&mut self, index: i64, current_slot: u64) {
|
|
pub fn set_leader_history(&mut self, index: i64, current_slot: u64) {
|
|
|
// Check if we found longest fork to extract sequence from
|
|
// Check if we found longest fork to extract sequence from
|
|
|
match index {
|
|
match index {
|
|
@@ -633,7 +672,7 @@ impl ConsensusState {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- self.leaders_history.push(0);
|
|
|
|
|
|
|
+ //self.leaders_history.push(0);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// Utility function to extract leader selection lottery randomness(eta),
|
|
/// Utility function to extract leader selection lottery randomness(eta),
|