2019-01-16 17:47:43 +03:00
|
|
|
package input
|
2016-11-23 11:29:05 +03:00
|
|
|
|
|
|
|
import (
|
2020-03-10 15:23:17 +03:00
|
|
|
"math/big"
|
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/blockchain"
|
2020-03-10 15:23:17 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2020-03-10 15:23:17 +03:00
|
|
|
"github.com/lightningnetwork/lnd/keychain"
|
2016-11-23 11:29:05 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-09-26 02:35:19 +03: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 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-10-03 05:57:22 +03:00
|
|
|
// P2WPKHSize 22 bytes
|
|
|
|
// - OP_0: 1 byte
|
|
|
|
// - OP_DATA: 1 byte (PublicKeyHASH160 length)
|
|
|
|
// - PublicKeyHASH160: 20 bytes
|
|
|
|
P2WPKHSize = 1 + 1 + 20
|
|
|
|
|
2020-03-04 19:00:33 +03:00
|
|
|
// NestedP2WPKHSize 23 bytes
|
|
|
|
// - OP_DATA: 1 byte (P2WPKHSize)
|
|
|
|
// - P2WPKHWitnessProgram: 22 bytes
|
|
|
|
NestedP2WPKHSize = 1 + P2WPKHSize
|
|
|
|
|
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
|
|
|
|
|
2020-03-04 19:00:33 +03:00
|
|
|
// NestedP2WSHSize 35 bytes
|
|
|
|
// - OP_DATA: 1 byte (P2WSHSize)
|
2020-03-10 04:34:09 +03:00
|
|
|
// - P2WSHWitnessProgram: 34 bytes
|
2020-03-04 19:00:33 +03:00
|
|
|
NestedP2WSHSize = 1 + P2WSHSize
|
|
|
|
|
2017-09-26 02:35:19 +03:00
|
|
|
// P2PKHOutputSize 34 bytes
|
|
|
|
// - value: 8 bytes
|
|
|
|
// - var_int: 1 byte (pkscript_length)
|
|
|
|
// - pkscript (p2pkh): 25 bytes
|
|
|
|
P2PKHOutputSize = 8 + 1 + 25
|
|
|
|
|
2017-08-30 08:14:04 +03:00
|
|
|
// P2WKHOutputSize 31 bytes
|
|
|
|
// - value: 8 bytes
|
|
|
|
// - var_int: 1 byte (pkscript_length)
|
|
|
|
// - pkscript (p2wpkh): 22 bytes
|
2017-10-03 05:57:22 +03:00
|
|
|
P2WKHOutputSize = 8 + 1 + P2WPKHSize
|
2017-08-30 08:14:04 +03:00
|
|
|
|
|
|
|
// P2WSHOutputSize 43 bytes
|
|
|
|
// - value: 8 bytes
|
|
|
|
// - var_int: 1 byte (pkscript_length)
|
|
|
|
// - pkscript (p2wsh): 34 bytes
|
2017-10-03 05:57:22 +03:00
|
|
|
P2WSHOutputSize = 8 + 1 + P2WSHSize
|
2017-08-30 08:14:04 +03:00
|
|
|
|
2017-10-03 05:57:22 +03:00
|
|
|
// P2SHOutputSize 32 bytes
|
|
|
|
// - value: 8 bytes
|
|
|
|
// - var_int: 1 byte (pkscript_length)
|
|
|
|
// - pkscript (p2sh): 23 bytes
|
|
|
|
P2SHOutputSize = 8 + 1 + 23
|
2016-11-23 11:29:05 +03:00
|
|
|
|
2017-09-26 02:35:19 +03:00
|
|
|
// P2PKHScriptSigSize 108 bytes
|
2017-08-30 08:14:04 +03:00
|
|
|
// - OP_DATA: 1 byte (signature length)
|
|
|
|
// - signature
|
|
|
|
// - OP_DATA: 1 byte (pubkey length)
|
|
|
|
// - pubkey
|
2017-09-26 02:35:19 +03: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 08:14:04 +03:00
|
|
|
|
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-08-30 08:14:04 +03:00
|
|
|
// InputSize 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
|
2017-08-30 08:14:04 +03:00
|
|
|
InputSize = 32 + 4 + 1 + 4
|
|
|
|
|
|
|
|
// 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 11:29:05 +03:00
|
|
|
|
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
|
|
|
|
|
2020-03-06 18:11:46 +03:00
|
|
|
// CommitmentAnchorOutput 43 bytes
|
|
|
|
// - Value: 8 bytes
|
|
|
|
// - VarInt: 1 byte (PkScript length)
|
|
|
|
// - PkScript (P2WSH)
|
|
|
|
CommitmentAnchorOutput = 8 + 1 + P2WSHSize
|
|
|
|
|
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-09-26 02:35:19 +03: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 06:30:48 +03:00
|
|
|
// - Version: 4 bytes
|
|
|
|
// - LockTime: 4 bytes
|
2017-09-26 02:35:19 +03:00
|
|
|
BaseTxSize = 4 + 4
|
|
|
|
|
2017-08-30 08:14:04 +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
|
2017-09-26 02:35:19 +03:00
|
|
|
BaseCommitmentTxWeight = 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
|
|
|
|
2020-03-06 18:11:46 +03: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
|
|
|
|
|
|
|
|
// BaseAnchorCommitmentTxWeight 900 weight
|
|
|
|
BaseAnchorCommitmentTxWeight = witnessScaleFactor * BaseAnchorCommitmentTxSize
|
|
|
|
|
2020-01-06 13:42:04 +03:00
|
|
|
// CommitWeight 724 weight
|
|
|
|
CommitWeight = BaseCommitmentTxWeight + WitnessCommitmentTxWeight
|
|
|
|
|
2020-03-06 18:11:46 +03:00
|
|
|
// AnchorCommitWeight 1124 weight
|
|
|
|
AnchorCommitWeight = BaseAnchorCommitmentTxWeight + WitnessCommitmentTxWeight
|
|
|
|
|
2017-05-17 04:50:50 +03:00
|
|
|
// HTLCWeight 172 weight
|
2017-09-26 02:35:19 +03:00
|
|
|
HTLCWeight = 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
|
|
|
|
2020-03-06 18:11:49 +03: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
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
// HtlcSuccessWeightCOnfirmed is the weight of the HTLC success
|
|
|
|
// 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
|
|
|
|
|
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.
|
2017-08-30 08:14:04 +03: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 06:30:48 +03:00
|
|
|
// ToLocalScriptSize 79 bytes
|
2017-08-30 08:14:04 +03: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 06:30:48 +03:00
|
|
|
// - OP_DATA: 1 byte
|
|
|
|
// - revoke_key: 33 bytes
|
2017-08-30 08:14:04 +03: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 06:30:48 +03: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 08:14:04 +03: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 06:30:48 +03:00
|
|
|
// - OP_CHECKSIG: 1 byte
|
|
|
|
ToLocalScriptSize = 1 + 1 + 33 + 1 + 1 + 4 + 1 + 1 + 1 + 33 + 1 + 1
|
|
|
|
|
|
|
|
// 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 11:36:06 +03:00
|
|
|
ToLocalTimeoutWitnessSize = 1 + 1 + 73 + 1 + 1 + ToLocalScriptSize
|
2017-08-30 08:14:04 +03:00
|
|
|
|
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 06:30:48 +03:00
|
|
|
// ToLocalPenaltyWitnessSize 156 bytes
|
2017-08-30 08:14:04 +03:00
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - revocation_sig_length: 1 byte
|
|
|
|
// - revocation_sig: 73 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 06:30:48 +03:00
|
|
|
// - OP_TRUE: 1 byte
|
2017-08-30 08:14:04 +03:00
|
|
|
// - witness_script_length: 1 byte
|
|
|
|
// - witness_script (to_local_script)
|
2017-10-10 11:36:06 +03:00
|
|
|
ToLocalPenaltyWitnessSize = 1 + 1 + 73 + 1 + 1 + ToLocalScriptSize
|
2017-08-30 08:14:04 +03:00
|
|
|
|
2020-03-06 18:11:46 +03: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-03-06 18:11:45 +03:00
|
|
|
// AcceptedHtlcScriptSize 142 bytes
|
2017-08-30 08:14:04 +03: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
|
|
|
|
// - 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-03-06 18:11:45 +03:00
|
|
|
// - OP_1: 1 byte // These 3 extra bytes are used for both confirmed and regular
|
|
|
|
// - OP_CSV: 1 byte // HTLC script types. The size won't be correct in all cases,
|
|
|
|
// - OP_DROP: 1 byte // but it is just an upper bound used for fee estimation in any case.
|
2017-08-30 08:14:04 +03:00
|
|
|
// - OP_ENDIF: 1 byte
|
2017-10-10 11:36:06 +03:00
|
|
|
AcceptedHtlcScriptSize = 3*1 + 20 + 5*1 + 33 + 7*1 + 20 + 4*1 +
|
2020-03-06 18:11:45 +03:00
|
|
|
33 + 5*1 + 4 + 8*1
|
2017-08-30 08:14:04 +03:00
|
|
|
|
2020-03-06 18:11:45 +03:00
|
|
|
// AcceptedHtlcTimeoutWitnessSize 219
|
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 06:30:48 +03: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
|
|
|
|
|
2020-03-06 18:11:45 +03:00
|
|
|
// AcceptedHtlcPenaltyWitnessSize 252 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 06:30:48 +03: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 11:36:06 +03:00
|
|
|
AcceptedHtlcPenaltyWitnessSize = 1 + 1 + 73 + 1 + 33 + 1 + AcceptedHtlcScriptSize
|
2017-08-30 08:14:04 +03:00
|
|
|
|
2020-03-06 18:11:45 +03:00
|
|
|
// OfferedHtlcScriptSize 136 bytes
|
2017-08-30 08:14:04 +03: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-03-06 18:11:45 +03:00
|
|
|
// - OP_1: 1 byte
|
|
|
|
// - OP_CSV: 1 byte
|
|
|
|
// - OP_DROP: 1 byte
|
2017-08-30 08:14:04 +03:00
|
|
|
// - OP_ENDIF: 1 byte
|
2020-03-06 18:11:45 +03:00
|
|
|
OfferedHtlcScriptSize = 3*1 + 20 + 5*1 + 33 + 10*1 + 33 + 5*1 + 20 + 7*1
|
2017-10-10 11:36:06 +03:00
|
|
|
|
2020-03-06 18:11:45 +03:00
|
|
|
// OfferedHtlcSuccessWitnessSize 320 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 06:30:48 +03:00
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - nil_length: 1 byte
|
|
|
|
// - receiver_sig_length: 1 byte
|
|
|
|
// - receiver_sig: 73 bytes
|
|
|
|
// - sender_sig_length: 1 byte
|
|
|
|
// - sender_sig: 73 bytes
|
|
|
|
// - payment_preimage_length: 1 byte
|
|
|
|
// - payment_preimage: 32 bytes
|
|
|
|
// - witness_script_length: 1 byte
|
|
|
|
// - witness_script (offered_htlc_script)
|
|
|
|
OfferedHtlcSuccessWitnessSize = 1 + 1 + 1 + 73 + 1 + 73 + 1 + 32 + 1 + OfferedHtlcScriptSize
|
2018-01-17 05:03:08 +03:00
|
|
|
|
2020-03-06 18:11:45 +03:00
|
|
|
// OfferedHtlcPenaltyWitnessSize 246 bytes
|
2017-08-30 08:14:04 +03: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 06:30:48 +03:00
|
|
|
OfferedHtlcPenaltyWitnessSize = 1 + 1 + 73 + 1 + 33 + 1 + OfferedHtlcScriptSize
|
2016-11-23 11:29:05 +03:00
|
|
|
)
|
|
|
|
|
2020-03-10 15:23:17 +03:00
|
|
|
// dummySigner is a fake signer used for size (upper bound) calculations.
|
|
|
|
type dummySigner struct {
|
|
|
|
Signer
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignOutputRaw generates a signature for the passed transaction according to
|
|
|
|
// the data within the passed SignDescriptor.
|
|
|
|
func (s *dummySigner) SignOutputRaw(tx *wire.MsgTx, signDesc *SignDescriptor) (
|
|
|
|
[]byte, error) {
|
|
|
|
|
|
|
|
// Always return worst-case signature length, excluding the one byte
|
|
|
|
// sighash flag.
|
|
|
|
return make([]byte, 73-1), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
// dummyPubKey is a pubkey used in script size calculation.
|
|
|
|
dummyPubKey = btcec.PublicKey{
|
|
|
|
X: &big.Int{},
|
|
|
|
Y: &big.Int{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// dummyAnchorScript is a script used for size calculation.
|
|
|
|
dummyAnchorScript, _ = CommitScriptAnchor(&dummyPubKey)
|
|
|
|
|
|
|
|
// dummyAnchorWitness is a witness used for size calculation.
|
|
|
|
dummyAnchorWitness, _ = CommitSpendAnchor(
|
|
|
|
&dummySigner{},
|
|
|
|
&SignDescriptor{
|
|
|
|
KeyDesc: keychain.KeyDescriptor{
|
|
|
|
PubKey: &dummyPubKey,
|
|
|
|
},
|
|
|
|
WitnessScript: dummyAnchorScript,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
// AnchorWitnessSize 116 bytes
|
|
|
|
AnchorWitnessSize = dummyAnchorWitness.SerializeSize()
|
|
|
|
)
|
|
|
|
|
2019-01-16 17:47:43 +03:00
|
|
|
// EstimateCommitTxWeight estimate commitment transaction weight depending on
|
2017-05-17 04:50:50 +03: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 17:47:43 +03:00
|
|
|
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
|
|
|
}
|
2017-09-26 02:35:19 +03: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 05:02:46 +03: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 02:35:19 +03:00
|
|
|
type TxWeightEstimator struct {
|
|
|
|
hasWitness bool
|
|
|
|
inputCount uint32
|
|
|
|
outputCount uint32
|
|
|
|
inputSize int
|
|
|
|
inputWitnessSize int
|
|
|
|
outputSize int
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddP2PKHInput updates the weight estimate to account for an additional input
|
|
|
|
// spending a P2PKH output.
|
2018-01-17 05:02:46 +03:00
|
|
|
func (twe *TxWeightEstimator) AddP2PKHInput() *TxWeightEstimator {
|
2017-09-26 02:35:19 +03:00
|
|
|
twe.inputSize += InputSize + P2PKHScriptSigSize
|
|
|
|
twe.inputWitnessSize++
|
|
|
|
twe.inputCount++
|
2018-01-17 05:02:46 +03:00
|
|
|
|
|
|
|
return twe
|
2017-09-26 02:35:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddP2WKHInput updates the weight estimate to account for an additional input
|
2017-10-03 05:57:22 +03:00
|
|
|
// spending a native P2PWKH output.
|
2018-01-17 05:02:46 +03:00
|
|
|
func (twe *TxWeightEstimator) AddP2WKHInput() *TxWeightEstimator {
|
2017-09-26 06:13:53 +03:00
|
|
|
twe.AddWitnessInput(P2WKHWitnessSize)
|
2018-01-17 05:02:46 +03:00
|
|
|
|
|
|
|
return twe
|
2017-09-26 06:13:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddWitnessInput updates the weight estimate to account for an additional
|
2017-10-03 05:57:22 +03:00
|
|
|
// input spending a native pay-to-witness output. This accepts the total size
|
|
|
|
// of the witness as a parameter.
|
2018-01-17 05:02:46 +03:00
|
|
|
func (twe *TxWeightEstimator) AddWitnessInput(witnessSize int) *TxWeightEstimator {
|
2017-09-26 02:35:19 +03:00
|
|
|
twe.inputSize += InputSize
|
2017-09-26 06:13:53 +03:00
|
|
|
twe.inputWitnessSize += witnessSize
|
2017-09-26 02:35:19 +03:00
|
|
|
twe.inputCount++
|
|
|
|
twe.hasWitness = true
|
2018-01-17 05:02:46 +03:00
|
|
|
|
|
|
|
return twe
|
2017-09-26 02:35:19 +03:00
|
|
|
}
|
|
|
|
|
2017-10-03 05:57:22 +03: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 05:02:46 +03:00
|
|
|
func (twe *TxWeightEstimator) AddNestedP2WKHInput() *TxWeightEstimator {
|
2020-03-04 19:00:33 +03:00
|
|
|
twe.inputSize += InputSize + NestedP2WPKHSize
|
2017-10-03 05:57:22 +03:00
|
|
|
twe.inputWitnessSize += P2WKHWitnessSize
|
2020-03-04 19:00:33 +03:00
|
|
|
twe.inputCount++
|
2017-10-03 05:57:22 +03:00
|
|
|
twe.hasWitness = true
|
2018-01-17 05:02:46 +03:00
|
|
|
|
|
|
|
return twe
|
2017-10-03 05:57:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddNestedP2WSHInput updates the weight estimate to account for an additional
|
|
|
|
// input spending a P2SH output with a nested P2WSH redeem script.
|
2018-01-17 05:02:46 +03:00
|
|
|
func (twe *TxWeightEstimator) AddNestedP2WSHInput(witnessSize int) *TxWeightEstimator {
|
2020-03-04 19:00:33 +03:00
|
|
|
twe.inputSize += InputSize + NestedP2WSHSize
|
2017-10-03 05:57:22 +03:00
|
|
|
twe.inputWitnessSize += witnessSize
|
2020-03-04 19:00:33 +03:00
|
|
|
twe.inputCount++
|
2017-10-03 05:57:22 +03:00
|
|
|
twe.hasWitness = true
|
2018-01-17 05:02:46 +03:00
|
|
|
|
|
|
|
return twe
|
2017-10-03 05:57:22 +03:00
|
|
|
}
|
|
|
|
|
2017-09-26 02:35:19 +03:00
|
|
|
// AddP2PKHOutput updates the weight estimate to account for an additional P2PKH
|
|
|
|
// output.
|
2018-01-17 05:02:46 +03:00
|
|
|
func (twe *TxWeightEstimator) AddP2PKHOutput() *TxWeightEstimator {
|
2017-09-26 02:35:19 +03:00
|
|
|
twe.outputSize += P2PKHOutputSize
|
|
|
|
twe.outputCount++
|
2018-01-17 05:02:46 +03:00
|
|
|
|
|
|
|
return twe
|
2017-09-26 02:35:19 +03:00
|
|
|
}
|
|
|
|
|
2017-10-03 05:57:22 +03:00
|
|
|
// AddP2WKHOutput updates the weight estimate to account for an additional
|
|
|
|
// native P2WKH output.
|
2018-01-17 05:02:46 +03:00
|
|
|
func (twe *TxWeightEstimator) AddP2WKHOutput() *TxWeightEstimator {
|
2017-09-26 02:35:19 +03:00
|
|
|
twe.outputSize += P2WKHOutputSize
|
|
|
|
twe.outputCount++
|
2018-01-17 05:02:46 +03:00
|
|
|
|
|
|
|
return twe
|
2017-09-26 02:35:19 +03:00
|
|
|
}
|
|
|
|
|
2017-10-03 05:57:22 +03:00
|
|
|
// AddP2WSHOutput updates the weight estimate to account for an additional
|
|
|
|
// native P2WSH output.
|
2018-01-17 05:02:46 +03:00
|
|
|
func (twe *TxWeightEstimator) AddP2WSHOutput() *TxWeightEstimator {
|
2017-09-26 02:35:19 +03:00
|
|
|
twe.outputSize += P2WSHOutputSize
|
|
|
|
twe.outputCount++
|
2018-01-17 05:02:46 +03:00
|
|
|
|
|
|
|
return twe
|
2017-09-26 02:35:19 +03:00
|
|
|
}
|
|
|
|
|
2017-10-03 05:57:22 +03:00
|
|
|
// AddP2SHOutput updates the weight estimate to account for an additional P2SH
|
|
|
|
// output.
|
2018-01-17 05:02:46 +03:00
|
|
|
func (twe *TxWeightEstimator) AddP2SHOutput() *TxWeightEstimator {
|
2017-10-03 05:57:22 +03:00
|
|
|
twe.outputSize += P2SHOutputSize
|
|
|
|
twe.outputCount++
|
2018-01-17 05:02:46 +03:00
|
|
|
|
|
|
|
return twe
|
2017-10-03 05:57:22 +03:00
|
|
|
}
|
|
|
|
|
2017-09-26 02:35:19 +03:00
|
|
|
// Weight gets the estimated weight of the transaction.
|
|
|
|
func (twe *TxWeightEstimator) Weight() int {
|
|
|
|
txSizeStripped := BaseTxSize +
|
|
|
|
wire.VarIntSerializeSize(uint64(twe.inputCount)) + twe.inputSize +
|
|
|
|
wire.VarIntSerializeSize(uint64(twe.outputCount)) + twe.outputSize
|
|
|
|
weight := txSizeStripped * witnessScaleFactor
|
|
|
|
if twe.hasWitness {
|
|
|
|
weight += WitnessHeaderSize + twe.inputWitnessSize
|
|
|
|
}
|
|
|
|
return weight
|
|
|
|
}
|
2018-02-13 16:34:44 +03: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.
|
|
|
|
return (twe.Weight() + witnessScaleFactor - 1) / witnessScaleFactor
|
|
|
|
}
|