Browse Source

Add tests of size estimator in jmbitcoin

master
Adam Gibson 3 years ago
parent
commit
7bf6696228
No known key found for this signature in database
GPG Key ID: 141001A1AF77F20B
  1. 2
      jmbitcoin/jmbitcoin/secp256k1_transaction.py
  2. 22
      jmbitcoin/test/test_tx_signing.py

2
jmbitcoin/jmbitcoin/secp256k1_transaction.py

@ -160,7 +160,7 @@ def estimate_tx_size(ins, outs):
# here, outputs are always entirely nonwitness.
outmults = {"p2wsh": 43,
"p2wpkh": 31,
"p2sh-p2wpkh": 64,
"p2sh-p2wpkh": 32,
"p2pkh": 34}
# nVersion, nLockTime, nins, nouts:

22
jmbitcoin/test/test_tx_signing.py

@ -4,6 +4,28 @@ import hashlib
from jmbase import bintohex
import jmbitcoin as btc
# Cases copied from:
# https://github.com/kristapsk/bitcoin-scripts/blob/0b847bec016638e60313ecec2b81f2e8accd311b/tests/tx-vsize.bats
@pytest.mark.parametrize(
"inaddrtypes, outaddrtypes, size_expected",
[(["p2pkh"], ["p2pkh"], 192),
(["p2pkh"], ["p2pkh", "p2pkh"], 226),
(["p2pkh"], ["p2sh-p2wpkh", "p2sh-p2wpkh"], 222),
(["p2pkh"], ["p2pkh", "p2sh-p2wpkh"], 224),
(["p2sh-p2wpkh"], ["p2sh-p2wpkh"], 135),
(["p2wpkh"], ["p2wpkh"], 111),
])
def test_tx_size_estimate(inaddrtypes, outaddrtypes, size_expected):
# non-sw only inputs result in a single integer return,
# segwit inputs return (witness size, non-witness size)
x = btc.estimate_tx_size(inaddrtypes, outaddrtypes)
if btc.there_is_one_segwit_input(inaddrtypes):
s = int(x[0]/4 + x[1])
else:
s = x
assert s == size_expected
@pytest.mark.parametrize(
"addrtype",
[("p2wpkh"),

Loading…
Cancel
Save