ut_tx.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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,UnsignedTX
  8. from mmgen.tx.file import MMGenTxFile
  9. from mmgen.daemon import CoinDaemon
  10. from mmgen.protocol import init_proto
  11. from mmgen.cfg import Config
  12. from ..include.common import cfg, qmsg, vmsg
  13. async def do_txfile_test(desc, fns, cfg=cfg, check=False):
  14. qmsg(f' Testing CompletedTX initializer ({desc})')
  15. for fn in fns:
  16. qmsg(f' parsing: {os.path.basename(fn)}')
  17. fpath = os.path.join('test','ref',fn)
  18. tx = await CompletedTX(cfg=cfg, filename=fpath, quiet_open=True)
  19. vmsg('\n' + tx.info.format())
  20. f = MMGenTxFile(tx)
  21. fn_gen = f.make_filename()
  22. if cfg.debug_utf8:
  23. fn_gen = fn_gen.replace('-α','')
  24. assert fn_gen == os.path.basename(fn), f'{fn_gen} != {fn}'
  25. if check:
  26. text = f.format()
  27. with open(fpath) as fh:
  28. text_chk = fh.read()
  29. assert text == text_chk, f'\nformatted text:\n{text}\n !=\noriginal file:\n{text_chk}'
  30. qmsg(' OK')
  31. return True
  32. class unit_tests:
  33. altcoin_deps = ('txfile_alt', 'txfile_alt_legacy')
  34. async def tx(self,name,ut):
  35. qmsg(' Testing NewTX initializer')
  36. d = CoinDaemon( cfg, 'btc', test_suite=True )
  37. d.start()
  38. proto = init_proto( cfg, 'btc', need_amt=True )
  39. await NewTX( cfg=cfg, proto=proto )
  40. d.stop()
  41. d.remove_datadir()
  42. qmsg(' OK')
  43. return True
  44. async def txfile(self, name, ut):
  45. return await do_txfile_test(
  46. 'Bitcoin',
  47. (
  48. 'tx/7A8157[6.65227,34].rawtx',
  49. 'tx/BB3FD2[7.57134314,123].sigtx',
  50. 'tx/0A869F[1.23456,32].regtest.asubtx',
  51. ),
  52. check = True
  53. )
  54. async def txfile_alt(self, name, ut):
  55. return await do_txfile_test(
  56. 'altcoins',
  57. (
  58. 'tx/C09D73-LTC[981.73747,2000].testnet.rawtx',
  59. 'tx/91060A-BCH[1.23456].regtest.arawtx',
  60. 'tx/D850C6-MM1[43.21,50000].subtx', # token tx
  61. ),
  62. # token resolved by tracking wallet under data_dir:
  63. cfg = Config({'data_dir': 'test/ref/data_dir'}),
  64. check = True
  65. )
  66. async def txfile_legacy(self, name, ut):
  67. return await do_txfile_test(
  68. 'Bitcoin - legacy file format',
  69. (
  70. '0B8D5A[15.31789,14,tl=1320969600].rawtx',
  71. '542169[5.68152,34].sigtx',
  72. '0C7115[15.86255,14,tl=1320969600].testnet.rawtx',
  73. '25EFA3[2.34].testnet.rawtx',
  74. )
  75. )
  76. async def txfile_alt_legacy(self, name, ut):
  77. return await do_txfile_test(
  78. 'altcoins - legacy file format',
  79. (
  80. '460D4D-BCH[10.19764,tl=1320969600].rawtx',
  81. 'ethereum/5881D2-MM1[1.23456,50000].rawtx',
  82. 'ethereum/6BDB25-MM1[1.23456,50000].testnet.rawtx',
  83. 'ethereum/88FEFD-ETH[23.45495,40000].rawtx',
  84. 'ethereum/B472BD-ETH[23.45495,40000].testnet.rawtx',
  85. 'ethereum/B472BD-ETH[23.45495,40000].testnet.sigtx',
  86. 'litecoin/A5A1E0-LTC[1454.64322,1453,tl=1320969600].testnet.rawtx',
  87. 'litecoin/AF3CDF-LTC[620.76194,1453,tl=1320969600].rawtx',
  88. )
  89. )
  90. def errors(self,name,ut):
  91. async def bad1():
  92. await CompletedTX(cfg, filename='foo')
  93. def bad2():
  94. UnsignedTX(cfg, filename='foo')
  95. bad_data = (
  96. ('forbidden positional args', 'TypeError', 'positional arguments', bad1),
  97. ('forbidden positional args', 'TypeError', 'positional arguments', bad2),
  98. )
  99. ut.process_bad_data(bad_data)
  100. return True