Skip to content

Support opening anchor channels and test end-to-end unilateral close - #1860

Merged
TheBlueMatt merged 6 commits into
lightningdevkit:mainfrom
wpaulino:open-channel-anchors-support
Jan 19, 2023
Merged

Support opening anchor channels and test end-to-end unilateral close#1860
TheBlueMatt merged 6 commits into
lightningdevkit:mainfrom
wpaulino:open-channel-anchors-support

Conversation

@wpaulino

Copy link
Copy Markdown
Contributor

This PR introduces support for the anchors feature bit and opening/accepting anchor channels. It also includes an end-to-end test of the unilateral channel close flow, consuming the recently added anchor events and attaching fees to transactions when necessary.

Depends on #1825.

@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Currently, it doesn't expose any way to automatically reject inbound anchor channels (e.g., low onchain funds) and it doesn't force users to manually accept them either (like with zero conf). I'd imagine we'd at least want to implement the latter before considering merging this.

@ariard

Copy link
Copy Markdown

Note, there was a security issue affecting LND/Core Lightning anchor output support few months ago: https://lists.linuxfoundation.org/pipermail/lightning-dev/2022-April/003561.html. Briefly, anchor output relaxes the sighash flags for second-stage HTLC transactions from SIGHASH_ALL to SIGHASH_SINGLE | SIGHASH_ANYONECANPAY, allowing your counterparty to combine them in a single-batch. If your implementation was doing assumptions on the inputs/outputs size or order to parse those second-stage HTLC transactions in case of revoked state, a malicious party could extract funds.

With the current state of our parsing logic in check_spend_counterparty_htlc, I think we're encumbering the same exact security issue due to the following checks (1231907, channelmonitors, L2644) and we should adapt our logic in consequence:

 if tx.input.len() != 1 || tx.output.len() != 1 || tx.input[0].witness.len() != 5 {

@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Thanks for the note @ariard, I've addressed this in #1825 (commit aaf458d). I definitely missed this, but I plan to follow up with a series of tests that I imagine would've caught this. For now, this PR will just focus on an initial end-to-end test to make sure the happy path works.

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

Let's make sure we have test coverage that would have caught #1825 (comment)

@ariard

Copy link
Copy Markdown

More test coverage requested from #1825: #1825 (comment)

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

Needs rebase now 🎉

@ariard

Copy link
Copy Markdown

And more test coverage: #1825 (comment)

@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Working on the aggregated revoked HTLC test before I push another update to this. There are definitely several more cases we should be testing, but in the spirit of keeping PRs relatively small, this PR will just focus on that edge case and the happy case.

@ariardariard left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Left few reviews comments.

There are definitely several more cases we should be testing, but in the spirit of keeping PRs relatively small, this PR will just focus on that edge case and the happy case.

We can even move the aggregated revoked HTLC test in its own PR, bundled with few others from #1825. I think it's already good if we focus just on all the cases of features bit interpretation in both directions here, including unsafe option_anchor_outputs attempts.

Comment threadlightning/src/ln/features.rs Outdated
Comment threadlightning/src/ln/features.rs
Comment threadlightning/src/util/config.rs
Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channel.rs
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 1231907 to 0d7b5faCompareDecember 16, 2022 23:16
@wpaulino
wpaulino marked this pull request as ready for review December 16, 2022 23:17
@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Rebased and addressed @ariard's comments. This now depends on #1922.

Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channel.rs
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
/// [`DecodeError::InvalidValue`]: crate::ln::msgs::DecodeError::InvalidValue
/// [`SIGHASH_SINGLE + update_fee Considered Harmful`]: https://lists.linuxfoundation.org/pipermail/lightning-dev/2020-September/002796.html
pub negotiate_anchors_zero_fee_htlc_tx: bool,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we want a separate flag for "dont accept anchor channels" in addition to "open outbound channels"?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Possibly. We'll also want to base our acceptance of anchor channels on our availability of fee UTXOs, which will come in a follow-up, so we could choose to do it all there.

Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/chain/onchaintx.rs Outdated
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 0d7b5fa to 523c412CompareDecember 19, 2022 19:24
@codecov-commenter

codecov-commenter commented Dec 19, 2022

Copy link
Copy Markdown

Codecov Report

Base: 90.69% // Head: 90.69% // Decreases project coverage by -0.01%⚠️

Coverage data is based on head (27a74fe) compared to base (01fe9ba).
Patch coverage: 69.00% of modified lines in pull request are covered.

❗ Current head 27a74fe differs from pull request most recent head 202ff7f. Consider uploading reports for the commit 202ff7f to get more accurate results

Additional details and impacted files
@@ Coverage Diff @@## main #1860 +/- ##
==========================================
- Coverage 90.69% 90.69% -0.01% 
==========================================
Files 97 95 -2 Lines 50577 50074 -503 Branches 50577 50074 -503 ==========================================
- Hits 45870 45413 -457 + Misses 4707 4661 -46 
Impacted FilesCoverage Δ
lightning/src/ln/monitor_tests.rs99.56% <ø> (ø)
lightning/src/util/config.rs65.90% <0.00%> (ø)
lightning/src/util/events.rs29.35% <ø> (-1.15%)⬇️
lightning/src/ln/functional_test_utils.rs93.44% <60.00%> (+1.96%)⬆️
lightning/src/ln/channel.rs88.45% <62.06%> (-0.32%)⬇️
lightning/src/ln/features.rs98.46% <80.00%> (-1.23%)⬇️
lightning/src/ln/channelmanager.rs86.72% <100.00%> (-0.39%)⬇️
lightning/src/ln/functional_tests.rs96.90% <100.00%> (-0.01%)⬇️
lightning/src/util/macro_logger.rs86.15% <100.00%> (ø)
lightning/src/util/fairrwlock.rs85.71% <0.00%> (-2.53%)⬇️
... and 45 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.

@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 523c412 to 27a74feCompareDecember 19, 2022 22:09
Comment threadlightning/src/ln/features.rs Outdated
Comment threadlightning/src/ln/channel.rs Outdated
F::Target: FeeEstimator,
{
let opt_anchors = false; // TODO - should be based on features
#[cfg(anchors)]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is only used for "is it even possible to build a channel with this amount". Should we just drop this and always pass anchors (since its a slightly higher commitment tx fee)? Otherwise I'm worried about making decisions based on the anchor flag which isn't really right, its just the initial state.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

It's also setting the value in ChannelTransactionParameters. That of course won't be locked in until we receive an accept_channel with the same channel_type we extended. Are you also suggesting we delay setting that value in ChannelTransactionParameters until then?

Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 27a74fe to a88d650CompareDecember 22, 2022 19:51
Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/util/config.rs
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch 2 times, most recently from 9cf8351 to 404f3d1CompareJanuary 4, 2023 00:35
Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/ln/features.rs
Comment threadlightning/src/ln/monitor_tests.rs

@TheBlueMattTheBlueMatt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. Needs a pretty trivial rebase to drop the first commit.

Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/ln/channel.rs

@ariardariard left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Otherwise test coverage sounds good.


#[cfg(anchors)]
#[test]
fn test_supports_anchors_zero_htlc_tx_fee() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I don't know if the flow that matters, though when our negotiate_anchors_zero_fee_htlc_tx=true and their init features option_anchors_zero_fee_htlc_tx=false doesn't seem cover:

diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs
index 8cf5e54f..b552b61b 100644
--- a/lightning/src/ln/channel.rs
+++ b/lightning/src/ln/channel.rs
@@ -892,10 +892,8 @@ impl<Signer: Sign> Channel<Signer> {
// Optionally, if the user would like to negotiate the `anchors_zero_fee_htlc_tx` option, we
// set it now. If they don't understand it, we'll fall back to our default of
// `only_static_remotekey`.
- #[cfg(anchors)]
{ // Attributes are not allowed on if expressions on our current MSRV of 1.41.
- if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx &&
- their_features.supports_anchors_zero_fee_htlc_tx() {
+ if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx {
ret.set_anchors_zero_fee_htlc_tx_required();
}
}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Should be covered now.

// We support opening a few different types of channels. Try removing our additional
// features one by one until we've either arrived at our default or the counterparty has
// accepted one.
if self.channel_type.supports_anchors_zero_fee_htlc_tx() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do we have test coverage for handle_error once we got an error message due to rejection of channel type ?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Should be covered now.

@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 7e7abb5 to 6dbcde5CompareJanuary 17, 2023 23:03
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 6dbcde5 to 202ff7fCompareJanuary 17, 2023 23:04
@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Would appreciate a thorough pass from reviewers as it's been a while since the previous review iteration.

TheBlueMatt
TheBlueMatt previously approved these changes Jan 18, 2023

@TheBlueMattTheBlueMatt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, comments can be in a followup or the comment request ignored.

// We support opening a few different types of channels. Try removing our additional
// features one by one until we've either arrived at our default or the counterparty has
// accepted one.
if self.channel_type.supports_anchors_zero_fee_htlc_tx() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

May be worth adding a comment that the ordering here would mean that we'd never negotiate anchor with a peer that supports anchor but not scid privacy, except that we check the peer's init flags above so in general this code should really only apply for a peer that is setting init flags but not actually supporting the channel type, which should be very rare.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

May be worth adding a comment that the ordering here would mean that we'd never negotiate anchor with a peer that supports anchor but not scid privacy

Comment added.

this code should really only apply for a peer that is setting init flags but not actually supporting the channel type, which should be very rare

It won't be too rare with anchors if the inbound node doesn't have enough onchain funds to bump their commitments. They'll still advertise the feature, but reject the channel.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sure, sorry, my point was if we didn't do the check initially we'd never negotiate anchors for lots of old lnd nodes - because anchors predates the scid privacy stuff we'd always end up with no features. As long as we check their features, we're safe from that.

if self.channel_type.supports_anchors_zero_fee_htlc_tx() {
self.channel_type.clear_anchors_zero_fee_htlc_tx();
assert!(self.channel_transaction_parameters.opt_non_zero_fee_anchors.is_none());
self.channel_transaction_parameters.opt_anchors = None;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Given we're only supporting anchors with channel type negotiation, we should probably just drop opt_anchors entirely and do channel_type.supports... instead? Can come in a followup though since its a new change...I really do hate having duplicate state tracking the same thing :)

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I don't think we can drop ChannelTransactionParamerters.opt_anchors since signers rely on it in provide_channel_parameters.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We can regenerate it for them, though, rather than storing it explicitly in Channel and in its serialized form.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Ah, I'll look into this later then.

Comment threadlightning/src/ln/monitor_tests.rs Outdated
Comment threadlightning/src/ln/channel.rs
arik-so
arik-so previously approved these changes Jan 18, 2023
@wpaulino
wpaulino dismissed stale reviews from arik-so and TheBlueMatt via 660165cJanuary 18, 2023 22:49
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 202ff7f to 660165cCompareJanuary 18, 2023 22:49
/// If set, we attempt to negotiate the `anchors_zero_fee_htlc_tx`option for outbound channels.
///
/// If this option is set, channels may be created that will not be readable by LDK versions
/// prior to 0.0.114, causing [`ChannelManager`]'s read method to return a

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reminder that we may need to update this when we make anchor not cfg-gated anymore. Would be nice if we didn't and that happens in 0.0.114, but if not, that's okay too.

@TheBlueMatt
TheBlueMatt merged commit 50d1260 into lightningdevkit:mainJan 19, 2023
@wpaulino
wpaulino deleted the open-channel-anchors-support branch January 19, 2023 01:03
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@wpaulino@ariard@TheBlueMatt@codecov-commenter@arik-so
, '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" + '
Support opening anchor channels and test end-to-end unilateral close by wpaulino · Pull Request #1860 · lightningdevkit/rust-lightning · GitHub
Skip to content

Support opening anchor channels and test end-to-end unilateral close - #1860

Merged
TheBlueMatt merged 6 commits into
lightningdevkit:mainfrom
wpaulino:open-channel-anchors-support
Jan 19, 2023
Merged

Support opening anchor channels and test end-to-end unilateral close#1860
TheBlueMatt merged 6 commits into
lightningdevkit:mainfrom
wpaulino:open-channel-anchors-support

Conversation

@wpaulino

Copy link
Copy Markdown
Contributor

This PR introduces support for the anchors feature bit and opening/accepting anchor channels. It also includes an end-to-end test of the unilateral channel close flow, consuming the recently added anchor events and attaching fees to transactions when necessary.

Depends on #1825.

@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Currently, it doesn't expose any way to automatically reject inbound anchor channels (e.g., low onchain funds) and it doesn't force users to manually accept them either (like with zero conf). I'd imagine we'd at least want to implement the latter before considering merging this.

@ariard

Copy link
Copy Markdown

Note, there was a security issue affecting LND/Core Lightning anchor output support few months ago: https://lists.linuxfoundation.org/pipermail/lightning-dev/2022-April/003561.html. Briefly, anchor output relaxes the sighash flags for second-stage HTLC transactions from SIGHASH_ALL to SIGHASH_SINGLE | SIGHASH_ANYONECANPAY, allowing your counterparty to combine them in a single-batch. If your implementation was doing assumptions on the inputs/outputs size or order to parse those second-stage HTLC transactions in case of revoked state, a malicious party could extract funds.

With the current state of our parsing logic in check_spend_counterparty_htlc, I think we're encumbering the same exact security issue due to the following checks (1231907, channelmonitors, L2644) and we should adapt our logic in consequence:

 if tx.input.len() != 1 || tx.output.len() != 1 || tx.input[0].witness.len() != 5 {

@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Thanks for the note @ariard, I've addressed this in #1825 (commit aaf458d). I definitely missed this, but I plan to follow up with a series of tests that I imagine would've caught this. For now, this PR will just focus on an initial end-to-end test to make sure the happy path works.

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

Let's make sure we have test coverage that would have caught #1825 (comment)

@ariard

Copy link
Copy Markdown

More test coverage requested from #1825: #1825 (comment)

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

Needs rebase now 🎉

@ariard

Copy link
Copy Markdown

And more test coverage: #1825 (comment)

@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Working on the aggregated revoked HTLC test before I push another update to this. There are definitely several more cases we should be testing, but in the spirit of keeping PRs relatively small, this PR will just focus on that edge case and the happy case.

@ariardariard left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Left few reviews comments.

There are definitely several more cases we should be testing, but in the spirit of keeping PRs relatively small, this PR will just focus on that edge case and the happy case.

We can even move the aggregated revoked HTLC test in its own PR, bundled with few others from #1825. I think it's already good if we focus just on all the cases of features bit interpretation in both directions here, including unsafe option_anchor_outputs attempts.

Comment threadlightning/src/ln/features.rs Outdated
Comment threadlightning/src/ln/features.rs
Comment threadlightning/src/util/config.rs
Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channel.rs
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 1231907 to 0d7b5faCompareDecember 16, 2022 23:16
@wpaulino
wpaulino marked this pull request as ready for review December 16, 2022 23:17
@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Rebased and addressed @ariard's comments. This now depends on #1922.

Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channel.rs
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
/// [`DecodeError::InvalidValue`]: crate::ln::msgs::DecodeError::InvalidValue
/// [`SIGHASH_SINGLE + update_fee Considered Harmful`]: https://lists.linuxfoundation.org/pipermail/lightning-dev/2020-September/002796.html
pub negotiate_anchors_zero_fee_htlc_tx: bool,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we want a separate flag for "dont accept anchor channels" in addition to "open outbound channels"?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Possibly. We'll also want to base our acceptance of anchor channels on our availability of fee UTXOs, which will come in a follow-up, so we could choose to do it all there.

Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/chain/onchaintx.rs Outdated
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 0d7b5fa to 523c412CompareDecember 19, 2022 19:24
@codecov-commenter

codecov-commenter commented Dec 19, 2022

Copy link
Copy Markdown

Codecov Report

Base: 90.69% // Head: 90.69% // Decreases project coverage by -0.01%⚠️

Coverage data is based on head (27a74fe) compared to base (01fe9ba).
Patch coverage: 69.00% of modified lines in pull request are covered.

❗ Current head 27a74fe differs from pull request most recent head 202ff7f. Consider uploading reports for the commit 202ff7f to get more accurate results

Additional details and impacted files
@@ Coverage Diff @@## main #1860 +/- ##
==========================================
- Coverage 90.69% 90.69% -0.01% 
==========================================
Files 97 95 -2 Lines 50577 50074 -503 Branches 50577 50074 -503 ==========================================
- Hits 45870 45413 -457 + Misses 4707 4661 -46 
Impacted FilesCoverage Δ
lightning/src/ln/monitor_tests.rs99.56% <ø> (ø)
lightning/src/util/config.rs65.90% <0.00%> (ø)
lightning/src/util/events.rs29.35% <ø> (-1.15%)⬇️
lightning/src/ln/functional_test_utils.rs93.44% <60.00%> (+1.96%)⬆️
lightning/src/ln/channel.rs88.45% <62.06%> (-0.32%)⬇️
lightning/src/ln/features.rs98.46% <80.00%> (-1.23%)⬇️
lightning/src/ln/channelmanager.rs86.72% <100.00%> (-0.39%)⬇️
lightning/src/ln/functional_tests.rs96.90% <100.00%> (-0.01%)⬇️
lightning/src/util/macro_logger.rs86.15% <100.00%> (ø)
lightning/src/util/fairrwlock.rs85.71% <0.00%> (-2.53%)⬇️
... and 45 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.

@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 523c412 to 27a74feCompareDecember 19, 2022 22:09
Comment threadlightning/src/ln/features.rs Outdated
Comment threadlightning/src/ln/channel.rs Outdated
F::Target: FeeEstimator,
{
let opt_anchors = false; // TODO - should be based on features
#[cfg(anchors)]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is only used for "is it even possible to build a channel with this amount". Should we just drop this and always pass anchors (since its a slightly higher commitment tx fee)? Otherwise I'm worried about making decisions based on the anchor flag which isn't really right, its just the initial state.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

It's also setting the value in ChannelTransactionParameters. That of course won't be locked in until we receive an accept_channel with the same channel_type we extended. Are you also suggesting we delay setting that value in ChannelTransactionParameters until then?

Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 27a74fe to a88d650CompareDecember 22, 2022 19:51
Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/util/config.rs
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch 2 times, most recently from 9cf8351 to 404f3d1CompareJanuary 4, 2023 00:35
Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/ln/features.rs
Comment threadlightning/src/ln/monitor_tests.rs

@TheBlueMattTheBlueMatt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. Needs a pretty trivial rebase to drop the first commit.

Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/ln/channel.rs

@ariardariard left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Otherwise test coverage sounds good.


#[cfg(anchors)]
#[test]
fn test_supports_anchors_zero_htlc_tx_fee() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I don't know if the flow that matters, though when our negotiate_anchors_zero_fee_htlc_tx=true and their init features option_anchors_zero_fee_htlc_tx=false doesn't seem cover:

diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs
index 8cf5e54f..b552b61b 100644
--- a/lightning/src/ln/channel.rs
+++ b/lightning/src/ln/channel.rs
@@ -892,10 +892,8 @@ impl<Signer: Sign> Channel<Signer> {
// Optionally, if the user would like to negotiate the `anchors_zero_fee_htlc_tx` option, we
// set it now. If they don't understand it, we'll fall back to our default of
// `only_static_remotekey`.
- #[cfg(anchors)]
{ // Attributes are not allowed on if expressions on our current MSRV of 1.41.
- if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx &&
- their_features.supports_anchors_zero_fee_htlc_tx() {
+ if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx {
ret.set_anchors_zero_fee_htlc_tx_required();
}
}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Should be covered now.

// We support opening a few different types of channels. Try removing our additional
// features one by one until we've either arrived at our default or the counterparty has
// accepted one.
if self.channel_type.supports_anchors_zero_fee_htlc_tx() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do we have test coverage for handle_error once we got an error message due to rejection of channel type ?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Should be covered now.

@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 7e7abb5 to 6dbcde5CompareJanuary 17, 2023 23:03
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 6dbcde5 to 202ff7fCompareJanuary 17, 2023 23:04
@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Would appreciate a thorough pass from reviewers as it's been a while since the previous review iteration.

TheBlueMatt
TheBlueMatt previously approved these changes Jan 18, 2023

@TheBlueMattTheBlueMatt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, comments can be in a followup or the comment request ignored.

// We support opening a few different types of channels. Try removing our additional
// features one by one until we've either arrived at our default or the counterparty has
// accepted one.
if self.channel_type.supports_anchors_zero_fee_htlc_tx() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

May be worth adding a comment that the ordering here would mean that we'd never negotiate anchor with a peer that supports anchor but not scid privacy, except that we check the peer's init flags above so in general this code should really only apply for a peer that is setting init flags but not actually supporting the channel type, which should be very rare.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

May be worth adding a comment that the ordering here would mean that we'd never negotiate anchor with a peer that supports anchor but not scid privacy

Comment added.

this code should really only apply for a peer that is setting init flags but not actually supporting the channel type, which should be very rare

It won't be too rare with anchors if the inbound node doesn't have enough onchain funds to bump their commitments. They'll still advertise the feature, but reject the channel.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sure, sorry, my point was if we didn't do the check initially we'd never negotiate anchors for lots of old lnd nodes - because anchors predates the scid privacy stuff we'd always end up with no features. As long as we check their features, we're safe from that.

if self.channel_type.supports_anchors_zero_fee_htlc_tx() {
self.channel_type.clear_anchors_zero_fee_htlc_tx();
assert!(self.channel_transaction_parameters.opt_non_zero_fee_anchors.is_none());
self.channel_transaction_parameters.opt_anchors = None;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Given we're only supporting anchors with channel type negotiation, we should probably just drop opt_anchors entirely and do channel_type.supports... instead? Can come in a followup though since its a new change...I really do hate having duplicate state tracking the same thing :)

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I don't think we can drop ChannelTransactionParamerters.opt_anchors since signers rely on it in provide_channel_parameters.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We can regenerate it for them, though, rather than storing it explicitly in Channel and in its serialized form.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Ah, I'll look into this later then.

Comment threadlightning/src/ln/monitor_tests.rs Outdated
Comment threadlightning/src/ln/channel.rs
arik-so
arik-so previously approved these changes Jan 18, 2023
@wpaulino
wpaulino dismissed stale reviews from arik-so and TheBlueMatt via 660165cJanuary 18, 2023 22:49
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 202ff7f to 660165cCompareJanuary 18, 2023 22:49
/// If set, we attempt to negotiate the `anchors_zero_fee_htlc_tx`option for outbound channels.
///
/// If this option is set, channels may be created that will not be readable by LDK versions
/// prior to 0.0.114, causing [`ChannelManager`]'s read method to return a

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reminder that we may need to update this when we make anchor not cfg-gated anymore. Would be nice if we didn't and that happens in 0.0.114, but if not, that's okay too.

@TheBlueMatt
TheBlueMatt merged commit 50d1260 into lightningdevkit:mainJan 19, 2023
@wpaulino
wpaulino deleted the open-channel-anchors-support branch January 19, 2023 01:03
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@wpaulino@ariard@TheBlueMatt@codecov-commenter@arik-so
, '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('^' + ".*" + ' Support opening anchor channels and test end-to-end unilateral close by wpaulino · Pull Request #1860 · lightningdevkit/rust-lightning · GitHub
Skip to content

Support opening anchor channels and test end-to-end unilateral close - #1860

Merged
TheBlueMatt merged 6 commits into
lightningdevkit:mainfrom
wpaulino:open-channel-anchors-support
Jan 19, 2023
Merged

Support opening anchor channels and test end-to-end unilateral close#1860
TheBlueMatt merged 6 commits into
lightningdevkit:mainfrom
wpaulino:open-channel-anchors-support

Conversation

@wpaulino

Copy link
Copy Markdown
Contributor

This PR introduces support for the anchors feature bit and opening/accepting anchor channels. It also includes an end-to-end test of the unilateral channel close flow, consuming the recently added anchor events and attaching fees to transactions when necessary.

Depends on #1825.

@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Currently, it doesn't expose any way to automatically reject inbound anchor channels (e.g., low onchain funds) and it doesn't force users to manually accept them either (like with zero conf). I'd imagine we'd at least want to implement the latter before considering merging this.

@ariard

Copy link
Copy Markdown

Note, there was a security issue affecting LND/Core Lightning anchor output support few months ago: https://lists.linuxfoundation.org/pipermail/lightning-dev/2022-April/003561.html. Briefly, anchor output relaxes the sighash flags for second-stage HTLC transactions from SIGHASH_ALL to SIGHASH_SINGLE | SIGHASH_ANYONECANPAY, allowing your counterparty to combine them in a single-batch. If your implementation was doing assumptions on the inputs/outputs size or order to parse those second-stage HTLC transactions in case of revoked state, a malicious party could extract funds.

With the current state of our parsing logic in check_spend_counterparty_htlc, I think we're encumbering the same exact security issue due to the following checks (1231907, channelmonitors, L2644) and we should adapt our logic in consequence:

 if tx.input.len() != 1 || tx.output.len() != 1 || tx.input[0].witness.len() != 5 {

@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Thanks for the note @ariard, I've addressed this in #1825 (commit aaf458d). I definitely missed this, but I plan to follow up with a series of tests that I imagine would've caught this. For now, this PR will just focus on an initial end-to-end test to make sure the happy path works.

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

Let's make sure we have test coverage that would have caught #1825 (comment)

@ariard

Copy link
Copy Markdown

More test coverage requested from #1825: #1825 (comment)

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

Needs rebase now 🎉

@ariard

Copy link
Copy Markdown

And more test coverage: #1825 (comment)

@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Working on the aggregated revoked HTLC test before I push another update to this. There are definitely several more cases we should be testing, but in the spirit of keeping PRs relatively small, this PR will just focus on that edge case and the happy case.

@ariardariard left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Left few reviews comments.

There are definitely several more cases we should be testing, but in the spirit of keeping PRs relatively small, this PR will just focus on that edge case and the happy case.

We can even move the aggregated revoked HTLC test in its own PR, bundled with few others from #1825. I think it's already good if we focus just on all the cases of features bit interpretation in both directions here, including unsafe option_anchor_outputs attempts.

Comment threadlightning/src/ln/features.rs Outdated
Comment threadlightning/src/ln/features.rs
Comment threadlightning/src/util/config.rs
Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channel.rs
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 1231907 to 0d7b5faCompareDecember 16, 2022 23:16
@wpaulino
wpaulino marked this pull request as ready for review December 16, 2022 23:17
@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Rebased and addressed @ariard's comments. This now depends on #1922.

Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channel.rs
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
/// [`DecodeError::InvalidValue`]: crate::ln::msgs::DecodeError::InvalidValue
/// [`SIGHASH_SINGLE + update_fee Considered Harmful`]: https://lists.linuxfoundation.org/pipermail/lightning-dev/2020-September/002796.html
pub negotiate_anchors_zero_fee_htlc_tx: bool,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we want a separate flag for "dont accept anchor channels" in addition to "open outbound channels"?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Possibly. We'll also want to base our acceptance of anchor channels on our availability of fee UTXOs, which will come in a follow-up, so we could choose to do it all there.

Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/chain/onchaintx.rs Outdated
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 0d7b5fa to 523c412CompareDecember 19, 2022 19:24
@codecov-commenter

codecov-commenter commented Dec 19, 2022

Copy link
Copy Markdown

Codecov Report

Base: 90.69% // Head: 90.69% // Decreases project coverage by -0.01%⚠️

Coverage data is based on head (27a74fe) compared to base (01fe9ba).
Patch coverage: 69.00% of modified lines in pull request are covered.

❗ Current head 27a74fe differs from pull request most recent head 202ff7f. Consider uploading reports for the commit 202ff7f to get more accurate results

Additional details and impacted files
@@ Coverage Diff @@## main #1860 +/- ##
==========================================
- Coverage 90.69% 90.69% -0.01% 
==========================================
Files 97 95 -2 Lines 50577 50074 -503 Branches 50577 50074 -503 ==========================================
- Hits 45870 45413 -457 + Misses 4707 4661 -46 
Impacted FilesCoverage Δ
lightning/src/ln/monitor_tests.rs99.56% <ø> (ø)
lightning/src/util/config.rs65.90% <0.00%> (ø)
lightning/src/util/events.rs29.35% <ø> (-1.15%)⬇️
lightning/src/ln/functional_test_utils.rs93.44% <60.00%> (+1.96%)⬆️
lightning/src/ln/channel.rs88.45% <62.06%> (-0.32%)⬇️
lightning/src/ln/features.rs98.46% <80.00%> (-1.23%)⬇️
lightning/src/ln/channelmanager.rs86.72% <100.00%> (-0.39%)⬇️
lightning/src/ln/functional_tests.rs96.90% <100.00%> (-0.01%)⬇️
lightning/src/util/macro_logger.rs86.15% <100.00%> (ø)
lightning/src/util/fairrwlock.rs85.71% <0.00%> (-2.53%)⬇️
... and 45 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.

@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 523c412 to 27a74feCompareDecember 19, 2022 22:09
Comment threadlightning/src/ln/features.rs Outdated
Comment threadlightning/src/ln/channel.rs Outdated
F::Target: FeeEstimator,
{
let opt_anchors = false; // TODO - should be based on features
#[cfg(anchors)]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is only used for "is it even possible to build a channel with this amount". Should we just drop this and always pass anchors (since its a slightly higher commitment tx fee)? Otherwise I'm worried about making decisions based on the anchor flag which isn't really right, its just the initial state.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

It's also setting the value in ChannelTransactionParameters. That of course won't be locked in until we receive an accept_channel with the same channel_type we extended. Are you also suggesting we delay setting that value in ChannelTransactionParameters until then?

Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 27a74fe to a88d650CompareDecember 22, 2022 19:51
Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/util/config.rs
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch 2 times, most recently from 9cf8351 to 404f3d1CompareJanuary 4, 2023 00:35
Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/ln/features.rs
Comment threadlightning/src/ln/monitor_tests.rs

@TheBlueMattTheBlueMatt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. Needs a pretty trivial rebase to drop the first commit.

Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/ln/channel.rs

@ariardariard left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Otherwise test coverage sounds good.


#[cfg(anchors)]
#[test]
fn test_supports_anchors_zero_htlc_tx_fee() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I don't know if the flow that matters, though when our negotiate_anchors_zero_fee_htlc_tx=true and their init features option_anchors_zero_fee_htlc_tx=false doesn't seem cover:

diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs
index 8cf5e54f..b552b61b 100644
--- a/lightning/src/ln/channel.rs
+++ b/lightning/src/ln/channel.rs
@@ -892,10 +892,8 @@ impl<Signer: Sign> Channel<Signer> {
// Optionally, if the user would like to negotiate the `anchors_zero_fee_htlc_tx` option, we
// set it now. If they don't understand it, we'll fall back to our default of
// `only_static_remotekey`.
- #[cfg(anchors)]
{ // Attributes are not allowed on if expressions on our current MSRV of 1.41.
- if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx &&
- their_features.supports_anchors_zero_fee_htlc_tx() {
+ if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx {
ret.set_anchors_zero_fee_htlc_tx_required();
}
}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Should be covered now.

// We support opening a few different types of channels. Try removing our additional
// features one by one until we've either arrived at our default or the counterparty has
// accepted one.
if self.channel_type.supports_anchors_zero_fee_htlc_tx() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do we have test coverage for handle_error once we got an error message due to rejection of channel type ?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Should be covered now.

@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 7e7abb5 to 6dbcde5CompareJanuary 17, 2023 23:03
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 6dbcde5 to 202ff7fCompareJanuary 17, 2023 23:04
@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Would appreciate a thorough pass from reviewers as it's been a while since the previous review iteration.

TheBlueMatt
TheBlueMatt previously approved these changes Jan 18, 2023

@TheBlueMattTheBlueMatt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, comments can be in a followup or the comment request ignored.

// We support opening a few different types of channels. Try removing our additional
// features one by one until we've either arrived at our default or the counterparty has
// accepted one.
if self.channel_type.supports_anchors_zero_fee_htlc_tx() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

May be worth adding a comment that the ordering here would mean that we'd never negotiate anchor with a peer that supports anchor but not scid privacy, except that we check the peer's init flags above so in general this code should really only apply for a peer that is setting init flags but not actually supporting the channel type, which should be very rare.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

May be worth adding a comment that the ordering here would mean that we'd never negotiate anchor with a peer that supports anchor but not scid privacy

Comment added.

this code should really only apply for a peer that is setting init flags but not actually supporting the channel type, which should be very rare

It won't be too rare with anchors if the inbound node doesn't have enough onchain funds to bump their commitments. They'll still advertise the feature, but reject the channel.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sure, sorry, my point was if we didn't do the check initially we'd never negotiate anchors for lots of old lnd nodes - because anchors predates the scid privacy stuff we'd always end up with no features. As long as we check their features, we're safe from that.

if self.channel_type.supports_anchors_zero_fee_htlc_tx() {
self.channel_type.clear_anchors_zero_fee_htlc_tx();
assert!(self.channel_transaction_parameters.opt_non_zero_fee_anchors.is_none());
self.channel_transaction_parameters.opt_anchors = None;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Given we're only supporting anchors with channel type negotiation, we should probably just drop opt_anchors entirely and do channel_type.supports... instead? Can come in a followup though since its a new change...I really do hate having duplicate state tracking the same thing :)

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I don't think we can drop ChannelTransactionParamerters.opt_anchors since signers rely on it in provide_channel_parameters.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We can regenerate it for them, though, rather than storing it explicitly in Channel and in its serialized form.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Ah, I'll look into this later then.

Comment threadlightning/src/ln/monitor_tests.rs Outdated
Comment threadlightning/src/ln/channel.rs
arik-so
arik-so previously approved these changes Jan 18, 2023
@wpaulino
wpaulino dismissed stale reviews from arik-so and TheBlueMatt via 660165cJanuary 18, 2023 22:49
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 202ff7f to 660165cCompareJanuary 18, 2023 22:49
/// If set, we attempt to negotiate the `anchors_zero_fee_htlc_tx`option for outbound channels.
///
/// If this option is set, channels may be created that will not be readable by LDK versions
/// prior to 0.0.114, causing [`ChannelManager`]'s read method to return a

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reminder that we may need to update this when we make anchor not cfg-gated anymore. Would be nice if we didn't and that happens in 0.0.114, but if not, that's okay too.

@TheBlueMatt
TheBlueMatt merged commit 50d1260 into lightningdevkit:mainJan 19, 2023
@wpaulino
wpaulino deleted the open-channel-anchors-support branch January 19, 2023 01:03
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@wpaulino@ariard@TheBlueMatt@codecov-commenter@arik-so
, '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('^' + ".*" + ' Support opening anchor channels and test end-to-end unilateral close by wpaulino · Pull Request #1860 · lightningdevkit/rust-lightning · GitHub
Skip to content

Support opening anchor channels and test end-to-end unilateral close - #1860

Merged
TheBlueMatt merged 6 commits into
lightningdevkit:mainfrom
wpaulino:open-channel-anchors-support
Jan 19, 2023
Merged

Support opening anchor channels and test end-to-end unilateral close#1860
TheBlueMatt merged 6 commits into
lightningdevkit:mainfrom
wpaulino:open-channel-anchors-support

Conversation

@wpaulino

Copy link
Copy Markdown
Contributor

This PR introduces support for the anchors feature bit and opening/accepting anchor channels. It also includes an end-to-end test of the unilateral channel close flow, consuming the recently added anchor events and attaching fees to transactions when necessary.

Depends on #1825.

@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Currently, it doesn't expose any way to automatically reject inbound anchor channels (e.g., low onchain funds) and it doesn't force users to manually accept them either (like with zero conf). I'd imagine we'd at least want to implement the latter before considering merging this.

@ariard

Copy link
Copy Markdown

Note, there was a security issue affecting LND/Core Lightning anchor output support few months ago: https://lists.linuxfoundation.org/pipermail/lightning-dev/2022-April/003561.html. Briefly, anchor output relaxes the sighash flags for second-stage HTLC transactions from SIGHASH_ALL to SIGHASH_SINGLE | SIGHASH_ANYONECANPAY, allowing your counterparty to combine them in a single-batch. If your implementation was doing assumptions on the inputs/outputs size or order to parse those second-stage HTLC transactions in case of revoked state, a malicious party could extract funds.

With the current state of our parsing logic in check_spend_counterparty_htlc, I think we're encumbering the same exact security issue due to the following checks (1231907, channelmonitors, L2644) and we should adapt our logic in consequence:

 if tx.input.len() != 1 || tx.output.len() != 1 || tx.input[0].witness.len() != 5 {

@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Thanks for the note @ariard, I've addressed this in #1825 (commit aaf458d). I definitely missed this, but I plan to follow up with a series of tests that I imagine would've caught this. For now, this PR will just focus on an initial end-to-end test to make sure the happy path works.

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

Let's make sure we have test coverage that would have caught #1825 (comment)

@ariard

Copy link
Copy Markdown

More test coverage requested from #1825: #1825 (comment)

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

Needs rebase now 🎉

@ariard

Copy link
Copy Markdown

And more test coverage: #1825 (comment)

@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Working on the aggregated revoked HTLC test before I push another update to this. There are definitely several more cases we should be testing, but in the spirit of keeping PRs relatively small, this PR will just focus on that edge case and the happy case.

@ariardariard left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Left few reviews comments.

There are definitely several more cases we should be testing, but in the spirit of keeping PRs relatively small, this PR will just focus on that edge case and the happy case.

We can even move the aggregated revoked HTLC test in its own PR, bundled with few others from #1825. I think it's already good if we focus just on all the cases of features bit interpretation in both directions here, including unsafe option_anchor_outputs attempts.

Comment threadlightning/src/ln/features.rs Outdated
Comment threadlightning/src/ln/features.rs
Comment threadlightning/src/util/config.rs
Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channel.rs
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 1231907 to 0d7b5faCompareDecember 16, 2022 23:16
@wpaulino
wpaulino marked this pull request as ready for review December 16, 2022 23:17
@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Rebased and addressed @ariard's comments. This now depends on #1922.

Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channel.rs
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
/// [`DecodeError::InvalidValue`]: crate::ln::msgs::DecodeError::InvalidValue
/// [`SIGHASH_SINGLE + update_fee Considered Harmful`]: https://lists.linuxfoundation.org/pipermail/lightning-dev/2020-September/002796.html
pub negotiate_anchors_zero_fee_htlc_tx: bool,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we want a separate flag for "dont accept anchor channels" in addition to "open outbound channels"?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Possibly. We'll also want to base our acceptance of anchor channels on our availability of fee UTXOs, which will come in a follow-up, so we could choose to do it all there.

Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/chain/onchaintx.rs Outdated
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 0d7b5fa to 523c412CompareDecember 19, 2022 19:24
@codecov-commenter

codecov-commenter commented Dec 19, 2022

Copy link
Copy Markdown

Codecov Report

Base: 90.69% // Head: 90.69% // Decreases project coverage by -0.01%⚠️

Coverage data is based on head (27a74fe) compared to base (01fe9ba).
Patch coverage: 69.00% of modified lines in pull request are covered.

❗ Current head 27a74fe differs from pull request most recent head 202ff7f. Consider uploading reports for the commit 202ff7f to get more accurate results

Additional details and impacted files
@@ Coverage Diff @@## main #1860 +/- ##
==========================================
- Coverage 90.69% 90.69% -0.01% 
==========================================
Files 97 95 -2 Lines 50577 50074 -503 Branches 50577 50074 -503 ==========================================
- Hits 45870 45413 -457 + Misses 4707 4661 -46 
Impacted FilesCoverage Δ
lightning/src/ln/monitor_tests.rs99.56% <ø> (ø)
lightning/src/util/config.rs65.90% <0.00%> (ø)
lightning/src/util/events.rs29.35% <ø> (-1.15%)⬇️
lightning/src/ln/functional_test_utils.rs93.44% <60.00%> (+1.96%)⬆️
lightning/src/ln/channel.rs88.45% <62.06%> (-0.32%)⬇️
lightning/src/ln/features.rs98.46% <80.00%> (-1.23%)⬇️
lightning/src/ln/channelmanager.rs86.72% <100.00%> (-0.39%)⬇️
lightning/src/ln/functional_tests.rs96.90% <100.00%> (-0.01%)⬇️
lightning/src/util/macro_logger.rs86.15% <100.00%> (ø)
lightning/src/util/fairrwlock.rs85.71% <0.00%> (-2.53%)⬇️
... and 45 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.

@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 523c412 to 27a74feCompareDecember 19, 2022 22:09
Comment threadlightning/src/ln/features.rs Outdated
Comment threadlightning/src/ln/channel.rs Outdated
F::Target: FeeEstimator,
{
let opt_anchors = false; // TODO - should be based on features
#[cfg(anchors)]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is only used for "is it even possible to build a channel with this amount". Should we just drop this and always pass anchors (since its a slightly higher commitment tx fee)? Otherwise I'm worried about making decisions based on the anchor flag which isn't really right, its just the initial state.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

It's also setting the value in ChannelTransactionParameters. That of course won't be locked in until we receive an accept_channel with the same channel_type we extended. Are you also suggesting we delay setting that value in ChannelTransactionParameters until then?

Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 27a74fe to a88d650CompareDecember 22, 2022 19:51
Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/util/config.rs
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch 2 times, most recently from 9cf8351 to 404f3d1CompareJanuary 4, 2023 00:35
Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/ln/features.rs
Comment threadlightning/src/ln/monitor_tests.rs

@TheBlueMattTheBlueMatt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. Needs a pretty trivial rebase to drop the first commit.

Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/ln/channel.rs

@ariardariard left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Otherwise test coverage sounds good.


#[cfg(anchors)]
#[test]
fn test_supports_anchors_zero_htlc_tx_fee() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I don't know if the flow that matters, though when our negotiate_anchors_zero_fee_htlc_tx=true and their init features option_anchors_zero_fee_htlc_tx=false doesn't seem cover:

diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs
index 8cf5e54f..b552b61b 100644
--- a/lightning/src/ln/channel.rs
+++ b/lightning/src/ln/channel.rs
@@ -892,10 +892,8 @@ impl<Signer: Sign> Channel<Signer> {
// Optionally, if the user would like to negotiate the `anchors_zero_fee_htlc_tx` option, we
// set it now. If they don't understand it, we'll fall back to our default of
// `only_static_remotekey`.
- #[cfg(anchors)]
{ // Attributes are not allowed on if expressions on our current MSRV of 1.41.
- if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx &&
- their_features.supports_anchors_zero_fee_htlc_tx() {
+ if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx {
ret.set_anchors_zero_fee_htlc_tx_required();
}
}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Should be covered now.

// We support opening a few different types of channels. Try removing our additional
// features one by one until we've either arrived at our default or the counterparty has
// accepted one.
if self.channel_type.supports_anchors_zero_fee_htlc_tx() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do we have test coverage for handle_error once we got an error message due to rejection of channel type ?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Should be covered now.

@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 7e7abb5 to 6dbcde5CompareJanuary 17, 2023 23:03
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 6dbcde5 to 202ff7fCompareJanuary 17, 2023 23:04
@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Would appreciate a thorough pass from reviewers as it's been a while since the previous review iteration.

TheBlueMatt
TheBlueMatt previously approved these changes Jan 18, 2023

@TheBlueMattTheBlueMatt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, comments can be in a followup or the comment request ignored.

// We support opening a few different types of channels. Try removing our additional
// features one by one until we've either arrived at our default or the counterparty has
// accepted one.
if self.channel_type.supports_anchors_zero_fee_htlc_tx() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

May be worth adding a comment that the ordering here would mean that we'd never negotiate anchor with a peer that supports anchor but not scid privacy, except that we check the peer's init flags above so in general this code should really only apply for a peer that is setting init flags but not actually supporting the channel type, which should be very rare.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

May be worth adding a comment that the ordering here would mean that we'd never negotiate anchor with a peer that supports anchor but not scid privacy

Comment added.

this code should really only apply for a peer that is setting init flags but not actually supporting the channel type, which should be very rare

It won't be too rare with anchors if the inbound node doesn't have enough onchain funds to bump their commitments. They'll still advertise the feature, but reject the channel.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sure, sorry, my point was if we didn't do the check initially we'd never negotiate anchors for lots of old lnd nodes - because anchors predates the scid privacy stuff we'd always end up with no features. As long as we check their features, we're safe from that.

if self.channel_type.supports_anchors_zero_fee_htlc_tx() {
self.channel_type.clear_anchors_zero_fee_htlc_tx();
assert!(self.channel_transaction_parameters.opt_non_zero_fee_anchors.is_none());
self.channel_transaction_parameters.opt_anchors = None;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Given we're only supporting anchors with channel type negotiation, we should probably just drop opt_anchors entirely and do channel_type.supports... instead? Can come in a followup though since its a new change...I really do hate having duplicate state tracking the same thing :)

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I don't think we can drop ChannelTransactionParamerters.opt_anchors since signers rely on it in provide_channel_parameters.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We can regenerate it for them, though, rather than storing it explicitly in Channel and in its serialized form.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Ah, I'll look into this later then.

Comment threadlightning/src/ln/monitor_tests.rs Outdated
Comment threadlightning/src/ln/channel.rs
arik-so
arik-so previously approved these changes Jan 18, 2023
@wpaulino
wpaulino dismissed stale reviews from arik-so and TheBlueMatt via 660165cJanuary 18, 2023 22:49
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 202ff7f to 660165cCompareJanuary 18, 2023 22:49
/// If set, we attempt to negotiate the `anchors_zero_fee_htlc_tx`option for outbound channels.
///
/// If this option is set, channels may be created that will not be readable by LDK versions
/// prior to 0.0.114, causing [`ChannelManager`]'s read method to return a

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reminder that we may need to update this when we make anchor not cfg-gated anymore. Would be nice if we didn't and that happens in 0.0.114, but if not, that's okay too.

@TheBlueMatt
TheBlueMatt merged commit 50d1260 into lightningdevkit:mainJan 19, 2023
@wpaulino
wpaulino deleted the open-channel-anchors-support branch January 19, 2023 01:03
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@wpaulino@ariard@TheBlueMatt@codecov-commenter@arik-so
, '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" + ' Support opening anchor channels and test end-to-end unilateral close by wpaulino · Pull Request #1860 · lightningdevkit/rust-lightning · GitHub
Skip to content

Support opening anchor channels and test end-to-end unilateral close - #1860

Merged
TheBlueMatt merged 6 commits into
lightningdevkit:mainfrom
wpaulino:open-channel-anchors-support
Jan 19, 2023
Merged

Support opening anchor channels and test end-to-end unilateral close#1860
TheBlueMatt merged 6 commits into
lightningdevkit:mainfrom
wpaulino:open-channel-anchors-support

Conversation

@wpaulino

Copy link
Copy Markdown
Contributor

This PR introduces support for the anchors feature bit and opening/accepting anchor channels. It also includes an end-to-end test of the unilateral channel close flow, consuming the recently added anchor events and attaching fees to transactions when necessary.

Depends on #1825.

@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Currently, it doesn't expose any way to automatically reject inbound anchor channels (e.g., low onchain funds) and it doesn't force users to manually accept them either (like with zero conf). I'd imagine we'd at least want to implement the latter before considering merging this.

@ariard

Copy link
Copy Markdown

Note, there was a security issue affecting LND/Core Lightning anchor output support few months ago: https://lists.linuxfoundation.org/pipermail/lightning-dev/2022-April/003561.html. Briefly, anchor output relaxes the sighash flags for second-stage HTLC transactions from SIGHASH_ALL to SIGHASH_SINGLE | SIGHASH_ANYONECANPAY, allowing your counterparty to combine them in a single-batch. If your implementation was doing assumptions on the inputs/outputs size or order to parse those second-stage HTLC transactions in case of revoked state, a malicious party could extract funds.

With the current state of our parsing logic in check_spend_counterparty_htlc, I think we're encumbering the same exact security issue due to the following checks (1231907, channelmonitors, L2644) and we should adapt our logic in consequence:

 if tx.input.len() != 1 || tx.output.len() != 1 || tx.input[0].witness.len() != 5 {

@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Thanks for the note @ariard, I've addressed this in #1825 (commit aaf458d). I definitely missed this, but I plan to follow up with a series of tests that I imagine would've caught this. For now, this PR will just focus on an initial end-to-end test to make sure the happy path works.

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

Let's make sure we have test coverage that would have caught #1825 (comment)

@ariard

Copy link
Copy Markdown

More test coverage requested from #1825: #1825 (comment)

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

Needs rebase now 🎉

@ariard

Copy link
Copy Markdown

And more test coverage: #1825 (comment)

@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Working on the aggregated revoked HTLC test before I push another update to this. There are definitely several more cases we should be testing, but in the spirit of keeping PRs relatively small, this PR will just focus on that edge case and the happy case.

@ariardariard left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Left few reviews comments.

There are definitely several more cases we should be testing, but in the spirit of keeping PRs relatively small, this PR will just focus on that edge case and the happy case.

We can even move the aggregated revoked HTLC test in its own PR, bundled with few others from #1825. I think it's already good if we focus just on all the cases of features bit interpretation in both directions here, including unsafe option_anchor_outputs attempts.

Comment threadlightning/src/ln/features.rs Outdated
Comment threadlightning/src/ln/features.rs
Comment threadlightning/src/util/config.rs
Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channel.rs
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 1231907 to 0d7b5faCompareDecember 16, 2022 23:16
@wpaulino
wpaulino marked this pull request as ready for review December 16, 2022 23:17
@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Rebased and addressed @ariard's comments. This now depends on #1922.

Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channel.rs
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
/// [`DecodeError::InvalidValue`]: crate::ln::msgs::DecodeError::InvalidValue
/// [`SIGHASH_SINGLE + update_fee Considered Harmful`]: https://lists.linuxfoundation.org/pipermail/lightning-dev/2020-September/002796.html
pub negotiate_anchors_zero_fee_htlc_tx: bool,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we want a separate flag for "dont accept anchor channels" in addition to "open outbound channels"?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Possibly. We'll also want to base our acceptance of anchor channels on our availability of fee UTXOs, which will come in a follow-up, so we could choose to do it all there.

Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/chain/onchaintx.rs Outdated
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 0d7b5fa to 523c412CompareDecember 19, 2022 19:24
@codecov-commenter

codecov-commenter commented Dec 19, 2022

Copy link
Copy Markdown

Codecov Report

Base: 90.69% // Head: 90.69% // Decreases project coverage by -0.01%⚠️

Coverage data is based on head (27a74fe) compared to base (01fe9ba).
Patch coverage: 69.00% of modified lines in pull request are covered.

❗ Current head 27a74fe differs from pull request most recent head 202ff7f. Consider uploading reports for the commit 202ff7f to get more accurate results

Additional details and impacted files
@@ Coverage Diff @@## main #1860 +/- ##
==========================================
- Coverage 90.69% 90.69% -0.01% 
==========================================
Files 97 95 -2 Lines 50577 50074 -503 Branches 50577 50074 -503 ==========================================
- Hits 45870 45413 -457 + Misses 4707 4661 -46 
Impacted FilesCoverage Δ
lightning/src/ln/monitor_tests.rs99.56% <ø> (ø)
lightning/src/util/config.rs65.90% <0.00%> (ø)
lightning/src/util/events.rs29.35% <ø> (-1.15%)⬇️
lightning/src/ln/functional_test_utils.rs93.44% <60.00%> (+1.96%)⬆️
lightning/src/ln/channel.rs88.45% <62.06%> (-0.32%)⬇️
lightning/src/ln/features.rs98.46% <80.00%> (-1.23%)⬇️
lightning/src/ln/channelmanager.rs86.72% <100.00%> (-0.39%)⬇️
lightning/src/ln/functional_tests.rs96.90% <100.00%> (-0.01%)⬇️
lightning/src/util/macro_logger.rs86.15% <100.00%> (ø)
lightning/src/util/fairrwlock.rs85.71% <0.00%> (-2.53%)⬇️
... and 45 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.

@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 523c412 to 27a74feCompareDecember 19, 2022 22:09
Comment threadlightning/src/ln/features.rs Outdated
Comment threadlightning/src/ln/channel.rs Outdated
F::Target: FeeEstimator,
{
let opt_anchors = false; // TODO - should be based on features
#[cfg(anchors)]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is only used for "is it even possible to build a channel with this amount". Should we just drop this and always pass anchors (since its a slightly higher commitment tx fee)? Otherwise I'm worried about making decisions based on the anchor flag which isn't really right, its just the initial state.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

It's also setting the value in ChannelTransactionParameters. That of course won't be locked in until we receive an accept_channel with the same channel_type we extended. Are you also suggesting we delay setting that value in ChannelTransactionParameters until then?

Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 27a74fe to a88d650CompareDecember 22, 2022 19:51
Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/util/config.rs
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch 2 times, most recently from 9cf8351 to 404f3d1CompareJanuary 4, 2023 00:35
Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/ln/features.rs
Comment threadlightning/src/ln/monitor_tests.rs

@TheBlueMattTheBlueMatt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. Needs a pretty trivial rebase to drop the first commit.

Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/ln/channel.rs

@ariardariard left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Otherwise test coverage sounds good.


#[cfg(anchors)]
#[test]
fn test_supports_anchors_zero_htlc_tx_fee() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I don't know if the flow that matters, though when our negotiate_anchors_zero_fee_htlc_tx=true and their init features option_anchors_zero_fee_htlc_tx=false doesn't seem cover:

diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs
index 8cf5e54f..b552b61b 100644
--- a/lightning/src/ln/channel.rs
+++ b/lightning/src/ln/channel.rs
@@ -892,10 +892,8 @@ impl<Signer: Sign> Channel<Signer> {
// Optionally, if the user would like to negotiate the `anchors_zero_fee_htlc_tx` option, we
// set it now. If they don't understand it, we'll fall back to our default of
// `only_static_remotekey`.
- #[cfg(anchors)]
{ // Attributes are not allowed on if expressions on our current MSRV of 1.41.
- if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx &&
- their_features.supports_anchors_zero_fee_htlc_tx() {
+ if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx {
ret.set_anchors_zero_fee_htlc_tx_required();
}
}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Should be covered now.

// We support opening a few different types of channels. Try removing our additional
// features one by one until we've either arrived at our default or the counterparty has
// accepted one.
if self.channel_type.supports_anchors_zero_fee_htlc_tx() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do we have test coverage for handle_error once we got an error message due to rejection of channel type ?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Should be covered now.

@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 7e7abb5 to 6dbcde5CompareJanuary 17, 2023 23:03
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 6dbcde5 to 202ff7fCompareJanuary 17, 2023 23:04
@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Would appreciate a thorough pass from reviewers as it's been a while since the previous review iteration.

TheBlueMatt
TheBlueMatt previously approved these changes Jan 18, 2023

@TheBlueMattTheBlueMatt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, comments can be in a followup or the comment request ignored.

// We support opening a few different types of channels. Try removing our additional
// features one by one until we've either arrived at our default or the counterparty has
// accepted one.
if self.channel_type.supports_anchors_zero_fee_htlc_tx() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

May be worth adding a comment that the ordering here would mean that we'd never negotiate anchor with a peer that supports anchor but not scid privacy, except that we check the peer's init flags above so in general this code should really only apply for a peer that is setting init flags but not actually supporting the channel type, which should be very rare.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

May be worth adding a comment that the ordering here would mean that we'd never negotiate anchor with a peer that supports anchor but not scid privacy

Comment added.

this code should really only apply for a peer that is setting init flags but not actually supporting the channel type, which should be very rare

It won't be too rare with anchors if the inbound node doesn't have enough onchain funds to bump their commitments. They'll still advertise the feature, but reject the channel.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sure, sorry, my point was if we didn't do the check initially we'd never negotiate anchors for lots of old lnd nodes - because anchors predates the scid privacy stuff we'd always end up with no features. As long as we check their features, we're safe from that.

if self.channel_type.supports_anchors_zero_fee_htlc_tx() {
self.channel_type.clear_anchors_zero_fee_htlc_tx();
assert!(self.channel_transaction_parameters.opt_non_zero_fee_anchors.is_none());
self.channel_transaction_parameters.opt_anchors = None;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Given we're only supporting anchors with channel type negotiation, we should probably just drop opt_anchors entirely and do channel_type.supports... instead? Can come in a followup though since its a new change...I really do hate having duplicate state tracking the same thing :)

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I don't think we can drop ChannelTransactionParamerters.opt_anchors since signers rely on it in provide_channel_parameters.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We can regenerate it for them, though, rather than storing it explicitly in Channel and in its serialized form.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Ah, I'll look into this later then.

Comment threadlightning/src/ln/monitor_tests.rs Outdated
Comment threadlightning/src/ln/channel.rs
arik-so
arik-so previously approved these changes Jan 18, 2023
@wpaulino
wpaulino dismissed stale reviews from arik-so and TheBlueMatt via 660165cJanuary 18, 2023 22:49
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 202ff7f to 660165cCompareJanuary 18, 2023 22:49
/// If set, we attempt to negotiate the `anchors_zero_fee_htlc_tx`option for outbound channels.
///
/// If this option is set, channels may be created that will not be readable by LDK versions
/// prior to 0.0.114, causing [`ChannelManager`]'s read method to return a

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reminder that we may need to update this when we make anchor not cfg-gated anymore. Would be nice if we didn't and that happens in 0.0.114, but if not, that's okay too.

@TheBlueMatt
TheBlueMatt merged commit 50d1260 into lightningdevkit:mainJan 19, 2023
@wpaulino
wpaulino deleted the open-channel-anchors-support branch January 19, 2023 01:03
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@wpaulino@ariard@TheBlueMatt@codecov-commenter@arik-so
, '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('^' + ".*" + ' Support opening anchor channels and test end-to-end unilateral close by wpaulino · Pull Request #1860 · lightningdevkit/rust-lightning · GitHub
Skip to content

Support opening anchor channels and test end-to-end unilateral close - #1860

Merged
TheBlueMatt merged 6 commits into
lightningdevkit:mainfrom
wpaulino:open-channel-anchors-support
Jan 19, 2023
Merged

Support opening anchor channels and test end-to-end unilateral close#1860
TheBlueMatt merged 6 commits into
lightningdevkit:mainfrom
wpaulino:open-channel-anchors-support

Conversation

@wpaulino

Copy link
Copy Markdown
Contributor

This PR introduces support for the anchors feature bit and opening/accepting anchor channels. It also includes an end-to-end test of the unilateral channel close flow, consuming the recently added anchor events and attaching fees to transactions when necessary.

Depends on #1825.

@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Currently, it doesn't expose any way to automatically reject inbound anchor channels (e.g., low onchain funds) and it doesn't force users to manually accept them either (like with zero conf). I'd imagine we'd at least want to implement the latter before considering merging this.

@ariard

Copy link
Copy Markdown

Note, there was a security issue affecting LND/Core Lightning anchor output support few months ago: https://lists.linuxfoundation.org/pipermail/lightning-dev/2022-April/003561.html. Briefly, anchor output relaxes the sighash flags for second-stage HTLC transactions from SIGHASH_ALL to SIGHASH_SINGLE | SIGHASH_ANYONECANPAY, allowing your counterparty to combine them in a single-batch. If your implementation was doing assumptions on the inputs/outputs size or order to parse those second-stage HTLC transactions in case of revoked state, a malicious party could extract funds.

With the current state of our parsing logic in check_spend_counterparty_htlc, I think we're encumbering the same exact security issue due to the following checks (1231907, channelmonitors, L2644) and we should adapt our logic in consequence:

 if tx.input.len() != 1 || tx.output.len() != 1 || tx.input[0].witness.len() != 5 {

@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Thanks for the note @ariard, I've addressed this in #1825 (commit aaf458d). I definitely missed this, but I plan to follow up with a series of tests that I imagine would've caught this. For now, this PR will just focus on an initial end-to-end test to make sure the happy path works.

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

Let's make sure we have test coverage that would have caught #1825 (comment)

@ariard

Copy link
Copy Markdown

More test coverage requested from #1825: #1825 (comment)

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

Needs rebase now 🎉

@ariard

Copy link
Copy Markdown

And more test coverage: #1825 (comment)

@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Working on the aggregated revoked HTLC test before I push another update to this. There are definitely several more cases we should be testing, but in the spirit of keeping PRs relatively small, this PR will just focus on that edge case and the happy case.

@ariardariard left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Left few reviews comments.

There are definitely several more cases we should be testing, but in the spirit of keeping PRs relatively small, this PR will just focus on that edge case and the happy case.

We can even move the aggregated revoked HTLC test in its own PR, bundled with few others from #1825. I think it's already good if we focus just on all the cases of features bit interpretation in both directions here, including unsafe option_anchor_outputs attempts.

Comment threadlightning/src/ln/features.rs Outdated
Comment threadlightning/src/ln/features.rs
Comment threadlightning/src/util/config.rs
Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channel.rs
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 1231907 to 0d7b5faCompareDecember 16, 2022 23:16
@wpaulino
wpaulino marked this pull request as ready for review December 16, 2022 23:17
@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Rebased and addressed @ariard's comments. This now depends on #1922.

Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channel.rs
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
/// [`DecodeError::InvalidValue`]: crate::ln::msgs::DecodeError::InvalidValue
/// [`SIGHASH_SINGLE + update_fee Considered Harmful`]: https://lists.linuxfoundation.org/pipermail/lightning-dev/2020-September/002796.html
pub negotiate_anchors_zero_fee_htlc_tx: bool,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we want a separate flag for "dont accept anchor channels" in addition to "open outbound channels"?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Possibly. We'll also want to base our acceptance of anchor channels on our availability of fee UTXOs, which will come in a follow-up, so we could choose to do it all there.

Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/chain/onchaintx.rs Outdated
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 0d7b5fa to 523c412CompareDecember 19, 2022 19:24
@codecov-commenter

codecov-commenter commented Dec 19, 2022

Copy link
Copy Markdown

Codecov Report

Base: 90.69% // Head: 90.69% // Decreases project coverage by -0.01%⚠️

Coverage data is based on head (27a74fe) compared to base (01fe9ba).
Patch coverage: 69.00% of modified lines in pull request are covered.

❗ Current head 27a74fe differs from pull request most recent head 202ff7f. Consider uploading reports for the commit 202ff7f to get more accurate results

Additional details and impacted files
@@ Coverage Diff @@## main #1860 +/- ##
==========================================
- Coverage 90.69% 90.69% -0.01% 
==========================================
Files 97 95 -2 Lines 50577 50074 -503 Branches 50577 50074 -503 ==========================================
- Hits 45870 45413 -457 + Misses 4707 4661 -46 
Impacted FilesCoverage Δ
lightning/src/ln/monitor_tests.rs99.56% <ø> (ø)
lightning/src/util/config.rs65.90% <0.00%> (ø)
lightning/src/util/events.rs29.35% <ø> (-1.15%)⬇️
lightning/src/ln/functional_test_utils.rs93.44% <60.00%> (+1.96%)⬆️
lightning/src/ln/channel.rs88.45% <62.06%> (-0.32%)⬇️
lightning/src/ln/features.rs98.46% <80.00%> (-1.23%)⬇️
lightning/src/ln/channelmanager.rs86.72% <100.00%> (-0.39%)⬇️
lightning/src/ln/functional_tests.rs96.90% <100.00%> (-0.01%)⬇️
lightning/src/util/macro_logger.rs86.15% <100.00%> (ø)
lightning/src/util/fairrwlock.rs85.71% <0.00%> (-2.53%)⬇️
... and 45 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.

@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 523c412 to 27a74feCompareDecember 19, 2022 22:09
Comment threadlightning/src/ln/features.rs Outdated
Comment threadlightning/src/ln/channel.rs Outdated
F::Target: FeeEstimator,
{
let opt_anchors = false; // TODO - should be based on features
#[cfg(anchors)]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is only used for "is it even possible to build a channel with this amount". Should we just drop this and always pass anchors (since its a slightly higher commitment tx fee)? Otherwise I'm worried about making decisions based on the anchor flag which isn't really right, its just the initial state.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

It's also setting the value in ChannelTransactionParameters. That of course won't be locked in until we receive an accept_channel with the same channel_type we extended. Are you also suggesting we delay setting that value in ChannelTransactionParameters until then?

Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 27a74fe to a88d650CompareDecember 22, 2022 19:51
Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/util/config.rs
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch 2 times, most recently from 9cf8351 to 404f3d1CompareJanuary 4, 2023 00:35
Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/ln/features.rs
Comment threadlightning/src/ln/monitor_tests.rs

@TheBlueMattTheBlueMatt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. Needs a pretty trivial rebase to drop the first commit.

Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/ln/channel.rs

@ariardariard left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Otherwise test coverage sounds good.


#[cfg(anchors)]
#[test]
fn test_supports_anchors_zero_htlc_tx_fee() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I don't know if the flow that matters, though when our negotiate_anchors_zero_fee_htlc_tx=true and their init features option_anchors_zero_fee_htlc_tx=false doesn't seem cover:

diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs
index 8cf5e54f..b552b61b 100644
--- a/lightning/src/ln/channel.rs
+++ b/lightning/src/ln/channel.rs
@@ -892,10 +892,8 @@ impl<Signer: Sign> Channel<Signer> {
// Optionally, if the user would like to negotiate the `anchors_zero_fee_htlc_tx` option, we
// set it now. If they don't understand it, we'll fall back to our default of
// `only_static_remotekey`.
- #[cfg(anchors)]
{ // Attributes are not allowed on if expressions on our current MSRV of 1.41.
- if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx &&
- their_features.supports_anchors_zero_fee_htlc_tx() {
+ if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx {
ret.set_anchors_zero_fee_htlc_tx_required();
}
}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Should be covered now.

// We support opening a few different types of channels. Try removing our additional
// features one by one until we've either arrived at our default or the counterparty has
// accepted one.
if self.channel_type.supports_anchors_zero_fee_htlc_tx() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do we have test coverage for handle_error once we got an error message due to rejection of channel type ?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Should be covered now.

@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 7e7abb5 to 6dbcde5CompareJanuary 17, 2023 23:03
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 6dbcde5 to 202ff7fCompareJanuary 17, 2023 23:04
@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Would appreciate a thorough pass from reviewers as it's been a while since the previous review iteration.

TheBlueMatt
TheBlueMatt previously approved these changes Jan 18, 2023

@TheBlueMattTheBlueMatt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, comments can be in a followup or the comment request ignored.

// We support opening a few different types of channels. Try removing our additional
// features one by one until we've either arrived at our default or the counterparty has
// accepted one.
if self.channel_type.supports_anchors_zero_fee_htlc_tx() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

May be worth adding a comment that the ordering here would mean that we'd never negotiate anchor with a peer that supports anchor but not scid privacy, except that we check the peer's init flags above so in general this code should really only apply for a peer that is setting init flags but not actually supporting the channel type, which should be very rare.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

May be worth adding a comment that the ordering here would mean that we'd never negotiate anchor with a peer that supports anchor but not scid privacy

Comment added.

this code should really only apply for a peer that is setting init flags but not actually supporting the channel type, which should be very rare

It won't be too rare with anchors if the inbound node doesn't have enough onchain funds to bump their commitments. They'll still advertise the feature, but reject the channel.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sure, sorry, my point was if we didn't do the check initially we'd never negotiate anchors for lots of old lnd nodes - because anchors predates the scid privacy stuff we'd always end up with no features. As long as we check their features, we're safe from that.

if self.channel_type.supports_anchors_zero_fee_htlc_tx() {
self.channel_type.clear_anchors_zero_fee_htlc_tx();
assert!(self.channel_transaction_parameters.opt_non_zero_fee_anchors.is_none());
self.channel_transaction_parameters.opt_anchors = None;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Given we're only supporting anchors with channel type negotiation, we should probably just drop opt_anchors entirely and do channel_type.supports... instead? Can come in a followup though since its a new change...I really do hate having duplicate state tracking the same thing :)

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I don't think we can drop ChannelTransactionParamerters.opt_anchors since signers rely on it in provide_channel_parameters.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We can regenerate it for them, though, rather than storing it explicitly in Channel and in its serialized form.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Ah, I'll look into this later then.

Comment threadlightning/src/ln/monitor_tests.rs Outdated
Comment threadlightning/src/ln/channel.rs
arik-so
arik-so previously approved these changes Jan 18, 2023
@wpaulino
wpaulino dismissed stale reviews from arik-so and TheBlueMatt via 660165cJanuary 18, 2023 22:49
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 202ff7f to 660165cCompareJanuary 18, 2023 22:49
/// If set, we attempt to negotiate the `anchors_zero_fee_htlc_tx`option for outbound channels.
///
/// If this option is set, channels may be created that will not be readable by LDK versions
/// prior to 0.0.114, causing [`ChannelManager`]'s read method to return a

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reminder that we may need to update this when we make anchor not cfg-gated anymore. Would be nice if we didn't and that happens in 0.0.114, but if not, that's okay too.

@TheBlueMatt
TheBlueMatt merged commit 50d1260 into lightningdevkit:mainJan 19, 2023
@wpaulino
wpaulino deleted the open-channel-anchors-support branch January 19, 2023 01:03
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@wpaulino@ariard@TheBlueMatt@codecov-commenter@arik-so
, '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('^' + ".*" + ' Support opening anchor channels and test end-to-end unilateral close by wpaulino · Pull Request #1860 · lightningdevkit/rust-lightning · GitHub
Skip to content

Support opening anchor channels and test end-to-end unilateral close - #1860

Merged
TheBlueMatt merged 6 commits into
lightningdevkit:mainfrom
wpaulino:open-channel-anchors-support
Jan 19, 2023
Merged

Support opening anchor channels and test end-to-end unilateral close#1860
TheBlueMatt merged 6 commits into
lightningdevkit:mainfrom
wpaulino:open-channel-anchors-support

Conversation

@wpaulino

Copy link
Copy Markdown
Contributor

This PR introduces support for the anchors feature bit and opening/accepting anchor channels. It also includes an end-to-end test of the unilateral channel close flow, consuming the recently added anchor events and attaching fees to transactions when necessary.

Depends on #1825.

@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Currently, it doesn't expose any way to automatically reject inbound anchor channels (e.g., low onchain funds) and it doesn't force users to manually accept them either (like with zero conf). I'd imagine we'd at least want to implement the latter before considering merging this.

@ariard

Copy link
Copy Markdown

Note, there was a security issue affecting LND/Core Lightning anchor output support few months ago: https://lists.linuxfoundation.org/pipermail/lightning-dev/2022-April/003561.html. Briefly, anchor output relaxes the sighash flags for second-stage HTLC transactions from SIGHASH_ALL to SIGHASH_SINGLE | SIGHASH_ANYONECANPAY, allowing your counterparty to combine them in a single-batch. If your implementation was doing assumptions on the inputs/outputs size or order to parse those second-stage HTLC transactions in case of revoked state, a malicious party could extract funds.

With the current state of our parsing logic in check_spend_counterparty_htlc, I think we're encumbering the same exact security issue due to the following checks (1231907, channelmonitors, L2644) and we should adapt our logic in consequence:

 if tx.input.len() != 1 || tx.output.len() != 1 || tx.input[0].witness.len() != 5 {

@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Thanks for the note @ariard, I've addressed this in #1825 (commit aaf458d). I definitely missed this, but I plan to follow up with a series of tests that I imagine would've caught this. For now, this PR will just focus on an initial end-to-end test to make sure the happy path works.

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

Let's make sure we have test coverage that would have caught #1825 (comment)

@ariard

Copy link
Copy Markdown

More test coverage requested from #1825: #1825 (comment)

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

Needs rebase now 🎉

@ariard

Copy link
Copy Markdown

And more test coverage: #1825 (comment)

@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Working on the aggregated revoked HTLC test before I push another update to this. There are definitely several more cases we should be testing, but in the spirit of keeping PRs relatively small, this PR will just focus on that edge case and the happy case.

@ariardariard left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Left few reviews comments.

There are definitely several more cases we should be testing, but in the spirit of keeping PRs relatively small, this PR will just focus on that edge case and the happy case.

We can even move the aggregated revoked HTLC test in its own PR, bundled with few others from #1825. I think it's already good if we focus just on all the cases of features bit interpretation in both directions here, including unsafe option_anchor_outputs attempts.

Comment threadlightning/src/ln/features.rs Outdated
Comment threadlightning/src/ln/features.rs
Comment threadlightning/src/util/config.rs
Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channel.rs
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 1231907 to 0d7b5faCompareDecember 16, 2022 23:16
@wpaulino
wpaulino marked this pull request as ready for review December 16, 2022 23:17
@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Rebased and addressed @ariard's comments. This now depends on #1922.

Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channel.rs
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
/// [`DecodeError::InvalidValue`]: crate::ln::msgs::DecodeError::InvalidValue
/// [`SIGHASH_SINGLE + update_fee Considered Harmful`]: https://lists.linuxfoundation.org/pipermail/lightning-dev/2020-September/002796.html
pub negotiate_anchors_zero_fee_htlc_tx: bool,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we want a separate flag for "dont accept anchor channels" in addition to "open outbound channels"?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Possibly. We'll also want to base our acceptance of anchor channels on our availability of fee UTXOs, which will come in a follow-up, so we could choose to do it all there.

Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/chain/onchaintx.rs Outdated
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 0d7b5fa to 523c412CompareDecember 19, 2022 19:24
@codecov-commenter

codecov-commenter commented Dec 19, 2022

Copy link
Copy Markdown

Codecov Report

Base: 90.69% // Head: 90.69% // Decreases project coverage by -0.01%⚠️

Coverage data is based on head (27a74fe) compared to base (01fe9ba).
Patch coverage: 69.00% of modified lines in pull request are covered.

❗ Current head 27a74fe differs from pull request most recent head 202ff7f. Consider uploading reports for the commit 202ff7f to get more accurate results

Additional details and impacted files
@@ Coverage Diff @@## main #1860 +/- ##
==========================================
- Coverage 90.69% 90.69% -0.01% 
==========================================
Files 97 95 -2 Lines 50577 50074 -503 Branches 50577 50074 -503 ==========================================
- Hits 45870 45413 -457 + Misses 4707 4661 -46 
Impacted FilesCoverage Δ
lightning/src/ln/monitor_tests.rs99.56% <ø> (ø)
lightning/src/util/config.rs65.90% <0.00%> (ø)
lightning/src/util/events.rs29.35% <ø> (-1.15%)⬇️
lightning/src/ln/functional_test_utils.rs93.44% <60.00%> (+1.96%)⬆️
lightning/src/ln/channel.rs88.45% <62.06%> (-0.32%)⬇️
lightning/src/ln/features.rs98.46% <80.00%> (-1.23%)⬇️
lightning/src/ln/channelmanager.rs86.72% <100.00%> (-0.39%)⬇️
lightning/src/ln/functional_tests.rs96.90% <100.00%> (-0.01%)⬇️
lightning/src/util/macro_logger.rs86.15% <100.00%> (ø)
lightning/src/util/fairrwlock.rs85.71% <0.00%> (-2.53%)⬇️
... and 45 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.

@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 523c412 to 27a74feCompareDecember 19, 2022 22:09
Comment threadlightning/src/ln/features.rs Outdated
Comment threadlightning/src/ln/channel.rs Outdated
F::Target: FeeEstimator,
{
let opt_anchors = false; // TODO - should be based on features
#[cfg(anchors)]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is only used for "is it even possible to build a channel with this amount". Should we just drop this and always pass anchors (since its a slightly higher commitment tx fee)? Otherwise I'm worried about making decisions based on the anchor flag which isn't really right, its just the initial state.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

It's also setting the value in ChannelTransactionParameters. That of course won't be locked in until we receive an accept_channel with the same channel_type we extended. Are you also suggesting we delay setting that value in ChannelTransactionParameters until then?

Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 27a74fe to a88d650CompareDecember 22, 2022 19:51
Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/util/config.rs
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch 2 times, most recently from 9cf8351 to 404f3d1CompareJanuary 4, 2023 00:35
Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/ln/features.rs
Comment threadlightning/src/ln/monitor_tests.rs

@TheBlueMattTheBlueMatt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. Needs a pretty trivial rebase to drop the first commit.

Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/ln/channel.rs

@ariardariard left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Otherwise test coverage sounds good.


#[cfg(anchors)]
#[test]
fn test_supports_anchors_zero_htlc_tx_fee() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I don't know if the flow that matters, though when our negotiate_anchors_zero_fee_htlc_tx=true and their init features option_anchors_zero_fee_htlc_tx=false doesn't seem cover:

diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs
index 8cf5e54f..b552b61b 100644
--- a/lightning/src/ln/channel.rs
+++ b/lightning/src/ln/channel.rs
@@ -892,10 +892,8 @@ impl<Signer: Sign> Channel<Signer> {
// Optionally, if the user would like to negotiate the `anchors_zero_fee_htlc_tx` option, we
// set it now. If they don't understand it, we'll fall back to our default of
// `only_static_remotekey`.
- #[cfg(anchors)]
{ // Attributes are not allowed on if expressions on our current MSRV of 1.41.
- if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx &&
- their_features.supports_anchors_zero_fee_htlc_tx() {
+ if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx {
ret.set_anchors_zero_fee_htlc_tx_required();
}
}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Should be covered now.

// We support opening a few different types of channels. Try removing our additional
// features one by one until we've either arrived at our default or the counterparty has
// accepted one.
if self.channel_type.supports_anchors_zero_fee_htlc_tx() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do we have test coverage for handle_error once we got an error message due to rejection of channel type ?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Should be covered now.

@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 7e7abb5 to 6dbcde5CompareJanuary 17, 2023 23:03
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 6dbcde5 to 202ff7fCompareJanuary 17, 2023 23:04
@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Would appreciate a thorough pass from reviewers as it's been a while since the previous review iteration.

TheBlueMatt
TheBlueMatt previously approved these changes Jan 18, 2023

@TheBlueMattTheBlueMatt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, comments can be in a followup or the comment request ignored.

// We support opening a few different types of channels. Try removing our additional
// features one by one until we've either arrived at our default or the counterparty has
// accepted one.
if self.channel_type.supports_anchors_zero_fee_htlc_tx() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

May be worth adding a comment that the ordering here would mean that we'd never negotiate anchor with a peer that supports anchor but not scid privacy, except that we check the peer's init flags above so in general this code should really only apply for a peer that is setting init flags but not actually supporting the channel type, which should be very rare.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

May be worth adding a comment that the ordering here would mean that we'd never negotiate anchor with a peer that supports anchor but not scid privacy

Comment added.

this code should really only apply for a peer that is setting init flags but not actually supporting the channel type, which should be very rare

It won't be too rare with anchors if the inbound node doesn't have enough onchain funds to bump their commitments. They'll still advertise the feature, but reject the channel.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sure, sorry, my point was if we didn't do the check initially we'd never negotiate anchors for lots of old lnd nodes - because anchors predates the scid privacy stuff we'd always end up with no features. As long as we check their features, we're safe from that.

if self.channel_type.supports_anchors_zero_fee_htlc_tx() {
self.channel_type.clear_anchors_zero_fee_htlc_tx();
assert!(self.channel_transaction_parameters.opt_non_zero_fee_anchors.is_none());
self.channel_transaction_parameters.opt_anchors = None;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Given we're only supporting anchors with channel type negotiation, we should probably just drop opt_anchors entirely and do channel_type.supports... instead? Can come in a followup though since its a new change...I really do hate having duplicate state tracking the same thing :)

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I don't think we can drop ChannelTransactionParamerters.opt_anchors since signers rely on it in provide_channel_parameters.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We can regenerate it for them, though, rather than storing it explicitly in Channel and in its serialized form.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Ah, I'll look into this later then.

Comment threadlightning/src/ln/monitor_tests.rs Outdated
Comment threadlightning/src/ln/channel.rs
arik-so
arik-so previously approved these changes Jan 18, 2023
@wpaulino
wpaulino dismissed stale reviews from arik-so and TheBlueMatt via 660165cJanuary 18, 2023 22:49
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 202ff7f to 660165cCompareJanuary 18, 2023 22:49
/// If set, we attempt to negotiate the `anchors_zero_fee_htlc_tx`option for outbound channels.
///
/// If this option is set, channels may be created that will not be readable by LDK versions
/// prior to 0.0.114, causing [`ChannelManager`]'s read method to return a

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reminder that we may need to update this when we make anchor not cfg-gated anymore. Would be nice if we didn't and that happens in 0.0.114, but if not, that's okay too.

@TheBlueMatt
TheBlueMatt merged commit 50d1260 into lightningdevkit:mainJan 19, 2023
@wpaulino
wpaulino deleted the open-channel-anchors-support branch January 19, 2023 01:03
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@wpaulino@ariard@TheBlueMatt@codecov-commenter@arik-so
, '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); } })(); })(); Support opening anchor channels and test end-to-end unilateral close by wpaulino · Pull Request #1860 · lightningdevkit/rust-lightning · GitHub
Skip to content

Support opening anchor channels and test end-to-end unilateral close - #1860

Merged
TheBlueMatt merged 6 commits into
lightningdevkit:mainfrom
wpaulino:open-channel-anchors-support
Jan 19, 2023
Merged

Support opening anchor channels and test end-to-end unilateral close#1860
TheBlueMatt merged 6 commits into
lightningdevkit:mainfrom
wpaulino:open-channel-anchors-support

Conversation

@wpaulino

Copy link
Copy Markdown
Contributor

This PR introduces support for the anchors feature bit and opening/accepting anchor channels. It also includes an end-to-end test of the unilateral channel close flow, consuming the recently added anchor events and attaching fees to transactions when necessary.

Depends on #1825.

@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Currently, it doesn't expose any way to automatically reject inbound anchor channels (e.g., low onchain funds) and it doesn't force users to manually accept them either (like with zero conf). I'd imagine we'd at least want to implement the latter before considering merging this.

@ariard

Copy link
Copy Markdown

Note, there was a security issue affecting LND/Core Lightning anchor output support few months ago: https://lists.linuxfoundation.org/pipermail/lightning-dev/2022-April/003561.html. Briefly, anchor output relaxes the sighash flags for second-stage HTLC transactions from SIGHASH_ALL to SIGHASH_SINGLE | SIGHASH_ANYONECANPAY, allowing your counterparty to combine them in a single-batch. If your implementation was doing assumptions on the inputs/outputs size or order to parse those second-stage HTLC transactions in case of revoked state, a malicious party could extract funds.

With the current state of our parsing logic in check_spend_counterparty_htlc, I think we're encumbering the same exact security issue due to the following checks (1231907, channelmonitors, L2644) and we should adapt our logic in consequence:

 if tx.input.len() != 1 || tx.output.len() != 1 || tx.input[0].witness.len() != 5 {

@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Thanks for the note @ariard, I've addressed this in #1825 (commit aaf458d). I definitely missed this, but I plan to follow up with a series of tests that I imagine would've caught this. For now, this PR will just focus on an initial end-to-end test to make sure the happy path works.

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

Let's make sure we have test coverage that would have caught #1825 (comment)

@ariard

Copy link
Copy Markdown

More test coverage requested from #1825: #1825 (comment)

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

Needs rebase now 🎉

@ariard

Copy link
Copy Markdown

And more test coverage: #1825 (comment)

@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Working on the aggregated revoked HTLC test before I push another update to this. There are definitely several more cases we should be testing, but in the spirit of keeping PRs relatively small, this PR will just focus on that edge case and the happy case.

@ariardariard left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Left few reviews comments.

There are definitely several more cases we should be testing, but in the spirit of keeping PRs relatively small, this PR will just focus on that edge case and the happy case.

We can even move the aggregated revoked HTLC test in its own PR, bundled with few others from #1825. I think it's already good if we focus just on all the cases of features bit interpretation in both directions here, including unsafe option_anchor_outputs attempts.

Comment threadlightning/src/ln/features.rs Outdated
Comment threadlightning/src/ln/features.rs
Comment threadlightning/src/util/config.rs
Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channel.rs
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 1231907 to 0d7b5faCompareDecember 16, 2022 23:16
@wpaulino
wpaulino marked this pull request as ready for review December 16, 2022 23:17
@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Rebased and addressed @ariard's comments. This now depends on #1922.

Comment threadlightning/src/ln/channelmanager.rs Outdated
Comment threadlightning/src/ln/channel.rs
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
/// [`DecodeError::InvalidValue`]: crate::ln::msgs::DecodeError::InvalidValue
/// [`SIGHASH_SINGLE + update_fee Considered Harmful`]: https://lists.linuxfoundation.org/pipermail/lightning-dev/2020-September/002796.html
pub negotiate_anchors_zero_fee_htlc_tx: bool,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we want a separate flag for "dont accept anchor channels" in addition to "open outbound channels"?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Possibly. We'll also want to base our acceptance of anchor channels on our availability of fee UTXOs, which will come in a follow-up, so we could choose to do it all there.

Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/chain/onchaintx.rs Outdated
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 0d7b5fa to 523c412CompareDecember 19, 2022 19:24
@codecov-commenter

codecov-commenter commented Dec 19, 2022

Copy link
Copy Markdown

Codecov Report

Base: 90.69% // Head: 90.69% // Decreases project coverage by -0.01%⚠️

Coverage data is based on head (27a74fe) compared to base (01fe9ba).
Patch coverage: 69.00% of modified lines in pull request are covered.

❗ Current head 27a74fe differs from pull request most recent head 202ff7f. Consider uploading reports for the commit 202ff7f to get more accurate results

Additional details and impacted files
@@ Coverage Diff @@## main #1860 +/- ##
==========================================
- Coverage 90.69% 90.69% -0.01% 
==========================================
Files 97 95 -2 Lines 50577 50074 -503 Branches 50577 50074 -503 ==========================================
- Hits 45870 45413 -457 + Misses 4707 4661 -46 
Impacted FilesCoverage Δ
lightning/src/ln/monitor_tests.rs99.56% <ø> (ø)
lightning/src/util/config.rs65.90% <0.00%> (ø)
lightning/src/util/events.rs29.35% <ø> (-1.15%)⬇️
lightning/src/ln/functional_test_utils.rs93.44% <60.00%> (+1.96%)⬆️
lightning/src/ln/channel.rs88.45% <62.06%> (-0.32%)⬇️
lightning/src/ln/features.rs98.46% <80.00%> (-1.23%)⬇️
lightning/src/ln/channelmanager.rs86.72% <100.00%> (-0.39%)⬇️
lightning/src/ln/functional_tests.rs96.90% <100.00%> (-0.01%)⬇️
lightning/src/util/macro_logger.rs86.15% <100.00%> (ø)
lightning/src/util/fairrwlock.rs85.71% <0.00%> (-2.53%)⬇️
... and 45 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.

@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 523c412 to 27a74feCompareDecember 19, 2022 22:09
Comment threadlightning/src/ln/features.rs Outdated
Comment threadlightning/src/ln/channel.rs Outdated
F::Target: FeeEstimator,
{
let opt_anchors = false; // TODO - should be based on features
#[cfg(anchors)]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is only used for "is it even possible to build a channel with this amount". Should we just drop this and always pass anchors (since its a slightly higher commitment tx fee)? Otherwise I'm worried about making decisions based on the anchor flag which isn't really right, its just the initial state.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

It's also setting the value in ChannelTransactionParameters. That of course won't be locked in until we receive an accept_channel with the same channel_type we extended. Are you also suggesting we delay setting that value in ChannelTransactionParameters until then?

Comment threadlightning/src/ln/channel.rs Outdated
Comment threadlightning/src/ln/channelmanager.rs Outdated
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 27a74fe to a88d650CompareDecember 22, 2022 19:51
Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/util/config.rs
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch 2 times, most recently from 9cf8351 to 404f3d1CompareJanuary 4, 2023 00:35
Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/ln/features.rs
Comment threadlightning/src/ln/monitor_tests.rs

@TheBlueMattTheBlueMatt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. Needs a pretty trivial rebase to drop the first commit.

Comment threadlightning/src/ln/channel.rs
Comment threadlightning/src/ln/channel.rs

@ariardariard left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Otherwise test coverage sounds good.


#[cfg(anchors)]
#[test]
fn test_supports_anchors_zero_htlc_tx_fee() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I don't know if the flow that matters, though when our negotiate_anchors_zero_fee_htlc_tx=true and their init features option_anchors_zero_fee_htlc_tx=false doesn't seem cover:

diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs
index 8cf5e54f..b552b61b 100644
--- a/lightning/src/ln/channel.rs
+++ b/lightning/src/ln/channel.rs
@@ -892,10 +892,8 @@ impl<Signer: Sign> Channel<Signer> {
// Optionally, if the user would like to negotiate the `anchors_zero_fee_htlc_tx` option, we
// set it now. If they don't understand it, we'll fall back to our default of
// `only_static_remotekey`.
- #[cfg(anchors)]
{ // Attributes are not allowed on if expressions on our current MSRV of 1.41.
- if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx &&
- their_features.supports_anchors_zero_fee_htlc_tx() {
+ if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx {
ret.set_anchors_zero_fee_htlc_tx_required();
}
}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Should be covered now.

// We support opening a few different types of channels. Try removing our additional
// features one by one until we've either arrived at our default or the counterparty has
// accepted one.
if self.channel_type.supports_anchors_zero_fee_htlc_tx() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do we have test coverage for handle_error once we got an error message due to rejection of channel type ?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Should be covered now.

@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 7e7abb5 to 6dbcde5CompareJanuary 17, 2023 23:03
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 6dbcde5 to 202ff7fCompareJanuary 17, 2023 23:04
@wpaulino

Copy link
Copy Markdown
ContributorAuthor

Would appreciate a thorough pass from reviewers as it's been a while since the previous review iteration.

TheBlueMatt
TheBlueMatt previously approved these changes Jan 18, 2023

@TheBlueMattTheBlueMatt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, comments can be in a followup or the comment request ignored.

// We support opening a few different types of channels. Try removing our additional
// features one by one until we've either arrived at our default or the counterparty has
// accepted one.
if self.channel_type.supports_anchors_zero_fee_htlc_tx() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

May be worth adding a comment that the ordering here would mean that we'd never negotiate anchor with a peer that supports anchor but not scid privacy, except that we check the peer's init flags above so in general this code should really only apply for a peer that is setting init flags but not actually supporting the channel type, which should be very rare.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

May be worth adding a comment that the ordering here would mean that we'd never negotiate anchor with a peer that supports anchor but not scid privacy

Comment added.

this code should really only apply for a peer that is setting init flags but not actually supporting the channel type, which should be very rare

It won't be too rare with anchors if the inbound node doesn't have enough onchain funds to bump their commitments. They'll still advertise the feature, but reject the channel.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sure, sorry, my point was if we didn't do the check initially we'd never negotiate anchors for lots of old lnd nodes - because anchors predates the scid privacy stuff we'd always end up with no features. As long as we check their features, we're safe from that.

if self.channel_type.supports_anchors_zero_fee_htlc_tx() {
self.channel_type.clear_anchors_zero_fee_htlc_tx();
assert!(self.channel_transaction_parameters.opt_non_zero_fee_anchors.is_none());
self.channel_transaction_parameters.opt_anchors = None;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Given we're only supporting anchors with channel type negotiation, we should probably just drop opt_anchors entirely and do channel_type.supports... instead? Can come in a followup though since its a new change...I really do hate having duplicate state tracking the same thing :)

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I don't think we can drop ChannelTransactionParamerters.opt_anchors since signers rely on it in provide_channel_parameters.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We can regenerate it for them, though, rather than storing it explicitly in Channel and in its serialized form.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Ah, I'll look into this later then.

Comment threadlightning/src/ln/monitor_tests.rs Outdated
Comment threadlightning/src/ln/channel.rs
arik-so
arik-so previously approved these changes Jan 18, 2023
@wpaulino
wpaulino dismissed stale reviews from arik-so and TheBlueMatt via 660165cJanuary 18, 2023 22:49
@wpaulino
wpaulinoforce-pushed the open-channel-anchors-support branch from 202ff7f to 660165cCompareJanuary 18, 2023 22:49
/// If set, we attempt to negotiate the `anchors_zero_fee_htlc_tx`option for outbound channels.
///
/// If this option is set, channels may be created that will not be readable by LDK versions
/// prior to 0.0.114, causing [`ChannelManager`]'s read method to return a

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reminder that we may need to update this when we make anchor not cfg-gated anymore. Would be nice if we didn't and that happens in 0.0.114, but if not, that's okay too.

@TheBlueMatt
TheBlueMatt merged commit 50d1260 into lightningdevkit:mainJan 19, 2023
@wpaulino
wpaulino deleted the open-channel-anchors-support branch January 19, 2023 01:03
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@wpaulino@ariard@TheBlueMatt@codecov-commenter@arik-so