-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6703 from bottlepay/inbound-fees
htlcswitch: add inbound routing fees receive support
- Loading branch information
Showing
23 changed files
with
2,156 additions
and
1,444 deletions.
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
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
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 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 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
Oops, something went wrong.