Browse Source

fix test/unified/test_segwit.py

add_frost_channel_encryption
zebra-lucky 4 months ago
parent
commit
d5213ec45a
  1. 7
      test/unified/common.py
  2. 71
      test/unified/test_segwit.py

7
test/unified/common.py

@ -50,7 +50,7 @@ async def make_sign_and_push(ins_full,
output_addr = await wallet_service.get_new_addr( output_addr = await wallet_service.get_new_addr(
1, 1,
BaseWallet.ADDRESS_TYPE_INTERNAL) if not output_addr else output_addr BaseWallet.ADDRESS_TYPE_INTERNAL) if not output_addr else output_addr
change_addr = wallet_service.get_new_addr( change_addr = await wallet_service.get_new_addr(
0, 0,
BaseWallet.ADDRESS_TYPE_INTERNAL) if not change_addr else change_addr BaseWallet.ADDRESS_TYPE_INTERNAL) if not change_addr else change_addr
fee_est = estimate_tx_fee(len(ins), 2) if estimate_fee else 10000 fee_est = estimate_tx_fee(len(ins), 2) if estimate_fee else 10000
@ -135,8 +135,9 @@ async def make_wallets(n,
amt = mean_amt - sdev_amt / 2.0 + deviation amt = mean_amt - sdev_amt / 2.0 + deviation
if amt < 0: amt = 0.001 if amt < 0: amt = 0.001
amt = float(Decimal(amt).quantize(Decimal(10)**-8)) amt = float(Decimal(amt).quantize(Decimal(10)**-8))
jm_single().bc_interface.grab_coins(wallet_service.get_new_addr( dest_addr = await wallet_service.get_new_addr(
j, BaseWallet.ADDRESS_TYPE_INTERNAL), amt) j, BaseWallet.ADDRESS_TYPE_INTERNAL)
jm_single().bc_interface.grab_coins(dest_addr , amt)
return wallets return wallets

71
test/unified/test_segwit.py

@ -6,12 +6,25 @@ from common import make_wallets
from pprint import pformat from pprint import pformat
import jmbitcoin as btc import jmbitcoin as btc
import pytest import pytest
from unittest_parametrize import parametrize, ParametrizedTestCase
from jmbase import get_log, hextobin from jmbase import get_log, hextobin
from jmclient import load_test_config, jm_single, LegacyWallet, BaseWallet from jmclient import load_test_config, jm_single, LegacyWallet, BaseWallet
from common import TrialAsyncioTestCase
pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind")
log = get_log() log = get_log()
def test_segwit_valid_txs(setup_segwit): class SegWitTests(TrialAsyncioTestCase, ParametrizedTestCase):
async def asyncSetUp(self):
load_test_config()
jm_single().bc_interface.tick_forward_chain_interval = 1
async def test_segwit_valid_txs(self):
with open("test/tx_segwit_valid.json", "r") as f: with open("test/tx_segwit_valid.json", "r") as f:
json_data = f.read() json_data = f.read()
valid_txs = json.loads(json_data) valid_txs = json.loads(json_data)
@ -24,14 +37,14 @@ def test_segwit_valid_txs(setup_segwit):
#TODO use bcinterface to decoderawtransaction #TODO use bcinterface to decoderawtransaction
#and compare the json values #and compare the json values
@pytest.mark.parametrize( @parametrize(
"wallet_structure, in_amt, amount, segwit_amt, segwit_ins, o_ins", [ "wallet_structure, in_amt, amount, segwit_amt, segwit_ins, o_ins", [
([[1, 0, 0, 0, 0]], 1, 1000000, 1, [0, 1, 2], []), ([[1, 0, 0, 0, 0]], 1, 1000000, 1, [0, 1, 2], []),
([[4, 0, 0, 0, 1]], 3, 100000000, 1, [0, 2], [1, 3]), ([[4, 0, 0, 0, 1]], 3, 100000000, 1, [0, 2], [1, 3]),
([[4, 0, 0, 0, 1]], 3, 100000000, 1, [0, 5], [1, 2, 3, 4]), ([[4, 0, 0, 0, 1]], 3, 100000000, 1, [0, 5], [1, 2, 3, 4]),
([[4, 0, 0, 0, 0]], 2, 200000007, 0.3, [0, 1, 4, 5], [2, 3, 6]), ([[4, 0, 0, 0, 0]], 2, 200000007, 0.3, [0, 1, 4, 5], [2, 3, 6]),
]) ])
def test_spend_p2sh_p2wpkh_multi(setup_segwit, wallet_structure, in_amt, amount, async def test_spend_p2sh_p2wpkh_multi(self, wallet_structure, in_amt, amount,
segwit_amt, segwit_ins, o_ins): segwit_amt, segwit_ins, o_ins):
"""Creates a wallet from which non-segwit inputs/ """Creates a wallet from which non-segwit inputs/
outputs can be created, constructs one or more outputs can be created, constructs one or more
@ -48,14 +61,19 @@ def test_spend_p2sh_p2wpkh_multi(setup_segwit, wallet_structure, in_amt, amount,
MIXDEPTH = 0 MIXDEPTH = 0
# set up wallets and inputs # set up wallets and inputs
nsw_wallet_service = make_wallets(1, wallet_structure, in_amt, wallet_services = await make_wallets(
walletclass=LegacyWallet)[0]['wallet'] 1, wallet_structure, in_amt, walletclass=LegacyWallet)
nsw_wallet_service.sync_wallet(fast=True) nsw_wallet_service = wallet_services[0]['wallet']
sw_wallet_service = make_wallets(1, [[len(segwit_ins), 0, 0, 0, 0]], segwit_amt)[0]['wallet'] await nsw_wallet_service.sync_wallet(fast=True)
sw_wallet_service.sync_wallet(fast=True) sw_wallet_services = await make_wallets(
1, [[len(segwit_ins), 0, 0, 0, 0]], segwit_amt)
nsw_utxos = nsw_wallet_service.get_utxos_by_mixdepth()[MIXDEPTH] sw_wallet_service = sw_wallet_services[0]['wallet']
sw_utxos = sw_wallet_service.get_utxos_by_mixdepth()[MIXDEPTH] await sw_wallet_service.sync_wallet(fast=True)
nsw_utxos_by_md = await nsw_wallet_service.get_utxos_by_mixdepth()
nsw_utxos = nsw_utxos_by_md[MIXDEPTH]
sw_utxos_by_md = await sw_wallet_service.get_utxos_by_mixdepth()
sw_utxos = sw_utxos_by_md[MIXDEPTH]
assert len(o_ins) <= len(nsw_utxos), "sync failed" assert len(o_ins) <= len(nsw_utxos), "sync failed"
assert len(segwit_ins) <= len(sw_utxos), "sync failed" assert len(segwit_ins) <= len(sw_utxos), "sync failed"
@ -90,37 +108,38 @@ def test_spend_p2sh_p2wpkh_multi(setup_segwit, wallet_structure, in_amt, amount,
FEE = 50000 FEE = 50000
assert FEE < total_amt_in_sat - amount, "test broken, not enough funds" assert FEE < total_amt_in_sat - amount, "test broken, not enough funds"
cj_script = nsw_wallet_service.get_new_script(MIXDEPTH + 1, cj_script = await nsw_wallet_service.get_new_script(
BaseWallet.ADDRESS_TYPE_INTERNAL) MIXDEPTH + 1, BaseWallet.ADDRESS_TYPE_INTERNAL)
change_script = nsw_wallet_service.get_new_script(MIXDEPTH, change_script = await nsw_wallet_service.get_new_script(
BaseWallet.ADDRESS_TYPE_INTERNAL) MIXDEPTH, BaseWallet.ADDRESS_TYPE_INTERNAL)
change_amt = total_amt_in_sat - amount - FEE change_amt = total_amt_in_sat - amount - FEE
tx_outs = [ tx_outs = [
{'address': nsw_wallet_service.script_to_addr(cj_script), {'address': await nsw_wallet_service.script_to_addr(cj_script),
'value': amount}, 'value': amount},
{'address': nsw_wallet_service.script_to_addr(change_script), {'address': await nsw_wallet_service.script_to_addr(change_script),
'value': change_amt}] 'value': change_amt}]
tx = btc.mktx(tx_ins, tx_outs) tx = btc.mktx(tx_ins, tx_outs)
# import new addresses to bitcoind # import new addresses to bitcoind
jm_single().bc_interface.import_addresses( jm_single().bc_interface.import_addresses(
[nsw_wallet_service.script_to_addr(x) [(await nsw_wallet_service.script_to_addr(x))
for x in [cj_script, change_script]], nsw_wallet_service.get_wallet_name()) for x in [cj_script, change_script]],
nsw_wallet_service.get_wallet_name())
# sign tx # sign tx
scripts = {} scripts = {}
for nsw_in_index in o_ins: for nsw_in_index in o_ins:
inp = nsw_ins[nsw_in_index][1] inp = nsw_ins[nsw_in_index][1]
scripts[nsw_in_index] = (inp['script'], inp['value']) scripts[nsw_in_index] = (inp['script'], inp['value'])
success, msg = nsw_wallet_service.sign_tx(tx, scripts) success, msg = await nsw_wallet_service.sign_tx(tx, scripts)
assert success, msg assert success, msg
scripts = {} scripts = {}
for sw_in_index in segwit_ins: for sw_in_index in segwit_ins:
inp = sw_ins[sw_in_index][1] inp = sw_ins[sw_in_index][1]
scripts[sw_in_index] = (inp['script'], inp['value']) scripts[sw_in_index] = (inp['script'], inp['value'])
success, msg = sw_wallet_service.sign_tx(tx, scripts) success, msg = await sw_wallet_service.sign_tx(tx, scripts)
assert success, msg assert success, msg
print(tx) print(tx)
@ -130,18 +149,12 @@ def test_spend_p2sh_p2wpkh_multi(setup_segwit, wallet_structure, in_amt, amount,
assert txid assert txid
balances = jm_single().bc_interface.get_received_by_addr( balances = jm_single().bc_interface.get_received_by_addr(
[nsw_wallet_service.script_to_addr(cj_script), [(await nsw_wallet_service.script_to_addr(cj_script)),
nsw_wallet_service.script_to_addr(change_script)])['data'] (await nsw_wallet_service.script_to_addr(change_script))])['data']
assert balances[0]['balance'] == amount assert balances[0]['balance'] == amount
assert balances[1]['balance'] == change_amt assert balances[1]['balance'] == change_amt
@pytest.fixture(scope="module")
def setup_segwit():
load_test_config()
jm_single().bc_interface.tick_forward_chain_interval = 1
''' '''
Examples of valid segwit from the json with parsing Examples of valid segwit from the json with parsing

Loading…
Cancel
Save