Skip to content

Commit 8b9dcbb

Browse files
committed
tapchannel: refactor result type
This commit unifies the use of the lfn.Result return type as a preparation for the next commit, where we change a bunch of function/method signatures to use the Result type as well.
1 parent 39f1003 commit 8b9dcbb

File tree

2 files changed

+64
-47
lines changed

2 files changed

+64
-47
lines changed

tapchannel/aux_closer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func createCloseAlloc(isLocal, isInitiator bool, closeAsset *asset.Asset,
151151
}, nil
152152
}
153153

154-
// fundingSpendwitness creates a complete witness to spend the OP_TRUE funding
154+
// fundingSpendWitness creates a complete witness to spend the OP_TRUE funding
155155
// script of an asset funding output.
156156
func fundingSpendWitness() lfn.Result[wire.TxWitness] {
157157
fundingScriptTree := NewFundingScriptTree()
@@ -162,8 +162,8 @@ func fundingSpendWitness() lfn.Result[wire.TxWitness] {
162162
)
163163
ctrlBlockBytes, err := ctrlBlock.ToBytes()
164164
if err != nil {
165-
return lfn.Errf[wire.TxWitness]("unable to serialize control "+
166-
"block: %w", err)
165+
return lfn.Err[wire.TxWitness](fmt.Errorf("unable to "+
166+
"serialize control block: %w", err))
167167
}
168168

169169
return lfn.Ok(wire.TxWitness{

tapchannel/aux_sweeper.go

Lines changed: 61 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,15 @@ func (a *AuxSweeper) createSweepVpackets(sweepInputs []*cmsg.AssetOutput,
177177
tapscriptDesc lfn.Result[tapscriptSweepDesc],
178178
) lfn.Result[[]*tappsbt.VPacket] {
179179

180+
type returnType = []*tappsbt.VPacket
181+
180182
log.Infof("Creating sweep packets for %v inputs", len(sweepInputs))
181183

182184
// Unpack the tapscript desc, as we need it to be able to continue
183185
// forward.
184186
sweepDesc, err := tapscriptDesc.Unpack()
185187
if err != nil {
186-
return lfn.Err[[]*tappsbt.VPacket](err)
188+
return lfn.Err[returnType](err)
187189
}
188190

189191
// For each out we want to sweep, we'll construct an allocation that
@@ -197,7 +199,7 @@ func (a *AuxSweeper) createSweepVpackets(sweepInputs []*cmsg.AssetOutput,
197199
ctx, asset.TaprootAssetsKeyFamily,
198200
)
199201
if err != nil {
200-
return lfn.Err[[]*tappsbt.VPacket](err)
202+
return lfn.Err[returnType](err)
201203
}
202204

203205
// With the script key created, we can make a new allocation
@@ -238,17 +240,17 @@ func (a *AuxSweeper) createSweepVpackets(sweepInputs []*cmsg.AssetOutput,
238240
inputProofs, allocs, &a.cfg.ChainParams,
239241
)
240242
if err != nil {
241-
return lfn.Errf[[]*tappsbt.VPacket]("error distributing "+
242-
"coins: %w", err)
243+
return lfn.Err[returnType](fmt.Errorf("error distributing "+
244+
"coins: %w", err))
243245
}
244246

245247
log.Infof("Created %v sweep packets: %v", len(vPackets),
246248
limitSpewer.Sdump(vPackets))
247249

248250
fundingWitness, err := fundingSpendWitness().Unpack()
249251
if err != nil {
250-
return lfn.Errf[[]*tappsbt.VPacket]("unable to make "+
251-
"funding witness: %v", err)
252+
return lfn.Err[returnType](fmt.Errorf("unable to make "+
253+
"funding witness: %w", err))
252254
}
253255

254256
// Next, we'll prepare all the vPackets for the sweep transaction, and
@@ -265,8 +267,8 @@ func (a *AuxSweeper) createSweepVpackets(sweepInputs []*cmsg.AssetOutput,
265267

266268
err := tapsend.PrepareOutputAssets(ctx, vPackets[idx])
267269
if err != nil {
268-
return lfn.Errf[[]*tappsbt.VPacket]("unable to "+
269-
"prepare output assets: %w", err)
270+
return lfn.Err[returnType](fmt.Errorf("unable to "+
271+
"prepare output assets: %w", err))
270272
}
271273

272274
// Next before we sign, we'll make sure to update the witness
@@ -354,6 +356,8 @@ func (a *AuxSweeper) createAndSignSweepVpackets(
354356
sweepDesc lfn.Result[tapscriptSweepDesc],
355357
) lfn.Result[[]*tappsbt.VPacket] {
356358

359+
type returnType = []*tappsbt.VPacket
360+
357361
// Based on the sweep inputs, make vPackets that sweep all the inputs
358362
// into a new output with a fresh script key. They won't have an
359363
// internal key set, we'll do that when we go to make the output to
@@ -364,7 +368,7 @@ func (a *AuxSweeper) createAndSignSweepVpackets(
364368

365369
err := a.signSweepVpackets(vPkts, signDesc, desc)
366370
if err != nil {
367-
return lfn.Err[[]*tappsbt.VPacket](err)
371+
return lfn.Err[returnType](err)
368372
}
369373

370374
return lfn.Ok(vPkts)
@@ -397,15 +401,17 @@ type tapscriptSweepDesc struct {
397401
func commitNoDelaySweepDesc(keyRing *lnwallet.CommitmentKeyRing,
398402
csvDelay uint32) lfn.Result[tapscriptSweepDesc] {
399403

404+
type returnType = tapscriptSweepDesc
405+
400406
// We'll make the script tree for the to remote script (we're remote as
401407
// this is their commitment transaction). We don't have an auxLeaf here
402408
// as we're on the TAP layer.
403409
toRemoteScriptTree, err := input.NewRemoteCommitScriptTree(
404410
keyRing.ToRemoteKey, input.NoneTapLeaf(),
405411
)
406412
if err != nil {
407-
return lfn.Errf[tapscriptSweepDesc]("unable to make remote "+
408-
"script tree: %w", err)
413+
return lfn.Err[returnType](fmt.Errorf("unable to make remote "+
414+
"script tree: %w", err))
409415
}
410416

411417
// Now that we have the script tree, we'll make the control block
@@ -414,13 +420,13 @@ func commitNoDelaySweepDesc(keyRing *lnwallet.CommitmentKeyRing,
414420
input.ScriptPathSuccess,
415421
)
416422
if err != nil {
417-
return lfn.Errf[tapscriptSweepDesc]("unable to make "+
418-
"ctrl block: %w", err)
423+
return lfn.Err[returnType](fmt.Errorf("unable to make ctrl "+
424+
"block: %w", err))
419425
}
420426
ctrlBlockBytes, err := ctrlBlock.ToBytes()
421427
if err != nil {
422-
return lfn.Errf[tapscriptSweepDesc]("unable to encode ctrl "+
423-
"block: %w", err)
428+
return lfn.Err[returnType](fmt.Errorf("unable to encode ctrl "+
429+
"block: %w", err))
424430
}
425431

426432
return lfn.Ok(tapscriptSweepDesc{
@@ -436,6 +442,8 @@ func commitNoDelaySweepDesc(keyRing *lnwallet.CommitmentKeyRing,
436442
func commitDelaySweepDesc(keyRing *lnwallet.CommitmentKeyRing,
437443
csvDelay uint32) lfn.Result[tapscriptSweepDesc] {
438444

445+
type returnType = tapscriptSweepDesc
446+
439447
// We'll make the script tree for the to remote script (we're remote as
440448
// this is their commitment transaction). We don't have an auxLeaf here
441449
// as we're on the TAP layer.
@@ -444,7 +452,7 @@ func commitDelaySweepDesc(keyRing *lnwallet.CommitmentKeyRing,
444452
input.NoneTapLeaf(),
445453
)
446454
if err != nil {
447-
return lfn.Err[tapscriptSweepDesc](err)
455+
return lfn.Err[returnType](err)
448456
}
449457

450458
// Now that we have the script tree, we'll make the control block
@@ -453,11 +461,11 @@ func commitDelaySweepDesc(keyRing *lnwallet.CommitmentKeyRing,
453461
input.ScriptPathSuccess,
454462
)
455463
if err != nil {
456-
return lfn.Err[tapscriptSweepDesc](err)
464+
return lfn.Err[returnType](err)
457465
}
458466
ctrlBlockBytes, err := ctrlBlock.ToBytes()
459467
if err != nil {
460-
return lfn.Err[tapscriptSweepDesc](err)
468+
return lfn.Err[returnType](err)
461469
}
462470

463471
return lfn.Ok(tapscriptSweepDesc{
@@ -473,6 +481,8 @@ func commitDelaySweepDesc(keyRing *lnwallet.CommitmentKeyRing,
473481
func commitRevokeSweepDesc(keyRing *lnwallet.CommitmentKeyRing,
474482
csvDelay uint32) lfn.Result[tapscriptSweepDesc] {
475483

484+
type returnType = tapscriptSweepDesc
485+
476486
// To sweep their revoked output, we'll make the script tree for the
477487
// local tree of their commitment transaction, which is actually their
478488
// output.
@@ -481,7 +491,7 @@ func commitRevokeSweepDesc(keyRing *lnwallet.CommitmentKeyRing,
481491
input.NoneTapLeaf(),
482492
)
483493
if err != nil {
484-
return lfn.Err[tapscriptSweepDesc](err)
494+
return lfn.Err[returnType](err)
485495
}
486496

487497
// Now that we have the script tree, we'll make the control block
@@ -490,11 +500,11 @@ func commitRevokeSweepDesc(keyRing *lnwallet.CommitmentKeyRing,
490500
input.ScriptPathRevocation,
491501
)
492502
if err != nil {
493-
return lfn.Err[tapscriptSweepDesc](err)
503+
return lfn.Err[returnType](err)
494504
}
495505
ctrlBlockBytes, err := ctrlBlock.ToBytes()
496506
if err != nil {
497-
return lfn.Err[tapscriptSweepDesc](err)
507+
return lfn.Err[returnType](err)
498508
}
499509

500510
return lfn.Ok(tapscriptSweepDesc{
@@ -1097,6 +1107,8 @@ func (a *AuxSweeper) importCommitTx(req lnwallet.ResolutionReq,
10971107
func (a *AuxSweeper) resolveContract(
10981108
req lnwallet.ResolutionReq) lfn.Result[tlv.Blob] {
10991109

1110+
type returnType = tlv.Blob
1111+
11001112
// If there's no commit blob, then there's nothing to resolve.
11011113
if req.CommitBlob.IsNone() {
11021114
return lfn.Err[tlv.Blob](nil)
@@ -1112,13 +1124,13 @@ func (a *AuxSweeper) resolveContract(
11121124
req.CommitBlob.UnwrapOr(nil),
11131125
)
11141126
if err != nil {
1115-
return lfn.Err[tlv.Blob](err)
1127+
return lfn.Err[returnType](err)
11161128
}
11171129
fundingInfo, err := tapchannelmsg.DecodeOpenChannel(
11181130
req.FundingBlob.UnwrapOr(nil),
11191131
)
11201132
if err != nil {
1121-
return lfn.Err[tlv.Blob](err)
1133+
return lfn.Err[returnType](err)
11221134
}
11231135

11241136
// To be able to construct all the proofs we need to spend later, we'll
@@ -1130,16 +1142,16 @@ func (a *AuxSweeper) resolveContract(
11301142
ctx, fn.Some(req.CommitTx.TxHash()), false,
11311143
)
11321144
if err != nil {
1133-
return lfn.Err[tlv.Blob](err)
1145+
return lfn.Err[returnType](err)
11341146
}
11351147
if len(commitParcel) == 0 {
11361148
log.Infof("First time seeing commit_txid=%v, importing",
11371149
req.CommitTx.TxHash())
11381150

11391151
err := a.importCommitTx(req, commitState, fundingInfo)
11401152
if err != nil {
1141-
return lfn.Errf[tlv.Blob]("unable to import "+
1142-
"commitment txn: %v", err)
1153+
return lfn.Err[returnType](fmt.Errorf("unable to "+
1154+
"import commitment txn: %w", err))
11431155
}
11441156
} else {
11451157
log.Infof("Commitment commit_txid=%v already imported, "+
@@ -1190,7 +1202,7 @@ func (a *AuxSweeper) resolveContract(
11901202
sweepDesc = commitRevokeSweepDesc(req.KeyRing, req.CsvDelay)
11911203

11921204
default:
1193-
return lfn.Err[tlv.Blob](fmt.Errorf("unknown resolution "+
1205+
return lfn.Err[returnType](fmt.Errorf("unknown resolution "+
11941206
"type: %v", req.Type))
11951207
}
11961208

@@ -1218,7 +1230,7 @@ func (a *AuxSweeper) resolveContract(
12181230

12191231
var b bytes.Buffer
12201232
if err := res.Encode(&b); err != nil {
1221-
return lfn.Err[tlv.Blob](err)
1233+
return lfn.Err[returnType](err)
12221234
}
12231235

12241236
return lfn.Ok(b.Bytes())
@@ -1230,6 +1242,8 @@ func (a *AuxSweeper) resolveContract(
12301242
// none of the inputs have any resolution blobs. Then an empty slice will be
12311243
// returned.
12321244
func extractInputVPackets(inputs []input.Input) lfn.Result[[]*tappsbt.VPacket] {
1245+
type returnType = []*tappsbt.VPacket
1246+
12331247
// Otherwise, we'll extract the set of resolution blobs from the inputs
12341248
// passed in.
12351249
relevantInputs := fn.Filter(inputs, func(i input.Input) bool {
@@ -1254,7 +1268,7 @@ func extractInputVPackets(inputs []input.Input) lfn.Result[[]*tappsbt.VPacket] {
12541268
},
12551269
)
12561270
if err != nil {
1257-
return lfn.Err[[]*tappsbt.VPacket](err)
1271+
return lfn.Err[returnType](err)
12581272
}
12591273

12601274
return lfn.Ok(vPkts)
@@ -1266,13 +1280,15 @@ func extractInputVPackets(inputs []input.Input) lfn.Result[[]*tappsbt.VPacket] {
12661280
func (a *AuxSweeper) sweepContracts(inputs []input.Input,
12671281
change lnwallet.AddrWithKey) lfn.Result[sweep.SweepOutput] {
12681282

1283+
type returnType = sweep.SweepOutput
1284+
12691285
// If none of the inputs have a resolution blob, then we have nothing
12701286
// to generate.
12711287
if fn.NotAny(inputs, func(i input.Input) bool {
12721288
return !i.ResolutionBlob().IsNone()
12731289
}) {
12741290

1275-
return lfn.Err[sweep.SweepOutput](nil)
1291+
return lfn.Err[returnType](nil)
12761292
}
12771293

12781294
// TODO(roasbeef): can pipline entire thing instead?
@@ -1281,7 +1297,7 @@ func (a *AuxSweeper) sweepContracts(inputs []input.Input,
12811297
// vPackets from the inputs.
12821298
vPkts, err := extractInputVPackets(inputs).Unpack()
12831299
if err != nil {
1284-
return lfn.Err[sweep.SweepOutput](err)
1300+
return lfn.Err[returnType](err)
12851301
}
12861302

12871303
log.Infof("Generating anchor output for vpkts=%v",
@@ -1296,7 +1312,7 @@ func (a *AuxSweeper) sweepContracts(inputs []input.Input,
12961312
context.Background(), asset.TaprootAssetsKeyFamily,
12971313
)
12981314
if err != nil {
1299-
return lfn.Err[sweep.SweepOutput](err)
1315+
return lfn.Err[returnType](err)
13001316
}
13011317
for idx := range vPkts {
13021318
for _, vOut := range vPkts[idx].Outputs {
@@ -1310,15 +1326,14 @@ func (a *AuxSweeper) sweepContracts(inputs []input.Input,
13101326
// out of all the vPackets contained.
13111327
outCommitments, err := tapsend.CreateOutputCommitments(vPkts)
13121328
if err != nil {
1313-
return lfn.Errf[sweep.SweepOutput]("unable to create output "+
1314-
"commitments: %w", err)
1329+
return lfn.Err[returnType](fmt.Errorf("unable to create "+
1330+
"output commitments: %w", err))
13151331
}
13161332

13171333
// We should only have a single output commitment at this point.
13181334
if len(outCommitments) != 1 {
1319-
return lfn.Err[sweep.SweepOutput](fmt.Errorf("expected a "+
1320-
"single output commitment, got: %v",
1321-
len(outCommitments)))
1335+
return lfn.Err[returnType](fmt.Errorf("expected a single "+
1336+
"output commitment, got: %v", len(outCommitments)))
13221337
}
13231338

13241339
// With the output commitments created, we'll now create the anchor
@@ -1327,7 +1342,7 @@ func (a *AuxSweeper) sweepContracts(inputs []input.Input,
13271342
internalKey.PubKey, nil, outCommitments[0],
13281343
)
13291344
if err != nil {
1330-
return lfn.Err[sweep.SweepOutput](err)
1345+
return lfn.Err[returnType](err)
13311346
}
13321347

13331348
return lfn.Ok(sweep.SweepOutput{
@@ -1506,18 +1521,20 @@ func (a *AuxSweeper) contractResolver() {
15061521
func (a *AuxSweeper) ResolveContract(
15071522
req lnwallet.ResolutionReq) lfn.Result[tlv.Blob] {
15081523

1524+
type returnType = tlv.Blob
1525+
15091526
auxReq := &resolutionReq{
15101527
req: req,
15111528
resp: make(chan lfn.Result[tlv.Blob], 1),
15121529
}
15131530

15141531
if !fn.SendOrQuit(a.resolutionReqs, auxReq, a.quit) {
1515-
return lfn.Err[tlv.Blob](fmt.Errorf("aux sweeper stopped"))
1532+
return lfn.Err[returnType](fmt.Errorf("aux sweeper stopped"))
15161533
}
15171534

15181535
resp, quitErr := fn.RecvResp(auxReq.resp, nil, a.quit)
15191536
if quitErr != nil {
1520-
return lfn.Err[tlv.Blob](quitErr)
1537+
return lfn.Err[returnType](quitErr)
15211538
}
15221539

15231540
return resp
@@ -1529,21 +1546,21 @@ func (a *AuxSweeper) ResolveContract(
15291546
func (a *AuxSweeper) DeriveSweepAddr(inputs []input.Input,
15301547
change lnwallet.AddrWithKey) lfn.Result[sweep.SweepOutput] {
15311548

1549+
type returnType = sweep.SweepOutput
1550+
15321551
auxReq := &sweepAddrReq{
15331552
inputs: inputs,
15341553
change: change,
15351554
resp: make(chan lfn.Result[sweep.SweepOutput], 1),
15361555
}
15371556

15381557
if !fn.SendOrQuit(a.sweepAddrReqs, auxReq, a.quit) {
1539-
return lfn.Err[sweep.SweepOutput](
1540-
fmt.Errorf("aux sweeper stopped"),
1541-
)
1558+
return lfn.Err[returnType](fmt.Errorf("aux sweeper stopped"))
15421559
}
15431560

15441561
resp, quitErr := fn.RecvResp(auxReq.resp, nil, a.quit)
15451562
if quitErr != nil {
1546-
return lfn.Err[sweep.SweepOutput](quitErr)
1563+
return lfn.Err[returnType](quitErr)
15471564
}
15481565

15491566
return resp

0 commit comments

Comments
 (0)