@@ -667,6 +667,38 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, M, T, F, L> = ChannelManage
667
667
/// essentially you should default to using a SimpleRefChannelManager, and use a
668
668
/// SimpleArcChannelManager when you require a ChannelManager with a static lifetime, such as when
669
669
/// you're using lightning-net-tokio.
670
+ //
671
+ // Lock order:
672
+ // The tree structure below illustrates the lock order requirements for the different locks of the
673
+ // `ChannelManager`. Locks can be held at the same time if they are on the same branch in the tree,
674
+ // and should then be taken in the order of the lowest to the highest level in the tree.
675
+ // Note that locks on different branches shall not be taken at the same time, as doing so will
676
+ // create a new lock order for those specific locks in the order they were taken.
677
+ //
678
+ // Lock order tree:
679
+ //
680
+ // `total_consistency_lock`
681
+ // |
682
+ // |__`forward_htlcs`
683
+ // |
684
+ // |__`channel_state`
685
+ // | |
686
+ // | |__`id_to_peer`
687
+ // | |
688
+ // | |__`per_peer_state`
689
+ // | |
690
+ // | |__`outbound_scid_aliases`
691
+ // | |
692
+ // | |__`pending_inbound_payments`
693
+ // | |
694
+ // | |__`pending_outbound_payments`
695
+ // | |
696
+ // | |__`best_block`
697
+ // | |
698
+ // | |__`pending_events`
699
+ // | |
700
+ // | |__`pending_background_events`
701
+ //
670
702
pub struct ChannelManager < Signer : Sign , M : Deref , T : Deref , K : Deref , F : Deref , L : Deref >
671
703
where M :: Target : chain:: Watch < Signer > ,
672
704
T :: Target : BroadcasterInterface ,
@@ -680,12 +712,14 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
680
712
chain_monitor : M ,
681
713
tx_broadcaster : T ,
682
714
715
+ /// See `ChannelManager` struct-level documentation for lock order requirements.
683
716
#[ cfg( test) ]
684
717
pub ( super ) best_block : RwLock < BestBlock > ,
685
718
#[ cfg( not( test) ) ]
686
719
best_block : RwLock < BestBlock > ,
687
720
secp_ctx : Secp256k1 < secp256k1:: All > ,
688
721
722
+ /// See `ChannelManager` struct-level documentation for lock order requirements.
689
723
#[ cfg( any( test, feature = "_test_utils" ) ) ]
690
724
pub ( super ) channel_state : Mutex < ChannelHolder < Signer > > ,
691
725
#[ cfg( not( any( test, feature = "_test_utils" ) ) ) ]
@@ -695,7 +729,8 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
695
729
/// expose them to users via a PaymentReceived event. HTLCs which do not meet the requirements
696
730
/// here are failed when we process them as pending-forwardable-HTLCs, and entries are removed
697
731
/// after we generate a PaymentReceived upon receipt of all MPP parts or when they time out.
698
- /// Locked *after* channel_state.
732
+ ///
733
+ /// See `ChannelManager` struct-level documentation for lock order requirements.
699
734
pending_inbound_payments : Mutex < HashMap < PaymentHash , PendingInboundPayment > > ,
700
735
701
736
/// The session_priv bytes and retry metadata of outbound payments which are pending resolution.
@@ -709,7 +744,7 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
709
744
///
710
745
/// See `PendingOutboundPayment` documentation for more info.
711
746
///
712
- /// Locked *after* channel_state .
747
+ /// See `ChannelManager` struct-level documentation for lock order requirements .
713
748
pending_outbound_payments : Mutex < HashMap < PaymentId , PendingOutboundPayment > > ,
714
749
715
750
/// SCID/SCID Alias -> forward infos. Key of 0 means payments received.
@@ -720,6 +755,8 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
720
755
///
721
756
/// Note that no consistency guarantees are made about the existence of a channel with the
722
757
/// `short_channel_id` here, nor the `short_channel_id` in the `PendingHTLCInfo`!
758
+ ///
759
+ /// See `ChannelManager` struct-level documentation for lock order requirements.
723
760
#[ cfg( test) ]
724
761
pub ( super ) forward_htlcs : Mutex < HashMap < u64 , Vec < HTLCForwardInfo > > > ,
725
762
#[ cfg( not( test) ) ]
@@ -729,6 +766,8 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
729
766
/// and some closed channels which reached a usable state prior to being closed. This is used
730
767
/// only to avoid duplicates, and is not persisted explicitly to disk, but rebuilt from the
731
768
/// active channel list on load.
769
+ ///
770
+ /// See `ChannelManager` struct-level documentation for lock order requirements.
732
771
outbound_scid_aliases : Mutex < HashSet < u64 > > ,
733
772
734
773
/// `channel_id` -> `counterparty_node_id`.
@@ -748,6 +787,8 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
748
787
/// We should add `counterparty_node_id`s to `MonitorEvent`s, and eventually rely on it in the
749
788
/// future. That would make this map redundant, as only the `ChannelManager::per_peer_state` is
750
789
/// required to access the channel with the `counterparty_node_id`.
790
+ ///
791
+ /// See `ChannelManager` struct-level documentation for lock order requirements.
751
792
id_to_peer : Mutex < HashMap < [ u8 ; 32 ] , PublicKey > > ,
752
793
753
794
our_network_key : SecretKey ,
@@ -779,10 +820,12 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
779
820
/// operate on the inner value freely. Sadly, this prevents parallel operation when opening a
780
821
/// new channel.
781
822
///
782
- /// If also holding `channel_state` lock, must lock `channel_state` prior to `per_peer_state` .
823
+ /// See `ChannelManager` struct-level documentation for lock order requirements .
783
824
per_peer_state : RwLock < HashMap < PublicKey , Mutex < PeerState > > > ,
784
825
826
+ /// See `ChannelManager` struct-level documentation for lock order requirements.
785
827
pending_events : Mutex < Vec < events:: Event > > ,
828
+ /// See `ChannelManager` struct-level documentation for lock order requirements.
786
829
pending_background_events : Mutex < Vec < BackgroundEvent > > ,
787
830
/// Used when we have to take a BIG lock to make sure everything is self-consistent.
788
831
/// Essentially just when we're serializing ourselves out.
0 commit comments