ut_tx.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env python3
  2. """
  3. test.modtest_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 CompletedTX, UnsignedTX
  8. from mmgen.tx.file import MMGenTxFile
  9. from mmgen.protocol import init_proto
  10. from mmgen.cfg import Config
  11. from ..include.common import cfg, qmsg, vmsg
  12. async def do_txfile_test(desc, fns, cfg=cfg, check=False):
  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('\n' + 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. if check:
  25. text = f.format()
  26. with open(fpath) as fh:
  27. text_chk = fh.read()
  28. assert text == text_chk, f'\nformatted text:\n{text}\n !=\noriginal file:\n{text_chk}'
  29. qmsg(' OK')
  30. return True
  31. class unit_tests:
  32. altcoin_deps = ('txfile_alt', 'txfile_alt_legacy')
  33. async def txfile(self, name, ut):
  34. return await do_txfile_test(
  35. 'Bitcoin',
  36. (
  37. 'tx/7A8157[6.65227,34].rawtx',
  38. 'tx/BB3FD2[7.57134314,123].sigtx',
  39. 'tx/0A869F[1.23456,32].regtest.asubtx',
  40. ),
  41. check = True
  42. )
  43. async def txfile_alt(self, name, ut):
  44. return await do_txfile_test(
  45. 'altcoins',
  46. (
  47. 'tx/C09D73-LTC[981.73747,2000].testnet.rawtx',
  48. 'tx/91060A-BCH[1.23456].regtest.arawtx',
  49. 'tx/D850C6-MM1[43.21,50000].subtx', # token tx
  50. ),
  51. # token resolved by tracking wallet under data_dir:
  52. cfg = Config({'data_dir': 'test/ref/data_dir'}),
  53. check = True
  54. )
  55. async def txfile_legacy(self, name, ut):
  56. return await do_txfile_test(
  57. 'Bitcoin - legacy file format',
  58. (
  59. '0B8D5A[15.31789,14,tl=1320969600].rawtx',
  60. '542169[5.68152,34].sigtx',
  61. '0C7115[15.86255,14,tl=1320969600].testnet.rawtx',
  62. '25EFA3[2.34].testnet.rawtx',
  63. )
  64. )
  65. async def txfile_alt_legacy(self, name, ut):
  66. return await do_txfile_test(
  67. 'altcoins - legacy file format',
  68. (
  69. '460D4D-BCH[10.19764,tl=1320969600].rawtx',
  70. 'ethereum/5881D2-MM1[1.23456,50000].rawtx',
  71. 'ethereum/6BDB25-MM1[1.23456,50000].testnet.rawtx',
  72. 'ethereum/88FEFD-ETH[23.45495,40000].rawtx',
  73. 'ethereum/B472BD-ETH[23.45495,40000].testnet.rawtx',
  74. 'ethereum/B472BD-ETH[23.45495,40000].testnet.sigtx',
  75. 'litecoin/A5A1E0-LTC[1454.64322,1453,tl=1320969600].testnet.rawtx',
  76. 'litecoin/AF3CDF-LTC[620.76194,1453,tl=1320969600].rawtx',
  77. )
  78. )
  79. def errors(self, name, ut):
  80. async def bad1():
  81. await CompletedTX(cfg, filename='foo')
  82. def bad2():
  83. UnsignedTX(cfg, filename='foo')
  84. bad_data = (
  85. ('forbidden positional args', 'TypeError', 'positional arguments', bad1),
  86. ('forbidden positional args', 'TypeError', 'positional arguments', bad2),
  87. )
  88. ut.process_bad_data(bad_data)
  89. return True