131 lines
3.8 KiB
Python
Executable file
131 lines
3.8 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
"""
|
|
test/unit_tests_d/ut_tx_deserialize: TX deserialization unit test for the MMGen suite
|
|
"""
|
|
|
|
import os
|
|
from mmgen.common import *
|
|
from test.common import *
|
|
|
|
class unit_test(object):
|
|
|
|
def _get_core_repo_root(self):
|
|
self.core_repo_root = os.getenv('CORE_REPO_ROOT')
|
|
if not self.core_repo_root:
|
|
die(1,'The environmental variable CORE_REPO_ROOT must be set before running this test')
|
|
|
|
def run_test(self,name,ut):
|
|
|
|
def test_tx(txhex,desc,n):
|
|
|
|
def has_nonstandard_outputs(outputs):
|
|
for o in outputs:
|
|
t = o['scriptPubKey']['type']
|
|
if t in ('nonstandard','pubkey','nulldata'):
|
|
return True
|
|
return False
|
|
|
|
d = g.rpch.decoderawtransaction(txhex)
|
|
|
|
if has_nonstandard_outputs(d['vout']): return False
|
|
|
|
dt = DeserializedTX(txhex)
|
|
|
|
if opt.verbose:
|
|
Msg('\n====================================================')
|
|
Msg_r('.' if opt.quiet else '{:>3}) {}\n'.format(n,desc))
|
|
if opt.verbose:
|
|
Pmsg(d)
|
|
Msg('----------------------------------------------------')
|
|
Pmsg(dt)
|
|
|
|
# metadata
|
|
assert dt['txid'] == d['txid'],'TXID does not match'
|
|
assert dt['lock_time'] == d['locktime'],'Locktime does not match'
|
|
assert dt['version'] == d['version'],'Version does not match'
|
|
|
|
# inputs
|
|
a,b = d['vin'],dt['txins']
|
|
for i in range(len(a)):
|
|
assert a[i]['txid'] == b[i]['txid'],'TxID of input {} does not match'.format(i)
|
|
assert a[i]['vout'] == b[i]['vout'],'vout of input {} does not match'.format(i)
|
|
assert a[i]['sequence'] == int(b[i]['nSeq'],16),(
|
|
'nSeq of input {} does not match'.format(i))
|
|
if 'txinwitness' in a[i]:
|
|
assert a[i]['txinwitness'] == b[i]['witness'],(
|
|
'witness of input {} does not match'.format(i))
|
|
|
|
# outputs
|
|
a,b = d['vout'],dt['txouts']
|
|
for i in range(len(a)):
|
|
A = a[i]['scriptPubKey']['addresses'][0]
|
|
B = b[i]['address']
|
|
fs = 'address of output {} does not match\nA: {}\nB: {}'
|
|
assert A == B, fs.format(i,A,B)
|
|
|
|
A = a[i]['value']
|
|
B = b[i]['amount']
|
|
fs = 'value of output {} does not match\nA: {}\nB: {}'
|
|
assert A == B, fs.format(i,A,B)
|
|
|
|
A = a[i]['scriptPubKey']['hex']
|
|
B = b[i]['scriptPubKey']
|
|
fs = 'scriptPubKey of output {} does not match\nA: {}\nB: {}'
|
|
assert A == B, fs.format(i,A,B)
|
|
|
|
return True
|
|
|
|
def print_info(fn,extra_desc):
|
|
if opt.names:
|
|
Msg_r('{} {} ({}){}'.format(
|
|
purple('Testing'),
|
|
cyan(name),
|
|
extra_desc,
|
|
'' if opt.quiet else '\n'))
|
|
else:
|
|
Msg_r('Testing transactions from {!r}'.format(fn))
|
|
if not opt.quiet: Msg('')
|
|
|
|
def test_core_vectors():
|
|
self._get_core_repo_root()
|
|
fn_b = 'src/test/data/tx_valid.json'
|
|
fn = os.path.join(self.core_repo_root,fn_b)
|
|
data = json.loads(open(fn).read())
|
|
print_info(fn_b,'Core test vectors')
|
|
n = 1
|
|
for e in data:
|
|
if type(e[0]) == list:
|
|
test_tx(e[1],desc,n)
|
|
n += 1
|
|
else:
|
|
desc = e[0]
|
|
Msg('OK')
|
|
|
|
def test_mmgen_txs():
|
|
fns = ( ('btc',False,'test/ref/0B8D5A[15.31789,14,tl=1320969600].rawtx'),
|
|
('btc',True,'test/ref/0C7115[15.86255,14,tl=1320969600].testnet.rawtx'),
|
|
('bch',False,'test/ref/460D4D-BCH[10.19764,tl=1320969600].rawtx') )
|
|
from mmgen.protocol import init_coin
|
|
from mmgen.tx import MMGenTX
|
|
from mmgen.test_daemon import TestDaemon
|
|
print_info('test/ref/*rawtx','MMGen reference transactions')
|
|
for n,(coin,tn,fn) in enumerate(fns):
|
|
init_coin(coin,tn)
|
|
g.proto.daemon_data_dir = 'test/daemons/' + g.coin.lower()
|
|
g.rpc_port = TestDaemon(coin + ('','_tn')[tn]).rpc_port
|
|
rpc_init(reinit=True)
|
|
test_tx(MMGenTX(fn).hex,fn,n+1)
|
|
init_coin('btc',False)
|
|
g.rpc_port = TestDaemon('btc').rpc_port
|
|
rpc_init(reinit=True)
|
|
Msg('OK')
|
|
|
|
from mmgen.tx import DeserializedTX
|
|
import json
|
|
|
|
start_test_daemons('btc','btc_tn','bch')
|
|
test_mmgen_txs()
|
|
test_core_vectors()
|
|
stop_test_daemons('btc','btc_tn','bch')
|
|
|
|
return True
|