2019-12-10 17:04:10 +03:00
|
|
|
package sweep
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
"github.com/lightningnetwork/lnd/input"
|
2019-12-10 18:06:45 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2019-12-10 17:04:10 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestTxInputSet tests adding various sized inputs to the set.
|
|
|
|
func TestTxInputSet(t *testing.T) {
|
|
|
|
const (
|
|
|
|
feeRate = 1000
|
|
|
|
relayFee = 300
|
|
|
|
maxInputs = 10
|
|
|
|
)
|
2019-12-10 18:06:45 +03:00
|
|
|
set := newTxInputSet(nil, feeRate, relayFee, maxInputs)
|
2019-12-10 17:04:10 +03:00
|
|
|
|
|
|
|
if set.dustLimit != 537 {
|
|
|
|
t.Fatalf("incorrect dust limit")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a 300 sat input. The fee to sweep this input to a P2WKH output
|
|
|
|
// is 439 sats. That means that this input yields -139 sats and we
|
|
|
|
// expect it not to be added.
|
2019-12-13 17:04:32 +03:00
|
|
|
if set.add(createP2WKHInput(300), constraintsRegular) {
|
2019-12-10 17:04:10 +03:00
|
|
|
t.Fatal("expected add of negatively yielding input to fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
// A 700 sat input should be accepted into the set, because it yields
|
|
|
|
// positively.
|
2019-12-13 17:04:32 +03:00
|
|
|
if !set.add(createP2WKHInput(700), constraintsRegular) {
|
2019-12-10 17:04:10 +03:00
|
|
|
t.Fatal("expected add of positively yielding input to succeed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// The tx output should now be 700-439 = 261 sats. The dust limit isn't
|
|
|
|
// reached yet.
|
|
|
|
if set.outputValue != 261 {
|
|
|
|
t.Fatal("unexpected output value")
|
|
|
|
}
|
|
|
|
if set.dustLimitReached() {
|
|
|
|
t.Fatal("expected dust limit not yet to be reached")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a 1000 sat input. This increases the tx fee to 712 sats. The tx
|
|
|
|
// output should now be 1000+700 - 712 = 988 sats.
|
2019-12-13 17:04:32 +03:00
|
|
|
if !set.add(createP2WKHInput(1000), constraintsRegular) {
|
2019-12-10 17:04:10 +03:00
|
|
|
t.Fatal("expected add of positively yielding input to succeed")
|
|
|
|
}
|
|
|
|
if set.outputValue != 988 {
|
|
|
|
t.Fatal("unexpected output value")
|
|
|
|
}
|
|
|
|
if !set.dustLimitReached() {
|
|
|
|
t.Fatal("expected dust limit to be reached")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 18:06:45 +03:00
|
|
|
// TestTxInputSetFromWallet tests adding a wallet input to a TxInputSet to reach
|
|
|
|
// the dust limit.
|
|
|
|
func TestTxInputSetFromWallet(t *testing.T) {
|
|
|
|
const (
|
|
|
|
feeRate = 500
|
|
|
|
relayFee = 300
|
|
|
|
maxInputs = 10
|
|
|
|
)
|
|
|
|
|
|
|
|
wallet := &mockWallet{}
|
|
|
|
set := newTxInputSet(wallet, feeRate, relayFee, maxInputs)
|
|
|
|
|
|
|
|
// Add a 700 sat input to the set. It yields positively, but doesn't
|
|
|
|
// reach the output dust limit.
|
2019-12-13 17:04:32 +03:00
|
|
|
if !set.add(createP2WKHInput(700), constraintsRegular) {
|
2019-12-10 18:06:45 +03:00
|
|
|
t.Fatal("expected add of positively yielding input to succeed")
|
|
|
|
}
|
|
|
|
if set.dustLimitReached() {
|
|
|
|
t.Fatal("expected dust limit not yet to be reached")
|
|
|
|
}
|
|
|
|
|
2019-12-09 17:40:05 +03:00
|
|
|
// Expect that adding a negative yield input fails.
|
|
|
|
if set.add(createP2WKHInput(50), constraintsRegular) {
|
|
|
|
t.Fatal("expected negative yield input add to fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force add the negative yield input. It should succeed.
|
|
|
|
if !set.add(createP2WKHInput(50), constraintsForce) {
|
|
|
|
t.Fatal("expected forced add to succeed")
|
|
|
|
}
|
|
|
|
|
2019-12-10 18:06:45 +03:00
|
|
|
err := set.tryAddWalletInputsIfNeeded()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !set.dustLimitReached() {
|
|
|
|
t.Fatal("expected dust limit to be reached")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 17:04:10 +03:00
|
|
|
// createP2WKHInput returns a P2WKH test input with the specified amount.
|
|
|
|
func createP2WKHInput(amt btcutil.Amount) input.Input {
|
|
|
|
input := createTestInput(int64(amt), input.WitnessKeyHash)
|
|
|
|
return &input
|
|
|
|
}
|
2019-12-10 18:06:45 +03:00
|
|
|
|
|
|
|
type mockWallet struct {
|
|
|
|
Wallet
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockWallet) ListUnspentWitness(minconfirms, maxconfirms int32) (
|
|
|
|
[]*lnwallet.Utxo, error) {
|
|
|
|
|
|
|
|
return []*lnwallet.Utxo{
|
|
|
|
{
|
|
|
|
AddressType: lnwallet.WitnessPubKey,
|
|
|
|
Value: 10000,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|