-
Notifications
You must be signed in to change notification settings - Fork 912
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Channel lockup corner case workaround #3500
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -363,8 +363,19 @@ static bool get_room_above_reserve(const struct channel *channel, | |
return true; | ||
} | ||
|
||
static size_t num_untrimmed_htlcs(enum side side, | ||
struct amount_sat dust_limit, | ||
u32 feerate, | ||
const struct htlc **committed, | ||
const struct htlc **adding, | ||
const struct htlc **removing) | ||
{ | ||
return commit_tx_num_untrimmed(committed, feerate, dust_limit, side) | ||
+ commit_tx_num_untrimmed(adding, feerate, dust_limit, side) | ||
- commit_tx_num_untrimmed(removing, feerate, dust_limit, side); | ||
} | ||
|
||
static struct amount_sat fee_for_htlcs(const struct channel *channel, | ||
const struct channel_view *view, | ||
const struct htlc **committed, | ||
const struct htlc **adding, | ||
const struct htlc **removing, | ||
|
@@ -374,16 +385,64 @@ static struct amount_sat fee_for_htlcs(const struct channel *channel, | |
struct amount_sat dust_limit = channel->config[side].dust_limit; | ||
size_t untrimmed; | ||
|
||
untrimmed = commit_tx_num_untrimmed(committed, feerate, dust_limit, | ||
side) | ||
+ commit_tx_num_untrimmed(adding, feerate, dust_limit, | ||
side) | ||
- commit_tx_num_untrimmed(removing, feerate, dust_limit, | ||
side); | ||
untrimmed = num_untrimmed_htlcs(side, dust_limit, feerate, | ||
committed, adding, removing); | ||
|
||
return commit_tx_base_fee(feerate, untrimmed); | ||
} | ||
|
||
/* There is a corner case where the funder can spend so much that the | ||
* non-funder can't add any non-dust HTLCs (since the funder would | ||
* have to pay the additional fee, but it can't afford to). This | ||
* leads to the channel starving at the feast! This was reported by | ||
* ACINQ's @t-bast | ||
* (https://github.com/lightningnetwork/lightning-rfc/issues/728) and | ||
* demonstrated with c-lightning by @m-schmook | ||
* (https://github.com/ElementsProject/lightning/pull/3498). | ||
* | ||
* To mostly avoid this situation, at least from our side, we apply an | ||
* additional constraint when we're funder trying to add an HTLC: make | ||
* sure we can afford one more HTLC, even if fees increase 50%. | ||
* | ||
* We could do this for the peer, as well, by rejecting their HTLC | ||
* immediately in this case. But rejecting a remote HTLC here causes | ||
* us to get upset with them and close the channel: we're not well | ||
* architected to reject HTLCs in channeld (it's usually lightningd's | ||
* job, but it doesn't have all the channel balance change calculation | ||
* logic. So we look after ourselves for now, and hope other nodes start | ||
* self-regulating too. */ | ||
static bool local_funder_has_fee_headroom(const struct channel *channel, | ||
struct amount_msat remainder, | ||
const struct htlc **committed, | ||
const struct htlc **adding, | ||
const struct htlc **removing) | ||
{ | ||
u32 feerate = channel_feerate(channel, LOCAL); | ||
size_t untrimmed; | ||
struct amount_sat fee; | ||
|
||
assert(channel->funder == LOCAL); | ||
|
||
/* How many untrimmed at current feerate? Increasing feerate can | ||
* only *reduce* this number, so use current feerate here! */ | ||
untrimmed = num_untrimmed_htlcs(LOCAL, channel->config[LOCAL].dust_limit, | ||
feerate, | ||
committed, adding, removing); | ||
|
||
/* Now, how much would it cost us if feerate increases 50% and we added | ||
* another HTLC? */ | ||
fee = commit_tx_base_fee(feerate + feerate/2, untrimmed + 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cleanup: we should split out the |
||
if (amount_msat_greater_eq_sat(remainder, fee)) | ||
return true; | ||
|
||
status_debug("Adding HTLC would leave us only %s:" | ||
" we need %s for another HTLC if fees increase 50%% to %uperkw", | ||
type_to_string(tmpctx, struct amount_msat, &remainder), | ||
type_to_string(tmpctx, struct amount_sat, &fee), | ||
feerate + feerate/2); | ||
return false; | ||
} | ||
|
||
static enum channel_add_err add_htlc(struct channel *channel, | ||
enum htlc_state state, | ||
u64 id, | ||
|
@@ -536,7 +595,7 @@ static enum channel_add_err add_htlc(struct channel *channel, | |
*/ | ||
if (enforce_aggregate_limits) { | ||
struct amount_msat remainder; | ||
struct amount_sat fee = fee_for_htlcs(channel, view, | ||
struct amount_sat fee = fee_for_htlcs(channel, | ||
committed, | ||
adding, | ||
removing, | ||
|
@@ -565,6 +624,15 @@ static enum channel_add_err add_htlc(struct channel *channel, | |
&remainder)); | ||
return CHANNEL_ERR_CHANNEL_CAPACITY_EXCEEDED; | ||
} | ||
|
||
if (sender == LOCAL | ||
&& !local_funder_has_fee_headroom(channel, | ||
remainder, | ||
committed, | ||
adding, | ||
removing)) { | ||
return CHANNEL_ERR_CHANNEL_CAPACITY_EXCEEDED; | ||
} | ||
} | ||
|
||
/* Try not to add a payment which will take funder into fees | ||
|
@@ -578,7 +646,7 @@ static enum channel_add_err add_htlc(struct channel *channel, | |
/* Should be able to afford both their own commit tx | ||
* fee, and other's commit tx fee, which are subtly | ||
* different! */ | ||
fee = fee_for_htlcs(channel, view, | ||
fee = fee_for_htlcs(channel, | ||
committed, | ||
adding, | ||
removing, | ||
|
@@ -596,7 +664,7 @@ static enum channel_add_err add_htlc(struct channel *channel, | |
&remainder)); | ||
return CHANNEL_ERR_CHANNEL_CAPACITY_EXCEEDED; | ||
} | ||
fee = fee_for_htlcs(channel, view, | ||
fee = fee_for_htlcs(channel, | ||
committed, | ||
adding, | ||
removing, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case the peer would have to be funder and us the fundee?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.