From f2bf16bbdbfba6155ab459c47649ed7e7c3ea32e Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Fri, 13 Oct 2023 09:51:13 +0000 Subject: [PATCH] minor fixes and cleanups --- mmgen/contrib/keccak.py | 6 +++--- mmgen/daemon.py | 2 +- mmgen/main_passgen.py | 2 +- mmgen/rpc.py | 6 ++++-- mmgen/sha2.py | 7 ++----- mmgen/tw/addresses.py | 10 ++++++---- mmgen/tw/unspent.py | 10 ++++++---- mmgen/tw/view.py | 1 + mmgen/tx/base.py | 5 +++-- mmgen/tx/file.py | 3 ++- test/include/coin_daemon_control.py | 1 - test/overlay/fakemods/mmgen/proto/btc/tw/unspent.py | 4 ++-- test/overlay/fakemods/mmgen/tw/unspent.py | 6 +++--- test/overlay/fakemods/mmgen/tx/new.py | 8 ++++---- test/unit_tests_d/ut_rpc.py | 3 +++ 15 files changed, 41 insertions(+), 33 deletions(-) diff --git a/mmgen/contrib/keccak.py b/mmgen/contrib/keccak.py index 5c82505a..95ebbaba 100755 --- a/mmgen/contrib/keccak.py +++ b/mmgen/contrib/keccak.py @@ -200,9 +200,9 @@ class KeccakState: i = 0 for y in self.rangeH: for x in self.rangeW: - v = KeccakState.lane2bytes(self.s[x][y], self.lanew) - out[i:i+8] = v - i += 8 + v = KeccakState.lane2bytes(self.s[x][y], self.lanew) + out[i:i+8] = v + i += 8 return out def set_bytes(self, bb): diff --git a/mmgen/daemon.py b/mmgen/daemon.py index e0ab15a0..6d3efe6b 100755 --- a/mmgen/daemon.py +++ b/mmgen/daemon.py @@ -83,7 +83,7 @@ class Daemon(Lockable): out = (PIPE,None)[is_daemon and self.opt.no_daemonize] try: cp = run(cmd,check=False,stdout=out,stderr=out) - except Exception as e: + except OSError as e: die( 'MMGenCalledProcessError', f'Error starting executable: {type(e).__name__} [Errno {e.errno}]' ) set_vt100() if self.debug: diff --git a/mmgen/main_passgen.py b/mmgen/main_passgen.py index 86a67025..1bb015be 100755 --- a/mmgen/main_passgen.py +++ b/mmgen/main_passgen.py @@ -28,6 +28,7 @@ from .addrlist import AddrIdxList from .passwdlist import PasswordList from .wallet import Wallet from .obj import MMGenPWIDString +from .ui import keypress_confirm pwi = PasswordList.pw_info @@ -178,7 +179,6 @@ af = al.file af.format() -from .ui import keypress_confirm if keypress_confirm( cfg, 'Encrypt password list?' ): af.encrypt() af.write(binary=True,desc='encrypted password list') diff --git a/mmgen/rpc.py b/mmgen/rpc.py index 7bdd808f..3487bac2 100755 --- a/mmgen/rpc.py +++ b/mmgen/rpc.py @@ -24,7 +24,7 @@ import sys,re,base64,json,asyncio,importlib from decimal import Decimal from collections import namedtuple -from .util import msg,die,fmt,fmt_list,pp_fmt,oneshot_warning +from .util import msg,ymsg,die,fmt,fmt_list,pp_fmt,oneshot_warning from .base_obj import AsyncInit from .obj import NonNegativeInt from .objmethods import HiliteStr,InitErrors,MMGenObject @@ -93,6 +93,7 @@ class RPCBackends: self.timeout = caller.timeout self.http_hdrs = caller.http_hdrs self.name = type(self).__name__ + self.caller = caller class aiohttp(base,metaclass=AsyncInit): """ @@ -231,7 +232,8 @@ class RPCBackends: async def run(self,payload,timeout,host_path): data = json.dumps(payload,cls=json_encoder) if len(data) > self.arg_max: - return self.httplib(payload,timeout=timeout) # TODO: check + ymsg('Warning: Curl data payload length exceeded - falling back on httplib') + return RPCBackends.httplib(self.caller).run(payload,timeout,host_path) dmsg_rpc_backend(self.host_url,host_path,payload) exec_cmd = [ 'curl', diff --git a/mmgen/sha2.py b/mmgen/sha2.py index ddb263df..0f000d64 100755 --- a/mmgen/sha2.py +++ b/mmgen/sha2.py @@ -63,13 +63,10 @@ class Sha2: cbrt = lambda n: pow(n, 1 / 3) # First wordBits bits of the fractional parts of the square roots of the first 8 primes - H = (getFractionalBits(sqrt(n)) for n in primes[:8]) + cls.H_init = tuple(getFractionalBits(sqrt(n)) for n in primes[:8]) # First wordBits bits of the fractional parts of the cube roots of the first nRounds primes - K = (getFractionalBits(cbrt(n)) for n in primes) - - cls.H_init = tuple(H) - cls.K = tuple(K) + cls.K = tuple(getFractionalBits(cbrt(n)) for n in primes) def __init__(self,message,preprocess=True): 'Use preprocess=False for Sha256Compress' diff --git a/mmgen/tw/addresses.py b/mmgen/tw/addresses.py index 5f4cb1d7..8d7045a2 100755 --- a/mmgen/tw/addresses.py +++ b/mmgen/tw/addresses.py @@ -64,10 +64,12 @@ class TwAddresses(TwView): MMGenListItem.__init__(self,**kwargs) class conv_funcs: - def amt(self,value): - return self.proto.coin_amt(value) - def recvd(self,value): - return self.proto.coin_amt(value) + @staticmethod + def amt(instance,value): + return instance.proto.coin_amt(value) + @staticmethod + def recvd(instance,value): + return instance.proto.coin_amt(value) @property def coinaddr_list(self): diff --git a/mmgen/tw/unspent.py b/mmgen/tw/unspent.py index 1a750037..5f3cf9a2 100755 --- a/mmgen/tw/unspent.py +++ b/mmgen/tw/unspent.py @@ -70,10 +70,12 @@ class TwUnspentOutputs(TwView): MMGenListItem.__init__(self,**kwargs) class conv_funcs: - def amt(self,value): - return self.proto.coin_amt(value) - def amt2(self,value): - return self.proto.coin_amt(value) + @staticmethod + def amt(instance,value): + return instance.proto.coin_amt(value) + @staticmethod + def amt2(instance,value): + return instance.proto.coin_amt(value) async def __init__(self,cfg,proto,minconf=1,addrs=[]): await super().__init__(cfg,proto) diff --git a/mmgen/tw/view.py b/mmgen/tw/view.py index 6ab25a11..a7afc78c 100755 --- a/mmgen/tw/view.py +++ b/mmgen/tw/view.py @@ -76,6 +76,7 @@ class TwView(MMGenObject,metaclass=AsyncInit): class line_processing: class print: + @staticmethod def do(method,data,cw,fs,color,fmt_method): return [l.rstrip() for l in method(data,cw,fs,color,fmt_method)] diff --git a/mmgen/tx/base.py b/mmgen/tx/base.py index b8d2d925..3c9c71eb 100755 --- a/mmgen/tx/base.py +++ b/mmgen/tx/base.py @@ -57,8 +57,9 @@ class MMGenTxIO(MMGenListItem): None ) class conv_funcs: - def amt(self,value): - return self.proto.coin_amt(value) + @staticmethod + def amt(instance,value): + return instance.proto.coin_amt(value) class MMGenTxIOList(list,MMGenObject): diff --git a/mmgen/tx/file.py b/mmgen/tx/file.py index a8fede79..be23ba29 100755 --- a/mmgen/tx/file.py +++ b/mmgen/tx/file.py @@ -63,7 +63,8 @@ class MMGenTxFile(MMGenObject): try: desc = 'data' if len(tx_data) > tx.cfg.max_tx_file_size: - die( 'MaxFileSizeExceeded', f'Transaction file size exceeds limit ({tx.cfg.max_tx_file_size} bytes)' ) + die('MaxFileSizeExceeded', + f'Transaction file size exceeds limit ({tx.cfg.max_tx_file_size} bytes)') tx_data = tx_data.splitlines() assert len(tx_data) >= 5,'number of lines less than 5' assert len(tx_data[0]) == 6,'invalid length of first line' diff --git a/test/include/coin_daemon_control.py b/test/include/coin_daemon_control.py index 94ce2c35..926ac327 100755 --- a/test/include/coin_daemon_control.py +++ b/test/include/coin_daemon_control.py @@ -12,7 +12,6 @@ test.include.coin_daemon_control: Start and stop daemons for the MMGen test suite """ -import include.test_init from mmgen.cfg import Config,gc from mmgen.util import msg,die,oneshot_warning,async_run from mmgen.protocol import init_proto diff --git a/test/overlay/fakemods/mmgen/proto/btc/tw/unspent.py b/test/overlay/fakemods/mmgen/proto/btc/tw/unspent.py index 3e6f8ab8..7632e7b0 100644 --- a/test/overlay/fakemods/mmgen/proto/btc/tw/unspent.py +++ b/test/overlay/fakemods/mmgen/proto/btc/tw/unspent.py @@ -3,7 +3,7 @@ from .unspent_orig import * if overlay_fake_os.getenv('MMGEN_BOGUS_UNSPENT_DATA'): - class overlay_fake_data: + class overlay_fake_BitcoinTwUnspentOutputs(BitcoinTwUnspentOutputs): async def get_rpc_data(self): from decimal import Decimal @@ -14,4 +14,4 @@ if overlay_fake_os.getenv('MMGEN_BOGUS_UNSPENT_DATA'): overlay_fake_os.getenv('MMGEN_BOGUS_UNSPENT_DATA') ), parse_float=Decimal) - BitcoinTwUnspentOutputs.get_rpc_data = overlay_fake_data.get_rpc_data + BitcoinTwUnspentOutputs.get_rpc_data = overlay_fake_BitcoinTwUnspentOutputs.get_rpc_data diff --git a/test/overlay/fakemods/mmgen/tw/unspent.py b/test/overlay/fakemods/mmgen/tw/unspent.py index ca508f03..f0cfb8b3 100644 --- a/test/overlay/fakemods/mmgen/tw/unspent.py +++ b/test/overlay/fakemods/mmgen/tw/unspent.py @@ -3,10 +3,10 @@ from .unspent_orig import * if overlay_fake_os.getenv('MMGEN_BOGUS_UNSPENT_DATA'): - class overlay_fake_data: + class overlay_fake_TwUnspentOutputs(TwUnspentOutputs): - async def set_dates(_,us): + async def set_dates(self,us): for o in us: o.date = 1831006505 - int(9.7 * 60 * (o.confs - 1)) - TwUnspentOutputs.set_dates = overlay_fake_data.set_dates + TwUnspentOutputs.set_dates = overlay_fake_TwUnspentOutputs.set_dates diff --git a/test/overlay/fakemods/mmgen/tx/new.py b/test/overlay/fakemods/mmgen/tx/new.py index 98867fe0..59d9697a 100644 --- a/test/overlay/fakemods/mmgen/tx/new.py +++ b/test/overlay/fakemods/mmgen/tx/new.py @@ -3,10 +3,10 @@ from .new_orig import * if overlay_fake_os.getenv('MMGEN_BOGUS_UNSPENT_DATA'): - class overlay_fake_data: + class overlay_fake_New(New): - async def warn_chg_addr_used(_,us): + async def warn_chg_addr_used(self,_): from ..util import ymsg - ymsg('Bogus unspent data: skipping change address is used check') + ymsg('Bogus unspent data: skipping used change address check') - New.warn_chg_addr_used = overlay_fake_data.warn_chg_addr_used + New.warn_chg_addr_used = overlay_fake_New.warn_chg_addr_used diff --git a/test/unit_tests_d/ut_rpc.py b/test/unit_tests_d/ut_rpc.py index 87db6a5b..4b71fc0b 100755 --- a/test/unit_tests_d/ut_rpc.py +++ b/test/unit_tests_d/ut_rpc.py @@ -80,6 +80,7 @@ def do_msg(rpc,backend): class init_test: + @staticmethod async def btc(proto,backend,daemon): rpc = await rpc_init(cfg,proto,backend,daemon) do_msg(rpc,backend) @@ -89,6 +90,7 @@ class init_test: await rpc.gathered_call(None,(('getblock',(bh,)),('getblock',(bh,1))),timeout=300) return rpc + @staticmethod async def bch(proto,backend,daemon): rpc = await rpc_init(cfg,proto,backend,daemon) do_msg(rpc,backend) @@ -96,6 +98,7 @@ class init_test: ltc = bch + @staticmethod async def eth(proto,backend,daemon): rpc = await rpc_init(cfg,proto,backend,daemon) do_msg(rpc,backend)