@@ -16,7 +16,6 @@ use alloc::sync::Arc;
1616use bitcoin:: hashes:: hex:: FromHex ;
1717use bitcoin:: { BlockHash , Txid } ;
1818
19- use core:: cmp;
2019use core:: future:: Future ;
2120use core:: ops:: Deref ;
2221use core:: pin:: Pin ;
@@ -938,14 +937,22 @@ where
938937 for monitor_key in monitor_keys {
939938 let monitor_name = MonitorName :: from_str ( & monitor_key) ?;
940939 let ( _, current_monitor) = self . read_monitor ( & monitor_name, & monitor_key) . await ?;
941- let primary = CHANNEL_MONITOR_UPDATE_PERSISTENCE_PRIMARY_NAMESPACE ;
942- let updates = self . kv_store . list ( primary, monitor_key. as_str ( ) ) . await ?;
943- for update in updates {
944- let update_name = UpdateName :: new ( update) ?;
945- // if the update_id is lower than the stored monitor, delete
946- if update_name. 0 <= current_monitor. get_latest_update_id ( ) {
947- self . kv_store . remove ( primary, & monitor_key, update_name. as_str ( ) , lazy) . await ?;
948- }
940+ let latest_update_id = current_monitor. get_latest_update_id ( ) ;
941+ self . cleanup_stale_updates_for_monitor_to ( & monitor_key, latest_update_id, lazy) . await ;
942+ }
943+ Ok ( ( ) )
944+ }
945+
946+ async fn cleanup_stale_updates_for_monitor_to (
947+ & self , monitor_key : & str , latest_update_id : u64 , lazy : bool ,
948+ ) -> Result < ( ) , io:: Error > {
949+ let primary = CHANNEL_MONITOR_UPDATE_PERSISTENCE_PRIMARY_NAMESPACE ;
950+ let updates = self . kv_store . list ( primary, monitor_key) . await ?;
951+ for update in updates {
952+ let update_name = UpdateName :: new ( update) ?;
953+ // if the update_id is lower than the stored monitor, delete
954+ if update_name. 0 <= latest_update_id {
955+ self . kv_store . remove ( primary, monitor_key, update_name. as_str ( ) , lazy) . await ?;
949956 }
950957 }
951958 Ok ( ( ) )
@@ -989,40 +996,24 @@ where
989996 . write ( primary, & monitor_key, update_name. as_str ( ) , update. encode ( ) )
990997 . await
991998 } else {
992- // In case of channel-close monitor update, we need to read old monitor before persisting
993- // the new one in order to determine the cleanup range.
994- let maybe_old_monitor = match monitor. get_latest_update_id ( ) {
995- LEGACY_CLOSED_CHANNEL_UPDATE_ID => {
996- let monitor_key = monitor_name. to_string ( ) ;
997- self . read_monitor ( & monitor_name, & monitor_key) . await . ok ( )
998- } ,
999- _ => None ,
1000- } ;
1001-
1002999 // We could write this update, but it meets criteria of our design that calls for a full monitor write.
10031000 let write_status = self . persist_new_channel ( monitor_name, monitor) . await ;
10041001
10051002 if let Ok ( ( ) ) = write_status {
10061003 let channel_closed_legacy =
10071004 monitor. get_latest_update_id ( ) == LEGACY_CLOSED_CHANNEL_UPDATE_ID ;
1008- let cleanup_range = if channel_closed_legacy {
1009- // If there is an error while reading old monitor, we skip clean up.
1010- maybe_old_monitor. map ( |( _, ref old_monitor) | {
1011- let start = old_monitor. get_latest_update_id ( ) ;
1012- // We never persist an update with the legacy closed update_id
1013- let end = cmp:: min (
1014- start. saturating_add ( self . maximum_pending_updates ) ,
1015- LEGACY_CLOSED_CHANNEL_UPDATE_ID - 1 ,
1016- ) ;
1017- ( start, end)
1018- } )
1005+ let latest_update_id = monitor. get_latest_update_id ( ) ;
1006+ if channel_closed_legacy {
1007+ let monitor_key = monitor_name. to_string ( ) ;
1008+ self . cleanup_stale_updates_for_monitor_to (
1009+ & monitor_key,
1010+ latest_update_id,
1011+ true ,
1012+ )
1013+ . await ;
10191014 } else {
1020- let end = monitor . get_latest_update_id ( ) ;
1015+ let end = latest_update_id ;
10211016 let start = end. saturating_sub ( self . maximum_pending_updates ) ;
1022- Some ( ( start, end) )
1023- } ;
1024-
1025- if let Some ( ( start, end) ) = cleanup_range {
10261017 self . cleanup_in_range ( monitor_name, start, end) . await ;
10271018 }
10281019 }
@@ -1263,6 +1254,7 @@ mod tests {
12631254 use crate :: util:: test_utils:: { self , TestStore } ;
12641255 use crate :: { check_added_monitors, check_closed_broadcast} ;
12651256 use bitcoin:: hashes:: hex:: FromHex ;
1257+ use core:: cmp;
12661258
12671259 const EXPECTED_UPDATES_PER_PAYMENT : u64 = 5 ;
12681260
0 commit comments