From fdefc670a296f199c6b76ae5eb65f1ef0d004b3f Mon Sep 17 00:00:00 2001 From: Andrei <92177534+andrei-21@users.noreply.github.com> Date: Fri, 4 Oct 2024 10:24:38 +0100 Subject: [PATCH] Return the same pending activities (#1209) No matter of the number of activities were requested. Before this, we were searching for pending activities within the initially requested number of activities (with some leeway), resulting in potentially returning a different number of pending activities. This PR does not address the original issue directly, but ensures that the code now consistently returns some pending activities. --- src/lib.rs | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 37db3708..6840cbe4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1058,18 +1058,21 @@ impl LightningNode { .lock_unwrap() .retrieve_created_invoices(number_of_completed_activities)?; - let activities = self.multiplex_activities(breez_activities, created_invoices); - - // Split by state. - let mut pending_activities = Vec::new(); - let mut completed_activities = Vec::new(); - activities.into_iter().for_each(|m| { - if m.is_pending() { - pending_activities.push(m) - } else { - completed_activities.push(m) - } - }); + let number_of_created_invoices = created_invoices.len(); + let mut activities = self.multiplex_activities(breez_activities, created_invoices); + activities.sort_by_cached_key(|m| Reverse(m.get_time())); + + // To produce stable output we look for pending activities only in the + // first `look_for_pending` latest activities. + // Yes, we risk to omit old pending ones. + let look_for_pending = LEEWAY_FOR_PENDING_PAYMENTS as usize + number_of_created_invoices; + let mut tail_activities = activities.split_off(look_for_pending); + let head_activities = activities; + let (mut pending_activities, mut completed_activities): (Vec<_>, Vec<_>) = + head_activities.into_iter().partition(Activity::is_pending); + tail_activities.retain(|m| !m.is_pending()); + completed_activities.append(&mut tail_activities); + completed_activities.truncate(number_of_completed_activities as usize); if let Some(in_progress_swap) = self .rt @@ -1099,10 +1102,8 @@ impl LightningNode { }, }) } + pending_activities.sort_by_cached_key(|m| Reverse(m.get_time())); - pending_activities.sort_by_key(|m| Reverse(m.get_time())); - completed_activities.sort_by_key(|m| Reverse(m.get_time())); - completed_activities.truncate(number_of_completed_activities as usize); Ok(ListActivitiesResponse { pending_activities, completed_activities,