@@ -446,6 +446,13 @@ where
446
446
/// If you have many stale updates stored (such as after a crash with pending lazy deletes), and
447
447
/// would like to get rid of them, consider using the
448
448
/// [`MonitorUpdatingPersister::cleanup_stale_updates`] function.
449
+ ///
450
+ /// # Size-based persistence optimization
451
+ ///
452
+ /// For small channel monitors (below `minimum_monitor_size_for_updates` bytes when serialized),
453
+ /// this persister will always write the full monitor instead of individual updates. This avoids
454
+ /// the overhead of managing update files and later compaction for tiny monitors that don't benefit
455
+ /// from differential updates.
449
456
pub struct MonitorUpdatingPersister < K : Deref , L : Deref , ES : Deref , SP : Deref , BI : Deref , FE : Deref >
450
457
where
451
458
K :: Target : KVStore ,
@@ -458,6 +465,7 @@ where
458
465
kv_store : K ,
459
466
logger : L ,
460
467
maximum_pending_updates : u64 ,
468
+ minimum_monitor_size_for_updates : usize ,
461
469
entropy_source : ES ,
462
470
signer_provider : SP ,
463
471
broadcaster : BI ,
@@ -491,21 +499,45 @@ where
491
499
/// less frequent "waves."
492
500
/// - [`MonitorUpdatingPersister`] will potentially have more listing to do if you need to run
493
501
/// [`MonitorUpdatingPersister::cleanup_stale_updates`].
502
+ ///
503
+ /// The `minimum_monitor_size_for_updates` parameter sets the minimum serialized size (in bytes)
504
+ /// for a [`ChannelMonitor`] to use update-based persistence. Monitors smaller than this threshold
505
+ /// will always be persisted in full, avoiding the overhead of managing update files for tiny
506
+ /// monitors. A reasonable default is 4096 bytes (4 KiB). Set to 0 to always use update-based
507
+ /// persistence regardless of size.
494
508
pub fn new (
495
- kv_store : K , logger : L , maximum_pending_updates : u64 , entropy_source : ES ,
509
+ kv_store : K , logger : L , maximum_pending_updates : u64 , minimum_monitor_size_for_updates : usize , entropy_source : ES ,
496
510
signer_provider : SP , broadcaster : BI , fee_estimator : FE ,
497
511
) -> Self {
498
512
MonitorUpdatingPersister {
499
513
kv_store,
500
514
logger,
501
515
maximum_pending_updates,
516
+ minimum_monitor_size_for_updates,
502
517
entropy_source,
503
518
signer_provider,
504
519
broadcaster,
505
520
fee_estimator,
506
521
}
507
522
}
508
523
524
+ /// Constructs a new [`MonitorUpdatingPersister`] with a default minimum monitor size threshold.
525
+ ///
526
+ /// This is a convenience method that sets `minimum_monitor_size_for_updates` to 4096 bytes (4 KiB),
527
+ /// which is a reasonable default for most use cases. Monitors smaller than this will be persisted
528
+ /// in full rather than using update-based persistence.
529
+ ///
530
+ /// For other parameters, see [`MonitorUpdatingPersister::new`].
531
+ pub fn new_with_default_threshold (
532
+ kv_store : K , logger : L , maximum_pending_updates : u64 , entropy_source : ES ,
533
+ signer_provider : SP , broadcaster : BI , fee_estimator : FE ,
534
+ ) -> Self {
535
+ Self :: new (
536
+ kv_store, logger, maximum_pending_updates, 4096 , entropy_source,
537
+ signer_provider, broadcaster, fee_estimator,
538
+ )
539
+ }
540
+
509
541
/// Reads all stored channel monitors, along with any stored updates for them.
510
542
///
511
543
/// It is extremely important that your [`KVStore::read`] implementation uses the
@@ -752,7 +784,12 @@ where
752
784
) -> chain:: ChannelMonitorUpdateStatus {
753
785
const LEGACY_CLOSED_CHANNEL_UPDATE_ID : u64 = u64:: MAX ;
754
786
if let Some ( update) = update {
755
- let persist_update = update. update_id != LEGACY_CLOSED_CHANNEL_UPDATE_ID
787
+ // Check if monitor is too small for update-based persistence
788
+ let monitor_size = monitor. serialized_length ( ) ;
789
+ let use_full_persistence = monitor_size < self . minimum_monitor_size_for_updates ;
790
+
791
+ let persist_update = !use_full_persistence
792
+ && update. update_id != LEGACY_CLOSED_CHANNEL_UPDATE_ID
756
793
&& update. update_id % self . maximum_pending_updates != 0 ;
757
794
if persist_update {
758
795
let monitor_key = monitor_name. to_string ( ) ;
@@ -1156,6 +1193,7 @@ mod tests {
1156
1193
kv_store : & TestStore :: new ( false ) ,
1157
1194
logger : & TestLogger :: new ( ) ,
1158
1195
maximum_pending_updates : persister_0_max_pending_updates,
1196
+ minimum_monitor_size_for_updates : 0 ,
1159
1197
entropy_source : & chanmon_cfgs[ 0 ] . keys_manager ,
1160
1198
signer_provider : & chanmon_cfgs[ 0 ] . keys_manager ,
1161
1199
broadcaster : & chanmon_cfgs[ 0 ] . tx_broadcaster ,
@@ -1165,6 +1203,7 @@ mod tests {
1165
1203
kv_store : & TestStore :: new ( false ) ,
1166
1204
logger : & TestLogger :: new ( ) ,
1167
1205
maximum_pending_updates : persister_1_max_pending_updates,
1206
+ minimum_monitor_size_for_updates : 0 ,
1168
1207
entropy_source : & chanmon_cfgs[ 1 ] . keys_manager ,
1169
1208
signer_provider : & chanmon_cfgs[ 1 ] . keys_manager ,
1170
1209
broadcaster : & chanmon_cfgs[ 1 ] . tx_broadcaster ,
@@ -1330,6 +1369,7 @@ mod tests {
1330
1369
kv_store : & TestStore :: new ( true ) ,
1331
1370
logger : & TestLogger :: new ( ) ,
1332
1371
maximum_pending_updates : 11 ,
1372
+ minimum_monitor_size_for_updates : 0 ,
1333
1373
entropy_source : node_cfgs[ 0 ] . keys_manager ,
1334
1374
signer_provider : node_cfgs[ 0 ] . keys_manager ,
1335
1375
broadcaster : node_cfgs[ 0 ] . tx_broadcaster ,
@@ -1376,6 +1416,7 @@ mod tests {
1376
1416
kv_store : & TestStore :: new ( false ) ,
1377
1417
logger : & TestLogger :: new ( ) ,
1378
1418
maximum_pending_updates : test_max_pending_updates,
1419
+ minimum_monitor_size_for_updates : 0 ,
1379
1420
entropy_source : & chanmon_cfgs[ 0 ] . keys_manager ,
1380
1421
signer_provider : & chanmon_cfgs[ 0 ] . keys_manager ,
1381
1422
broadcaster : & chanmon_cfgs[ 0 ] . tx_broadcaster ,
@@ -1385,6 +1426,7 @@ mod tests {
1385
1426
kv_store : & TestStore :: new ( false ) ,
1386
1427
logger : & TestLogger :: new ( ) ,
1387
1428
maximum_pending_updates : test_max_pending_updates,
1429
+ minimum_monitor_size_for_updates : 0 ,
1388
1430
entropy_source : & chanmon_cfgs[ 1 ] . keys_manager ,
1389
1431
signer_provider : & chanmon_cfgs[ 1 ] . keys_manager ,
1390
1432
broadcaster : & chanmon_cfgs[ 1 ] . tx_broadcaster ,
0 commit comments