ut_tx_deserialize.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env python3
  2. """
  3. test/unit_tests_d/ut_tx_deserialize: TX deserialization unit test for the MMGen suite
  4. """
  5. import os
  6. from mmgen.common import *
  7. from ..include.common import *
  8. class unit_test(object):
  9. def _get_core_repo_root(self):
  10. self.core_repo_root = os.getenv('CORE_REPO_ROOT')
  11. if not self.core_repo_root:
  12. die(1,'The environmental variable CORE_REPO_ROOT must be set before running this test')
  13. def run_test(self,name,ut):
  14. def test_tx(txhex,desc,n):
  15. def has_nonstandard_outputs(outputs):
  16. for o in outputs:
  17. t = o['scriptPubKey']['type']
  18. if t in ('nonstandard','pubkey','nulldata'):
  19. return True
  20. return False
  21. d = g.rpch.decoderawtransaction(txhex)
  22. if has_nonstandard_outputs(d['vout']): return False
  23. dt = DeserializedTX(txhex)
  24. if opt.verbose:
  25. Msg('\n====================================================')
  26. Msg_r('.' if opt.quiet else '{:>3}) {}\n'.format(n,desc))
  27. if opt.verbose:
  28. Pmsg(d)
  29. Msg('----------------------------------------------------')
  30. Pmsg(dt)
  31. # metadata
  32. assert dt['txid'] == d['txid'],'TXID does not match'
  33. assert dt['lock_time'] == d['locktime'],'Locktime does not match'
  34. assert dt['version'] == d['version'],'Version does not match'
  35. # inputs
  36. a,b = d['vin'],dt['txins']
  37. for i in range(len(a)):
  38. assert a[i]['txid'] == b[i]['txid'],'TxID of input {} does not match'.format(i)
  39. assert a[i]['vout'] == b[i]['vout'],'vout of input {} does not match'.format(i)
  40. assert a[i]['sequence'] == int(b[i]['nSeq'],16),(
  41. 'nSeq of input {} does not match'.format(i))
  42. if 'txinwitness' in a[i]:
  43. assert a[i]['txinwitness'] == b[i]['witness'],(
  44. 'witness of input {} does not match'.format(i))
  45. # outputs
  46. a,b = d['vout'],dt['txouts']
  47. for i in range(len(a)):
  48. A = a[i]['scriptPubKey']['addresses'][0]
  49. B = b[i]['address']
  50. fs = 'address of output {} does not match\nA: {}\nB: {}'
  51. assert A == B, fs.format(i,A,B)
  52. A = a[i]['value']
  53. B = b[i]['amount']
  54. fs = 'value of output {} does not match\nA: {}\nB: {}'
  55. assert A == B, fs.format(i,A,B)
  56. A = a[i]['scriptPubKey']['hex']
  57. B = b[i]['scriptPubKey']
  58. fs = 'scriptPubKey of output {} does not match\nA: {}\nB: {}'
  59. assert A == B, fs.format(i,A,B)
  60. return True
  61. def print_info(fn,extra_desc):
  62. if opt.names:
  63. Msg_r('{} {} ({}){}'.format(
  64. purple('Testing'),
  65. cyan(name),
  66. extra_desc,
  67. '' if opt.quiet else '\n'))
  68. else:
  69. Msg_r('Testing transactions from {!r}'.format(fn))
  70. if not opt.quiet: Msg('')
  71. def test_core_vectors():
  72. self._get_core_repo_root()
  73. fn_b = 'src/test/data/tx_valid.json'
  74. fn = os.path.join(self.core_repo_root,fn_b)
  75. data = json.loads(open(fn).read())
  76. print_info(fn_b,'Core test vectors')
  77. n = 1
  78. for e in data:
  79. if type(e[0]) == list:
  80. test_tx(e[1],desc,n)
  81. n += 1
  82. else:
  83. desc = e[0]
  84. Msg('OK')
  85. def test_mmgen_txs():
  86. fns = ( ('btc',False,'test/ref/0B8D5A[15.31789,14,tl=1320969600].rawtx'),
  87. ('btc',True,'test/ref/0C7115[15.86255,14,tl=1320969600].testnet.rawtx'),
  88. # ('bch',False,'test/ref/460D4D-BCH[10.19764,tl=1320969600].rawtx')
  89. )
  90. from mmgen.protocol import init_coin
  91. from mmgen.tx import MMGenTX
  92. from mmgen.daemon import CoinDaemon
  93. print_info('test/ref/*rawtx','MMGen reference transactions')
  94. for n,(coin,tn,fn) in enumerate(fns):
  95. init_coin(coin,tn)
  96. g.proto.daemon_data_dir = 'test/daemons/' + g.coin.lower()
  97. g.rpc_port = CoinDaemon(coin + ('','_tn')[tn],test_suite=True).rpc_port
  98. rpc_init(reinit=True)
  99. test_tx(MMGenTX(fn).hex,fn,n+1)
  100. init_coin('btc',False)
  101. g.rpc_port = CoinDaemon('btc',test_suite=True).rpc_port
  102. rpc_init(reinit=True)
  103. Msg('OK')
  104. from mmgen.tx import DeserializedTX
  105. import json
  106. start_test_daemons('btc','btc_tn') # ,'bch')
  107. test_mmgen_txs()
  108. test_core_vectors()
  109. stop_test_daemons('btc','btc_tn') # ,'bch')
  110. return True