diff --git a/input/size.go b/input/size.go index 68fa85e4..a9406f8a 100644 --- a/input/size.go +++ b/input/size.go @@ -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 diff --git a/lnwallet/size_test.go b/input/size_test.go similarity index 77% rename from lnwallet/size_test.go rename to input/size_test.go index 36dac258..91643402 100644 --- a/lnwallet/size_test.go +++ b/input/size_test.go @@ -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 {