2016-11-23 11:29:05 +03:00
|
|
|
package lnwallet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/roasbeef/btcd/blockchain"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-05-17 04:50:50 +03:00
|
|
|
// The weight(weight), which is different from the !size! (see BIP-141),
|
2016-11-23 11:29:05 +03:00
|
|
|
// is calculated as:
|
|
|
|
// Weight = 4 * BaseSize + WitnessSize (weight).
|
|
|
|
// BaseSize - size of the transaction without witness data (bytes).
|
|
|
|
// WitnessSize - witness size (bytes).
|
2017-05-17 04:50:50 +03:00
|
|
|
// Weight - the metric for determining the weight of the transaction.
|
2016-11-23 11:29:05 +03:00
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
// P2WSHSize 34 bytes
|
2016-11-23 11:29:05 +03:00
|
|
|
// - OP_0: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (WitnessScriptSHA256 length)
|
|
|
|
// - WitnessScriptSHA256: 32 bytes
|
|
|
|
P2WSHSize = 1 + 1 + 32
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
// P2WPKHSize 22 bytes
|
2016-11-23 11:29:05 +03:00
|
|
|
// - OP_0: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (PublicKeyHASH160 length)
|
|
|
|
// - PublicKeyHASH160: 20 bytes
|
|
|
|
P2WPKHSize = 1 + 1 + 20
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
// MultiSigSize 71 bytes
|
2016-11-23 11:29:05 +03:00
|
|
|
// - OP_2: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (pubKeyAlice length)
|
|
|
|
// - pubKeyAlice: 33 bytes
|
|
|
|
// - OP_DATA: 1 byte (pubKeyBob length)
|
|
|
|
// - pubKeyBob: 33 bytes
|
|
|
|
// - OP_2: 1 byte
|
|
|
|
// - OP_CHECKMULTISIG: 1 byte
|
|
|
|
MultiSigSize = 1 + 1 + 33 + 1 + 33 + 1 + 1
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
// WitnessSize 222 bytes
|
2016-11-23 11:29:05 +03:00
|
|
|
// - NumberOfWitnessElements: 1 byte
|
|
|
|
// - NilLength: 1 byte
|
|
|
|
// - sigAliceLength: 1 byte
|
|
|
|
// - sigAlice: 73 bytes
|
|
|
|
// - sigBobLength: 1 byte
|
|
|
|
// - sigBob: 73 bytes
|
|
|
|
// - WitnessScriptLength: 1 byte
|
|
|
|
// - WitnessScript (MultiSig)
|
|
|
|
WitnessSize = 1 + 1 + 1 + 73 + 1 + 73 + 1 + MultiSigSize
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
// FundingInputSize 41 bytes
|
2016-11-23 11:29:05 +03:00
|
|
|
// - PreviousOutPoint:
|
|
|
|
// - Hash: 32 bytes
|
|
|
|
// - Index: 4 bytes
|
|
|
|
// - OP_DATA: 1 byte (ScriptSigLength)
|
|
|
|
// - ScriptSig: 0 bytes
|
|
|
|
// - Witness <---- we use "Witness" instead of "ScriptSig" for
|
|
|
|
// transaction validation, but "Witness" is stored
|
2017-05-17 04:50:50 +03:00
|
|
|
// separately and weight for it size is smaller. So
|
2016-11-23 11:29:05 +03:00
|
|
|
// we separate the calculation of ordinary data
|
|
|
|
// from witness data.
|
|
|
|
// - Sequence: 4 bytes
|
|
|
|
FundingInputSize = 32 + 4 + 1 + 4
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
// CommitmentDelayOutput 43 bytes
|
2016-11-23 11:29:05 +03:00
|
|
|
// - Value: 8 bytes
|
|
|
|
// - VarInt: 1 byte (PkScript length)
|
|
|
|
// - PkScript (P2WSH)
|
|
|
|
CommitmentDelayOutput = 8 + 1 + P2WSHSize
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
// CommitmentKeyHashOutput 31 bytes
|
2016-11-23 11:29:05 +03:00
|
|
|
// - Value: 8 bytes
|
|
|
|
// - VarInt: 1 byte (PkScript length)
|
|
|
|
// - PkScript (P2WPKH)
|
|
|
|
CommitmentKeyHashOutput = 8 + 1 + P2WPKHSize
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
// HTLCSize 43 bytes
|
2016-11-23 11:29:05 +03:00
|
|
|
// - Value: 8 bytes
|
|
|
|
// - VarInt: 1 byte (PkScript length)
|
|
|
|
// - PkScript (PW2SH)
|
|
|
|
HTLCSize = 8 + 1 + P2WSHSize
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
// WitnessHeaderSize 2 bytes
|
2016-11-23 11:29:05 +03:00
|
|
|
// - Flag: 1 byte
|
|
|
|
// - Marker: 1 byte
|
|
|
|
WitnessHeaderSize = 1 + 1
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
// BaseCommitmentTxSize 125 43 * num-htlc-outputs bytes
|
2016-11-23 11:29:05 +03:00
|
|
|
// - Version: 4 bytes
|
|
|
|
// - WitnessHeader <---- part of the witness data
|
|
|
|
// - CountTxIn: 1 byte
|
2016-12-14 17:01:48 +03:00
|
|
|
// - TxIn: 41 bytes
|
2016-11-23 11:29:05 +03:00
|
|
|
// FundingInput
|
|
|
|
// - CountTxOut: 1 byte
|
2016-12-14 17:01:48 +03:00
|
|
|
// - TxOut: 74 + 43 * num-htlc-outputs bytes
|
2016-11-23 11:29:05 +03:00
|
|
|
// OutputPayingToThem,
|
|
|
|
// OutputPayingToUs,
|
|
|
|
// ....HTLCOutputs...
|
|
|
|
// - LockTime: 4 bytes
|
|
|
|
BaseCommitmentTxSize = 4 + 1 + FundingInputSize + 1 +
|
|
|
|
CommitmentDelayOutput + CommitmentKeyHashOutput + 4
|
|
|
|
|
2017-05-17 04:50:50 +03:00
|
|
|
// BaseCommitmentTxWeight 500 weight
|
|
|
|
BaseCommitmentTxWeight = blockchain.WitnessScaleFactor * BaseCommitmentTxSize
|
2016-11-23 11:29:05 +03:00
|
|
|
|
2017-05-17 04:50:50 +03:00
|
|
|
// WitnessCommitmentTxWeight 224 weight
|
|
|
|
WitnessCommitmentTxWeight = WitnessHeaderSize + WitnessSize
|
2016-11-23 11:29:05 +03:00
|
|
|
|
2017-05-17 04:50:50 +03:00
|
|
|
// HTLCWeight 172 weight
|
|
|
|
HTLCWeight = blockchain.WitnessScaleFactor * HTLCSize
|
2016-11-23 11:29:05 +03:00
|
|
|
|
2017-07-30 04:21:41 +03:00
|
|
|
// HtlcTimeoutWeight is the weight of the HTLC timeout transaction
|
|
|
|
// which will transition an outgoing HTLC to the delay-and-claim state.
|
|
|
|
HtlcTimeoutWeight = 663
|
|
|
|
|
|
|
|
// HtlcSuccessWeight is the weight of the HTLC success transaction
|
|
|
|
// which will transition an incoming HTLC to the delay-and-claim state.
|
|
|
|
HtlcSuccessWeight = 703
|
2017-07-30 04:22:11 +03:00
|
|
|
|
|
|
|
// MaxHTLCNumber is the maximum number HTLCs which can be included in a
|
|
|
|
// commitment transaction. This limit was chosen such that, in the case
|
|
|
|
// of a contract breach, the punishment transaction is able to sweep
|
|
|
|
// all the HTLC's yet still remain below the widely used standard
|
|
|
|
// weight limits.
|
|
|
|
MaxHTLCNumber = 967
|
2016-11-23 11:29:05 +03:00
|
|
|
)
|
|
|
|
|
2017-05-17 04:50:50 +03:00
|
|
|
// estimateCommitTxWeight estimate commitment transaction weight depending on
|
|
|
|
// the precalculated weight of base transaction, witness data, which is needed
|
|
|
|
// for paying for funding tx, and htlc weight multiplied by their count.
|
|
|
|
func estimateCommitTxWeight(count int, prediction bool) int64 {
|
2016-11-23 11:29:05 +03:00
|
|
|
// Make prediction about the size of commitment transaction with
|
|
|
|
// additional HTLC.
|
|
|
|
if prediction {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
|
2017-05-17 04:50:50 +03:00
|
|
|
htlcWeight := int64(count * HTLCWeight)
|
|
|
|
baseWeight := int64(BaseCommitmentTxWeight)
|
|
|
|
witnessWeight := int64(WitnessCommitmentTxWeight)
|
2016-11-23 11:29:05 +03:00
|
|
|
|
2017-05-17 04:50:50 +03:00
|
|
|
return htlcWeight + baseWeight + witnessWeight
|
2016-11-29 06:43:57 +03:00
|
|
|
}
|