2019-01-16 15:47:43 +01:00
|
|
|
package input
|
2016-11-23 09:29:05 +01:00
|
|
|
|
|
|
|
import (
|
2018-06-05 03:34:16 +02:00
|
|
|
"github.com/btcsuite/btcd/blockchain"
|
2022-03-18 18:37:51 +01:00
|
|
|
"github.com/btcsuite/btcd/txscript"
|
2018-06-05 03:34:16 +02:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2022-03-18 18:37:50 +01:00
|
|
|
"github.com/btcsuite/btcwallet/waddrmgr"
|
2024-05-24 15:56:30 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2016-11-23 09:29:05 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-09-26 01:35:19 +02:00
|
|
|
// witnessScaleFactor determines the level of "discount" witness data
|
|
|
|
// receives compared to "base" data. A scale factor of 4, denotes that
|
|
|
|
// witness data is 1/4 as cheap as regular non-witness data. Value copied
|
|
|
|
// here for convenience.
|
|
|
|
witnessScaleFactor = blockchain.WitnessScaleFactor
|
|
|
|
|
2017-05-17 03:50:50 +02:00
|
|
|
// The weight(weight), which is different from the !size! (see BIP-141),
|
2016-11-23 09:29:05 +01: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 03:50:50 +02:00
|
|
|
// Weight - the metric for determining the weight of the transaction.
|
2016-11-23 09:29:05 +01:00
|
|
|
|
2017-10-03 04:57:22 +02:00
|
|
|
// P2WPKHSize 22 bytes
|
|
|
|
// - OP_0: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (PublicKeyHASH160 length)
|
|
|
|
// - PublicKeyHASH160: 20 bytes
|
|
|
|
P2WPKHSize = 1 + 1 + 20
|
|
|
|
|
2020-03-04 17:00:33 +01:00
|
|
|
// NestedP2WPKHSize 23 bytes
|
|
|
|
// - OP_DATA: 1 byte (P2WPKHSize)
|
|
|
|
// - P2WPKHWitnessProgram: 22 bytes
|
|
|
|
NestedP2WPKHSize = 1 + P2WPKHSize
|
|
|
|
|
2017-02-23 20:56:47 +01:00
|
|
|
// P2WSHSize 34 bytes
|
2016-11-23 09:29:05 +01:00
|
|
|
// - OP_0: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (WitnessScriptSHA256 length)
|
|
|
|
// - WitnessScriptSHA256: 32 bytes
|
|
|
|
P2WSHSize = 1 + 1 + 32
|
|
|
|
|
2020-03-04 17:00:33 +01:00
|
|
|
// NestedP2WSHSize 35 bytes
|
|
|
|
// - OP_DATA: 1 byte (P2WSHSize)
|
2020-03-10 02:34:09 +01:00
|
|
|
// - P2WSHWitnessProgram: 34 bytes
|
2020-03-04 17:00:33 +01:00
|
|
|
NestedP2WSHSize = 1 + P2WSHSize
|
|
|
|
|
2021-09-23 21:40:37 +02:00
|
|
|
// UnknownWitnessSize 42 bytes
|
|
|
|
// - OP_x: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (max-size length)
|
|
|
|
// - max-size: 40 bytes
|
|
|
|
UnknownWitnessSize = 1 + 1 + 40
|
|
|
|
|
2024-02-06 12:25:53 +01:00
|
|
|
// BaseOutputSize 9 bytes
|
|
|
|
// - value: 8 bytes
|
|
|
|
// - var_int: 1 byte (pkscript_length)
|
|
|
|
BaseOutputSize = 8 + 1
|
|
|
|
|
2022-02-07 13:58:28 +01:00
|
|
|
// P2PKHSize 25 bytes.
|
2021-09-23 21:40:37 +02:00
|
|
|
P2PKHSize = 25
|
|
|
|
|
2017-09-26 01:35:19 +02:00
|
|
|
// P2PKHOutputSize 34 bytes
|
|
|
|
// - value: 8 bytes
|
|
|
|
// - var_int: 1 byte (pkscript_length)
|
|
|
|
// - pkscript (p2pkh): 25 bytes
|
2024-02-06 12:25:53 +01:00
|
|
|
P2PKHOutputSize = BaseOutputSize + P2PKHSize
|
2017-09-26 01:35:19 +02:00
|
|
|
|
2017-08-30 07:14:04 +02:00
|
|
|
// P2WKHOutputSize 31 bytes
|
|
|
|
// - value: 8 bytes
|
|
|
|
// - var_int: 1 byte (pkscript_length)
|
|
|
|
// - pkscript (p2wpkh): 22 bytes
|
2024-02-06 12:25:53 +01:00
|
|
|
P2WKHOutputSize = BaseOutputSize + P2WPKHSize
|
2017-08-30 07:14:04 +02:00
|
|
|
|
|
|
|
// P2WSHOutputSize 43 bytes
|
|
|
|
// - value: 8 bytes
|
|
|
|
// - var_int: 1 byte (pkscript_length)
|
|
|
|
// - pkscript (p2wsh): 34 bytes
|
2024-02-06 12:25:53 +01:00
|
|
|
P2WSHOutputSize = BaseOutputSize + P2WSHSize
|
2017-08-30 07:14:04 +02:00
|
|
|
|
2022-02-07 13:58:28 +01:00
|
|
|
// P2SHSize 23 bytes.
|
2021-09-23 21:40:37 +02:00
|
|
|
P2SHSize = 23
|
|
|
|
|
2017-10-03 04:57:22 +02:00
|
|
|
// P2SHOutputSize 32 bytes
|
|
|
|
// - value: 8 bytes
|
|
|
|
// - var_int: 1 byte (pkscript_length)
|
|
|
|
// - pkscript (p2sh): 23 bytes
|
2024-02-06 12:25:53 +01:00
|
|
|
P2SHOutputSize = BaseOutputSize + P2SHSize
|
2016-11-23 09:29:05 +01:00
|
|
|
|
2022-03-18 18:37:46 +01:00
|
|
|
// P2TRSize 34 bytes
|
|
|
|
// - OP_0: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (x-only public key length)
|
|
|
|
// - x-only public key length: 32 bytes
|
|
|
|
P2TRSize = 34
|
|
|
|
|
|
|
|
// P2TROutputSize 43 bytes
|
|
|
|
// - value: 8 bytes
|
|
|
|
// - var_int: 1 byte (pkscript_length)
|
|
|
|
// - pkscript (p2tr): 34 bytes
|
2024-02-06 12:25:53 +01:00
|
|
|
P2TROutputSize = BaseOutputSize + P2TRSize
|
2022-03-18 18:37:46 +01:00
|
|
|
|
2017-09-26 01:35:19 +02:00
|
|
|
// P2PKHScriptSigSize 108 bytes
|
2017-08-30 07:14:04 +02:00
|
|
|
// - OP_DATA: 1 byte (signature length)
|
|
|
|
// - signature
|
|
|
|
// - OP_DATA: 1 byte (pubkey length)
|
|
|
|
// - pubkey
|
2017-09-26 01:35:19 +02:00
|
|
|
P2PKHScriptSigSize = 1 + 73 + 1 + 33
|
|
|
|
|
|
|
|
// P2WKHWitnessSize 109 bytes
|
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - signature_length: 1 byte
|
|
|
|
// - signature
|
|
|
|
// - pubkey_length: 1 byte
|
|
|
|
// - pubkey
|
|
|
|
P2WKHWitnessSize = 1 + 1 + 73 + 1 + 33
|
2017-08-30 07:14:04 +02:00
|
|
|
|
2017-02-23 20:56:47 +01:00
|
|
|
// MultiSigSize 71 bytes
|
2016-11-23 09:29:05 +01: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
|
|
|
|
|
2020-04-06 02:07:14 +02:00
|
|
|
// MultiSigWitnessSize 222 bytes
|
2016-11-23 09:29:05 +01:00
|
|
|
// - NumberOfWitnessElements: 1 byte
|
|
|
|
// - NilLength: 1 byte
|
|
|
|
// - sigAliceLength: 1 byte
|
|
|
|
// - sigAlice: 73 bytes
|
|
|
|
// - sigBobLength: 1 byte
|
|
|
|
// - sigBob: 73 bytes
|
|
|
|
// - WitnessScriptLength: 1 byte
|
|
|
|
// - WitnessScript (MultiSig)
|
2020-04-06 02:07:14 +02:00
|
|
|
MultiSigWitnessSize = 1 + 1 + 1 + 73 + 1 + 73 + 1 + MultiSigSize
|
2016-11-23 09:29:05 +01:00
|
|
|
|
2017-08-30 07:14:04 +02:00
|
|
|
// InputSize 41 bytes
|
2016-11-23 09:29:05 +01: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 03:50:50 +02:00
|
|
|
// separately and weight for it size is smaller. So
|
2016-11-23 09:29:05 +01:00
|
|
|
// we separate the calculation of ordinary data
|
|
|
|
// from witness data.
|
|
|
|
// - Sequence: 4 bytes
|
2017-08-30 07:14:04 +02:00
|
|
|
InputSize = 32 + 4 + 1 + 4
|
|
|
|
|
2020-11-17 15:24:56 +01:00
|
|
|
// FundingInputSize 41 bytes
|
2017-08-30 07:14:04 +02:00
|
|
|
// FundingInputSize represents the size of an input to a funding
|
|
|
|
// transaction, and is equivalent to the size of a standard segwit input
|
|
|
|
// as calculated above.
|
|
|
|
FundingInputSize = InputSize
|
2016-11-23 09:29:05 +01:00
|
|
|
|
2017-02-23 20:56:47 +01:00
|
|
|
// CommitmentDelayOutput 43 bytes
|
2016-11-23 09:29:05 +01:00
|
|
|
// - Value: 8 bytes
|
|
|
|
// - VarInt: 1 byte (PkScript length)
|
|
|
|
// - PkScript (P2WSH)
|
|
|
|
CommitmentDelayOutput = 8 + 1 + P2WSHSize
|
|
|
|
|
2023-03-02 06:48:28 +01:00
|
|
|
// TaprootCommitmentOutput 43 bytes
|
|
|
|
// - Value: 8 bytes
|
|
|
|
// - VarInt: 1 byte (PkScript length)
|
|
|
|
// - PkScript (P2TR)
|
|
|
|
TaprootCommitmentOutput = 8 + 1 + P2TRSize
|
|
|
|
|
2017-02-23 20:56:47 +01:00
|
|
|
// CommitmentKeyHashOutput 31 bytes
|
2016-11-23 09:29:05 +01:00
|
|
|
// - Value: 8 bytes
|
|
|
|
// - VarInt: 1 byte (PkScript length)
|
|
|
|
// - PkScript (P2WPKH)
|
|
|
|
CommitmentKeyHashOutput = 8 + 1 + P2WPKHSize
|
|
|
|
|
2020-03-06 16:11:46 +01:00
|
|
|
// CommitmentAnchorOutput 43 bytes
|
|
|
|
// - Value: 8 bytes
|
|
|
|
// - VarInt: 1 byte (PkScript length)
|
|
|
|
// - PkScript (P2WSH)
|
|
|
|
CommitmentAnchorOutput = 8 + 1 + P2WSHSize
|
|
|
|
|
2023-03-02 06:48:28 +01:00
|
|
|
// TaprootCommitmentAnchorOutput 43 bytes
|
|
|
|
// - Value: 8 bytes
|
|
|
|
// - VarInt: 1 byte (PkScript length)
|
|
|
|
// - PkScript (P2TR)
|
|
|
|
TaprootCommitmentAnchorOutput = 8 + 1 + P2TRSize
|
|
|
|
|
2017-02-23 20:56:47 +01:00
|
|
|
// HTLCSize 43 bytes
|
2016-11-23 09:29:05 +01:00
|
|
|
// - Value: 8 bytes
|
|
|
|
// - VarInt: 1 byte (PkScript length)
|
|
|
|
// - PkScript (PW2SH)
|
|
|
|
HTLCSize = 8 + 1 + P2WSHSize
|
|
|
|
|
2017-02-23 20:56:47 +01:00
|
|
|
// WitnessHeaderSize 2 bytes
|
2016-11-23 09:29:05 +01:00
|
|
|
// - Flag: 1 byte
|
|
|
|
// - Marker: 1 byte
|
|
|
|
WitnessHeaderSize = 1 + 1
|
|
|
|
|
2017-09-26 01:35:19 +02:00
|
|
|
// BaseTxSize 8 bytes
|
lnwallet/size: correct commit to-local and 2nd stage script sizes
In this commit, we correct our size estimates for to-local scripts,
which are used on the commitment transaction and the htlc
success/timeout transactions. There have been observed cases of
transactions getting stuck because our estimates were too low, and cause
the transactions to not be relayed.
Our previous estimate for the commitment to-local script was derived
from an older version of the script. Though the estimate is greater than
the actual size, this has been updated with the current estimate of 79
bytes.
This estimates makes the assumption that CSV delays will be at most
4 bytes when serialized. Since this value is expressed in relative block
heights, this should be more than sufficient for our needs, even though
the maximum possible size for the little-endian int64 is 9 bytes (plus
an OP_DATA).
The other correction is to use the ToLocalScriptSize as our estimate for
htlc timeout/success scripts, as they are the same script. Previously,
our estimate was derived from the proper script, though we were 6 bytes
shy of the new to-local estimate, since we counted the csv_delay as 1
byte, and missed some other OP_DATAs.
All derived estimates have been updating depending on the new and
improved ToLocalScriptSize estimate, and fix some estimates that did not
include the witness length in the estimate.
Finally, we correct some weight miscalculations in:
- AcceptedHtlcTimeoutWitnessSize: missing data push lengths
- OfferedHtlcSuccessWitnessSize: extra 73 byte sig, missing data push lengths
- OfferedHtlcPenaltyWitnessSize: missing 33 byte pubkey
2018-07-25 05:30:48 +02:00
|
|
|
// - Version: 4 bytes
|
|
|
|
// - LockTime: 4 bytes
|
2017-09-26 01:35:19 +02:00
|
|
|
BaseTxSize = 4 + 4
|
|
|
|
|
2017-08-30 07:14:04 +02:00
|
|
|
// BaseCommitmentTxSize 125 + 43 * num-htlc-outputs bytes
|
2016-11-23 09:29:05 +01:00
|
|
|
// - Version: 4 bytes
|
|
|
|
// - WitnessHeader <---- part of the witness data
|
|
|
|
// - CountTxIn: 1 byte
|
2016-12-14 15:01:48 +01:00
|
|
|
// - TxIn: 41 bytes
|
2016-11-23 09:29:05 +01:00
|
|
|
// FundingInput
|
|
|
|
// - CountTxOut: 1 byte
|
2016-12-14 15:01:48 +01:00
|
|
|
// - TxOut: 74 + 43 * num-htlc-outputs bytes
|
2016-11-23 09:29:05 +01:00
|
|
|
// OutputPayingToThem,
|
|
|
|
// OutputPayingToUs,
|
|
|
|
// ....HTLCOutputs...
|
|
|
|
// - LockTime: 4 bytes
|
|
|
|
BaseCommitmentTxSize = 4 + 1 + FundingInputSize + 1 +
|
|
|
|
CommitmentDelayOutput + CommitmentKeyHashOutput + 4
|
|
|
|
|
2017-05-17 03:50:50 +02:00
|
|
|
// BaseCommitmentTxWeight 500 weight
|
2017-09-26 01:35:19 +02:00
|
|
|
BaseCommitmentTxWeight = witnessScaleFactor * BaseCommitmentTxSize
|
2016-11-23 09:29:05 +01:00
|
|
|
|
2017-05-17 03:50:50 +02:00
|
|
|
// WitnessCommitmentTxWeight 224 weight
|
2020-04-06 02:07:14 +02:00
|
|
|
WitnessCommitmentTxWeight = WitnessHeaderSize + MultiSigWitnessSize
|
2016-11-23 09:29:05 +01:00
|
|
|
|
2020-03-06 16:11:46 +01:00
|
|
|
// BaseAnchorCommitmentTxSize 225 + 43 * num-htlc-outputs bytes
|
|
|
|
// - Version: 4 bytes
|
|
|
|
// - WitnessHeader <---- part of the witness data
|
|
|
|
// - CountTxIn: 1 byte
|
|
|
|
// - TxIn: 41 bytes
|
|
|
|
// FundingInput
|
|
|
|
// - CountTxOut: 3 byte
|
|
|
|
// - TxOut: 4*43 + 43 * num-htlc-outputs bytes
|
|
|
|
// OutputPayingToThem,
|
|
|
|
// OutputPayingToUs,
|
|
|
|
// AnchorPayingToThem,
|
|
|
|
// AnchorPayingToUs,
|
|
|
|
// ....HTLCOutputs...
|
|
|
|
// - LockTime: 4 bytes
|
|
|
|
BaseAnchorCommitmentTxSize = 4 + 1 + FundingInputSize + 3 +
|
|
|
|
2*CommitmentDelayOutput + 2*CommitmentAnchorOutput + 4
|
|
|
|
|
2022-02-07 13:58:28 +01:00
|
|
|
// BaseAnchorCommitmentTxWeight 900 weight.
|
2020-03-06 16:11:46 +01:00
|
|
|
BaseAnchorCommitmentTxWeight = witnessScaleFactor * BaseAnchorCommitmentTxSize
|
|
|
|
|
2023-03-02 06:48:28 +01:00
|
|
|
// BaseTaprootCommitmentTxWeight 225 + 43 * num-htlc-outputs bytes
|
|
|
|
// - Version: 4 bytes
|
|
|
|
// - WitnessHeader <---- part of the witness data
|
|
|
|
// - CountTxIn: 1 byte
|
|
|
|
// - TxIn: 41 bytes
|
|
|
|
// FundingInput
|
|
|
|
// - CountTxOut: 3 byte
|
|
|
|
// - TxOut: 172 + 43 * num-htlc-outputs bytes
|
|
|
|
// OutputPayingToThem,
|
|
|
|
// OutputPayingToUs,
|
|
|
|
// ....HTLCOutputs...
|
|
|
|
// - LockTime: 4 bytes
|
|
|
|
BaseTaprootCommitmentTxWeight = (4 + 1 + FundingInputSize + 3 +
|
2023-08-09 07:22:12 +02:00
|
|
|
2*TaprootCommitmentOutput + 2*TaprootCommitmentAnchorOutput +
|
|
|
|
4) * witnessScaleFactor
|
2023-03-02 06:48:28 +01:00
|
|
|
|
2022-02-07 13:58:28 +01:00
|
|
|
// CommitWeight 724 weight.
|
2020-01-06 11:42:04 +01:00
|
|
|
CommitWeight = BaseCommitmentTxWeight + WitnessCommitmentTxWeight
|
|
|
|
|
2022-02-07 13:58:28 +01:00
|
|
|
// AnchorCommitWeight 1124 weight.
|
2020-03-06 16:11:46 +01:00
|
|
|
AnchorCommitWeight = BaseAnchorCommitmentTxWeight + WitnessCommitmentTxWeight
|
|
|
|
|
2023-03-02 06:48:28 +01:00
|
|
|
// TaprootCommitWeight 968 weight.
|
|
|
|
TaprootCommitWeight = (BaseTaprootCommitmentTxWeight +
|
|
|
|
WitnessHeaderSize + TaprootKeyPathWitnessSize)
|
|
|
|
|
2022-02-07 13:58:28 +01:00
|
|
|
// HTLCWeight 172 weight.
|
2017-09-26 01:35:19 +02:00
|
|
|
HTLCWeight = witnessScaleFactor * HTLCSize
|
2016-11-23 09:29:05 +01:00
|
|
|
|
2020-11-17 15:24:56 +01:00
|
|
|
// HtlcTimeoutWeight 663 weight
|
2017-07-30 03:21:41 +02:00
|
|
|
// HtlcTimeoutWeight is the weight of the HTLC timeout transaction
|
|
|
|
// which will transition an outgoing HTLC to the delay-and-claim state.
|
|
|
|
HtlcTimeoutWeight = 663
|
|
|
|
|
2023-03-02 06:48:28 +01:00
|
|
|
// TaprootHtlcTimeoutWeight is the total weight of the taproot HTLC
|
|
|
|
// timeout transaction.
|
|
|
|
TaprootHtlcTimeoutWeight = 645
|
|
|
|
|
2020-11-17 15:24:56 +01:00
|
|
|
// HtlcSuccessWeight 703 weight
|
2017-07-30 03:21:41 +02:00
|
|
|
// 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 03:22:11 +02:00
|
|
|
|
2023-03-02 06:48:28 +01:00
|
|
|
// TaprootHtlcSuccessWeight is the total weight of the taproot HTLC
|
|
|
|
// success transaction.
|
|
|
|
TaprootHtlcSuccessWeight = 705
|
|
|
|
|
2020-11-17 15:24:56 +01:00
|
|
|
// HtlcConfirmedScriptOverhead 3 bytes
|
2020-03-06 16:11:49 +01:00
|
|
|
// HtlcConfirmedScriptOverhead is the extra length of an HTLC script
|
|
|
|
// that requires confirmation before it can be spent. These extra bytes
|
|
|
|
// is a result of the extra CSV check.
|
|
|
|
HtlcConfirmedScriptOverhead = 3
|
|
|
|
|
2020-11-17 15:24:56 +01:00
|
|
|
// HtlcTimeoutWeightConfirmed 666 weight
|
2020-03-06 16:11:49 +01:00
|
|
|
// HtlcTimeoutWeightConfirmed is the weight of the HTLC timeout
|
|
|
|
// transaction which will transition an outgoing HTLC to the
|
|
|
|
// delay-and-claim state, for the confirmed HTLC outputs. It is 3 bytes
|
|
|
|
// larger because of the additional CSV check in the input script.
|
|
|
|
HtlcTimeoutWeightConfirmed = HtlcTimeoutWeight + HtlcConfirmedScriptOverhead
|
|
|
|
|
2020-11-17 15:24:56 +01:00
|
|
|
// HtlcSuccessWeightConfirmed 706 weight
|
|
|
|
// HtlcSuccessWeightConfirmed is the weight of the HTLC success
|
2020-03-06 16:11:49 +01:00
|
|
|
// transaction which will transition an incoming HTLC to the
|
|
|
|
// delay-and-claim state, for the confirmed HTLC outputs. It is 3 bytes
|
|
|
|
// larger because of the cdditional CSV check in the input script.
|
|
|
|
HtlcSuccessWeightConfirmed = HtlcSuccessWeight + HtlcConfirmedScriptOverhead
|
|
|
|
|
2020-11-17 15:24:56 +01:00
|
|
|
// MaxHTLCNumber 966
|
2017-07-30 03:22:11 +02: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.
|
2017-08-30 07:14:04 +02:00
|
|
|
MaxHTLCNumber = 966
|
|
|
|
|
lnwallet/size: correct commit to-local and 2nd stage script sizes
In this commit, we correct our size estimates for to-local scripts,
which are used on the commitment transaction and the htlc
success/timeout transactions. There have been observed cases of
transactions getting stuck because our estimates were too low, and cause
the transactions to not be relayed.
Our previous estimate for the commitment to-local script was derived
from an older version of the script. Though the estimate is greater than
the actual size, this has been updated with the current estimate of 79
bytes.
This estimates makes the assumption that CSV delays will be at most
4 bytes when serialized. Since this value is expressed in relative block
heights, this should be more than sufficient for our needs, even though
the maximum possible size for the little-endian int64 is 9 bytes (plus
an OP_DATA).
The other correction is to use the ToLocalScriptSize as our estimate for
htlc timeout/success scripts, as they are the same script. Previously,
our estimate was derived from the proper script, though we were 6 bytes
shy of the new to-local estimate, since we counted the csv_delay as 1
byte, and missed some other OP_DATAs.
All derived estimates have been updating depending on the new and
improved ToLocalScriptSize estimate, and fix some estimates that did not
include the witness length in the estimate.
Finally, we correct some weight miscalculations in:
- AcceptedHtlcTimeoutWitnessSize: missing data push lengths
- OfferedHtlcSuccessWitnessSize: extra 73 byte sig, missing data push lengths
- OfferedHtlcPenaltyWitnessSize: missing 33 byte pubkey
2018-07-25 05:30:48 +02:00
|
|
|
// ToLocalScriptSize 79 bytes
|
2017-08-30 07:14:04 +02:00
|
|
|
// - OP_IF: 1 byte
|
lnwallet/size: correct commit to-local and 2nd stage script sizes
In this commit, we correct our size estimates for to-local scripts,
which are used on the commitment transaction and the htlc
success/timeout transactions. There have been observed cases of
transactions getting stuck because our estimates were too low, and cause
the transactions to not be relayed.
Our previous estimate for the commitment to-local script was derived
from an older version of the script. Though the estimate is greater than
the actual size, this has been updated with the current estimate of 79
bytes.
This estimates makes the assumption that CSV delays will be at most
4 bytes when serialized. Since this value is expressed in relative block
heights, this should be more than sufficient for our needs, even though
the maximum possible size for the little-endian int64 is 9 bytes (plus
an OP_DATA).
The other correction is to use the ToLocalScriptSize as our estimate for
htlc timeout/success scripts, as they are the same script. Previously,
our estimate was derived from the proper script, though we were 6 bytes
shy of the new to-local estimate, since we counted the csv_delay as 1
byte, and missed some other OP_DATAs.
All derived estimates have been updating depending on the new and
improved ToLocalScriptSize estimate, and fix some estimates that did not
include the witness length in the estimate.
Finally, we correct some weight miscalculations in:
- AcceptedHtlcTimeoutWitnessSize: missing data push lengths
- OfferedHtlcSuccessWitnessSize: extra 73 byte sig, missing data push lengths
- OfferedHtlcPenaltyWitnessSize: missing 33 byte pubkey
2018-07-25 05:30:48 +02:00
|
|
|
// - OP_DATA: 1 byte
|
|
|
|
// - revoke_key: 33 bytes
|
2017-08-30 07:14:04 +02:00
|
|
|
// - OP_ELSE: 1 byte
|
lnwallet/size: correct commit to-local and 2nd stage script sizes
In this commit, we correct our size estimates for to-local scripts,
which are used on the commitment transaction and the htlc
success/timeout transactions. There have been observed cases of
transactions getting stuck because our estimates were too low, and cause
the transactions to not be relayed.
Our previous estimate for the commitment to-local script was derived
from an older version of the script. Though the estimate is greater than
the actual size, this has been updated with the current estimate of 79
bytes.
This estimates makes the assumption that CSV delays will be at most
4 bytes when serialized. Since this value is expressed in relative block
heights, this should be more than sufficient for our needs, even though
the maximum possible size for the little-endian int64 is 9 bytes (plus
an OP_DATA).
The other correction is to use the ToLocalScriptSize as our estimate for
htlc timeout/success scripts, as they are the same script. Previously,
our estimate was derived from the proper script, though we were 6 bytes
shy of the new to-local estimate, since we counted the csv_delay as 1
byte, and missed some other OP_DATAs.
All derived estimates have been updating depending on the new and
improved ToLocalScriptSize estimate, and fix some estimates that did not
include the witness length in the estimate.
Finally, we correct some weight miscalculations in:
- AcceptedHtlcTimeoutWitnessSize: missing data push lengths
- OfferedHtlcSuccessWitnessSize: extra 73 byte sig, missing data push lengths
- OfferedHtlcPenaltyWitnessSize: missing 33 byte pubkey
2018-07-25 05:30:48 +02:00
|
|
|
// - OP_DATA: 1 byte
|
|
|
|
// - csv_delay: 4 bytes
|
|
|
|
// - OP_CHECKSEQUENCEVERIFY: 1 byte
|
|
|
|
// - OP_DROP: 1 byte
|
|
|
|
// - OP_DATA: 1 byte
|
|
|
|
// - delay_key: 33 bytes
|
2017-08-30 07:14:04 +02:00
|
|
|
// - OP_ENDIF: 1 byte
|
lnwallet/size: correct commit to-local and 2nd stage script sizes
In this commit, we correct our size estimates for to-local scripts,
which are used on the commitment transaction and the htlc
success/timeout transactions. There have been observed cases of
transactions getting stuck because our estimates were too low, and cause
the transactions to not be relayed.
Our previous estimate for the commitment to-local script was derived
from an older version of the script. Though the estimate is greater than
the actual size, this has been updated with the current estimate of 79
bytes.
This estimates makes the assumption that CSV delays will be at most
4 bytes when serialized. Since this value is expressed in relative block
heights, this should be more than sufficient for our needs, even though
the maximum possible size for the little-endian int64 is 9 bytes (plus
an OP_DATA).
The other correction is to use the ToLocalScriptSize as our estimate for
htlc timeout/success scripts, as they are the same script. Previously,
our estimate was derived from the proper script, though we were 6 bytes
shy of the new to-local estimate, since we counted the csv_delay as 1
byte, and missed some other OP_DATAs.
All derived estimates have been updating depending on the new and
improved ToLocalScriptSize estimate, and fix some estimates that did not
include the witness length in the estimate.
Finally, we correct some weight miscalculations in:
- AcceptedHtlcTimeoutWitnessSize: missing data push lengths
- OfferedHtlcSuccessWitnessSize: extra 73 byte sig, missing data push lengths
- OfferedHtlcPenaltyWitnessSize: missing 33 byte pubkey
2018-07-25 05:30:48 +02:00
|
|
|
// - OP_CHECKSIG: 1 byte
|
|
|
|
ToLocalScriptSize = 1 + 1 + 33 + 1 + 1 + 4 + 1 + 1 + 1 + 33 + 1 + 1
|
|
|
|
|
2021-07-14 01:40:23 +02:00
|
|
|
// LeaseWitnessScriptSizeOverhead represents the size overhead in bytes
|
|
|
|
// of the witness scripts used within script enforced lease commitments.
|
|
|
|
// This overhead results from the additional CLTV clause required to
|
|
|
|
// spend.
|
|
|
|
//
|
|
|
|
// - OP_DATA: 1 byte
|
|
|
|
// - lease_expiry: 4 bytes
|
|
|
|
// - OP_CHECKLOCKTIMEVERIFY: 1 byte
|
|
|
|
// - OP_DROP: 1 byte
|
|
|
|
LeaseWitnessScriptSizeOverhead = 1 + 4 + 1 + 1
|
|
|
|
|
lnwallet/size: correct commit to-local and 2nd stage script sizes
In this commit, we correct our size estimates for to-local scripts,
which are used on the commitment transaction and the htlc
success/timeout transactions. There have been observed cases of
transactions getting stuck because our estimates were too low, and cause
the transactions to not be relayed.
Our previous estimate for the commitment to-local script was derived
from an older version of the script. Though the estimate is greater than
the actual size, this has been updated with the current estimate of 79
bytes.
This estimates makes the assumption that CSV delays will be at most
4 bytes when serialized. Since this value is expressed in relative block
heights, this should be more than sufficient for our needs, even though
the maximum possible size for the little-endian int64 is 9 bytes (plus
an OP_DATA).
The other correction is to use the ToLocalScriptSize as our estimate for
htlc timeout/success scripts, as they are the same script. Previously,
our estimate was derived from the proper script, though we were 6 bytes
shy of the new to-local estimate, since we counted the csv_delay as 1
byte, and missed some other OP_DATAs.
All derived estimates have been updating depending on the new and
improved ToLocalScriptSize estimate, and fix some estimates that did not
include the witness length in the estimate.
Finally, we correct some weight miscalculations in:
- AcceptedHtlcTimeoutWitnessSize: missing data push lengths
- OfferedHtlcSuccessWitnessSize: extra 73 byte sig, missing data push lengths
- OfferedHtlcPenaltyWitnessSize: missing 33 byte pubkey
2018-07-25 05:30:48 +02:00
|
|
|
// ToLocalTimeoutWitnessSize 156 bytes
|
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - local_delay_sig_length: 1 byte
|
|
|
|
// - local_delay_sig: 73 bytes
|
|
|
|
// - zero_length: 1 byte
|
|
|
|
// - witness_script_length: 1 byte
|
|
|
|
// - witness_script (to_local_script)
|
2017-10-10 10:36:06 +02:00
|
|
|
ToLocalTimeoutWitnessSize = 1 + 1 + 73 + 1 + 1 + ToLocalScriptSize
|
2017-08-30 07:14:04 +02:00
|
|
|
|
2020-04-06 02:07:14 +02:00
|
|
|
// ToLocalPenaltyWitnessSize 157 bytes
|
2017-08-30 07:14:04 +02:00
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - revocation_sig_length: 1 byte
|
|
|
|
// - revocation_sig: 73 bytes
|
2020-04-06 02:07:14 +02:00
|
|
|
// - OP_TRUE_length: 1 byte
|
lnwallet/size: correct commit to-local and 2nd stage script sizes
In this commit, we correct our size estimates for to-local scripts,
which are used on the commitment transaction and the htlc
success/timeout transactions. There have been observed cases of
transactions getting stuck because our estimates were too low, and cause
the transactions to not be relayed.
Our previous estimate for the commitment to-local script was derived
from an older version of the script. Though the estimate is greater than
the actual size, this has been updated with the current estimate of 79
bytes.
This estimates makes the assumption that CSV delays will be at most
4 bytes when serialized. Since this value is expressed in relative block
heights, this should be more than sufficient for our needs, even though
the maximum possible size for the little-endian int64 is 9 bytes (plus
an OP_DATA).
The other correction is to use the ToLocalScriptSize as our estimate for
htlc timeout/success scripts, as they are the same script. Previously,
our estimate was derived from the proper script, though we were 6 bytes
shy of the new to-local estimate, since we counted the csv_delay as 1
byte, and missed some other OP_DATAs.
All derived estimates have been updating depending on the new and
improved ToLocalScriptSize estimate, and fix some estimates that did not
include the witness length in the estimate.
Finally, we correct some weight miscalculations in:
- AcceptedHtlcTimeoutWitnessSize: missing data push lengths
- OfferedHtlcSuccessWitnessSize: extra 73 byte sig, missing data push lengths
- OfferedHtlcPenaltyWitnessSize: missing 33 byte pubkey
2018-07-25 05:30:48 +02:00
|
|
|
// - OP_TRUE: 1 byte
|
2017-08-30 07:14:04 +02:00
|
|
|
// - witness_script_length: 1 byte
|
|
|
|
// - witness_script (to_local_script)
|
2020-04-06 02:07:14 +02:00
|
|
|
ToLocalPenaltyWitnessSize = 1 + 1 + 73 + 1 + 1 + 1 + ToLocalScriptSize
|
2017-08-30 07:14:04 +02:00
|
|
|
|
2020-03-06 16:11:46 +01:00
|
|
|
// ToRemoteConfirmedScriptSize 37 bytes
|
|
|
|
// - OP_DATA: 1 byte
|
|
|
|
// - to_remote_key: 33 bytes
|
|
|
|
// - OP_CHECKSIGVERIFY: 1 byte
|
|
|
|
// - OP_1: 1 byte
|
|
|
|
// - OP_CHECKSEQUENCEVERIFY: 1 byte
|
|
|
|
ToRemoteConfirmedScriptSize = 1 + 33 + 1 + 1 + 1
|
|
|
|
|
|
|
|
// ToRemoteConfirmedWitnessSize 113 bytes
|
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - sig_length: 1 byte
|
|
|
|
// - sig: 73 bytes
|
|
|
|
// - witness_script_length: 1 byte
|
|
|
|
// - witness_script (to_remote_delayed_script)
|
|
|
|
ToRemoteConfirmedWitnessSize = 1 + 1 + 73 + 1 + ToRemoteConfirmedScriptSize
|
|
|
|
|
2020-11-17 13:23:00 +01:00
|
|
|
// AcceptedHtlcScriptSize 140 bytes
|
2017-08-30 07:14:04 +02:00
|
|
|
// - OP_DUP: 1 byte
|
|
|
|
// - OP_HASH160: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (RIPEMD160(SHA256(revocationkey)) length)
|
|
|
|
// - RIPEMD160(SHA256(revocationkey)): 20 bytes
|
|
|
|
// - OP_EQUAL: 1 byte
|
|
|
|
// - OP_IF: 1 byte
|
|
|
|
// - OP_CHECKSIG: 1 byte
|
|
|
|
// - OP_ELSE: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (remotekey length)
|
|
|
|
// - remotekey: 33 bytes
|
|
|
|
// - OP_SWAP: 1 byte
|
|
|
|
// - OP_SIZE: 1 byte
|
2020-04-06 02:07:14 +02:00
|
|
|
// - OP_DATA: 1 byte (32 length)
|
2017-08-30 07:14:04 +02:00
|
|
|
// - 32: 1 byte
|
|
|
|
// - OP_EQUAL: 1 byte
|
|
|
|
// - OP_IF: 1 byte
|
|
|
|
// - OP_HASH160: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (RIPEMD160(payment_hash) length)
|
|
|
|
// - RIPEMD160(payment_hash): 20 bytes
|
|
|
|
// - OP_EQUALVERIFY: 1 byte
|
|
|
|
// - 2: 1 byte
|
|
|
|
// - OP_SWAP: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (localkey length)
|
|
|
|
// - localkey: 33 bytes
|
|
|
|
// - 2: 1 byte
|
|
|
|
// - OP_CHECKMULTISIG: 1 byte
|
|
|
|
// - OP_ELSE: 1 byte
|
|
|
|
// - OP_DROP: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (cltv_expiry length)
|
|
|
|
// - cltv_expiry: 4 bytes
|
|
|
|
// - OP_CHECKLOCKTIMEVERIFY: 1 byte
|
|
|
|
// - OP_DROP: 1 byte
|
|
|
|
// - OP_CHECKSIG: 1 byte
|
|
|
|
// - OP_ENDIF: 1 byte
|
2020-11-17 13:23:00 +01:00
|
|
|
// - OP_1: 1 byte // These 3 extra bytes are only
|
|
|
|
// - OP_CSV: 1 byte // present for the confirmed
|
|
|
|
// - OP_DROP: 1 byte // HTLC script types.
|
2017-08-30 07:14:04 +02:00
|
|
|
// - OP_ENDIF: 1 byte
|
2020-04-06 02:07:14 +02:00
|
|
|
AcceptedHtlcScriptSize = 3*1 + 20 + 5*1 + 33 + 8*1 + 20 + 4*1 +
|
2020-11-17 13:23:00 +01:00
|
|
|
33 + 5*1 + 4 + 5*1
|
2017-08-30 07:14:04 +02:00
|
|
|
|
2022-02-07 13:58:28 +01:00
|
|
|
// AcceptedHtlcScriptSizeConfirmed 143 bytes.
|
2020-11-17 13:23:00 +01:00
|
|
|
AcceptedHtlcScriptSizeConfirmed = AcceptedHtlcScriptSize +
|
|
|
|
HtlcConfirmedScriptOverhead
|
2020-12-09 12:24:02 +01:00
|
|
|
|
2020-11-17 15:24:56 +01:00
|
|
|
// AcceptedHtlcTimeoutWitnessSize 217 bytes
|
lnwallet/size: correct commit to-local and 2nd stage script sizes
In this commit, we correct our size estimates for to-local scripts,
which are used on the commitment transaction and the htlc
success/timeout transactions. There have been observed cases of
transactions getting stuck because our estimates were too low, and cause
the transactions to not be relayed.
Our previous estimate for the commitment to-local script was derived
from an older version of the script. Though the estimate is greater than
the actual size, this has been updated with the current estimate of 79
bytes.
This estimates makes the assumption that CSV delays will be at most
4 bytes when serialized. Since this value is expressed in relative block
heights, this should be more than sufficient for our needs, even though
the maximum possible size for the little-endian int64 is 9 bytes (plus
an OP_DATA).
The other correction is to use the ToLocalScriptSize as our estimate for
htlc timeout/success scripts, as they are the same script. Previously,
our estimate was derived from the proper script, though we were 6 bytes
shy of the new to-local estimate, since we counted the csv_delay as 1
byte, and missed some other OP_DATAs.
All derived estimates have been updating depending on the new and
improved ToLocalScriptSize estimate, and fix some estimates that did not
include the witness length in the estimate.
Finally, we correct some weight miscalculations in:
- AcceptedHtlcTimeoutWitnessSize: missing data push lengths
- OfferedHtlcSuccessWitnessSize: extra 73 byte sig, missing data push lengths
- OfferedHtlcPenaltyWitnessSize: missing 33 byte pubkey
2018-07-25 05:30:48 +02:00
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - sender_sig_length: 1 byte
|
|
|
|
// - sender_sig: 73 bytes
|
|
|
|
// - nil_length: 1 byte
|
|
|
|
// - witness_script_length: 1 byte
|
|
|
|
// - witness_script: (accepted_htlc_script)
|
|
|
|
AcceptedHtlcTimeoutWitnessSize = 1 + 1 + 73 + 1 + 1 + AcceptedHtlcScriptSize
|
|
|
|
|
2022-02-07 13:58:28 +01:00
|
|
|
// AcceptedHtlcTimeoutWitnessSizeConfirmed 220 bytes.
|
2020-11-17 13:23:00 +01:00
|
|
|
AcceptedHtlcTimeoutWitnessSizeConfirmed = 1 + 1 + 73 + 1 + 1 +
|
|
|
|
AcceptedHtlcScriptSizeConfirmed
|
|
|
|
|
2020-11-17 15:24:56 +01:00
|
|
|
// AcceptedHtlcPenaltyWitnessSize 250 bytes
|
lnwallet/size: correct commit to-local and 2nd stage script sizes
In this commit, we correct our size estimates for to-local scripts,
which are used on the commitment transaction and the htlc
success/timeout transactions. There have been observed cases of
transactions getting stuck because our estimates were too low, and cause
the transactions to not be relayed.
Our previous estimate for the commitment to-local script was derived
from an older version of the script. Though the estimate is greater than
the actual size, this has been updated with the current estimate of 79
bytes.
This estimates makes the assumption that CSV delays will be at most
4 bytes when serialized. Since this value is expressed in relative block
heights, this should be more than sufficient for our needs, even though
the maximum possible size for the little-endian int64 is 9 bytes (plus
an OP_DATA).
The other correction is to use the ToLocalScriptSize as our estimate for
htlc timeout/success scripts, as they are the same script. Previously,
our estimate was derived from the proper script, though we were 6 bytes
shy of the new to-local estimate, since we counted the csv_delay as 1
byte, and missed some other OP_DATAs.
All derived estimates have been updating depending on the new and
improved ToLocalScriptSize estimate, and fix some estimates that did not
include the witness length in the estimate.
Finally, we correct some weight miscalculations in:
- AcceptedHtlcTimeoutWitnessSize: missing data push lengths
- OfferedHtlcSuccessWitnessSize: extra 73 byte sig, missing data push lengths
- OfferedHtlcPenaltyWitnessSize: missing 33 byte pubkey
2018-07-25 05:30:48 +02:00
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - revocation_sig_length: 1 byte
|
|
|
|
// - revocation_sig: 73 bytes
|
|
|
|
// - revocation_key_length: 1 byte
|
|
|
|
// - revocation_key: 33 bytes
|
|
|
|
// - witness_script_length: 1 byte
|
|
|
|
// - witness_script (accepted_htlc_script)
|
2017-10-10 10:36:06 +02:00
|
|
|
AcceptedHtlcPenaltyWitnessSize = 1 + 1 + 73 + 1 + 33 + 1 + AcceptedHtlcScriptSize
|
2017-08-30 07:14:04 +02:00
|
|
|
|
2022-02-07 13:58:28 +01:00
|
|
|
// AcceptedHtlcPenaltyWitnessSizeConfirmed 253 bytes.
|
2020-11-17 13:23:00 +01:00
|
|
|
AcceptedHtlcPenaltyWitnessSizeConfirmed = 1 + 1 + 73 + 1 + 33 + 1 +
|
|
|
|
AcceptedHtlcScriptSizeConfirmed
|
|
|
|
|
2020-11-17 15:24:56 +01:00
|
|
|
// AcceptedHtlcSuccessWitnessSize 324 bytes
|
2020-04-06 02:07:14 +02:00
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - nil_length: 1 byte
|
|
|
|
// - sig_alice_length: 1 byte
|
|
|
|
// - sig_alice: 73 bytes
|
|
|
|
// - sig_bob_length: 1 byte
|
|
|
|
// - sig_bob: 73 bytes
|
|
|
|
// - preimage_length: 1 byte
|
|
|
|
// - preimage: 32 bytes
|
|
|
|
// - witness_script_length: 1 byte
|
|
|
|
// - witness_script (accepted_htlc_script)
|
2020-11-17 13:23:00 +01:00
|
|
|
//
|
|
|
|
// Input to second level success tx, spending non-delayed HTLC output.
|
2020-04-06 02:07:14 +02:00
|
|
|
AcceptedHtlcSuccessWitnessSize = 1 + 1 + 1 + 73 + 1 + 73 + 1 + 32 + 1 +
|
|
|
|
AcceptedHtlcScriptSize
|
|
|
|
|
2020-11-17 15:24:56 +01:00
|
|
|
// AcceptedHtlcSuccessWitnessSizeConfirmed 327 bytes
|
2020-12-09 12:24:02 +01:00
|
|
|
//
|
|
|
|
// Input to second level success tx, spending 1 CSV delayed HTLC output.
|
|
|
|
AcceptedHtlcSuccessWitnessSizeConfirmed = 1 + 1 + 1 + 73 + 1 + 73 + 1 + 32 + 1 +
|
|
|
|
AcceptedHtlcScriptSizeConfirmed
|
|
|
|
|
2020-11-17 13:23:00 +01:00
|
|
|
// OfferedHtlcScriptSize 133 bytes
|
2017-08-30 07:14:04 +02:00
|
|
|
// - OP_DUP: 1 byte
|
|
|
|
// - OP_HASH160: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (RIPEMD160(SHA256(revocationkey)) length)
|
|
|
|
// - RIPEMD160(SHA256(revocationkey)): 20 bytes
|
|
|
|
// - OP_EQUAL: 1 byte
|
|
|
|
// - OP_IF: 1 byte
|
|
|
|
// - OP_CHECKSIG: 1 byte
|
|
|
|
// - OP_ELSE: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (remotekey length)
|
|
|
|
// - remotekey: 33 bytes
|
|
|
|
// - OP_SWAP: 1 byte
|
|
|
|
// - OP_SIZE: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (32 length)
|
|
|
|
// - 32: 1 byte
|
|
|
|
// - OP_EQUAL: 1 byte
|
|
|
|
// - OP_NOTIF: 1 byte
|
|
|
|
// - OP_DROP: 1 byte
|
|
|
|
// - 2: 1 byte
|
|
|
|
// - OP_SWAP: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (localkey length)
|
|
|
|
// - localkey: 33 bytes
|
|
|
|
// - 2: 1 byte
|
|
|
|
// - OP_CHECKMULTISIG: 1 byte
|
|
|
|
// - OP_ELSE: 1 byte
|
|
|
|
// - OP_HASH160: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (RIPEMD160(payment_hash) length)
|
|
|
|
// - RIPEMD160(payment_hash): 20 bytes
|
|
|
|
// - OP_EQUALVERIFY: 1 byte
|
|
|
|
// - OP_CHECKSIG: 1 byte
|
|
|
|
// - OP_ENDIF: 1 byte
|
2020-11-17 13:23:00 +01:00
|
|
|
// - OP_1: 1 byte // These 3 extra bytes are only
|
|
|
|
// - OP_CSV: 1 byte // present for the confirmed
|
|
|
|
// - OP_DROP: 1 byte // HTLC script types.
|
2017-08-30 07:14:04 +02:00
|
|
|
// - OP_ENDIF: 1 byte
|
2020-11-17 13:23:00 +01:00
|
|
|
OfferedHtlcScriptSize = 3*1 + 20 + 5*1 + 33 + 10*1 + 33 + 5*1 + 20 + 4*1
|
2017-10-10 10:36:06 +02:00
|
|
|
|
2022-02-07 13:58:28 +01:00
|
|
|
// OfferedHtlcScriptSizeConfirmed 136 bytes.
|
2020-11-17 13:23:00 +01:00
|
|
|
OfferedHtlcScriptSizeConfirmed = OfferedHtlcScriptSize +
|
|
|
|
HtlcConfirmedScriptOverhead
|
2020-12-09 12:24:02 +01:00
|
|
|
|
2020-11-17 13:23:00 +01:00
|
|
|
// OfferedHtlcSuccessWitnessSize 242 bytes
|
lnwallet/size: correct commit to-local and 2nd stage script sizes
In this commit, we correct our size estimates for to-local scripts,
which are used on the commitment transaction and the htlc
success/timeout transactions. There have been observed cases of
transactions getting stuck because our estimates were too low, and cause
the transactions to not be relayed.
Our previous estimate for the commitment to-local script was derived
from an older version of the script. Though the estimate is greater than
the actual size, this has been updated with the current estimate of 79
bytes.
This estimates makes the assumption that CSV delays will be at most
4 bytes when serialized. Since this value is expressed in relative block
heights, this should be more than sufficient for our needs, even though
the maximum possible size for the little-endian int64 is 9 bytes (plus
an OP_DATA).
The other correction is to use the ToLocalScriptSize as our estimate for
htlc timeout/success scripts, as they are the same script. Previously,
our estimate was derived from the proper script, though we were 6 bytes
shy of the new to-local estimate, since we counted the csv_delay as 1
byte, and missed some other OP_DATAs.
All derived estimates have been updating depending on the new and
improved ToLocalScriptSize estimate, and fix some estimates that did not
include the witness length in the estimate.
Finally, we correct some weight miscalculations in:
- AcceptedHtlcTimeoutWitnessSize: missing data push lengths
- OfferedHtlcSuccessWitnessSize: extra 73 byte sig, missing data push lengths
- OfferedHtlcPenaltyWitnessSize: missing 33 byte pubkey
2018-07-25 05:30:48 +02:00
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - receiver_sig_length: 1 byte
|
|
|
|
// - receiver_sig: 73 bytes
|
|
|
|
// - payment_preimage_length: 1 byte
|
|
|
|
// - payment_preimage: 32 bytes
|
|
|
|
// - witness_script_length: 1 byte
|
|
|
|
// - witness_script (offered_htlc_script)
|
2020-04-06 02:07:14 +02:00
|
|
|
OfferedHtlcSuccessWitnessSize = 1 + 1 + 73 + 1 + 32 + 1 + OfferedHtlcScriptSize
|
|
|
|
|
2022-02-07 13:58:28 +01:00
|
|
|
// OfferedHtlcSuccessWitnessSizeConfirmed 245 bytes.
|
2020-11-17 13:23:00 +01:00
|
|
|
OfferedHtlcSuccessWitnessSizeConfirmed = 1 + 1 + 73 + 1 + 32 + 1 +
|
|
|
|
OfferedHtlcScriptSizeConfirmed
|
|
|
|
|
2020-11-17 15:24:56 +01:00
|
|
|
// OfferedHtlcTimeoutWitnessSize 285 bytes
|
2020-04-06 02:07:14 +02:00
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - nil_length: 1 byte
|
|
|
|
// - sig_alice_length: 1 byte
|
|
|
|
// - sig_alice: 73 bytes
|
|
|
|
// - sig_bob_length: 1 byte
|
|
|
|
// - sig_bob: 73 bytes
|
|
|
|
// - nil_length: 1 byte
|
|
|
|
// - witness_script_length: 1 byte
|
|
|
|
// - witness_script (offered_htlc_script)
|
2020-11-17 13:23:00 +01:00
|
|
|
//
|
|
|
|
// Input to second level timeout tx, spending non-delayed HTLC output.
|
|
|
|
OfferedHtlcTimeoutWitnessSize = 1 + 1 + 1 + 73 + 1 + 73 + 1 + 1 +
|
|
|
|
OfferedHtlcScriptSize
|
2018-01-17 03:03:08 +01:00
|
|
|
|
2020-11-17 15:24:56 +01:00
|
|
|
// OfferedHtlcTimeoutWitnessSizeConfirmed 288 bytes
|
2020-12-09 12:24:02 +01:00
|
|
|
//
|
|
|
|
// Input to second level timeout tx, spending 1 CSV delayed HTLC output.
|
|
|
|
OfferedHtlcTimeoutWitnessSizeConfirmed = 1 + 1 + 1 + 73 + 1 + 73 + 1 + 1 +
|
|
|
|
OfferedHtlcScriptSizeConfirmed
|
|
|
|
|
2020-11-17 13:23:00 +01:00
|
|
|
// OfferedHtlcPenaltyWitnessSize 243 bytes
|
2017-08-30 07:14:04 +02:00
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - revocation_sig_length: 1 byte
|
|
|
|
// - revocation_sig: 73 bytes
|
|
|
|
// - revocation_key_length: 1 byte
|
|
|
|
// - revocation_key: 33 bytes
|
|
|
|
// - witness_script_length: 1 byte
|
|
|
|
// - witness_script (offered_htlc_script)
|
lnwallet/size: correct commit to-local and 2nd stage script sizes
In this commit, we correct our size estimates for to-local scripts,
which are used on the commitment transaction and the htlc
success/timeout transactions. There have been observed cases of
transactions getting stuck because our estimates were too low, and cause
the transactions to not be relayed.
Our previous estimate for the commitment to-local script was derived
from an older version of the script. Though the estimate is greater than
the actual size, this has been updated with the current estimate of 79
bytes.
This estimates makes the assumption that CSV delays will be at most
4 bytes when serialized. Since this value is expressed in relative block
heights, this should be more than sufficient for our needs, even though
the maximum possible size for the little-endian int64 is 9 bytes (plus
an OP_DATA).
The other correction is to use the ToLocalScriptSize as our estimate for
htlc timeout/success scripts, as they are the same script. Previously,
our estimate was derived from the proper script, though we were 6 bytes
shy of the new to-local estimate, since we counted the csv_delay as 1
byte, and missed some other OP_DATAs.
All derived estimates have been updating depending on the new and
improved ToLocalScriptSize estimate, and fix some estimates that did not
include the witness length in the estimate.
Finally, we correct some weight miscalculations in:
- AcceptedHtlcTimeoutWitnessSize: missing data push lengths
- OfferedHtlcSuccessWitnessSize: extra 73 byte sig, missing data push lengths
- OfferedHtlcPenaltyWitnessSize: missing 33 byte pubkey
2018-07-25 05:30:48 +02:00
|
|
|
OfferedHtlcPenaltyWitnessSize = 1 + 1 + 73 + 1 + 33 + 1 + OfferedHtlcScriptSize
|
2016-11-23 09:29:05 +01:00
|
|
|
|
2022-02-07 13:58:28 +01:00
|
|
|
// OfferedHtlcPenaltyWitnessSizeConfirmed 246 bytes.
|
2020-11-17 13:23:00 +01:00
|
|
|
OfferedHtlcPenaltyWitnessSizeConfirmed = 1 + 1 + 73 + 1 + 33 + 1 +
|
|
|
|
OfferedHtlcScriptSizeConfirmed
|
|
|
|
|
2020-04-06 02:07:14 +02:00
|
|
|
// AnchorScriptSize 40 bytes
|
|
|
|
// - pubkey_length: 1 byte
|
|
|
|
// - pubkey: 33 bytes
|
|
|
|
// - OP_CHECKSIG: 1 byte
|
|
|
|
// - OP_IFDUP: 1 byte
|
|
|
|
// - OP_NOTIF: 1 byte
|
|
|
|
// - OP_16: 1 byte
|
|
|
|
// - OP_CSV 1 byte
|
|
|
|
// - OP_ENDIF: 1 byte
|
|
|
|
AnchorScriptSize = 1 + 33 + 6*1
|
2020-03-10 13:23:17 +01:00
|
|
|
|
|
|
|
// AnchorWitnessSize 116 bytes
|
2020-04-06 02:07:14 +02:00
|
|
|
// - number_of_witnes_elements: 1 byte
|
|
|
|
// - signature_length: 1 byte
|
|
|
|
// - signature: 73 bytes
|
|
|
|
// - witness_script_length: 1 byte
|
|
|
|
// - witness_script (anchor_script)
|
|
|
|
AnchorWitnessSize = 1 + 1 + 73 + 1 + AnchorScriptSize
|
2022-03-18 18:37:46 +01:00
|
|
|
|
2022-03-18 18:37:50 +01:00
|
|
|
// TaprootSignatureWitnessSize 65 bytes
|
|
|
|
// - sigLength: 1 byte
|
|
|
|
// - sig: 64 bytes
|
|
|
|
TaprootSignatureWitnessSize = 1 + 64
|
|
|
|
|
2022-03-18 18:37:46 +01:00
|
|
|
// TaprootKeyPathWitnessSize 66 bytes
|
|
|
|
// - NumberOfWitnessElements: 1 byte
|
|
|
|
// - sigLength: 1 byte
|
|
|
|
// - sig: 64 bytes
|
2022-03-18 18:37:50 +01:00
|
|
|
TaprootKeyPathWitnessSize = 1 + TaprootSignatureWitnessSize
|
2022-03-18 18:37:46 +01:00
|
|
|
|
|
|
|
// TaprootKeyPathCustomSighashWitnessSize 67 bytes
|
|
|
|
// - NumberOfWitnessElements: 1 byte
|
|
|
|
// - sigLength: 1 byte
|
|
|
|
// - sig: 64 bytes
|
|
|
|
// - sighashFlag: 1 byte
|
|
|
|
TaprootKeyPathCustomSighashWitnessSize = TaprootKeyPathWitnessSize + 1
|
2022-03-18 18:37:50 +01:00
|
|
|
|
|
|
|
// TaprootBaseControlBlockWitnessSize 33 bytes
|
|
|
|
// - leafVersionAndParity: 1 byte
|
|
|
|
// - schnorrPubKey: 32 byte
|
|
|
|
TaprootBaseControlBlockWitnessSize = 33
|
2023-03-02 06:48:28 +01:00
|
|
|
|
|
|
|
// TaprootToLocalScriptSize
|
|
|
|
// - OP_DATA: 1 byte (pub key len)
|
|
|
|
// - local_key: 32 bytes
|
|
|
|
// - OP_CHECKSIG: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (csv delay)
|
|
|
|
// - csv_delay: 4 bytes (worst case estimate)
|
|
|
|
// - OP_CSV: 1 byte
|
|
|
|
// - OP_DROP: 1 byte
|
|
|
|
TaprootToLocalScriptSize = 41
|
|
|
|
|
|
|
|
// TaprootToLocalWitnessSize: 175 bytes
|
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - sig_len: 1 byte
|
|
|
|
// - sweep_sig: 65 bytes (worst case w/o sighash default)
|
|
|
|
// - script_len: 1 byte
|
|
|
|
// - taproot_to_local_script_size: 41 bytes
|
|
|
|
// - ctrl_block_len: 1 byte
|
|
|
|
// - base_control_block_size: 33 bytes
|
|
|
|
// - sibling_merkle_hash: 32 bytes
|
|
|
|
TaprootToLocalWitnessSize = 1 + 1 + 65 + 1 + TaprootToLocalScriptSize +
|
|
|
|
1 + TaprootBaseControlBlockWitnessSize + 32
|
|
|
|
|
|
|
|
// TaprootToLocalRevokeScriptSize: 68 bytes
|
|
|
|
// - OP_DATA: 1 byte
|
|
|
|
// - local key: 32 bytes
|
|
|
|
// - OP_DROP: 1 byte
|
|
|
|
// - OP_DATA: 1 byte
|
|
|
|
// - revocation key: 32 bytes
|
|
|
|
// - OP_CHECKSIG: 1 byte
|
|
|
|
TaprootToLocalRevokeScriptSize = 1 + 32 + 1 + 1 + 32 + 1
|
|
|
|
|
|
|
|
// TaprootToLocalRevokeWitnessSize: 202 bytes
|
|
|
|
// - NumberOfWitnessElements: 1 byte
|
|
|
|
// - sigLength: 1 byte
|
|
|
|
// - sweep sig: 65 bytes
|
|
|
|
// - script len: 1 byte
|
|
|
|
// - revocation script size: 68 bytes
|
|
|
|
// - ctrl block size: 1 byte
|
|
|
|
// - base control block: 33 bytes
|
|
|
|
// - merkle proof: 32
|
2023-08-09 07:22:12 +02:00
|
|
|
TaprootToLocalRevokeWitnessSize = (1 + 1 + 65 + 1 +
|
|
|
|
TaprootToLocalRevokeScriptSize + 1 + 33 + 32)
|
2023-03-02 06:48:28 +01:00
|
|
|
|
|
|
|
// TaprootToRemoteScriptSize
|
|
|
|
// - OP_DATA: 1 byte
|
|
|
|
// - remote key: 32 bytes
|
|
|
|
// - OP_CHECKSIG: 1 byte
|
|
|
|
// - OP_1: 1 byte
|
|
|
|
// - OP_CHECKSEQUENCEVERIFY: 1 byte
|
|
|
|
// - OP_DROP: 1 byte
|
|
|
|
TaprootToRemoteScriptSize = 1 + 32 + 1 + 1 + 1 + 1
|
|
|
|
|
|
|
|
// TaprootToRemoteWitnessSize:
|
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - sig_len: 1 byte
|
|
|
|
// - sweep_sig: 65 bytes (worst case w/o sighash default)
|
|
|
|
// - script_len: 1 byte
|
|
|
|
// - taproot_to_local_script_size: 36 bytes
|
|
|
|
// - ctrl_block_len: 1 byte
|
|
|
|
// - base_control_block_size: 33 bytes
|
2023-08-09 07:22:12 +02:00
|
|
|
TaprootToRemoteWitnessSize = (1 + 1 + 65 + 1 +
|
|
|
|
TaprootToRemoteScriptSize + 1 +
|
|
|
|
TaprootBaseControlBlockWitnessSize)
|
2023-03-02 06:48:28 +01:00
|
|
|
|
|
|
|
// TaprootAnchorWitnessSize: 67 bytes
|
|
|
|
//
|
|
|
|
// In this case, we use the custom sighash size to give the most
|
|
|
|
// pessemistic estimate.
|
|
|
|
TaprootAnchorWitnessSize = TaprootKeyPathCustomSighashWitnessSize
|
|
|
|
|
|
|
|
// TaprootSecondLevelHtlcScriptSize: 41 bytes
|
|
|
|
// - OP_DATA: 1 byte (pub key len)
|
|
|
|
// - local_key: 32 bytes
|
|
|
|
// - OP_CHECKSIG: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (csv delay)
|
|
|
|
// - csv_delay: 4 bytes (worst case)
|
|
|
|
// - OP_CSV: 1 byte
|
|
|
|
// - OP_DROP: 1 byte
|
|
|
|
TaprootSecondLevelHtlcScriptSize = 1 + 32 + 1 + 1 + 4 + 1 + 1
|
|
|
|
|
|
|
|
// TaprootSecondLevelHtlcWitnessSize:
|
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - sig_len: 1 byte
|
|
|
|
// - sweep_sig: 65 bytes (worst case w/o sighash default)
|
|
|
|
// - script_len: 1 byte
|
|
|
|
// - taproot_second_level_htlc_script_size: 40 bytes
|
|
|
|
// - ctrl_block_len: 1 byte
|
|
|
|
// - base_control_block_size: 33 bytes
|
|
|
|
TaprootSecondLevelHtlcWitnessSize = 1 + 1 + 65 + 1 +
|
|
|
|
TaprootSecondLevelHtlcScriptSize + 1 +
|
|
|
|
TaprootBaseControlBlockWitnessSize
|
|
|
|
|
|
|
|
// TaprootSecondLevelRevokeWitnessSize
|
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - sig_len: 1 byte
|
|
|
|
// - sweep_sig: 65 bytes (worst case w/o sighash default)
|
2023-08-09 07:22:12 +02:00
|
|
|
//nolint:lll
|
2023-03-02 06:48:28 +01:00
|
|
|
TaprootSecondLevelRevokeWitnessSize = TaprootKeyPathCustomSighashWitnessSize
|
|
|
|
|
|
|
|
// TaprootAcceptedRevokeWitnessSize:
|
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - sig_len: 1 byte
|
|
|
|
// - sweep_sig: 65 bytes (worst case w/o sighash default)
|
2023-08-09 07:22:12 +02:00
|
|
|
//nolint:lll
|
2023-03-02 06:48:28 +01:00
|
|
|
TaprootAcceptedRevokeWitnessSize = TaprootKeyPathCustomSighashWitnessSize
|
|
|
|
|
|
|
|
// TaprootOfferedRevokeWitnessSize:
|
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - sig_len: 1 byte
|
|
|
|
// - sweep_sig: 65 bytes (worst case w/o sighash default)
|
|
|
|
TaprootOfferedRevokeWitnessSize = TaprootKeyPathCustomSighashWitnessSize
|
|
|
|
|
|
|
|
// TaprootHtlcOfferedRemoteTimeoutScriptSize: 42 bytes
|
|
|
|
// - OP_DATA: 1 byte (pub key len)
|
|
|
|
// - local_key: 32 bytes
|
|
|
|
// - OP_CHECKSIG: 1 byte
|
|
|
|
// - OP_1: 1 byte
|
|
|
|
// - OP_DROP: 1 byte
|
|
|
|
// - OP_CHECKSEQUENCEVERIFY: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (cltv_expiry length)
|
|
|
|
// - cltv_expiry: 4 bytes
|
|
|
|
// - OP_CHECKLOCKTIMEVERIFY: 1 byte
|
|
|
|
// - OP_DROP: 1 byte
|
2023-08-09 07:22:12 +02:00
|
|
|
TaprootHtlcOfferedRemoteTimeoutScriptSize = (1 + 32 + 1 + 1 + 1 + 1 +
|
|
|
|
1 + 4 + 1 + 1)
|
2023-03-02 06:48:28 +01:00
|
|
|
|
|
|
|
// TaprootHtlcOfferedRemoteTimeoutwitSize: 176 bytes
|
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - sig_len: 1 byte
|
|
|
|
// - sweep_sig: 65 bytes (worst case w/o sighash default)
|
|
|
|
// - script_len: 1 byte
|
|
|
|
// - taproot_offered_htlc_script_size: 42 bytes
|
|
|
|
// - ctrl_block_len: 1 byte
|
|
|
|
// - base_control_block_size: 33 bytes
|
|
|
|
// - sibilng_merkle_proof: 32 bytes
|
|
|
|
TaprootHtlcOfferedRemoteTimeoutWitnessSize = 1 + 1 + 65 + 1 +
|
|
|
|
TaprootHtlcOfferedRemoteTimeoutScriptSize + 1 +
|
|
|
|
TaprootBaseControlBlockWitnessSize + 32
|
|
|
|
|
|
|
|
// TaprootHtlcOfferedLocalTmeoutScriptSize:
|
|
|
|
// - OP_DATA: 1 byte (pub key len)
|
|
|
|
// - local_key: 32 bytes
|
|
|
|
// - OP_CHECKSIGVERIFY: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (pub key len)
|
|
|
|
// - remote_key: 32 bytes
|
|
|
|
// - OP_CHECKSIG: 1 byte
|
|
|
|
TaprootHtlcOfferedLocalTimeoutScriptSize = 1 + 32 + 1 + 1 + 32 + 1
|
|
|
|
|
|
|
|
// TaprootOfferedLocalTimeoutWitnessSize
|
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - sig_len: 1 byte
|
|
|
|
// - sweep_sig: 65 bytes (worst case w/o sighash default)
|
|
|
|
// - sig_len: 1 byte
|
|
|
|
// - sweep_sig: 65 bytes (worst case w/o sighash default)
|
|
|
|
// - script_len: 1 byte
|
|
|
|
// - taproot_offered_htlc_script_timeout_size:
|
|
|
|
// - ctrl_block_len: 1 byte
|
|
|
|
// - base_control_block_size: 33 bytes
|
|
|
|
// - sibilng_merkle_proof: 32 bytes
|
|
|
|
TaprootOfferedLocalTimeoutWitnessSize = 1 + 1 + 65 + 1 + 65 + 1 +
|
|
|
|
TaprootHtlcOfferedLocalTimeoutScriptSize + 1 +
|
|
|
|
TaprootBaseControlBlockWitnessSize + 32
|
|
|
|
|
|
|
|
// TaprootHtlcAcceptedRemoteSuccessScriptSize:
|
|
|
|
// - OP_SIZE: 1 byte
|
|
|
|
// - OP_DATA: 1 byte
|
|
|
|
// - 32: 1 byte
|
|
|
|
// - OP_EQUALVERIFY: 1 byte
|
|
|
|
// - OP_HASH160: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (RIPEMD160(payment_hash) length)
|
|
|
|
// - RIPEMD160(payment_hash): 20 bytes
|
|
|
|
// - OP_EQUALVERIFY: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (pub key len)
|
|
|
|
// - remote_key: 32 bytes
|
|
|
|
// - OP_CHECKSIG: 1 byte
|
|
|
|
// - OP_1: 1 byte
|
|
|
|
// - OP_CSV: 1 byte
|
|
|
|
// - OP_DROP: 1 byte
|
|
|
|
TaprootHtlcAcceptedRemoteSuccessScriptSize = 1 + 1 + 1 + 1 + 1 + 1 +
|
|
|
|
1 + 20 + 1 + 32 + 1 + 1 + 1 + 1
|
|
|
|
|
|
|
|
// TaprootHtlcAcceptedRemoteSuccessScriptSize:
|
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - sig_len: 1 byte
|
|
|
|
// - sweep_sig: 65 bytes (worst case w/o sighash default)
|
|
|
|
// - payment_preimage_length: 1 byte
|
|
|
|
// - payment_preimage: 32 bytes
|
|
|
|
// - script_len: 1 byte
|
|
|
|
// - taproot_offered_htlc_script_success_size:
|
|
|
|
// - ctrl_block_len: 1 byte
|
|
|
|
// - base_control_block_size: 33 bytes
|
|
|
|
// - sibilng_merkle_proof: 32 bytes
|
|
|
|
TaprootHtlcAcceptedRemoteSuccessWitnessSize = 1 + 1 + 65 + 1 + 32 + 1 +
|
|
|
|
TaprootHtlcAcceptedRemoteSuccessScriptSize + 1 +
|
|
|
|
TaprootBaseControlBlockWitnessSize + 32
|
|
|
|
|
|
|
|
// TaprootHtlcAcceptedLocalSuccessScriptSize:
|
|
|
|
// - OP_SIZE: 1 byte
|
|
|
|
// - OP_DATA: 1 byte
|
|
|
|
// - 32: 1 byte
|
|
|
|
// - OP_EQUALVERIFY: 1 byte
|
|
|
|
// - OP_HASH160: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (RIPEMD160(payment_hash) length)
|
|
|
|
// - RIPEMD160(payment_hash): 20 bytes
|
|
|
|
// - OP_EQUALVERIFY: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (pub key len)
|
|
|
|
// - local_key: 32 bytes
|
|
|
|
// - OP_CHECKSIGVERIFY: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (pub key len)
|
|
|
|
// - remote_key: 32 bytes
|
|
|
|
// - OP_CHECKSIG: 1 byte
|
|
|
|
TaprootHtlcAcceptedLocalSuccessScriptSize = 1 + 1 + 1 + 1 + 1 + 1 +
|
|
|
|
20 + 1 + 1 + 32 + 1 + 1 + 32 + 1
|
|
|
|
|
|
|
|
// TaprootHtlcAcceptedLocalSuccessWitnessSize:
|
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - sig_len: 1 byte
|
|
|
|
// - sweep_sig: 65 bytes (worst case w/o sighash default)
|
|
|
|
// - sig_len: 1 byte
|
|
|
|
// - sweep_sig: 65 bytes (worst case w/o sighash default)
|
|
|
|
// - payment_preimage_length: 1 byte
|
|
|
|
// - payment_preimage: 32 bytes
|
|
|
|
// - script_len: 1 byte
|
|
|
|
// - taproot_accepted_htlc_script_success_size:
|
|
|
|
// - ctrl_block_len: 1 byte
|
|
|
|
// - base_control_block_size: 33 bytes
|
|
|
|
// - sibilng_merkle_proof: 32 bytes
|
|
|
|
TaprootHtlcAcceptedLocalSuccessWitnessSize = 1 + 1 + 65 + 1 + 65 + 1 +
|
|
|
|
32 + 1 + TaprootHtlcAcceptedLocalSuccessScriptSize + 1 +
|
|
|
|
TaprootBaseControlBlockWitnessSize + 32
|
2020-03-10 13:23:17 +01:00
|
|
|
)
|
|
|
|
|
2019-01-16 15:47:43 +01:00
|
|
|
// EstimateCommitTxWeight estimate commitment transaction weight depending on
|
2017-05-17 03:50:50 +02:00
|
|
|
// the precalculated weight of base transaction, witness data, which is needed
|
|
|
|
// for paying for funding tx, and htlc weight multiplied by their count.
|
2019-01-16 15:47:43 +01:00
|
|
|
func EstimateCommitTxWeight(count int, prediction bool) int64 {
|
2016-11-23 09:29:05 +01:00
|
|
|
// Make prediction about the size of commitment transaction with
|
|
|
|
// additional HTLC.
|
|
|
|
if prediction {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
|
2017-05-17 03:50:50 +02:00
|
|
|
htlcWeight := int64(count * HTLCWeight)
|
|
|
|
baseWeight := int64(BaseCommitmentTxWeight)
|
|
|
|
witnessWeight := int64(WitnessCommitmentTxWeight)
|
2016-11-23 09:29:05 +01:00
|
|
|
|
2023-03-02 06:48:28 +01:00
|
|
|
// TODO(roasbeef): need taproot modifier? also no anchor so wrong?
|
|
|
|
|
2017-05-17 03:50:50 +02:00
|
|
|
return htlcWeight + baseWeight + witnessWeight
|
2016-11-29 04:43:57 +01:00
|
|
|
}
|
2017-09-26 01:35:19 +02:00
|
|
|
|
|
|
|
// TxWeightEstimator is able to calculate weight estimates for transactions
|
|
|
|
// based on the input and output types. For purposes of estimation, all
|
2018-01-17 03:02:46 +01:00
|
|
|
// signatures are assumed to be of the maximum possible size, 73 bytes. Each
|
|
|
|
// method of the estimator returns an instance with the estimate applied. This
|
|
|
|
// allows callers to chain each of the methods
|
2017-09-26 01:35:19 +02:00
|
|
|
type TxWeightEstimator struct {
|
|
|
|
hasWitness bool
|
|
|
|
inputCount uint32
|
|
|
|
outputCount uint32
|
|
|
|
inputSize int
|
2024-05-24 15:56:30 +02:00
|
|
|
inputWitnessSize lntypes.WeightUnit
|
2017-09-26 01:35:19 +02:00
|
|
|
outputSize int
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddP2PKHInput updates the weight estimate to account for an additional input
|
|
|
|
// spending a P2PKH output.
|
2018-01-17 03:02:46 +01:00
|
|
|
func (twe *TxWeightEstimator) AddP2PKHInput() *TxWeightEstimator {
|
2017-09-26 01:35:19 +02:00
|
|
|
twe.inputSize += InputSize + P2PKHScriptSigSize
|
|
|
|
twe.inputWitnessSize++
|
|
|
|
twe.inputCount++
|
2018-01-17 03:02:46 +01:00
|
|
|
|
|
|
|
return twe
|
2017-09-26 01:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddP2WKHInput updates the weight estimate to account for an additional input
|
2017-10-03 04:57:22 +02:00
|
|
|
// spending a native P2PWKH output.
|
2018-01-17 03:02:46 +01:00
|
|
|
func (twe *TxWeightEstimator) AddP2WKHInput() *TxWeightEstimator {
|
2017-09-26 05:13:53 +02:00
|
|
|
twe.AddWitnessInput(P2WKHWitnessSize)
|
2018-01-17 03:02:46 +01:00
|
|
|
|
|
|
|
return twe
|
2017-09-26 05:13:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddWitnessInput updates the weight estimate to account for an additional
|
2017-10-03 04:57:22 +02:00
|
|
|
// input spending a native pay-to-witness output. This accepts the total size
|
|
|
|
// of the witness as a parameter.
|
2024-05-24 15:56:30 +02:00
|
|
|
func (twe *TxWeightEstimator) AddWitnessInput(
|
|
|
|
witnessSize lntypes.WeightUnit) *TxWeightEstimator {
|
|
|
|
|
2017-09-26 01:35:19 +02:00
|
|
|
twe.inputSize += InputSize
|
2017-09-26 05:13:53 +02:00
|
|
|
twe.inputWitnessSize += witnessSize
|
2017-09-26 01:35:19 +02:00
|
|
|
twe.inputCount++
|
|
|
|
twe.hasWitness = true
|
2018-01-17 03:02:46 +01:00
|
|
|
|
|
|
|
return twe
|
2017-09-26 01:35:19 +02:00
|
|
|
}
|
|
|
|
|
2022-03-18 18:37:50 +01:00
|
|
|
// AddTapscriptInput updates the weight estimate to account for an additional
|
|
|
|
// input spending a segwit v1 pay-to-taproot output using the script path. This
|
|
|
|
// accepts the total size of the witness for the script leaf that is executed
|
|
|
|
// and adds the size of the control block to the total witness size.
|
|
|
|
//
|
|
|
|
// NOTE: The leaf witness size must be calculated without the byte that accounts
|
|
|
|
// for the number of witness elements, only the total size of all elements on
|
|
|
|
// the stack that are consumed by the revealed script should be counted.
|
2024-05-24 15:56:30 +02:00
|
|
|
func (twe *TxWeightEstimator) AddTapscriptInput(
|
|
|
|
leafWitnessSize lntypes.WeightUnit,
|
2022-03-18 18:37:50 +01:00
|
|
|
tapscript *waddrmgr.Tapscript) *TxWeightEstimator {
|
|
|
|
|
|
|
|
// We add 1 byte for the total number of witness elements.
|
|
|
|
controlBlockWitnessSize := 1 + TaprootBaseControlBlockWitnessSize +
|
|
|
|
// 1 byte for the length of the element plus the element itself.
|
|
|
|
1 + len(tapscript.RevealedScript) +
|
|
|
|
1 + len(tapscript.ControlBlock.InclusionProof)
|
|
|
|
|
|
|
|
twe.inputSize += InputSize
|
2024-05-24 15:56:30 +02:00
|
|
|
twe.inputWitnessSize += leafWitnessSize + lntypes.WeightUnit(
|
|
|
|
controlBlockWitnessSize,
|
|
|
|
)
|
2022-03-18 18:37:50 +01:00
|
|
|
twe.inputCount++
|
|
|
|
twe.hasWitness = true
|
|
|
|
|
|
|
|
return twe
|
|
|
|
}
|
|
|
|
|
2022-03-18 18:37:51 +01:00
|
|
|
// AddTaprootKeySpendInput updates the weight estimate to account for an
|
|
|
|
// additional input spending a segwit v1 pay-to-taproot output using the key
|
|
|
|
// spend path. This accepts the sighash type being used since that has an
|
|
|
|
// influence on the total size of the signature.
|
|
|
|
func (twe *TxWeightEstimator) AddTaprootKeySpendInput(
|
|
|
|
hashType txscript.SigHashType) *TxWeightEstimator {
|
|
|
|
|
|
|
|
twe.inputSize += InputSize
|
|
|
|
|
|
|
|
if hashType == txscript.SigHashDefault {
|
|
|
|
twe.inputWitnessSize += TaprootKeyPathWitnessSize
|
|
|
|
} else {
|
|
|
|
twe.inputWitnessSize += TaprootKeyPathCustomSighashWitnessSize
|
|
|
|
}
|
|
|
|
|
|
|
|
twe.inputCount++
|
|
|
|
twe.hasWitness = true
|
|
|
|
|
|
|
|
return twe
|
|
|
|
}
|
|
|
|
|
2017-10-03 04:57:22 +02:00
|
|
|
// AddNestedP2WKHInput updates the weight estimate to account for an additional
|
|
|
|
// input spending a P2SH output with a nested P2WKH redeem script.
|
2018-01-17 03:02:46 +01:00
|
|
|
func (twe *TxWeightEstimator) AddNestedP2WKHInput() *TxWeightEstimator {
|
2020-03-04 17:00:33 +01:00
|
|
|
twe.inputSize += InputSize + NestedP2WPKHSize
|
2017-10-03 04:57:22 +02:00
|
|
|
twe.inputWitnessSize += P2WKHWitnessSize
|
2020-03-04 17:00:33 +01:00
|
|
|
twe.inputCount++
|
2017-10-03 04:57:22 +02:00
|
|
|
twe.hasWitness = true
|
2018-01-17 03:02:46 +01:00
|
|
|
|
|
|
|
return twe
|
2017-10-03 04:57:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddNestedP2WSHInput updates the weight estimate to account for an additional
|
|
|
|
// input spending a P2SH output with a nested P2WSH redeem script.
|
2024-05-24 15:56:30 +02:00
|
|
|
func (twe *TxWeightEstimator) AddNestedP2WSHInput(
|
|
|
|
witnessSize lntypes.WeightUnit) *TxWeightEstimator {
|
|
|
|
|
2020-03-04 17:00:33 +01:00
|
|
|
twe.inputSize += InputSize + NestedP2WSHSize
|
2017-10-03 04:57:22 +02:00
|
|
|
twe.inputWitnessSize += witnessSize
|
2020-03-04 17:00:33 +01:00
|
|
|
twe.inputCount++
|
2017-10-03 04:57:22 +02:00
|
|
|
twe.hasWitness = true
|
2018-01-17 03:02:46 +01:00
|
|
|
|
|
|
|
return twe
|
2017-10-03 04:57:22 +02:00
|
|
|
}
|
|
|
|
|
2020-11-06 19:41:56 +01:00
|
|
|
// AddTxOutput adds a known TxOut to the weight estimator.
|
|
|
|
func (twe *TxWeightEstimator) AddTxOutput(txOut *wire.TxOut) *TxWeightEstimator {
|
|
|
|
twe.outputSize += txOut.SerializeSize()
|
|
|
|
twe.outputCount++
|
|
|
|
|
|
|
|
return twe
|
|
|
|
}
|
|
|
|
|
2017-09-26 01:35:19 +02:00
|
|
|
// AddP2PKHOutput updates the weight estimate to account for an additional P2PKH
|
|
|
|
// output.
|
2018-01-17 03:02:46 +01:00
|
|
|
func (twe *TxWeightEstimator) AddP2PKHOutput() *TxWeightEstimator {
|
2017-09-26 01:35:19 +02:00
|
|
|
twe.outputSize += P2PKHOutputSize
|
|
|
|
twe.outputCount++
|
2018-01-17 03:02:46 +01:00
|
|
|
|
|
|
|
return twe
|
2017-09-26 01:35:19 +02:00
|
|
|
}
|
|
|
|
|
2017-10-03 04:57:22 +02:00
|
|
|
// AddP2WKHOutput updates the weight estimate to account for an additional
|
|
|
|
// native P2WKH output.
|
2018-01-17 03:02:46 +01:00
|
|
|
func (twe *TxWeightEstimator) AddP2WKHOutput() *TxWeightEstimator {
|
2017-09-26 01:35:19 +02:00
|
|
|
twe.outputSize += P2WKHOutputSize
|
|
|
|
twe.outputCount++
|
2018-01-17 03:02:46 +01:00
|
|
|
|
|
|
|
return twe
|
2017-09-26 01:35:19 +02:00
|
|
|
}
|
|
|
|
|
2017-10-03 04:57:22 +02:00
|
|
|
// AddP2WSHOutput updates the weight estimate to account for an additional
|
|
|
|
// native P2WSH output.
|
2018-01-17 03:02:46 +01:00
|
|
|
func (twe *TxWeightEstimator) AddP2WSHOutput() *TxWeightEstimator {
|
2017-09-26 01:35:19 +02:00
|
|
|
twe.outputSize += P2WSHOutputSize
|
|
|
|
twe.outputCount++
|
2018-01-17 03:02:46 +01:00
|
|
|
|
|
|
|
return twe
|
2017-09-26 01:35:19 +02:00
|
|
|
}
|
|
|
|
|
2022-03-18 18:37:46 +01:00
|
|
|
// AddP2TROutput updates the weight estimate to account for an additional native
|
|
|
|
// SegWit v1 P2TR output.
|
|
|
|
func (twe *TxWeightEstimator) AddP2TROutput() *TxWeightEstimator {
|
|
|
|
twe.outputSize += P2TROutputSize
|
|
|
|
twe.outputCount++
|
|
|
|
|
|
|
|
return twe
|
|
|
|
}
|
|
|
|
|
2017-10-03 04:57:22 +02:00
|
|
|
// AddP2SHOutput updates the weight estimate to account for an additional P2SH
|
|
|
|
// output.
|
2018-01-17 03:02:46 +01:00
|
|
|
func (twe *TxWeightEstimator) AddP2SHOutput() *TxWeightEstimator {
|
2017-10-03 04:57:22 +02:00
|
|
|
twe.outputSize += P2SHOutputSize
|
|
|
|
twe.outputCount++
|
2018-01-17 03:02:46 +01:00
|
|
|
|
|
|
|
return twe
|
2017-10-03 04:57:22 +02:00
|
|
|
}
|
|
|
|
|
2024-02-06 12:25:53 +01:00
|
|
|
// AddOutput estimates the weight of an output based on the pkScript.
|
|
|
|
func (twe *TxWeightEstimator) AddOutput(pkScript []byte) *TxWeightEstimator {
|
|
|
|
twe.outputSize += BaseOutputSize + len(pkScript)
|
|
|
|
twe.outputCount++
|
|
|
|
|
|
|
|
return twe
|
|
|
|
}
|
|
|
|
|
2017-09-26 01:35:19 +02:00
|
|
|
// Weight gets the estimated weight of the transaction.
|
2024-05-24 15:56:30 +02:00
|
|
|
func (twe *TxWeightEstimator) Weight() lntypes.WeightUnit {
|
2017-09-26 01:35:19 +02:00
|
|
|
txSizeStripped := BaseTxSize +
|
|
|
|
wire.VarIntSerializeSize(uint64(twe.inputCount)) + twe.inputSize +
|
|
|
|
wire.VarIntSerializeSize(uint64(twe.outputCount)) + twe.outputSize
|
2024-05-24 15:56:30 +02:00
|
|
|
weight := lntypes.WeightUnit(txSizeStripped * witnessScaleFactor)
|
|
|
|
|
2017-09-26 01:35:19 +02:00
|
|
|
if twe.hasWitness {
|
|
|
|
weight += WitnessHeaderSize + twe.inputWitnessSize
|
|
|
|
}
|
|
|
|
return weight
|
|
|
|
}
|
2018-02-13 14:34:44 +01:00
|
|
|
|
|
|
|
// VSize gets the estimated virtual size of the transactions, in vbytes.
|
|
|
|
func (twe *TxWeightEstimator) VSize() int {
|
|
|
|
// A tx's vsize is 1/4 of the weight, rounded up.
|
2024-05-24 15:56:30 +02:00
|
|
|
return int(twe.Weight().ToVB())
|
2018-02-13 14:34:44 +01:00
|
|
|
}
|