@@ -15,9 +15,10 @@ use crate::chain::chaininterface;
15
15
use crate :: chain:: chaininterface:: ConfirmationTarget ;
16
16
#[ cfg( test) ]
17
17
use crate :: chain:: chaininterface:: FEERATE_FLOOR_SATS_PER_KW ;
18
- use crate :: chain:: chainmonitor;
19
- use crate :: chain:: channelmonitor;
20
- use crate :: chain:: channelmonitor:: MonitorEvent ;
18
+ use crate :: chain:: chainmonitor:: { ChainMonitor , Persist } ;
19
+ use crate :: chain:: channelmonitor:: {
20
+ ChannelMonitor , ChannelMonitorUpdate , ChannelMonitorUpdateStep , MonitorEvent ,
21
+ } ;
21
22
use crate :: chain:: transaction:: OutPoint ;
22
23
use crate :: chain:: WatchedOutput ;
23
24
use crate :: events;
@@ -386,16 +387,16 @@ impl SignerProvider for OnlyReadsKeysInterface {
386
387
}
387
388
388
389
pub struct TestChainMonitor < ' a > {
389
- pub added_monitors : Mutex < Vec < ( OutPoint , channelmonitor :: ChannelMonitor < TestChannelSigner > ) > > ,
390
- pub monitor_updates : Mutex < HashMap < ChannelId , Vec < channelmonitor :: ChannelMonitorUpdate > > > ,
390
+ pub added_monitors : Mutex < Vec < ( OutPoint , ChannelMonitor < TestChannelSigner > ) > > ,
391
+ pub monitor_updates : Mutex < HashMap < ChannelId , Vec < ChannelMonitorUpdate > > > ,
391
392
pub latest_monitor_update_id : Mutex < HashMap < ChannelId , ( OutPoint , u64 , u64 ) > > ,
392
- pub chain_monitor : chainmonitor :: ChainMonitor <
393
+ pub chain_monitor : ChainMonitor <
393
394
TestChannelSigner ,
394
395
& ' a TestChainSource ,
395
396
& ' a dyn chaininterface:: BroadcasterInterface ,
396
397
& ' a TestFeeEstimator ,
397
398
& ' a TestLogger ,
398
- & ' a dyn chainmonitor :: Persist < TestChannelSigner > ,
399
+ & ' a dyn Persist < TestChannelSigner > ,
399
400
> ,
400
401
pub keys_manager : & ' a TestKeysInterface ,
401
402
/// If this is set to Some(), the next update_channel call (not watch_channel) must be a
@@ -410,29 +411,23 @@ impl<'a> TestChainMonitor<'a> {
410
411
pub fn new (
411
412
chain_source : Option < & ' a TestChainSource > ,
412
413
broadcaster : & ' a dyn chaininterface:: BroadcasterInterface , logger : & ' a TestLogger ,
413
- fee_estimator : & ' a TestFeeEstimator ,
414
- persister : & ' a dyn chainmonitor:: Persist < TestChannelSigner > ,
414
+ fee_estimator : & ' a TestFeeEstimator , persister : & ' a dyn Persist < TestChannelSigner > ,
415
415
keys_manager : & ' a TestKeysInterface ,
416
416
) -> Self {
417
- let added_monitors = Mutex :: new ( Vec :: new ( ) ) ;
418
- let monitor_updates = Mutex :: new ( new_hash_map ( ) ) ;
419
- let latest_monitor_update_id = Mutex :: new ( new_hash_map ( ) ) ;
420
- let expect_channel_force_closed = Mutex :: new ( None ) ;
421
- let expect_monitor_round_trip_fail = Mutex :: new ( None ) ;
422
417
Self {
423
- added_monitors,
424
- monitor_updates,
425
- latest_monitor_update_id,
426
- chain_monitor : chainmonitor :: ChainMonitor :: new (
418
+ added_monitors : Mutex :: new ( Vec :: new ( ) ) ,
419
+ monitor_updates : Mutex :: new ( new_hash_map ( ) ) ,
420
+ latest_monitor_update_id : Mutex :: new ( new_hash_map ( ) ) ,
421
+ chain_monitor : ChainMonitor :: new (
427
422
chain_source,
428
423
broadcaster,
429
424
logger,
430
425
fee_estimator,
431
426
persister,
432
427
) ,
433
428
keys_manager,
434
- expect_channel_force_closed,
435
- expect_monitor_round_trip_fail,
429
+ expect_channel_force_closed : Mutex :: new ( None ) ,
430
+ expect_monitor_round_trip_fail : Mutex :: new ( None ) ,
436
431
}
437
432
}
438
433
@@ -444,13 +439,13 @@ impl<'a> TestChainMonitor<'a> {
444
439
}
445
440
impl < ' a > chain:: Watch < TestChannelSigner > for TestChainMonitor < ' a > {
446
441
fn watch_channel (
447
- & self , funding_txo : OutPoint , monitor : channelmonitor :: ChannelMonitor < TestChannelSigner > ,
442
+ & self , funding_txo : OutPoint , monitor : ChannelMonitor < TestChannelSigner > ,
448
443
) -> Result < chain:: ChannelMonitorUpdateStatus , ( ) > {
449
444
// At every point where we get a monitor update, we should be able to send a useful monitor
450
445
// to a watchtower and disk...
451
446
let mut w = TestVecWriter ( Vec :: new ( ) ) ;
452
447
monitor. write ( & mut w) . unwrap ( ) ;
453
- let new_monitor = <( BlockHash , channelmonitor :: ChannelMonitor < TestChannelSigner > ) >:: read (
448
+ let new_monitor = <( BlockHash , ChannelMonitor < TestChannelSigner > ) >:: read (
454
449
& mut io:: Cursor :: new ( & w. 0 ) ,
455
450
( self . keys_manager , self . keys_manager ) ,
456
451
)
@@ -466,15 +461,12 @@ impl<'a> chain::Watch<TestChannelSigner> for TestChainMonitor<'a> {
466
461
}
467
462
468
463
fn update_channel (
469
- & self , funding_txo : OutPoint , update : & channelmonitor :: ChannelMonitorUpdate ,
464
+ & self , funding_txo : OutPoint , update : & ChannelMonitorUpdate ,
470
465
) -> chain:: ChannelMonitorUpdateStatus {
471
466
// Every monitor update should survive roundtrip
472
467
let mut w = TestVecWriter ( Vec :: new ( ) ) ;
473
468
update. write ( & mut w) . unwrap ( ) ;
474
- assert ! (
475
- channelmonitor:: ChannelMonitorUpdate :: read( & mut io:: Cursor :: new( & w. 0 ) ) . unwrap( )
476
- == * update
477
- ) ;
469
+ assert_eq ! ( ChannelMonitorUpdate :: read( & mut & w. 0 [ ..] ) . unwrap( ) , * update) ;
478
470
let channel_id =
479
471
update. channel_id . unwrap_or ( ChannelId :: v1_from_funding_outpoint ( funding_txo) ) ;
480
472
@@ -488,11 +480,9 @@ impl<'a> chain::Watch<TestChannelSigner> for TestChainMonitor<'a> {
488
480
if let Some ( exp) = self . expect_channel_force_closed . lock ( ) . unwrap ( ) . take ( ) {
489
481
assert_eq ! ( channel_id, exp. 0 ) ;
490
482
assert_eq ! ( update. updates. len( ) , 1 ) ;
491
- if let channelmonitor:: ChannelMonitorUpdateStep :: ChannelForceClosed {
492
- should_broadcast,
493
- } = update. updates [ 0 ]
494
- {
495
- assert_eq ! ( should_broadcast, exp. 1 ) ;
483
+ let update = & update. updates [ 0 ] ;
484
+ if let ChannelMonitorUpdateStep :: ChannelForceClosed { should_broadcast } = update {
485
+ assert_eq ! ( * should_broadcast, exp. 1 ) ;
496
486
} else {
497
487
panic ! ( ) ;
498
488
}
@@ -508,7 +498,7 @@ impl<'a> chain::Watch<TestChannelSigner> for TestChainMonitor<'a> {
508
498
let monitor = self . chain_monitor . get_monitor ( funding_txo) . unwrap ( ) ;
509
499
w. 0 . clear ( ) ;
510
500
monitor. write ( & mut w) . unwrap ( ) ;
511
- let new_monitor = <( BlockHash , channelmonitor :: ChannelMonitor < TestChannelSigner > ) >:: read (
501
+ let new_monitor = <( BlockHash , ChannelMonitor < TestChannelSigner > ) >:: read (
512
502
& mut io:: Cursor :: new ( & w. 0 ) ,
513
503
( self . keys_manager , self . keys_manager ) ,
514
504
)
@@ -598,11 +588,9 @@ impl WatchtowerPersister {
598
588
}
599
589
600
590
#[ cfg( test) ]
601
- impl < Signer : sign:: ecdsa:: EcdsaChannelSigner > chainmonitor:: Persist < Signer >
602
- for WatchtowerPersister
603
- {
591
+ impl < Signer : sign:: ecdsa:: EcdsaChannelSigner > Persist < Signer > for WatchtowerPersister {
604
592
fn persist_new_channel (
605
- & self , funding_txo : OutPoint , data : & channelmonitor :: ChannelMonitor < Signer > ,
593
+ & self , funding_txo : OutPoint , data : & ChannelMonitor < Signer > ,
606
594
) -> chain:: ChannelMonitorUpdateStatus {
607
595
let res = self . persister . persist_new_channel ( funding_txo, data) ;
608
596
@@ -635,8 +623,8 @@ impl<Signer: sign::ecdsa::EcdsaChannelSigner> chainmonitor::Persist<Signer>
635
623
}
636
624
637
625
fn update_persisted_channel (
638
- & self , funding_txo : OutPoint , update : Option < & channelmonitor :: ChannelMonitorUpdate > ,
639
- data : & channelmonitor :: ChannelMonitor < Signer > ,
626
+ & self , funding_txo : OutPoint , update : Option < & ChannelMonitorUpdate > ,
627
+ data : & ChannelMonitor < Signer > ,
640
628
) -> chain:: ChannelMonitorUpdateStatus {
641
629
let res = self . persister . update_persisted_channel ( funding_txo, update, data) ;
642
630
@@ -679,7 +667,7 @@ impl<Signer: sign::ecdsa::EcdsaChannelSigner> chainmonitor::Persist<Signer>
679
667
}
680
668
681
669
fn archive_persisted_channel ( & self , funding_txo : OutPoint ) {
682
- <TestPersister as chainmonitor :: Persist < TestChannelSigner > >:: archive_persisted_channel (
670
+ <TestPersister as Persist < TestChannelSigner > >:: archive_persisted_channel (
683
671
& self . persister ,
684
672
funding_txo,
685
673
) ;
@@ -692,8 +680,6 @@ pub struct TestPersister {
692
680
pub update_rets : Mutex < VecDeque < chain:: ChannelMonitorUpdateStatus > > ,
693
681
/// When we get an update_persisted_channel call *with* a ChannelMonitorUpdate, we insert the
694
682
/// [`ChannelMonitor::get_latest_update_id`] here.
695
- ///
696
- /// [`ChannelMonitor`]: channelmonitor::ChannelMonitor
697
683
pub offchain_monitor_updates : Mutex < HashMap < OutPoint , HashSet < u64 > > > ,
698
684
/// When we get an update_persisted_channel call with no ChannelMonitorUpdate, we insert the
699
685
/// monitor's funding outpoint here.
@@ -712,9 +698,9 @@ impl TestPersister {
712
698
self . update_rets . lock ( ) . unwrap ( ) . push_back ( next_ret) ;
713
699
}
714
700
}
715
- impl < Signer : sign:: ecdsa:: EcdsaChannelSigner > chainmonitor :: Persist < Signer > for TestPersister {
701
+ impl < Signer : sign:: ecdsa:: EcdsaChannelSigner > Persist < Signer > for TestPersister {
716
702
fn persist_new_channel (
717
- & self , _funding_txo : OutPoint , _data : & channelmonitor :: ChannelMonitor < Signer > ,
703
+ & self , _funding_txo : OutPoint , _data : & ChannelMonitor < Signer > ,
718
704
) -> chain:: ChannelMonitorUpdateStatus {
719
705
if let Some ( update_ret) = self . update_rets . lock ( ) . unwrap ( ) . pop_front ( ) {
720
706
return update_ret;
@@ -723,8 +709,8 @@ impl<Signer: sign::ecdsa::EcdsaChannelSigner> chainmonitor::Persist<Signer> for
723
709
}
724
710
725
711
fn update_persisted_channel (
726
- & self , funding_txo : OutPoint , update : Option < & channelmonitor :: ChannelMonitorUpdate > ,
727
- _data : & channelmonitor :: ChannelMonitor < Signer > ,
712
+ & self , funding_txo : OutPoint , update : Option < & ChannelMonitorUpdate > ,
713
+ _data : & ChannelMonitor < Signer > ,
728
714
) -> chain:: ChannelMonitorUpdateStatus {
729
715
let mut ret = chain:: ChannelMonitorUpdateStatus :: Completed ;
730
716
if let Some ( update_ret) = self . update_rets . lock ( ) . unwrap ( ) . pop_front ( ) {
@@ -918,13 +904,10 @@ impl TestChannelMessageHandler {
918
904
919
905
impl TestChannelMessageHandler {
920
906
pub fn new ( chain_hash : ChainHash ) -> Self {
921
- let pending_events = Mutex :: new ( Vec :: new ( ) ) ;
922
- let expected_recv_msgs = Mutex :: new ( None ) ;
923
- let connected_peers = Mutex :: new ( new_hash_set ( ) ) ;
924
907
TestChannelMessageHandler {
925
- pending_events,
926
- expected_recv_msgs,
927
- connected_peers,
908
+ pending_events : Mutex :: new ( Vec :: new ( ) ) ,
909
+ expected_recv_msgs : Mutex :: new ( None ) ,
910
+ connected_peers : Mutex :: new ( new_hash_set ( ) ) ,
928
911
chain_hash,
929
912
}
930
913
}
@@ -1576,19 +1559,14 @@ impl SignerProvider for TestKeysInterface {
1576
1559
impl TestKeysInterface {
1577
1560
pub fn new ( seed : & [ u8 ; 32 ] , network : Network ) -> Self {
1578
1561
let now = Duration :: from_secs ( genesis_block ( network) . header . time as u64 ) ;
1579
- let override_random_bytes = Mutex :: new ( None ) ;
1580
- let enforcement_states = Mutex :: new ( new_hash_map ( ) ) ;
1581
- let expectations = Mutex :: new ( None ) ;
1582
- let unavailable_signers_ops = Mutex :: new ( new_hash_map ( ) ) ;
1583
- let next_signer_disabled_ops = Mutex :: new ( new_hash_set ( ) ) ;
1584
1562
Self {
1585
1563
backing : sign:: PhantomKeysManager :: new ( seed, now. as_secs ( ) , now. subsec_nanos ( ) , seed) ,
1586
- override_random_bytes,
1564
+ override_random_bytes : Mutex :: new ( None ) ,
1587
1565
disable_revocation_policy_check : false ,
1588
- enforcement_states,
1589
- expectations,
1590
- unavailable_signers_ops,
1591
- next_signer_disabled_ops,
1566
+ enforcement_states : Mutex :: new ( new_hash_map ( ) ) ,
1567
+ expectations : Mutex :: new ( None ) ,
1568
+ unavailable_signers_ops : Mutex :: new ( new_hash_map ( ) ) ,
1569
+ next_signer_disabled_ops : Mutex :: new ( new_hash_set ( ) ) ,
1592
1570
}
1593
1571
}
1594
1572
@@ -1662,14 +1640,12 @@ impl TestChainSource {
1662
1640
let script_pubkey = Builder :: new ( ) . push_opcode ( opcodes:: OP_TRUE ) . into_script ( ) ;
1663
1641
let utxo_ret =
1664
1642
Mutex :: new ( UtxoResult :: Sync ( Ok ( TxOut { value : Amount :: MAX , script_pubkey } ) ) ) ;
1665
- let watched_txn = Mutex :: new ( new_hash_set ( ) ) ;
1666
- let watched_outputs = Mutex :: new ( new_hash_set ( ) ) ;
1667
1643
Self {
1668
1644
chain_hash : ChainHash :: using_genesis_block ( network) ,
1669
1645
utxo_ret,
1670
1646
get_utxo_call_count : AtomicUsize :: new ( 0 ) ,
1671
- watched_txn,
1672
- watched_outputs,
1647
+ watched_txn : Mutex :: new ( new_hash_set ( ) ) ,
1648
+ watched_outputs : Mutex :: new ( new_hash_set ( ) ) ,
1673
1649
}
1674
1650
}
1675
1651
pub fn remove_watched_txn_and_outputs ( & self , outpoint : OutPoint , script_pubkey : ScriptBuf ) {
0 commit comments