Skip to content

Commit f9f7b55

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 f44cdd1 commit f9f7b55

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

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

tapchannel/aux_sweeper.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ type broadcastReq struct {
8484
// fee is the fee that was used for the transaction.
8585
fee btcutil.Amount
8686

87+
// outpointToTxIndex maps a spent outpoint to the tx index on the sweep
88+
// transaction of the corresponding output. This is only needed to make
89+
// sure we make proofs properly for the pre-signed HTLC transactions.
90+
outpointToTxIndex map[wire.OutPoint]int
91+
8792
// resp is the error result of the broadcast.
8893
resp chan error
8994
}
@@ -2215,7 +2220,8 @@ func sweepExclusionProofGen(sweepInternalKey keychain.KeyDescriptor,
22152220
// registerAndBroadcastSweep finalizes a sweep attempt by generating a
22162221
// transition proof for it, then registering the sweep with the porter.
22172222
func (a *AuxSweeper) registerAndBroadcastSweep(req *sweep.BumpRequest,
2218-
sweepTx *wire.MsgTx, fee btcutil.Amount) error {
2223+
sweepTx *wire.MsgTx, fee btcutil.Amount,
2224+
outpointToTxIndex map[wire.OutPoint]int) error {
22192225

22202226
// TODO(roasbeef): need to handle replacement -- will porter just
22212227
// upsert in place?
@@ -2292,6 +2298,27 @@ func (a *AuxSweeper) registerAndBroadcastSweep(req *sweep.BumpRequest,
22922298
}
22932299
}
22942300

2301+
// For pre-signed HTLC txns we'll need to make sure we update the output
2302+
// index in the vPkt. As the ordering is only determined at broadcast
2303+
// time.
2304+
if outpointToTxIndex != nil {
2305+
for _, sweepPkt := range vPkts.allVpktsWithInput() {
2306+
op := sweepPkt.btcInput.OutPoint()
2307+
finalOutputIndex, ok := outpointToTxIndex[op]
2308+
if !ok {
2309+
continue
2310+
}
2311+
2312+
for _, vPkt := range sweepPkt.vPkts {
2313+
for _, vOut := range vPkt.Outputs {
2314+
vOut.AnchorOutputIndex = uint32(
2315+
finalOutputIndex,
2316+
)
2317+
}
2318+
}
2319+
}
2320+
}
2321+
22952322
// If we have second level vPkts, then we'll need to sign them here, as
22962323
// now we know the input we're spending which was set above.
22972324
for _, sweepSet := range vPkts.secondLevel {
@@ -2390,6 +2417,7 @@ func (a *AuxSweeper) contractResolver() {
23902417
case req := <-a.broadcastReqs:
23912418
req.resp <- a.registerAndBroadcastSweep(
23922419
req.req, req.tx, req.fee,
2420+
req.outpointToTxIndex,
23932421
)
23942422

23952423
case <-a.quit:
@@ -2477,13 +2505,15 @@ func (a *AuxSweeper) ExtraBudgetForInputs(
24772505
// NotifyBroadcast is used to notify external callers of the broadcast of a
24782506
// sweep transaction, generated by the passed BumpRequest.
24792507
func (a *AuxSweeper) NotifyBroadcast(req *sweep.BumpRequest,
2480-
tx *wire.MsgTx, fee btcutil.Amount) error {
2508+
tx *wire.MsgTx, fee btcutil.Amount,
2509+
outpointToTxIndex map[wire.OutPoint]int) error {
24812510

24822511
auxReq := &broadcastReq{
2483-
req: req,
2484-
tx: tx,
2485-
fee: fee,
2486-
resp: make(chan error, 1),
2512+
req: req,
2513+
tx: tx,
2514+
fee: fee,
2515+
outpointToTxIndex: outpointToTxIndex,
2516+
resp: make(chan error, 1),
24872517
}
24882518

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

0 commit comments

Comments
 (0)