Skip to content

Commit f552957

Browse files
f - Add Lock order tree, and update docs
1 parent 9768cad commit f552957

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -669,18 +669,28 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, M, T, F, L> = ChannelManage
669669
/// you're using lightning-net-tokio.
670670
//
671671
// Lock order:
672-
// These `ChannelManager` locks should be aquired in the following lock order, if any of these
673-
// locks are held at the same time:
674-
// 1. `channel_state`
675-
// 2. `per_peer_state`
676-
// 3. `pending_inbound_payments`
677-
// 4. `pending_outbound_payments`
678-
// 5. `pending_events`
679-
// 6. `pending_background_events`
672+
// The tree structure below illustrates the lock order requirements for the different locks of the
673+
// `ChannelManager`. If two or more locks on the same branch is held at the same time, locks should
674+
// be taken in the order of the lowest to highest level in the tree.
680675
//
681-
// These locks should be aquired after the `channel_state` lock, in case the `channel_state` lock
682-
// is taken at the same time:
683-
// * `id_to_peer`
676+
// Lock order tree:
677+
// │
678+
// │──`channel_state`
679+
// │ │
680+
// │ │──`id_to_peer`
681+
// │ │
682+
// │ │──`per_peer_state`
683+
// │ │
684+
// │ │──`best_block`
685+
// │ │
686+
// │ │──`pending_inbound_payments`
687+
// │ │
688+
// │ │──`pending_outbound_payments`
689+
// │ │
690+
// │ │──`pending_events`
691+
// │ │
692+
// │ │──`pending_background_events`
693+
// │
684694
pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
685695
where M::Target: chain::Watch<Signer>,
686696
T::Target: BroadcasterInterface,
@@ -694,12 +704,14 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
694704
chain_monitor: M,
695705
tx_broadcaster: T,
696706

707+
/// See `ChannelManager` struct-level documentation for lock order requirements.
697708
#[cfg(test)]
698709
pub(super) best_block: RwLock<BestBlock>,
699710
#[cfg(not(test))]
700711
best_block: RwLock<BestBlock>,
701712
secp_ctx: Secp256k1<secp256k1::All>,
702713

714+
/// See `ChannelManager` struct-level documentation for lock order requirements.
703715
#[cfg(any(test, feature = "_test_utils"))]
704716
pub(super) channel_state: Mutex<ChannelHolder<Signer>>,
705717
#[cfg(not(any(test, feature = "_test_utils")))]
@@ -709,6 +721,8 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
709721
/// expose them to users via a PaymentReceived event. HTLCs which do not meet the requirements
710722
/// here are failed when we process them as pending-forwardable-HTLCs, and entries are removed
711723
/// after we generate a PaymentReceived upon receipt of all MPP parts or when they time out.
724+
///
725+
/// See `ChannelManager` struct-level documentation for lock order requirements.
712726
pending_inbound_payments: Mutex<HashMap<PaymentHash, PendingInboundPayment>>,
713727

714728
/// The session_priv bytes and retry metadata of outbound payments which are pending resolution.

0 commit comments

Comments
 (0)