Skip to content

Commit 3c49e69

Browse files
committed
test: fix weight estimates in functional tests
Updates the weight estimate to be more accurate by removing byte buffers and calculating the length of the count of scriptWitnesses rather than just using the count itself.
1 parent ac19235 commit 3c49e69

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

test/functional/rpc_psbt.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -753,11 +753,16 @@ def test_psbt_input_keys(psbt_input, keys):
753753
break
754754
psbt_in = dec["inputs"][input_idx]
755755
# Calculate the input weight
756-
# (prevout + sequence + length of scriptSig + scriptsig + 1 byte buffer) * WITNESS_SCALE_FACTOR + num scriptWitness stack items + (length of stack item + stack item) * N stack items + 1 byte buffer
756+
# (prevout + sequence + length of scriptSig + scriptsig) * WITNESS_SCALE_FACTOR + len of num scriptWitness stack items + (length of stack item + stack item) * N stack items
757+
# Note that occasionally this weight estimate may be slightly larger or smaller than the real weight
758+
# as sometimes ECDSA signatures are one byte shorter than expected with a probability of 1/128
757759
len_scriptsig = len(psbt_in["final_scriptSig"]["hex"]) // 2 if "final_scriptSig" in psbt_in else 0
758-
len_scriptsig += len(ser_compact_size(len_scriptsig)) + 1
759-
len_scriptwitness = (sum([(len(x) // 2) + len(ser_compact_size(len(x) // 2)) for x in psbt_in["final_scriptwitness"]]) + len(psbt_in["final_scriptwitness"]) + 1) if "final_scriptwitness" in psbt_in else 0
760-
input_weight = ((40 + len_scriptsig) * WITNESS_SCALE_FACTOR) + len_scriptwitness
760+
len_scriptsig += len(ser_compact_size(len_scriptsig))
761+
len_scriptwitness = (sum([(len(x) // 2) + len(ser_compact_size(len(x) // 2)) for x in psbt_in["final_scriptwitness"]]) + len(ser_compact_size(len(psbt_in["final_scriptwitness"])))) if "final_scriptwitness" in psbt_in else 0
762+
len_prevout_txid = 32
763+
len_prevout_index = 4
764+
len_sequence = 4
765+
input_weight = ((len_prevout_txid + len_prevout_index + len_sequence + len_scriptsig) * WITNESS_SCALE_FACTOR) + len_scriptwitness
761766
low_input_weight = input_weight // 2
762767
high_input_weight = input_weight * 2
763768

test/functional/wallet_send.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,11 +543,16 @@ def run_test(self):
543543
break
544544
psbt_in = dec["inputs"][input_idx]
545545
# Calculate the input weight
546-
# (prevout + sequence + length of scriptSig + scriptsig + 1 byte buffer) * WITNESS_SCALE_FACTOR + num scriptWitness stack items + (length of stack item + stack item) * N stack items + 1 byte buffer
546+
# (prevout + sequence + length of scriptSig + scriptsig) * WITNESS_SCALE_FACTOR + len of num scriptWitness stack items + (length of stack item + stack item) * N stack items
547+
# Note that occasionally this weight estimate may be slightly larger or smaller than the real weight
548+
# as sometimes ECDSA signatures are one byte shorter than expected with a probability of 1/128
547549
len_scriptsig = len(psbt_in["final_scriptSig"]["hex"]) // 2 if "final_scriptSig" in psbt_in else 0
548-
len_scriptsig += len(ser_compact_size(len_scriptsig)) + 1
549-
len_scriptwitness = (sum([(len(x) // 2) + len(ser_compact_size(len(x) // 2)) for x in psbt_in["final_scriptwitness"]]) + len(psbt_in["final_scriptwitness"]) + 1) if "final_scriptwitness" in psbt_in else 0
550-
input_weight = ((40 + len_scriptsig) * WITNESS_SCALE_FACTOR) + len_scriptwitness
550+
len_scriptsig += len(ser_compact_size(len_scriptsig))
551+
len_scriptwitness = (sum([(len(x) // 2) + len(ser_compact_size(len(x) // 2)) for x in psbt_in["final_scriptwitness"]]) + len(ser_compact_size(len(psbt_in["final_scriptwitness"])))) if "final_scriptwitness" in psbt_in else 0
552+
len_prevout_txid = 32
553+
len_prevout_index = 4
554+
len_sequence = 4
555+
input_weight = ((len_prevout_txid + len_prevout_index + len_sequence + len_scriptsig) * WITNESS_SCALE_FACTOR) + len_scriptwitness
551556

552557
# Input weight error conditions
553558
assert_raises_rpc_error(

0 commit comments

Comments
 (0)