mirror of
https://github.com/lightningnetwork/lnd.git
synced 2026-08-13 12:32:48 +02:00
In this commit, we have DustLimitForSize fall back to the generic witness dust threshold for any script size that doesn't match one of the well-known templates. The size switch covered P2WPKH, P2WSH, P2SH, P2PKH, and the explicit unknown-witness size, and treated every other length as unreachable. That's a narrower assumption than the callers can actually make good on: a witness program for versions 1 through 16 carries a program of anywhere from 2 to 40 bytes, so its serialized length won't always land on one of those exact values. The dust calculation only needs a representative output of roughly the right shape, and the unknown-witness pricing is the conservative choice among the ones we have, so we make it the default. That leaves the helper well defined across the whole range of sizes callers can pass it, including scripts carrying witness versions we don't know about yet.
108 lines
2.6 KiB
Go
108 lines
2.6 KiB
Go
package lnwallet
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/btcsuite/btcd/btcutil/v2"
|
|
"github.com/lightningnetwork/lnd/input"
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// TestDefaultRoutingFeeLimitForAmount tests that we use the correct default
|
|
// routing fee depending on the amount.
|
|
func TestDefaultRoutingFeeLimitForAmount(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
amount lnwire.MilliSatoshi
|
|
expectedLimit lnwire.MilliSatoshi
|
|
}{
|
|
{
|
|
amount: 1,
|
|
expectedLimit: 1,
|
|
},
|
|
{
|
|
amount: lnwire.NewMSatFromSatoshis(1_000),
|
|
expectedLimit: lnwire.NewMSatFromSatoshis(1_000),
|
|
},
|
|
{
|
|
amount: lnwire.NewMSatFromSatoshis(1_001),
|
|
expectedLimit: 50_050,
|
|
},
|
|
{
|
|
amount: 5_000_000_000,
|
|
expectedLimit: 250_000_000,
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%d sats", test.amount), func(t *testing.T) {
|
|
feeLimit := DefaultRoutingFeeLimitForAmount(test.amount)
|
|
require.Equal(t, int64(test.expectedLimit), int64(feeLimit))
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestDustLimitForSize tests that we receive the expected dust limits for
|
|
// various script types from btcd's GetDustThreshold function.
|
|
func TestDustLimitForSize(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
size int
|
|
expectedLimit btcutil.Amount
|
|
}{
|
|
{
|
|
name: "p2pkh dust limit",
|
|
size: input.P2PKHSize,
|
|
expectedLimit: btcutil.Amount(546),
|
|
},
|
|
{
|
|
name: "p2sh dust limit",
|
|
size: input.P2SHSize,
|
|
expectedLimit: btcutil.Amount(540),
|
|
},
|
|
{
|
|
name: "p2wpkh dust limit",
|
|
size: input.P2WPKHSize,
|
|
expectedLimit: btcutil.Amount(294),
|
|
},
|
|
{
|
|
name: "p2wsh dust limit",
|
|
size: input.P2WSHSize,
|
|
expectedLimit: btcutil.Amount(330),
|
|
},
|
|
{
|
|
name: "unknown witness limit",
|
|
size: input.UnknownWitnessSize,
|
|
expectedLimit: btcutil.Amount(354),
|
|
},
|
|
{
|
|
// An arbitrary short length that matches no known
|
|
// template is priced as a generic witness output
|
|
// rather than treated as an error.
|
|
name: "arbitrary small size",
|
|
size: 7,
|
|
expectedLimit: btcutil.Amount(354),
|
|
},
|
|
{
|
|
// The largest witness program length is also handled
|
|
// as a generic witness output.
|
|
name: "arbitrary large witness size",
|
|
size: 42,
|
|
expectedLimit: btcutil.Amount(354),
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
dustlimit := DustLimitForSize(test.size)
|
|
require.Equal(t, test.expectedLimit, dustlimit)
|
|
})
|
|
}
|
|
}
|