input: add AddOutput method to TxWeightEstimator

This commit adds a helper method to estimate the weight of an output by
the given pkScript.
This commit is contained in:
Oliver Gugger 2024-02-06 12:25:53 +01:00
parent 6773d6a6f6
commit 90c2926fdf
No known key found for this signature in database
GPG key ID: 8E4256593F177720

View file

@ -49,6 +49,11 @@ const (
// - max-size: 40 bytes
UnknownWitnessSize = 1 + 1 + 40
// BaseOutputSize 9 bytes
// - value: 8 bytes
// - var_int: 1 byte (pkscript_length)
BaseOutputSize = 8 + 1
// P2PKHSize 25 bytes.
P2PKHSize = 25
@ -56,19 +61,19 @@ const (
// - value: 8 bytes
// - var_int: 1 byte (pkscript_length)
// - pkscript (p2pkh): 25 bytes
P2PKHOutputSize = 8 + 1 + P2PKHSize
P2PKHOutputSize = BaseOutputSize + P2PKHSize
// P2WKHOutputSize 31 bytes
// - value: 8 bytes
// - var_int: 1 byte (pkscript_length)
// - pkscript (p2wpkh): 22 bytes
P2WKHOutputSize = 8 + 1 + P2WPKHSize
P2WKHOutputSize = BaseOutputSize + P2WPKHSize
// P2WSHOutputSize 43 bytes
// - value: 8 bytes
// - var_int: 1 byte (pkscript_length)
// - pkscript (p2wsh): 34 bytes
P2WSHOutputSize = 8 + 1 + P2WSHSize
P2WSHOutputSize = BaseOutputSize + P2WSHSize
// P2SHSize 23 bytes.
P2SHSize = 23
@ -77,7 +82,7 @@ const (
// - value: 8 bytes
// - var_int: 1 byte (pkscript_length)
// - pkscript (p2sh): 23 bytes
P2SHOutputSize = 8 + 1 + P2SHSize
P2SHOutputSize = BaseOutputSize + P2SHSize
// P2TRSize 34 bytes
// - OP_0: 1 byte
@ -89,7 +94,7 @@ const (
// - value: 8 bytes
// - var_int: 1 byte (pkscript_length)
// - pkscript (p2tr): 34 bytes
P2TROutputSize = 8 + 1 + P2TRSize
P2TROutputSize = BaseOutputSize + P2TRSize
// P2PKHScriptSigSize 108 bytes
// - OP_DATA: 1 byte (signature length)
@ -1013,6 +1018,14 @@ func (twe *TxWeightEstimator) AddP2SHOutput() *TxWeightEstimator {
return twe
}
// 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
}
// Weight gets the estimated weight of the transaction.
func (twe *TxWeightEstimator) Weight() int {
txSizeStripped := BaseTxSize +