|
| 1 | +use polkadot_primitives::v2::{BlockNumber, CandidateHash}; |
| 2 | +use std::collections::{BTreeMap, HashMap, HashSet}; |
| 3 | + |
| 4 | +/// Keeps `CandidateHash` in reference counted way. |
| 5 | +/// Each `insert` saves a value with `reference count == 1` or increases the reference |
| 6 | +/// count if the value already exists. |
| 7 | +/// Each `remove` decreases the reference count for the corresponding `CandidateHash`. |
| 8 | +/// If the reference count reaches 0 - the value is removed. |
| 9 | +struct RefCountedCandidates { |
| 10 | + candidates: HashMap<CandidateHash, usize>, |
| 11 | +} |
| 12 | + |
| 13 | +impl RefCountedCandidates { |
| 14 | + pub fn new() -> Self { |
| 15 | + Self { candidates: HashMap::new() } |
| 16 | + } |
| 17 | + // If `CandidateHash` doesn't exist in the `HashMap` it is created and its reference |
| 18 | + // count is set to 1. |
| 19 | + // If `CandidateHash` already exists in the `HashMap` its reference count is increased. |
| 20 | + pub fn insert(&mut self, candidate: CandidateHash) { |
| 21 | + *self.candidates.entry(candidate).or_default() += 1; |
| 22 | + } |
| 23 | + |
| 24 | + // If a `CandidateHash` with reference count equals to 1 is about to be removed - the |
| 25 | + // candidate is dropped from the container too. |
| 26 | + // If a `CandidateHash` with reference count biger than 1 is about to be removed - the |
| 27 | + // reference count is decreased and the candidate remains in the container. |
| 28 | + pub fn remove(&mut self, candidate: &CandidateHash) { |
| 29 | + match self.candidates.get_mut(candidate) { |
| 30 | + Some(v) if *v > 1 => *v -= 1, |
| 31 | + Some(v) => { |
| 32 | + assert!(*v == 1); |
| 33 | + self.candidates.remove(candidate); |
| 34 | + }, |
| 35 | + None => {}, |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + pub fn contains(&self, candidate: &CandidateHash) -> bool { |
| 40 | + self.candidates.contains_key(&candidate) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +#[cfg(test)] |
| 45 | +mod ref_counted_candidates_tests { |
| 46 | + use super::*; |
| 47 | + use polkadot_primitives::v2::{BlakeTwo256, HashT}; |
| 48 | + |
| 49 | + #[test] |
| 50 | + fn element_is_removed_when_refcount_reaches_zero() { |
| 51 | + let mut container = RefCountedCandidates::new(); |
| 52 | + |
| 53 | + let zero = CandidateHash(BlakeTwo256::hash(&vec![0])); |
| 54 | + let one = CandidateHash(BlakeTwo256::hash(&vec![1])); |
| 55 | + // add two separate candidates |
| 56 | + container.insert(zero); // refcount == 1 |
| 57 | + container.insert(one); |
| 58 | + |
| 59 | + // and increase the reference count for the first |
| 60 | + container.insert(zero); // refcount == 2 |
| 61 | + |
| 62 | + assert!(container.contains(&zero)); |
| 63 | + assert!(container.contains(&one)); |
| 64 | + |
| 65 | + // remove once -> refcount == 1 |
| 66 | + container.remove(&zero); |
| 67 | + assert!(container.contains(&zero)); |
| 68 | + assert!(container.contains(&one)); |
| 69 | + |
| 70 | + // remove once -> refcount == 0 |
| 71 | + container.remove(&zero); |
| 72 | + assert!(!container.contains(&zero)); |
| 73 | + assert!(container.contains(&one)); |
| 74 | + |
| 75 | + // remove the other element |
| 76 | + container.remove(&one); |
| 77 | + assert!(!container.contains(&zero)); |
| 78 | + assert!(!container.contains(&one)); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/// Keeps track of scraped candidates. Supports `insert`, `remove_up_to_height` and `contains` |
| 83 | +/// operations. |
| 84 | +pub struct ScrapedCandidates { |
| 85 | + /// Main data structure which keeps the candidates we know about. `contains` does lookups only here. |
| 86 | + candidates: RefCountedCandidates, |
| 87 | + /// Keeps track at which block number a candidate was inserted. Used in `remove_up_to_height`. |
| 88 | + /// Without this tracking we won't be able to remove all candidates before block X. |
| 89 | + candidates_by_block_number: BTreeMap<BlockNumber, HashSet<CandidateHash>>, |
| 90 | +} |
| 91 | + |
| 92 | +impl ScrapedCandidates { |
| 93 | + pub fn new() -> Self { |
| 94 | + Self { |
| 95 | + candidates: RefCountedCandidates::new(), |
| 96 | + candidates_by_block_number: BTreeMap::new(), |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + pub fn contains(&self, candidate_hash: &CandidateHash) -> bool { |
| 101 | + self.candidates.contains(candidate_hash) |
| 102 | + } |
| 103 | + |
| 104 | + // Removes all candidates up to a given height. The candidates at the block height are NOT removed. |
| 105 | + pub fn remove_up_to_height(&mut self, height: &BlockNumber) { |
| 106 | + let not_stale = self.candidates_by_block_number.split_off(&height); |
| 107 | + let stale = std::mem::take(&mut self.candidates_by_block_number); |
| 108 | + self.candidates_by_block_number = not_stale; |
| 109 | + for candidates in stale.values() { |
| 110 | + for c in candidates { |
| 111 | + self.candidates.remove(c); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + pub fn insert(&mut self, block_number: BlockNumber, candidate_hash: CandidateHash) { |
| 117 | + self.candidates.insert(candidate_hash); |
| 118 | + self.candidates_by_block_number |
| 119 | + .entry(block_number) |
| 120 | + .or_default() |
| 121 | + .insert(candidate_hash); |
| 122 | + } |
| 123 | + |
| 124 | + // Used only for tests to verify the pruning doesn't leak data. |
| 125 | + #[cfg(test)] |
| 126 | + pub fn candidates_by_block_number_is_empty(&self) -> bool { |
| 127 | + self.candidates_by_block_number.is_empty() |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +#[cfg(test)] |
| 132 | +mod scraped_candidates_tests { |
| 133 | + use super::*; |
| 134 | + use polkadot_primitives::v2::{BlakeTwo256, HashT}; |
| 135 | + |
| 136 | + #[test] |
| 137 | + fn stale_candidates_are_removed() { |
| 138 | + let mut candidates = ScrapedCandidates::new(); |
| 139 | + let target = CandidateHash(BlakeTwo256::hash(&vec![1, 2, 3])); |
| 140 | + candidates.insert(1, target); |
| 141 | + |
| 142 | + assert!(candidates.contains(&target)); |
| 143 | + |
| 144 | + candidates.remove_up_to_height(&2); |
| 145 | + assert!(!candidates.contains(&target)); |
| 146 | + assert!(candidates.candidates_by_block_number_is_empty()); |
| 147 | + } |
| 148 | +} |
0 commit comments