Skip to content

Commit ef792da

Browse files
committed
tapchannel: extract allocation creation from CreateSecondLevelHtlcPackets
This'll be useful later when we need to generate the vPkt for second level spends.
1 parent 565bc6d commit ef792da

File tree

1 file changed

+47
-30
lines changed

1 file changed

+47
-30
lines changed

tapchannel/commitment.go

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,45 +1220,46 @@ func collectOutputs(a *Allocation,
12201220
return outputs, nil
12211221
}
12221222

1223-
// CreateSecondLevelHtlcPackets creates the virtual packets for the second level
1224-
// HTLC transaction.
1225-
func CreateSecondLevelHtlcPackets(chanState lnwallet.AuxChanState,
1226-
commitTx *wire.MsgTx, htlcAmt btcutil.Amount,
1227-
keys lnwallet.CommitmentKeyRing, chainParams *address.ChainParams,
1228-
htlcOutputs []*cmsg.AssetOutput) ([]*tappsbt.VPacket, []*Allocation,
1229-
error) {
1223+
// createSecondLevelHtlcAllocations creates the allocations for the second level
1224+
// HTLCs. This will be used to generate the vPkts that corresponds to the second
1225+
// level HTLC sweep.
1226+
func createSecondLevelHtlcAllocations(chanType channeldb.ChannelType,
1227+
initiator bool, htlcOutputs []*cmsg.AssetOutput, htlcAmt btcutil.Amount,
1228+
commitCsvDelay uint32, keys lnwallet.CommitmentKeyRing,
1229+
outputIndex fn.Option[uint32],
1230+
) ([]*Allocation, error) {
1231+
1232+
// TODO(roasbeef): thaw height not implemented for taproot chans rn
1233+
// (lease expiry)
12301234

1231-
var leaseExpiry uint32
1232-
if chanState.ChanType.HasLeaseExpiration() {
1233-
leaseExpiry = chanState.ThawHeight
1234-
}
1235-
1236-
// Next, we'll generate the script used as the output for all second
1237-
// level HTLC which forces a covenant w.r.t what can be done with all
1238-
// HTLC outputs.
12391235
scriptInfo, err := lnwallet.SecondLevelHtlcScript(
1240-
chanState.ChanType, chanState.IsInitiator, keys.RevocationKey,
1241-
keys.ToLocalKey, uint32(chanState.LocalChanCfg.CsvDelay),
1242-
leaseExpiry, lfn.None[txscript.TapLeaf](),
1236+
chanType, initiator, keys.RevocationKey,
1237+
keys.ToLocalKey, commitCsvDelay,
1238+
0, lfn.None[txscript.TapLeaf](),
12431239
)
12441240
if err != nil {
1245-
return nil, nil, fmt.Errorf("error creating second level htlc "+
1246-
"script: %w", err)
1241+
return nil, fmt.Errorf("error creating second level "+
1242+
"htlc script: %w", err)
12471243
}
12481244

12491245
sibling, htlcTree, err := LeavesFromTapscriptScriptTree(scriptInfo)
12501246
if err != nil {
1251-
return nil, nil, fmt.Errorf("error creating second level HTLC "+
1247+
return nil, fmt.Errorf("error creating second level HTLC "+
12521248
"script sibling: %w", err)
12531249
}
12541250

12551251
allocations := []*Allocation{{
1256-
Type: SecondLevelHtlcAllocation,
1252+
Type: SecondLevelHtlcAllocation,
1253+
// If we're making the second-level transaction just to sign,
1254+
// then we'll have an output index of zero. Otherwise, we'll
1255+
// want to use the output index as appears in the final
1256+
// commitment transaction.
1257+
OutputIndex: outputIndex.UnwrapOr(0),
12571258
Amount: cmsg.OutputSum(htlcOutputs),
12581259
AssetVersion: asset.V1,
12591260
BtcAmount: htlcAmt,
12601261
Sequence: lnwallet.HtlcSecondLevelInputSequence(
1261-
chanState.ChanType,
1262+
chanType,
12621263
),
12631264
InternalKey: htlcTree.InternalKey,
12641265
NonAssetLeaves: sibling,
@@ -1268,13 +1269,31 @@ func CreateSecondLevelHtlcPackets(chanState lnwallet.AuxChanState,
12681269
),
12691270
}}
12701271

1271-
// The proofs in the asset outputs don't have the full commitment
1272-
// transaction, so we need to add it now to make them complete.
1272+
return allocations, nil
1273+
}
1274+
1275+
// CreateSecondLevelHtlcPackets creates the virtual packets for the second level
1276+
// HTLC.
1277+
func CreateSecondLevelHtlcPackets(chanState lnwallet.AuxChanState,
1278+
commitTx *wire.MsgTx, htlcAmt btcutil.Amount,
1279+
keys lnwallet.CommitmentKeyRing, chainParams *address.ChainParams,
1280+
htlcOutputs []*cmsg.AssetOutput) ([]*tappsbt.VPacket, []*Allocation,
1281+
error) {
1282+
1283+
allocations, err := createSecondLevelHtlcAllocations(
1284+
chanState.ChanType, chanState.IsInitiator,
1285+
htlcOutputs, htlcAmt,
1286+
uint32(chanState.LocalChanCfg.CsvDelay), keys,
1287+
fn.None[uint32](),
1288+
)
1289+
if err != nil {
1290+
return nil, nil, err
1291+
}
1292+
12731293
inputProofs := fn.Map(
12741294
htlcOutputs, func(o *cmsg.AssetOutput) *proof.Proof {
12751295
p := o.Proof.Val
12761296
p.AnchorTx = *commitTx
1277-
12781297
return &p
12791298
},
12801299
)
@@ -1284,14 +1303,12 @@ func CreateSecondLevelHtlcPackets(chanState lnwallet.AuxChanState,
12841303
return nil, nil, fmt.Errorf("error distributing coins: %w", err)
12851304
}
12861305

1287-
// Prepare the output assets for each virtual packet, then create the
1288-
// output commitments.
12891306
ctx := context.Background()
12901307
for idx := range vPackets {
12911308
err := tapsend.PrepareOutputAssets(ctx, vPackets[idx])
12921309
if err != nil {
1293-
return nil, nil, fmt.Errorf("unable to prepare output "+
1294-
"assets: %w", err)
1310+
return nil, nil, fmt.Errorf("unable to prepare "+
1311+
"output assets: %w", err)
12951312
}
12961313
}
12971314

0 commit comments

Comments
 (0)