From 6b3691a86e7d712a6ab9a52976517096e9b7691b Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Fri, 1 Feb 2019 17:19:27 -0800 Subject: [PATCH] watchtower/wtpolicy: add ComputAltruistOutput calc --- watchtower/wtpolicy/policy.go | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/watchtower/wtpolicy/policy.go b/watchtower/wtpolicy/policy.go index 3336b07c5..b59e31952 100644 --- a/watchtower/wtpolicy/policy.go +++ b/watchtower/wtpolicy/policy.go @@ -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 +}