stats.rs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. use crate::types::*;
  2. #[derive(Debug, Clone, Copy, Default)]
  3. pub struct CurrentStats {
  4. pub dirty: bool,
  5. pub min_tot_nodes: NodeCount,
  6. pub min_tot_nodes_quorum: RegionCount,
  7. pub max_tot_nodes: NodeCount,
  8. pub max_tot_nodes_quorum: RegionCount,
  9. pub min_tot_honest: NodeCount,
  10. pub min_tot_honest_quorum: RegionCount,
  11. pub max_tot_honest: NodeCount,
  12. pub max_tot_honest_quorum: RegionCount,
  13. pub min_tot_malicious: NodeCount,
  14. pub min_tot_malicious_quorum: RegionCount,
  15. pub max_tot_malicious: NodeCount,
  16. pub max_tot_malicious_quorum: RegionCount,
  17. pub min_tot_last_join: TimeCount,
  18. pub min_tot_last_join_quorum: RegionCount,
  19. pub max_tot_last_join: TimeCount,
  20. pub max_tot_last_join_quorum: RegionCount,
  21. pub min_b_0: f64,
  22. pub min_b_0_quorum: RegionCount,
  23. pub max_b_0: f64,
  24. pub max_b_0_quorum: RegionCount,
  25. }
  26. impl CurrentStats {
  27. pub fn update(&mut self, i: RegionCount, q: &Quorum, force: bool) -> bool {
  28. let mut below_bmax: bool = true;
  29. if self.dirty == false && (
  30. self.min_tot_nodes_quorum == i ||
  31. self.max_tot_nodes_quorum == i ||
  32. self.min_tot_honest_quorum == i ||
  33. self.max_tot_honest_quorum == i ||
  34. self.min_tot_malicious_quorum == i ||
  35. self.max_tot_malicious_quorum == i ||
  36. self.min_tot_last_join_quorum == i ||
  37. self.max_tot_last_join_quorum == i ||
  38. self.min_b_0_quorum == i ||
  39. self.max_b_0_quorum == i) {
  40. self.dirty = true;
  41. }
  42. let nodes = q.tot_honest + q.tot_malicious;
  43. if force || nodes < self.min_tot_nodes {
  44. self.min_tot_nodes = nodes;
  45. self.min_tot_nodes_quorum = i;
  46. }
  47. if force || nodes > self.max_tot_nodes {
  48. self.max_tot_nodes = nodes;
  49. self.max_tot_nodes_quorum = i;
  50. }
  51. if force || q.tot_honest < self.min_tot_honest {
  52. self.min_tot_honest = q.tot_honest;
  53. self.min_tot_honest_quorum = i;
  54. }
  55. if force || q.tot_honest > self.max_tot_honest {
  56. self.max_tot_honest = q.tot_honest;
  57. self.max_tot_honest_quorum = i;
  58. }
  59. if force || q.tot_malicious < self.min_tot_malicious {
  60. self.min_tot_malicious = q.tot_malicious;
  61. self.min_tot_malicious_quorum = i;
  62. }
  63. if force || q.tot_malicious > self.max_tot_malicious {
  64. self.max_tot_malicious = q.tot_malicious;
  65. self.max_tot_malicious_quorum = i;
  66. }
  67. if force || q.tot_last_join < self.min_tot_last_join {
  68. self.min_tot_last_join = q.tot_last_join;
  69. self.min_tot_last_join_quorum = i;
  70. }
  71. if force || q.tot_last_join > self.max_tot_last_join {
  72. self.max_tot_last_join = q.tot_last_join;
  73. self.max_tot_last_join_quorum = i;
  74. }
  75. let b_0 : f64 = (q.tot_malicious as f64) / (q.tot_honest as f64 + q.tot_malicious as f64);
  76. if force || (b_0 < self.min_b_0 && q.tot_malicious != 0) {
  77. self.min_b_0 = b_0;
  78. self.min_b_0_quorum = i;
  79. }
  80. if force || b_0 > self.max_b_0 {
  81. self.max_b_0 = b_0;
  82. self.max_b_0_quorum = i;
  83. if b_0 > 0.25 {
  84. below_bmax = false;
  85. }
  86. }
  87. below_bmax
  88. }
  89. #[allow(dead_code)]
  90. pub fn print(&self) {
  91. print!("nodes {} ({}) {} ({}) ", self.min_tot_nodes, self.min_tot_nodes_quorum, self.max_tot_nodes, self.max_tot_nodes_quorum);
  92. print!("honest {} ({}) {} ({}) ", self.min_tot_honest, self.min_tot_honest_quorum, self.max_tot_honest, self.max_tot_honest_quorum);
  93. print!("malicious {} ({}) {} ({}) ", self.min_tot_malicious, self.min_tot_malicious_quorum, self.max_tot_malicious, self.max_tot_malicious_quorum);
  94. print!("lastjoin {} ({}) {} ({}) ", self.min_tot_last_join, self.min_tot_last_join_quorum, self.max_tot_last_join, self.max_tot_last_join_quorum);
  95. println!("b_0 {} ({}) {} ({})", self.min_b_0, self.min_b_0_quorum, self.max_b_0, self.max_b_0_quorum);
  96. }
  97. }
  98. #[derive(Debug, Clone, Copy, Default)]
  99. pub struct CumulativeStats {
  100. pub max_tot_honest: NodeCount,
  101. pub min_tot_honest: NodeCount,
  102. pub min_tot_malicious: NodeCount,
  103. pub max_tot_malicious: NodeCount,
  104. pub min_tot_nodes: NodeCount,
  105. pub max_tot_nodes: NodeCount,
  106. pub min_age: TimeCount,
  107. pub max_age: TimeCount,
  108. pub min_b_0: f64,
  109. pub max_b_0: f64,
  110. }
  111. impl CumulativeStats {
  112. #[allow(dead_code)]
  113. pub fn print(&self) {
  114. println!("Total nodes: min {} max {}, honest: min {} max {}, malicious: min {} max {}, b_0: min {} max {}",
  115. self.min_tot_nodes, self.max_tot_nodes,
  116. self.min_tot_honest, self.max_tot_honest,
  117. self.min_tot_malicious, self.max_tot_malicious,
  118. self.min_b_0, self.max_b_0);
  119. }
  120. }