Skip to content

Commit e00e528

Browse files
committed
fix(transition_frontier/candidates): prune invalid candidates list to prevent it from forever growing
1 parent 90aed9b commit e00e528

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

node/src/transition_frontier/candidate/transition_frontier_candidate_state.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::BTreeSet;
1+
use std::collections::{BTreeMap, BTreeSet};
22

33
use mina_p2p_messages::v2::StateHash;
44
use serde::{Deserialize, Serialize};
@@ -127,7 +127,7 @@ pub struct TransitionFrontierCandidatesState {
127127
/// or block proof verification. We move them here so that they
128128
/// consume less memory while still preventing us from triggering
129129
/// revalidation for an invalid block if we receive it on p2p again.
130-
invalid: BTreeSet<StateHash>,
130+
invalid: BTreeMap<StateHash, u32>,
131131
}
132132

133133
impl TransitionFrontierCandidatesState {
@@ -136,7 +136,7 @@ impl TransitionFrontierCandidatesState {
136136
}
137137

138138
pub fn contains(&self, hash: &StateHash) -> bool {
139-
self.invalid.contains(hash) || self.get(hash).is_some()
139+
self.invalid.contains_key(hash) || self.get(hash).is_some()
140140
}
141141

142142
pub(super) fn get(&self, hash: &StateHash) -> Option<&TransitionFrontierCandidateState> {
@@ -181,8 +181,14 @@ impl TransitionFrontierCandidatesState {
181181
}
182182

183183
pub(super) fn invalidate(&mut self, hash: &StateHash) {
184-
self.ordered.retain(|s| s.block.hash() != hash);
185-
self.invalid.insert(hash.clone());
184+
self.ordered.retain(|s| {
185+
if s.block.hash() == hash {
186+
self.invalid.insert(hash.clone(), s.block.global_slot());
187+
false
188+
} else {
189+
true
190+
}
191+
});
186192
}
187193

188194
pub(super) fn set_chain_proof(
@@ -206,6 +212,11 @@ impl TransitionFrontierCandidatesState {
206212
// verified candidate.
207213
self.ordered.retain(|s| {
208214
if s.block.hash() == &best_candidate_hash {
215+
// prune all invalid block hashes which are for older
216+
// slots than the current best candidate.
217+
let best_candidate_slot = s.block.global_slot();
218+
self.invalid.retain(|_, slot| *slot >= best_candidate_slot);
219+
209220
has_reached_best_candidate = true;
210221
}
211222

0 commit comments

Comments
 (0)