watchtower/wtpolicy: add ComputAltruistOutput calc

This commit is contained in:
Conner Fromknecht 2019-02-01 17:19:27 -08:00
parent b7387a5972
commit 6b3691a86e
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7

@ -1,8 +1,10 @@
package wtpolicy
import (
"errors"
"fmt"
"github.com/btcsuite/btcutil"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/watchtower/blob"
)
@ -22,6 +24,17 @@ const (
DefaultSweepFeeRate = 3000
)
var (
// ErrFeeExceedsInputs signals that the total input value of breaching
// commitment txn is insufficient to cover the fees required to sweep
// it.
ErrFeeExceedsInputs = errors.New("sweep fee exceeds input value")
// ErrCreatesDust signals that the session's policy would create a dust
// output for the victim.
ErrCreatesDust = errors.New("justice transaction creates dust at fee rate")
)
// DefaultPolicy returns a Policy containing the default parameters that can be
// used by clients or servers.
func DefaultPolicy() Policy {
@ -71,3 +84,28 @@ func (p Policy) String() string {
"sweep-fee-rate=%d)", p.BlobType, p.MaxUpdates, p.RewardRate,
p.SweepFeeRate)
}
// ComputeAltruistOutput computes the lone output value of a justice transaction
// that pays no reward to the tower. The value is computed using the weight of
// of the justice transaction and subtracting an amount that satisfies the
// policy's fee rate.
func (p *Policy) ComputeAltruistOutput(totalAmt btcutil.Amount,
txWeight int64) (btcutil.Amount, error) {
txFee := p.SweepFeeRate.FeeForWeight(txWeight)
if txFee > totalAmt {
return 0, ErrFeeExceedsInputs
}
sweepAmt := totalAmt - txFee
// TODO(conner): replace w/ configurable dust limit
dustLimit := lnwallet.DefaultDustLimit()
// Check that the created outputs won't be dusty.
if sweepAmt <= dustLimit {
return 0, ErrCreatesDust
}
return sweepAmt, nil
}