Skip to content
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

Block the mon update removing a preimage until upstream mon writes #2169

Merged

Conversation

TheBlueMatt
Copy link
Collaborator

@TheBlueMatt TheBlueMatt commented Apr 7, 2023

When we forward a payment and receive an update_fulfill_htlc
message from the downstream channel, we immediately claim the HTLC
on the upstream channel, before even doing a commitment_signed
dance on the downstream channel. This implies that our
ChannelMonitorUpdates "go out" in the right order - first we
ensure we'll get our money by writing the preimage down, then we
write the update that resolves giving money on the downstream node.

This is safe as long as ChannelMonitorUpdates complete in the
order in which they are generated, but of course looking forward we
want to support asynchronous updates, which may complete in any
order.

Thus, here, we enforce the correct ordering by blocking the
downstream ChannelMonitorUpdate until the upstream one completes.
Like the PaymentSent event handling we do so only for the
revoke_and_ack ChannelMonitorUpdate, ensuring the
preimage-containing upstream update has a full RTT to complete
before we actually manage to slow anything down.

This completes the work started in #2167 and depends on (and older version of) #2111 and #2112. This completes much of the work required for fully async ChannelMonitorUpdates. It introduces #2168, though I may try to fix that prior to marking ready, but its not super critical as even after this PR we do not "officially support" async ChannelMonitorUpdates.

@codecov-commenter
Copy link

codecov-commenter commented Apr 7, 2023

Codecov Report

Patch coverage: 98.24% and project coverage change: +1.77% 🎉

Comparison is base (8de8861) 90.45% compared to head (34c20b0) 92.23%.
Report is 4 commits behind head on main.

❗ Current head 34c20b0 differs from pull request most recent head 5d9c602. Consider uploading reports for the commit 5d9c602 to get more accurate results

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2169      +/-   ##
==========================================
+ Coverage   90.45%   92.23%   +1.77%     
==========================================
  Files         112      111       -1     
  Lines       58566    69661   +11095     
  Branches    58566    69661   +11095     
==========================================
+ Hits        52977    64253   +11276     
+ Misses       5589     5408     -181     
Files Changed Coverage Δ
lightning/src/util/test_utils.rs 73.61% <ø> (+4.90%) ⬆️
lightning/src/ln/functional_test_utils.rs 91.43% <93.33%> (+2.52%) ⬆️
lightning/src/ln/channelmanager.rs 91.85% <97.91%> (+5.14%) ⬆️
lightning/src/ln/chanmon_update_fail_tests.rs 98.76% <99.14%> (+0.09%) ⬆️
lightning/src/chain/channelmonitor.rs 94.59% <100.00%> (-0.06%) ⬇️
lightning/src/ln/chan_utils.rs 95.03% <100.00%> (ø)
lightning/src/ln/functional_tests.rs 99.19% <100.00%> (+1.09%) ⬆️
lightning/src/ln/payment_tests.rs 98.64% <100.00%> (+1.18%) ⬆️
lightning/src/ln/shutdown_tests.rs 99.50% <100.00%> (+0.97%) ⬆️

... and 36 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@TheBlueMatt TheBlueMatt force-pushed the 2023-03-monitor-e-monitor branch 3 times, most recently from 70684b1 to f17550b Compare May 4, 2023 23:14
@TheBlueMatt TheBlueMatt added this to In Progress in Async Support via automation May 16, 2023
@valentinewallace
Copy link
Contributor

Could you fix the build? thanks!

@wpaulino wpaulino self-requested a review May 24, 2023 17:52
@TheBlueMatt TheBlueMatt force-pushed the 2023-03-monitor-e-monitor branch 2 times, most recently from b30d299 to a1517aa Compare June 20, 2023 02:24
@TheBlueMatt
Copy link
Collaborator Author

Rebased on #2362

@TheBlueMatt
Copy link
Collaborator Author

Rebased.

Copy link
Contributor

@jkczyz jkczyz left a comment

Choose a reason for hiding this comment

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

Feel free to squash, IMO.

@dunxen
Copy link
Contributor

dunxen commented Sep 11, 2023

Generally LGTM. Will have another look after squash

@TheBlueMatt
Copy link
Collaborator Author

Squashed without further changes.

@wpaulino wpaulino removed their request for review September 11, 2023 19:15
When we need to rebroadcast a `commitment_signed` on reconnect in
response to a previous update (ie not one which contains any
updates) we previously hacked in support for it by passing a `-1`
for the number of expected update_add_htlcs. This is a mess, and
with the introduction of `ReconnectArgs` we can now clean it up
easily with a new bool.
@TheBlueMatt
Copy link
Collaborator Author

Rebased without changes.

Copy link
Contributor

@jkczyz jkczyz left a comment

Choose a reason for hiding this comment

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

Generally looks good outside some clarification in the test comments.

lightning/src/ln/chanmon_update_fail_tests.rs Outdated Show resolved Hide resolved
lightning/src/ln/chanmon_update_fail_tests.rs Outdated Show resolved Hide resolved
@jkczyz
Copy link
Contributor

jkczyz commented Sep 12, 2023

LGTM

When we forward a payment and receive an `update_fulfill_htlc`
message from the downstream channel, we immediately claim the HTLC
on the upstream channel, before even doing a `commitment_signed`
dance on the downstream channel. This implies that our
`ChannelMonitorUpdate`s "go out" in the right order - first we
ensure we'll get our money by writing the preimage down, then we
write the update that resolves giving money on the downstream node.

This is safe as long as `ChannelMonitorUpdate`s complete in the
order in which they are generated, but of course looking forward we
want to support asynchronous updates, which may complete in any
order.

Thus, here, we enforce the correct ordering by blocking the
downstream `ChannelMonitorUpdate` until the upstream one completes.
Like the `PaymentSent` event handling we do so only for the
`revoke_and_ack` `ChannelMonitorUpdate`, ensuring the
preimage-containing upstream update has a full RTT to complete
before we actually manage to slow anything down.
`expect_payment_forwarded` takes a bool to indicate that the
inbound channel on which we received a forwarded payment has been
closed, but then ignores it in favor of looking at the fee in the
event. While this is generally correct, in cases where we process
an event after a channel was closed, which was generated before a
channel closed this is incorrect.

Instead, we examine the bool we already passed and use that.
Because some of these tests require connecting blocks without
calling `get_and_clear_pending_msg_events`, we need to split up
the block connection utilities to only optionally call
sanity-checks.
Also allowing us to pass the event manually.
This adds a test for monitor update actions being completed on
startup if a monitor update completed "while we were shut down"
(or, really, the manager didn't get persisted after the update
completed).
@TheBlueMatt
Copy link
Collaborator Author

Squashed without further changes.

@TheBlueMatt TheBlueMatt merged commit 3dcdb14 into lightningdevkit:main Sep 12, 2023
11 of 14 checks passed
Async Support automation moved this from In Progress to Done Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

None yet

5 participants