ut_tx.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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.rpc_port = d.rpc_port
  19. async def do():
  20. tx = MMGenTX.New(proto=proto)
  21. tx.rpc = await rpc_init(proto=proto)
  22. run_session(do())
  23. d.stop()
  24. qmsg(' OK')
  25. return True
  26. def txfile(self,name,ut):
  27. qmsg(' Testing TX file operations')
  28. fns = ( # TODO: add altcoin TX files
  29. '0B8D5A[15.31789,14,tl=1320969600].rawtx',
  30. '0C7115[15.86255,14,tl=1320969600].testnet.rawtx',
  31. '460D4D-BCH[10.19764,tl=1320969600].rawtx',
  32. '25EFA3[2.34].testnet.rawtx',
  33. )
  34. for fn in fns:
  35. vmsg(f' parsing: {fn}')
  36. fpath = os.path.join('test','ref',fn)
  37. tx = MMGenTX.Unsigned(filename=fpath,quiet_open=True)
  38. f = MMGenTxFile(tx)
  39. fn_gen = f.make_filename()
  40. assert fn_gen == fn, f'{fn_gen} != {fn}'
  41. text = f.format()
  42. # New in version 3.3: Support for the unicode legacy literal (u'value') was
  43. # reintroduced to simplify the maintenance of dual Python 2.x and 3.x codebases.
  44. # See PEP 414 for more information.
  45. chk = re.subn(r"\bu(['\"])",r"\1",open(fpath).read())[0] # remove Python2 'u' string prefixes from ref files
  46. diff = get_ndiff(chk,text)
  47. #print('\n'.join(diff))
  48. nLines = len([i for i in diff if i.startswith('-')])
  49. assert nLines in (0,1), f'{nLines} lines differ: only checksum line may differ'
  50. #break # FIXME - test BCH, testnet
  51. qmsg(' OK')
  52. return True