-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sweep: prepare for sweeper #1978
Changes from all commits
9fcc7ee
4dab405
7d69df7
c1d845a
6977d59
95bf858
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,10 @@ import ( | |
"crypto/sha256" | ||
"encoding/binary" | ||
"fmt" | ||
"github.com/lightningnetwork/lnd/sweep" | ||
"io" | ||
"io/ioutil" | ||
|
||
"github.com/btcsuite/btcd/txscript" | ||
"github.com/btcsuite/btcd/wire" | ||
"github.com/davecgh/go-spew/spew" | ||
"github.com/lightningnetwork/lnd/chainntnfs" | ||
|
@@ -446,59 +446,19 @@ func (h *htlcSuccessResolver) Resolve() (ContractResolver, error) { | |
"incoming+remote htlc confirmed", h, | ||
h.payHash[:]) | ||
|
||
// In this case, we can sweep it directly from the | ||
// commitment output. We'll first grab a fresh address | ||
// from the wallet to sweep the output. | ||
addr, err := h.NewSweepAddr() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// With our address obtained, we'll query for an | ||
// estimate to be confirmed at ease. | ||
// | ||
// TODO(roasbeef): signal up if fee would be too large | ||
// to sweep singly, need to batch | ||
feePerKw, err := h.FeeEstimator.EstimateFeePerKW(6) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
log.Debugf("%T(%x): using %v sat/kw to sweep htlc"+ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar comment here re losing the debug logging. |
||
"incoming+remote htlc confirmed", h, | ||
h.payHash[:], int64(feePerKw)) | ||
|
||
// Using a weight estimator, we'll compute the total | ||
// fee required, and from that the value we'll end up | ||
// with. | ||
totalWeight := (&lnwallet.TxWeightEstimator{}). | ||
AddWitnessInput(lnwallet.OfferedHtlcSuccessWitnessSize). | ||
AddP2WKHOutput().Weight() | ||
totalFees := feePerKw.FeeForWeight(int64(totalWeight)) | ||
sweepAmt := h.htlcResolution.SweepSignDesc.Output.Value - | ||
int64(totalFees) | ||
|
||
// With the fee computation finished, we'll now | ||
// construct the sweep transaction. | ||
htlcPoint := h.htlcResolution.ClaimOutpoint | ||
h.sweepTx = wire.NewMsgTx(2) | ||
h.sweepTx.AddTxIn(&wire.TxIn{ | ||
PreviousOutPoint: htlcPoint, | ||
}) | ||
h.sweepTx.AddTxOut(&wire.TxOut{ | ||
PkScript: addr, | ||
Value: sweepAmt, | ||
}) | ||
|
||
// With the transaction fully assembled, we can now | ||
// generate a valid witness for the transaction. | ||
h.htlcResolution.SweepSignDesc.SigHashes = txscript.NewTxSigHashes( | ||
h.sweepTx, | ||
) | ||
h.sweepTx.TxIn[0].Witness, err = lnwallet.SenderHtlcSpendRedeem( | ||
h.Signer, &h.htlcResolution.SweepSignDesc, h.sweepTx, | ||
input := sweep.MakeHtlcSucceedInput( | ||
&h.htlcResolution.ClaimOutpoint, | ||
&h.htlcResolution.SweepSignDesc, | ||
h.htlcResolution.Preimage[:], | ||
) | ||
|
||
var err error | ||
|
||
// TODO: Set tx lock time to current block height instead of | ||
// zero. Will be taken care of once sweeper implementation is | ||
// complete. | ||
h.sweepTx, err = h.Sweeper.CreateSweepTx( | ||
[]sweep.Input{&input}, 0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -1256,46 +1216,16 @@ func (c *commitSweepResolver) Resolve() (ContractResolver, error) { | |
// If the sweep transaction isn't already generated, and the remote | ||
// party broadcast the commitment transaction then we'll create it now. | ||
case c.sweepTx == nil && !isLocalCommitTx: | ||
// Now that the commitment transaction has confirmed, we'll | ||
// craft a transaction to sweep this output into the wallet. | ||
signDesc := c.commitResolution.SelfOutputSignDesc | ||
|
||
// First, we'll estimate the total weight so we can compute | ||
// fees properly. We'll use a lax estimate, as this output is | ||
// in no immediate danger. | ||
feePerKw, err := c.FeeEstimator.EstimateFeePerKW(6) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
log.Debugf("%T(%v): using %v sat/kw for sweep tx", c, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With this change, we've lost the debug logging here. Also just realized that we don't have a way currently to signal to the sweeper what conf timing, or fee rate that we'd like when sweeping. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved logging to sweeper. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added TODO for configurable fee rate. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before we merge we should be able to specify the fee rate. In distinct areas in the codebase, we use 3 or 6 depending on how quickly we want the transaction to enter the chain. Some cases (CLTV timeouts) are more time sensitive than others. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
c.chanPoint, int64(feePerKw)) | ||
|
||
totalWeight := (&lnwallet.TxWeightEstimator{}). | ||
AddP2WKHInput(). | ||
AddP2WKHOutput().Weight() | ||
totalFees := feePerKw.FeeForWeight(int64(totalWeight)) | ||
sweepAmt := signDesc.Output.Value - int64(totalFees) | ||
|
||
c.sweepTx = wire.NewMsgTx(2) | ||
c.sweepTx.AddTxIn(&wire.TxIn{ | ||
PreviousOutPoint: c.commitResolution.SelfOutPoint, | ||
}) | ||
sweepAddr, err := c.NewSweepAddr() | ||
if err != nil { | ||
return nil, err | ||
} | ||
c.sweepTx.AddTxOut(&wire.TxOut{ | ||
PkScript: sweepAddr, | ||
Value: sweepAmt, | ||
}) | ||
|
||
// With the transaction fully assembled, we can now generate a | ||
// valid witness for the transaction. | ||
signDesc.SigHashes = txscript.NewTxSigHashes(c.sweepTx) | ||
c.sweepTx.TxIn[0].Witness, err = lnwallet.CommitSpendNoDelay( | ||
c.Signer, &signDesc, c.sweepTx, | ||
) | ||
input := sweep.MakeBaseInput( | ||
&c.commitResolution.SelfOutPoint, | ||
lnwallet.CommitmentNoDelay, | ||
&c.commitResolution.SelfOutputSignDesc) | ||
|
||
// TODO: Set tx lock time to current block height instead of | ||
// zero. Will be taken care of once sweeper implementation is | ||
// complete. | ||
c.sweepTx, err = c.Sweeper.CreateSweepTx( | ||
[]sweep.Input{&input}, 0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the rationale behind this change? Seems better from the PoV of unit tests, that we can feed in this interface into various methods in his file. After all, we may eventually extract this into the
contractcourt
package.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the interface because it was not used anywhere. But indeed, I can also see the
breacharbiter
use an (its own) instance of the sweeper. Converted toInput
types.