@@ -1200,6 +1200,7 @@ mod tests {
12001200 kv_store : & TestStore :: new ( false ) ,
12011201 logger : & TestLogger :: new ( ) ,
12021202 maximum_pending_updates : persister_0_max_pending_updates,
1203+ min_monitor_size_for_updates_bytes : 0 ,
12031204 entropy_source : & chanmon_cfgs[ 0 ] . keys_manager ,
12041205 signer_provider : & chanmon_cfgs[ 0 ] . keys_manager ,
12051206 broadcaster : & chanmon_cfgs[ 0 ] . tx_broadcaster ,
@@ -1209,6 +1210,7 @@ mod tests {
12091210 kv_store : & TestStore :: new ( false ) ,
12101211 logger : & TestLogger :: new ( ) ,
12111212 maximum_pending_updates : persister_1_max_pending_updates,
1213+ min_monitor_size_for_updates_bytes : 0 ,
12121214 entropy_source : & chanmon_cfgs[ 1 ] . keys_manager ,
12131215 signer_provider : & chanmon_cfgs[ 1 ] . keys_manager ,
12141216 broadcaster : & chanmon_cfgs[ 1 ] . tx_broadcaster ,
@@ -1374,6 +1376,7 @@ mod tests {
13741376 kv_store : & TestStore :: new ( true ) ,
13751377 logger : & TestLogger :: new ( ) ,
13761378 maximum_pending_updates : 11 ,
1379+ min_monitor_size_for_updates_bytes : 0 ,
13771380 entropy_source : node_cfgs[ 0 ] . keys_manager ,
13781381 signer_provider : node_cfgs[ 0 ] . keys_manager ,
13791382 broadcaster : node_cfgs[ 0 ] . tx_broadcaster ,
@@ -1416,24 +1419,36 @@ mod tests {
14161419 fn clean_stale_updates_works ( ) {
14171420 let test_max_pending_updates = 7 ;
14181421 let chanmon_cfgs = create_chanmon_cfgs ( 3 ) ;
1419- let persister_0 = MonitorUpdatingPersister {
1420- kv_store : & TestStore :: new ( false ) ,
1421- logger : & TestLogger :: new ( ) ,
1422- maximum_pending_updates : test_max_pending_updates,
1423- entropy_source : & chanmon_cfgs[ 0 ] . keys_manager ,
1424- signer_provider : & chanmon_cfgs[ 0 ] . keys_manager ,
1425- broadcaster : & chanmon_cfgs[ 0 ] . tx_broadcaster ,
1426- fee_estimator : & chanmon_cfgs[ 0 ] . fee_estimator ,
1427- } ;
1428- let persister_1 = MonitorUpdatingPersister {
1429- kv_store : & TestStore :: new ( false ) ,
1430- logger : & TestLogger :: new ( ) ,
1431- maximum_pending_updates : test_max_pending_updates,
1432- entropy_source : & chanmon_cfgs[ 1 ] . keys_manager ,
1433- signer_provider : & chanmon_cfgs[ 1 ] . keys_manager ,
1434- broadcaster : & chanmon_cfgs[ 1 ] . tx_broadcaster ,
1435- fee_estimator : & chanmon_cfgs[ 1 ] . fee_estimator ,
1436- } ;
1422+ let store_0 = TestStore :: new ( false ) ;
1423+ let logger_0 = TestLogger :: new ( ) ;
1424+ let store_1 = TestStore :: new ( false ) ;
1425+ let logger_1 = TestLogger :: new ( ) ;
1426+
1427+ // Test the default new() constructor (uses 4096 byte threshold)
1428+ let persister_0 = MonitorUpdatingPersister :: new (
1429+ & store_0,
1430+ & logger_0,
1431+ test_max_pending_updates,
1432+ & chanmon_cfgs[ 0 ] . keys_manager ,
1433+ & chanmon_cfgs[ 0 ] . keys_manager ,
1434+ & chanmon_cfgs[ 0 ] . tx_broadcaster ,
1435+ & chanmon_cfgs[ 0 ] . fee_estimator ,
1436+ ) ;
1437+ // Test the custom threshold constructor with zero threshold
1438+ let persister_1 = MonitorUpdatingPersister :: new_with_monitor_size_threshold (
1439+ & store_1,
1440+ & logger_1,
1441+ test_max_pending_updates,
1442+ 0 , // 0 byte threshold for maximum update usage
1443+ & chanmon_cfgs[ 1 ] . keys_manager ,
1444+ & chanmon_cfgs[ 1 ] . keys_manager ,
1445+ & chanmon_cfgs[ 1 ] . tx_broadcaster ,
1446+ & chanmon_cfgs[ 1 ] . fee_estimator ,
1447+ ) ;
1448+
1449+ // Verify the constructors set the thresholds correctly
1450+ assert_eq ! ( persister_0. min_monitor_size_for_updates_bytes, 4096 ) ;
1451+ assert_eq ! ( persister_1. min_monitor_size_for_updates_bytes, 0 ) ;
14371452 let mut node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
14381453 let chain_mon_0 = test_utils:: TestChainMonitor :: new (
14391454 Some ( & chanmon_cfgs[ 0 ] . chain_source ) ,
@@ -1496,6 +1511,118 @@ mod tests {
14961511 . is_err( ) ) ;
14971512 }
14981513
1514+ #[ test]
1515+ fn test_size_based_optimization ( ) {
1516+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
1517+ let store_0 = TestStore :: new ( false ) ;
1518+ let logger_0 = TestLogger :: new ( ) ;
1519+ let store_1 = TestStore :: new ( false ) ;
1520+ let logger_1 = TestLogger :: new ( ) ;
1521+
1522+ // Create a persister with a huge minimum size threshold (100KB)
1523+ // This should force all monitors to use full persistence instead of updates
1524+ // Test the new_with_monitor_size_threshold constructor with large threshold
1525+ let large_threshold_persister = MonitorUpdatingPersister :: new_with_monitor_size_threshold (
1526+ & store_0,
1527+ & logger_0,
1528+ 5 ,
1529+ 100_000 ,
1530+ & chanmon_cfgs[ 0 ] . keys_manager ,
1531+ & chanmon_cfgs[ 0 ] . keys_manager ,
1532+ & chanmon_cfgs[ 0 ] . tx_broadcaster ,
1533+ & chanmon_cfgs[ 0 ] . fee_estimator ,
1534+ ) ;
1535+
1536+ // Create a persister with zero minimum size threshold
1537+ // This should allow all monitors to use update-based persistence
1538+ // Test the new_with_monitor_size_threshold constructor with zero threshold
1539+ let small_threshold_persister = MonitorUpdatingPersister :: new_with_monitor_size_threshold (
1540+ & store_1,
1541+ & logger_1,
1542+ 5 ,
1543+ 0 , // allows all monitors to use updates
1544+ & chanmon_cfgs[ 1 ] . keys_manager ,
1545+ & chanmon_cfgs[ 1 ] . keys_manager ,
1546+ & chanmon_cfgs[ 1 ] . tx_broadcaster ,
1547+ & chanmon_cfgs[ 1 ] . fee_estimator ,
1548+ ) ;
1549+
1550+ // Verify the constructors set the thresholds correctly
1551+ assert_eq ! ( large_threshold_persister. min_monitor_size_for_updates_bytes, 100_000 ) ;
1552+ assert_eq ! ( small_threshold_persister. min_monitor_size_for_updates_bytes, 0 ) ;
1553+
1554+ let mut node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
1555+ let chain_mon_0 = test_utils:: TestChainMonitor :: new (
1556+ Some ( & chanmon_cfgs[ 0 ] . chain_source ) ,
1557+ & chanmon_cfgs[ 0 ] . tx_broadcaster ,
1558+ & chanmon_cfgs[ 0 ] . logger ,
1559+ & chanmon_cfgs[ 0 ] . fee_estimator ,
1560+ & large_threshold_persister,
1561+ & chanmon_cfgs[ 0 ] . keys_manager ,
1562+ ) ;
1563+ let chain_mon_1 = test_utils:: TestChainMonitor :: new (
1564+ Some ( & chanmon_cfgs[ 1 ] . chain_source ) ,
1565+ & chanmon_cfgs[ 1 ] . tx_broadcaster ,
1566+ & chanmon_cfgs[ 1 ] . logger ,
1567+ & chanmon_cfgs[ 1 ] . fee_estimator ,
1568+ & small_threshold_persister,
1569+ & chanmon_cfgs[ 1 ] . keys_manager ,
1570+ ) ;
1571+ node_cfgs[ 0 ] . chain_monitor = chain_mon_0;
1572+ node_cfgs[ 1 ] . chain_monitor = chain_mon_1;
1573+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
1574+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
1575+
1576+ // Create a channel and make a payment to trigger monitor updates
1577+ let _ = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
1578+ send_payment ( & nodes[ 0 ] , & vec ! [ & nodes[ 1 ] ] [ ..] , 1_000_000 ) ;
1579+
1580+ // Test passes if we can create the channels and send payments without issues.
1581+ // The actual verification is that the different persisters behave differently
1582+ // based on their thresholds, which we can verify by ensuring no panics occur.
1583+
1584+ // Verify that monitors were created
1585+ let persisted_data_0 =
1586+ large_threshold_persister. read_all_channel_monitors_with_updates ( ) . unwrap ( ) ;
1587+ let persisted_data_1 =
1588+ small_threshold_persister. read_all_channel_monitors_with_updates ( ) . unwrap ( ) ;
1589+
1590+ assert_eq ! ( persisted_data_0. len( ) , 1 ) ;
1591+ assert_eq ! ( persisted_data_1. len( ) , 1 ) ;
1592+
1593+ // Verify the monitors exist and are of reasonable size
1594+ for ( _, monitor) in persisted_data_0. iter ( ) {
1595+ let monitor_size = monitor. serialized_length ( ) ;
1596+ // Verify the monitor is not empty and reasonably sized
1597+ assert ! (
1598+ monitor_size > 1000 ,
1599+ "Monitor should be at least 1KB in size, got {} bytes" ,
1600+ monitor_size
1601+ ) ;
1602+ assert ! (
1603+ monitor_size < 100_000 ,
1604+ "Monitor should be smaller than 100KB threshold, got {} bytes" ,
1605+ monitor_size
1606+ ) ;
1607+ }
1608+
1609+ for ( _, monitor) in persisted_data_1. iter ( ) {
1610+ let monitor_size = monitor. serialized_length ( ) ;
1611+ // Verify the monitor is not empty and reasonably sized
1612+ assert ! (
1613+ monitor_size > 1000 ,
1614+ "Monitor should be at least 1KB in size, got {} bytes" ,
1615+ monitor_size
1616+ ) ;
1617+ // Since threshold is 0, this monitor should be large enough to use updates
1618+ assert ! (
1619+ monitor_size > 0 ,
1620+ "Monitor should be larger than 0 byte threshold, got {} bytes" ,
1621+ monitor_size
1622+ ) ;
1623+ }
1624+ }
1625+
14991626 fn persist_fn < P : Deref , ChannelSigner : EcdsaChannelSigner > ( _persist : P ) -> bool
15001627 where
15011628 P :: Target : Persist < ChannelSigner > ,
0 commit comments