ut_tx.py 2.6 KB

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