breacharbiter: split waitForSpendEvent

We split the method waitForSpendEvent into two, such that we can reuse
it in case the commitment is spent by various transactions.
This commit is contained in:
Johan T. Halseth 2021-02-15 13:51:07 +01:00
parent bca5839929
commit a6724c1088
No known key found for this signature in database
GPG key ID: 15BAADA29DA20D26

View file

@ -318,24 +318,22 @@ func convertToSecondLevelRevoke(bo *breachedOutput, breachInfo *retributionInfo,
bo.outpoint) bo.outpoint)
} }
// waitForSpendEvent waits for any of the breached outputs to get spent, and // spend is used to wrap the index of the retributionInfo output that gets
// mutates the breachInfo to be able to sweep it. This method should be used // spent together with the spend details.
// when we fail to publish the justice tx because of a double spend, indicating
// that the counter party has taken one of the breached outputs to the second
// level. The spendNtfns map is a cache used to store registered spend
// subscriptions, in case we must call this method multiple times.
func (b *breachArbiter) waitForSpendEvent(breachInfo *retributionInfo,
spendNtfns map[wire.OutPoint]*chainntnfs.SpendEvent) error {
inputs := breachInfo.breachedOutputs
// spend is used to wrap the index of the output that gets spent
// together with the spend details.
type spend struct { type spend struct {
index int index int
detail *chainntnfs.SpendDetail detail *chainntnfs.SpendDetail
} }
// waitForSpendEvent waits for any of the breached outputs to get spent, and
// returns the spend details for those outputs. The spendNtfns map is a cache
// used to store registered spend subscriptions, in case we must call this
// method multiple times.
func (b *breachArbiter) waitForSpendEvent(breachInfo *retributionInfo,
spendNtfns map[wire.OutPoint]*chainntnfs.SpendEvent) ([]spend, error) {
inputs := breachInfo.breachedOutputs
// We create a channel the first goroutine that gets a spend event can // We create a channel the first goroutine that gets a spend event can
// signal. We make it buffered in case multiple spend events come in at // signal. We make it buffered in case multiple spend events come in at
// the same time. // the same time.
@ -378,7 +376,7 @@ func (b *breachArbiter) waitForSpendEvent(breachInfo *retributionInfo,
// to avoid entering an infinite loop. // to avoid entering an infinite loop.
select { select {
case <-b.quit: case <-b.quit:
return errBrarShuttingDown return nil, errBrarShuttingDown
default: default:
continue continue
} }
@ -438,11 +436,31 @@ func (b *breachArbiter) waitForSpendEvent(breachInfo *retributionInfo,
// channel before ranging over its content. // channel before ranging over its content.
close(allSpends) close(allSpends)
doneOutputs := make(map[int]struct{}) // Gather all detected spends and return them.
var spends []spend
for s := range allSpends { for s := range allSpends {
breachedOutput := &inputs[s.index] breachedOutput := &inputs[s.index]
delete(spendNtfns, breachedOutput.outpoint) delete(spendNtfns, breachedOutput.outpoint)
spends = append(spends, s)
}
return spends, nil
case <-b.quit:
return nil, errBrarShuttingDown
}
}
// updateBreachInfo mutates the passed breachInfo by removing or converting any
// outputs among the spends.
func updateBreachInfo(breachInfo *retributionInfo, spends []spend) {
inputs := breachInfo.breachedOutputs
doneOutputs := make(map[int]struct{})
for _, s := range spends {
breachedOutput := &inputs[s.index]
switch breachedOutput.witnessType { switch breachedOutput.witnessType {
case input.HtlcAcceptedRevoke: case input.HtlcAcceptedRevoke:
fallthrough fallthrough
@ -451,8 +469,7 @@ func (b *breachArbiter) waitForSpendEvent(breachInfo *retributionInfo,
"%s(%v) for ChannelPoint(%v) "+ "%s(%v) for ChannelPoint(%v) "+
"transitions to second-level output", "transitions to second-level output",
breachedOutput.witnessType, breachedOutput.witnessType,
breachedOutput.outpoint, breachedOutput.outpoint, breachInfo.chanPoint)
breachInfo.chanPoint)
// In this case we'll morph our initial revoke // In this case we'll morph our initial revoke
// spend to instead point to the second level // spend to instead point to the second level
@ -488,12 +505,6 @@ func (b *breachArbiter) waitForSpendEvent(breachInfo *retributionInfo,
// Update our remaining set of outputs before continuing with // Update our remaining set of outputs before continuing with
// another attempt at publication. // another attempt at publication.
breachInfo.breachedOutputs = inputs[:nextIndex] breachInfo.breachedOutputs = inputs[:nextIndex]
case <-b.quit:
return errBrarShuttingDown
}
return nil
} }
// exactRetribution is a goroutine which is executed once a contract breach has // exactRetribution is a goroutine which is executed once a contract breach has
@ -587,7 +598,9 @@ justiceTxBroadcast:
"attempting to craft new justice tx.") "attempting to craft new justice tx.")
finalTx = nil finalTx = nil
err := b.waitForSpendEvent(breachInfo, spendNtfns) spends, err := b.waitForSpendEvent(
breachInfo, spendNtfns,
)
if err != nil { if err != nil {
if err != errBrarShuttingDown { if err != errBrarShuttingDown {
brarLog.Errorf("error waiting for "+ brarLog.Errorf("error waiting for "+
@ -596,6 +609,7 @@ justiceTxBroadcast:
return return
} }
updateBreachInfo(breachInfo, spends)
if len(breachInfo.breachedOutputs) == 0 { if len(breachInfo.breachedOutputs) == 0 {
brarLog.Debugf("No more outputs to sweep for "+ brarLog.Debugf("No more outputs to sweep for "+
"breach, marking ChannelPoint(%v) "+ "breach, marking ChannelPoint(%v) "+