Skip to content

Commit 7cf893c

Browse files
committed
tapchannel: update registerAndBroadcastSweep for HTLC sweeps
In this commit, we update `registerAndBroadcastSweep` to be able to handle HTLC sweeps. First, we only need to set the anchor for first level sweeps. We weren't able to sign the 2nd level sweeps earlier as we didn't know the txid of the transaction that swept them. Now that we're about to broadcast, we know the sweeping transaction so we can set the prevID properly, then sign the sweeping transaction.
1 parent a6fa937 commit 7cf893c

File tree

1 file changed

+47
-9
lines changed

1 file changed

+47
-9
lines changed

tapchannel/aux_sweeper.go

+47-9
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,7 @@ func (a *AuxSweeper) resolveContract(
17131713
default:
17141714
return lfn.Err[tlv.Blob](fmt.Errorf("unknown resolution "+
17151715
"type: %v", req.Type))
1716+
// TODO(roasbeef): need to do HTLC revocation casesj:w
17161717
}
17171718

17181719
// The input proofs above were made originally using the fake commit tx
@@ -1944,6 +1945,7 @@ func prepVpkts(bRes lfn.Result[blobWithWitnessInfo],
19441945
// extractInputVPackets extracts the vPackets from the inputs passed in. If
19451946
// none of the inputs have any resolution blobs. Then an empty slice will be
19461947
// returned.
1948+
func extractInputVPackets(inputs []input.Input) lfn.Result[sweepVpkts] {
19471949
// Otherwise, we'll extract the set of resolution blobs from the inputs
19481950
// passed in.
19491951
relevantInputs := fn.Filter(inputs, func(i input.Input) bool {
@@ -2148,7 +2150,7 @@ func (a *AuxSweeper) registerAndBroadcastSweep(req *sweep.BumpRequest,
21482150

21492151
// If we don't have any vPackets that had our resolution data in them,
21502152
// then we can exit early.
2151-
if len(vPkts) == 0 {
2153+
if len(vPkts.firstLevel) == 0 && len(vPkts.secondLevel) == 0 {
21522154
log.Infof("Sweep request had no vPkts, exiting")
21532155
return nil
21542156
}
@@ -2170,17 +2172,52 @@ func (a *AuxSweeper) registerAndBroadcastSweep(req *sweep.BumpRequest,
21702172
internalKey.PubKey.SerializeCompressed())
21712173

21722174
// We'll also use the passed in context to set the anchor key again for
2173-
// all the vOuts.
2174-
for idx := range vPkts {
2175-
for _, vOut := range vPkts[idx].Outputs {
2175+
// all the vOuts, but only for first level vPkts, as second level
2176+
// packets already commit to the internal key of the vOut.
2177+
for idx := range vPkts.firstLevelPkts() {
2178+
for _, vOut := range vPkts.firstLevelPkts()[idx].Outputs {
21762179
vOut.SetAnchorInternalKey(
21772180
internalKey, a.cfg.ChainParams.HDCoinType,
21782181
)
21792182
}
21802183
}
21812184

2185+
// For any second level outputs we're sweeping, we'll need to sign for
2186+
// it, as now we know the txid of the sweeping transaction.
2187+
for _, sweepSet := range vPkts.secondLevel {
2188+
for _, vPkt := range sweepSet.vPkts {
2189+
for _, vIn := range vPkt.Inputs {
2190+
vIn.PrevID.OutPoint = sweepSet.btcInput.OutPoint()
2191+
}
2192+
}
2193+
}
2194+
2195+
// If we have second level vPkts, then we'll need to sign them here, as
2196+
// now we know the input we're spending which was set above.
2197+
for _, sweepSet := range vPkts.secondLevel {
2198+
tapSigDesc, err := sweepSet.tapSigDesc.UnwrapOrErr(
2199+
fmt.Errorf("tap sig desc not populated"),
2200+
)
2201+
if err != nil {
2202+
return err
2203+
}
2204+
2205+
err = a.signSweepVpackets(
2206+
sweepSet.vPkts, *sweepSet.btcInput.SignDesc(),
2207+
tapSigDesc.TapTweak.Val, tapSigDesc.CtrlBlock.Val,
2208+
lfn.None[lnwallet.AuxSigDesc](),
2209+
lfn.None[uint32](),
2210+
)
2211+
if err != nil {
2212+
return fmt.Errorf("unable to sign second level "+
2213+
"vPkts: %w", err)
2214+
}
2215+
}
2216+
21822217
// Now that we have our vPkts, we'll re-create the output commitments.
2183-
outCommitments, err := tapsend.CreateOutputCommitments(vPkts)
2218+
outCommitments, err := tapsend.CreateOutputCommitments(
2219+
vPkts.allPkts(),
2220+
)
21842221
if err != nil {
21852222
return fmt.Errorf("unable to create output "+
21862223
"commitments: %w", err)
@@ -2202,15 +2239,16 @@ func (a *AuxSweeper) registerAndBroadcastSweep(req *sweep.BumpRequest,
22022239
//
22032240
// TODO(roasbeef): base off allocations? then can serialize, then
22042241
// re-use the logic
2205-
for idx := range vPkts {
2206-
vPkt := vPkts[idx]
2242+
allVpkts := vPkts.allPkts()
2243+
for idx := range allVpkts {
2244+
vPkt := allVpkts[idx]
22072245
for outIdx := range vPkt.Outputs {
22082246
exclusionCreator := sweepExclusionProofGen(
22092247
changeInternalKey,
22102248
)
22112249

22122250
proofSuffix, err := tapsend.CreateProofSuffixCustom(
2213-
sweepTx, vPkt, outCommitments, outIdx, vPkts,
2251+
sweepTx, vPkt, outCommitments, outIdx, allVpkts,
22142252
exclusionCreator,
22152253
)
22162254
if err != nil {
@@ -2230,7 +2268,7 @@ func (a *AuxSweeper) registerAndBroadcastSweep(req *sweep.BumpRequest,
22302268
// We pass false for the last arg as we already updated our suffix
22312269
// proofs here.
22322270
return shipChannelTxn(
2233-
a.cfg.TxSender, sweepTx, outCommitments, vPkts, int64(fee),
2271+
a.cfg.TxSender, sweepTx, outCommitments, allVpkts, int64(fee),
22342272
)
22352273
}
22362274

0 commit comments

Comments
 (0)