@@ -16,7 +16,6 @@ use alloc::sync::Arc;
16
16
use bitcoin:: hashes:: hex:: FromHex ;
17
17
use bitcoin:: { BlockHash , Txid } ;
18
18
19
- use core:: cmp;
20
19
use core:: future:: Future ;
21
20
use core:: ops:: Deref ;
22
21
use core:: pin:: Pin ;
@@ -938,14 +937,22 @@ where
938
937
for monitor_key in monitor_keys {
939
938
let monitor_name = MonitorName :: from_str ( & monitor_key) ?;
940
939
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 ?;
949
956
}
950
957
}
951
958
Ok ( ( ) )
@@ -989,40 +996,24 @@ where
989
996
. write ( primary, & monitor_key, update_name. as_str ( ) , update. encode ( ) )
990
997
. await
991
998
} 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
-
1002
999
// We could write this update, but it meets criteria of our design that calls for a full monitor write.
1003
1000
let write_status = self . persist_new_channel ( monitor_name, monitor) . await ;
1004
1001
1005
1002
if let Ok ( ( ) ) = write_status {
1006
1003
let channel_closed_legacy =
1007
1004
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 ;
1019
1014
} else {
1020
- let end = monitor . get_latest_update_id ( ) ;
1015
+ let end = latest_update_id ;
1021
1016
let start = end. saturating_sub ( self . maximum_pending_updates ) ;
1022
- Some ( ( start, end) )
1023
- } ;
1024
-
1025
- if let Some ( ( start, end) ) = cleanup_range {
1026
1017
self . cleanup_in_range ( monitor_name, start, end) . await ;
1027
1018
}
1028
1019
}
@@ -1263,6 +1254,7 @@ mod tests {
1263
1254
use crate :: util:: test_utils:: { self , TestStore } ;
1264
1255
use crate :: { check_added_monitors, check_closed_broadcast} ;
1265
1256
use bitcoin:: hashes:: hex:: FromHex ;
1257
+ use core:: cmp;
1266
1258
1267
1259
const EXPECTED_UPDATES_PER_PAYMENT : u64 = 5 ;
1268
1260
0 commit comments