Skip to content

Commit 8c772aa

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 e9cad66 commit 8c772aa

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
@@ -1170,5 +1170,5 @@ func (s *Server) NotifyBroadcast(req *sweep.BumpRequest,
11701170
return err
11711171
}
11721172

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

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
}
@@ -2226,7 +2231,8 @@ func sweepExclusionProofGen(sweepInternalKey keychain.KeyDescriptor,
22262231
// registerAndBroadcastSweep finalizes a sweep attempt by generating a
22272232
// transition proof for it, then registering the sweep with the porter.
22282233
func (a *AuxSweeper) registerAndBroadcastSweep(req *sweep.BumpRequest,
2229-
sweepTx *wire.MsgTx, fee btcutil.Amount) error {
2234+
sweepTx *wire.MsgTx, fee btcutil.Amount,
2235+
outpointToTxIndex map[wire.OutPoint]int) error {
22302236

22312237
// TODO(roasbeef): need to handle replacement -- will porter just
22322238
// upsert in place?
@@ -2302,6 +2308,27 @@ func (a *AuxSweeper) registerAndBroadcastSweep(req *sweep.BumpRequest,
23022308
}
23032309
}
23042310

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

24052433
case <-a.quit:
@@ -2487,13 +2515,15 @@ func (a *AuxSweeper) ExtraBudgetForInputs(
24872515
// NotifyBroadcast is used to notify external callers of the broadcast of a
24882516
// sweep transaction, generated by the passed BumpRequest.
24892517
func (a *AuxSweeper) NotifyBroadcast(req *sweep.BumpRequest,
2490-
tx *wire.MsgTx, fee btcutil.Amount) error {
2518+
tx *wire.MsgTx, fee btcutil.Amount,
2519+
outpointToTxIndex map[wire.OutPoint]int) error {
24912520

24922521
auxReq := &broadcastReq{
2493-
req: req,
2494-
tx: tx,
2495-
fee: fee,
2496-
resp: make(chan error, 1),
2522+
req: req,
2523+
tx: tx,
2524+
fee: fee,
2525+
outpointToTxIndex: outpointToTxIndex,
2526+
resp: make(chan error, 1),
24972527
}
24982528

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

0 commit comments

Comments
 (0)