ut_tx.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. with open(fpath) as fp:
  25. # remove Python2 'u' string prefixes from ref files:
  26. # New in version 3.3: Support for the unicode legacy literal (u'value') was
  27. # reintroduced to simplify the maintenance of dual Python 2.x and 3.x codebases.
  28. # See PEP 414 for more information.
  29. chk = re.subn( r"\bu(['\"])", r'\1', fp.read() )[0]
  30. diff = get_ndiff(chk,text)
  31. nLines = len([i for i in diff if i.startswith('-')])
  32. assert nLines in (0,1), f'{nLines} lines differ: only checksum line may differ'
  33. qmsg(' OK')
  34. return True
  35. class unit_tests:
  36. altcoin_deps = ('txfile_alt',)
  37. async def tx(self,name,ut):
  38. qmsg(' Testing NewTX initializer')
  39. d = CoinDaemon('btc',test_suite=True)
  40. d.start()
  41. proto = init_proto('btc',need_amt=True)
  42. tx = await NewTX(proto=proto)
  43. d.stop()
  44. qmsg(' OK')
  45. return True
  46. async def txfile(self,name,ut):
  47. return await do_txfile_test(
  48. 'Bitcoin',
  49. (
  50. '0B8D5A[15.31789,14,tl=1320969600].rawtx',
  51. '542169[5.68152,34].sigtx',
  52. '0C7115[15.86255,14,tl=1320969600].testnet.rawtx',
  53. '25EFA3[2.34].testnet.rawtx',
  54. )
  55. )
  56. async def txfile_alt(self,name,ut):
  57. return await do_txfile_test(
  58. 'altcoins',
  59. (
  60. '460D4D-BCH[10.19764,tl=1320969600].rawtx',
  61. 'ethereum/5881D2-MM1[1.23456,50000].rawtx',
  62. 'ethereum/6BDB25-MM1[1.23456,50000].testnet.rawtx',
  63. 'ethereum/88FEFD-ETH[23.45495,40000].rawtx',
  64. 'ethereum/B472BD-ETH[23.45495,40000].testnet.rawtx',
  65. 'ethereum/B472BD-ETH[23.45495,40000].testnet.sigtx',
  66. 'litecoin/A5A1E0-LTC[1454.64322,1453,tl=1320969600].testnet.rawtx',
  67. 'litecoin/AF3CDF-LTC[620.76194,1453,tl=1320969600].rawtx',
  68. )
  69. )