Fix wrong ref counting (#1358)

* Fix wrong ref counting

in case of multiple inserts at same height.

* More warn

---------

Co-authored-by: eskimor <eskimor@no-such-url.com>
This commit is contained in:
eskimor
2023-09-01 20:56:09 +02:00
committed by GitHub
parent a2b6470607
commit 23a2b7b5b3
2 changed files with 10 additions and 5 deletions
@@ -1271,7 +1271,7 @@ impl Initialized {
if import_result.has_fresh_byzantine_threshold_against() {
let blocks_including = self.scraper.get_blocks_including_candidate(&candidate_hash);
for (parent_block_number, parent_block_hash) in &blocks_including {
gum::trace!(
gum::warn!(
target: LOG_TARGET,
?candidate_hash,
?parent_block_number,
@@ -1282,7 +1282,7 @@ impl Initialized {
if blocks_including.len() > 0 {
ctx.send_message(ChainSelectionMessage::RevertBlocks(blocks_including)).await;
} else {
gum::debug!(
gum::warn!(
target: LOG_TARGET,
?candidate_hash,
?session,
@@ -135,11 +135,14 @@ impl ScrapedCandidates {
}
pub fn insert(&mut self, block_number: BlockNumber, candidate_hash: CandidateHash) {
self.candidates.insert(candidate_hash);
self.candidates_by_block_number
if self
.candidates_by_block_number
.entry(block_number)
.or_default()
.insert(candidate_hash);
.insert(candidate_hash)
{
self.candidates.insert(candidate_hash);
}
}
// Used only for tests to verify the pruning doesn't leak data.
@@ -159,6 +162,8 @@ mod scraped_candidates_tests {
let mut candidates = ScrapedCandidates::new();
let target = CandidateHash(BlakeTwo256::hash(&vec![1, 2, 3]));
candidates.insert(1, target);
// Repeated inserts at same height should be fine:
candidates.insert(1, target);
assert!(candidates.contains(&target));