Skip to content

Use interior mutability in ChannelMonitor #813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

jkczyz
Copy link
Contributor

@jkczyz jkczyz commented Mar 1, 2021

ChainMonitor holds a map of ChannelMonitors guarded by a single Mutex. This prevents parallelizing update_channel operations. This PR moves the Mutex inside ChannelMonitor such that it uses interior mutability, making its interface in terms of &self rather than &mut self. Thus, ChainMonitor can use a RwLock for its map instead of a Mutex, allowing for parallel operations other than when updating the map in watch_channel.

Additionally, since ChannelMonitor now uses interior mutability, it can implement chain::Listen without using a RefCell.

Resolves #805

@jkczyz jkczyz requested a review from TheBlueMatt March 1, 2021 07:08
@jkczyz
Copy link
Contributor Author

jkczyz commented Mar 1, 2021

@TheBlueMatt Not sure if this is what you had in mind with moving Mutex inside ChannelMonitor. It was definitely the simplest way of accomplishing this, but it means multiple operations on a ChannelMonitor require obtaining the lock more than once. Additionally, get_funding_txo and get_outputs_to_watch need to clone their output since a reference from the locked struct cannot be returned.

@codecov
Copy link

codecov bot commented Mar 1, 2021

Codecov Report

Merging #813 (e8ea0d9) into main (9fba7c9) will decrease coverage by 0.05%.
The diff coverage is 96.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #813      +/-   ##
==========================================
- Coverage   91.06%   91.01%   -0.06%     
==========================================
  Files          48       48              
  Lines       25495    25562      +67     
==========================================
+ Hits        23218    23266      +48     
- Misses       2277     2296      +19     
Impacted Files Coverage Δ
lightning-block-sync/src/init.rs 93.56% <ø> (ø)
lightning/src/util/macro_logger.rs 88.33% <ø> (ø)
lightning/src/chain/channelmonitor.rs 95.52% <94.61%> (-0.24%) ⬇️
lightning/src/chain/chainmonitor.rs 94.20% <100.00%> (+0.08%) ⬆️
lightning/src/ln/chanmon_update_fail_tests.rs 97.56% <100.00%> (ø)
lightning/src/ln/channel.rs 87.83% <100.00%> (ø)
lightning/src/ln/functional_test_utils.rs 94.97% <100.00%> (ø)
lightning/src/ln/functional_tests.rs 96.92% <100.00%> (-0.23%) ⬇️
lightning/src/util/test_utils.rs 83.49% <100.00%> (ø)
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9fba7c9...e8ea0d9. Read the comment docs.

@TheBlueMatt TheBlueMatt added this to the 0.0.13 milestone Mar 1, 2021
@TheBlueMatt
Copy link
Collaborator

Concept ACK.

@valentinewallace
Copy link
Contributor

Concept ACK :)

@jkczyz jkczyz force-pushed the 2021-02-channel-monitor-mutex branch from 2d42d4d to 7aa17e6 Compare March 1, 2021 20:43
@jkczyz jkczyz marked this pull request as ready for review March 1, 2021 20:43
///
/// panics if the given update is not the next update by update_id.
pub fn update_monitor<B: Deref, F: Deref, L: Deref>(
&self,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I find rustfmt-style unwrapping like this rather hard to read, its something that my head thinks shouldn't be vertical but is (and I have to check it against the use which is horizontal), and suddenly I can't see as much context.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was resistant to this for awhile until dealing with extremely long signatures in lightning-block-sync. I think using this format for long signatures is a good convention because it makes the parameters easy to scan and provides a consistent rule for formatting.

Otherwise, parameters can be easily missed if they are bin packed. It may seem a little odd at first, but it doesn't take long to adjust to reading this format. And it has the advantage of clearly indicating a parameter's type. This otherwise becomes especially difficult for more elaborate types like tuples or those with more than one type parameter, as the additional commas make the parameter list harder to scan.

That said, I do prefer to bin pack arguments at the call sites as I've done throughout in cases where the line exceeds the 100-character limit. Here, there are no types so any advantage of breaking each argument across lines is lost. I think this is a good middle ground as the additional vertical spacing is limited to signatures.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is somewhere in between, indeed, and I see the value of it, but I'd argue that more things are missed in review due to missing context than scan errors. Sometimes just a few lines is needed, but sometimes even the next function up is important (or being able to fit the full parameter list while looking at a function). Sadly, different people have different terminal heights so there's not an easy fix.

}
}

#[cfg(test)]
Copy link
Contributor

@valentinewallace valentinewallace Mar 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, why are these functions test-only now?

Edit: oh, I guess that does make sense. In that case, maybe we could make the inner's corresponding methods private, instead of pub(crate)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good point. Done!

Copy link
Contributor

@valentinewallace valentinewallace left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes the public API so much nicer

jkczyz added 4 commits March 1, 2021 22:12
ChainMonitor accesses a set of ChannelMonitors behind a single Mutex.
As a result, update_channel operations cannot be parallelized. It also
requires using a RefCell around a ChannelMonitor when implementing
chain::Listen.

Moving the Mutex into ChannelMonitor avoids these problems and aligns it
better with other interfaces. Note, however, that get_funding_txo and
get_outputs_to_watch now clone the underlying data rather than returning
references.
Now that ChannelMonitor uses an internal Mutex to support interior
mutability, ChainMonitor can use a RwLock to manage its ChannelMonitor
map. This allows parallelization of update_channel operations since an
exclusive lock only needs to be held when adding to the map in
watch_channel.
The implementation of chain::Listen for ChannelMonitor required using a
RefCell since its block_connected method required a mutable borrow. This
is no longer the case since ChannelMonitor now uses interior mutability
via a Mutex. So the RefCell is no longer needed.
@jkczyz jkczyz force-pushed the 2021-02-channel-monitor-mutex branch from 7aa17e6 to e8ea0d9 Compare March 2, 2021 06:16
@TheBlueMatt TheBlueMatt merged commit e241ca4 into lightningdevkit:main Mar 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add inner mutexes to ChannelMonitor
3 participants