mirror of
https://github.com/lightninglabs/loop.git
synced 2026-08-13 12:33:03 +02:00
utils: function to get dust limit for a pkscript.
This commit is contained in:
parent
bce9b5d45d
commit
94168c3fe5
2 changed files with 47 additions and 0 deletions
15
utils/dust_limit.go
Normal file
15
utils/dust_limit.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"github.com/btcsuite/btcd/btcutil"
|
||||
"github.com/btcsuite/btcd/mempool"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
)
|
||||
|
||||
// DustLimitForPkScript returns the dust limit for a given pkScript. An output
|
||||
// must be greater or equal to this value.
|
||||
func DustLimitForPkScript(pkscript []byte) btcutil.Amount {
|
||||
return btcutil.Amount(mempool.GetDustThreshold(&wire.TxOut{
|
||||
PkScript: pkscript,
|
||||
}))
|
||||
}
|
||||
32
utils/dust_limit_test.go
Normal file
32
utils/dust_limit_test.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/lightningnetwork/lnd/input"
|
||||
"github.com/lightningnetwork/lnd/lnwallet"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type pkScriptGetter func([]byte) ([]byte, error)
|
||||
|
||||
// TestDustLimitForPkScript checks that the dust limit for a given script size
|
||||
// matches the calculation in lnwallet.DustLimitForSize.
|
||||
func TestDustLimitForPkScript(t *testing.T) {
|
||||
getScripts := map[int]pkScriptGetter{
|
||||
input.P2WPKHSize: input.WitnessPubKeyHash,
|
||||
input.P2WSHSize: input.WitnessScriptHash,
|
||||
input.P2SHSize: input.GenerateP2SH,
|
||||
input.P2PKHSize: input.GenerateP2PKH,
|
||||
}
|
||||
|
||||
for scriptSize, getPkScript := range getScripts {
|
||||
pkScript, err := getPkScript([]byte{})
|
||||
require.NoError(t, err, "failed to generate pkScript")
|
||||
|
||||
require.Equal(
|
||||
t, lnwallet.DustLimitForSize(scriptSize),
|
||||
DustLimitForPkScript(pkScript),
|
||||
)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue