1
- use std:: collections:: BTreeSet ;
1
+ use std:: collections:: { BTreeMap , BTreeSet } ;
2
2
3
3
use mina_p2p_messages:: v2:: StateHash ;
4
4
use serde:: { Deserialize , Serialize } ;
@@ -127,7 +127,7 @@ pub struct TransitionFrontierCandidatesState {
127
127
/// or block proof verification. We move them here so that they
128
128
/// consume less memory while still preventing us from triggering
129
129
/// revalidation for an invalid block if we receive it on p2p again.
130
- invalid : BTreeSet < StateHash > ,
130
+ invalid : BTreeMap < StateHash , u32 > ,
131
131
}
132
132
133
133
impl TransitionFrontierCandidatesState {
@@ -136,7 +136,7 @@ impl TransitionFrontierCandidatesState {
136
136
}
137
137
138
138
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 ( )
140
140
}
141
141
142
142
pub ( super ) fn get ( & self , hash : & StateHash ) -> Option < & TransitionFrontierCandidateState > {
@@ -181,8 +181,14 @@ impl TransitionFrontierCandidatesState {
181
181
}
182
182
183
183
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
+ } ) ;
186
192
}
187
193
188
194
pub ( super ) fn set_chain_proof (
@@ -206,6 +212,11 @@ impl TransitionFrontierCandidatesState {
206
212
// verified candidate.
207
213
self . ordered . retain ( |s| {
208
214
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
+
209
220
has_reached_best_candidate = true ;
210
221
}
211
222
0 commit comments