@@ -87,6 +87,11 @@ struct LocalTx<P> {
8787 flooded : bool ,
8888 /// Peers the body was sent to, and when.
8989 sent : Vec < ( P , Instant ) > ,
90+ /// Set when a plan found nothing more to send for this transaction: the
91+ /// route generation it was planned under, and when the first of those
92+ /// sends is old enough to be repeated. Until then, and while the route
93+ /// has not changed, a plan skips it without looking at the peers.
94+ settled : Option < ( u64 , Instant ) > ,
9095}
9196
9297/// Where the transactions go right now.
@@ -111,6 +116,8 @@ pub struct TxRouter<P> {
111116 /// The same transactions, oldest first (stale entries are skipped).
112117 order : VecDeque < ( Instant , TxHash ) > ,
113118 max_local : usize ,
119+ /// Changes whenever the targets or the identified peers do.
120+ generation : u64 ,
114121 /// Transactions peers sent here directly. They were not relayed (the
115122 /// sender delivers to every target itself); they are kept so that this
116123 /// node can join in, and serve the body, if the sender later has to
@@ -137,6 +144,7 @@ impl<P: Copy + Eq + Hash> TxRouter<P> {
137144 local : HashMap :: new ( ) ,
138145 order : VecDeque :: new ( ) ,
139146 max_local,
147+ generation : 0 ,
140148 direct : lru:: LruCache :: new ( std:: num:: NonZeroUsize :: new ( MAX_DIRECT_TXS ) . unwrap ( ) ) ,
141149 }
142150 }
@@ -152,12 +160,15 @@ impl<P: Copy + Eq + Hash> TxRouter<P> {
152160 return false ;
153161 }
154162 self . targets = targets;
163+ self . generation += 1 ;
155164 true
156165 }
157166
158167 /// A connected peer proved that it is `node`.
159168 pub fn peer_identified ( & mut self , peer : P , node : NodeId ) {
160- self . node_of_peer . insert ( peer, node) ;
169+ if self . node_of_peer . insert ( peer, node) != Some ( node) {
170+ self . generation += 1 ;
171+ }
161172 }
162173
163174 pub fn identified_peer_count ( & self ) -> usize {
@@ -173,6 +184,7 @@ impl<P: Copy + Eq + Hash> TxRouter<P> {
173184 /// restarted node has an empty mempool: forget what it was sent.
174185 pub fn peer_disconnected ( & mut self , peer : & P ) {
175186 self . node_of_peer . remove ( peer) ;
187+ self . generation += 1 ;
176188 for entry in self . local . values_mut ( ) {
177189 entry. sent . retain ( |( p, _) | p != peer) ;
178190 }
@@ -182,6 +194,7 @@ impl<P: Copy + Eq + Hash> TxRouter<P> {
182194 pub fn mark_unsent ( & mut self , peer : & P , hashes : & [ TxHash ] ) {
183195 for hash in hashes {
184196 if let Some ( entry) = self . local . get_mut ( hash) {
197+ entry. settled = None ;
185198 entry. sent . retain ( |( p, _) | p != peer) ;
186199 }
187200 }
@@ -206,10 +219,11 @@ impl<P: Copy + Eq + Hash> TxRouter<P> {
206219 added : now,
207220 flooded : false ,
208221 sent : Vec :: new ( ) ,
222+ settled : None ,
209223 } ;
210224 let route = self . route ( ) ;
211225 let mut plan = SendPlan :: default ( ) ;
212- Self :: plan_one ( & route, & mut entry, now, & mut plan) ;
226+ Self :: plan_one ( & route, self . generation , & mut entry, now, & mut plan) ;
213227 self . local . insert ( hash, entry) ;
214228 plan
215229 }
@@ -251,6 +265,7 @@ impl<P: Copy + Eq + Hash> TxRouter<P> {
251265 pub fn mark_unflooded ( & mut self , hashes : & [ TxHash ] ) {
252266 for hash in hashes {
253267 if let Some ( entry) = self . local . get_mut ( hash) {
268+ entry. settled = None ;
254269 entry. flooded = false ;
255270 }
256271 }
@@ -279,9 +294,10 @@ impl<P: Copy + Eq + Hash> TxRouter<P> {
279294 . filter ( |( added, hash) | self . local . get ( hash) . is_some_and ( |e| e. added == * added) )
280295 . map ( |( _, hash) | * hash)
281296 . collect ( ) ;
297+ let generation = self . generation ;
282298 for hash in hashes {
283299 if let Some ( entry) = self . local . get_mut ( & hash) {
284- Self :: plan_one ( & route, entry, now, & mut plan) ;
300+ Self :: plan_one ( & route, generation , entry, now, & mut plan) ;
285301 }
286302 }
287303 plan
@@ -328,23 +344,36 @@ impl<P: Copy + Eq + Hash> TxRouter<P> {
328344 Route :: Direct ( peers)
329345 }
330346
331- fn plan_one ( route : & Route < P > , entry : & mut LocalTx < P > , now : Instant , plan : & mut SendPlan < P > ) {
347+ fn plan_one (
348+ route : & Route < P > ,
349+ generation : u64 ,
350+ entry : & mut LocalTx < P > ,
351+ now : Instant ,
352+ plan : & mut SendPlan < P > ,
353+ ) {
332354 if entry. flooded {
333355 return ;
334356 }
357+ if let Some ( ( planned, due) ) = entry. settled {
358+ if planned == generation && now < due {
359+ return ;
360+ }
361+ }
335362 match route {
336363 Route :: Flood => {
337364 entry. flooded = true ;
338365 entry. sent . clear ( ) ;
339366 plan. flood . push ( Arc :: clone ( & entry. tx ) ) ;
340367 }
341368 Route :: Direct ( peers) => {
369+ let mut next_due = now + RESEND_INTERVAL ;
342370 for peer in peers {
343- let recently = entry. sent . iter ( ) . any ( |( p, at) | {
344- p == peer && now. saturating_duration_since ( * at) < RESEND_INTERVAL
345- } ) ;
346- if recently {
347- continue ;
371+ let sent_at = entry. sent . iter ( ) . find ( |( p, _) | p == peer) . map ( |( _, at) | * at) ;
372+ if let Some ( at) = sent_at {
373+ if now. saturating_duration_since ( at) < RESEND_INTERVAL {
374+ next_due = next_due. min ( at + RESEND_INTERVAL ) ;
375+ continue ;
376+ }
348377 }
349378 entry. sent . retain ( |( p, _) | p != peer) ;
350379 entry. sent . push ( ( * peer, now) ) ;
@@ -353,6 +382,7 @@ impl<P: Copy + Eq + Hash> TxRouter<P> {
353382 None => plan. direct . push ( ( * peer, vec ! [ Arc :: clone( & entry. tx) ] ) ) ,
354383 }
355384 }
385+ entry. settled = Some ( ( generation, next_due) ) ;
356386 }
357387 }
358388 }
@@ -630,6 +660,28 @@ mod tests {
630660 assert ! ( router. body( b. hash( ) ) . is_some( ) ) ;
631661 }
632662
663+ #[ test]
664+ fn a_plan_with_unchanged_targets_repeats_a_send_only_after_the_interval ( ) {
665+ // With every validator as a target the list never changes: a body a
666+ // peer dropped is only recovered by the periodic plan.
667+ let mut router = mesh ( ) ;
668+ router. set_targets ( Some ( vec ! [ LEADER1 , LEADER2 , OTHER ] ) ) ;
669+ let now = Instant :: now ( ) ;
670+ let a = tx ( 1 ) ;
671+ assert_eq ! ( router. add_local( a. clone( ) , now) . direct. len( ) , 3 ) ;
672+ for secs in [ 1 , 10 , 59 ] {
673+ assert ! ( router. plan_all( now + Duration :: from_secs( secs) ) . is_empty( ) ) ;
674+ }
675+ let plan = router. plan_all ( now + RESEND_INTERVAL ) ;
676+ assert_eq ! ( plan. direct. len( ) , 3 ) ;
677+ assert ! ( router. plan_all( now + RESEND_INTERVAL + Duration :: from_secs( 1 ) ) . is_empty( ) ) ;
678+ // A failed send is repeated at the next plan, whatever the interval.
679+ router. mark_unsent ( & P2 , & [ * a. hash ( ) ] ) ;
680+ let plan = router. plan_all ( now + RESEND_INTERVAL + Duration :: from_secs ( 2 ) ) ;
681+ assert_eq ! ( direct_hashes( & plan, P2 ) , vec![ * a. hash( ) ] ) ;
682+ assert ! ( direct_hashes( & plan, P1 ) . is_empty( ) ) ;
683+ }
684+
633685 #[ test]
634686 fn a_flood_that_reached_nobody_is_repeated ( ) {
635687 let mut router = Router :: new ( ) ;
0 commit comments