diff --git a/server.go b/server.go index b0c74011c..39b5432c5 100644 --- a/server.go +++ b/server.go @@ -1168,5 +1168,5 @@ func (s *Server) NotifyBroadcast(req *sweep.BumpRequest, return err } - return s.cfg.AuxSweeper.NotifyBroadcast(req, tx, fee) + return s.cfg.AuxSweeper.NotifyBroadcast(req, tx, fee, outpointToTxIndex) } diff --git a/tapchannel/aux_sweeper.go b/tapchannel/aux_sweeper.go index 7b52cb80a..6df5e7096 100644 --- a/tapchannel/aux_sweeper.go +++ b/tapchannel/aux_sweeper.go @@ -76,6 +76,11 @@ type broadcastReq struct { // fee is the fee that was used for the transaction. fee btcutil.Amount + // outpointToTxIndex maps a spent outpoint to the tx index on the sweep + // transaction of the corresponding output. This is only needed to make + // sure we make proofs properly for the pre-signed HTLC transactions. + outpointToTxIndex map[wire.OutPoint]int + // resp is the error result of the broadcast. resp chan error } @@ -2229,7 +2234,8 @@ func sweepExclusionProofGen(sweepInternalKey keychain.KeyDescriptor, // registerAndBroadcastSweep finalizes a sweep attempt by generating a // transition proof for it, then registering the sweep with the porter. func (a *AuxSweeper) registerAndBroadcastSweep(req *sweep.BumpRequest, - sweepTx *wire.MsgTx, fee btcutil.Amount) error { + sweepTx *wire.MsgTx, fee btcutil.Amount, + outpointToTxIndex map[wire.OutPoint]int) error { // TODO(roasbeef): need to handle replacement -- will porter just // upsert in place? @@ -2304,6 +2310,27 @@ func (a *AuxSweeper) registerAndBroadcastSweep(req *sweep.BumpRequest, } } + // For pre-signed HTLC txns we'll need to make sure we update the output + // index in the vPkt. As the ordering is only determined at broadcast + // time. + if outpointToTxIndex != nil { + for _, sweepPkt := range vPkts.allVpktsWithInput() { + op := sweepPkt.btcInput.OutPoint() + finalOutputIndex, ok := outpointToTxIndex[op] + if !ok { + continue + } + + for _, vPkt := range sweepPkt.vPkts { + for _, vOut := range vPkt.Outputs { + vOut.AnchorOutputIndex = uint32( + finalOutputIndex, + ) + } + } + } + } + // If we have second level vPkts, then we'll need to sign them here, as // now we know the input we're spending which was set above. for _, sweepSet := range vPkts.secondLevel { @@ -2402,6 +2429,7 @@ func (a *AuxSweeper) contractResolver() { case req := <-a.broadcastReqs: req.resp <- a.registerAndBroadcastSweep( req.req, req.tx, req.fee, + req.outpointToTxIndex, ) case <-a.quit: @@ -2489,13 +2517,15 @@ func (a *AuxSweeper) ExtraBudgetForInputs( // NotifyBroadcast is used to notify external callers of the broadcast of a // sweep transaction, generated by the passed BumpRequest. func (a *AuxSweeper) NotifyBroadcast(req *sweep.BumpRequest, - tx *wire.MsgTx, fee btcutil.Amount) error { + tx *wire.MsgTx, fee btcutil.Amount, + outpointToTxIndex map[wire.OutPoint]int) error { auxReq := &broadcastReq{ - req: req, - tx: tx, - fee: fee, - resp: make(chan error, 1), + req: req, + tx: tx, + fee: fee, + outpointToTxIndex: outpointToTxIndex, + resp: make(chan error, 1), } if !fn.SendOrQuit(a.broadcastReqs, auxReq, a.quit) {