From f45c140350867ca3c22528cbdf643fd132388ff5 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Wed, 16 Nov 2022 17:56:05 +0000 Subject: [PATCH] tx.get_chg_output_idx() -> tx.chg_idx --- mmgen/proto/btc/tx/completed.py | 2 +- mmgen/proto/btc/tx/new.py | 9 +++++---- mmgen/tx/base.py | 22 +++++++++++++++++----- mmgen/tx/bump.py | 5 +++-- mmgen/tx/new.py | 10 +++++----- 5 files changed, 31 insertions(+), 17 deletions(-) diff --git a/mmgen/proto/btc/tx/completed.py b/mmgen/proto/btc/tx/completed.py index 4221fc0f..6d8a3591 100755 --- a/mmgen/proto/btc/tx/completed.py +++ b/mmgen/proto/btc/tx/completed.py @@ -65,7 +65,7 @@ class Completed(Base,TxBase.Completed): @property def send_amt(self): return self.sum_outputs( - exclude = None if len(self.outputs) == 1 else self.get_chg_output_idx() + exclude = None if len(self.outputs) == 1 else self.chg_idx ) def check_txfile_hex_data(self): diff --git a/mmgen/proto/btc/tx/new.py b/mmgen/proto/btc/tx/new.py index 25b52e46..bb1e24eb 100755 --- a/mmgen/proto/btc/tx/new.py +++ b/mmgen/proto/btc/tx/new.py @@ -86,12 +86,13 @@ class New(Base,TxBase.New): return [] def update_change_output(self,funds_left): - chg_idx = self.get_chg_output_idx() - if funds_left == 0: + if funds_left == 0: # TODO: test msg(self.no_chg_msg) - self.outputs.pop(chg_idx) + self.outputs.pop(self.chg_idx) else: - self.update_output_amt(chg_idx,self.proto.coin_amt(funds_left)) + self.update_output_amt( + self.chg_idx, + self.proto.coin_amt(funds_left) ) def check_fee(self): fee = self.sum_inputs() - self.sum_outputs() diff --git a/mmgen/tx/base.py b/mmgen/tx/base.py index 3d5db4eb..3a618dfc 100755 --- a/mmgen/tx/base.py +++ b/mmgen/tx/base.py @@ -142,12 +142,24 @@ class Base(MMGenObject): return self.proto.coin_amt('0') return self.proto.coin_amt(sum(e.amt for e in olist)) - def get_chg_output_idx(self): - ch_ops = [x.is_chg for x in self.outputs] - try: - return ch_ops.index(True) - except ValueError: + def _chg_output_ops(self,op): + is_chgs = [x.is_chg for x in self.outputs] + if is_chgs.count(True) == 1: + return ( + is_chgs.index(True) if op == 'idx' else + self.outputs[is_chgs.index(True)] ) + elif is_chgs.count(True) == 0: return None + else: + raise ValueError('more than one change output!') + + @property + def chg_idx(self): + return self._chg_output_ops('idx') + + @property + def chg_output(self): + return self._chg_output_ops('output') def add_timestamp(self): self.timestamp = make_timestamp() diff --git a/mmgen/tx/bump.py b/mmgen/tx/bump.py index 3e08f4f8..c666ea5c 100755 --- a/mmgen/tx/bump.py +++ b/mmgen/tx/bump.py @@ -42,8 +42,6 @@ class Bump(Completed,New): f'All outputs contain less than the minimum fee ({self.min_fee} {self.coin})') def choose_output(self): - chg_idx = self.get_chg_output_idx() - init_reply = opt.output_to_reduce def check_sufficient_funds(o_amt): if o_amt < self.min_fee: @@ -58,6 +56,9 @@ class Bump(Completed,New): else: die(1,'Insufficient funds to bump transaction') + init_reply = opt.output_to_reduce + chg_idx = self.chg_idx + while True: if init_reply == None: from ..ui import line_input diff --git a/mmgen/tx/new.py b/mmgen/tx/new.py index 526c6c6f..a81d1db3 100755 --- a/mmgen/tx/new.py +++ b/mmgen/tx/new.py @@ -183,7 +183,7 @@ class New(Base): else: die(2,f'{addr}: invalid {err_desc} {{!r}}'.format(f'{addr},{amt}' if amt else addr)) - if not amt and self.get_chg_output_idx() is not None: + if not (amt or self.chg_idx is None): die(2,'ERROR: More than one change address {} on command line'.format( 'requested' if self.chg_autoselected else 'listed')) @@ -194,7 +194,7 @@ class New(Base): for a in cmd_args: await self.process_cmd_arg(a,ad_f,ad_w) - if self.get_chg_output_idx() == None: + if self.chg_idx is None: die(2,( fmt( self.msg_no_change_output.format(self.dcoin) ).strip() if len(self.outputs) == 1 else @@ -235,9 +235,9 @@ class New(Base): self.add_mmaddrs_to_outputs(ad_w,ad_f) self.check_dup_addrs('outputs') - chg_idx = self.get_chg_output_idx() - if chg_idx is not None: - await self.warn_chg_addr_used(self.outputs[chg_idx]) + if self.chg_output is not None: + if len(self.outputs) > 1: + await self.warn_chg_addr_used(self.chg_output) async def warn_chg_addr_used(self,chg): from ..tw.addresses import TwAddresses