Skip to content

Commit

Permalink
tapchannel: utilize new outpointToTxIndex map in NotifyBroadcast
Browse files Browse the repository at this point in the history
In this commit, we update the logic of `notifyBroadcast` to use the new
`outpointToTxIndex` map. We'll use this to make sure that the vOut has
the correct anchor output index, as the sweeper applies a sorting
routine before broadcast.

Otherwise, we'll have invalid inclusion proofs.
  • Loading branch information
Roasbeef committed Nov 13, 2024
1 parent 4d95ae2 commit 4a49d93
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1170,5 +1170,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)
}
42 changes: 36 additions & 6 deletions tapchannel/aux_sweeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -2226,7 +2231,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?
Expand Down Expand Up @@ -2302,6 +2308,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 {
Expand Down Expand Up @@ -2400,6 +2427,7 @@ func (a *AuxSweeper) contractResolver() {
case req := <-a.broadcastReqs:
req.resp <- a.registerAndBroadcastSweep(
req.req, req.tx, req.fee,
req.outpointToTxIndex,
)

case <-a.quit:
Expand Down Expand Up @@ -2487,13 +2515,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) {
Expand Down

0 comments on commit 4a49d93

Please sign in to comment.