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 // ListUnspentWitness returns all UTXOs from the source that have
// between minConfs and maxConfs number of confirmations. // between minConfs and maxConfs number of confirmations.
ListUnspentWitness(minConfs, maxConfs int32) ([]*lnwallet.Utxo, error) 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 // 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. // sweeper to generate and sign a transaction for us.
var inputsToSweep []input.Input var inputsToSweep []input.Input
for _, output := range allOutputs { 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, // As we'll be signing for outputs under control of the wallet,
// we only need to populate the output value and output script. // we only need to populate the output value and output script.
// The rest of the items will be populated internally within // The rest of the items will be populated internally within
// the sweeper via the witness generation function. // the sweeper via the witness generation function.
signDesc := &input.SignDescriptor{ signDesc := &input.SignDescriptor{
Output: outputInfo, Output: &wire.TxOut{
PkScript: output.PkScript,
Value: int64(output.Value),
},
HashType: txscript.SigHashAll, HashType: txscript.SigHashAll,
} }
pkScript := outputInfo.PkScript pkScript := output.PkScript
// Based on the output type, we'll map it to the proper witness // 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 // type so we can generate the set of input scripts needed to
// sweep the output. // sweep the output.
var witnessType input.WitnessType var witnessType input.WitnessType
switch { switch output.AddressType {
// If this is a p2wkh output, then we'll assume it's a witness // If this is a p2wkh output, then we'll assume it's a witness
// key hash witness type. // key hash witness type.
case txscript.IsPayToWitnessPubKeyHash(pkScript): case lnwallet.WitnessPubKey:
witnessType = input.WitnessKeyHash witnessType = input.WitnessKeyHash
// If this is a p2sh output, then as since it's under control // 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. // of the wallet, we'll assume it's a nested p2sh output.
case txscript.IsPayToScriptHash(pkScript): case lnwallet.NestedWitnessPubKey:
witnessType = input.NestedWitnessKeyHash witnessType = input.NestedWitnessKeyHash
// All other output types we count as unknown and will fail to // All other output types we count as unknown and will fail to

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