9
9
10
10
//! Further functional tests which test blockchain reorganizations.
11
11
12
- use chain:: channelmonitor:: ANTI_REORG_DELAY ;
12
+ use chain:: channelmonitor:: { ANTI_REORG_DELAY , ChannelMonitor } ;
13
+ use chain:: Watch ;
14
+ use ln:: channelmanager:: { ChannelManager , ChannelManagerReadArgs } ;
13
15
use ln:: features:: InitFeatures ;
14
16
use ln:: msgs:: { ChannelMessageHandler , ErrorAction , HTLCFailChannelUpdate } ;
17
+ use util:: config:: UserConfig ;
18
+ use util:: enforcing_trait_impls:: EnforcingSigner ;
15
19
use util:: events:: { Event , EventsProvider , MessageSendEvent , MessageSendEventsProvider } ;
20
+ use util:: test_utils;
21
+ use util:: ser:: { ReadableArgs , Writeable } ;
16
22
17
23
use bitcoin:: blockdata:: block:: { Block , BlockHeader } ;
24
+ use bitcoin:: hash_types:: BlockHash ;
18
25
19
- use std:: default :: Default ;
26
+ use std:: collections :: HashMap ;
20
27
use std:: mem;
21
28
22
29
use ln:: functional_test_utils:: * ;
@@ -182,13 +189,15 @@ fn test_onchain_htlc_timeout_delay_remote_commitment() {
182
189
do_test_onchain_htlc_reorg ( false , false ) ;
183
190
}
184
191
185
- #[ test]
186
- fn test_unconf_chan ( ) {
192
+ fn do_test_unconf_chan ( reload_node : bool ) {
187
193
// After creating a chan between nodes, we disconnect all blocks previously seen to force a channel close on nodes[0] side
188
194
let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
189
195
let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
190
196
let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
191
- let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
197
+ let persister: test_utils:: TestPersister ;
198
+ let new_chain_monitor: test_utils:: TestChainMonitor ;
199
+ let nodes_0_deserialized: ChannelManager < EnforcingSigner , & test_utils:: TestChainMonitor , & test_utils:: TestBroadcaster , & test_utils:: TestKeysInterface , & test_utils:: TestFeeEstimator , & test_utils:: TestLogger > ;
200
+ let mut nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
192
201
create_announced_chan_between_nodes ( & nodes, 0 , 1 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) ;
193
202
194
203
let channel_state = nodes[ 0 ] . node . channel_state . lock ( ) . unwrap ( ) ;
@@ -207,11 +216,59 @@ fn test_unconf_chan() {
207
216
nodes[ 0 ] . node . block_disconnected ( & headers. pop ( ) . unwrap ( ) ) ;
208
217
}
209
218
check_closed_broadcast ! ( nodes[ 0 ] , false ) ;
219
+ {
220
+ let channel_state = nodes[ 0 ] . node . channel_state . lock ( ) . unwrap ( ) ;
221
+ assert_eq ! ( channel_state. by_id. len( ) , 0 ) ;
222
+ assert_eq ! ( channel_state. short_to_id. len( ) , 0 ) ;
223
+ }
224
+
225
+ if reload_node {
226
+ // Since we currently have a background event pending, its good to test that we survive a
227
+ // serialization roundtrip. Further, this tests the somewhat awkward edge-case of dropping
228
+ // the Channel object from the ChannelManager, but still having a monitor event pending for
229
+ // it when we go to deserialize, and then use the ChannelManager.
230
+ let nodes_0_serialized = nodes[ 0 ] . node . encode ( ) ;
231
+ let mut chan_0_monitor_serialized = test_utils:: TestVecWriter ( Vec :: new ( ) ) ;
232
+ nodes[ 0 ] . chain_monitor . chain_monitor . monitors . lock ( ) . unwrap ( ) . iter ( ) . next ( ) . unwrap ( ) . 1 . write ( & mut chan_0_monitor_serialized) . unwrap ( ) ;
233
+
234
+ persister = test_utils:: TestPersister :: new ( ) ;
235
+ let keys_manager = & chanmon_cfgs[ 0 ] . keys_manager ;
236
+ new_chain_monitor = test_utils:: TestChainMonitor :: new ( Some ( nodes[ 0 ] . chain_source ) , nodes[ 0 ] . tx_broadcaster . clone ( ) , nodes[ 0 ] . logger , node_cfgs[ 0 ] . fee_estimator , & persister, keys_manager) ;
237
+ nodes[ 0 ] . chain_monitor = & new_chain_monitor;
238
+ let mut chan_0_monitor_read = & chan_0_monitor_serialized. 0 [ ..] ;
239
+ let ( _, mut chan_0_monitor) = <( BlockHash , ChannelMonitor < EnforcingSigner > ) >:: read (
240
+ & mut chan_0_monitor_read, keys_manager) . unwrap ( ) ;
241
+ assert ! ( chan_0_monitor_read. is_empty( ) ) ;
242
+
243
+ let mut nodes_0_read = & nodes_0_serialized[ ..] ;
244
+ let config = UserConfig :: default ( ) ;
245
+ nodes_0_deserialized = {
246
+ let mut channel_monitors = HashMap :: new ( ) ;
247
+ channel_monitors. insert ( chan_0_monitor. get_funding_txo ( ) . 0 , & mut chan_0_monitor) ;
248
+ <( BlockHash , ChannelManager < EnforcingSigner , & test_utils:: TestChainMonitor , & test_utils:: TestBroadcaster , & test_utils:: TestKeysInterface , & test_utils:: TestFeeEstimator , & test_utils:: TestLogger > ) >:: read ( & mut nodes_0_read, ChannelManagerReadArgs {
249
+ default_config : config,
250
+ keys_manager,
251
+ fee_estimator : node_cfgs[ 0 ] . fee_estimator ,
252
+ chain_monitor : nodes[ 0 ] . chain_monitor ,
253
+ tx_broadcaster : nodes[ 0 ] . tx_broadcaster . clone ( ) ,
254
+ logger : nodes[ 0 ] . logger ,
255
+ channel_monitors,
256
+ } ) . unwrap ( ) . 1
257
+ } ;
258
+ nodes[ 0 ] . node = & nodes_0_deserialized;
259
+ assert ! ( nodes_0_read. is_empty( ) ) ;
260
+
261
+ nodes[ 0 ] . chain_monitor . watch_channel ( chan_0_monitor. get_funding_txo ( ) . 0 . clone ( ) , chan_0_monitor) . unwrap ( ) ;
262
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
263
+ }
264
+
210
265
nodes[ 0 ] . node . process_background_events ( ) ; // Required to free the pending background monitor update
211
266
check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
212
- let channel_state = nodes[ 0 ] . node . channel_state . lock ( ) . unwrap ( ) ;
213
- assert_eq ! ( channel_state. by_id. len( ) , 0 ) ;
214
- assert_eq ! ( channel_state. short_to_id. len( ) , 0 ) ;
267
+ }
268
+ #[ test]
269
+ fn test_unconf_chan ( ) {
270
+ do_test_unconf_chan ( true ) ;
271
+ do_test_unconf_chan ( false ) ;
215
272
}
216
273
217
274
#[ test]
0 commit comments