Skip to content

Commit 54a6979

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 8a861c4 commit 54a6979

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,8 @@ func (s *Server) ExtraBudgetForInputs(
11601160
//
11611161
// NOTE: This method is part of the sweep.AuxSweeper interface.
11621162
func (s *Server) NotifyBroadcast(req *sweep.BumpRequest,
1163-
tx *wire.MsgTx, fee btcutil.Amount) error {
1163+
tx *wire.MsgTx, fee btcutil.Amount,
1164+
outpointToTxIndex map[wire.OutPoint]int) error {
11641165

11651166
srvrLog.Tracef("NotifyBroadcast called, req=%v, tx=%v, fee=%v",
11661167
spew.Sdump(req), spew.Sdump(tx), fee)
@@ -1169,5 +1170,5 @@ func (s *Server) NotifyBroadcast(req *sweep.BumpRequest,
11691170
return err
11701171
}
11711172

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

tapchannel/aux_sweeper.go

Lines changed: 33 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
}
@@ -2147,7 +2152,8 @@ func sweepExclusionProofGen(sweepInternalKey keychain.KeyDescriptor,
21472152
// registerAndBroadcastSweep finalizes a sweep attempt by generating a
21482153
// transition proof for it, then registering the sweep with the porter.
21492154
func (a *AuxSweeper) registerAndBroadcastSweep(req *sweep.BumpRequest,
2150-
sweepTx *wire.MsgTx, fee btcutil.Amount) error {
2155+
sweepTx *wire.MsgTx, fee btcutil.Amount,
2156+
outpointToTxIndex map[wire.OutPoint]int) error {
21512157

21522158
// TODO(roasbeef): need to handle replacement -- will porter just
21532159
// upsert in place?
@@ -2202,6 +2208,24 @@ func (a *AuxSweeper) registerAndBroadcastSweep(req *sweep.BumpRequest,
22022208
for _, vIn := range vPkt.Inputs {
22032209
vIn.PrevID.OutPoint = sweepSet.btcInput.OutPoint()
22042210
}
2211+
// For pre-signed HTLC txns we'll need to make sure we update the output
2212+
// index in the vPkt. As the ordering is only determined at broadcast
2213+
// time.
2214+
if outpointToTxIndex != nil {
2215+
for _, sweepPkt := range vPkts.allVpktsWithInput() {
2216+
op := sweepPkt.btcInput.OutPoint()
2217+
finalOutputIndex, ok := outpointToTxIndex[op]
2218+
if !ok {
2219+
continue
2220+
}
2221+
2222+
for _, vPkt := range sweepPkt.vPkts {
2223+
for _, vOut := range vPkt.Outputs {
2224+
vOut.AnchorOutputIndex = uint32(
2225+
finalOutputIndex,
2226+
)
2227+
}
2228+
}
22052229
}
22062230
}
22072231

@@ -2303,6 +2327,7 @@ func (a *AuxSweeper) contractResolver() {
23032327
case req := <-a.broadcastReqs:
23042328
req.resp <- a.registerAndBroadcastSweep(
23052329
req.req, req.tx, req.fee,
2330+
req.outpointToTxIndex,
23062331
)
23072332

23082333
case <-a.quit:
@@ -2390,13 +2415,15 @@ func (a *AuxSweeper) ExtraBudgetForInputs(
23902415
// NotifyBroadcast is used to notify external callers of the broadcast of a
23912416
// sweep transaction, generated by the passed BumpRequest.
23922417
func (a *AuxSweeper) NotifyBroadcast(req *sweep.BumpRequest,
2393-
tx *wire.MsgTx, fee btcutil.Amount) error {
2418+
tx *wire.MsgTx, fee btcutil.Amount,
2419+
outpointToTxIndex map[wire.OutPoint]int) error {
23942420

23952421
auxReq := &broadcastReq{
2396-
req: req,
2397-
tx: tx,
2398-
fee: fee,
2399-
resp: make(chan error, 1),
2422+
req: req,
2423+
tx: tx,
2424+
fee: fee,
2425+
outpointToTxIndex: outpointToTxIndex,
2426+
resp: make(chan error, 1),
24002427
}
24012428

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

0 commit comments

Comments
 (0)