Skip to content

Commit

Permalink
breacharbiter: simplify deps in BreachConfig fields + sweep incoming …
Browse files Browse the repository at this point in the history
…htlcs
  • Loading branch information
cfromknecht committed Sep 20, 2017
1 parent f93ad11 commit 6087c52
Showing 1 changed file with 39 additions and 22 deletions.
61 changes: 39 additions & 22 deletions breacharbiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ type BreachConfig struct {
// it should respond to channel closure.
DB *channeldb.DB

// Wallet facilitates sweep transaction publication and further
// facilitates the generation of receiver scripts when sweeping funds.
Wallet *lnwallet.LightningWallet
// PublishTransaction facilitates the process of broadcasting a
// transaction to the network.
PublishTransaction func(*wire.MsgTx) error

// Notifier provides a publish/subscribe interface for event driven
// notifications regarding the confirmation of txids.
Expand All @@ -61,18 +61,19 @@ type BreachConfig struct {
// transactions.
Estimator lnwallet.FeeEstimator

// HtlcSwitch gives the breach arbiter access to shutdown any channel
// links for which it detects a breach, ensuring no further activity
// will continue across the link.
HtlcSwitch *htlcswitch.Switch
// CloseLink allows the breach arbiter to shutdown any channel links for
// which it detects a breach, ensuring now further activity will
// continue across the link. The method accepts link's channel point and a
// close type to be included in the channel close summary.
CloseLink func(*wire.OutPoint, htlcswitch.ChannelCloseType)

// Store is a persistent resource that maintains information regarding
// breached channels. This is used in conjunction with DB to recover
// from crashes, restarts, or other failures.
Store RetributionStore

// GenSweepScript generates the receiving scripts for swept outputs.
GenSweepScript func(lnwallet.WalletController) ([]byte, error)
GenSweepScript func() ([]byte, error)
}

// breachArbiter is a special subsystem which is responsible for watching and
Expand Down Expand Up @@ -232,10 +233,8 @@ func (b *breachArbiter) Start() error {
// notify the HTLC switch that this link should be
// closed, and that all activity on the link should
// cease.
b.cfg.HtlcSwitch.CloseLink(
&chanState.FundingOutpoint,
htlcswitch.CloseBreach,
)
b.cfg.CloseLink(&chanState.FundingOutpoint,
htlcswitch.CloseBreach)

// Ensure channeldb is consistent with the persisted
// breach.
Expand Down Expand Up @@ -558,7 +557,7 @@ func (b *breachArbiter) exactRetribution(

// Finally, broadcast the transaction, finalizing the channels'
// retribution against the cheating counterparty.
if err := b.cfg.Wallet.PublishTransaction(justiceTx); err != nil {
if err := b.cfg.PublishTransaction(justiceTx); err != nil {
brarLog.Errorf("unable to broadcast "+
"justice tx: %v", err)
return
Expand Down Expand Up @@ -692,9 +691,7 @@ func (b *breachArbiter) breachObserver(contract *lnwallet.LightningChannel,
"outputs with: %v",
spew.Sdump(sweepTx))

err = b.cfg.Wallet.PublishTransaction(
sweepTx,
)
err = b.cfg.PublishTransaction(sweepTx)
if err != nil {
brarLog.Errorf("unable to "+
"broadcast tx: %v", err)
Expand Down Expand Up @@ -726,7 +723,7 @@ func (b *breachArbiter) breachObserver(contract *lnwallet.LightningChannel,
// breached in order to ensure any incoming or outgoing
// multi-hop HTLCs aren't sent over this link, nor any other
// links associated with this peer.
b.cfg.HtlcSwitch.CloseLink(chanPoint, htlcswitch.CloseBreach)
b.cfg.CloseLink(chanPoint, htlcswitch.CloseBreach)

// TODO(roasbeef): need to handle case of remote broadcast
// mid-local initiated state-transition, possible
Expand Down Expand Up @@ -951,9 +948,19 @@ func newRetributionInfo(chanPoint *wire.OutPoint,
// it must sign and broadcast the justice transaction.
htlcOutputs := make([]*breachedOutput, nHtlcs)
for i, breachedHtlc := range breachInfo.HtlcRetributions {
// Using the breachedHtlc's incoming flag, determine the
// appropriate witness type that needs to be generated in order
// to sweep the HTLC output.
var htlcWitnessType lnwallet.WitnessType
if breachedHtlc.IsIncoming {
htlcWitnessType = lnwallet.HtlcAcceptedRevoke
} else {
htlcWitnessType = lnwallet.HtlcOfferedRevoke
}

htlcOutputs[i] = newBreachedOutput(
&breachedHtlc.OutPoint,
lnwallet.HtlcReceiverRevoke,
htlcWitnessType,
&breachedHtlc.SignDesc,
)
}
Expand Down Expand Up @@ -1002,9 +1009,19 @@ func (b *breachArbiter) createJusticeTx(
txWeight += 4*lnwallet.InputSize + lnwallet.ToLocalPenaltyWitnessSize
// Add to_remote p2wpkh witness and tx input.
txWeight += 4*lnwallet.InputSize + lnwallet.P2WKHWitnessSize
// Add revoked offered-htlc witnesses and tx inputs.
txWeight += uint64(nHtlcs) *
(4*lnwallet.InputSize + lnwallet.OfferedHtlcWitnessSize)

// Compute the appropriate weight contributed by each revoked accepted
// or offered HTLC witnesses and tx inputs.
for _, htlcOutput := range r.htlcOutputs {
switch htlcOutput.witnessType {
case lnwallet.HtlcOfferedRevoke:
txWeight += 4*lnwallet.InputSize +
lnwallet.OfferedHtlcPenaltyWitnessSize
case lnwallet.HtlcAcceptedRevoke:
txWeight += 4*lnwallet.InputSize +
lnwallet.AcceptedHtlcPenaltyWitnessSize
}
}

return b.sweepSpendableOutputsTxn(txWeight, breachedOutputs...)
}
Expand Down Expand Up @@ -1046,7 +1063,7 @@ func (b *breachArbiter) sweepSpendableOutputsTxn(txWeight uint64,
// sweep the funds to.
// TODO(roasbeef): possibly create many outputs to minimize change in
// the future?
pkScript, err := b.cfg.GenSweepScript(b.cfg.Wallet)
pkScript, err := b.cfg.GenSweepScript()
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 6087c52

Please sign in to comment.