@@ -76,6 +76,11 @@ type broadcastReq struct {
7676 // fee is the fee that was used for the transaction.
7777 fee btcutil.Amount
7878
79+ // outpointToTxIndex maps a spent outpoint to the tx index on the sweep
80+ // transaction of the corresponding output. This is only needed to make
81+ // sure we make proofs properly for the pre-signed HTLC transactions.
82+ outpointToTxIndex map [wire.OutPoint ]int
83+
7984 // resp is the error result of the broadcast.
8085 resp chan error
8186}
@@ -490,6 +495,11 @@ func (s sweepVpkts) allPkts() []*tappsbt.VPacket {
490495 return append (s .firstLevelPkts (), s .secondLevelPkts ()... )
491496}
492497
498+ // allVpktsWithInput returns a slice of all vPktsWithInput.
499+ func (s sweepVpkts ) allVpktsWithInput () []vPktsWithInput {
500+ return append (s .firstLevel , s .secondLevel ... )
501+ }
502+
493503// createAndSignSweepVpackets creates vPackets that sweep the funds from the
494504// channel to the wallet, and then signs them as well.
495505func (a * AuxSweeper ) createAndSignSweepVpackets (
@@ -2149,7 +2159,8 @@ func sweepExclusionProofGen(sweepInternalKey keychain.KeyDescriptor,
21492159// registerAndBroadcastSweep finalizes a sweep attempt by generating a
21502160// transition proof for it, then registering the sweep with the porter.
21512161func (a * AuxSweeper ) registerAndBroadcastSweep (req * sweep.BumpRequest ,
2152- sweepTx * wire.MsgTx , fee btcutil.Amount ) error {
2162+ sweepTx * wire.MsgTx , fee btcutil.Amount ,
2163+ outpointToTxIndex map [wire.OutPoint ]int ) error {
21532164
21542165 // TODO(roasbeef): need to handle replacement -- will porter just
21552166 // upsert in place?
@@ -2207,6 +2218,27 @@ func (a *AuxSweeper) registerAndBroadcastSweep(req *sweep.BumpRequest,
22072218 }
22082219 }
22092220
2221+ // For pre-signed HTLC txns we'll need to make sure we update the output
2222+ // index in the vPkt. As the ordering is only determined at broadcast
2223+ // time.
2224+ if outpointToTxIndex != nil {
2225+ for _ , sweepPkt := range vPkts .allVpktsWithInput () {
2226+ op := sweepPkt .btcInput .OutPoint ()
2227+ finalOutputIndex , ok := outpointToTxIndex [op ]
2228+ if ! ok {
2229+ continue
2230+ }
2231+
2232+ for _ , vPkt := range sweepPkt .vPkts {
2233+ for _ , vOut := range vPkt .Outputs {
2234+ vOut .AnchorOutputIndex = uint32 (
2235+ finalOutputIndex ,
2236+ )
2237+ }
2238+ }
2239+ }
2240+ }
2241+
22102242 // If we have second level vPkts, then we'll need to sign them here, as
22112243 // now we know the input we're spending which was set above.
22122244 for _ , sweepSet := range vPkts .secondLevel {
@@ -2305,6 +2337,7 @@ func (a *AuxSweeper) contractResolver() {
23052337 case req := <- a .broadcastReqs :
23062338 req .resp <- a .registerAndBroadcastSweep (
23072339 req .req , req .tx , req .fee ,
2340+ req .outpointToTxIndex ,
23082341 )
23092342
23102343 case <- a .quit :
@@ -2392,13 +2425,15 @@ func (a *AuxSweeper) ExtraBudgetForInputs(
23922425// NotifyBroadcast is used to notify external callers of the broadcast of a
23932426// sweep transaction, generated by the passed BumpRequest.
23942427func (a * AuxSweeper ) NotifyBroadcast (req * sweep.BumpRequest ,
2395- tx * wire.MsgTx , fee btcutil.Amount ) error {
2428+ tx * wire.MsgTx , fee btcutil.Amount ,
2429+ outpointToTxIndex map [wire.OutPoint ]int ) error {
23962430
23972431 auxReq := & broadcastReq {
2398- req : req ,
2399- tx : tx ,
2400- fee : fee ,
2401- resp : make (chan error , 1 ),
2432+ req : req ,
2433+ tx : tx ,
2434+ fee : fee ,
2435+ outpointToTxIndex : outpointToTxIndex ,
2436+ resp : make (chan error , 1 ),
24022437 }
24032438
24042439 if ! fn .SendOrQuit (a .broadcastReqs , auxReq , a .quit ) {
0 commit comments