Skip to content

Commit b3adc7b

Browse files
Roasbeefguggero
authored andcommitted
input: refactor all inputs to use MakeBaseInput, add opts
In this commit, we refactor all the other constructors for the input to use MakeBaseInput. We also add a new set of functional options as well. This'll be useful later on to ensure that new options are properly applied to all the input types.
1 parent ea3e4b9 commit b3adc7b

File tree

2 files changed

+104
-81
lines changed

2 files changed

+104
-81
lines changed

input/input.go

Lines changed: 91 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,18 @@ func (i *inputKit) UnconfParent() *TxInfo {
156156
return i.unconfParent
157157
}
158158

159+
// inputOpts holds options for the input.
160+
type inputOpts struct {
161+
}
162+
163+
// defaultInputOpts returns a new inputOpts with default values.
164+
func defaultInputOpts() *inputOpts {
165+
return &inputOpts{}
166+
}
167+
168+
// InputOpt is a functional option argument to the input constructor.
169+
type InputOpt func(*inputOpts) //nolint:revive
170+
159171
// BaseInput contains all the information needed to sweep a basic
160172
// output (CSV/CLTV/no time lock).
161173
type BaseInput struct {
@@ -166,7 +178,12 @@ type BaseInput struct {
166178
// sweep transaction.
167179
func MakeBaseInput(outpoint *wire.OutPoint, witnessType WitnessType,
168180
signDescriptor *SignDescriptor, heightHint uint32,
169-
unconfParent *TxInfo) BaseInput {
181+
unconfParent *TxInfo, opts ...InputOpt) BaseInput {
182+
183+
opt := defaultInputOpts()
184+
for _, optF := range opts {
185+
optF(opt)
186+
}
170187

171188
return BaseInput{
172189
inputKit{
@@ -182,10 +199,11 @@ func MakeBaseInput(outpoint *wire.OutPoint, witnessType WitnessType,
182199
// NewBaseInput allocates and assembles a new *BaseInput that can be used to
183200
// construct a sweep transaction.
184201
func NewBaseInput(outpoint *wire.OutPoint, witnessType WitnessType,
185-
signDescriptor *SignDescriptor, heightHint uint32) *BaseInput {
202+
signDescriptor *SignDescriptor, heightHint uint32,
203+
opts ...InputOpt) *BaseInput {
186204

187205
input := MakeBaseInput(
188-
outpoint, witnessType, signDescriptor, heightHint, nil,
206+
outpoint, witnessType, signDescriptor, heightHint, nil, opts...,
189207
)
190208

191209
return &input
@@ -195,36 +213,31 @@ func NewBaseInput(outpoint *wire.OutPoint, witnessType WitnessType,
195213
// construct a sweep transaction.
196214
func NewCsvInput(outpoint *wire.OutPoint, witnessType WitnessType,
197215
signDescriptor *SignDescriptor, heightHint uint32,
198-
blockToMaturity uint32) *BaseInput {
216+
blockToMaturity uint32, opts ...InputOpt) *BaseInput {
199217

200-
return &BaseInput{
201-
inputKit{
202-
outpoint: *outpoint,
203-
witnessType: witnessType,
204-
signDesc: *signDescriptor,
205-
heightHint: heightHint,
206-
blockToMaturity: blockToMaturity,
207-
},
208-
}
218+
input := MakeBaseInput(
219+
outpoint, witnessType, signDescriptor, heightHint, nil, opts...,
220+
)
221+
222+
input.blockToMaturity = blockToMaturity
223+
224+
return &input
209225
}
210226

211227
// NewCsvInputWithCltv assembles a new csv and cltv locked input that can be
212228
// used to construct a sweep transaction.
213229
func NewCsvInputWithCltv(outpoint *wire.OutPoint, witnessType WitnessType,
214230
signDescriptor *SignDescriptor, heightHint uint32,
215-
csvDelay uint32, cltvExpiry uint32) *BaseInput {
231+
csvDelay uint32, cltvExpiry uint32, opts ...InputOpt) *BaseInput {
216232

217-
return &BaseInput{
218-
inputKit{
219-
outpoint: *outpoint,
220-
witnessType: witnessType,
221-
signDesc: *signDescriptor,
222-
heightHint: heightHint,
223-
blockToMaturity: csvDelay,
224-
cltvExpiry: cltvExpiry,
225-
unconfParent: nil,
226-
},
227-
}
233+
input := MakeBaseInput(
234+
outpoint, witnessType, signDescriptor, heightHint, nil, opts...,
235+
)
236+
237+
input.blockToMaturity = csvDelay
238+
input.cltvExpiry = cltvExpiry
239+
240+
return &input
228241
}
229242

230243
// CraftInputScript returns a valid set of input scripts allowing this output
@@ -256,16 +269,16 @@ type HtlcSucceedInput struct {
256269
// construct a sweep transaction.
257270
func MakeHtlcSucceedInput(outpoint *wire.OutPoint,
258271
signDescriptor *SignDescriptor, preimage []byte, heightHint,
259-
blocksToMaturity uint32) HtlcSucceedInput {
272+
blocksToMaturity uint32, opts ...InputOpt) HtlcSucceedInput {
273+
274+
input := MakeBaseInput(
275+
outpoint, HtlcAcceptedRemoteSuccess, signDescriptor,
276+
heightHint, nil, opts...,
277+
)
278+
input.blockToMaturity = blocksToMaturity
260279

261280
return HtlcSucceedInput{
262-
inputKit: inputKit{
263-
outpoint: *outpoint,
264-
witnessType: HtlcAcceptedRemoteSuccess,
265-
signDesc: *signDescriptor,
266-
heightHint: heightHint,
267-
blockToMaturity: blocksToMaturity,
268-
},
281+
inputKit: input.inputKit,
269282
preimage: preimage,
270283
}
271284
}
@@ -274,16 +287,17 @@ func MakeHtlcSucceedInput(outpoint *wire.OutPoint,
274287
// to spend an HTLC output for a taproot channel on the remote party's
275288
// commitment transaction.
276289
func MakeTaprootHtlcSucceedInput(op *wire.OutPoint, signDesc *SignDescriptor,
277-
preimage []byte, heightHint, blocksToMaturity uint32) HtlcSucceedInput {
290+
preimage []byte, heightHint, blocksToMaturity uint32,
291+
opts ...InputOpt) HtlcSucceedInput {
292+
293+
input := MakeBaseInput(
294+
op, TaprootHtlcAcceptedRemoteSuccess, signDesc,
295+
heightHint, nil, opts...,
296+
)
297+
input.blockToMaturity = blocksToMaturity
278298

279299
return HtlcSucceedInput{
280-
inputKit: inputKit{
281-
outpoint: *op,
282-
witnessType: TaprootHtlcAcceptedRemoteSuccess,
283-
signDesc: *signDesc,
284-
heightHint: heightHint,
285-
blockToMaturity: blocksToMaturity,
286-
},
300+
inputKit: input.inputKit,
287301
preimage: preimage,
288302
}
289303
}
@@ -388,7 +402,8 @@ func (i *HtlcSecondLevelAnchorInput) CraftInputScript(signer Signer,
388402
// to spend the HTLC output on our commit using the second level timeout
389403
// transaction.
390404
func MakeHtlcSecondLevelTimeoutAnchorInput(signedTx *wire.MsgTx,
391-
signDetails *SignDetails, heightHint uint32) HtlcSecondLevelAnchorInput {
405+
signDetails *SignDetails, heightHint uint32,
406+
opts ...InputOpt) HtlcSecondLevelAnchorInput {
392407

393408
// Spend an HTLC output on our local commitment tx using the
394409
// 2nd timeout transaction.
@@ -408,16 +423,15 @@ func MakeHtlcSecondLevelTimeoutAnchorInput(signedTx *wire.MsgTx,
408423
)
409424
}
410425

426+
input := MakeBaseInput(
427+
&signedTx.TxIn[0].PreviousOutPoint,
428+
HtlcOfferedTimeoutSecondLevelInputConfirmed,
429+
&signDetails.SignDesc, heightHint, nil, opts...,
430+
)
431+
input.blockToMaturity = 1
432+
411433
return HtlcSecondLevelAnchorInput{
412-
inputKit: inputKit{
413-
outpoint: signedTx.TxIn[0].PreviousOutPoint,
414-
witnessType: HtlcOfferedTimeoutSecondLevelInputConfirmed,
415-
signDesc: signDetails.SignDesc,
416-
heightHint: heightHint,
417-
418-
// CSV delay is always 1 for these inputs.
419-
blockToMaturity: 1,
420-
},
434+
inputKit: input.inputKit,
421435
SignedTx: signedTx,
422436
createWitness: createWitness,
423437
}
@@ -429,7 +443,7 @@ func MakeHtlcSecondLevelTimeoutAnchorInput(signedTx *wire.MsgTx,
429443
// sweep the second level HTLC aggregated with other transactions.
430444
func MakeHtlcSecondLevelTimeoutTaprootInput(signedTx *wire.MsgTx,
431445
signDetails *SignDetails,
432-
heightHint uint32) HtlcSecondLevelAnchorInput {
446+
heightHint uint32, opts ...InputOpt) HtlcSecondLevelAnchorInput {
433447

434448
createWitness := func(signer Signer, txn *wire.MsgTx,
435449
hashCache *txscript.TxSigHashes,
@@ -453,16 +467,15 @@ func MakeHtlcSecondLevelTimeoutTaprootInput(signedTx *wire.MsgTx,
453467
)
454468
}
455469

470+
input := MakeBaseInput(
471+
&signedTx.TxIn[0].PreviousOutPoint,
472+
TaprootHtlcLocalOfferedTimeout,
473+
&signDetails.SignDesc, heightHint, nil, opts...,
474+
)
475+
input.blockToMaturity = 1
476+
456477
return HtlcSecondLevelAnchorInput{
457-
inputKit: inputKit{
458-
outpoint: signedTx.TxIn[0].PreviousOutPoint,
459-
witnessType: TaprootHtlcLocalOfferedTimeout,
460-
signDesc: signDetails.SignDesc,
461-
heightHint: heightHint,
462-
463-
// CSV delay is always 1 for these inputs.
464-
blockToMaturity: 1,
465-
},
478+
inputKit: input.inputKit,
466479
SignedTx: signedTx,
467480
createWitness: createWitness,
468481
}
@@ -473,7 +486,7 @@ func MakeHtlcSecondLevelTimeoutTaprootInput(signedTx *wire.MsgTx,
473486
// transaction.
474487
func MakeHtlcSecondLevelSuccessAnchorInput(signedTx *wire.MsgTx,
475488
signDetails *SignDetails, preimage lntypes.Preimage,
476-
heightHint uint32) HtlcSecondLevelAnchorInput {
489+
heightHint uint32, opts ...InputOpt) HtlcSecondLevelAnchorInput {
477490

478491
// Spend an HTLC output on our local commitment tx using the 2nd
479492
// success transaction.
@@ -492,18 +505,16 @@ func MakeHtlcSecondLevelSuccessAnchorInput(signedTx *wire.MsgTx,
492505
preimage[:], signer, &desc, txn,
493506
)
494507
}
508+
input := MakeBaseInput(
509+
&signedTx.TxIn[0].PreviousOutPoint,
510+
HtlcAcceptedSuccessSecondLevelInputConfirmed,
511+
&signDetails.SignDesc, heightHint, nil, opts...,
512+
)
513+
input.blockToMaturity = 1
495514

496515
return HtlcSecondLevelAnchorInput{
497-
inputKit: inputKit{
498-
outpoint: signedTx.TxIn[0].PreviousOutPoint,
499-
witnessType: HtlcAcceptedSuccessSecondLevelInputConfirmed,
500-
signDesc: signDetails.SignDesc,
501-
heightHint: heightHint,
502-
503-
// CSV delay is always 1 for these inputs.
504-
blockToMaturity: 1,
505-
},
506516
SignedTx: signedTx,
517+
inputKit: input.inputKit,
507518
createWitness: createWitness,
508519
}
509520
}
@@ -513,7 +524,7 @@ func MakeHtlcSecondLevelSuccessAnchorInput(signedTx *wire.MsgTx,
513524
// commitment transaction.
514525
func MakeHtlcSecondLevelSuccessTaprootInput(signedTx *wire.MsgTx,
515526
signDetails *SignDetails, preimage lntypes.Preimage,
516-
heightHint uint32) HtlcSecondLevelAnchorInput {
527+
heightHint uint32, opts ...InputOpt) HtlcSecondLevelAnchorInput {
517528

518529
createWitness := func(signer Signer, txn *wire.MsgTx,
519530
hashCache *txscript.TxSigHashes,
@@ -537,16 +548,15 @@ func MakeHtlcSecondLevelSuccessTaprootInput(signedTx *wire.MsgTx,
537548
)
538549
}
539550

551+
input := MakeBaseInput(
552+
&signedTx.TxIn[0].PreviousOutPoint,
553+
TaprootHtlcAcceptedLocalSuccess,
554+
&signDetails.SignDesc, heightHint, nil, opts...,
555+
)
556+
input.blockToMaturity = 1
557+
540558
return HtlcSecondLevelAnchorInput{
541-
inputKit: inputKit{
542-
outpoint: signedTx.TxIn[0].PreviousOutPoint,
543-
witnessType: TaprootHtlcAcceptedLocalSuccess,
544-
signDesc: signDetails.SignDesc,
545-
heightHint: heightHint,
546-
547-
// CSV delay is always 1 for these inputs.
548-
blockToMaturity: 1,
549-
},
559+
inputKit: input.inputKit,
550560
SignedTx: signedTx,
551561
createWitness: createWitness,
552562
}

input/mocks.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"github.com/btcsuite/btcd/btcec/v2/schnorr/musig2"
99
"github.com/btcsuite/btcd/txscript"
1010
"github.com/btcsuite/btcd/wire"
11+
"github.com/lightningnetwork/lnd/fn"
1112
"github.com/lightningnetwork/lnd/keychain"
1213
"github.com/lightningnetwork/lnd/lntypes"
14+
"github.com/lightningnetwork/lnd/tlv"
1315
"github.com/stretchr/testify/mock"
1416
)
1517

@@ -127,6 +129,17 @@ func (m *MockInput) UnconfParent() *TxInfo {
127129
return info.(*TxInfo)
128130
}
129131

132+
func (m *MockInput) ResolutionBlob() fn.Option[tlv.Blob] {
133+
args := m.Called()
134+
135+
info := args.Get(0)
136+
if info == nil {
137+
return fn.None[tlv.Blob]()
138+
}
139+
140+
return info.(fn.Option[tlv.Blob])
141+
}
142+
130143
// MockWitnessType implements the `WitnessType` interface and is used by other
131144
// packages for mock testing.
132145
type MockWitnessType struct {

0 commit comments

Comments
 (0)