@@ -170,10 +170,13 @@ impl BackgroundProcessor {
170
170
171
171
#[ cfg( test) ]
172
172
mod tests {
173
+ use bitcoin:: blockdata:: block:: BlockHeader ;
173
174
use bitcoin:: blockdata:: constants:: genesis_block;
174
175
use bitcoin:: blockdata:: transaction:: { Transaction , TxOut } ;
175
176
use bitcoin:: network:: constants:: Network ;
177
+ use lightning:: chain:: Confirm ;
176
178
use lightning:: chain:: chainmonitor;
179
+ use lightning:: chain:: channelmonitor:: ANTI_REORG_DELAY ;
177
180
use lightning:: chain:: keysinterface:: { InMemorySigner , KeysInterface , KeysManager } ;
178
181
use lightning:: chain:: transaction:: OutPoint ;
179
182
use lightning:: get_event_msg;
@@ -192,6 +195,8 @@ mod tests {
192
195
use std:: time:: Duration ;
193
196
use super :: { BackgroundProcessor , FRESHNESS_TIMER } ;
194
197
198
+ const EVENT_DEADLINE : u64 = 5 * FRESHNESS_TIMER ;
199
+
195
200
#[ derive( Clone , Eq , Hash , PartialEq ) ]
196
201
struct TestDescriptor { }
197
202
impl SocketDescriptor for TestDescriptor {
@@ -209,7 +214,9 @@ mod tests {
209
214
peer_manager : Arc < PeerManager < TestDescriptor , Arc < test_utils:: TestChannelMessageHandler > , Arc < test_utils:: TestRoutingMessageHandler > , Arc < test_utils:: TestLogger > > > ,
210
215
chain_monitor : Arc < ChainMonitor > ,
211
216
persister : Arc < FilesystemPersister > ,
217
+ tx_broadcaster : Arc < test_utils:: TestBroadcaster > ,
212
218
logger : Arc < test_utils:: TestLogger > ,
219
+ best_block : BestBlock ,
213
220
}
214
221
215
222
impl Drop for Node {
@@ -241,14 +248,12 @@ mod tests {
241
248
let now = Duration :: from_secs ( genesis_block ( network) . header . time as u64 ) ;
242
249
let keys_manager = Arc :: new ( KeysManager :: new ( & seed, now. as_secs ( ) , now. subsec_nanos ( ) ) ) ;
243
250
let chain_monitor = Arc :: new ( chainmonitor:: ChainMonitor :: new ( Some ( chain_source. clone ( ) ) , tx_broadcaster. clone ( ) , logger. clone ( ) , fee_estimator. clone ( ) , persister. clone ( ) ) ) ;
244
- let params = ChainParameters {
245
- network,
246
- best_block : BestBlock :: from_genesis ( network) ,
247
- } ;
248
- let manager = Arc :: new ( ChannelManager :: new ( fee_estimator. clone ( ) , chain_monitor. clone ( ) , tx_broadcaster, logger. clone ( ) , keys_manager. clone ( ) , UserConfig :: default ( ) , params) ) ;
251
+ let best_block = BestBlock :: from_genesis ( network) ;
252
+ let params = ChainParameters { network, best_block } ;
253
+ let manager = Arc :: new ( ChannelManager :: new ( fee_estimator. clone ( ) , chain_monitor. clone ( ) , tx_broadcaster. clone ( ) , logger. clone ( ) , keys_manager. clone ( ) , UserConfig :: default ( ) , params) ) ;
249
254
let msg_handler = MessageHandler { chan_handler : Arc :: new ( test_utils:: TestChannelMessageHandler :: new ( ) ) , route_handler : Arc :: new ( test_utils:: TestRoutingMessageHandler :: new ( ) ) } ;
250
255
let peer_manager = Arc :: new ( PeerManager :: new ( msg_handler, keys_manager. get_node_secret ( ) , & seed, logger. clone ( ) ) ) ;
251
- let node = Node { node : manager, peer_manager, chain_monitor, persister, logger } ;
256
+ let node = Node { node : manager, peer_manager, chain_monitor, persister, tx_broadcaster , logger, best_block } ;
252
257
nodes. push ( node) ;
253
258
}
254
259
nodes
@@ -298,6 +303,27 @@ mod tests {
298
303
} }
299
304
}
300
305
306
+ fn confirm_transaction ( node : & mut Node , tx : & Transaction ) {
307
+ for i in 1 ..=ANTI_REORG_DELAY {
308
+ let prev_blockhash = node. best_block . block_hash ( ) ;
309
+ let height = node. best_block . height ( ) + 1 ;
310
+ let header = BlockHeader { version : 0x20000000 , prev_blockhash, merkle_root : Default :: default ( ) , time : height, bits : 42 , nonce : 42 } ;
311
+ let txdata = vec ! [ ( 0 , tx) ] ;
312
+ node. best_block = BestBlock :: new ( header. block_hash ( ) , height) ;
313
+ match i {
314
+ 1 => {
315
+ node. node . transactions_confirmed ( & header, & txdata, height) ;
316
+ node. chain_monitor . transactions_confirmed ( & header, & txdata, height) ;
317
+ } ,
318
+ ANTI_REORG_DELAY => {
319
+ node. node . best_block_updated ( & header, height) ;
320
+ node. chain_monitor . best_block_updated ( & header, height) ;
321
+ } ,
322
+ _ => { } ,
323
+ }
324
+ }
325
+ }
326
+
301
327
#[ test]
302
328
fn test_background_processor ( ) {
303
329
// Test that when a new channel is created, the ChannelManager needs to be re-persisted with
@@ -391,7 +417,7 @@ mod tests {
391
417
392
418
#[ test]
393
419
fn test_background_event_handling ( ) {
394
- let nodes = create_nodes ( 2 , "test_background_event_handling" . to_string ( ) ) ;
420
+ let mut nodes = create_nodes ( 2 , "test_background_event_handling" . to_string ( ) ) ;
395
421
let channel_value = 100000 ;
396
422
let data_dir = nodes[ 0 ] . persister . get_data_dir ( ) ;
397
423
let persister = move |node : & _ | FilesystemPersister :: persist_manager ( data_dir. clone ( ) , node) ;
@@ -401,15 +427,39 @@ mod tests {
401
427
let event_handler = move |event| {
402
428
sender. send ( handle_funding_generation_ready ! ( event, channel_value) ) . unwrap ( ) ;
403
429
} ;
404
- let bg_processor = BackgroundProcessor :: start ( persister, event_handler, nodes[ 0 ] . chain_monitor . clone ( ) , nodes[ 0 ] . node . clone ( ) , nodes[ 0 ] . peer_manager . clone ( ) , nodes[ 0 ] . logger . clone ( ) ) ;
430
+ let bg_processor = BackgroundProcessor :: start ( persister. clone ( ) , event_handler, nodes[ 0 ] . chain_monitor . clone ( ) , nodes[ 0 ] . node . clone ( ) , nodes[ 0 ] . peer_manager . clone ( ) , nodes[ 0 ] . logger . clone ( ) ) ;
405
431
406
432
// Open a channel and check that the FundingGenerationReady event was handled.
407
433
begin_open_channel ! ( nodes[ 0 ] , nodes[ 1 ] , channel_value) ;
408
- let timeout = Duration :: from_secs ( 5 * FRESHNESS_TIMER ) ;
409
- let ( temporary_channel_id, tx) = receiver
410
- . recv_timeout ( timeout)
434
+ let ( temporary_channel_id, funding_tx) = receiver
435
+ . recv_timeout ( Duration :: from_secs ( EVENT_DEADLINE ) )
411
436
. expect ( "FundingGenerationReady not handled within deadline" ) ;
412
- end_open_channel ! ( nodes[ 0 ] , nodes[ 1 ] , temporary_channel_id, tx) ;
437
+ end_open_channel ! ( nodes[ 0 ] , nodes[ 1 ] , temporary_channel_id, funding_tx) ;
438
+
439
+ // Confirm the funding transaction.
440
+ confirm_transaction ( & mut nodes[ 0 ] , & funding_tx) ;
441
+ confirm_transaction ( & mut nodes[ 1 ] , & funding_tx) ;
442
+ nodes[ 0 ] . node . handle_funding_locked ( & nodes[ 1 ] . node . get_our_node_id ( ) , & get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendFundingLocked , nodes[ 0 ] . node. get_our_node_id( ) ) ) ;
443
+ nodes[ 1 ] . node . handle_funding_locked ( & nodes[ 0 ] . node . get_our_node_id ( ) , & get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendFundingLocked , nodes[ 1 ] . node. get_our_node_id( ) ) ) ;
444
+
445
+ assert ! ( bg_processor. stop( ) . is_ok( ) ) ;
446
+
447
+ // Set up a background event handler for SpendableOutputs events.
448
+ let ( sender, receiver) = std:: sync:: mpsc:: sync_channel ( 1 ) ;
449
+ let event_handler = move |event| sender. send ( event) . unwrap ( ) ;
450
+ let bg_processor = BackgroundProcessor :: start ( persister, event_handler, nodes[ 0 ] . chain_monitor . clone ( ) , nodes[ 0 ] . node . clone ( ) , nodes[ 0 ] . peer_manager . clone ( ) , nodes[ 0 ] . logger . clone ( ) ) ;
451
+
452
+ // Force close the channel and check that the SpendableOutputs event was handled.
453
+ nodes[ 0 ] . node . force_close_channel ( & nodes[ 0 ] . node . list_channels ( ) [ 0 ] . channel_id ) . unwrap ( ) ;
454
+ let commitment_tx = nodes[ 0 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . pop ( ) . unwrap ( ) ;
455
+ confirm_transaction ( & mut nodes[ 0 ] , & commitment_tx) ;
456
+ let event = receiver
457
+ . recv_timeout ( Duration :: from_secs ( EVENT_DEADLINE ) )
458
+ . expect ( "SpendableOutputs not handled within deadline" ) ;
459
+ match event {
460
+ Event :: SpendableOutputs { .. } => { } ,
461
+ _ => panic ! ( "Unexpected event: {:?}" , event) ,
462
+ }
413
463
414
464
assert ! ( bg_processor. stop( ) . is_ok( ) ) ;
415
465
}
0 commit comments