Skip to content

Commit fc12d43

Browse files
Add lock order docs to ChannelManager fields
1 parent 3379f8c commit fc12d43

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,38 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, M, T, F, L> = ChannelManage
667667
/// essentially you should default to using a SimpleRefChannelManager, and use a
668668
/// SimpleArcChannelManager when you require a ChannelManager with a static lifetime, such as when
669669
/// 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+
//
670702
pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
671703
where M::Target: chain::Watch<Signer>,
672704
T::Target: BroadcasterInterface,
@@ -680,12 +712,14 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
680712
chain_monitor: M,
681713
tx_broadcaster: T,
682714

715+
/// See `ChannelManager` struct-level documentation for lock order requirements.
683716
#[cfg(test)]
684717
pub(super) best_block: RwLock<BestBlock>,
685718
#[cfg(not(test))]
686719
best_block: RwLock<BestBlock>,
687720
secp_ctx: Secp256k1<secp256k1::All>,
688721

722+
/// See `ChannelManager` struct-level documentation for lock order requirements.
689723
#[cfg(any(test, feature = "_test_utils"))]
690724
pub(super) channel_state: Mutex<ChannelHolder<Signer>>,
691725
#[cfg(not(any(test, feature = "_test_utils")))]
@@ -695,7 +729,8 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
695729
/// expose them to users via a PaymentReceived event. HTLCs which do not meet the requirements
696730
/// here are failed when we process them as pending-forwardable-HTLCs, and entries are removed
697731
/// 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.
699734
pending_inbound_payments: Mutex<HashMap<PaymentHash, PendingInboundPayment>>,
700735

701736
/// 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,
709744
///
710745
/// See `PendingOutboundPayment` documentation for more info.
711746
///
712-
/// Locked *after* channel_state.
747+
/// See `ChannelManager` struct-level documentation for lock order requirements.
713748
pending_outbound_payments: Mutex<HashMap<PaymentId, PendingOutboundPayment>>,
714749

715750
/// 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,
720755
///
721756
/// Note that no consistency guarantees are made about the existence of a channel with the
722757
/// `short_channel_id` here, nor the `short_channel_id` in the `PendingHTLCInfo`!
758+
///
759+
/// See `ChannelManager` struct-level documentation for lock order requirements.
723760
#[cfg(test)]
724761
pub(super) forward_htlcs: Mutex<HashMap<u64, Vec<HTLCForwardInfo>>>,
725762
#[cfg(not(test))]
@@ -729,6 +766,8 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
729766
/// and some closed channels which reached a usable state prior to being closed. This is used
730767
/// only to avoid duplicates, and is not persisted explicitly to disk, but rebuilt from the
731768
/// active channel list on load.
769+
///
770+
/// See `ChannelManager` struct-level documentation for lock order requirements.
732771
outbound_scid_aliases: Mutex<HashSet<u64>>,
733772

734773
/// `channel_id` -> `counterparty_node_id`.
@@ -748,6 +787,8 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
748787
/// We should add `counterparty_node_id`s to `MonitorEvent`s, and eventually rely on it in the
749788
/// future. That would make this map redundant, as only the `ChannelManager::per_peer_state` is
750789
/// required to access the channel with the `counterparty_node_id`.
790+
///
791+
/// See `ChannelManager` struct-level documentation for lock order requirements.
751792
id_to_peer: Mutex<HashMap<[u8; 32], PublicKey>>,
752793

753794
our_network_key: SecretKey,
@@ -779,10 +820,12 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
779820
/// operate on the inner value freely. Sadly, this prevents parallel operation when opening a
780821
/// new channel.
781822
///
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.
783824
per_peer_state: RwLock<HashMap<PublicKey, Mutex<PeerState>>>,
784825

826+
/// See `ChannelManager` struct-level documentation for lock order requirements.
785827
pending_events: Mutex<Vec<events::Event>>,
828+
/// See `ChannelManager` struct-level documentation for lock order requirements.
786829
pending_background_events: Mutex<Vec<BackgroundEvent>>,
787830
/// Used when we have to take a BIG lock to make sure everything is self-consistent.
788831
/// Essentially just when we're serializing ourselves out.

0 commit comments

Comments
 (0)