Skip to content

Commit 274ee47

Browse files
committed
tapchannel: refactor result type
This commit unifies the use of the lfn.Result return type as a preparation for the final commit in this PR, where we change a bunch of function/method signatures to use the Result type as well. We use lnf.Err(fnt.Errorf()) instead of lnf.Errf because the intellisense of some IDEs complain about the '%w' verb only being usable with fmt.Errorf().
1 parent 7ee0c0f commit 274ee47

File tree

2 files changed

+64
-48
lines changed

2 files changed

+64
-48
lines changed

tapchannel/aux_closer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func createCloseAlloc(isLocal, isInitiator bool, closeAsset *asset.Asset,
152152
}, nil
153153
}
154154

155-
// fundingSpendwitness creates a complete witness to spend the OP_TRUE funding
155+
// fundingSpendWitness creates a complete witness to spend the OP_TRUE funding
156156
// script of an asset funding output.
157157
func fundingSpendWitness() lfn.Result[wire.TxWitness] {
158158
fundingScriptTree := tapscript.NewChannelFundingScriptTree()

tapchannel/aux_sweeper.go

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

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

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

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

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

246247
log.Infof("Created %v sweep packets: %v", len(vPackets),
247248
limitSpewer.Sdump(vPackets))
248249

249250
fundingWitness, err := fundingSpendWitness().Unpack()
250251
if err != nil {
251-
return lfn.Errf[[]*tappsbt.VPacket]("unable to make "+
252-
"funding witness: %v", err)
252+
return lfn.Errf[returnType]("unable to make funding witness: "+
253+
"%w", err)
253254
}
254255

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

267268
err := tapsend.PrepareOutputAssets(ctx, vPackets[idx])
268269
if err != nil {
269-
return lfn.Errf[[]*tappsbt.VPacket]("unable to "+
270-
"prepare output assets: %w", err)
270+
return lfn.Errf[returnType]("unable to prepare output "+
271+
"assets: %w", err)
271272
}
272273

273274
// Next before we sign, we'll make sure to update the witness
@@ -355,6 +356,8 @@ func (a *AuxSweeper) createAndSignSweepVpackets(
355356
sweepDesc lfn.Result[tapscriptSweepDesc],
356357
) lfn.Result[[]*tappsbt.VPacket] {
357358

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

366369
err := a.signSweepVpackets(vPkts, signDesc, desc)
367370
if err != nil {
368-
return lfn.Err[[]*tappsbt.VPacket](err)
371+
return lfn.Err[returnType](err)
369372
}
370373

371374
return lfn.Ok(vPkts)
@@ -398,15 +401,17 @@ type tapscriptSweepDesc struct {
398401
func commitNoDelaySweepDesc(keyRing *lnwallet.CommitmentKeyRing,
399402
csvDelay uint32) lfn.Result[tapscriptSweepDesc] {
400403

404+
type returnType = tapscriptSweepDesc
405+
401406
// We'll make the script tree for the to remote script (we're remote as
402407
// this is their commitment transaction). We don't have an auxLeaf here
403408
// as we're on the TAP layer.
404409
toRemoteScriptTree, err := input.NewRemoteCommitScriptTree(
405410
keyRing.ToRemoteKey, input.NoneTapLeaf(),
406411
)
407412
if err != nil {
408-
return lfn.Errf[tapscriptSweepDesc]("unable to make remote "+
409-
"script tree: %w", err)
413+
return lfn.Errf[returnType]("unable to make remote script "+
414+
"tree: %w", err)
410415
}
411416

412417
// Now that we have the script tree, we'll make the control block
@@ -415,13 +420,13 @@ func commitNoDelaySweepDesc(keyRing *lnwallet.CommitmentKeyRing,
415420
input.ScriptPathSuccess,
416421
)
417422
if err != nil {
418-
return lfn.Errf[tapscriptSweepDesc]("unable to make "+
419-
"ctrl block: %w", err)
423+
return lfn.Errf[returnType]("unable to make ctrl block: %w",
424+
err)
420425
}
421426
ctrlBlockBytes, err := ctrlBlock.ToBytes()
422427
if err != nil {
423-
return lfn.Errf[tapscriptSweepDesc]("unable to encode ctrl "+
424-
"block: %w", err)
428+
return lfn.Errf[returnType]("unable to encode ctrl block: %w",
429+
err)
425430
}
426431

427432
return lfn.Ok(tapscriptSweepDesc{
@@ -437,6 +442,8 @@ func commitNoDelaySweepDesc(keyRing *lnwallet.CommitmentKeyRing,
437442
func commitDelaySweepDesc(keyRing *lnwallet.CommitmentKeyRing,
438443
csvDelay uint32) lfn.Result[tapscriptSweepDesc] {
439444

445+
type returnType = tapscriptSweepDesc
446+
440447
// We'll make the script tree for the to remote script (we're remote as
441448
// this is their commitment transaction). We don't have an auxLeaf here
442449
// as we're on the TAP layer.
@@ -445,7 +452,7 @@ func commitDelaySweepDesc(keyRing *lnwallet.CommitmentKeyRing,
445452
input.NoneTapLeaf(),
446453
)
447454
if err != nil {
448-
return lfn.Err[tapscriptSweepDesc](err)
455+
return lfn.Err[returnType](err)
449456
}
450457

451458
// Now that we have the script tree, we'll make the control block
@@ -454,11 +461,11 @@ func commitDelaySweepDesc(keyRing *lnwallet.CommitmentKeyRing,
454461
input.ScriptPathSuccess,
455462
)
456463
if err != nil {
457-
return lfn.Err[tapscriptSweepDesc](err)
464+
return lfn.Err[returnType](err)
458465
}
459466
ctrlBlockBytes, err := ctrlBlock.ToBytes()
460467
if err != nil {
461-
return lfn.Err[tapscriptSweepDesc](err)
468+
return lfn.Err[returnType](err)
462469
}
463470

464471
return lfn.Ok(tapscriptSweepDesc{
@@ -474,6 +481,8 @@ func commitDelaySweepDesc(keyRing *lnwallet.CommitmentKeyRing,
474481
func commitRevokeSweepDesc(keyRing *lnwallet.CommitmentKeyRing,
475482
csvDelay uint32) lfn.Result[tapscriptSweepDesc] {
476483

484+
type returnType = tapscriptSweepDesc
485+
477486
// To sweep their revoked output, we'll make the script tree for the
478487
// local tree of their commitment transaction, which is actually their
479488
// output.
@@ -482,7 +491,7 @@ func commitRevokeSweepDesc(keyRing *lnwallet.CommitmentKeyRing,
482491
input.NoneTapLeaf(),
483492
)
484493
if err != nil {
485-
return lfn.Err[tapscriptSweepDesc](err)
494+
return lfn.Err[returnType](err)
486495
}
487496

488497
// Now that we have the script tree, we'll make the control block
@@ -491,11 +500,11 @@ func commitRevokeSweepDesc(keyRing *lnwallet.CommitmentKeyRing,
491500
input.ScriptPathRevocation,
492501
)
493502
if err != nil {
494-
return lfn.Err[tapscriptSweepDesc](err)
503+
return lfn.Err[returnType](err)
495504
}
496505
ctrlBlockBytes, err := ctrlBlock.ToBytes()
497506
if err != nil {
498-
return lfn.Err[tapscriptSweepDesc](err)
507+
return lfn.Err[returnType](err)
499508
}
500509

501510
return lfn.Ok(tapscriptSweepDesc{
@@ -1098,6 +1107,8 @@ func (a *AuxSweeper) importCommitTx(req lnwallet.ResolutionReq,
10981107
func (a *AuxSweeper) resolveContract(
10991108
req lnwallet.ResolutionReq) lfn.Result[tlv.Blob] {
11001109

1110+
type returnType = tlv.Blob
1111+
11011112
// If there's no commit blob, then there's nothing to resolve.
11021113
if req.CommitBlob.IsNone() {
11031114
return lfn.Err[tlv.Blob](nil)
@@ -1113,13 +1124,13 @@ func (a *AuxSweeper) resolveContract(
11131124
req.CommitBlob.UnwrapOr(nil),
11141125
)
11151126
if err != nil {
1116-
return lfn.Err[tlv.Blob](err)
1127+
return lfn.Err[returnType](err)
11171128
}
11181129
fundingInfo, err := tapchannelmsg.DecodeOpenChannel(
11191130
req.FundingBlob.UnwrapOr(nil),
11201131
)
11211132
if err != nil {
1122-
return lfn.Err[tlv.Blob](err)
1133+
return lfn.Err[returnType](err)
11231134
}
11241135

11251136
// To be able to construct all the proofs we need to spend later, we'll
@@ -1131,16 +1142,16 @@ func (a *AuxSweeper) resolveContract(
11311142
ctx, fn.Some(req.CommitTx.TxHash()), false,
11321143
)
11331144
if err != nil {
1134-
return lfn.Err[tlv.Blob](err)
1145+
return lfn.Err[returnType](err)
11351146
}
11361147
if len(commitParcel) == 0 {
11371148
log.Infof("First time seeing commit_txid=%v, importing",
11381149
req.CommitTx.TxHash())
11391150

11401151
err := a.importCommitTx(req, commitState, fundingInfo)
11411152
if err != nil {
1142-
return lfn.Errf[tlv.Blob]("unable to import "+
1143-
"commitment txn: %v", err)
1153+
return lfn.Errf[returnType]("unable to import "+
1154+
"commitment txn: %w", err)
11441155
}
11451156
} else {
11461157
log.Infof("Commitment commit_txid=%v already imported, "+
@@ -1191,8 +1202,8 @@ func (a *AuxSweeper) resolveContract(
11911202
sweepDesc = commitRevokeSweepDesc(req.KeyRing, req.CsvDelay)
11921203

11931204
default:
1194-
return lfn.Err[tlv.Blob](fmt.Errorf("unknown resolution "+
1195-
"type: %v", req.Type))
1205+
return lfn.Errf[returnType]("unknown resolution type: %v",
1206+
req.Type)
11961207
}
11971208

11981209
// The input proofs above were made originally using the fake commit tx
@@ -1219,7 +1230,7 @@ func (a *AuxSweeper) resolveContract(
12191230

12201231
var b bytes.Buffer
12211232
if err := res.Encode(&b); err != nil {
1222-
return lfn.Err[tlv.Blob](err)
1233+
return lfn.Err[returnType](err)
12231234
}
12241235

12251236
return lfn.Ok(b.Bytes())
@@ -1231,6 +1242,8 @@ func (a *AuxSweeper) resolveContract(
12311242
// none of the inputs have any resolution blobs. Then an empty slice will be
12321243
// returned.
12331244
func extractInputVPackets(inputs []input.Input) lfn.Result[[]*tappsbt.VPacket] {
1245+
type returnType = []*tappsbt.VPacket
1246+
12341247
// Otherwise, we'll extract the set of resolution blobs from the inputs
12351248
// passed in.
12361249
relevantInputs := fn.Filter(inputs, func(i input.Input) bool {
@@ -1255,7 +1268,7 @@ func extractInputVPackets(inputs []input.Input) lfn.Result[[]*tappsbt.VPacket] {
12551268
},
12561269
)
12571270
if err != nil {
1258-
return lfn.Err[[]*tappsbt.VPacket](err)
1271+
return lfn.Err[returnType](err)
12591272
}
12601273

12611274
return lfn.Ok(vPkts)
@@ -1267,13 +1280,15 @@ func extractInputVPackets(inputs []input.Input) lfn.Result[[]*tappsbt.VPacket] {
12671280
func (a *AuxSweeper) sweepContracts(inputs []input.Input,
12681281
change lnwallet.AddrWithKey) lfn.Result[sweep.SweepOutput] {
12691282

1283+
type returnType = sweep.SweepOutput
1284+
12701285
// If none of the inputs have a resolution blob, then we have nothing
12711286
// to generate.
12721287
if fn.NotAny(inputs, func(i input.Input) bool {
12731288
return !i.ResolutionBlob().IsNone()
12741289
}) {
12751290

1276-
return lfn.Err[sweep.SweepOutput](nil)
1291+
return lfn.Err[returnType](nil)
12771292
}
12781293

12791294
// TODO(roasbeef): can pipline entire thing instead?
@@ -1282,7 +1297,7 @@ func (a *AuxSweeper) sweepContracts(inputs []input.Input,
12821297
// vPackets from the inputs.
12831298
vPkts, err := extractInputVPackets(inputs).Unpack()
12841299
if err != nil {
1285-
return lfn.Err[sweep.SweepOutput](err)
1300+
return lfn.Err[returnType](err)
12861301
}
12871302

12881303
log.Infof("Generating anchor output for vpkts=%v",
@@ -1297,7 +1312,7 @@ func (a *AuxSweeper) sweepContracts(inputs []input.Input,
12971312
context.Background(), asset.TaprootAssetsKeyFamily,
12981313
)
12991314
if err != nil {
1300-
return lfn.Err[sweep.SweepOutput](err)
1315+
return lfn.Err[returnType](err)
13011316
}
13021317
for idx := range vPkts {
13031318
for _, vOut := range vPkts[idx].Outputs {
@@ -1311,15 +1326,14 @@ func (a *AuxSweeper) sweepContracts(inputs []input.Input,
13111326
// out of all the vPackets contained.
13121327
outCommitments, err := tapsend.CreateOutputCommitments(vPkts)
13131328
if err != nil {
1314-
return lfn.Errf[sweep.SweepOutput]("unable to create output "+
1315-
"commitments: %w", err)
1329+
return lfn.Errf[returnType]("unable to create "+
1330+
"output commitments: %w", err)
13161331
}
13171332

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

13251339
// With the output commitments created, we'll now create the anchor
@@ -1328,7 +1342,7 @@ func (a *AuxSweeper) sweepContracts(inputs []input.Input,
13281342
internalKey.PubKey, nil, outCommitments[0],
13291343
)
13301344
if err != nil {
1331-
return lfn.Err[sweep.SweepOutput](err)
1345+
return lfn.Err[returnType](err)
13321346
}
13331347

13341348
return lfn.Ok(sweep.SweepOutput{
@@ -1351,8 +1365,8 @@ func sweepExclusionProofGen(sweepInternalKey keychain.KeyDescriptor,
13511365

13521366
tsProof, err := proof.CreateTapscriptProof(nil)
13531367
if err != nil {
1354-
return fmt.Errorf("error creating tapscript "+
1355-
"proof: %w", err)
1368+
return fmt.Errorf("error creating tapscript proof: %w",
1369+
err)
13561370
}
13571371

13581372
// We only need to generate an exclusion proof for the second
@@ -1507,18 +1521,20 @@ func (a *AuxSweeper) contractResolver() {
15071521
func (a *AuxSweeper) ResolveContract(
15081522
req lnwallet.ResolutionReq) lfn.Result[tlv.Blob] {
15091523

1524+
type returnType = tlv.Blob
1525+
15101526
auxReq := &resolutionReq{
15111527
req: req,
15121528
resp: make(chan lfn.Result[tlv.Blob], 1),
15131529
}
15141530

15151531
if !fn.SendOrQuit(a.resolutionReqs, auxReq, a.quit) {
1516-
return lfn.Err[tlv.Blob](fmt.Errorf("aux sweeper stopped"))
1532+
return lfn.Errf[returnType]("aux sweeper stopped")
15171533
}
15181534

15191535
resp, quitErr := fn.RecvResp(auxReq.resp, nil, a.quit)
15201536
if quitErr != nil {
1521-
return lfn.Err[tlv.Blob](quitErr)
1537+
return lfn.Err[returnType](quitErr)
15221538
}
15231539

15241540
return resp
@@ -1530,21 +1546,21 @@ func (a *AuxSweeper) ResolveContract(
15301546
func (a *AuxSweeper) DeriveSweepAddr(inputs []input.Input,
15311547
change lnwallet.AddrWithKey) lfn.Result[sweep.SweepOutput] {
15321548

1549+
type returnType = sweep.SweepOutput
1550+
15331551
auxReq := &sweepAddrReq{
15341552
inputs: inputs,
15351553
change: change,
15361554
resp: make(chan lfn.Result[sweep.SweepOutput], 1),
15371555
}
15381556

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

15451561
resp, quitErr := fn.RecvResp(auxReq.resp, nil, a.quit)
15461562
if quitErr != nil {
1547-
return lfn.Err[sweep.SweepOutput](quitErr)
1563+
return lfn.Err[returnType](quitErr)
15481564
}
15491565

15501566
return resp

0 commit comments

Comments
 (0)