From: Viktor Tigerström <11711198+ViktorTigerstrom@users.noreply.github.com> Date: Mon, 15 Aug 2022 21:32:38 +0000 (+0200) Subject: Add lock order docs to `ChannelManager` fields X-Git-Tag: v0.0.112~31^2 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=fc12d4385611fc034dc1998fd2548202d751c725;p=rust-lightning Add lock order docs to `ChannelManager` fields --- diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 85602f3bb..72f1f4d59 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -667,6 +667,38 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, M, T, F, L> = ChannelManage /// essentially you should default to using a SimpleRefChannelManager, and use a /// SimpleArcChannelManager when you require a ChannelManager with a static lifetime, such as when /// you're using lightning-net-tokio. +// +// Lock order: +// The tree structure below illustrates the lock order requirements for the different locks of the +// `ChannelManager`. Locks can be held at the same time if they are on the same branch in the tree, +// and should then be taken in the order of the lowest to the highest level in the tree. +// Note that locks on different branches shall not be taken at the same time, as doing so will +// create a new lock order for those specific locks in the order they were taken. +// +// Lock order tree: +// +// `total_consistency_lock` +// | +// |__`forward_htlcs` +// | +// |__`channel_state` +// | | +// | |__`id_to_peer` +// | | +// | |__`per_peer_state` +// | | +// | |__`outbound_scid_aliases` +// | | +// | |__`pending_inbound_payments` +// | | +// | |__`pending_outbound_payments` +// | | +// | |__`best_block` +// | | +// | |__`pending_events` +// | | +// | |__`pending_background_events` +// pub struct ChannelManager where M::Target: chain::Watch, T::Target: BroadcasterInterface, @@ -680,12 +712,14 @@ pub struct ChannelManager, #[cfg(not(test))] best_block: RwLock, secp_ctx: Secp256k1, + /// See `ChannelManager` struct-level documentation for lock order requirements. #[cfg(any(test, feature = "_test_utils"))] pub(super) channel_state: Mutex>, #[cfg(not(any(test, feature = "_test_utils")))] @@ -695,7 +729,8 @@ pub struct ChannelManager>, /// The session_priv bytes and retry metadata of outbound payments which are pending resolution. @@ -709,7 +744,7 @@ pub struct ChannelManager>, /// SCID/SCID Alias -> forward infos. Key of 0 means payments received. @@ -720,6 +755,8 @@ pub struct ChannelManager>>, #[cfg(not(test))] @@ -729,6 +766,8 @@ pub struct ChannelManager>, /// `channel_id` -> `counterparty_node_id`. @@ -748,6 +787,8 @@ pub struct ChannelManager>, our_network_key: SecretKey, @@ -779,10 +820,12 @@ pub struct ChannelManager>>, + /// See `ChannelManager` struct-level documentation for lock order requirements. pending_events: Mutex>, + /// See `ChannelManager` struct-level documentation for lock order requirements. pending_background_events: Mutex>, /// Used when we have to take a BIG lock to make sure everything is self-consistent. /// Essentially just when we're serializing ourselves out.