sweep: remove unnecessary use of FetchInputInfo within CraftSweepAllTx

We already have all of the information required for the outputs from the
ListUnspent method.
This commit is contained in:
Wilmer Paulino 2019-08-19 14:25:51 -07:00
parent c7bdfe149a
commit 8b398bdbd1
No known key found for this signature in database
GPG Key ID: 6DF57B9F9514972F
2 changed files with 13 additions and 43 deletions

@ -101,11 +101,6 @@ type UtxoSource interface {
// ListUnspentWitness returns all UTXOs from the source that have
// between minConfs and maxConfs number of confirmations.
ListUnspentWitness(minConfs, maxConfs int32) ([]*lnwallet.Utxo, error)
// FetchInputInfo returns the matching output for an outpoint. If the
// outpoint doesn't belong to this UTXO source, then an error should be
// returned.
FetchInputInfo(*wire.OutPoint) (*wire.TxOut, error)
}
// CoinSelectionLocker is an interface that allows the caller to perform an
@ -217,41 +212,34 @@ func CraftSweepAllTx(feeRate lnwallet.SatPerKWeight, blockHeight uint32,
// sweeper to generate and sign a transaction for us.
var inputsToSweep []input.Input
for _, output := range allOutputs {
// We'll consult the utxoSource for information concerning this
// outpoint, we'll need to properly populate a signDescriptor
// for this output.
outputInfo, err := utxoSource.FetchInputInfo(&output.OutPoint)
if err != nil {
unlockOutputs()
return nil, err
}
// As we'll be signing for outputs under control of the wallet,
// we only need to populate the output value and output script.
// The rest of the items will be populated internally within
// the sweeper via the witness generation function.
signDesc := &input.SignDescriptor{
Output: outputInfo,
Output: &wire.TxOut{
PkScript: output.PkScript,
Value: int64(output.Value),
},
HashType: txscript.SigHashAll,
}
pkScript := outputInfo.PkScript
pkScript := output.PkScript
// Based on the output type, we'll map it to the proper witness
// type so we can generate the set of input scripts needed to
// sweep the output.
var witnessType input.WitnessType
switch {
switch output.AddressType {
// If this is a p2wkh output, then we'll assume it's a witness
// key hash witness type.
case txscript.IsPayToWitnessPubKeyHash(pkScript):
case lnwallet.WitnessPubKey:
witnessType = input.WitnessKeyHash
// If this is a p2sh output, then as since it's under control
// of the wallet, we'll assume it's a nested p2sh output.
case txscript.IsPayToScriptHash(pkScript):
case lnwallet.NestedWitnessPubKey:
witnessType = input.NestedWitnessKeyHash
// All other output types we count as unknown and will fail to

@ -108,25 +108,13 @@ func TestDetermineFeePerKw(t *testing.T) {
}
type mockUtxoSource struct {
outpoints map[wire.OutPoint]*wire.TxOut
outputs []*lnwallet.Utxo
}
func newMockUtxoSource(utxos []*lnwallet.Utxo) *mockUtxoSource {
m := &mockUtxoSource{
outputs: utxos,
outpoints: make(map[wire.OutPoint]*wire.TxOut),
return &mockUtxoSource{
outputs: utxos,
}
for _, utxo := range utxos {
m.outpoints[utxo.OutPoint] = &wire.TxOut{
Value: int64(utxo.Value),
PkScript: utxo.PkScript,
}
}
return m
}
func (m *mockUtxoSource) ListUnspentWitness(minConfs int32,
@ -135,15 +123,6 @@ func (m *mockUtxoSource) ListUnspentWitness(minConfs int32,
return m.outputs, nil
}
func (m *mockUtxoSource) FetchInputInfo(op *wire.OutPoint) (*wire.TxOut, error) {
txOut, ok := m.outpoints[*op]
if !ok {
return nil, fmt.Errorf("no output found")
}
return txOut, nil
}
type mockCoinSelectionLocker struct {
fail bool
}
@ -202,6 +181,7 @@ var deliveryAddr = func() btcutil.Address {
var testUtxos = []*lnwallet.Utxo{
{
// A p2wkh output.
AddressType: lnwallet.WitnessPubKey,
PkScript: []byte{
0x0, 0x14, 0x64, 0x3d, 0x8b, 0x15, 0x69, 0x4a, 0x54,
0x7d, 0x57, 0x33, 0x6e, 0x51, 0xdf, 0xfd, 0x38, 0xe3,
@ -215,6 +195,7 @@ var testUtxos = []*lnwallet.Utxo{
{
// A np2wkh output.
AddressType: lnwallet.NestedWitnessPubKey,
PkScript: []byte{
0xa9, 0x14, 0x97, 0x17, 0xf7, 0xd1, 0x5f, 0x6f, 0x8b,
0x7, 0xe3, 0x58, 0x43, 0x19, 0xb9, 0x7e, 0xa9, 0x20,
@ -228,6 +209,7 @@ var testUtxos = []*lnwallet.Utxo{
// A p2wsh output.
{
AddressType: lnwallet.UnknownAddressType,
PkScript: []byte{
0x0, 0x20, 0x70, 0x1a, 0x8d, 0x40, 0x1c, 0x84, 0xfb, 0x13,
0xe6, 0xba, 0xf1, 0x69, 0xd5, 0x96, 0x84, 0xe2, 0x7a, 0xbd,