Skip to content

Commit

Permalink
tapchannel: refactor result type
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
guggero committed Sep 19, 2024
1 parent 11d1821 commit 163d1ad
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 47 deletions.
6 changes: 3 additions & 3 deletions tapchannel/aux_closer.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func createCloseAlloc(isLocal, isInitiator bool, closeAsset *asset.Asset,
}, nil
}

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

return lfn.Ok(wire.TxWitness{
Expand Down
105 changes: 61 additions & 44 deletions tapchannel/aux_sweeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,15 @@ func (a *AuxSweeper) createSweepVpackets(sweepInputs []*cmsg.AssetOutput,
tapscriptDesc lfn.Result[tapscriptSweepDesc],
) lfn.Result[[]*tappsbt.VPacket] {

type returnType = []*tappsbt.VPacket

log.Infof("Creating sweep packets for %v inputs", len(sweepInputs))

// Unpack the tapscript desc, as we need it to be able to continue
// forward.
sweepDesc, err := tapscriptDesc.Unpack()
if err != nil {
return lfn.Err[[]*tappsbt.VPacket](err)
return lfn.Err[returnType](err)
}

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

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

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

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

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

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

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

type returnType = []*tappsbt.VPacket

// Based on the sweep inputs, make vPackets that sweep all the inputs
// into a new output with a fresh script key. They won't have an
// internal key set, we'll do that when we go to make the output to
Expand All @@ -364,7 +368,7 @@ func (a *AuxSweeper) createAndSignSweepVpackets(

err := a.signSweepVpackets(vPkts, signDesc, desc)
if err != nil {
return lfn.Err[[]*tappsbt.VPacket](err)
return lfn.Err[returnType](err)
}

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

type returnType = tapscriptSweepDesc

// We'll make the script tree for the to remote script (we're remote as
// this is their commitment transaction). We don't have an auxLeaf here
// as we're on the TAP layer.
toRemoteScriptTree, err := input.NewRemoteCommitScriptTree(
keyRing.ToRemoteKey, input.NoneTapLeaf(),
)
if err != nil {
return lfn.Errf[tapscriptSweepDesc]("unable to make remote "+
"script tree: %w", err)
return lfn.Err[returnType](fmt.Errorf("unable to make remote "+
"script tree: %w", err))
}

// Now that we have the script tree, we'll make the control block
Expand All @@ -414,13 +420,13 @@ func commitNoDelaySweepDesc(keyRing *lnwallet.CommitmentKeyRing,
input.ScriptPathSuccess,
)
if err != nil {
return lfn.Errf[tapscriptSweepDesc]("unable to make "+
"ctrl block: %w", err)
return lfn.Err[returnType](fmt.Errorf("unable to make ctrl "+
"block: %w", err))
}
ctrlBlockBytes, err := ctrlBlock.ToBytes()
if err != nil {
return lfn.Errf[tapscriptSweepDesc]("unable to encode ctrl "+
"block: %w", err)
return lfn.Err[returnType](fmt.Errorf("unable to encode ctrl "+
"block: %w", err))
}

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

type returnType = tapscriptSweepDesc

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

// Now that we have the script tree, we'll make the control block
Expand All @@ -453,11 +461,11 @@ func commitDelaySweepDesc(keyRing *lnwallet.CommitmentKeyRing,
input.ScriptPathSuccess,
)
if err != nil {
return lfn.Err[tapscriptSweepDesc](err)
return lfn.Err[returnType](err)
}
ctrlBlockBytes, err := ctrlBlock.ToBytes()
if err != nil {
return lfn.Err[tapscriptSweepDesc](err)
return lfn.Err[returnType](err)
}

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

type returnType = tapscriptSweepDesc

// To sweep their revoked output, we'll make the script tree for the
// local tree of their commitment transaction, which is actually their
// output.
Expand All @@ -481,7 +491,7 @@ func commitRevokeSweepDesc(keyRing *lnwallet.CommitmentKeyRing,
input.NoneTapLeaf(),
)
if err != nil {
return lfn.Err[tapscriptSweepDesc](err)
return lfn.Err[returnType](err)
}

// Now that we have the script tree, we'll make the control block
Expand All @@ -490,11 +500,11 @@ func commitRevokeSweepDesc(keyRing *lnwallet.CommitmentKeyRing,
input.ScriptPathRevocation,
)
if err != nil {
return lfn.Err[tapscriptSweepDesc](err)
return lfn.Err[returnType](err)
}
ctrlBlockBytes, err := ctrlBlock.ToBytes()
if err != nil {
return lfn.Err[tapscriptSweepDesc](err)
return lfn.Err[returnType](err)
}

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

type returnType = tlv.Blob

// If there's no commit blob, then there's nothing to resolve.
if req.CommitBlob.IsNone() {
return lfn.Err[tlv.Blob](nil)
Expand All @@ -1112,13 +1124,13 @@ func (a *AuxSweeper) resolveContract(
req.CommitBlob.UnwrapOr(nil),
)
if err != nil {
return lfn.Err[tlv.Blob](err)
return lfn.Err[returnType](err)
}
fundingInfo, err := tapchannelmsg.DecodeOpenChannel(
req.FundingBlob.UnwrapOr(nil),
)
if err != nil {
return lfn.Err[tlv.Blob](err)
return lfn.Err[returnType](err)
}

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

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

default:
return lfn.Err[tlv.Blob](fmt.Errorf("unknown resolution "+
return lfn.Err[returnType](fmt.Errorf("unknown resolution "+
"type: %v", req.Type))
}

Expand Down Expand Up @@ -1218,7 +1230,7 @@ func (a *AuxSweeper) resolveContract(

var b bytes.Buffer
if err := res.Encode(&b); err != nil {
return lfn.Err[tlv.Blob](err)
return lfn.Err[returnType](err)
}

return lfn.Ok(b.Bytes())
Expand All @@ -1230,6 +1242,8 @@ func (a *AuxSweeper) resolveContract(
// none of the inputs have any resolution blobs. Then an empty slice will be
// returned.
func extractInputVPackets(inputs []input.Input) lfn.Result[[]*tappsbt.VPacket] {
type returnType = []*tappsbt.VPacket

// Otherwise, we'll extract the set of resolution blobs from the inputs
// passed in.
relevantInputs := fn.Filter(inputs, func(i input.Input) bool {
Expand All @@ -1254,7 +1268,7 @@ func extractInputVPackets(inputs []input.Input) lfn.Result[[]*tappsbt.VPacket] {
},
)
if err != nil {
return lfn.Err[[]*tappsbt.VPacket](err)
return lfn.Err[returnType](err)
}

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

type returnType = sweep.SweepOutput

// If none of the inputs have a resolution blob, then we have nothing
// to generate.
if fn.NotAny(inputs, func(i input.Input) bool {
return !i.ResolutionBlob().IsNone()
}) {

return lfn.Err[sweep.SweepOutput](nil)
return lfn.Err[returnType](nil)
}

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

log.Infof("Generating anchor output for vpkts=%v",
Expand All @@ -1296,7 +1312,7 @@ func (a *AuxSweeper) sweepContracts(inputs []input.Input,
context.Background(), asset.TaprootAssetsKeyFamily,
)
if err != nil {
return lfn.Err[sweep.SweepOutput](err)
return lfn.Err[returnType](err)
}
for idx := range vPkts {
for _, vOut := range vPkts[idx].Outputs {
Expand All @@ -1310,15 +1326,14 @@ func (a *AuxSweeper) sweepContracts(inputs []input.Input,
// out of all the vPackets contained.
outCommitments, err := tapsend.CreateOutputCommitments(vPkts)
if err != nil {
return lfn.Errf[sweep.SweepOutput]("unable to create output "+
"commitments: %w", err)
return lfn.Err[returnType](fmt.Errorf("unable to create "+
"output commitments: %w", err))
}

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

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

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

type returnType = tlv.Blob

auxReq := &resolutionReq{
req: req,
resp: make(chan lfn.Result[tlv.Blob], 1),
}

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

resp, quitErr := fn.RecvResp(auxReq.resp, nil, a.quit)
if quitErr != nil {
return lfn.Err[tlv.Blob](quitErr)
return lfn.Err[returnType](quitErr)
}

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

type returnType = sweep.SweepOutput

auxReq := &sweepAddrReq{
inputs: inputs,
change: change,
resp: make(chan lfn.Result[sweep.SweepOutput], 1),
}

if !fn.SendOrQuit(a.sweepAddrReqs, auxReq, a.quit) {
return lfn.Err[sweep.SweepOutput](
fmt.Errorf("aux sweeper stopped"),
)
return lfn.Err[returnType](fmt.Errorf("aux sweeper stopped"))
}

resp, quitErr := fn.RecvResp(auxReq.resp, nil, a.quit)
if quitErr != nil {
return lfn.Err[sweep.SweepOutput](quitErr)
return lfn.Err[returnType](quitErr)
}

return resp
Expand Down

0 comments on commit 163d1ad

Please sign in to comment.