ut_tx.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/env python3
  2. """
  3. test.daemontest_d.ut_tx: TX daemon tests for the MMGen suite
  4. """
  5. import os, json
  6. from mmgen.color import purple, cyan
  7. from mmgen.util import msg, Msg, Msg_r
  8. from mmgen.devtools import Pmsg
  9. from mmgen.protocol import init_proto
  10. from mmgen.tx import CompletedTX
  11. from mmgen.proto.btc.tx.base import DeserializeTX
  12. from mmgen.rpc import rpc_init
  13. from mmgen.daemon import CoinDaemon
  14. from mmgen.tx import NewTX
  15. from ..include.common import cfg, start_test_daemons, stop_test_daemons, qmsg
  16. def print_info(name, extra_desc):
  17. if cfg.names:
  18. Msg_r('{} {} {}'.format(
  19. purple('Testing'),
  20. cyan(f'{name} ({extra_desc})'),
  21. '' if cfg.quiet else '\n'))
  22. else:
  23. Msg_r(f'Testing {extra_desc}')
  24. if not cfg.quiet:
  25. Msg('')
  26. async def test_tx(tx_proto, tx_hex, desc, n):
  27. def has_nonstandard_outputs(outputs):
  28. for o in outputs:
  29. t = o['scriptPubKey']['type']
  30. if t in ('nonstandard', 'pubkey', 'nulldata'):
  31. return True
  32. return False
  33. rpc = await rpc_init(cfg, proto=tx_proto, ignore_wallet=True)
  34. d = await rpc.call('decoderawtransaction', tx_hex)
  35. if has_nonstandard_outputs(d['vout']):
  36. return False
  37. dt = DeserializeTX(tx_proto, tx_hex)
  38. if cfg.verbose:
  39. Msg('\n\n================================ Core vector: ==================================')
  40. Msg_r('.' if cfg.quiet else f'{n:>3}) {desc}\n')
  41. if cfg.verbose:
  42. Pmsg(d)
  43. Msg('\n------------------------------ MMGen deserialized: -----------------------------')
  44. Pmsg(dt._asdict())
  45. # metadata
  46. assert dt.txid == d['txid'], 'TXID does not match'
  47. assert dt.locktime == d['locktime'], 'Locktime does not match'
  48. assert dt.version == d['version'], 'Version does not match'
  49. # inputs
  50. a, b = d['vin'], dt.txins
  51. for i in range(len(a)):
  52. assert a[i]['txid'] == b[i]['txid'], f'TxID of input {i} does not match'
  53. assert a[i]['vout'] == b[i]['vout'], f'vout of input {i} does not match'
  54. assert a[i]['sequence'] == int(b[i]['nSeq'], 16), (
  55. f'nSeq of input {i} does not match')
  56. if 'txinwitness' in a[i]:
  57. assert a[i]['txinwitness'] == b[i]['witness'], (
  58. f'witness of input {i} does not match')
  59. # outputs
  60. a, b = d['vout'], dt.txouts
  61. for i in range(len(a)):
  62. if 'addresses' in a[i]['scriptPubKey']:
  63. A = a[i]['scriptPubKey']['addresses'][0]
  64. B = b[i]['address']
  65. fs = 'address of output {} does not match\nA: {}\nB: {}'
  66. assert A == B, fs.format(i, A, B)
  67. A = tx_proto.coin_amt(a[i]['value'])
  68. B = b[i]['amt']
  69. fs = 'value of output {} does not match\nA: {}\nB: {}'
  70. assert A == B, fs.format(i, A, B)
  71. A = a[i]['scriptPubKey']['hex']
  72. B = b[i]['scriptPubKey']
  73. fs = 'scriptPubKey of output {} does not match\nA: {}\nB: {}'
  74. assert A == B, fs.format(i, A, B)
  75. async def do_mmgen_ref(daemons, fns, name, desc):
  76. # NB: remove_datadir is required here for some reason (seems to be Bitcoin Core version-dependent)
  77. start_test_daemons(*daemons, remove_datadir=True)
  78. print_info(name, desc)
  79. for n, fn in enumerate(fns):
  80. tx = await CompletedTX(cfg=cfg, filename=fn, quiet_open=True)
  81. await test_tx(
  82. tx_proto = tx.proto,
  83. tx_hex = tx.serialized,
  84. desc = fn,
  85. n = n + 1)
  86. stop_test_daemons(*daemons, remove_datadir=True)
  87. Msg('OK')
  88. return True
  89. class unit_tests:
  90. altcoin_deps = ('mmgen_ref_alt',)
  91. async def newtx(self, name, ut):
  92. qmsg(' Testing NewTX initializer')
  93. d = CoinDaemon(cfg, 'btc', test_suite=True)
  94. d.start()
  95. proto = init_proto(cfg, 'btc', need_amt=True)
  96. await NewTX(cfg=cfg, proto=proto)
  97. d.stop()
  98. d.remove_datadir()
  99. qmsg(' OK')
  100. return True
  101. async def core_vectors(self, name, ut):
  102. core_repo_root = os.getenv('CORE_REPO_ROOT')
  103. if not core_repo_root:
  104. msg('The environmental variable CORE_REPO_ROOT must be set before running this test')
  105. return False
  106. start_test_daemons('btc')
  107. fn_b = 'src/test/data/tx_valid.json'
  108. fn = os.path.join(core_repo_root, fn_b)
  109. with open(fn) as fp:
  110. core_data = json.loads(fp.read())
  111. print_info(name, 'Bitcoin Core test vectors')
  112. n = 1
  113. for e in core_data:
  114. if isinstance(e[0], list):
  115. await test_tx(
  116. tx_proto = init_proto(cfg, 'btc', need_amt=True),
  117. tx_hex = e[1],
  118. desc = desc,
  119. n = n)
  120. n += 1
  121. else:
  122. desc = e[0]
  123. Msg('OK')
  124. stop_test_daemons('btc', remove_datadir=True)
  125. return True
  126. async def mmgen_ref(self, name, ut):
  127. return await do_mmgen_ref(
  128. ('btc', 'btc_tn'),
  129. (
  130. 'test/ref/0B8D5A[15.31789,14,tl=1320969600].rawtx',
  131. 'test/ref/0C7115[15.86255,14,tl=1320969600].testnet.rawtx',
  132. 'test/ref/542169[5.68152,34].sigtx',
  133. ),
  134. name,
  135. 'MMGen reference transactions [Bitcoin]')
  136. async def mmgen_ref_alt(self, name, ut):
  137. return await do_mmgen_ref(
  138. ('ltc', 'ltc_tn', 'bch'),
  139. (
  140. 'test/ref/litecoin/AF3CDF-LTC[620.76194,1453,tl=1320969600].rawtx',
  141. 'test/ref/litecoin/A5A1E0-LTC[1454.64322,1453,tl=1320969600].testnet.rawtx',
  142. 'test/ref/460D4D-BCH[10.19764,tl=1320969600].rawtx'
  143. ),
  144. name,
  145. 'MMGen reference transactions [Altcoin]')