aboutsummaryrefslogtreecommitdiff
path: root/test/functional/test_framework
diff options
context:
space:
mode:
authorSebastian Falbesoner <sebastian.falbesoner@gmail.com>2021-05-27 18:09:07 +0200
committerSebastian Falbesoner <sebastian.falbesoner@gmail.com>2021-06-08 19:38:17 +0200
commitce024b1c0ef2dcd307023aaaab40373c8bf17db1 (patch)
tree5a50efb7ba833b954e43e7ffadb400ab78668702 /test/functional/test_framework
parent82bc7faec8079b50f248655a97950087948f065d (diff)
downloadbitcoin-ce024b1c0ef2dcd307023aaaab40373c8bf17db1.tar.xz
test: MiniWallet: force P2PK signature to have fixed size (71 bytes)
In order to enable exact fee calculation for transactions that spend P2PK outputs in the MiniWallet, we enforce the created signatures to have a fixed length (>49.89% probability) by default. With that it is easier to check the created transactions vsize and avoid flaky tests that would appear whenever the signatures R- or S-values are smaller (due to leading zero bytes). Note that to get the total scriptSig size one has to add another 2 bytes, as there is also the OP_PUSHx instruction on the front and the sighash type byte on the back, leading to a final scriptSig size of 73 bytes.
Diffstat (limited to 'test/functional/test_framework')
-rw-r--r--test/functional/test_framework/wallet.py12
1 files changed, 10 insertions, 2 deletions
diff --git a/test/functional/test_framework/wallet.py b/test/functional/test_framework/wallet.py
index 0962a5cb54..4b761128ee 100644
--- a/test/functional/test_framework/wallet.py
+++ b/test/functional/test_framework/wallet.py
@@ -88,12 +88,20 @@ class MiniWallet:
if out['scriptPubKey']['hex'] == self._scriptPubKey.hex():
self._utxos.append({'txid': tx['txid'], 'vout': out['n'], 'value': out['value']})
- def sign_tx(self, tx):
+ def sign_tx(self, tx, fixed_length=True):
"""Sign tx that has been created by MiniWallet in P2PK mode"""
assert self._priv_key is not None
(sighash, err) = LegacySignatureHash(CScript(self._scriptPubKey), tx, 0, SIGHASH_ALL)
assert err is None
- tx.vin[0].scriptSig = CScript([self._priv_key.sign_ecdsa(sighash) + bytes(bytearray([SIGHASH_ALL]))])
+ # for exact fee calculation, create only signatures with fixed size by default (>49.89% probability):
+ # 65 bytes: high-R val (33 bytes) + low-S val (32 bytes)
+ # with the DER header/skeleton data of 6 bytes added, this leads to a target size of 71 bytes
+ der_sig = b''
+ while not len(der_sig) == 71:
+ der_sig = self._priv_key.sign_ecdsa(sighash)
+ if not fixed_length:
+ break
+ tx.vin[0].scriptSig = CScript([der_sig + bytes(bytearray([SIGHASH_ALL]))])
def generate(self, num_blocks):
"""Generate blocks with coinbase outputs to the internal address, and append the outputs to the internal list"""