2019-10-09 17:14:22 +02:00
|
|
|
package sweep
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-03-19 16:51:32 +01:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2019-10-09 17:14:22 +02:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/lightningnetwork/lnd/input"
|
2022-07-15 22:25:31 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-10-09 17:14:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
witnessTypes = []input.WitnessType{
|
|
|
|
input.CommitmentTimeLock,
|
|
|
|
input.HtlcAcceptedSuccessSecondLevel,
|
|
|
|
input.HtlcOfferedRemoteTimeout,
|
|
|
|
input.WitnessKeyHash,
|
|
|
|
}
|
2020-11-17 13:23:00 +01:00
|
|
|
expectedWeight = int64(1460)
|
2020-03-19 16:51:32 +01:00
|
|
|
expectedSummary = "0000000000000000000000000000000000000000000000000000000000000000:10 (CommitmentTimeLock), " +
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000001:11 (HtlcAcceptedSuccessSecondLevel), " +
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000002:12 (HtlcOfferedRemoteTimeout), " +
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000003:13 (WitnessKeyHash)"
|
2019-10-09 17:14:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestWeightEstimate tests that the estimated weight and number of CSVs/CLTVs
|
|
|
|
// used is correct for a transaction that uses inputs with the witness types
|
|
|
|
// defined in witnessTypes.
|
|
|
|
func TestWeightEstimate(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var inputs []input.Input
|
2020-03-19 16:51:32 +01:00
|
|
|
for i, witnessType := range witnessTypes {
|
2019-10-09 17:14:22 +02:00
|
|
|
inputs = append(inputs, input.NewBaseInput(
|
2020-03-19 16:51:32 +01:00
|
|
|
&wire.OutPoint{
|
|
|
|
Hash: chainhash.Hash{byte(i)},
|
|
|
|
Index: uint32(i) + 10,
|
|
|
|
}, witnessType,
|
2019-10-09 17:14:22 +02:00
|
|
|
&input.SignDescriptor{}, 0,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2022-07-15 22:25:31 +02:00
|
|
|
// Create a sweep script that is always fed into the weight estimator,
|
|
|
|
// regardless if it's actually included in the tx. It will be a P2WKH
|
|
|
|
// script.
|
|
|
|
changePkScript := []byte{
|
|
|
|
0x00, 0x14,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00,
|
|
|
|
}
|
|
|
|
|
2023-08-22 05:06:51 +02:00
|
|
|
_, estimator, err := getWeightEstimate(
|
|
|
|
inputs, nil, 0, 0, changePkScript,
|
|
|
|
)
|
2022-07-15 22:25:31 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-09-04 11:28:17 +02:00
|
|
|
weight := int64(estimator.weight())
|
2019-10-09 17:14:22 +02:00
|
|
|
if weight != expectedWeight {
|
|
|
|
t.Fatalf("unexpected weight. expected %d but got %d.",
|
|
|
|
expectedWeight, weight)
|
|
|
|
}
|
2019-10-23 13:00:25 +02:00
|
|
|
summary := inputTypeSummary(inputs)
|
|
|
|
if summary != expectedSummary {
|
|
|
|
t.Fatalf("unexpected summary. expected %s but got %s.",
|
|
|
|
expectedSummary, summary)
|
2019-10-09 17:14:22 +02:00
|
|
|
}
|
|
|
|
}
|
2022-07-15 22:25:31 +02:00
|
|
|
|
|
|
|
// TestWeightEstimatorUnknownScript tests that the weight estimator fails when
|
|
|
|
// given an unknown script and succeeds when given a known script.
|
|
|
|
func TestWeightEstimatorUnknownScript(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
pkscript []byte
|
|
|
|
expectFail bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "p2tr output",
|
|
|
|
pkscript: []byte{
|
|
|
|
0x51, 0x20,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "p2wsh output",
|
|
|
|
pkscript: []byte{
|
|
|
|
0x00, 0x20,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "p2wkh output",
|
|
|
|
pkscript: []byte{
|
|
|
|
0x00, 0x14,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "p2pkh output",
|
|
|
|
pkscript: []byte{
|
|
|
|
0x76, 0xa9, 0x14,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x88, 0xac,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "p2sh output",
|
|
|
|
pkscript: []byte{
|
|
|
|
0xa9, 0x14,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x87,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unknown script",
|
|
|
|
pkscript: []byte{0x00},
|
|
|
|
expectFail: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
testUnknownScriptInner(
|
|
|
|
t, test.pkscript, test.expectFail,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testUnknownScriptInner(t *testing.T, pkscript []byte, expectFail bool) {
|
|
|
|
var inputs []input.Input
|
|
|
|
for i, witnessType := range witnessTypes {
|
|
|
|
inputs = append(inputs, input.NewBaseInput(
|
|
|
|
&wire.OutPoint{
|
|
|
|
Hash: chainhash.Hash{byte(i)},
|
|
|
|
Index: uint32(i) + 10,
|
|
|
|
}, witnessType,
|
|
|
|
&input.SignDescriptor{}, 0,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2023-08-22 05:06:51 +02:00
|
|
|
_, _, err := getWeightEstimate(inputs, nil, 0, 0, pkscript)
|
2022-07-15 22:25:31 +02:00
|
|
|
if expectFail {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|