From ba291d47998c0621ded3b8c9e7dcef3e4c85013f Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Sat, 26 Nov 2022 18:55:56 +0000 Subject: [PATCH] tx.new.process_cmd_arg(): cleanups --- mmgen/tw/addresses.py | 9 +++------ mmgen/tx/new.py | 38 +++++++++++++++++++++--------------- test/test_py_d/ts_regtest.py | 2 +- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/mmgen/tw/addresses.py b/mmgen/tw/addresses.py index b341e0fe..783df26e 100755 --- a/mmgen/tw/addresses.py +++ b/mmgen/tw/addresses.py @@ -257,7 +257,7 @@ class TwAddresses(TwView): False: no unused addresses in wallet with requested AddrListID """ - def get_start(): + def get_start(bot,top): """ bisecting algorithm to find first entry with requested al_id @@ -265,8 +265,6 @@ class TwAddresses(TwView): stringwise, we can just search on raw twmmids. """ pre_target = al_id + ':0' - bot = 0 - top = len(data) - 1 n = top >> 1 while True: @@ -281,11 +279,10 @@ class TwAddresses(TwView): n = (top + bot) >> 1 - self.reverse = False - self.do_sort('twmmid') + assert self.sort_key == 'twmmid' data = self.data - start = get_start() + start = get_start( bot=0, top=len(data) - 1 ) if start is not None: for d in data[start:]: diff --git a/mmgen/tx/new.py b/mmgen/tx/new.py index 39613dff..90eca720 100755 --- a/mmgen/tx/new.py +++ b/mmgen/tx/new.py @@ -18,7 +18,12 @@ from .base import Base from ..color import pink,yellow from ..obj import get_obj,MMGenList from ..util import msg,qmsg,fmt,die,suf,remove_dups,get_extension -from ..addr import is_mmgen_id,CoinAddr,is_coin_addr,AddrListID,is_addrlist_id +from ..addr import ( + is_mmgen_id, + CoinAddr, + is_coin_addr, + is_addrlist_id +) def mmaddr2coinaddr(mmaddr,ad_w,ad_f,proto): @@ -168,33 +173,34 @@ class New(Base): def add_output(self,coinaddr,amt,is_chg=None): self.outputs.append(self.Output(self.proto,addr=coinaddr,amt=amt,is_chg=is_chg)) - async def process_cmd_arg(self,arg,ad_f,ad_w): + async def process_cmd_arg(self,arg_in,ad_f,ad_w): - if ',' in arg: - addr,amt = arg.split(',',1) - err_desc = 'coin argument in command-line argument' - else: - addr,amt = (arg,None) - err_desc = 'command-line argument' + arg,amt = arg_in.split(',',1) if ',' in arg_in else (arg_in,None) - if is_mmgen_id(self.proto,addr): - coin_addr = mmaddr2coinaddr(addr,ad_w,ad_f,self.proto) - elif is_coin_addr(self.proto,addr): - coin_addr = CoinAddr(self.proto,addr) - elif is_addrlist_id(self.proto,addr): + if is_mmgen_id(self.proto,arg): + coin_addr = mmaddr2coinaddr(arg,ad_w,ad_f,self.proto) + elif is_coin_addr(self.proto,arg): + coin_addr = CoinAddr(self.proto,arg) + elif is_addrlist_id(self.proto,arg): if self.proto.base_proto_coin != 'BTC': die(2,f'Change addresses not supported for {self.proto.name} protocol') + from ..tw.addresses import TwAddresses - res = (await TwAddresses(self.proto,get_data=True)).get_change_address(addr) + al = await TwAddresses(self.proto,get_data=True) + al.reverse = False + al.do_sort('twmmid') + + res = al.get_change_address(arg) + if res: coin_addr = res.addr self.chg_autoselected = True else: die(2,'Tracking wallet contains no {t}addresses from address list {a!r}'.format( t = ('unused ','')[res is None], - a = addr )) + a = arg )) else: - die(2,f'{addr}: invalid {err_desc} {{!r}}'.format(f'{addr},{amt}' if amt else addr)) + die(2,f'{arg_in}: invalid command-line argument') if not (amt or self.chg_idx is None): die(2,'ERROR: More than one change address {} on command line'.format( diff --git a/test/test_py_d/ts_regtest.py b/test/test_py_d/ts_regtest.py index 2fc9ff86..6cd6eb8c 100755 --- a/test/test_py_d/ts_regtest.py +++ b/test/test_py_d/ts_regtest.py @@ -1465,7 +1465,7 @@ class TestSuiteRegtest(TestSuiteBase,TestSuiteShared): def bob_auto_chg_bad3(self): return self._bob_auto_chg_bad( self._user_sid('bob') + ':L', - 'contains no unused addresses' ) + 'contains no unused addresses from address list' ) def stop(self): if opt.no_daemon_stop: