Skip to content

Commit 13e7901

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 a4d8485 commit 13e7901

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-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: 41 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
}
@@ -490,6 +495,11 @@ func (s sweepVpkts) allPkts() []*tappsbt.VPacket {
490495
return append(s.firstLevelPkts(), s.secondLevelPkts()...)
491496
}
492497

498+
// allVpktsWithInput returns a slice of all vPktsWithInput.
499+
func (s sweepVpkts) allVpktsWithInput() []vPktsWithInput {
500+
return append(s.firstLevel, s.secondLevel...)
501+
}
502+
493503
// createAndSignSweepVpackets creates vPackets that sweep the funds from the
494504
// channel to the wallet, and then signs them as well.
495505
func (a *AuxSweeper) createAndSignSweepVpackets(
@@ -2149,7 +2159,8 @@ func sweepExclusionProofGen(sweepInternalKey keychain.KeyDescriptor,
21492159
// registerAndBroadcastSweep finalizes a sweep attempt by generating a
21502160
// transition proof for it, then registering the sweep with the porter.
21512161
func (a *AuxSweeper) registerAndBroadcastSweep(req *sweep.BumpRequest,
2152-
sweepTx *wire.MsgTx, fee btcutil.Amount) error {
2162+
sweepTx *wire.MsgTx, fee btcutil.Amount,
2163+
outpointToTxIndex map[wire.OutPoint]int) error {
21532164

21542165
// TODO(roasbeef): need to handle replacement -- will porter just
21552166
// upsert in place?
@@ -2207,6 +2218,27 @@ func (a *AuxSweeper) registerAndBroadcastSweep(req *sweep.BumpRequest,
22072218
}
22082219
}
22092220

2221+
// For pre-signed HTLC txns we'll need to make sure we update the output
2222+
// index in the vPkt. As the ordering is only determined at broadcast
2223+
// time.
2224+
if outpointToTxIndex != nil {
2225+
for _, sweepPkt := range vPkts.allVpktsWithInput() {
2226+
op := sweepPkt.btcInput.OutPoint()
2227+
finalOutputIndex, ok := outpointToTxIndex[op]
2228+
if !ok {
2229+
continue
2230+
}
2231+
2232+
for _, vPkt := range sweepPkt.vPkts {
2233+
for _, vOut := range vPkt.Outputs {
2234+
vOut.AnchorOutputIndex = uint32(
2235+
finalOutputIndex,
2236+
)
2237+
}
2238+
}
2239+
}
2240+
}
2241+
22102242
// If we have second level vPkts, then we'll need to sign them here, as
22112243
// now we know the input we're spending which was set above.
22122244
for _, sweepSet := range vPkts.secondLevel {
@@ -2305,6 +2337,7 @@ func (a *AuxSweeper) contractResolver() {
23052337
case req := <-a.broadcastReqs:
23062338
req.resp <- a.registerAndBroadcastSweep(
23072339
req.req, req.tx, req.fee,
2340+
req.outpointToTxIndex,
23082341
)
23092342

23102343
case <-a.quit:
@@ -2392,13 +2425,15 @@ func (a *AuxSweeper) ExtraBudgetForInputs(
23922425
// NotifyBroadcast is used to notify external callers of the broadcast of a
23932426
// sweep transaction, generated by the passed BumpRequest.
23942427
func (a *AuxSweeper) NotifyBroadcast(req *sweep.BumpRequest,
2395-
tx *wire.MsgTx, fee btcutil.Amount) error {
2428+
tx *wire.MsgTx, fee btcutil.Amount,
2429+
outpointToTxIndex map[wire.OutPoint]int) error {
23962430

23972431
auxReq := &broadcastReq{
2398-
req: req,
2399-
tx: tx,
2400-
fee: fee,
2401-
resp: make(chan error, 1),
2432+
req: req,
2433+
tx: tx,
2434+
fee: fee,
2435+
outpointToTxIndex: outpointToTxIndex,
2436+
resp: make(chan error, 1),
24022437
}
24032438

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

0 commit comments

Comments
 (0)