-
Notifications
You must be signed in to change notification settings - Fork 2.2k
htlcswitch: add inbound routing fees receive support #6703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3e6adbf
lnwire+channeldb: parse inbound fees
joostjager e8c97de
htlcswitch: add receiver-side inbound fee support
joostjager 763787e
routing+lnrpc: add inbound fee policy update
joostjager ba21ca7
itest: multi-hop payment test with negative inbound fee
joostjager 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package models | ||
|
||
import "github.com/lightningnetwork/lnd/lnwire" | ||
|
||
const ( | ||
// maxFeeRate is the maximum fee rate that we allow. It is set to allow | ||
// a variable fee component of up to 10x the payment amount. | ||
maxFeeRate = 10 * feeRateParts | ||
) | ||
|
||
type InboundFee struct { | ||
Base int32 | ||
Rate int32 | ||
} | ||
|
||
// NewInboundFeeFromWire constructs an inbound fee structure from a wire fee. | ||
func NewInboundFeeFromWire(fee lnwire.Fee) InboundFee { | ||
return InboundFee{ | ||
Base: fee.BaseFee, | ||
Rate: fee.FeeRate, | ||
} | ||
} | ||
|
||
// ToWire converts the inbound fee to a wire fee structure. | ||
func (i *InboundFee) ToWire() lnwire.Fee { | ||
return lnwire.Fee{ | ||
BaseFee: i.Base, | ||
FeeRate: i.Rate, | ||
} | ||
} | ||
|
||
// CalcFee calculates what the inbound fee should minimally be for forwarding | ||
// the given amount. This amount is the total of the outgoing amount plus the | ||
// outbound fee, which is what the inbound fee is based on. | ||
func (i *InboundFee) CalcFee(amt lnwire.MilliSatoshi) int64 { | ||
fee := int64(i.Base) | ||
rate := int64(i.Rate) | ||
|
||
// Cap the rate to prevent overflows. | ||
switch { | ||
case rate > maxFeeRate: | ||
rate = maxFeeRate | ||
|
||
case rate < -maxFeeRate: | ||
rate = -maxFeeRate | ||
} | ||
|
||
// Calculate proportional component. To keep the integer math simple, | ||
// positive fees are rounded down while negative fees are rounded up. | ||
fee += rate * int64(amt) / feeRateParts | ||
|
||
return fee | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package models | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestInboundFee(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Test positive fee. | ||
i := InboundFee{ | ||
Base: 5, | ||
Rate: 500000, | ||
} | ||
|
||
require.Equal(t, int64(6), i.CalcFee(2)) | ||
|
||
// Expect fee to be rounded down. | ||
require.Equal(t, int64(6), i.CalcFee(3)) | ||
|
||
// Test negative fee. | ||
i = InboundFee{ | ||
Base: -5, | ||
Rate: -500000, | ||
} | ||
|
||
require.Equal(t, int64(-6), i.CalcFee(2)) | ||
|
||
// Expect fee to be rounded up. | ||
require.Equal(t, int64(-6), i.CalcFee(3)) | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.