minor fixes and cleanups

This commit is contained in:
The MMGen Project 2026-09-09 18:33:59 +00:00
commit 2bc8a5dda4
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
5 changed files with 36 additions and 25 deletions

View file

@ -27,7 +27,11 @@ from .completed import Completed, TokenCompleted
class Unsigned(VmUnsigned, Completed, TxBase.Unsigned):
def parse_txfile_serialized_data(self):
d = self.serialized if isinstance(self.serialized, dict) else json.loads(self.serialized)
if isinstance(self.serialized, dict):
d = self.serialized
else:
d = json.loads(self.serialized)
o = {
'from': CoinAddr(self.proto, d['from']),
# NB: for token, 'to' is sendto address

View file

@ -12,6 +12,7 @@
tx.completed: completed transaction class
"""
from ..util import msg, ymsg, die
from .base import Base
class Completed(Base):
@ -36,7 +37,6 @@ class Completed(Base):
self.check_serialized_integrity()
except Exception:
from ..color import orange
from ..util import msg
msg(orange(
f'Something is wrong with transaction file ‘{filename}’\n'
'To fix this problem, please move or delete the file'))
@ -46,7 +46,6 @@ class Completed(Base):
self.check_correct_chain()
if self.check_sigs() != self.signed:
from ..util import die
die(1, 'Transaction is {}signed!'.format('not ' if self.signed else ''))
self.infile = filename
@ -95,7 +94,6 @@ class Completed(Base):
if mmid := getattr(self, 'swap_recv_addr_mmid', None):
pass
elif self.cfg.allow_non_wallet_swap:
from ..util import ymsg
ymsg('Warning: allowing swap to non-wallet address (--allow-non-wallet-swap)')
else:
raise ValueError('Swap to non-wallet address forbidden (override with --allow-non-wallet-swap)')

View file

@ -12,8 +12,8 @@
tx.unsigned: unsigned transaction class
"""
from .completed import Completed
from ..util import remove_dups
from .completed import Completed
class Unsigned(Completed):
desc = 'unsigned transaction'

View file

@ -195,21 +195,27 @@ def clean(cfgs, tmpdir_ids=None, extra_dirs=[]):
def get_tmpfile(cfg, fn):
return os.path.join(cfg['tmpdir'], fn)
def write_to_file(fn, data, binary=False):
def write_to_file(fn, data, **kwargs):
write_data_to_file(
cfg,
fn,
data,
quiet = True,
no_stdout = True,
binary = binary,
ignore_opt_outdir = True)
ignore_opt_outdir = True,
**kwargs)
def write_to_cfgfile(lines, **kwargs):
return write_to_file(
os.path.join(cfg.test_datadir, 'mmgen.cfg'),
'\n'.join(lines) + ('\n' if lines else ''),
ask_overwrite = False)
def write_to_tmpfile(cfg, fn, data, binary=False):
write_to_file(os.path.join(cfg['tmpdir'], fn), data=data, binary=binary)
write_to_file(os.path.join(cfg['tmpdir'], fn), data, binary=binary)
def read_from_file(fn, binary=False):
return get_data_from_file(cfg, fn, quiet=True, binary=binary)
def read_from_file(fn, **kwargs):
return get_data_from_file(cfg, fn, quiet=True, **kwargs)
def read_from_tmpfile(cfg, fn, binary=False):
return read_from_file(os.path.join(cfg['tmpdir'], fn), binary=binary)

View file

@ -17,8 +17,8 @@ async def do_txfile_test(desc, fns, cfg=cfg, do_format=True):
qmsg(f'\n Testing CompletedTX initializer ({desc})')
for fn in fns:
qmsg(f' parsing: {os.path.basename(fn)}')
fpath = os.path.join('test', 'ref', fn)
tx = await CompletedTX(cfg=cfg, filename=fpath, quiet_open=True)
ref_tx_path = os.path.join('test', 'ref', fn)
tx = await CompletedTX(cfg=cfg, filename=ref_tx_path, quiet_open=True)
vmsg('\n' + tx.info.format())
@ -34,16 +34,20 @@ async def do_txfile_test(desc, fns, cfg=cfg, do_format=True):
import json
from mmgen.tx.file import txfile_json_dumps
from mmgen.util import make_chksum_6
text = f.format()
with open(fpath) as fh:
text_chk = fh.read()
data_chk = json.loads(text_chk)
outputs = data_chk['MMGenTransaction']['outputs']
for n, o in enumerate(outputs):
outputs[n] = {k:v for k,v in o.items() if not (type(v) is bool and v is False)}
data_chk['chksum'] = make_chksum_6(txfile_json_dumps(data_chk['MMGenTransaction']))
text_chk_fixed = txfile_json_dumps(data_chk)
assert text == text_chk_fixed, f'\nformatted text:\n{text}\n !=\noriginal file:\n{text_chk_fixed}'
# process reference file, fixing up deprecated outputs:
with open(ref_tx_path) as fh:
ref_data = json.loads(fh.read())
ref_outputs = ref_data['MMGenTransaction']['outputs']
for n, o in enumerate(ref_outputs):
ref_outputs[n] = {k:v for k,v in o.items() if not (type(v) is bool and v is False)}
ref_data['chksum'] = make_chksum_6(txfile_json_dumps(ref_data['MMGenTransaction']))
ref_text = txfile_json_dumps(ref_data)
# process newly-formatted data:
new_text = f.format()
assert new_text == ref_text, f'\nformatted text:\n{new_text}\n !=\noriginal file:\n{ref_text}'
qmsg(' OK')
return True
@ -58,8 +62,7 @@ class unit_tests:
'tx/7A8157[6.65227,34].rawtx',
'tx/B498CE[5.55788,38].rawtx',
'tx/BB3FD2[7.57134314,123].sigtx',
'tx/0A869F[1.23456,32].regtest.asubtx',
))
'tx/0A869F[1.23456,32].regtest.asubtx'))
async def txfile_alt(self, name, ut, desc='displaying and formatting transaction files (LTC, BCH, ETH)'):
return await do_txfile_test(