|
5 | 5 | use std::{collections::BTreeMap, fmt::Display}; |
6 | 6 |
|
7 | 7 | use iota_types::{digests::ConsensusCommitDigest, messages_consensus::ConsensusTransaction}; |
| 8 | +use itertools::Itertools as _; |
8 | 9 |
|
9 | 10 | use crate::consensus_types::AuthorityIndex; |
10 | 11 | /// A list of tuples of: |
@@ -135,4 +136,90 @@ impl ConsensusOutputAPI for starfish_core::CommittedSubDag { |
135 | 136 | }); |
136 | 137 | num_of_committed_headers.into_iter().collect() |
137 | 138 | } |
| 139 | + |
| 140 | + fn misbehavior_counts(&self) -> ConsensusOutputMisbehaviorCounts { |
| 141 | + let (faulty_blocks_provable, faulty_blocks_unprovable, missing_proposals, equivocations) = |
| 142 | + self.misbehavior_counts |
| 143 | + .iter() |
| 144 | + .map(|counts| match counts { |
| 145 | + starfish_core::MisbehaviorCounts::V1(v1) => ( |
| 146 | + v1.faulty_blocks_provable, |
| 147 | + v1.faulty_blocks_unprovable, |
| 148 | + v1.missing_proposals, |
| 149 | + v1.equivocations, |
| 150 | + ), |
| 151 | + }) |
| 152 | + .multiunzip(); |
| 153 | + ConsensusOutputMisbehaviorCounts { |
| 154 | + faulty_blocks_provable, |
| 155 | + faulty_blocks_unprovable, |
| 156 | + missing_proposals, |
| 157 | + equivocations, |
| 158 | + } |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +#[cfg(test)] |
| 163 | +mod tests { |
| 164 | + use starfish_core::{ |
| 165 | + BlockRef, CommitDigest, CommitRef, CommittedSubDag, MisbehaviorCounts, MisbehaviorCountsV1, |
| 166 | + VerifiedBlockHeader, |
| 167 | + }; |
| 168 | + |
| 169 | + use super::*; |
| 170 | + |
| 171 | + #[test] |
| 172 | + fn test_misbehavior_counts_transposes_per_authority_to_per_field_vecs() { |
| 173 | + let counts = vec![ |
| 174 | + MisbehaviorCounts::V1(MisbehaviorCountsV1 { |
| 175 | + faulty_blocks_provable: 1, |
| 176 | + faulty_blocks_unprovable: 2, |
| 177 | + missing_proposals: 3, |
| 178 | + equivocations: 4, |
| 179 | + }), |
| 180 | + MisbehaviorCounts::V1(MisbehaviorCountsV1 { |
| 181 | + faulty_blocks_provable: 10, |
| 182 | + faulty_blocks_unprovable: 20, |
| 183 | + missing_proposals: 30, |
| 184 | + equivocations: 40, |
| 185 | + }), |
| 186 | + MisbehaviorCounts::default(), |
| 187 | + ]; |
| 188 | + |
| 189 | + let subdag = CommittedSubDag::new( |
| 190 | + BlockRef::MIN, |
| 191 | + Vec::<VerifiedBlockHeader>::new(), |
| 192 | + vec![], |
| 193 | + vec![], |
| 194 | + 0, |
| 195 | + CommitRef::new(1, CommitDigest::MIN), |
| 196 | + vec![], |
| 197 | + counts, |
| 198 | + ); |
| 199 | + |
| 200 | + let out = subdag.misbehavior_counts(); |
| 201 | + assert_eq!(out.faulty_blocks_provable, vec![1, 10, 0]); |
| 202 | + assert_eq!(out.faulty_blocks_unprovable, vec![2, 20, 0]); |
| 203 | + assert_eq!(out.missing_proposals, vec![3, 30, 0]); |
| 204 | + assert_eq!(out.equivocations, vec![4, 40, 0]); |
| 205 | + } |
| 206 | + |
| 207 | + #[test] |
| 208 | + fn test_misbehavior_counts_empty_snapshot_produces_empty_vecs() { |
| 209 | + let subdag = CommittedSubDag::new( |
| 210 | + BlockRef::MIN, |
| 211 | + Vec::<VerifiedBlockHeader>::new(), |
| 212 | + vec![], |
| 213 | + vec![], |
| 214 | + 0, |
| 215 | + CommitRef::new(1, CommitDigest::MIN), |
| 216 | + vec![], |
| 217 | + vec![], |
| 218 | + ); |
| 219 | + let out = subdag.misbehavior_counts(); |
| 220 | + assert!(out.faulty_blocks_provable.is_empty()); |
| 221 | + assert!(out.faulty_blocks_unprovable.is_empty()); |
| 222 | + assert!(out.missing_proposals.is_empty()); |
| 223 | + assert!(out.equivocations.is_empty()); |
| 224 | + } |
138 | 225 | } |
0 commit comments