stats.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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_epsilon_quorum: RegionCount,
  25. }
  26. impl CurrentStats {
  27. pub fn update(&mut self, i: RegionCount, q: &Quorum, force: bool) {
  28. if self.dirty == false && (
  29. self.min_tot_nodes_quorum == i ||
  30. self.max_tot_nodes_quorum == i ||
  31. self.min_tot_honest_quorum == i ||
  32. self.max_tot_honest_quorum == i ||
  33. self.min_tot_malicious_quorum == i ||
  34. self.max_tot_malicious_quorum == i ||
  35. self.min_tot_last_join_quorum == i ||
  36. self.max_tot_last_join_quorum == i ||
  37. self.min_b_0_quorum == i ||
  38. self.max_epsilon_quorum == i) {
  39. self.dirty = true;
  40. }
  41. let nodes = q.tot_honest + q.tot_malicious;
  42. if force || nodes < self.min_tot_nodes {
  43. self.min_tot_nodes = nodes;
  44. self.min_tot_nodes_quorum = i;
  45. }
  46. if force || nodes > self.max_tot_nodes {
  47. self.max_tot_nodes = nodes;
  48. self.max_tot_nodes_quorum = i;
  49. }
  50. if force || q.tot_honest < self.min_tot_honest {
  51. self.min_tot_honest = q.tot_honest;
  52. self.min_tot_honest_quorum = i;
  53. }
  54. if force || q.tot_honest > self.max_tot_honest {
  55. self.max_tot_honest = q.tot_honest;
  56. self.max_tot_honest_quorum = i;
  57. }
  58. if force || q.tot_malicious < self.min_tot_malicious {
  59. self.min_tot_malicious = q.tot_malicious;
  60. self.min_tot_malicious_quorum = i;
  61. }
  62. if force || q.tot_malicious > self.max_tot_malicious {
  63. self.max_tot_malicious = q.tot_malicious;
  64. self.max_tot_malicious_quorum = i;
  65. }
  66. if force || q.tot_last_join < self.min_tot_last_join {
  67. self.min_tot_last_join = q.tot_last_join;
  68. self.min_tot_last_join_quorum = i;
  69. }
  70. if force || q.tot_last_join > self.max_tot_last_join {
  71. self.max_tot_last_join = q.tot_last_join;
  72. self.max_tot_last_join_quorum = i;
  73. }
  74. let b_0: f64 = if q.tot_honest > 0 {
  75. (q.tot_malicious as f64) /
  76. (q.tot_honest as f64)
  77. } else if q.tot_malicious > 0 {
  78. 1000000.0
  79. } else {
  80. 0.0
  81. };
  82. if force || b_0 < self.min_b_0 {
  83. self.min_b_0 = b_0;
  84. self.min_b_0_quorum = i;
  85. }
  86. if force || b_0 > self.max_b_0 {
  87. self.max_b_0 = b_0;
  88. self.max_epsilon_quorum = i;
  89. }
  90. }
  91. #[allow(dead_code)]
  92. pub fn print(&self) {
  93. print!("nodes {} ({}) {} ({}) ", self.min_tot_nodes, self.min_tot_nodes_quorum, self.max_tot_nodes, self.max_tot_nodes_quorum);
  94. print!("honest {} ({}) {} ({}) ", self.min_tot_honest, self.min_tot_honest_quorum, self.max_tot_honest, self.max_tot_honest_quorum);
  95. print!("malicious {} ({}) {} ({}) ", self.min_tot_malicious, self.min_tot_malicious_quorum, self.max_tot_malicious, self.max_tot_malicious_quorum);
  96. print!("lastjoin {} ({}) {} ({}) ", self.min_tot_last_join, self.min_tot_last_join_quorum, self.max_tot_last_join, self.max_tot_last_join_quorum);
  97. println!("b_0 {} ({}) {} ({})", self.min_b_0, self.min_b_0_quorum, self.max_b_0, self.max_epsilon_quorum);
  98. }
  99. }
  100. #[derive(Debug, Clone, Copy, Default)]
  101. pub struct CumulativeStats {
  102. pub max_tot_honest: NodeCount,
  103. pub min_tot_honest: NodeCount,
  104. pub min_tot_malicious: NodeCount,
  105. pub max_tot_malicious: NodeCount,
  106. pub min_tot_nodes: NodeCount,
  107. pub max_tot_nodes: NodeCount,
  108. pub min_age: TimeCount,
  109. pub max_age: TimeCount,
  110. pub min_b_0: f64,
  111. pub max_b_0: f64,
  112. }
  113. impl CumulativeStats {
  114. #[allow(dead_code)]
  115. pub fn print(&self) {
  116. print!("nodes {} {} ", self.min_tot_nodes, self.max_tot_nodes);
  117. print!("honest {} {} ", self.min_tot_honest, self.max_tot_honest);
  118. print!("malicious {} {} ", self.min_tot_malicious, self.max_tot_malicious);
  119. print!("age {} {} ", self.min_age, self.max_age);
  120. println!("b_0 {} {}", self.min_b_0, self.max_b_0);
  121. }
  122. }