ut_tx.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python3
  2. """
  3. test.unit_tests_d.ut_tx: TX unit tests for the MMGen suite
  4. """
  5. import re
  6. from mmgen.common import *
  7. from mmgen.tx import NewTX,CompletedTX
  8. from mmgen.tx.file import MMGenTxFile
  9. from mmgen.daemon import CoinDaemon
  10. from mmgen.protocol import init_proto
  11. async def do_txfile_test(desc,fns):
  12. qmsg(f' Testing CompletedTX initializer ({desc})')
  13. for fn in fns:
  14. qmsg(f' parsing: {os.path.basename(fn)}')
  15. fpath = os.path.join('test','ref',fn)
  16. tx = await CompletedTX(filename=fpath,quiet_open=True)
  17. vmsg(tx.info.format())
  18. f = MMGenTxFile(tx)
  19. fn_gen = f.make_filename()
  20. if g.debug_utf8:
  21. fn_gen = fn_gen.replace('-α','')
  22. assert fn_gen == os.path.basename(fn), f'{fn_gen} != {fn}'
  23. text = f.format()
  24. continue # TODO: check disabled after label -> comment patch
  25. with open(fpath) as fp:
  26. chk = fp.read()
  27. # remove Python2 'u' string prefixes from ref files:
  28. # New in version 3.3: Support for the unicode legacy literal (u'value') was
  29. # reintroduced to simplify the maintenance of dual Python 2.x and 3.x codebases.
  30. # See PEP 414 for more information.
  31. chk = chk.replace("'label':","'comment':") # TODO
  32. chk = re.subn( r"\bu(['\"])", r'\1', chk )[0]
  33. diff = get_ndiff(chk,text)
  34. print(get_diff(chk,text,from_json=False))
  35. nLines = len([i for i in diff if i.startswith('-')])
  36. assert nLines in (0,1), f'{nLines} lines differ: only checksum line may differ'
  37. qmsg(' OK')
  38. return True
  39. class unit_tests:
  40. altcoin_deps = ('txfile_alt',)
  41. async def tx(self,name,ut):
  42. qmsg(' Testing NewTX initializer')
  43. d = CoinDaemon('btc',test_suite=True)
  44. d.start()
  45. proto = init_proto('btc',need_amt=True)
  46. tx = await NewTX(proto=proto)
  47. d.stop()
  48. qmsg(' OK')
  49. return True
  50. async def txfile(self,name,ut):
  51. return await do_txfile_test(
  52. 'Bitcoin',
  53. (
  54. '0B8D5A[15.31789,14,tl=1320969600].rawtx',
  55. '542169[5.68152,34].sigtx',
  56. '0C7115[15.86255,14,tl=1320969600].testnet.rawtx',
  57. '25EFA3[2.34].testnet.rawtx',
  58. )
  59. )
  60. async def txfile_alt(self,name,ut):
  61. return await do_txfile_test(
  62. 'altcoins',
  63. (
  64. '460D4D-BCH[10.19764,tl=1320969600].rawtx',
  65. 'ethereum/5881D2-MM1[1.23456,50000].rawtx',
  66. 'ethereum/6BDB25-MM1[1.23456,50000].testnet.rawtx',
  67. 'ethereum/88FEFD-ETH[23.45495,40000].rawtx',
  68. 'ethereum/B472BD-ETH[23.45495,40000].testnet.rawtx',
  69. 'ethereum/B472BD-ETH[23.45495,40000].testnet.sigtx',
  70. 'litecoin/A5A1E0-LTC[1454.64322,1453,tl=1320969600].testnet.rawtx',
  71. 'litecoin/AF3CDF-LTC[620.76194,1453,tl=1320969600].rawtx',
  72. )
  73. )