breacharbiter: use BOLT 5 constants

This commit is contained in:
Conner Fromknecht 2017-08-29 19:07:52 -07:00
parent 7f93aec31c
commit f0ac991979
No known key found for this signature in database
GPG Key ID: 39DE78FBE6ACB0EF

@ -14,6 +14,7 @@ import (
"github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/htlcswitch" "github.com/lightningnetwork/lnd/htlcswitch"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/roasbeef/btcd/blockchain"
"github.com/roasbeef/btcd/btcec" "github.com/roasbeef/btcd/btcec"
"github.com/roasbeef/btcd/chaincfg/chainhash" "github.com/roasbeef/btcd/chaincfg/chainhash"
"github.com/roasbeef/btcd/txscript" "github.com/roasbeef/btcd/txscript"
@ -168,11 +169,11 @@ func (b *breachArbiter) Start() error {
// channels can be discarded, as their fate will be placed in the hands // channels can be discarded, as their fate will be placed in the hands
// of an exactRetribution task spawned later. // of an exactRetribution task spawned later.
// //
// NOTE Spawning of the exactRetribution task is intentionally postponed // NOTE: Spawning of the exactRetribution task is intentionally
// until after this step in order to ensure that the all breached // postponed until after this step in order to ensure that the all
// channels are reflected as closed in channeldb and consistent with // breached channels are reflected as closed in channeldb and consistent
// what is checkpointed by the breach arbiter. Instead of treating the // with what is checkpointed by the breach arbiter. Instead of treating
// breached-and-closed and breached-but-still-active channels as // the breached-and-closed and breached-but-still-active channels as
// separate sets of channels, we first ensure that all // separate sets of channels, we first ensure that all
// breached-but-still-active channels are promoted to // breached-but-still-active channels are promoted to
// breached-and-closed during restart, allowing us to treat them as a // breached-and-closed during restart, allowing us to treat them as a
@ -799,8 +800,8 @@ type breachedOutput struct {
witnessFunc lnwallet.WitnessGenerator witnessFunc lnwallet.WitnessGenerator
} }
// newBreachedOutput assembles new breachedOutput that can be used by the breach // newBreachedOutput assembles a new breachedOutput that can be used by the
// arbiter to construct a justice or sweep transaction. // breach arbiter to construct a justice or sweep transaction.
func newBreachedOutput(outpoint *wire.OutPoint, func newBreachedOutput(outpoint *wire.OutPoint,
witnessType lnwallet.WitnessType, witnessType lnwallet.WitnessType,
signDescriptor *lnwallet.SignDescriptor) *breachedOutput { signDescriptor *lnwallet.SignDescriptor) *breachedOutput {
@ -864,7 +865,7 @@ type retributionInfo struct {
commitHash chainhash.Hash commitHash chainhash.Hash
chanPoint wire.OutPoint chanPoint wire.OutPoint
// TODO(conner) remove the following group of fields after decoupling // TODO(conner): remove the following group of fields after decoupling
// the breach arbiter from the wallet. // the breach arbiter from the wallet.
// Fields copied from channel snapshot when a breach is detected. This // Fields copied from channel snapshot when a breach is detected. This
@ -931,7 +932,7 @@ func newRetributionInfo(chanPoint *wire.OutPoint,
) )
} }
// TODO(conner) remove dependency on channel snapshot after decoupling // TODO(conner): remove dependency on channel snapshot after decoupling
// channel closure from the breach arbiter. // channel closure from the breach arbiter.
return &retributionInfo{ return &retributionInfo{
@ -967,17 +968,16 @@ func (b *breachArbiter) createJusticeTx(
} }
var txWeight uint64 var txWeight uint64
// Begin with a base txn weight of 4 * tx_non_wit_data + // Begin with a base txn weight, e.g. version, nLockTime, etc.
// witness_header_size. txWeight += 4*lnwallet.BaseSweepTxSize + lnwallet.WitnessHeaderSize
txWeight += 4*53 + 2
// Add to_local revoke script and tx input. // Add to_local revoke script and tx input.
txWeight += 154 + 4*41 txWeight += 4*lnwallet.InputSize + lnwallet.ToLocalPenaltyWitnessSize
// Add to_remote p2wpkh witness and tx input. // Add to_remote p2wpkh witness and tx input.
txWeight += 108 + 4*41 txWeight += 4*lnwallet.InputSize + lnwallet.P2WKHWitnessSize
for range r.htlcOutputs { // Add revoked offered-htlc witnesses and tx inputs.
// Add revoke offered htlc witness and tx input. txWeight += uint64(len(r.htlcOutputs)) *
txWeight += 243 + 4*41 (4*lnwallet.InputSize + lnwallet.OfferedHtlcWitnessSize)
}
return b.sweepSpendableOutputsTxn(txWeight, breachedOutputs...) return b.sweepSpendableOutputsTxn(txWeight, breachedOutputs...)
} }
@ -1000,19 +1000,17 @@ func (b *breachArbiter) craftCommitSweepTx(
) )
var txWeight uint64 var txWeight uint64
// Begin with a base txn weight of 4 * tx_non_wit_data + // Begin with a base txn weight, e.g. version, nLockTime, etc.
// witness_header_size. txWeight += 4*lnwallet.BaseSweepTxSize + lnwallet.WitnessHeaderSize
txWeight += 4*53 + 2 // Add to_local p2wpkh witness and tx input.
// Add receiver script witness and tx input txWeight += 4*lnwallet.InputSize + lnwallet.P2WKHWitnessSize
txWeight += 325 + 4*41
return b.sweepSpendableOutputsTxn(txWeight, selfOutput) return b.sweepSpendableOutputsTxn(txWeight, selfOutput)
} }
// sweepSpendableOutputsTxn creates a signed transaction from a sequence of // sweepSpendableOutputsTxn creates a signed transaction from a sequence of
// spendable outputs by sweeping the funds into a single p2wkh output. // spendable outputs by sweeping the funds into a single p2wkh output.
func (b *breachArbiter) sweepSpendableOutputsTxn( func (b *breachArbiter) sweepSpendableOutputsTxn(txWeight uint64,
txWeight uint64,
inputs ...SpendableOutput) (*wire.MsgTx, error) { inputs ...SpendableOutput) (*wire.MsgTx, error) {
// First, we obtain a new public key script from the wallet which we'll // First, we obtain a new public key script from the wallet which we'll
@ -1053,6 +1051,13 @@ func (b *breachArbiter) sweepSpendableOutputsTxn(
}) })
} }
// Before signing the transaction, check to ensure that it meets some
// basic validity requirements.
btx := btcutil.NewTx(txn)
if err := blockchain.CheckTransactionSanity(btx); err != nil {
return nil, err
}
// Create a sighash cache to improve the performance of hashing and // Create a sighash cache to improve the performance of hashing and
// signing SigHashAll inputs. // signing SigHashAll inputs.
hashCache := txscript.NewTxSigHashes(txn) hashCache := txscript.NewTxSigHashes(txn)