66// accordance with one or both of these licenses.
77
88use lightning:: ln:: functional_test_utils:: {
9- connect_block, create_announced_chan_between_nodes, create_chanmon_cfgs, create_dummy_block,
10- create_network, create_node_cfgs, create_node_chanmgrs, send_payment, TestChanMonCfg ,
9+ connect_block, create_announced_chan_between_nodes, create_chanmon_cfgs, create_dummy_block,
10+ create_network, create_node_cfgs, create_node_chanmgrs, send_payment, TestChanMonCfg ,
1111} ;
1212use lightning:: util:: persist:: {
13- KVStore , MonitorName , MonitorUpdatingPersister ,
14- CHANNEL_MONITOR_UPDATE_PERSISTENCE_PRIMARY_NAMESPACE , KVSTORE_NAMESPACE_KEY_MAX_LEN ,
13+ KVStore , MonitorName , MonitorUpdatingPersister ,
14+ CHANNEL_MONITOR_UPDATE_PERSISTENCE_PRIMARY_NAMESPACE , KVSTORE_NAMESPACE_KEY_MAX_LEN ,
1515} ;
1616
1717use lightning:: events:: ClosureReason ;
@@ -25,133 +25,133 @@ use std::panic::RefUnwindSafe;
2525use std:: path:: PathBuf ;
2626
2727type TestMonitorUpdatePersister < ' a , K > = MonitorUpdatingPersister <
28- & ' a K ,
29- & ' a test_utils:: TestLogger ,
30- & ' a test_utils:: TestKeysInterface ,
31- & ' a test_utils:: TestKeysInterface ,
32- & ' a test_utils:: TestBroadcaster ,
33- & ' a test_utils:: TestFeeEstimator ,
28+ & ' a K ,
29+ & ' a test_utils:: TestLogger ,
30+ & ' a test_utils:: TestKeysInterface ,
31+ & ' a test_utils:: TestKeysInterface ,
32+ & ' a test_utils:: TestBroadcaster ,
33+ & ' a test_utils:: TestFeeEstimator ,
3434> ;
3535
3636const EXPECTED_UPDATES_PER_PAYMENT : u64 = 5 ;
3737
3838pub ( crate ) fn random_storage_path ( ) -> PathBuf {
39- let mut temp_path = std:: env:: temp_dir ( ) ;
40- let mut rng = thread_rng ( ) ;
41- let rand_dir: String = ( 0 ..7 ) . map ( |_| rng. sample ( Alphanumeric ) as char ) . collect ( ) ;
42- temp_path. push ( rand_dir) ;
43- temp_path
39+ let mut temp_path = std:: env:: temp_dir ( ) ;
40+ let mut rng = thread_rng ( ) ;
41+ let rand_dir: String = ( 0 ..7 ) . map ( |_| rng. sample ( Alphanumeric ) as char ) . collect ( ) ;
42+ temp_path. push ( rand_dir) ;
43+ temp_path
4444}
4545
4646pub ( crate ) fn do_read_write_remove_list_persist < K : KVStore + RefUnwindSafe > ( kv_store : & K ) {
47- let data = [ 42u8 ; 32 ] ;
47+ let data = [ 42u8 ; 32 ] ;
4848
49- let primary_namespace = "testspace" ;
50- let secondary_namespace = "testsubspace" ;
51- let key = "testkey" ;
49+ let primary_namespace = "testspace" ;
50+ let secondary_namespace = "testsubspace" ;
51+ let key = "testkey" ;
5252
53- // Test the basic KVStore operations.
54- kv_store. write ( primary_namespace, secondary_namespace, key, & data) . unwrap ( ) ;
53+ // Test the basic KVStore operations.
54+ kv_store. write ( primary_namespace, secondary_namespace, key, & data) . unwrap ( ) ;
5555
56- // Test empty primary/secondary namespaces are allowed, but not empty primary namespace and non-empty
57- // secondary primary_namespace, and not empty key.
58- kv_store. write ( "" , "" , key, & data) . unwrap ( ) ;
59- let res = std:: panic:: catch_unwind ( || kv_store. write ( "" , secondary_namespace, key, & data) ) ;
60- assert ! ( res. is_err( ) ) ;
61- let res = std:: panic:: catch_unwind ( || {
62- kv_store. write ( primary_namespace, secondary_namespace, "" , & data)
63- } ) ;
64- assert ! ( res. is_err( ) ) ;
56+ // Test empty primary/secondary namespaces are allowed, but not empty primary namespace and non-empty
57+ // secondary primary_namespace, and not empty key.
58+ kv_store. write ( "" , "" , key, & data) . unwrap ( ) ;
59+ let res = std:: panic:: catch_unwind ( || kv_store. write ( "" , secondary_namespace, key, & data) ) ;
60+ assert ! ( res. is_err( ) ) ;
61+ let res = std:: panic:: catch_unwind ( || {
62+ kv_store. write ( primary_namespace, secondary_namespace, "" , & data)
63+ } ) ;
64+ assert ! ( res. is_err( ) ) ;
6565
66- let listed_keys = kv_store. list ( primary_namespace, secondary_namespace) . unwrap ( ) ;
67- assert_eq ! ( listed_keys. len( ) , 1 ) ;
68- assert_eq ! ( listed_keys[ 0 ] , key) ;
66+ let listed_keys = kv_store. list ( primary_namespace, secondary_namespace) . unwrap ( ) ;
67+ assert_eq ! ( listed_keys. len( ) , 1 ) ;
68+ assert_eq ! ( listed_keys[ 0 ] , key) ;
6969
70- let read_data = kv_store. read ( primary_namespace, secondary_namespace, key) . unwrap ( ) ;
71- assert_eq ! ( data, & * read_data) ;
70+ let read_data = kv_store. read ( primary_namespace, secondary_namespace, key) . unwrap ( ) ;
71+ assert_eq ! ( data, & * read_data) ;
7272
73- kv_store. remove ( primary_namespace, secondary_namespace, key, false ) . unwrap ( ) ;
73+ kv_store. remove ( primary_namespace, secondary_namespace, key, false ) . unwrap ( ) ;
7474
75- let listed_keys = kv_store. list ( primary_namespace, secondary_namespace) . unwrap ( ) ;
76- assert_eq ! ( listed_keys. len( ) , 0 ) ;
75+ let listed_keys = kv_store. list ( primary_namespace, secondary_namespace) . unwrap ( ) ;
76+ assert_eq ! ( listed_keys. len( ) , 0 ) ;
7777
78- // Ensure we have no issue operating with primary_namespace/secondary_namespace/key being KVSTORE_NAMESPACE_KEY_MAX_LEN
79- let max_chars: String = std:: iter:: repeat ( 'A' ) . take ( KVSTORE_NAMESPACE_KEY_MAX_LEN ) . collect ( ) ;
80- kv_store. write ( & max_chars, & max_chars, & max_chars, & data) . unwrap ( ) ;
78+ // Ensure we have no issue operating with primary_namespace/secondary_namespace/key being KVSTORE_NAMESPACE_KEY_MAX_LEN
79+ let max_chars: String = std:: iter:: repeat ( 'A' ) . take ( KVSTORE_NAMESPACE_KEY_MAX_LEN ) . collect ( ) ;
80+ kv_store. write ( & max_chars, & max_chars, & max_chars, & data) . unwrap ( ) ;
8181
82- let listed_keys = kv_store. list ( & max_chars, & max_chars) . unwrap ( ) ;
83- assert_eq ! ( listed_keys. len( ) , 1 ) ;
84- assert_eq ! ( listed_keys[ 0 ] , max_chars) ;
82+ let listed_keys = kv_store. list ( & max_chars, & max_chars) . unwrap ( ) ;
83+ assert_eq ! ( listed_keys. len( ) , 1 ) ;
84+ assert_eq ! ( listed_keys[ 0 ] , max_chars) ;
8585
86- let read_data = kv_store. read ( & max_chars, & max_chars, & max_chars) . unwrap ( ) ;
87- assert_eq ! ( data, & * read_data) ;
86+ let read_data = kv_store. read ( & max_chars, & max_chars, & max_chars) . unwrap ( ) ;
87+ assert_eq ! ( data, & * read_data) ;
8888
89- kv_store. remove ( & max_chars, & max_chars, & max_chars, false ) . unwrap ( ) ;
89+ kv_store. remove ( & max_chars, & max_chars, & max_chars, false ) . unwrap ( ) ;
9090
91- let listed_keys = kv_store. list ( & max_chars, & max_chars) . unwrap ( ) ;
92- assert_eq ! ( listed_keys. len( ) , 0 ) ;
91+ let listed_keys = kv_store. list ( & max_chars, & max_chars) . unwrap ( ) ;
92+ assert_eq ! ( listed_keys. len( ) , 0 ) ;
9393}
9494
9595pub ( crate ) fn create_persister < ' a , K : KVStore > (
96- store : & ' a K , chanmon_cfg : & ' a TestChanMonCfg , max_pending_updates : u64 ,
96+ store : & ' a K , chanmon_cfg : & ' a TestChanMonCfg , max_pending_updates : u64 ,
9797) -> TestMonitorUpdatePersister < ' a , K > {
98- let persister: TestMonitorUpdatePersister < ' a , K > = MonitorUpdatingPersister :: new (
99- store,
100- & chanmon_cfg. logger ,
101- max_pending_updates,
102- & chanmon_cfg. keys_manager ,
103- & chanmon_cfg. keys_manager ,
104- & chanmon_cfg. tx_broadcaster ,
105- & chanmon_cfg. fee_estimator ,
106- ) ;
107- return persister;
98+ let persister: TestMonitorUpdatePersister < ' a , K > = MonitorUpdatingPersister :: new (
99+ store,
100+ & chanmon_cfg. logger ,
101+ max_pending_updates,
102+ & chanmon_cfg. keys_manager ,
103+ & chanmon_cfg. keys_manager ,
104+ & chanmon_cfg. tx_broadcaster ,
105+ & chanmon_cfg. fee_estimator ,
106+ ) ;
107+ return persister;
108108}
109109
110- pub ( crate ) fn create_chain_monitor < ' a , K : KVStore > (
111- chanmon_cfg : & ' a TestChanMonCfg , persister : & ' a TestMonitorUpdatePersister < ' a , K > ,
110+ pub ( crate ) fn create_chain_monitor < ' a , K : KVStore + Sync > (
111+ chanmon_cfg : & ' a TestChanMonCfg , persister : & ' a TestMonitorUpdatePersister < ' a , K > ,
112112) -> test_utils:: TestChainMonitor < ' a > {
113- let chain_mon = test_utils:: TestChainMonitor :: new (
114- Some ( & chanmon_cfg. chain_source ) ,
115- & chanmon_cfg. tx_broadcaster ,
116- & chanmon_cfg. logger ,
117- & chanmon_cfg. fee_estimator ,
118- persister,
119- & chanmon_cfg. keys_manager ,
120- ) ;
121- return chain_mon;
113+ let chain_mon = test_utils:: TestChainMonitor :: new (
114+ Some ( & chanmon_cfg. chain_source ) ,
115+ & chanmon_cfg. tx_broadcaster ,
116+ & chanmon_cfg. logger ,
117+ & chanmon_cfg. fee_estimator ,
118+ persister,
119+ & chanmon_cfg. keys_manager ,
120+ ) ;
121+ return chain_mon;
122122}
123123
124124// Integration-test the given KVStore implementation. Test relaying a few payments and check that
125125// the persisted data is updated the appropriate number of times.
126- pub ( crate ) fn do_test_store < K : KVStore > ( store_0 : & K , store_1 : & K ) {
127- // This value is used later to limit how many iterations we perform.
128- let persister_0_max_pending_updates = 7 ;
129- // Intentionally set this to a smaller value to test a different alignment.
130- let persister_1_max_pending_updates = 3 ;
131-
132- let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
133-
134- let persister_0 = create_persister ( store_0, & chanmon_cfgs[ 0 ] , persister_0_max_pending_updates) ;
135- let persister_1 = create_persister ( store_1, & chanmon_cfgs[ 1 ] , persister_1_max_pending_updates) ;
136-
137- let chain_mon_0 = create_chain_monitor ( & chanmon_cfgs[ 0 ] , & persister_0) ;
138- let chain_mon_1 = create_chain_monitor ( & chanmon_cfgs[ 1 ] , & persister_1) ;
139-
140- let mut node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
141- node_cfgs[ 0 ] . chain_monitor = chain_mon_0;
142- node_cfgs[ 1 ] . chain_monitor = chain_mon_1;
143- let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
144- let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
145-
146- // Check that the persisted channel data is empty before any channels are
147- // open.
148- let mut persisted_chan_data_0 = persister_0. read_all_channel_monitors_with_updates ( ) . unwrap ( ) ;
149- assert_eq ! ( persisted_chan_data_0. len( ) , 0 ) ;
150- let mut persisted_chan_data_1 = persister_1. read_all_channel_monitors_with_updates ( ) . unwrap ( ) ;
151- assert_eq ! ( persisted_chan_data_1. len( ) , 0 ) ;
152-
153- // Helper to make sure the channel is on the expected update ID.
154- macro_rules! check_persisted_data {
126+ pub ( crate ) fn do_test_store < K : KVStore + Sync > ( store_0 : & K , store_1 : & K ) {
127+ // This value is used later to limit how many iterations we perform.
128+ let persister_0_max_pending_updates = 7 ;
129+ // Intentionally set this to a smaller value to test a different alignment.
130+ let persister_1_max_pending_updates = 3 ;
131+
132+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
133+
134+ let persister_0 = create_persister ( store_0, & chanmon_cfgs[ 0 ] , persister_0_max_pending_updates) ;
135+ let persister_1 = create_persister ( store_1, & chanmon_cfgs[ 1 ] , persister_1_max_pending_updates) ;
136+
137+ let chain_mon_0 = create_chain_monitor ( & chanmon_cfgs[ 0 ] , & persister_0) ;
138+ let chain_mon_1 = create_chain_monitor ( & chanmon_cfgs[ 1 ] , & persister_1) ;
139+
140+ let mut node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
141+ node_cfgs[ 0 ] . chain_monitor = chain_mon_0;
142+ node_cfgs[ 1 ] . chain_monitor = chain_mon_1;
143+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
144+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
145+
146+ // Check that the persisted channel data is empty before any channels are
147+ // open.
148+ let mut persisted_chan_data_0 = persister_0. read_all_channel_monitors_with_updates ( ) . unwrap ( ) ;
149+ assert_eq ! ( persisted_chan_data_0. len( ) , 0 ) ;
150+ let mut persisted_chan_data_1 = persister_1. read_all_channel_monitors_with_updates ( ) . unwrap ( ) ;
151+ assert_eq ! ( persisted_chan_data_1. len( ) , 0 ) ;
152+
153+ // Helper to make sure the channel is on the expected update ID.
154+ macro_rules! check_persisted_data {
155155 ( $expected_update_id: expr) => {
156156 persisted_chan_data_0 = persister_0. read_all_channel_monitors_with_updates( ) . unwrap( ) ;
157157 // check that we stored only one monitor
@@ -195,60 +195,60 @@ pub(crate) fn do_test_store<K: KVStore>(store_0: &K, store_1: &K) {
195195 } ;
196196 }
197197
198- // Create some initial channel and check that a channel was persisted.
199- let _ = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
200- check_persisted_data ! ( 0 ) ;
201-
202- // Send a few payments and make sure the monitors are updated to the latest.
203- let expected_route = & [ & nodes[ 1 ] ] [ ..] ;
204- send_payment ( & nodes[ 0 ] , expected_route, 8_000_000 ) ;
205- check_persisted_data ! ( EXPECTED_UPDATES_PER_PAYMENT ) ;
206- let expected_route = & [ & nodes[ 0 ] ] [ ..] ;
207- send_payment ( & nodes[ 1 ] , expected_route, 4_000_000 ) ;
208- check_persisted_data ! ( 2 * EXPECTED_UPDATES_PER_PAYMENT ) ;
209-
210- // Send a few more payments to try all the alignments of max pending updates with
211- // updates for a payment sent and received.
212- let mut sender = 0 ;
213- for i in 3 ..=persister_0_max_pending_updates * 2 {
214- let receiver;
215- if sender == 0 {
216- sender = 1 ;
217- receiver = 0 ;
218- } else {
219- sender = 0 ;
220- receiver = 1 ;
221- }
222- let expected_route = & [ & nodes[ receiver] ] [ ..] ;
223- send_payment ( & nodes[ sender] , expected_route, 21_000 ) ;
224- check_persisted_data ! ( i * EXPECTED_UPDATES_PER_PAYMENT ) ;
225- }
226-
227- // Force close because cooperative close doesn't result in any persisted
228- // updates.
229-
230- let node_id_1 = nodes[ 1 ] . node . get_our_node_id ( ) ;
231- let chan_id = nodes[ 0 ] . node . list_channels ( ) [ 0 ] . channel_id ;
232- let err_msg = "Channel force-closed" . to_string ( ) ;
233- nodes[ 0 ] . node . force_close_broadcasting_latest_txn ( & chan_id, & node_id_1, err_msg) . unwrap ( ) ;
234-
235- let reason = ClosureReason :: HolderForceClosed { broadcasted_latest_txn : Some ( true ) } ;
236- check_closed_event ! ( nodes[ 0 ] , 1 , reason, false , [ node_id_1] , 100000 ) ;
237- check_closed_broadcast ! ( nodes[ 0 ] , true ) ;
238- check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
239-
240- let node_txn = nodes[ 0 ] . tx_broadcaster . txn_broadcast ( ) ;
241- assert_eq ! ( node_txn. len( ) , 1 ) ;
242- let txn = vec ! [ node_txn[ 0 ] . clone( ) , node_txn[ 0 ] . clone( ) ] ;
243- let dummy_block = create_dummy_block ( nodes[ 0 ] . best_block_hash ( ) , 42 , txn) ;
244- connect_block ( & nodes[ 1 ] , & dummy_block) ;
245-
246- check_closed_broadcast ! ( nodes[ 1 ] , true ) ;
247- let reason = ClosureReason :: CommitmentTxConfirmed ;
248- let node_id_0 = nodes[ 0 ] . node . get_our_node_id ( ) ;
249- check_closed_event ! ( nodes[ 1 ] , 1 , reason, false , [ node_id_0] , 100000 ) ;
250- check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
251-
252- // Make sure everything is persisted as expected after close.
253- check_persisted_data ! ( persister_0_max_pending_updates * 2 * EXPECTED_UPDATES_PER_PAYMENT + 1 ) ;
198+ // Create some initial channel and check that a channel was persisted.
199+ let _ = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
200+ check_persisted_data ! ( 0 ) ;
201+
202+ // Send a few payments and make sure the monitors are updated to the latest.
203+ let expected_route = & [ & nodes[ 1 ] ] [ ..] ;
204+ send_payment ( & nodes[ 0 ] , expected_route, 8_000_000 ) ;
205+ check_persisted_data ! ( EXPECTED_UPDATES_PER_PAYMENT ) ;
206+ let expected_route = & [ & nodes[ 0 ] ] [ ..] ;
207+ send_payment ( & nodes[ 1 ] , expected_route, 4_000_000 ) ;
208+ check_persisted_data ! ( 2 * EXPECTED_UPDATES_PER_PAYMENT ) ;
209+
210+ // Send a few more payments to try all the alignments of max pending updates with
211+ // updates for a payment sent and received.
212+ let mut sender = 0 ;
213+ for i in 3 ..=persister_0_max_pending_updates * 2 {
214+ let receiver;
215+ if sender == 0 {
216+ sender = 1 ;
217+ receiver = 0 ;
218+ } else {
219+ sender = 0 ;
220+ receiver = 1 ;
221+ }
222+ let expected_route = & [ & nodes[ receiver] ] [ ..] ;
223+ send_payment ( & nodes[ sender] , expected_route, 21_000 ) ;
224+ check_persisted_data ! ( i * EXPECTED_UPDATES_PER_PAYMENT ) ;
225+ }
226+
227+ // Force close because cooperative close doesn't result in any persisted
228+ // updates.
229+
230+ let node_id_1 = nodes[ 1 ] . node . get_our_node_id ( ) ;
231+ let chan_id = nodes[ 0 ] . node . list_channels ( ) [ 0 ] . channel_id ;
232+ let err_msg = "Channel force-closed" . to_string ( ) ;
233+ nodes[ 0 ] . node . force_close_broadcasting_latest_txn ( & chan_id, & node_id_1, err_msg) . unwrap ( ) ;
234+
235+ let reason = ClosureReason :: HolderForceClosed { broadcasted_latest_txn : Some ( true ) } ;
236+ check_closed_event ! ( nodes[ 0 ] , 1 , reason, false , [ node_id_1] , 100000 ) ;
237+ check_closed_broadcast ! ( nodes[ 0 ] , true ) ;
238+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
239+
240+ let node_txn = nodes[ 0 ] . tx_broadcaster . txn_broadcast ( ) ;
241+ assert_eq ! ( node_txn. len( ) , 1 ) ;
242+ let txn = vec ! [ node_txn[ 0 ] . clone( ) , node_txn[ 0 ] . clone( ) ] ;
243+ let dummy_block = create_dummy_block ( nodes[ 0 ] . best_block_hash ( ) , 42 , txn) ;
244+ connect_block ( & nodes[ 1 ] , & dummy_block) ;
245+
246+ check_closed_broadcast ! ( nodes[ 1 ] , true ) ;
247+ let reason = ClosureReason :: CommitmentTxConfirmed ;
248+ let node_id_0 = nodes[ 0 ] . node . get_our_node_id ( ) ;
249+ check_closed_event ! ( nodes[ 1 ] , 1 , reason, false , [ node_id_0] , 100000 ) ;
250+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
251+
252+ // Make sure everything is persisted as expected after close.
253+ check_persisted_data ! ( persister_0_max_pending_updates * 2 * EXPECTED_UPDATES_PER_PAYMENT + 1 ) ;
254254}
0 commit comments