Skip to content

Do not broadcast commitment txn on Permanent mon update failure - #1106

Merged
TheBlueMatt merged 7 commits into
lightningdevkit:mainfrom
TheBlueMatt:2021-10-no-perm-err-broadcast
Sep 29, 2022
Merged

Do not broadcast commitment txn on Permanent mon update failure#1106
TheBlueMatt merged 7 commits into
lightningdevkit:mainfrom
TheBlueMatt:2021-10-no-perm-err-broadcast

Conversation

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

See doc updates for more info on the edge case this prevents, and
there isn't really a strong reason why we would need to broadcast
the latest state immediately. Specifically, in the case of HTLC
claims (the most important reason to ensure we have state on chain
if it cannot be persisted), we will still force-close if there are
HTLCs which need claiming and are going to expire.

Surprisingly, there were no tests which failed as a result of this
change, but a new one has been added.

@codecov

codecovBot commented Oct 5, 2021

Copy link
Copy Markdown

Codecov Report

Merging #1106 (5f848de) into main (107c6c7) will increase coverage by 1.71%.
The diff coverage is 90.00%.

❗ Current head 5f848de differs from pull request most recent head f99da12. Consider uploading reports for the commit f99da12 to get more accurate results
Impacted file tree graph

@@ Coverage Diff @@## main #1106 +/- ##
==========================================
+ Coverage 90.40% 92.12% +1.71% 
==========================================
Files 68 66 -2 Lines 34796 40933 +6137 ==========================================
+ Hits 31458 37708 +6250 + Misses 3338 3225 -113 
Impacted FilesCoverage Δ
lightning/src/ln/channelmanager.rs89.20% <50.00%> (+5.68%)⬆️
lightning/src/chain/channelmonitor.rs91.28% <100.00%> (+0.33%)⬆️
lightning/src/ln/chanmon_update_fail_tests.rs98.79% <100.00%> (+1.14%)⬆️
lightning/src/chain/mod.rs58.82% <0.00%> (-2.29%)⬇️
lightning-background-processor/src/lib.rs93.75% <0.00%> (-0.49%)⬇️
lightning/src/chain/onchaintx.rs94.27% <0.00%> (-0.48%)⬇️
lightning/src/ln/onion_utils.rs94.91% <0.00%> (-0.46%)⬇️
lightning-invoice/src/utils.rs84.09% <0.00%> (-0.18%)⬇️
lightning-persister/src/lib.rs94.21% <0.00%> (-0.10%)⬇️
lightning/src/ln/payment_tests.rs98.75% <0.00%> (-0.09%)⬇️
... and 23 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 107c6c7...f99da12. Read the comment docs.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from bd79c03 to e6966ceCompareOctober 5, 2021 06:02
Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment on lines +150 to +151
/// or, e.g. we've moved on to a different watchtower and cannot update with all watchtowers
/// that were previously informed of this channel).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Trying to parse this watchtower case -- in this situation, we're switching watchtowers, but have no way of contacting the old watchtower to just delete our data, therefore the channel needs to close via PermanentFailure?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Yea, but honestly its confusing, I replaced it with a better example.

Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment on lines +167 to +168
/// [`ChannelMonitor::get_latest_holder_commitment_txn`] once you've safely ensured no further
/// off-chain updates to the channel can occur.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Checking -- "ensure no further off-chain updates can occur" == "applied the final monitor update mentioned on L170"? Or?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

It means "no ChannelManager still knows about this channel", I reworded it.

Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/chain/channelmonitor.rs Outdated
@valentinewallace

Copy link
Copy Markdown
Contributor

Bit confused how to implement these new requirements in the sample. I guess we now need a proxy layer implementing Persist between the FilesystemPersister and ChainMonitor?

@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Bit confused how to implement these new requirements in the sample.

I think we, in general, need to think hard about how we handle disk failures in the sample/lightning-persister. If the disk gets unplugged while we're running, we should just shut down and move on, but there's no way to get an error out of the Persister. The way the code works today is unsafe in this condition - if the disk gets unplugged, we'll broadcast the latest state and keep going, if the user then plugs the disk back in and restarts we may revoke the now-broadcasted state.

I think the end result for the sample needs to be a "really definitely broadcast the latest state from the channelmonitor for channel X" command, which is marked unsafe. We should probably also have a programatic way to detect monitors in this condition and not just rely on the logs in ChannelManager deserialization, I'll work on that bit here.

@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Rebased on upstream. Need to make sure all the updated content in the failure enum docs is still kept and should rename temporaryfailure -> asyncpersist or so.

@TheBlueMatt
TheBlueMatt marked this pull request as draft October 20, 2021 01:34
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 6ad8583 to f99da12CompareOctober 20, 2021 02:55
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from f99da12 to 2572ac5CompareJuly 17, 2022 20:11
@codecov-commenter

codecov-commenter commented Jul 17, 2022

Copy link
Copy Markdown

Codecov Report

Base: 90.78% // Head: 91.21% // Increases project coverage by +0.43% 🎉

Coverage data is based on head (74745cb) compared to base (48d21ba).
Patch coverage: 89.09% of modified lines in pull request are covered.

❗ Current head 74745cb differs from pull request most recent head 52934e0. Consider uploading reports for the commit 52934e0 to get more accurate results

Additional details and impacted files
@@ Coverage Diff @@## main #1106 +/- ##
==========================================
+ Coverage 90.78% 91.21% +0.43% 
==========================================
Files 86 87 +1 Lines 46631 50794 +4163 Branches 46631 50794 +4163 ==========================================
+ Hits 42335 46333 +3998 - Misses 4296 4461 +165 
Impacted FilesCoverage Δ
lightning/src/chain/mod.rs68.18% <ø> (ø)
lightning/src/util/errors.rs72.22% <0.00%> (ø)
lightning/src/chain/channelmonitor.rs91.21% <75.00%> (+<0.01%)⬆️
lightning/src/ln/channelmanager.rs88.11% <83.23%> (+3.05%)⬆️
lightning-invoice/src/payment.rs93.13% <87.50%> (+2.26%)⬆️
lightning/src/util/persist.rs95.23% <87.50%> (+0.50%)⬆️
lightning/src/chain/chainmonitor.rs97.78% <88.46%> (-0.28%)⬇️
lightning-persister/src/lib.rs93.45% <100.00%> (ø)
lightning/src/ln/chanmon_update_fail_tests.rs97.72% <100.00%> (+<0.01%)⬆️
lightning/src/ln/channel.rs88.66% <100.00%> (ø)
... and 17 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 2572ac5 to 0fe1f28CompareJuly 18, 2022 01:59
@TheBlueMatt
TheBlueMatt marked this pull request as ready for review July 18, 2022 01:59
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Finally getting back to the monitor work, finally updated this to include a monitor update failure type rework, but no further functional changes.

Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/chain/chainmonitor.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/chanmon_update_fail_tests.rs
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from dc2888a to a9f656aCompareAugust 16, 2022 22:32
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Oops, somehow I totally lost track of this PR having a pending review, so sorry about that! Rebased on latest git and addressed feedback.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from a9f656a to e3347f1CompareAugust 16, 2022 22:39
See doc updates for more info on the edge case this prevents, and
there isn't really a strong reason why we would need to broadcast
the latest state immediately. Specifically, in the case of HTLC
claims (the most important reason to ensure we have state on chain
if it cannot be persisted), we will still force-close if there are
HTLCs which need claiming and are going to expire.
Surprisingly, there were no tests which failed as a result of this
change, but a new one has been added.
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 86a5a28 to 24f8279CompareSeptember 15, 2022 19:38
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Rebased to address conflicts and went ahead and squashed as its been a while.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 24f8279 to 4825a96CompareSeptember 15, 2022 19:52
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
// a monitor that we didn't manage to store (and that we don't care about - we
// don't respond with the funding_signed so the channel can never go on chain).
let (_monitor_update, failed_htlcs) = chan.force_shutdown(true);
let (_monitor_update, failed_htlcs) = chan.force_shutdown(false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like there is not test for this scenario. All tests pass when reverting to true.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Its unobservable - note that the monitor_update, which is the place the bool is propagated to, is drop'd.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 4825a96 to c0bb4f4CompareSeptember 25, 2022 07:50
Comment threadlightning/src/util/errors.rs Outdated
Comment on lines 49 to 50
/// An attempt to call watch/update_channel returned an Err (ie you did this!), causing the
/// attempted action to fail.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you update the docs since it's no longer from an Err? Would be good to link to the corresponding method docs and maybe phrase the docs to be a little clearer, too.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1, also a bit weird that it's still considered an APIError variant, but that might require some more brainstorming than a simple rename/doc update.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Yea, did update the docs, but agree - this shouldnt really be an error at all, it should just be Ok. I think there's only one(-ish) place where it Errs - sending payments - so it should be easy to do in a followup.

Comment on lines +223 to +228
/// When this is returned, [`ChannelManager`] will force-close the channel but *not* broadcast
/// our current commitment transaction. This avoids a dangerous case where a local disk failure
/// (e.g. the Linux-default remounting of the disk as read-only) causes [`PermanentFailure`]s
/// for all monitor updates. If we were to broadcast our latest commitment transaction and then
/// restart, we could end up reading a previous [`ChannelMonitor`] and [`ChannelManager`],
/// revoking our now-broadcasted state before seeing it confirm and losing all our funds.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

IIUC, while the user could choose not to broadcast the latest commitment transaction, which has been revoked, it could still be done by LDK if it contained any pending HTLCs that are expiring soon.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Indeed, somewhat of a separate issue that should be addressed in #1593

Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/util/errors.rs Outdated
Comment on lines 49 to 50
/// An attempt to call watch/update_channel returned an Err (ie you did this!), causing the
/// attempted action to fail.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1, also a bit weird that it's still considered an APIError variant, but that might require some more brainstorming than a simple rename/doc update.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from c0bb4f4 to 74745cbCompareSeptember 28, 2022 15:14

@wpaulinowpaulino left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, feel free to squash.

Comment threadlightning/src/ln/channel.rs Outdated
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 74745cb to fc18acbCompareSeptember 28, 2022 21:11

@jkczyzjkczyz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, please squash.

Comment threadlightning/src/util/errors.rs Outdated
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from fc18acb to 4ac5804CompareSeptember 29, 2022 09:02
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Addressed one more comment and squashed. Diff since last push:

$ git diff-tree -U1 fc18acb4 4ac58048
diff --git a/lightning/src/util/errors.rs b/lightning/src/util/errors.rs
index 83324eab6..ad6993542 100644
--- a/lightning/src/util/errors.rs+++ b/lightning/src/util/errors.rs@@ -48,7 +48,9 @@ pub enum APIError {
},
-	/// An attempt to call watch/update_channel returned a-	/// [`ChannelMonitorUpdateStatus::InProgress`] indicating the persistence of a monitor update-	/// is awaiting async resolution. Once it resolves the attempted action should complete-	/// automatically.+	/// An attempt to call [`chain::Watch::watch_channel`]/[`chain::Watch::update_channel`]+	/// returned a [`ChannelMonitorUpdateStatus::InProgress`] indicating the persistence of a+	/// monitor update is awaiting async resolution. Once it resolves the attempted action should+	/// complete automatically.
///
+	/// [`chain::Watch::watch_channel`]: crate::chain::Watch::watch_channel+	/// [`chain::Watch::update_channel`]: crate::chain::Watch::update_channel
/// [`ChannelMonitorUpdateStatus::InProgress`]: crate::chain::ChannelMonitorUpdateStatus::InProgress

Comment threadlightning/src/util/errors.rs
When a `chain::Watch` `ChannelMonitor` update method is called, the
user has three options:
(a) persist the monitor update immediately and return success,
(b) fail to persist the monitor update immediately and return
failure,
(c) return a flag indicating the monitor update is in progress and
will complete in the future.
(c) is rather harmless, and in some deployments should be expected
to be the return value for all monitor update calls, but currently
requires returning `Err(ChannelMonitorUpdateErr::TemporaryFailure)`
which isn't very descriptive and sounds scarier than it is.
Instead, here, we change the return type used to be a single enum
(rather than a Result) and rename `TemporaryFailure`
`UpdateInProgress`.
If we receive a monitor event from a forwarded-to channel which
contains a preimage for an HTLC, we have to propogate that preimage
back to the forwarded-from channel monitor. However, once we have
that update, we're running in a relatively unsafe state - we have
the preimage in memory, but if we were to crash the forwarded-to
channel monitor will not regenerate the update with the preimage
for us. If we haven't managed to write the monitor update to the
forwarded-from channel by that point, we've lost the preimage, and,
thus, money!
This much more accurately represents the error, indicating that a
monitor update is in progress asynchronously and may complete at a
later time.
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 4ac5804 to 52934e0CompareSeptember 29, 2022 20:28
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Oops, sorry, reordered the missing hunk into the right commit, no ultimate diff, though.

@TheBlueMatt
TheBlueMatt merged commit 7544030 into lightningdevkit:mainSep 29, 2022
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

7 participants

@TheBlueMatt@valentinewallace@codecov-commenter@G8XSU@jkczyz@wpaulino@tnull
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Do not broadcast commitment txn on Permanent mon update failure by TheBlueMatt · Pull Request #1106 · lightningdevkit/rust-lightning · GitHub
Skip to content

Do not broadcast commitment txn on Permanent mon update failure - #1106

Merged
TheBlueMatt merged 7 commits into
lightningdevkit:mainfrom
TheBlueMatt:2021-10-no-perm-err-broadcast
Sep 29, 2022
Merged

Do not broadcast commitment txn on Permanent mon update failure#1106
TheBlueMatt merged 7 commits into
lightningdevkit:mainfrom
TheBlueMatt:2021-10-no-perm-err-broadcast

Conversation

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

See doc updates for more info on the edge case this prevents, and
there isn't really a strong reason why we would need to broadcast
the latest state immediately. Specifically, in the case of HTLC
claims (the most important reason to ensure we have state on chain
if it cannot be persisted), we will still force-close if there are
HTLCs which need claiming and are going to expire.

Surprisingly, there were no tests which failed as a result of this
change, but a new one has been added.

@codecov

codecovBot commented Oct 5, 2021

Copy link
Copy Markdown

Codecov Report

Merging #1106 (5f848de) into main (107c6c7) will increase coverage by 1.71%.
The diff coverage is 90.00%.

❗ Current head 5f848de differs from pull request most recent head f99da12. Consider uploading reports for the commit f99da12 to get more accurate results
Impacted file tree graph

@@ Coverage Diff @@## main #1106 +/- ##
==========================================
+ Coverage 90.40% 92.12% +1.71% 
==========================================
Files 68 66 -2 Lines 34796 40933 +6137 ==========================================
+ Hits 31458 37708 +6250 + Misses 3338 3225 -113 
Impacted FilesCoverage Δ
lightning/src/ln/channelmanager.rs89.20% <50.00%> (+5.68%)⬆️
lightning/src/chain/channelmonitor.rs91.28% <100.00%> (+0.33%)⬆️
lightning/src/ln/chanmon_update_fail_tests.rs98.79% <100.00%> (+1.14%)⬆️
lightning/src/chain/mod.rs58.82% <0.00%> (-2.29%)⬇️
lightning-background-processor/src/lib.rs93.75% <0.00%> (-0.49%)⬇️
lightning/src/chain/onchaintx.rs94.27% <0.00%> (-0.48%)⬇️
lightning/src/ln/onion_utils.rs94.91% <0.00%> (-0.46%)⬇️
lightning-invoice/src/utils.rs84.09% <0.00%> (-0.18%)⬇️
lightning-persister/src/lib.rs94.21% <0.00%> (-0.10%)⬇️
lightning/src/ln/payment_tests.rs98.75% <0.00%> (-0.09%)⬇️
... and 23 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 107c6c7...f99da12. Read the comment docs.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from bd79c03 to e6966ceCompareOctober 5, 2021 06:02
Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment on lines +150 to +151
/// or, e.g. we've moved on to a different watchtower and cannot update with all watchtowers
/// that were previously informed of this channel).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Trying to parse this watchtower case -- in this situation, we're switching watchtowers, but have no way of contacting the old watchtower to just delete our data, therefore the channel needs to close via PermanentFailure?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Yea, but honestly its confusing, I replaced it with a better example.

Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment on lines +167 to +168
/// [`ChannelMonitor::get_latest_holder_commitment_txn`] once you've safely ensured no further
/// off-chain updates to the channel can occur.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Checking -- "ensure no further off-chain updates can occur" == "applied the final monitor update mentioned on L170"? Or?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

It means "no ChannelManager still knows about this channel", I reworded it.

Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/chain/channelmonitor.rs Outdated
@valentinewallace

Copy link
Copy Markdown
Contributor

Bit confused how to implement these new requirements in the sample. I guess we now need a proxy layer implementing Persist between the FilesystemPersister and ChainMonitor?

@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Bit confused how to implement these new requirements in the sample.

I think we, in general, need to think hard about how we handle disk failures in the sample/lightning-persister. If the disk gets unplugged while we're running, we should just shut down and move on, but there's no way to get an error out of the Persister. The way the code works today is unsafe in this condition - if the disk gets unplugged, we'll broadcast the latest state and keep going, if the user then plugs the disk back in and restarts we may revoke the now-broadcasted state.

I think the end result for the sample needs to be a "really definitely broadcast the latest state from the channelmonitor for channel X" command, which is marked unsafe. We should probably also have a programatic way to detect monitors in this condition and not just rely on the logs in ChannelManager deserialization, I'll work on that bit here.

@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Rebased on upstream. Need to make sure all the updated content in the failure enum docs is still kept and should rename temporaryfailure -> asyncpersist or so.

@TheBlueMatt
TheBlueMatt marked this pull request as draft October 20, 2021 01:34
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 6ad8583 to f99da12CompareOctober 20, 2021 02:55
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from f99da12 to 2572ac5CompareJuly 17, 2022 20:11
@codecov-commenter

codecov-commenter commented Jul 17, 2022

Copy link
Copy Markdown

Codecov Report

Base: 90.78% // Head: 91.21% // Increases project coverage by +0.43% 🎉

Coverage data is based on head (74745cb) compared to base (48d21ba).
Patch coverage: 89.09% of modified lines in pull request are covered.

❗ Current head 74745cb differs from pull request most recent head 52934e0. Consider uploading reports for the commit 52934e0 to get more accurate results

Additional details and impacted files
@@ Coverage Diff @@## main #1106 +/- ##
==========================================
+ Coverage 90.78% 91.21% +0.43% 
==========================================
Files 86 87 +1 Lines 46631 50794 +4163 Branches 46631 50794 +4163 ==========================================
+ Hits 42335 46333 +3998 - Misses 4296 4461 +165 
Impacted FilesCoverage Δ
lightning/src/chain/mod.rs68.18% <ø> (ø)
lightning/src/util/errors.rs72.22% <0.00%> (ø)
lightning/src/chain/channelmonitor.rs91.21% <75.00%> (+<0.01%)⬆️
lightning/src/ln/channelmanager.rs88.11% <83.23%> (+3.05%)⬆️
lightning-invoice/src/payment.rs93.13% <87.50%> (+2.26%)⬆️
lightning/src/util/persist.rs95.23% <87.50%> (+0.50%)⬆️
lightning/src/chain/chainmonitor.rs97.78% <88.46%> (-0.28%)⬇️
lightning-persister/src/lib.rs93.45% <100.00%> (ø)
lightning/src/ln/chanmon_update_fail_tests.rs97.72% <100.00%> (+<0.01%)⬆️
lightning/src/ln/channel.rs88.66% <100.00%> (ø)
... and 17 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 2572ac5 to 0fe1f28CompareJuly 18, 2022 01:59
@TheBlueMatt
TheBlueMatt marked this pull request as ready for review July 18, 2022 01:59
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Finally getting back to the monitor work, finally updated this to include a monitor update failure type rework, but no further functional changes.

Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/chain/chainmonitor.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/chanmon_update_fail_tests.rs
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from dc2888a to a9f656aCompareAugust 16, 2022 22:32
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Oops, somehow I totally lost track of this PR having a pending review, so sorry about that! Rebased on latest git and addressed feedback.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from a9f656a to e3347f1CompareAugust 16, 2022 22:39
See doc updates for more info on the edge case this prevents, and
there isn't really a strong reason why we would need to broadcast
the latest state immediately. Specifically, in the case of HTLC
claims (the most important reason to ensure we have state on chain
if it cannot be persisted), we will still force-close if there are
HTLCs which need claiming and are going to expire.
Surprisingly, there were no tests which failed as a result of this
change, but a new one has been added.
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 86a5a28 to 24f8279CompareSeptember 15, 2022 19:38
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Rebased to address conflicts and went ahead and squashed as its been a while.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 24f8279 to 4825a96CompareSeptember 15, 2022 19:52
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
// a monitor that we didn't manage to store (and that we don't care about - we
// don't respond with the funding_signed so the channel can never go on chain).
let (_monitor_update, failed_htlcs) = chan.force_shutdown(true);
let (_monitor_update, failed_htlcs) = chan.force_shutdown(false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like there is not test for this scenario. All tests pass when reverting to true.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Its unobservable - note that the monitor_update, which is the place the bool is propagated to, is drop'd.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 4825a96 to c0bb4f4CompareSeptember 25, 2022 07:50
Comment threadlightning/src/util/errors.rs Outdated
Comment on lines 49 to 50
/// An attempt to call watch/update_channel returned an Err (ie you did this!), causing the
/// attempted action to fail.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you update the docs since it's no longer from an Err? Would be good to link to the corresponding method docs and maybe phrase the docs to be a little clearer, too.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1, also a bit weird that it's still considered an APIError variant, but that might require some more brainstorming than a simple rename/doc update.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Yea, did update the docs, but agree - this shouldnt really be an error at all, it should just be Ok. I think there's only one(-ish) place where it Errs - sending payments - so it should be easy to do in a followup.

Comment on lines +223 to +228
/// When this is returned, [`ChannelManager`] will force-close the channel but *not* broadcast
/// our current commitment transaction. This avoids a dangerous case where a local disk failure
/// (e.g. the Linux-default remounting of the disk as read-only) causes [`PermanentFailure`]s
/// for all monitor updates. If we were to broadcast our latest commitment transaction and then
/// restart, we could end up reading a previous [`ChannelMonitor`] and [`ChannelManager`],
/// revoking our now-broadcasted state before seeing it confirm and losing all our funds.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

IIUC, while the user could choose not to broadcast the latest commitment transaction, which has been revoked, it could still be done by LDK if it contained any pending HTLCs that are expiring soon.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Indeed, somewhat of a separate issue that should be addressed in #1593

Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/util/errors.rs Outdated
Comment on lines 49 to 50
/// An attempt to call watch/update_channel returned an Err (ie you did this!), causing the
/// attempted action to fail.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1, also a bit weird that it's still considered an APIError variant, but that might require some more brainstorming than a simple rename/doc update.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from c0bb4f4 to 74745cbCompareSeptember 28, 2022 15:14

@wpaulinowpaulino left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, feel free to squash.

Comment threadlightning/src/ln/channel.rs Outdated
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 74745cb to fc18acbCompareSeptember 28, 2022 21:11

@jkczyzjkczyz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, please squash.

Comment threadlightning/src/util/errors.rs Outdated
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from fc18acb to 4ac5804CompareSeptember 29, 2022 09:02
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Addressed one more comment and squashed. Diff since last push:

$ git diff-tree -U1 fc18acb4 4ac58048
diff --git a/lightning/src/util/errors.rs b/lightning/src/util/errors.rs
index 83324eab6..ad6993542 100644
--- a/lightning/src/util/errors.rs+++ b/lightning/src/util/errors.rs@@ -48,7 +48,9 @@ pub enum APIError {
},
-	/// An attempt to call watch/update_channel returned a-	/// [`ChannelMonitorUpdateStatus::InProgress`] indicating the persistence of a monitor update-	/// is awaiting async resolution. Once it resolves the attempted action should complete-	/// automatically.+	/// An attempt to call [`chain::Watch::watch_channel`]/[`chain::Watch::update_channel`]+	/// returned a [`ChannelMonitorUpdateStatus::InProgress`] indicating the persistence of a+	/// monitor update is awaiting async resolution. Once it resolves the attempted action should+	/// complete automatically.
///
+	/// [`chain::Watch::watch_channel`]: crate::chain::Watch::watch_channel+	/// [`chain::Watch::update_channel`]: crate::chain::Watch::update_channel
/// [`ChannelMonitorUpdateStatus::InProgress`]: crate::chain::ChannelMonitorUpdateStatus::InProgress

Comment threadlightning/src/util/errors.rs
When a `chain::Watch` `ChannelMonitor` update method is called, the
user has three options:
(a) persist the monitor update immediately and return success,
(b) fail to persist the monitor update immediately and return
failure,
(c) return a flag indicating the monitor update is in progress and
will complete in the future.
(c) is rather harmless, and in some deployments should be expected
to be the return value for all monitor update calls, but currently
requires returning `Err(ChannelMonitorUpdateErr::TemporaryFailure)`
which isn't very descriptive and sounds scarier than it is.
Instead, here, we change the return type used to be a single enum
(rather than a Result) and rename `TemporaryFailure`
`UpdateInProgress`.
If we receive a monitor event from a forwarded-to channel which
contains a preimage for an HTLC, we have to propogate that preimage
back to the forwarded-from channel monitor. However, once we have
that update, we're running in a relatively unsafe state - we have
the preimage in memory, but if we were to crash the forwarded-to
channel monitor will not regenerate the update with the preimage
for us. If we haven't managed to write the monitor update to the
forwarded-from channel by that point, we've lost the preimage, and,
thus, money!
This much more accurately represents the error, indicating that a
monitor update is in progress asynchronously and may complete at a
later time.
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 4ac5804 to 52934e0CompareSeptember 29, 2022 20:28
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Oops, sorry, reordered the missing hunk into the right commit, no ultimate diff, though.

@TheBlueMatt
TheBlueMatt merged commit 7544030 into lightningdevkit:mainSep 29, 2022
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

7 participants

@TheBlueMatt@valentinewallace@codecov-commenter@G8XSU@jkczyz@wpaulino@tnull
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Do not broadcast commitment txn on Permanent mon update failure by TheBlueMatt · Pull Request #1106 · lightningdevkit/rust-lightning · GitHub
Skip to content

Do not broadcast commitment txn on Permanent mon update failure - #1106

Merged
TheBlueMatt merged 7 commits into
lightningdevkit:mainfrom
TheBlueMatt:2021-10-no-perm-err-broadcast
Sep 29, 2022
Merged

Do not broadcast commitment txn on Permanent mon update failure#1106
TheBlueMatt merged 7 commits into
lightningdevkit:mainfrom
TheBlueMatt:2021-10-no-perm-err-broadcast

Conversation

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

See doc updates for more info on the edge case this prevents, and
there isn't really a strong reason why we would need to broadcast
the latest state immediately. Specifically, in the case of HTLC
claims (the most important reason to ensure we have state on chain
if it cannot be persisted), we will still force-close if there are
HTLCs which need claiming and are going to expire.

Surprisingly, there were no tests which failed as a result of this
change, but a new one has been added.

@codecov

codecovBot commented Oct 5, 2021

Copy link
Copy Markdown

Codecov Report

Merging #1106 (5f848de) into main (107c6c7) will increase coverage by 1.71%.
The diff coverage is 90.00%.

❗ Current head 5f848de differs from pull request most recent head f99da12. Consider uploading reports for the commit f99da12 to get more accurate results
Impacted file tree graph

@@ Coverage Diff @@## main #1106 +/- ##
==========================================
+ Coverage 90.40% 92.12% +1.71% 
==========================================
Files 68 66 -2 Lines 34796 40933 +6137 ==========================================
+ Hits 31458 37708 +6250 + Misses 3338 3225 -113 
Impacted FilesCoverage Δ
lightning/src/ln/channelmanager.rs89.20% <50.00%> (+5.68%)⬆️
lightning/src/chain/channelmonitor.rs91.28% <100.00%> (+0.33%)⬆️
lightning/src/ln/chanmon_update_fail_tests.rs98.79% <100.00%> (+1.14%)⬆️
lightning/src/chain/mod.rs58.82% <0.00%> (-2.29%)⬇️
lightning-background-processor/src/lib.rs93.75% <0.00%> (-0.49%)⬇️
lightning/src/chain/onchaintx.rs94.27% <0.00%> (-0.48%)⬇️
lightning/src/ln/onion_utils.rs94.91% <0.00%> (-0.46%)⬇️
lightning-invoice/src/utils.rs84.09% <0.00%> (-0.18%)⬇️
lightning-persister/src/lib.rs94.21% <0.00%> (-0.10%)⬇️
lightning/src/ln/payment_tests.rs98.75% <0.00%> (-0.09%)⬇️
... and 23 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 107c6c7...f99da12. Read the comment docs.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from bd79c03 to e6966ceCompareOctober 5, 2021 06:02
Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment on lines +150 to +151
/// or, e.g. we've moved on to a different watchtower and cannot update with all watchtowers
/// that were previously informed of this channel).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Trying to parse this watchtower case -- in this situation, we're switching watchtowers, but have no way of contacting the old watchtower to just delete our data, therefore the channel needs to close via PermanentFailure?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Yea, but honestly its confusing, I replaced it with a better example.

Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment on lines +167 to +168
/// [`ChannelMonitor::get_latest_holder_commitment_txn`] once you've safely ensured no further
/// off-chain updates to the channel can occur.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Checking -- "ensure no further off-chain updates can occur" == "applied the final monitor update mentioned on L170"? Or?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

It means "no ChannelManager still knows about this channel", I reworded it.

Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/chain/channelmonitor.rs Outdated
@valentinewallace

Copy link
Copy Markdown
Contributor

Bit confused how to implement these new requirements in the sample. I guess we now need a proxy layer implementing Persist between the FilesystemPersister and ChainMonitor?

@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Bit confused how to implement these new requirements in the sample.

I think we, in general, need to think hard about how we handle disk failures in the sample/lightning-persister. If the disk gets unplugged while we're running, we should just shut down and move on, but there's no way to get an error out of the Persister. The way the code works today is unsafe in this condition - if the disk gets unplugged, we'll broadcast the latest state and keep going, if the user then plugs the disk back in and restarts we may revoke the now-broadcasted state.

I think the end result for the sample needs to be a "really definitely broadcast the latest state from the channelmonitor for channel X" command, which is marked unsafe. We should probably also have a programatic way to detect monitors in this condition and not just rely on the logs in ChannelManager deserialization, I'll work on that bit here.

@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Rebased on upstream. Need to make sure all the updated content in the failure enum docs is still kept and should rename temporaryfailure -> asyncpersist or so.

@TheBlueMatt
TheBlueMatt marked this pull request as draft October 20, 2021 01:34
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 6ad8583 to f99da12CompareOctober 20, 2021 02:55
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from f99da12 to 2572ac5CompareJuly 17, 2022 20:11
@codecov-commenter

codecov-commenter commented Jul 17, 2022

Copy link
Copy Markdown

Codecov Report

Base: 90.78% // Head: 91.21% // Increases project coverage by +0.43% 🎉

Coverage data is based on head (74745cb) compared to base (48d21ba).
Patch coverage: 89.09% of modified lines in pull request are covered.

❗ Current head 74745cb differs from pull request most recent head 52934e0. Consider uploading reports for the commit 52934e0 to get more accurate results

Additional details and impacted files
@@ Coverage Diff @@## main #1106 +/- ##
==========================================
+ Coverage 90.78% 91.21% +0.43% 
==========================================
Files 86 87 +1 Lines 46631 50794 +4163 Branches 46631 50794 +4163 ==========================================
+ Hits 42335 46333 +3998 - Misses 4296 4461 +165 
Impacted FilesCoverage Δ
lightning/src/chain/mod.rs68.18% <ø> (ø)
lightning/src/util/errors.rs72.22% <0.00%> (ø)
lightning/src/chain/channelmonitor.rs91.21% <75.00%> (+<0.01%)⬆️
lightning/src/ln/channelmanager.rs88.11% <83.23%> (+3.05%)⬆️
lightning-invoice/src/payment.rs93.13% <87.50%> (+2.26%)⬆️
lightning/src/util/persist.rs95.23% <87.50%> (+0.50%)⬆️
lightning/src/chain/chainmonitor.rs97.78% <88.46%> (-0.28%)⬇️
lightning-persister/src/lib.rs93.45% <100.00%> (ø)
lightning/src/ln/chanmon_update_fail_tests.rs97.72% <100.00%> (+<0.01%)⬆️
lightning/src/ln/channel.rs88.66% <100.00%> (ø)
... and 17 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 2572ac5 to 0fe1f28CompareJuly 18, 2022 01:59
@TheBlueMatt
TheBlueMatt marked this pull request as ready for review July 18, 2022 01:59
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Finally getting back to the monitor work, finally updated this to include a monitor update failure type rework, but no further functional changes.

Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/chain/chainmonitor.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/chanmon_update_fail_tests.rs
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from dc2888a to a9f656aCompareAugust 16, 2022 22:32
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Oops, somehow I totally lost track of this PR having a pending review, so sorry about that! Rebased on latest git and addressed feedback.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from a9f656a to e3347f1CompareAugust 16, 2022 22:39
See doc updates for more info on the edge case this prevents, and
there isn't really a strong reason why we would need to broadcast
the latest state immediately. Specifically, in the case of HTLC
claims (the most important reason to ensure we have state on chain
if it cannot be persisted), we will still force-close if there are
HTLCs which need claiming and are going to expire.
Surprisingly, there were no tests which failed as a result of this
change, but a new one has been added.
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 86a5a28 to 24f8279CompareSeptember 15, 2022 19:38
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Rebased to address conflicts and went ahead and squashed as its been a while.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 24f8279 to 4825a96CompareSeptember 15, 2022 19:52
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
// a monitor that we didn't manage to store (and that we don't care about - we
// don't respond with the funding_signed so the channel can never go on chain).
let (_monitor_update, failed_htlcs) = chan.force_shutdown(true);
let (_monitor_update, failed_htlcs) = chan.force_shutdown(false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like there is not test for this scenario. All tests pass when reverting to true.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Its unobservable - note that the monitor_update, which is the place the bool is propagated to, is drop'd.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 4825a96 to c0bb4f4CompareSeptember 25, 2022 07:50
Comment threadlightning/src/util/errors.rs Outdated
Comment on lines 49 to 50
/// An attempt to call watch/update_channel returned an Err (ie you did this!), causing the
/// attempted action to fail.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you update the docs since it's no longer from an Err? Would be good to link to the corresponding method docs and maybe phrase the docs to be a little clearer, too.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1, also a bit weird that it's still considered an APIError variant, but that might require some more brainstorming than a simple rename/doc update.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Yea, did update the docs, but agree - this shouldnt really be an error at all, it should just be Ok. I think there's only one(-ish) place where it Errs - sending payments - so it should be easy to do in a followup.

Comment on lines +223 to +228
/// When this is returned, [`ChannelManager`] will force-close the channel but *not* broadcast
/// our current commitment transaction. This avoids a dangerous case where a local disk failure
/// (e.g. the Linux-default remounting of the disk as read-only) causes [`PermanentFailure`]s
/// for all monitor updates. If we were to broadcast our latest commitment transaction and then
/// restart, we could end up reading a previous [`ChannelMonitor`] and [`ChannelManager`],
/// revoking our now-broadcasted state before seeing it confirm and losing all our funds.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

IIUC, while the user could choose not to broadcast the latest commitment transaction, which has been revoked, it could still be done by LDK if it contained any pending HTLCs that are expiring soon.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Indeed, somewhat of a separate issue that should be addressed in #1593

Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/util/errors.rs Outdated
Comment on lines 49 to 50
/// An attempt to call watch/update_channel returned an Err (ie you did this!), causing the
/// attempted action to fail.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1, also a bit weird that it's still considered an APIError variant, but that might require some more brainstorming than a simple rename/doc update.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from c0bb4f4 to 74745cbCompareSeptember 28, 2022 15:14

@wpaulinowpaulino left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, feel free to squash.

Comment threadlightning/src/ln/channel.rs Outdated
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 74745cb to fc18acbCompareSeptember 28, 2022 21:11

@jkczyzjkczyz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, please squash.

Comment threadlightning/src/util/errors.rs Outdated
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from fc18acb to 4ac5804CompareSeptember 29, 2022 09:02
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Addressed one more comment and squashed. Diff since last push:

$ git diff-tree -U1 fc18acb4 4ac58048
diff --git a/lightning/src/util/errors.rs b/lightning/src/util/errors.rs
index 83324eab6..ad6993542 100644
--- a/lightning/src/util/errors.rs+++ b/lightning/src/util/errors.rs@@ -48,7 +48,9 @@ pub enum APIError {
},
-	/// An attempt to call watch/update_channel returned a-	/// [`ChannelMonitorUpdateStatus::InProgress`] indicating the persistence of a monitor update-	/// is awaiting async resolution. Once it resolves the attempted action should complete-	/// automatically.+	/// An attempt to call [`chain::Watch::watch_channel`]/[`chain::Watch::update_channel`]+	/// returned a [`ChannelMonitorUpdateStatus::InProgress`] indicating the persistence of a+	/// monitor update is awaiting async resolution. Once it resolves the attempted action should+	/// complete automatically.
///
+	/// [`chain::Watch::watch_channel`]: crate::chain::Watch::watch_channel+	/// [`chain::Watch::update_channel`]: crate::chain::Watch::update_channel
/// [`ChannelMonitorUpdateStatus::InProgress`]: crate::chain::ChannelMonitorUpdateStatus::InProgress

Comment threadlightning/src/util/errors.rs
When a `chain::Watch` `ChannelMonitor` update method is called, the
user has three options:
(a) persist the monitor update immediately and return success,
(b) fail to persist the monitor update immediately and return
failure,
(c) return a flag indicating the monitor update is in progress and
will complete in the future.
(c) is rather harmless, and in some deployments should be expected
to be the return value for all monitor update calls, but currently
requires returning `Err(ChannelMonitorUpdateErr::TemporaryFailure)`
which isn't very descriptive and sounds scarier than it is.
Instead, here, we change the return type used to be a single enum
(rather than a Result) and rename `TemporaryFailure`
`UpdateInProgress`.
If we receive a monitor event from a forwarded-to channel which
contains a preimage for an HTLC, we have to propogate that preimage
back to the forwarded-from channel monitor. However, once we have
that update, we're running in a relatively unsafe state - we have
the preimage in memory, but if we were to crash the forwarded-to
channel monitor will not regenerate the update with the preimage
for us. If we haven't managed to write the monitor update to the
forwarded-from channel by that point, we've lost the preimage, and,
thus, money!
This much more accurately represents the error, indicating that a
monitor update is in progress asynchronously and may complete at a
later time.
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 4ac5804 to 52934e0CompareSeptember 29, 2022 20:28
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Oops, sorry, reordered the missing hunk into the right commit, no ultimate diff, though.

@TheBlueMatt
TheBlueMatt merged commit 7544030 into lightningdevkit:mainSep 29, 2022
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

7 participants

@TheBlueMatt@valentinewallace@codecov-commenter@G8XSU@jkczyz@wpaulino@tnull
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Do not broadcast commitment txn on Permanent mon update failure by TheBlueMatt · Pull Request #1106 · lightningdevkit/rust-lightning · GitHub
Skip to content

Do not broadcast commitment txn on Permanent mon update failure - #1106

Merged
TheBlueMatt merged 7 commits into
lightningdevkit:mainfrom
TheBlueMatt:2021-10-no-perm-err-broadcast
Sep 29, 2022
Merged

Do not broadcast commitment txn on Permanent mon update failure#1106
TheBlueMatt merged 7 commits into
lightningdevkit:mainfrom
TheBlueMatt:2021-10-no-perm-err-broadcast

Conversation

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

See doc updates for more info on the edge case this prevents, and
there isn't really a strong reason why we would need to broadcast
the latest state immediately. Specifically, in the case of HTLC
claims (the most important reason to ensure we have state on chain
if it cannot be persisted), we will still force-close if there are
HTLCs which need claiming and are going to expire.

Surprisingly, there were no tests which failed as a result of this
change, but a new one has been added.

@codecov

codecovBot commented Oct 5, 2021

Copy link
Copy Markdown

Codecov Report

Merging #1106 (5f848de) into main (107c6c7) will increase coverage by 1.71%.
The diff coverage is 90.00%.

❗ Current head 5f848de differs from pull request most recent head f99da12. Consider uploading reports for the commit f99da12 to get more accurate results
Impacted file tree graph

@@ Coverage Diff @@## main #1106 +/- ##
==========================================
+ Coverage 90.40% 92.12% +1.71% 
==========================================
Files 68 66 -2 Lines 34796 40933 +6137 ==========================================
+ Hits 31458 37708 +6250 + Misses 3338 3225 -113 
Impacted FilesCoverage Δ
lightning/src/ln/channelmanager.rs89.20% <50.00%> (+5.68%)⬆️
lightning/src/chain/channelmonitor.rs91.28% <100.00%> (+0.33%)⬆️
lightning/src/ln/chanmon_update_fail_tests.rs98.79% <100.00%> (+1.14%)⬆️
lightning/src/chain/mod.rs58.82% <0.00%> (-2.29%)⬇️
lightning-background-processor/src/lib.rs93.75% <0.00%> (-0.49%)⬇️
lightning/src/chain/onchaintx.rs94.27% <0.00%> (-0.48%)⬇️
lightning/src/ln/onion_utils.rs94.91% <0.00%> (-0.46%)⬇️
lightning-invoice/src/utils.rs84.09% <0.00%> (-0.18%)⬇️
lightning-persister/src/lib.rs94.21% <0.00%> (-0.10%)⬇️
lightning/src/ln/payment_tests.rs98.75% <0.00%> (-0.09%)⬇️
... and 23 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 107c6c7...f99da12. Read the comment docs.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from bd79c03 to e6966ceCompareOctober 5, 2021 06:02
Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment on lines +150 to +151
/// or, e.g. we've moved on to a different watchtower and cannot update with all watchtowers
/// that were previously informed of this channel).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Trying to parse this watchtower case -- in this situation, we're switching watchtowers, but have no way of contacting the old watchtower to just delete our data, therefore the channel needs to close via PermanentFailure?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Yea, but honestly its confusing, I replaced it with a better example.

Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment on lines +167 to +168
/// [`ChannelMonitor::get_latest_holder_commitment_txn`] once you've safely ensured no further
/// off-chain updates to the channel can occur.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Checking -- "ensure no further off-chain updates can occur" == "applied the final monitor update mentioned on L170"? Or?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

It means "no ChannelManager still knows about this channel", I reworded it.

Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/chain/channelmonitor.rs Outdated
@valentinewallace

Copy link
Copy Markdown
Contributor

Bit confused how to implement these new requirements in the sample. I guess we now need a proxy layer implementing Persist between the FilesystemPersister and ChainMonitor?

@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Bit confused how to implement these new requirements in the sample.

I think we, in general, need to think hard about how we handle disk failures in the sample/lightning-persister. If the disk gets unplugged while we're running, we should just shut down and move on, but there's no way to get an error out of the Persister. The way the code works today is unsafe in this condition - if the disk gets unplugged, we'll broadcast the latest state and keep going, if the user then plugs the disk back in and restarts we may revoke the now-broadcasted state.

I think the end result for the sample needs to be a "really definitely broadcast the latest state from the channelmonitor for channel X" command, which is marked unsafe. We should probably also have a programatic way to detect monitors in this condition and not just rely on the logs in ChannelManager deserialization, I'll work on that bit here.

@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Rebased on upstream. Need to make sure all the updated content in the failure enum docs is still kept and should rename temporaryfailure -> asyncpersist or so.

@TheBlueMatt
TheBlueMatt marked this pull request as draft October 20, 2021 01:34
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 6ad8583 to f99da12CompareOctober 20, 2021 02:55
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from f99da12 to 2572ac5CompareJuly 17, 2022 20:11
@codecov-commenter

codecov-commenter commented Jul 17, 2022

Copy link
Copy Markdown

Codecov Report

Base: 90.78% // Head: 91.21% // Increases project coverage by +0.43% 🎉

Coverage data is based on head (74745cb) compared to base (48d21ba).
Patch coverage: 89.09% of modified lines in pull request are covered.

❗ Current head 74745cb differs from pull request most recent head 52934e0. Consider uploading reports for the commit 52934e0 to get more accurate results

Additional details and impacted files
@@ Coverage Diff @@## main #1106 +/- ##
==========================================
+ Coverage 90.78% 91.21% +0.43% 
==========================================
Files 86 87 +1 Lines 46631 50794 +4163 Branches 46631 50794 +4163 ==========================================
+ Hits 42335 46333 +3998 - Misses 4296 4461 +165 
Impacted FilesCoverage Δ
lightning/src/chain/mod.rs68.18% <ø> (ø)
lightning/src/util/errors.rs72.22% <0.00%> (ø)
lightning/src/chain/channelmonitor.rs91.21% <75.00%> (+<0.01%)⬆️
lightning/src/ln/channelmanager.rs88.11% <83.23%> (+3.05%)⬆️
lightning-invoice/src/payment.rs93.13% <87.50%> (+2.26%)⬆️
lightning/src/util/persist.rs95.23% <87.50%> (+0.50%)⬆️
lightning/src/chain/chainmonitor.rs97.78% <88.46%> (-0.28%)⬇️
lightning-persister/src/lib.rs93.45% <100.00%> (ø)
lightning/src/ln/chanmon_update_fail_tests.rs97.72% <100.00%> (+<0.01%)⬆️
lightning/src/ln/channel.rs88.66% <100.00%> (ø)
... and 17 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 2572ac5 to 0fe1f28CompareJuly 18, 2022 01:59
@TheBlueMatt
TheBlueMatt marked this pull request as ready for review July 18, 2022 01:59
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Finally getting back to the monitor work, finally updated this to include a monitor update failure type rework, but no further functional changes.

Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/chain/chainmonitor.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/chanmon_update_fail_tests.rs
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from dc2888a to a9f656aCompareAugust 16, 2022 22:32
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Oops, somehow I totally lost track of this PR having a pending review, so sorry about that! Rebased on latest git and addressed feedback.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from a9f656a to e3347f1CompareAugust 16, 2022 22:39
See doc updates for more info on the edge case this prevents, and
there isn't really a strong reason why we would need to broadcast
the latest state immediately. Specifically, in the case of HTLC
claims (the most important reason to ensure we have state on chain
if it cannot be persisted), we will still force-close if there are
HTLCs which need claiming and are going to expire.
Surprisingly, there were no tests which failed as a result of this
change, but a new one has been added.
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 86a5a28 to 24f8279CompareSeptember 15, 2022 19:38
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Rebased to address conflicts and went ahead and squashed as its been a while.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 24f8279 to 4825a96CompareSeptember 15, 2022 19:52
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
// a monitor that we didn't manage to store (and that we don't care about - we
// don't respond with the funding_signed so the channel can never go on chain).
let (_monitor_update, failed_htlcs) = chan.force_shutdown(true);
let (_monitor_update, failed_htlcs) = chan.force_shutdown(false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like there is not test for this scenario. All tests pass when reverting to true.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Its unobservable - note that the monitor_update, which is the place the bool is propagated to, is drop'd.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 4825a96 to c0bb4f4CompareSeptember 25, 2022 07:50
Comment threadlightning/src/util/errors.rs Outdated
Comment on lines 49 to 50
/// An attempt to call watch/update_channel returned an Err (ie you did this!), causing the
/// attempted action to fail.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you update the docs since it's no longer from an Err? Would be good to link to the corresponding method docs and maybe phrase the docs to be a little clearer, too.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1, also a bit weird that it's still considered an APIError variant, but that might require some more brainstorming than a simple rename/doc update.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Yea, did update the docs, but agree - this shouldnt really be an error at all, it should just be Ok. I think there's only one(-ish) place where it Errs - sending payments - so it should be easy to do in a followup.

Comment on lines +223 to +228
/// When this is returned, [`ChannelManager`] will force-close the channel but *not* broadcast
/// our current commitment transaction. This avoids a dangerous case where a local disk failure
/// (e.g. the Linux-default remounting of the disk as read-only) causes [`PermanentFailure`]s
/// for all monitor updates. If we were to broadcast our latest commitment transaction and then
/// restart, we could end up reading a previous [`ChannelMonitor`] and [`ChannelManager`],
/// revoking our now-broadcasted state before seeing it confirm and losing all our funds.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

IIUC, while the user could choose not to broadcast the latest commitment transaction, which has been revoked, it could still be done by LDK if it contained any pending HTLCs that are expiring soon.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Indeed, somewhat of a separate issue that should be addressed in #1593

Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/util/errors.rs Outdated
Comment on lines 49 to 50
/// An attempt to call watch/update_channel returned an Err (ie you did this!), causing the
/// attempted action to fail.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1, also a bit weird that it's still considered an APIError variant, but that might require some more brainstorming than a simple rename/doc update.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from c0bb4f4 to 74745cbCompareSeptember 28, 2022 15:14

@wpaulinowpaulino left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, feel free to squash.

Comment threadlightning/src/ln/channel.rs Outdated
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 74745cb to fc18acbCompareSeptember 28, 2022 21:11

@jkczyzjkczyz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, please squash.

Comment threadlightning/src/util/errors.rs Outdated
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from fc18acb to 4ac5804CompareSeptember 29, 2022 09:02
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Addressed one more comment and squashed. Diff since last push:

$ git diff-tree -U1 fc18acb4 4ac58048
diff --git a/lightning/src/util/errors.rs b/lightning/src/util/errors.rs
index 83324eab6..ad6993542 100644
--- a/lightning/src/util/errors.rs+++ b/lightning/src/util/errors.rs@@ -48,7 +48,9 @@ pub enum APIError {
},
-	/// An attempt to call watch/update_channel returned a-	/// [`ChannelMonitorUpdateStatus::InProgress`] indicating the persistence of a monitor update-	/// is awaiting async resolution. Once it resolves the attempted action should complete-	/// automatically.+	/// An attempt to call [`chain::Watch::watch_channel`]/[`chain::Watch::update_channel`]+	/// returned a [`ChannelMonitorUpdateStatus::InProgress`] indicating the persistence of a+	/// monitor update is awaiting async resolution. Once it resolves the attempted action should+	/// complete automatically.
///
+	/// [`chain::Watch::watch_channel`]: crate::chain::Watch::watch_channel+	/// [`chain::Watch::update_channel`]: crate::chain::Watch::update_channel
/// [`ChannelMonitorUpdateStatus::InProgress`]: crate::chain::ChannelMonitorUpdateStatus::InProgress

Comment threadlightning/src/util/errors.rs
When a `chain::Watch` `ChannelMonitor` update method is called, the
user has three options:
(a) persist the monitor update immediately and return success,
(b) fail to persist the monitor update immediately and return
failure,
(c) return a flag indicating the monitor update is in progress and
will complete in the future.
(c) is rather harmless, and in some deployments should be expected
to be the return value for all monitor update calls, but currently
requires returning `Err(ChannelMonitorUpdateErr::TemporaryFailure)`
which isn't very descriptive and sounds scarier than it is.
Instead, here, we change the return type used to be a single enum
(rather than a Result) and rename `TemporaryFailure`
`UpdateInProgress`.
If we receive a monitor event from a forwarded-to channel which
contains a preimage for an HTLC, we have to propogate that preimage
back to the forwarded-from channel monitor. However, once we have
that update, we're running in a relatively unsafe state - we have
the preimage in memory, but if we were to crash the forwarded-to
channel monitor will not regenerate the update with the preimage
for us. If we haven't managed to write the monitor update to the
forwarded-from channel by that point, we've lost the preimage, and,
thus, money!
This much more accurately represents the error, indicating that a
monitor update is in progress asynchronously and may complete at a
later time.
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 4ac5804 to 52934e0CompareSeptember 29, 2022 20:28
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Oops, sorry, reordered the missing hunk into the right commit, no ultimate diff, though.

@TheBlueMatt
TheBlueMatt merged commit 7544030 into lightningdevkit:mainSep 29, 2022
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

7 participants

@TheBlueMatt@valentinewallace@codecov-commenter@G8XSU@jkczyz@wpaulino@tnull
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' Do not broadcast commitment txn on Permanent mon update failure by TheBlueMatt · Pull Request #1106 · lightningdevkit/rust-lightning · GitHub
Skip to content

Do not broadcast commitment txn on Permanent mon update failure - #1106

Merged
TheBlueMatt merged 7 commits into
lightningdevkit:mainfrom
TheBlueMatt:2021-10-no-perm-err-broadcast
Sep 29, 2022
Merged

Do not broadcast commitment txn on Permanent mon update failure#1106
TheBlueMatt merged 7 commits into
lightningdevkit:mainfrom
TheBlueMatt:2021-10-no-perm-err-broadcast

Conversation

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

See doc updates for more info on the edge case this prevents, and
there isn't really a strong reason why we would need to broadcast
the latest state immediately. Specifically, in the case of HTLC
claims (the most important reason to ensure we have state on chain
if it cannot be persisted), we will still force-close if there are
HTLCs which need claiming and are going to expire.

Surprisingly, there were no tests which failed as a result of this
change, but a new one has been added.

@codecov

codecovBot commented Oct 5, 2021

Copy link
Copy Markdown

Codecov Report

Merging #1106 (5f848de) into main (107c6c7) will increase coverage by 1.71%.
The diff coverage is 90.00%.

❗ Current head 5f848de differs from pull request most recent head f99da12. Consider uploading reports for the commit f99da12 to get more accurate results
Impacted file tree graph

@@ Coverage Diff @@## main #1106 +/- ##
==========================================
+ Coverage 90.40% 92.12% +1.71% 
==========================================
Files 68 66 -2 Lines 34796 40933 +6137 ==========================================
+ Hits 31458 37708 +6250 + Misses 3338 3225 -113 
Impacted FilesCoverage Δ
lightning/src/ln/channelmanager.rs89.20% <50.00%> (+5.68%)⬆️
lightning/src/chain/channelmonitor.rs91.28% <100.00%> (+0.33%)⬆️
lightning/src/ln/chanmon_update_fail_tests.rs98.79% <100.00%> (+1.14%)⬆️
lightning/src/chain/mod.rs58.82% <0.00%> (-2.29%)⬇️
lightning-background-processor/src/lib.rs93.75% <0.00%> (-0.49%)⬇️
lightning/src/chain/onchaintx.rs94.27% <0.00%> (-0.48%)⬇️
lightning/src/ln/onion_utils.rs94.91% <0.00%> (-0.46%)⬇️
lightning-invoice/src/utils.rs84.09% <0.00%> (-0.18%)⬇️
lightning-persister/src/lib.rs94.21% <0.00%> (-0.10%)⬇️
lightning/src/ln/payment_tests.rs98.75% <0.00%> (-0.09%)⬇️
... and 23 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 107c6c7...f99da12. Read the comment docs.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from bd79c03 to e6966ceCompareOctober 5, 2021 06:02
Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment on lines +150 to +151
/// or, e.g. we've moved on to a different watchtower and cannot update with all watchtowers
/// that were previously informed of this channel).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Trying to parse this watchtower case -- in this situation, we're switching watchtowers, but have no way of contacting the old watchtower to just delete our data, therefore the channel needs to close via PermanentFailure?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Yea, but honestly its confusing, I replaced it with a better example.

Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment on lines +167 to +168
/// [`ChannelMonitor::get_latest_holder_commitment_txn`] once you've safely ensured no further
/// off-chain updates to the channel can occur.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Checking -- "ensure no further off-chain updates can occur" == "applied the final monitor update mentioned on L170"? Or?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

It means "no ChannelManager still knows about this channel", I reworded it.

Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/chain/channelmonitor.rs Outdated
@valentinewallace

Copy link
Copy Markdown
Contributor

Bit confused how to implement these new requirements in the sample. I guess we now need a proxy layer implementing Persist between the FilesystemPersister and ChainMonitor?

@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Bit confused how to implement these new requirements in the sample.

I think we, in general, need to think hard about how we handle disk failures in the sample/lightning-persister. If the disk gets unplugged while we're running, we should just shut down and move on, but there's no way to get an error out of the Persister. The way the code works today is unsafe in this condition - if the disk gets unplugged, we'll broadcast the latest state and keep going, if the user then plugs the disk back in and restarts we may revoke the now-broadcasted state.

I think the end result for the sample needs to be a "really definitely broadcast the latest state from the channelmonitor for channel X" command, which is marked unsafe. We should probably also have a programatic way to detect monitors in this condition and not just rely on the logs in ChannelManager deserialization, I'll work on that bit here.

@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Rebased on upstream. Need to make sure all the updated content in the failure enum docs is still kept and should rename temporaryfailure -> asyncpersist or so.

@TheBlueMatt
TheBlueMatt marked this pull request as draft October 20, 2021 01:34
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 6ad8583 to f99da12CompareOctober 20, 2021 02:55
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from f99da12 to 2572ac5CompareJuly 17, 2022 20:11
@codecov-commenter

codecov-commenter commented Jul 17, 2022

Copy link
Copy Markdown

Codecov Report

Base: 90.78% // Head: 91.21% // Increases project coverage by +0.43% 🎉

Coverage data is based on head (74745cb) compared to base (48d21ba).
Patch coverage: 89.09% of modified lines in pull request are covered.

❗ Current head 74745cb differs from pull request most recent head 52934e0. Consider uploading reports for the commit 52934e0 to get more accurate results

Additional details and impacted files
@@ Coverage Diff @@## main #1106 +/- ##
==========================================
+ Coverage 90.78% 91.21% +0.43% 
==========================================
Files 86 87 +1 Lines 46631 50794 +4163 Branches 46631 50794 +4163 ==========================================
+ Hits 42335 46333 +3998 - Misses 4296 4461 +165 
Impacted FilesCoverage Δ
lightning/src/chain/mod.rs68.18% <ø> (ø)
lightning/src/util/errors.rs72.22% <0.00%> (ø)
lightning/src/chain/channelmonitor.rs91.21% <75.00%> (+<0.01%)⬆️
lightning/src/ln/channelmanager.rs88.11% <83.23%> (+3.05%)⬆️
lightning-invoice/src/payment.rs93.13% <87.50%> (+2.26%)⬆️
lightning/src/util/persist.rs95.23% <87.50%> (+0.50%)⬆️
lightning/src/chain/chainmonitor.rs97.78% <88.46%> (-0.28%)⬇️
lightning-persister/src/lib.rs93.45% <100.00%> (ø)
lightning/src/ln/chanmon_update_fail_tests.rs97.72% <100.00%> (+<0.01%)⬆️
lightning/src/ln/channel.rs88.66% <100.00%> (ø)
... and 17 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 2572ac5 to 0fe1f28CompareJuly 18, 2022 01:59
@TheBlueMatt
TheBlueMatt marked this pull request as ready for review July 18, 2022 01:59
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Finally getting back to the monitor work, finally updated this to include a monitor update failure type rework, but no further functional changes.

Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/chain/chainmonitor.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/chanmon_update_fail_tests.rs
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from dc2888a to a9f656aCompareAugust 16, 2022 22:32
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Oops, somehow I totally lost track of this PR having a pending review, so sorry about that! Rebased on latest git and addressed feedback.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from a9f656a to e3347f1CompareAugust 16, 2022 22:39
See doc updates for more info on the edge case this prevents, and
there isn't really a strong reason why we would need to broadcast
the latest state immediately. Specifically, in the case of HTLC
claims (the most important reason to ensure we have state on chain
if it cannot be persisted), we will still force-close if there are
HTLCs which need claiming and are going to expire.
Surprisingly, there were no tests which failed as a result of this
change, but a new one has been added.
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 86a5a28 to 24f8279CompareSeptember 15, 2022 19:38
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Rebased to address conflicts and went ahead and squashed as its been a while.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 24f8279 to 4825a96CompareSeptember 15, 2022 19:52
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
// a monitor that we didn't manage to store (and that we don't care about - we
// don't respond with the funding_signed so the channel can never go on chain).
let (_monitor_update, failed_htlcs) = chan.force_shutdown(true);
let (_monitor_update, failed_htlcs) = chan.force_shutdown(false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like there is not test for this scenario. All tests pass when reverting to true.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Its unobservable - note that the monitor_update, which is the place the bool is propagated to, is drop'd.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 4825a96 to c0bb4f4CompareSeptember 25, 2022 07:50
Comment threadlightning/src/util/errors.rs Outdated
Comment on lines 49 to 50
/// An attempt to call watch/update_channel returned an Err (ie you did this!), causing the
/// attempted action to fail.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you update the docs since it's no longer from an Err? Would be good to link to the corresponding method docs and maybe phrase the docs to be a little clearer, too.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1, also a bit weird that it's still considered an APIError variant, but that might require some more brainstorming than a simple rename/doc update.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Yea, did update the docs, but agree - this shouldnt really be an error at all, it should just be Ok. I think there's only one(-ish) place where it Errs - sending payments - so it should be easy to do in a followup.

Comment on lines +223 to +228
/// When this is returned, [`ChannelManager`] will force-close the channel but *not* broadcast
/// our current commitment transaction. This avoids a dangerous case where a local disk failure
/// (e.g. the Linux-default remounting of the disk as read-only) causes [`PermanentFailure`]s
/// for all monitor updates. If we were to broadcast our latest commitment transaction and then
/// restart, we could end up reading a previous [`ChannelMonitor`] and [`ChannelManager`],
/// revoking our now-broadcasted state before seeing it confirm and losing all our funds.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

IIUC, while the user could choose not to broadcast the latest commitment transaction, which has been revoked, it could still be done by LDK if it contained any pending HTLCs that are expiring soon.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Indeed, somewhat of a separate issue that should be addressed in #1593

Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/util/errors.rs Outdated
Comment on lines 49 to 50
/// An attempt to call watch/update_channel returned an Err (ie you did this!), causing the
/// attempted action to fail.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1, also a bit weird that it's still considered an APIError variant, but that might require some more brainstorming than a simple rename/doc update.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from c0bb4f4 to 74745cbCompareSeptember 28, 2022 15:14

@wpaulinowpaulino left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, feel free to squash.

Comment threadlightning/src/ln/channel.rs Outdated
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 74745cb to fc18acbCompareSeptember 28, 2022 21:11

@jkczyzjkczyz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, please squash.

Comment threadlightning/src/util/errors.rs Outdated
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from fc18acb to 4ac5804CompareSeptember 29, 2022 09:02
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Addressed one more comment and squashed. Diff since last push:

$ git diff-tree -U1 fc18acb4 4ac58048
diff --git a/lightning/src/util/errors.rs b/lightning/src/util/errors.rs
index 83324eab6..ad6993542 100644
--- a/lightning/src/util/errors.rs+++ b/lightning/src/util/errors.rs@@ -48,7 +48,9 @@ pub enum APIError {
},
-	/// An attempt to call watch/update_channel returned a-	/// [`ChannelMonitorUpdateStatus::InProgress`] indicating the persistence of a monitor update-	/// is awaiting async resolution. Once it resolves the attempted action should complete-	/// automatically.+	/// An attempt to call [`chain::Watch::watch_channel`]/[`chain::Watch::update_channel`]+	/// returned a [`ChannelMonitorUpdateStatus::InProgress`] indicating the persistence of a+	/// monitor update is awaiting async resolution. Once it resolves the attempted action should+	/// complete automatically.
///
+	/// [`chain::Watch::watch_channel`]: crate::chain::Watch::watch_channel+	/// [`chain::Watch::update_channel`]: crate::chain::Watch::update_channel
/// [`ChannelMonitorUpdateStatus::InProgress`]: crate::chain::ChannelMonitorUpdateStatus::InProgress

Comment threadlightning/src/util/errors.rs
When a `chain::Watch` `ChannelMonitor` update method is called, the
user has three options:
(a) persist the monitor update immediately and return success,
(b) fail to persist the monitor update immediately and return
failure,
(c) return a flag indicating the monitor update is in progress and
will complete in the future.
(c) is rather harmless, and in some deployments should be expected
to be the return value for all monitor update calls, but currently
requires returning `Err(ChannelMonitorUpdateErr::TemporaryFailure)`
which isn't very descriptive and sounds scarier than it is.
Instead, here, we change the return type used to be a single enum
(rather than a Result) and rename `TemporaryFailure`
`UpdateInProgress`.
If we receive a monitor event from a forwarded-to channel which
contains a preimage for an HTLC, we have to propogate that preimage
back to the forwarded-from channel monitor. However, once we have
that update, we're running in a relatively unsafe state - we have
the preimage in memory, but if we were to crash the forwarded-to
channel monitor will not regenerate the update with the preimage
for us. If we haven't managed to write the monitor update to the
forwarded-from channel by that point, we've lost the preimage, and,
thus, money!
This much more accurately represents the error, indicating that a
monitor update is in progress asynchronously and may complete at a
later time.
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 4ac5804 to 52934e0CompareSeptember 29, 2022 20:28
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Oops, sorry, reordered the missing hunk into the right commit, no ultimate diff, though.

@TheBlueMatt
TheBlueMatt merged commit 7544030 into lightningdevkit:mainSep 29, 2022
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

7 participants

@TheBlueMatt@valentinewallace@codecov-commenter@G8XSU@jkczyz@wpaulino@tnull
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Do not broadcast commitment txn on Permanent mon update failure by TheBlueMatt · Pull Request #1106 · lightningdevkit/rust-lightning · GitHub
Skip to content

Do not broadcast commitment txn on Permanent mon update failure - #1106

Merged
TheBlueMatt merged 7 commits into
lightningdevkit:mainfrom
TheBlueMatt:2021-10-no-perm-err-broadcast
Sep 29, 2022
Merged

Do not broadcast commitment txn on Permanent mon update failure#1106
TheBlueMatt merged 7 commits into
lightningdevkit:mainfrom
TheBlueMatt:2021-10-no-perm-err-broadcast

Conversation

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

See doc updates for more info on the edge case this prevents, and
there isn't really a strong reason why we would need to broadcast
the latest state immediately. Specifically, in the case of HTLC
claims (the most important reason to ensure we have state on chain
if it cannot be persisted), we will still force-close if there are
HTLCs which need claiming and are going to expire.

Surprisingly, there were no tests which failed as a result of this
change, but a new one has been added.

@codecov

codecovBot commented Oct 5, 2021

Copy link
Copy Markdown

Codecov Report

Merging #1106 (5f848de) into main (107c6c7) will increase coverage by 1.71%.
The diff coverage is 90.00%.

❗ Current head 5f848de differs from pull request most recent head f99da12. Consider uploading reports for the commit f99da12 to get more accurate results
Impacted file tree graph

@@ Coverage Diff @@## main #1106 +/- ##
==========================================
+ Coverage 90.40% 92.12% +1.71% 
==========================================
Files 68 66 -2 Lines 34796 40933 +6137 ==========================================
+ Hits 31458 37708 +6250 + Misses 3338 3225 -113 
Impacted FilesCoverage Δ
lightning/src/ln/channelmanager.rs89.20% <50.00%> (+5.68%)⬆️
lightning/src/chain/channelmonitor.rs91.28% <100.00%> (+0.33%)⬆️
lightning/src/ln/chanmon_update_fail_tests.rs98.79% <100.00%> (+1.14%)⬆️
lightning/src/chain/mod.rs58.82% <0.00%> (-2.29%)⬇️
lightning-background-processor/src/lib.rs93.75% <0.00%> (-0.49%)⬇️
lightning/src/chain/onchaintx.rs94.27% <0.00%> (-0.48%)⬇️
lightning/src/ln/onion_utils.rs94.91% <0.00%> (-0.46%)⬇️
lightning-invoice/src/utils.rs84.09% <0.00%> (-0.18%)⬇️
lightning-persister/src/lib.rs94.21% <0.00%> (-0.10%)⬇️
lightning/src/ln/payment_tests.rs98.75% <0.00%> (-0.09%)⬇️
... and 23 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 107c6c7...f99da12. Read the comment docs.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from bd79c03 to e6966ceCompareOctober 5, 2021 06:02
Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment on lines +150 to +151
/// or, e.g. we've moved on to a different watchtower and cannot update with all watchtowers
/// that were previously informed of this channel).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Trying to parse this watchtower case -- in this situation, we're switching watchtowers, but have no way of contacting the old watchtower to just delete our data, therefore the channel needs to close via PermanentFailure?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Yea, but honestly its confusing, I replaced it with a better example.

Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment on lines +167 to +168
/// [`ChannelMonitor::get_latest_holder_commitment_txn`] once you've safely ensured no further
/// off-chain updates to the channel can occur.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Checking -- "ensure no further off-chain updates can occur" == "applied the final monitor update mentioned on L170"? Or?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

It means "no ChannelManager still knows about this channel", I reworded it.

Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/chain/channelmonitor.rs Outdated
@valentinewallace

Copy link
Copy Markdown
Contributor

Bit confused how to implement these new requirements in the sample. I guess we now need a proxy layer implementing Persist between the FilesystemPersister and ChainMonitor?

@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Bit confused how to implement these new requirements in the sample.

I think we, in general, need to think hard about how we handle disk failures in the sample/lightning-persister. If the disk gets unplugged while we're running, we should just shut down and move on, but there's no way to get an error out of the Persister. The way the code works today is unsafe in this condition - if the disk gets unplugged, we'll broadcast the latest state and keep going, if the user then plugs the disk back in and restarts we may revoke the now-broadcasted state.

I think the end result for the sample needs to be a "really definitely broadcast the latest state from the channelmonitor for channel X" command, which is marked unsafe. We should probably also have a programatic way to detect monitors in this condition and not just rely on the logs in ChannelManager deserialization, I'll work on that bit here.

@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Rebased on upstream. Need to make sure all the updated content in the failure enum docs is still kept and should rename temporaryfailure -> asyncpersist or so.

@TheBlueMatt
TheBlueMatt marked this pull request as draft October 20, 2021 01:34
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 6ad8583 to f99da12CompareOctober 20, 2021 02:55
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from f99da12 to 2572ac5CompareJuly 17, 2022 20:11
@codecov-commenter

codecov-commenter commented Jul 17, 2022

Copy link
Copy Markdown

Codecov Report

Base: 90.78% // Head: 91.21% // Increases project coverage by +0.43% 🎉

Coverage data is based on head (74745cb) compared to base (48d21ba).
Patch coverage: 89.09% of modified lines in pull request are covered.

❗ Current head 74745cb differs from pull request most recent head 52934e0. Consider uploading reports for the commit 52934e0 to get more accurate results

Additional details and impacted files
@@ Coverage Diff @@## main #1106 +/- ##
==========================================
+ Coverage 90.78% 91.21% +0.43% 
==========================================
Files 86 87 +1 Lines 46631 50794 +4163 Branches 46631 50794 +4163 ==========================================
+ Hits 42335 46333 +3998 - Misses 4296 4461 +165 
Impacted FilesCoverage Δ
lightning/src/chain/mod.rs68.18% <ø> (ø)
lightning/src/util/errors.rs72.22% <0.00%> (ø)
lightning/src/chain/channelmonitor.rs91.21% <75.00%> (+<0.01%)⬆️
lightning/src/ln/channelmanager.rs88.11% <83.23%> (+3.05%)⬆️
lightning-invoice/src/payment.rs93.13% <87.50%> (+2.26%)⬆️
lightning/src/util/persist.rs95.23% <87.50%> (+0.50%)⬆️
lightning/src/chain/chainmonitor.rs97.78% <88.46%> (-0.28%)⬇️
lightning-persister/src/lib.rs93.45% <100.00%> (ø)
lightning/src/ln/chanmon_update_fail_tests.rs97.72% <100.00%> (+<0.01%)⬆️
lightning/src/ln/channel.rs88.66% <100.00%> (ø)
... and 17 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 2572ac5 to 0fe1f28CompareJuly 18, 2022 01:59
@TheBlueMatt
TheBlueMatt marked this pull request as ready for review July 18, 2022 01:59
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Finally getting back to the monitor work, finally updated this to include a monitor update failure type rework, but no further functional changes.

Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/chain/chainmonitor.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/chanmon_update_fail_tests.rs
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from dc2888a to a9f656aCompareAugust 16, 2022 22:32
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Oops, somehow I totally lost track of this PR having a pending review, so sorry about that! Rebased on latest git and addressed feedback.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from a9f656a to e3347f1CompareAugust 16, 2022 22:39
See doc updates for more info on the edge case this prevents, and
there isn't really a strong reason why we would need to broadcast
the latest state immediately. Specifically, in the case of HTLC
claims (the most important reason to ensure we have state on chain
if it cannot be persisted), we will still force-close if there are
HTLCs which need claiming and are going to expire.
Surprisingly, there were no tests which failed as a result of this
change, but a new one has been added.
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 86a5a28 to 24f8279CompareSeptember 15, 2022 19:38
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Rebased to address conflicts and went ahead and squashed as its been a while.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 24f8279 to 4825a96CompareSeptember 15, 2022 19:52
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
// a monitor that we didn't manage to store (and that we don't care about - we
// don't respond with the funding_signed so the channel can never go on chain).
let (_monitor_update, failed_htlcs) = chan.force_shutdown(true);
let (_monitor_update, failed_htlcs) = chan.force_shutdown(false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like there is not test for this scenario. All tests pass when reverting to true.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Its unobservable - note that the monitor_update, which is the place the bool is propagated to, is drop'd.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 4825a96 to c0bb4f4CompareSeptember 25, 2022 07:50
Comment threadlightning/src/util/errors.rs Outdated
Comment on lines 49 to 50
/// An attempt to call watch/update_channel returned an Err (ie you did this!), causing the
/// attempted action to fail.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you update the docs since it's no longer from an Err? Would be good to link to the corresponding method docs and maybe phrase the docs to be a little clearer, too.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1, also a bit weird that it's still considered an APIError variant, but that might require some more brainstorming than a simple rename/doc update.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Yea, did update the docs, but agree - this shouldnt really be an error at all, it should just be Ok. I think there's only one(-ish) place where it Errs - sending payments - so it should be easy to do in a followup.

Comment on lines +223 to +228
/// When this is returned, [`ChannelManager`] will force-close the channel but *not* broadcast
/// our current commitment transaction. This avoids a dangerous case where a local disk failure
/// (e.g. the Linux-default remounting of the disk as read-only) causes [`PermanentFailure`]s
/// for all monitor updates. If we were to broadcast our latest commitment transaction and then
/// restart, we could end up reading a previous [`ChannelMonitor`] and [`ChannelManager`],
/// revoking our now-broadcasted state before seeing it confirm and losing all our funds.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

IIUC, while the user could choose not to broadcast the latest commitment transaction, which has been revoked, it could still be done by LDK if it contained any pending HTLCs that are expiring soon.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Indeed, somewhat of a separate issue that should be addressed in #1593

Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/util/errors.rs Outdated
Comment on lines 49 to 50
/// An attempt to call watch/update_channel returned an Err (ie you did this!), causing the
/// attempted action to fail.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1, also a bit weird that it's still considered an APIError variant, but that might require some more brainstorming than a simple rename/doc update.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from c0bb4f4 to 74745cbCompareSeptember 28, 2022 15:14

@wpaulinowpaulino left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, feel free to squash.

Comment threadlightning/src/ln/channel.rs Outdated
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 74745cb to fc18acbCompareSeptember 28, 2022 21:11

@jkczyzjkczyz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, please squash.

Comment threadlightning/src/util/errors.rs Outdated
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from fc18acb to 4ac5804CompareSeptember 29, 2022 09:02
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Addressed one more comment and squashed. Diff since last push:

$ git diff-tree -U1 fc18acb4 4ac58048
diff --git a/lightning/src/util/errors.rs b/lightning/src/util/errors.rs
index 83324eab6..ad6993542 100644
--- a/lightning/src/util/errors.rs+++ b/lightning/src/util/errors.rs@@ -48,7 +48,9 @@ pub enum APIError {
},
-	/// An attempt to call watch/update_channel returned a-	/// [`ChannelMonitorUpdateStatus::InProgress`] indicating the persistence of a monitor update-	/// is awaiting async resolution. Once it resolves the attempted action should complete-	/// automatically.+	/// An attempt to call [`chain::Watch::watch_channel`]/[`chain::Watch::update_channel`]+	/// returned a [`ChannelMonitorUpdateStatus::InProgress`] indicating the persistence of a+	/// monitor update is awaiting async resolution. Once it resolves the attempted action should+	/// complete automatically.
///
+	/// [`chain::Watch::watch_channel`]: crate::chain::Watch::watch_channel+	/// [`chain::Watch::update_channel`]: crate::chain::Watch::update_channel
/// [`ChannelMonitorUpdateStatus::InProgress`]: crate::chain::ChannelMonitorUpdateStatus::InProgress

Comment threadlightning/src/util/errors.rs
When a `chain::Watch` `ChannelMonitor` update method is called, the
user has three options:
(a) persist the monitor update immediately and return success,
(b) fail to persist the monitor update immediately and return
failure,
(c) return a flag indicating the monitor update is in progress and
will complete in the future.
(c) is rather harmless, and in some deployments should be expected
to be the return value for all monitor update calls, but currently
requires returning `Err(ChannelMonitorUpdateErr::TemporaryFailure)`
which isn't very descriptive and sounds scarier than it is.
Instead, here, we change the return type used to be a single enum
(rather than a Result) and rename `TemporaryFailure`
`UpdateInProgress`.
If we receive a monitor event from a forwarded-to channel which
contains a preimage for an HTLC, we have to propogate that preimage
back to the forwarded-from channel monitor. However, once we have
that update, we're running in a relatively unsafe state - we have
the preimage in memory, but if we were to crash the forwarded-to
channel monitor will not regenerate the update with the preimage
for us. If we haven't managed to write the monitor update to the
forwarded-from channel by that point, we've lost the preimage, and,
thus, money!
This much more accurately represents the error, indicating that a
monitor update is in progress asynchronously and may complete at a
later time.
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 4ac5804 to 52934e0CompareSeptember 29, 2022 20:28
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Oops, sorry, reordered the missing hunk into the right commit, no ultimate diff, though.

@TheBlueMatt
TheBlueMatt merged commit 7544030 into lightningdevkit:mainSep 29, 2022
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

7 participants

@TheBlueMatt@valentinewallace@codecov-commenter@G8XSU@jkczyz@wpaulino@tnull
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Do not broadcast commitment txn on Permanent mon update failure by TheBlueMatt · Pull Request #1106 · lightningdevkit/rust-lightning · GitHub
Skip to content

Do not broadcast commitment txn on Permanent mon update failure - #1106

Merged
TheBlueMatt merged 7 commits into
lightningdevkit:mainfrom
TheBlueMatt:2021-10-no-perm-err-broadcast
Sep 29, 2022
Merged

Do not broadcast commitment txn on Permanent mon update failure#1106
TheBlueMatt merged 7 commits into
lightningdevkit:mainfrom
TheBlueMatt:2021-10-no-perm-err-broadcast

Conversation

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

See doc updates for more info on the edge case this prevents, and
there isn't really a strong reason why we would need to broadcast
the latest state immediately. Specifically, in the case of HTLC
claims (the most important reason to ensure we have state on chain
if it cannot be persisted), we will still force-close if there are
HTLCs which need claiming and are going to expire.

Surprisingly, there were no tests which failed as a result of this
change, but a new one has been added.

@codecov

codecovBot commented Oct 5, 2021

Copy link
Copy Markdown

Codecov Report

Merging #1106 (5f848de) into main (107c6c7) will increase coverage by 1.71%.
The diff coverage is 90.00%.

❗ Current head 5f848de differs from pull request most recent head f99da12. Consider uploading reports for the commit f99da12 to get more accurate results
Impacted file tree graph

@@ Coverage Diff @@## main #1106 +/- ##
==========================================
+ Coverage 90.40% 92.12% +1.71% 
==========================================
Files 68 66 -2 Lines 34796 40933 +6137 ==========================================
+ Hits 31458 37708 +6250 + Misses 3338 3225 -113 
Impacted FilesCoverage Δ
lightning/src/ln/channelmanager.rs89.20% <50.00%> (+5.68%)⬆️
lightning/src/chain/channelmonitor.rs91.28% <100.00%> (+0.33%)⬆️
lightning/src/ln/chanmon_update_fail_tests.rs98.79% <100.00%> (+1.14%)⬆️
lightning/src/chain/mod.rs58.82% <0.00%> (-2.29%)⬇️
lightning-background-processor/src/lib.rs93.75% <0.00%> (-0.49%)⬇️
lightning/src/chain/onchaintx.rs94.27% <0.00%> (-0.48%)⬇️
lightning/src/ln/onion_utils.rs94.91% <0.00%> (-0.46%)⬇️
lightning-invoice/src/utils.rs84.09% <0.00%> (-0.18%)⬇️
lightning-persister/src/lib.rs94.21% <0.00%> (-0.10%)⬇️
lightning/src/ln/payment_tests.rs98.75% <0.00%> (-0.09%)⬇️
... and 23 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 107c6c7...f99da12. Read the comment docs.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from bd79c03 to e6966ceCompareOctober 5, 2021 06:02
Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment on lines +150 to +151
/// or, e.g. we've moved on to a different watchtower and cannot update with all watchtowers
/// that were previously informed of this channel).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Trying to parse this watchtower case -- in this situation, we're switching watchtowers, but have no way of contacting the old watchtower to just delete our data, therefore the channel needs to close via PermanentFailure?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Yea, but honestly its confusing, I replaced it with a better example.

Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment on lines +167 to +168
/// [`ChannelMonitor::get_latest_holder_commitment_txn`] once you've safely ensured no further
/// off-chain updates to the channel can occur.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Checking -- "ensure no further off-chain updates can occur" == "applied the final monitor update mentioned on L170"? Or?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

It means "no ChannelManager still knows about this channel", I reworded it.

Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/chain/channelmonitor.rs Outdated
@valentinewallace

Copy link
Copy Markdown
Contributor

Bit confused how to implement these new requirements in the sample. I guess we now need a proxy layer implementing Persist between the FilesystemPersister and ChainMonitor?

@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Bit confused how to implement these new requirements in the sample.

I think we, in general, need to think hard about how we handle disk failures in the sample/lightning-persister. If the disk gets unplugged while we're running, we should just shut down and move on, but there's no way to get an error out of the Persister. The way the code works today is unsafe in this condition - if the disk gets unplugged, we'll broadcast the latest state and keep going, if the user then plugs the disk back in and restarts we may revoke the now-broadcasted state.

I think the end result for the sample needs to be a "really definitely broadcast the latest state from the channelmonitor for channel X" command, which is marked unsafe. We should probably also have a programatic way to detect monitors in this condition and not just rely on the logs in ChannelManager deserialization, I'll work on that bit here.

@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Rebased on upstream. Need to make sure all the updated content in the failure enum docs is still kept and should rename temporaryfailure -> asyncpersist or so.

@TheBlueMatt
TheBlueMatt marked this pull request as draft October 20, 2021 01:34
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 6ad8583 to f99da12CompareOctober 20, 2021 02:55
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from f99da12 to 2572ac5CompareJuly 17, 2022 20:11
@codecov-commenter

codecov-commenter commented Jul 17, 2022

Copy link
Copy Markdown

Codecov Report

Base: 90.78% // Head: 91.21% // Increases project coverage by +0.43% 🎉

Coverage data is based on head (74745cb) compared to base (48d21ba).
Patch coverage: 89.09% of modified lines in pull request are covered.

❗ Current head 74745cb differs from pull request most recent head 52934e0. Consider uploading reports for the commit 52934e0 to get more accurate results

Additional details and impacted files
@@ Coverage Diff @@## main #1106 +/- ##
==========================================
+ Coverage 90.78% 91.21% +0.43% 
==========================================
Files 86 87 +1 Lines 46631 50794 +4163 Branches 46631 50794 +4163 ==========================================
+ Hits 42335 46333 +3998 - Misses 4296 4461 +165 
Impacted FilesCoverage Δ
lightning/src/chain/mod.rs68.18% <ø> (ø)
lightning/src/util/errors.rs72.22% <0.00%> (ø)
lightning/src/chain/channelmonitor.rs91.21% <75.00%> (+<0.01%)⬆️
lightning/src/ln/channelmanager.rs88.11% <83.23%> (+3.05%)⬆️
lightning-invoice/src/payment.rs93.13% <87.50%> (+2.26%)⬆️
lightning/src/util/persist.rs95.23% <87.50%> (+0.50%)⬆️
lightning/src/chain/chainmonitor.rs97.78% <88.46%> (-0.28%)⬇️
lightning-persister/src/lib.rs93.45% <100.00%> (ø)
lightning/src/ln/chanmon_update_fail_tests.rs97.72% <100.00%> (+<0.01%)⬆️
lightning/src/ln/channel.rs88.66% <100.00%> (ø)
... and 17 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 2572ac5 to 0fe1f28CompareJuly 18, 2022 01:59
@TheBlueMatt
TheBlueMatt marked this pull request as ready for review July 18, 2022 01:59
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Finally getting back to the monitor work, finally updated this to include a monitor update failure type rework, but no further functional changes.

Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/chain/chainmonitor.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/chanmon_update_fail_tests.rs
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from dc2888a to a9f656aCompareAugust 16, 2022 22:32
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Oops, somehow I totally lost track of this PR having a pending review, so sorry about that! Rebased on latest git and addressed feedback.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from a9f656a to e3347f1CompareAugust 16, 2022 22:39
See doc updates for more info on the edge case this prevents, and
there isn't really a strong reason why we would need to broadcast
the latest state immediately. Specifically, in the case of HTLC
claims (the most important reason to ensure we have state on chain
if it cannot be persisted), we will still force-close if there are
HTLCs which need claiming and are going to expire.
Surprisingly, there were no tests which failed as a result of this
change, but a new one has been added.
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 86a5a28 to 24f8279CompareSeptember 15, 2022 19:38
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Rebased to address conflicts and went ahead and squashed as its been a while.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 24f8279 to 4825a96CompareSeptember 15, 2022 19:52
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
// a monitor that we didn't manage to store (and that we don't care about - we
// don't respond with the funding_signed so the channel can never go on chain).
let (_monitor_update, failed_htlcs) = chan.force_shutdown(true);
let (_monitor_update, failed_htlcs) = chan.force_shutdown(false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like there is not test for this scenario. All tests pass when reverting to true.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Its unobservable - note that the monitor_update, which is the place the bool is propagated to, is drop'd.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 4825a96 to c0bb4f4CompareSeptember 25, 2022 07:50
Comment threadlightning/src/util/errors.rs Outdated
Comment on lines 49 to 50
/// An attempt to call watch/update_channel returned an Err (ie you did this!), causing the
/// attempted action to fail.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you update the docs since it's no longer from an Err? Would be good to link to the corresponding method docs and maybe phrase the docs to be a little clearer, too.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1, also a bit weird that it's still considered an APIError variant, but that might require some more brainstorming than a simple rename/doc update.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Yea, did update the docs, but agree - this shouldnt really be an error at all, it should just be Ok. I think there's only one(-ish) place where it Errs - sending payments - so it should be easy to do in a followup.

Comment on lines +223 to +228
/// When this is returned, [`ChannelManager`] will force-close the channel but *not* broadcast
/// our current commitment transaction. This avoids a dangerous case where a local disk failure
/// (e.g. the Linux-default remounting of the disk as read-only) causes [`PermanentFailure`]s
/// for all monitor updates. If we were to broadcast our latest commitment transaction and then
/// restart, we could end up reading a previous [`ChannelMonitor`] and [`ChannelManager`],
/// revoking our now-broadcasted state before seeing it confirm and losing all our funds.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

IIUC, while the user could choose not to broadcast the latest commitment transaction, which has been revoked, it could still be done by LDK if it contained any pending HTLCs that are expiring soon.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Indeed, somewhat of a separate issue that should be addressed in #1593

Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/util/errors.rs Outdated
Comment on lines 49 to 50
/// An attempt to call watch/update_channel returned an Err (ie you did this!), causing the
/// attempted action to fail.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1, also a bit weird that it's still considered an APIError variant, but that might require some more brainstorming than a simple rename/doc update.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from c0bb4f4 to 74745cbCompareSeptember 28, 2022 15:14

@wpaulinowpaulino left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, feel free to squash.

Comment threadlightning/src/ln/channel.rs Outdated
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 74745cb to fc18acbCompareSeptember 28, 2022 21:11

@jkczyzjkczyz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, please squash.

Comment threadlightning/src/util/errors.rs Outdated
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from fc18acb to 4ac5804CompareSeptember 29, 2022 09:02
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Addressed one more comment and squashed. Diff since last push:

$ git diff-tree -U1 fc18acb4 4ac58048
diff --git a/lightning/src/util/errors.rs b/lightning/src/util/errors.rs
index 83324eab6..ad6993542 100644
--- a/lightning/src/util/errors.rs+++ b/lightning/src/util/errors.rs@@ -48,7 +48,9 @@ pub enum APIError {
},
-	/// An attempt to call watch/update_channel returned a-	/// [`ChannelMonitorUpdateStatus::InProgress`] indicating the persistence of a monitor update-	/// is awaiting async resolution. Once it resolves the attempted action should complete-	/// automatically.+	/// An attempt to call [`chain::Watch::watch_channel`]/[`chain::Watch::update_channel`]+	/// returned a [`ChannelMonitorUpdateStatus::InProgress`] indicating the persistence of a+	/// monitor update is awaiting async resolution. Once it resolves the attempted action should+	/// complete automatically.
///
+	/// [`chain::Watch::watch_channel`]: crate::chain::Watch::watch_channel+	/// [`chain::Watch::update_channel`]: crate::chain::Watch::update_channel
/// [`ChannelMonitorUpdateStatus::InProgress`]: crate::chain::ChannelMonitorUpdateStatus::InProgress

Comment threadlightning/src/util/errors.rs
When a `chain::Watch` `ChannelMonitor` update method is called, the
user has three options:
(a) persist the monitor update immediately and return success,
(b) fail to persist the monitor update immediately and return
failure,
(c) return a flag indicating the monitor update is in progress and
will complete in the future.
(c) is rather harmless, and in some deployments should be expected
to be the return value for all monitor update calls, but currently
requires returning `Err(ChannelMonitorUpdateErr::TemporaryFailure)`
which isn't very descriptive and sounds scarier than it is.
Instead, here, we change the return type used to be a single enum
(rather than a Result) and rename `TemporaryFailure`
`UpdateInProgress`.
If we receive a monitor event from a forwarded-to channel which
contains a preimage for an HTLC, we have to propogate that preimage
back to the forwarded-from channel monitor. However, once we have
that update, we're running in a relatively unsafe state - we have
the preimage in memory, but if we were to crash the forwarded-to
channel monitor will not regenerate the update with the preimage
for us. If we haven't managed to write the monitor update to the
forwarded-from channel by that point, we've lost the preimage, and,
thus, money!
This much more accurately represents the error, indicating that a
monitor update is in progress asynchronously and may complete at a
later time.
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 4ac5804 to 52934e0CompareSeptember 29, 2022 20:28
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Oops, sorry, reordered the missing hunk into the right commit, no ultimate diff, though.

@TheBlueMatt
TheBlueMatt merged commit 7544030 into lightningdevkit:mainSep 29, 2022
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

7 participants

@TheBlueMatt@valentinewallace@codecov-commenter@G8XSU@jkczyz@wpaulino@tnull
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); Do not broadcast commitment txn on Permanent mon update failure by TheBlueMatt · Pull Request #1106 · lightningdevkit/rust-lightning · GitHub
Skip to content

Do not broadcast commitment txn on Permanent mon update failure - #1106

Merged
TheBlueMatt merged 7 commits into
lightningdevkit:mainfrom
TheBlueMatt:2021-10-no-perm-err-broadcast
Sep 29, 2022
Merged

Do not broadcast commitment txn on Permanent mon update failure#1106
TheBlueMatt merged 7 commits into
lightningdevkit:mainfrom
TheBlueMatt:2021-10-no-perm-err-broadcast

Conversation

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

See doc updates for more info on the edge case this prevents, and
there isn't really a strong reason why we would need to broadcast
the latest state immediately. Specifically, in the case of HTLC
claims (the most important reason to ensure we have state on chain
if it cannot be persisted), we will still force-close if there are
HTLCs which need claiming and are going to expire.

Surprisingly, there were no tests which failed as a result of this
change, but a new one has been added.

@codecov

codecovBot commented Oct 5, 2021

Copy link
Copy Markdown

Codecov Report

Merging #1106 (5f848de) into main (107c6c7) will increase coverage by 1.71%.
The diff coverage is 90.00%.

❗ Current head 5f848de differs from pull request most recent head f99da12. Consider uploading reports for the commit f99da12 to get more accurate results
Impacted file tree graph

@@ Coverage Diff @@## main #1106 +/- ##
==========================================
+ Coverage 90.40% 92.12% +1.71% 
==========================================
Files 68 66 -2 Lines 34796 40933 +6137 ==========================================
+ Hits 31458 37708 +6250 + Misses 3338 3225 -113 
Impacted FilesCoverage Δ
lightning/src/ln/channelmanager.rs89.20% <50.00%> (+5.68%)⬆️
lightning/src/chain/channelmonitor.rs91.28% <100.00%> (+0.33%)⬆️
lightning/src/ln/chanmon_update_fail_tests.rs98.79% <100.00%> (+1.14%)⬆️
lightning/src/chain/mod.rs58.82% <0.00%> (-2.29%)⬇️
lightning-background-processor/src/lib.rs93.75% <0.00%> (-0.49%)⬇️
lightning/src/chain/onchaintx.rs94.27% <0.00%> (-0.48%)⬇️
lightning/src/ln/onion_utils.rs94.91% <0.00%> (-0.46%)⬇️
lightning-invoice/src/utils.rs84.09% <0.00%> (-0.18%)⬇️
lightning-persister/src/lib.rs94.21% <0.00%> (-0.10%)⬇️
lightning/src/ln/payment_tests.rs98.75% <0.00%> (-0.09%)⬇️
... and 23 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 107c6c7...f99da12. Read the comment docs.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from bd79c03 to e6966ceCompareOctober 5, 2021 06:02
Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment on lines +150 to +151
/// or, e.g. we've moved on to a different watchtower and cannot update with all watchtowers
/// that were previously informed of this channel).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Trying to parse this watchtower case -- in this situation, we're switching watchtowers, but have no way of contacting the old watchtower to just delete our data, therefore the channel needs to close via PermanentFailure?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Yea, but honestly its confusing, I replaced it with a better example.

Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment on lines +167 to +168
/// [`ChannelMonitor::get_latest_holder_commitment_txn`] once you've safely ensured no further
/// off-chain updates to the channel can occur.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Checking -- "ensure no further off-chain updates can occur" == "applied the final monitor update mentioned on L170"? Or?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

It means "no ChannelManager still knows about this channel", I reworded it.

Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/chain/channelmonitor.rs Outdated
@valentinewallace

Copy link
Copy Markdown
Contributor

Bit confused how to implement these new requirements in the sample. I guess we now need a proxy layer implementing Persist between the FilesystemPersister and ChainMonitor?

@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Bit confused how to implement these new requirements in the sample.

I think we, in general, need to think hard about how we handle disk failures in the sample/lightning-persister. If the disk gets unplugged while we're running, we should just shut down and move on, but there's no way to get an error out of the Persister. The way the code works today is unsafe in this condition - if the disk gets unplugged, we'll broadcast the latest state and keep going, if the user then plugs the disk back in and restarts we may revoke the now-broadcasted state.

I think the end result for the sample needs to be a "really definitely broadcast the latest state from the channelmonitor for channel X" command, which is marked unsafe. We should probably also have a programatic way to detect monitors in this condition and not just rely on the logs in ChannelManager deserialization, I'll work on that bit here.

@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Rebased on upstream. Need to make sure all the updated content in the failure enum docs is still kept and should rename temporaryfailure -> asyncpersist or so.

@TheBlueMatt
TheBlueMatt marked this pull request as draft October 20, 2021 01:34
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 6ad8583 to f99da12CompareOctober 20, 2021 02:55
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from f99da12 to 2572ac5CompareJuly 17, 2022 20:11
@codecov-commenter

codecov-commenter commented Jul 17, 2022

Copy link
Copy Markdown

Codecov Report

Base: 90.78% // Head: 91.21% // Increases project coverage by +0.43% 🎉

Coverage data is based on head (74745cb) compared to base (48d21ba).
Patch coverage: 89.09% of modified lines in pull request are covered.

❗ Current head 74745cb differs from pull request most recent head 52934e0. Consider uploading reports for the commit 52934e0 to get more accurate results

Additional details and impacted files
@@ Coverage Diff @@## main #1106 +/- ##
==========================================
+ Coverage 90.78% 91.21% +0.43% 
==========================================
Files 86 87 +1 Lines 46631 50794 +4163 Branches 46631 50794 +4163 ==========================================
+ Hits 42335 46333 +3998 - Misses 4296 4461 +165 
Impacted FilesCoverage Δ
lightning/src/chain/mod.rs68.18% <ø> (ø)
lightning/src/util/errors.rs72.22% <0.00%> (ø)
lightning/src/chain/channelmonitor.rs91.21% <75.00%> (+<0.01%)⬆️
lightning/src/ln/channelmanager.rs88.11% <83.23%> (+3.05%)⬆️
lightning-invoice/src/payment.rs93.13% <87.50%> (+2.26%)⬆️
lightning/src/util/persist.rs95.23% <87.50%> (+0.50%)⬆️
lightning/src/chain/chainmonitor.rs97.78% <88.46%> (-0.28%)⬇️
lightning-persister/src/lib.rs93.45% <100.00%> (ø)
lightning/src/ln/chanmon_update_fail_tests.rs97.72% <100.00%> (+<0.01%)⬆️
lightning/src/ln/channel.rs88.66% <100.00%> (ø)
... and 17 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 2572ac5 to 0fe1f28CompareJuly 18, 2022 01:59
@TheBlueMatt
TheBlueMatt marked this pull request as ready for review July 18, 2022 01:59
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Finally getting back to the monitor work, finally updated this to include a monitor update failure type rework, but no further functional changes.

Comment threadlightning/src/chain/channelmonitor.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/chain/chainmonitor.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/chanmon_update_fail_tests.rs
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from dc2888a to a9f656aCompareAugust 16, 2022 22:32
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Oops, somehow I totally lost track of this PR having a pending review, so sorry about that! Rebased on latest git and addressed feedback.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from a9f656a to e3347f1CompareAugust 16, 2022 22:39
See doc updates for more info on the edge case this prevents, and
there isn't really a strong reason why we would need to broadcast
the latest state immediately. Specifically, in the case of HTLC
claims (the most important reason to ensure we have state on chain
if it cannot be persisted), we will still force-close if there are
HTLCs which need claiming and are going to expire.
Surprisingly, there were no tests which failed as a result of this
change, but a new one has been added.
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 86a5a28 to 24f8279CompareSeptember 15, 2022 19:38
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Rebased to address conflicts and went ahead and squashed as its been a while.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 24f8279 to 4825a96CompareSeptember 15, 2022 19:52
Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
// a monitor that we didn't manage to store (and that we don't care about - we
// don't respond with the funding_signed so the channel can never go on chain).
let (_monitor_update, failed_htlcs) = chan.force_shutdown(true);
let (_monitor_update, failed_htlcs) = chan.force_shutdown(false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like there is not test for this scenario. All tests pass when reverting to true.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Its unobservable - note that the monitor_update, which is the place the bool is propagated to, is drop'd.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 4825a96 to c0bb4f4CompareSeptember 25, 2022 07:50
Comment threadlightning/src/util/errors.rs Outdated
Comment on lines 49 to 50
/// An attempt to call watch/update_channel returned an Err (ie you did this!), causing the
/// attempted action to fail.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you update the docs since it's no longer from an Err? Would be good to link to the corresponding method docs and maybe phrase the docs to be a little clearer, too.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1, also a bit weird that it's still considered an APIError variant, but that might require some more brainstorming than a simple rename/doc update.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Yea, did update the docs, but agree - this shouldnt really be an error at all, it should just be Ok. I think there's only one(-ish) place where it Errs - sending payments - so it should be easy to do in a followup.

Comment on lines +223 to +228
/// When this is returned, [`ChannelManager`] will force-close the channel but *not* broadcast
/// our current commitment transaction. This avoids a dangerous case where a local disk failure
/// (e.g. the Linux-default remounting of the disk as read-only) causes [`PermanentFailure`]s
/// for all monitor updates. If we were to broadcast our latest commitment transaction and then
/// restart, we could end up reading a previous [`ChannelMonitor`] and [`ChannelManager`],
/// revoking our now-broadcasted state before seeing it confirm and losing all our funds.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

IIUC, while the user could choose not to broadcast the latest commitment transaction, which has been revoked, it could still be done by LDK if it contained any pending HTLCs that are expiring soon.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Indeed, somewhat of a separate issue that should be addressed in #1593

Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/util/errors.rs Outdated
Comment on lines 49 to 50
/// An attempt to call watch/update_channel returned an Err (ie you did this!), causing the
/// attempted action to fail.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1, also a bit weird that it's still considered an APIError variant, but that might require some more brainstorming than a simple rename/doc update.

@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from c0bb4f4 to 74745cbCompareSeptember 28, 2022 15:14

@wpaulinowpaulino left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, feel free to squash.

Comment threadlightning/src/ln/channel.rs Outdated
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 74745cb to fc18acbCompareSeptember 28, 2022 21:11

@jkczyzjkczyz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, please squash.

Comment threadlightning/src/util/errors.rs Outdated
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from fc18acb to 4ac5804CompareSeptember 29, 2022 09:02
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Addressed one more comment and squashed. Diff since last push:

$ git diff-tree -U1 fc18acb4 4ac58048
diff --git a/lightning/src/util/errors.rs b/lightning/src/util/errors.rs
index 83324eab6..ad6993542 100644
--- a/lightning/src/util/errors.rs+++ b/lightning/src/util/errors.rs@@ -48,7 +48,9 @@ pub enum APIError {
},
-	/// An attempt to call watch/update_channel returned a-	/// [`ChannelMonitorUpdateStatus::InProgress`] indicating the persistence of a monitor update-	/// is awaiting async resolution. Once it resolves the attempted action should complete-	/// automatically.+	/// An attempt to call [`chain::Watch::watch_channel`]/[`chain::Watch::update_channel`]+	/// returned a [`ChannelMonitorUpdateStatus::InProgress`] indicating the persistence of a+	/// monitor update is awaiting async resolution. Once it resolves the attempted action should+	/// complete automatically.
///
+	/// [`chain::Watch::watch_channel`]: crate::chain::Watch::watch_channel+	/// [`chain::Watch::update_channel`]: crate::chain::Watch::update_channel
/// [`ChannelMonitorUpdateStatus::InProgress`]: crate::chain::ChannelMonitorUpdateStatus::InProgress

Comment threadlightning/src/util/errors.rs
When a `chain::Watch` `ChannelMonitor` update method is called, the
user has three options:
(a) persist the monitor update immediately and return success,
(b) fail to persist the monitor update immediately and return
failure,
(c) return a flag indicating the monitor update is in progress and
will complete in the future.
(c) is rather harmless, and in some deployments should be expected
to be the return value for all monitor update calls, but currently
requires returning `Err(ChannelMonitorUpdateErr::TemporaryFailure)`
which isn't very descriptive and sounds scarier than it is.
Instead, here, we change the return type used to be a single enum
(rather than a Result) and rename `TemporaryFailure`
`UpdateInProgress`.
If we receive a monitor event from a forwarded-to channel which
contains a preimage for an HTLC, we have to propogate that preimage
back to the forwarded-from channel monitor. However, once we have
that update, we're running in a relatively unsafe state - we have
the preimage in memory, but if we were to crash the forwarded-to
channel monitor will not regenerate the update with the preimage
for us. If we haven't managed to write the monitor update to the
forwarded-from channel by that point, we've lost the preimage, and,
thus, money!
This much more accurately represents the error, indicating that a
monitor update is in progress asynchronously and may complete at a
later time.
@TheBlueMatt
TheBlueMattforce-pushed the 2021-10-no-perm-err-broadcast branch from 4ac5804 to 52934e0CompareSeptember 29, 2022 20:28
@TheBlueMatt

Copy link
Copy Markdown
CollaboratorAuthor

Oops, sorry, reordered the missing hunk into the right commit, no ultimate diff, though.

@TheBlueMatt
TheBlueMatt merged commit 7544030 into lightningdevkit:mainSep 29, 2022
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

7 participants

@TheBlueMatt@valentinewallace@codecov-commenter@G8XSU@jkczyz@wpaulino@tnull