Skip to content

Commit 6e920d0

Browse files
committed
tapchannel: utilize new outpointToTxIndex map in NotifyBroadcast
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.
1 parent 236eca3 commit 6e920d0

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1168,5 +1168,5 @@ func (s *Server) NotifyBroadcast(req *sweep.BumpRequest,
11681168
return err
11691169
}
11701170

1171-
return s.cfg.AuxSweeper.NotifyBroadcast(req, tx, fee)
1171+
return s.cfg.AuxSweeper.NotifyBroadcast(req, tx, fee, outpointToTxIndex)
11721172
}

tapchannel/aux_sweeper.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
@@ -2229,7 +2234,8 @@ func sweepExclusionProofGen(sweepInternalKey keychain.KeyDescriptor,
22292234
// registerAndBroadcastSweep finalizes a sweep attempt by generating a
22302235
// transition proof for it, then registering the sweep with the porter.
22312236
func (a *AuxSweeper) registerAndBroadcastSweep(req *sweep.BumpRequest,
2232-
sweepTx *wire.MsgTx, fee btcutil.Amount) error {
2237+
sweepTx *wire.MsgTx, fee btcutil.Amount,
2238+
outpointToTxIndex map[wire.OutPoint]int) error {
22332239

22342240
// TODO(roasbeef): need to handle replacement -- will porter just
22352241
// upsert in place?
@@ -2304,6 +2310,27 @@ func (a *AuxSweeper) registerAndBroadcastSweep(req *sweep.BumpRequest,
23042310
}
23052311
}
23062312

2313+
// For pre-signed HTLC txns we'll need to make sure we update the output
2314+
// index in the vPkt. As the ordering is only determined at broadcast
2315+
// time.
2316+
if outpointToTxIndex != nil {
2317+
for _, sweepPkt := range vPkts.allVpktsWithInput() {
2318+
op := sweepPkt.btcInput.OutPoint()
2319+
finalOutputIndex, ok := outpointToTxIndex[op]
2320+
if !ok {
2321+
continue
2322+
}
2323+
2324+
for _, vPkt := range sweepPkt.vPkts {
2325+
for _, vOut := range vPkt.Outputs {
2326+
vOut.AnchorOutputIndex = uint32(
2327+
finalOutputIndex,
2328+
)
2329+
}
2330+
}
2331+
}
2332+
}
2333+
23072334
// If we have second level vPkts, then we'll need to sign them here, as
23082335
// now we know the input we're spending which was set above.
23092336
for _, sweepSet := range vPkts.secondLevel {
@@ -2402,6 +2429,7 @@ func (a *AuxSweeper) contractResolver() {
24022429
case req := <-a.broadcastReqs:
24032430
req.resp <- a.registerAndBroadcastSweep(
24042431
req.req, req.tx, req.fee,
2432+
req.outpointToTxIndex,
24052433
)
24062434

24072435
case <-a.quit:
@@ -2489,13 +2517,15 @@ func (a *AuxSweeper) ExtraBudgetForInputs(
24892517
// NotifyBroadcast is used to notify external callers of the broadcast of a
24902518
// sweep transaction, generated by the passed BumpRequest.
24912519
func (a *AuxSweeper) NotifyBroadcast(req *sweep.BumpRequest,
2492-
tx *wire.MsgTx, fee btcutil.Amount) error {
2520+
tx *wire.MsgTx, fee btcutil.Amount,
2521+
outpointToTxIndex map[wire.OutPoint]int) error {
24932522

24942523
auxReq := &broadcastReq{
2495-
req: req,
2496-
tx: tx,
2497-
fee: fee,
2498-
resp: make(chan error, 1),
2524+
req: req,
2525+
tx: tx,
2526+
fee: fee,
2527+
outpointToTxIndex: outpointToTxIndex,
2528+
resp: make(chan error, 1),
24992529
}
25002530

25012531
if !fn.SendOrQuit(a.broadcastReqs, auxReq, a.quit) {

0 commit comments

Comments
 (0)