lnwallet: Use TxWeightEstimator in breacharbiter.

This commit is contained in:
Jim Posen 2017-09-25 20:13:53 -07:00 committed by Olaoluwa Osuntokun
parent ced530f98e
commit 10a336db46
2 changed files with 20 additions and 33 deletions

@ -1027,7 +1027,7 @@ func (b *breachArbiter) createJusticeTx(
// transaction. // transaction.
var ( var (
spendableOutputs []SpendableOutput spendableOutputs []SpendableOutput
txWeight uint64 weightEstimate lnwallet.TxWeightEstimator
) )
// Allocate enough space to potentially hold each of the breached // Allocate enough space to potentially hold each of the breached
@ -1036,10 +1036,8 @@ func (b *breachArbiter) createJusticeTx(
// The justice transaction we construct will be a segwit transaction // The justice transaction we construct will be a segwit transaction
// that pays to a p2wkh output. Components such as the version, // that pays to a p2wkh output. Components such as the version,
// nLockTime, and output are included in the BaseSweepTxSize, while the // nLockTime, and output are already included in the TxWeightEstimator.
// WitnessHeaderSize accounts for the two bytes that signal this as a weightEstimate.AddP2WKHOutput()
// segwit transaction.
txWeight += 4*lnwallet.BaseSweepTxSize + lnwallet.WitnessHeaderSize
// Next, we iterate over the breached outputs contained in the // Next, we iterate over the breached outputs contained in the
// retribution info. For each, we switch over the witness type such // retribution info. For each, we switch over the witness type such
@ -1052,7 +1050,7 @@ func (b *breachArbiter) createJusticeTx(
// First, select the appropriate estimated witness weight for // First, select the appropriate estimated witness weight for
// the give witness type of this breached output. If the witness // the give witness type of this breached output. If the witness
// type is unrecognized, we will omit it from the transaction. // type is unrecognized, we will omit it from the transaction.
var witnessWeight uint64 var witnessWeight int
switch input.WitnessType() { switch input.WitnessType() {
case lnwallet.CommitmentNoDelay: case lnwallet.CommitmentNoDelay:
witnessWeight = lnwallet.ToLocalPenaltyWitnessSize witnessWeight = lnwallet.ToLocalPenaltyWitnessSize
@ -1072,20 +1070,13 @@ func (b *breachArbiter) createJusticeTx(
input.WitnessType()) input.WitnessType())
continue continue
} }
weightEstimate.AddWitnessInput(witnessWeight)
// Next, each of the outputs in the retribution info will be
// used as inputs to the justice transaction. An input is
// considered non-witness data, so it is scaled accordingly.
txWeight += 4 * lnwallet.InputSize
// Additionally, we contribute the weight of the witness
// directly to the total transaction weight.
txWeight += witnessWeight
// Finally, append this input to our list of spendable outputs. // Finally, append this input to our list of spendable outputs.
spendableOutputs = append(spendableOutputs, input) spendableOutputs = append(spendableOutputs, input)
} }
txWeight := uint64(weightEstimate.Weight())
return b.sweepSpendableOutputsTxn(txWeight, spendableOutputs...) return b.sweepSpendableOutputsTxn(txWeight, spendableOutputs...)
} }
@ -1108,12 +1099,13 @@ func (b *breachArbiter) craftCommitSweepTx(
// Compute the transaction weight of the commit sweep transaction, which // Compute the transaction weight of the commit sweep transaction, which
// includes a single input and output. // includes a single input and output.
var txWeight uint64 var weightEstimate lnwallet.TxWeightEstimator
// Begin with a base txn weight, e.g. version, nLockTime, etc. weightEstimate.AddP2WKHOutput()
txWeight += 4*lnwallet.BaseSweepTxSize + lnwallet.WitnessHeaderSize
// Add to_local p2wpkh witness and tx input.
txWeight += 4*lnwallet.InputSize + lnwallet.P2WKHWitnessSize
// Add to_local p2wpkh witness and tx input.
weightEstimate.AddP2WKHInput()
txWeight := uint64(weightEstimate.Weight())
return b.sweepSpendableOutputsTxn(txWeight, &selfOutput) return b.sweepSpendableOutputsTxn(txWeight, &selfOutput)
} }

@ -132,18 +132,6 @@ const (
// - LockTime: 4 bytes // - LockTime: 4 bytes
BaseTxSize = 4 + 4 BaseTxSize = 4 + 4
// BaseSweepTxSize 42 + 41 * num-swept-inputs bytes
// - Version: 4 bytes
// - WitnessHeader <---- part of the witness data
// - CountTxIn: 2 byte
// - TxIn: 41 * num-swept-inputs bytes
// ....SweptInputs....
// - CountTxOut: 1 byte
// - TxOut: 31 bytes
// P2WPKHOutput: 31 bytes
// - LockTime: 4 bytes
BaseSweepTxSize = 4 + 2 + 1 + P2WKHOutputSize + 4
// BaseCommitmentTxSize 125 + 43 * num-htlc-outputs bytes // BaseCommitmentTxSize 125 + 43 * num-htlc-outputs bytes
// - Version: 4 bytes // - Version: 4 bytes
// - WitnessHeader <---- part of the witness data // - WitnessHeader <---- part of the witness data
@ -351,8 +339,15 @@ func (twe *TxWeightEstimator) AddP2PKHInput() {
// AddP2WKHInput updates the weight estimate to account for an additional input // AddP2WKHInput updates the weight estimate to account for an additional input
// spending a P2PWKH output. // spending a P2PWKH output.
func (twe *TxWeightEstimator) AddP2WKHInput() { func (twe *TxWeightEstimator) AddP2WKHInput() {
twe.AddWitnessInput(P2WKHWitnessSize)
}
// AddWitnessInput updates the weight estimate to account for an additional
// input spending a pay-to-witness output. This accepts the total size of the
// witness as a parameter.
func (twe *TxWeightEstimator) AddWitnessInput(witnessSize int) {
twe.inputSize += InputSize twe.inputSize += InputSize
twe.inputWitnessSize += P2WKHWitnessSize twe.inputWitnessSize += witnessSize
twe.inputCount++ twe.inputCount++
twe.hasWitness = true twe.hasWitness = true
} }