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:
parent
bca5839929
commit
a6724c1088
@ -318,24 +318,22 @@ func convertToSecondLevelRevoke(bo *breachedOutput, breachInfo *retributionInfo,
|
||||
bo.outpoint)
|
||||
}
|
||||
|
||||
// waitForSpendEvent waits for any of the breached outputs to get spent, and
|
||||
// mutates the breachInfo to be able to sweep it. This method should be used
|
||||
// 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.
|
||||
// spend is used to wrap the index of the retributionInfo output that gets
|
||||
// spent together with the spend details.
|
||||
type spend struct {
|
||||
index int
|
||||
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
|
||||
// signal. We make it buffered in case multiple spend events come in at
|
||||
// the same time.
|
||||
@ -378,7 +376,7 @@ func (b *breachArbiter) waitForSpendEvent(breachInfo *retributionInfo,
|
||||
// to avoid entering an infinite loop.
|
||||
select {
|
||||
case <-b.quit:
|
||||
return errBrarShuttingDown
|
||||
return nil, errBrarShuttingDown
|
||||
default:
|
||||
continue
|
||||
}
|
||||
@ -438,11 +436,31 @@ func (b *breachArbiter) waitForSpendEvent(breachInfo *retributionInfo,
|
||||
// channel before ranging over its content.
|
||||
close(allSpends)
|
||||
|
||||
doneOutputs := make(map[int]struct{})
|
||||
// Gather all detected spends and return them.
|
||||
var spends []spend
|
||||
for s := range allSpends {
|
||||
breachedOutput := &inputs[s.index]
|
||||
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 {
|
||||
case input.HtlcAcceptedRevoke:
|
||||
fallthrough
|
||||
@ -451,8 +469,7 @@ func (b *breachArbiter) waitForSpendEvent(breachInfo *retributionInfo,
|
||||
"%s(%v) for ChannelPoint(%v) "+
|
||||
"transitions to second-level output",
|
||||
breachedOutput.witnessType,
|
||||
breachedOutput.outpoint,
|
||||
breachInfo.chanPoint)
|
||||
breachedOutput.outpoint, breachInfo.chanPoint)
|
||||
|
||||
// In this case we'll morph our initial revoke
|
||||
// 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
|
||||
// another attempt at publication.
|
||||
breachInfo.breachedOutputs = inputs[:nextIndex]
|
||||
|
||||
case <-b.quit:
|
||||
return errBrarShuttingDown
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// exactRetribution is a goroutine which is executed once a contract breach has
|
||||
@ -587,7 +598,9 @@ justiceTxBroadcast:
|
||||
"attempting to craft new justice tx.")
|
||||
finalTx = nil
|
||||
|
||||
err := b.waitForSpendEvent(breachInfo, spendNtfns)
|
||||
spends, err := b.waitForSpendEvent(
|
||||
breachInfo, spendNtfns,
|
||||
)
|
||||
if err != nil {
|
||||
if err != errBrarShuttingDown {
|
||||
brarLog.Errorf("error waiting for "+
|
||||
@ -596,6 +609,7 @@ justiceTxBroadcast:
|
||||
return
|
||||
}
|
||||
|
||||
updateBreachInfo(breachInfo, spends)
|
||||
if len(breachInfo.breachedOutputs) == 0 {
|
||||
brarLog.Debugf("No more outputs to sweep for "+
|
||||
"breach, marking ChannelPoint(%v) "+
|
||||
|
Loading…
Reference in New Issue
Block a user