watchtower/multi: use ComputeJusticeTxOuts, test altruist+reward

This commit is contained in:
Conner Fromknecht
2019-02-01 17:26:54 -08:00
parent 9054a3ee4c
commit 09e71a1d49
3 changed files with 83 additions and 84 deletions

View File

@@ -8,6 +8,7 @@ import (
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcutil/txsort"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/watchtower/blob"
"github.com/lightningnetwork/lnd/watchtower/wtdb"
@@ -166,40 +167,41 @@ func (p *JusticeDescriptor) assembleJusticeTxn(txWeight int64,
})
}
// Using the total input amount and the transaction's weight, compute
// the sweep and reward amounts. This corresponds to the amount returned
// to the victim and the amount paid to the tower, respectively. To do
// so, the required transaction fee is subtracted from the total, and
// the remaining amount is divided according to the prenegotiated reward
// rate from the client's session info.
sweepAmt, rewardAmt, err := p.SessionInfo.ComputeSweepOutputs(
totalAmt, txWeight,
// Using the session's policy, compute the outputs that should be added
// to the justice transaction. In the case of an altruist sweep, there
// will be a single output paying back to the victim. Otherwise for a
// reward sweep, there will be two outputs, one of which pays back to
// the victim while the other gives a cut to the tower.
outputs, err := p.SessionInfo.Policy.ComputeJusticeTxOuts(
totalAmt, txWeight, p.JusticeKit.SweepAddress[:],
p.SessionInfo.RewardAddress,
)
if err != nil {
return nil, err
}
// TODO(conner): abort/don't add if outputs are dusty
// Attach the computed txouts to the justice transaction.
justiceTxn.TxOut = outputs
// Add the sweep and reward outputs to the justice transaction.
justiceTxn.AddTxOut(&wire.TxOut{
PkScript: p.JusticeKit.SweepAddress[:],
Value: int64(sweepAmt),
})
justiceTxn.AddTxOut(&wire.TxOut{
PkScript: p.SessionInfo.RewardAddress,
Value: int64(rewardAmt),
})
// TODO(conner): apply and handle BIP69 sort
// Apply a BIP69 sort to the resulting transaction.
txsort.InPlaceSort(justiceTxn)
btx := btcutil.NewTx(justiceTxn)
if err := blockchain.CheckTransactionSanity(btx); err != nil {
return nil, err
}
// Since the transaction inputs could have been reordered as a result of the
// BIP69 sort, create an index mapping each prevout to it's new index.
inputIndex := make(map[wire.OutPoint]int)
for i, txIn := range justiceTxn.TxIn {
inputIndex[txIn.PreviousOutPoint] = i
}
// Attach each of the provided witnesses to the transaction.
for i, input := range inputs {
for _, input := range inputs {
// Lookup the input's new post-sort position.
i := inputIndex[input.outPoint]
justiceTxn.TxIn[i].Witness = input.witness
// Validate the reconstructed witnesses to ensure they are valid
@@ -229,9 +231,6 @@ func (p *JusticeDescriptor) CreateJusticeTxn() (*wire.MsgTx, error) {
weightEstimate input.TxWeightEstimator
)
// Add our reward address to the weight estimate.
weightEstimate.AddP2WKHOutput()
// Add the sweep address's contribution, depending on whether it is a
// p2wkh or p2wsh output.
switch len(p.JusticeKit.SweepAddress) {
@@ -245,6 +244,12 @@ func (p *JusticeDescriptor) CreateJusticeTxn() (*wire.MsgTx, error) {
return nil, ErrUnknownSweepAddrType
}
// Add our reward address to the weight estimate if the policy's blob
// type specifies a reward output.
if p.SessionInfo.Policy.BlobType.Has(blob.FlagReward) {
weightEstimate.AddP2WKHOutput()
}
// Assemble the breached to-local output from the justice descriptor and
// add it to our weight estimate.
toLocalInput, err := p.commitToLocalInput()