sweep: account for all script types in craftSweepTx
With this change, transactions created via craftSweepTx will be standard. Previously, p2wsh/p2pkh scripts passed in via SendCoins would be weighted as p2wpkh scripts. With a feerate of 1 sat/vbyte, transactions returned would be non-standard. Luckily, the critical sweeper subsystem only used p2wpkh scripts so this only affected callers from the rpcserver. Also added is an integration test that fails if SendCoins manages to generate a non-standard transaction. All script types are now accounted for in getWeightEstimate, which now errors if an unknown script type is passed in.
This commit is contained in:
6 files changed
+311
-15
No files matched your search
+108
-1
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/lightningnetwork/lnd/input"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -39,7 +40,19 @@ func TestWeightEstimate(t *testing.T) {
|
||||
))
|
||||
}
|
||||
|
||||
_, estimator := getWeightEstimate(inputs, nil, 0, nil)
|
||||
// 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,
|
||||
}
|
||||
|
||||
_, estimator, err := getWeightEstimate(inputs, nil, 0, changePkScript)
|
||||
require.NoError(t, err)
|
||||
|
||||
weight := int64(estimator.weight())
|
||||
if weight != expectedWeight {
|
||||
t.Fatalf("unexpected weight. expected %d but got %d.",
|
||||
@@ -51,3 +64,97 @@ func TestWeightEstimate(t *testing.T) {
|
||||
expectedSummary, summary)
|
||||
}
|
||||
}
|
||||
|
||||
// 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,
|
||||
))
|
||||
}
|
||||
|
||||
_, _, err := getWeightEstimate(inputs, nil, 0, pkscript)
|
||||
if expectFail {
|
||||
require.Error(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user