ut_tx.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python3
  2. """
  3. test.unit_tests_d.ut_tx: TX unit test for the MMGen suite
  4. """
  5. import re
  6. from mmgen.common import *
  7. from mmgen.tx import MMGenTX
  8. from mmgen.txfile import MMGenTxFile
  9. from mmgen.rpc import rpc_init
  10. from mmgen.daemon import CoinDaemon
  11. from mmgen.protocol import init_proto
  12. class unit_tests:
  13. def tx(self,name,ut):
  14. qmsg(' Testing transaction objects')
  15. proto = init_proto('btc')
  16. d = CoinDaemon('btc',test_suite=True)
  17. d.start()
  18. proto.daemon_data_dir = d.datadir # location of cookie file
  19. proto.rpc_port = d.rpc_port
  20. async def do():
  21. tx = MMGenTX.New(proto=proto)
  22. tx.rpc = await rpc_init(proto=proto)
  23. run_session(do())
  24. d.stop()
  25. qmsg(' OK')
  26. return True
  27. def txfile(self,name,ut):
  28. qmsg(' Testing TX file operations')
  29. fns = ( # TODO: add altcoin TX files
  30. '0B8D5A[15.31789,14,tl=1320969600].rawtx',
  31. '0C7115[15.86255,14,tl=1320969600].testnet.rawtx',
  32. '460D4D-BCH[10.19764,tl=1320969600].rawtx',
  33. '25EFA3[2.34].testnet.rawtx',
  34. )
  35. for fn in fns:
  36. vmsg(f' parsing: {fn}')
  37. fpath = os.path.join('test','ref',fn)
  38. tx = MMGenTX.Unsigned(filename=fpath,quiet_open=True)
  39. f = MMGenTxFile(tx)
  40. fn_gen = f.make_filename()
  41. assert fn_gen == fn, f'{fn_gen} != {fn}'
  42. text = f.format()
  43. # New in version 3.3: Support for the unicode legacy literal (u'value') was
  44. # reintroduced to simplify the maintenance of dual Python 2.x and 3.x codebases.
  45. # See PEP 414 for more information.
  46. chk = re.subn(r"\bu(['\"])",r"\1",open(fpath).read())[0] # remove Python2 'u' string prefixes from ref files
  47. diff = get_ndiff(chk,text)
  48. #print('\n'.join(diff))
  49. nLines = len([i for i in diff if i.startswith('-')])
  50. assert nLines in (0,1), f'{nLines} lines differ: only checksum line may differ'
  51. #break # FIXME - test BCH, testnet
  52. qmsg(' OK')
  53. return True