htlcswitch: add receiver-side inbound fee support
This commit is contained in:
1 parent
3e6adbf1c0
commit
e8c97deaef
10 files changed
+388
-19
No files matched your search
@@ -115,6 +115,9 @@ type ForwardingPolicy struct {
|
||||
// used to compute the required fee for a given HTLC.
|
||||
FeeRate lnwire.MilliSatoshi
|
||||
|
||||
// InboundFee is the fee that must be paid for incoming HTLCs.
|
||||
InboundFee InboundFee
|
||||
|
||||
// TimeLockDelta is the absolute time-lock value, expressed in blocks,
|
||||
// that will be subtracted from an incoming HTLC's timelock value to
|
||||
// create the time-lock value for the forwarded outgoing HTLC. The
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
Reference in new issue
Block a user