Merge pull request #4034 from cfromknecht/nested-size-fix

input/size: correct NP2WKH and NP2SH input count
This commit is contained in:
Conner Fromknecht 2020-03-09 18:24:52 -07:00 committed by GitHub
commit 852ab592bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 106 additions and 5 deletions

View File

@ -25,12 +25,22 @@ const (
// - PublicKeyHASH160: 20 bytes
P2WPKHSize = 1 + 1 + 20
// NestedP2WPKHSize 23 bytes
// - OP_DATA: 1 byte (P2WPKHSize)
// - P2WPKHWitnessProgram: 22 bytes
NestedP2WPKHSize = 1 + P2WPKHSize
// P2WSHSize 34 bytes
// - OP_0: 1 byte
// - OP_DATA: 1 byte (WitnessScriptSHA256 length)
// - WitnessScriptSHA256: 32 bytes
P2WSHSize = 1 + 1 + 32
// NestedP2WSHSize 35 bytes
// - OP_DATA: 1 byte (P2WSHSize)
// - P2WSHWitnessProgram: 35 bytes
NestedP2WSHSize = 1 + P2WSHSize
// P2PKHOutputSize 34 bytes
// - value: 8 bytes
// - var_int: 1 byte (pkscript_length)
@ -417,9 +427,9 @@ func (twe *TxWeightEstimator) AddWitnessInput(witnessSize int) *TxWeightEstimato
// AddNestedP2WKHInput updates the weight estimate to account for an additional
// input spending a P2SH output with a nested P2WKH redeem script.
func (twe *TxWeightEstimator) AddNestedP2WKHInput() *TxWeightEstimator {
twe.inputSize += InputSize + P2WPKHSize
twe.inputSize += InputSize + NestedP2WPKHSize
twe.inputWitnessSize += P2WKHWitnessSize
twe.inputSize++
twe.inputCount++
twe.hasWitness = true
return twe
@ -428,9 +438,9 @@ func (twe *TxWeightEstimator) AddNestedP2WKHInput() *TxWeightEstimator {
// AddNestedP2WSHInput updates the weight estimate to account for an additional
// input spending a P2SH output with a nested P2WSH redeem script.
func (twe *TxWeightEstimator) AddNestedP2WSHInput(witnessSize int) *TxWeightEstimator {
twe.inputSize += InputSize + P2WSHSize
twe.inputSize += InputSize + NestedP2WSHSize
twe.inputWitnessSize += witnessSize
twe.inputSize++
twe.inputCount++
twe.hasWitness = true
return twe

View File

@ -1,4 +1,4 @@
package lnwallet_test
package input_test
import (
"testing"
@ -68,6 +68,68 @@ func TestTxWeightEstimator(t *testing.T) {
numP2WSHOutputs int
numP2SHOutputs int
}{
// Assert base txn size.
{},
// Assert single input/output sizes.
{
numP2PKHInputs: 1,
},
{
numP2WKHInputs: 1,
},
{
numP2WSHInputs: 1,
},
{
numNestedP2WKHInputs: 1,
},
{
numNestedP2WSHInputs: 1,
},
{
numP2WKHOutputs: 1,
},
{
numP2PKHOutputs: 1,
},
{
numP2WSHOutputs: 1,
},
{
numP2SHOutputs: 1,
},
// Assert each input/output increments input/output counts.
{
numP2PKHInputs: 253,
},
{
numP2WKHInputs: 253,
},
{
numP2WSHInputs: 253,
},
{
numNestedP2WKHInputs: 253,
},
{
numNestedP2WSHInputs: 253,
},
{
numP2WKHOutputs: 253,
},
{
numP2PKHOutputs: 253,
},
{
numP2WSHOutputs: 253,
},
{
numP2SHOutputs: 253,
},
// Assert basic combinations of inputs and outputs.
{
numP2PKHInputs: 1,
numP2PKHOutputs: 2,
@ -104,6 +166,35 @@ func TestTxWeightEstimator(t *testing.T) {
numNestedP2WSHInputs: 1,
numP2WKHOutputs: 1,
},
// Assert disparate input/output types increment total
// input/output counts.
{
numP2PKHInputs: 50,
numP2WKHInputs: 50,
numP2WSHInputs: 51,
numNestedP2WKHInputs: 51,
numNestedP2WSHInputs: 51,
numP2WKHOutputs: 1,
},
{
numP2WKHInputs: 1,
numP2WKHOutputs: 63,
numP2PKHOutputs: 63,
numP2WSHOutputs: 63,
numP2SHOutputs: 64,
},
{
numP2PKHInputs: 50,
numP2WKHInputs: 50,
numP2WSHInputs: 51,
numNestedP2WKHInputs: 51,
numNestedP2WSHInputs: 51,
numP2WKHOutputs: 63,
numP2PKHOutputs: 63,
numP2WSHOutputs: 63,
numP2SHOutputs: 64,
},
}
for i, test := range testCases {